CamFiRESTManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Text;
  6. using BestHTTP;
  7. using UnityEngine.Events;
  8. namespace MartellGroupPhoto
  9. {
  10. /// <summary>
  11. /// CamFiREST控制模块
  12. /// 记得退订事件
  13. /// </summary>
  14. public class CamFiRESTManager : MonoBehaviour
  15. {
  16. public static CamFiRESTManager Instance;
  17. // ==================================================
  18. private void Awake()
  19. {
  20. Instance = this;
  21. }
  22. private void Start()
  23. {
  24. Init();
  25. }
  26. // ==================================================
  27. private void Init()
  28. {
  29. StartCoroutine("KeepCameraAlive");
  30. return;
  31. }
  32. // ==================================================
  33. // 拍照
  34. public event UnityAction OnTakePicture;
  35. public void CamFiTakePicture()
  36. {
  37. HTTPRequest request = new HTTPRequest(
  38. new Uri(CamFiStorage.TakePic),
  39. HTTPMethods.Get,
  40. TakePictureCallback);
  41. request.Send();
  42. return;
  43. }
  44. private void TakePictureCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
  45. {
  46. if (httpResponse == null)
  47. {
  48. Debug.Log("[CamFiManager]请求无响应...[Error]");
  49. return;
  50. }
  51. if (httpResponse.StatusCode == 200)
  52. {
  53. Debug.Log("[CamFiManager]拍照成功...[Done]");
  54. if (OnTakePicture != null)
  55. {
  56. OnTakePicture();
  57. }
  58. }
  59. else
  60. {
  61. Debug.Log("[CamFiManager]拍照失败...[Done]");
  62. }
  63. return;
  64. }
  65. // ==================================================
  66. // 查询照片列表
  67. // 通常会获取最新照片index-1
  68. // 请勿使用
  69. public event UnityAction<string> OnGetFileList;
  70. public void CamFiGetFileList()
  71. {
  72. HTTPRequest request = new HTTPRequest(
  73. new Uri(CamFiStorage.GetFileList),
  74. HTTPMethods.Get,
  75. GetFileListCallback);
  76. request.Send();
  77. return;
  78. }
  79. private void GetFileListCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
  80. {
  81. if (httpResponse == null)
  82. {
  83. Debug.Log("[CamFiManager]请求无响应...[Error]");
  84. return;
  85. }
  86. if (httpResponse.StatusCode == 200)
  87. {
  88. string pictureListText = httpResponse.DataAsText.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\"", string.Empty);
  89. string[] pictureList = pictureListText.Split(",");
  90. foreach (string pictureName in pictureList)
  91. {
  92. Debug.Log($"[CamFiManager]{pictureName}...[Debug]");
  93. }
  94. Debug.Log("[CamFiManager]获取照片列表成功...[Done]");
  95. if (OnGetFileList != null) // 从订阅传出最新照片
  96. {
  97. OnGetFileList(pictureList[pictureList.Length - 1]);
  98. }
  99. }
  100. else
  101. {
  102. Debug.Log("[CamFiManager]获取照片列表失败...[Done]");
  103. }
  104. return;
  105. }
  106. // ==================================================
  107. // 获取照片原图
  108. public event UnityAction<Texture2D> OnGetRawFile;
  109. public void CamFiGetRawFile(string fileName)
  110. {
  111. fileName = fileName.Replace("/", "%2F");
  112. HTTPRequest request = new HTTPRequest(
  113. new Uri(CamFiStorage.GetRawFile + fileName),
  114. HTTPMethods.Get,
  115. GetRawFileCallback);
  116. request.Send();
  117. return;
  118. }
  119. private void GetRawFileCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
  120. {
  121. if (httpResponse == null)
  122. {
  123. Debug.Log("[CamFiManager]请求无响应...[Error]");
  124. return;
  125. }
  126. if (httpResponse.StatusCode == 200)
  127. {
  128. Debug.Log("[CamFiManager]获取照片成功...[Done]");
  129. if (OnGetRawFile != null) // 从订阅传出最新照片
  130. {
  131. OnGetRawFile(httpResponse.DataAsTexture2D);
  132. }
  133. }
  134. else
  135. {
  136. Debug.Log("[CamFiManager]获取照片失败...[Done]");
  137. }
  138. return;
  139. }
  140. // ==================================================
  141. // 实时取景
  142. public event UnityAction<bool> OnLiveView;
  143. public void CamFiLiveView(bool value)
  144. {
  145. string liveViewState;
  146. if (value)
  147. {
  148. liveViewState = CamFiStorage.StartLiveView;
  149. }
  150. else
  151. {
  152. liveViewState = CamFiStorage.StopLiveView;
  153. }
  154. HTTPRequest request = new HTTPRequest(
  155. new Uri(liveViewState),
  156. HTTPMethods.Get,
  157. LiveViewCallback);
  158. request.Send();
  159. return;
  160. }
  161. private void LiveViewCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
  162. {
  163. if (httpResponse == null)
  164. {
  165. Debug.Log("[CamFiManager]请求无响应...[Error]");
  166. return;
  167. }
  168. if (httpResponse.StatusCode == 200)
  169. {
  170. Debug.Log("[CamFiManager]取景视频流开启/关闭成功...[Done]");
  171. CamFiLiveViewManager.Instance.StartLiveView();
  172. if (OnLiveView != null)
  173. {
  174. OnLiveView(true);
  175. }
  176. }
  177. else
  178. {
  179. Debug.Log("[CamFiManager]取景视频流开启/关闭失败...[Done]");
  180. }
  181. return;
  182. }
  183. // ==================================================
  184. // oi,别tm睡着了
  185. private IEnumerator KeepCameraAlive()
  186. {
  187. HTTPRequest httpRequest = new HTTPRequest(
  188. new Uri(CamFiStorage.Live),
  189. HTTPMethods.Get);
  190. httpRequest.Send();
  191. yield return new WaitForSeconds(60f);
  192. StartCoroutine("KeepCameraAlive");
  193. yield break;
  194. }
  195. // ==================================================
  196. // // 参考
  197. // private IEnumerator KeepCameraAlive()
  198. // {
  199. // HTTPRequest httpRequest = new HTTPRequest(
  200. // new Uri($"{camFiAddress}{CamFiStorage.Live}"),
  201. // HTTPMethods.Get,
  202. // (HTTPRequest httpRequest, HTTPResponse httpResponse) =>
  203. // {
  204. // // Debug.Log(httpResponse.StatusCode);
  205. // });
  206. // yield return httpRequest.Send();
  207. // Debug.Log(httpRequest.Response.StatusCode);
  208. // yield return new WaitForSeconds(60f);
  209. // StartCoroutine("KeepCameraAlive");
  210. // yield break;
  211. // }
  212. // http://192.168.50.101/raw/%2Fstore_00020001%2FDCIM%2F100EOS5D%2F6A0A7754.JPG
  213. }
  214. }