| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.Text;
- using BestHTTP;
- using UnityEngine.Events;
- namespace MartellGroupPhoto
- {
- /// <summary>
- /// CamFiREST控制模块
- /// 记得退订事件
- /// </summary>
- public class CamFiRESTManager : MonoBehaviour
- {
- public static CamFiRESTManager Instance;
- // ==================================================
- private void Awake()
- {
- Instance = this;
- }
- private void Start()
- {
- Init();
- }
- // ==================================================
- private void Init()
- {
- StartCoroutine("KeepCameraAlive");
- return;
- }
- // ==================================================
- // 拍照
- public event UnityAction OnTakePicture;
- public void CamFiTakePicture()
- {
- HTTPRequest request = new HTTPRequest(
- new Uri(CamFiStorage.TakePic),
- HTTPMethods.Get,
- TakePictureCallback);
- request.Send();
- return;
- }
- private void TakePictureCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
- {
- if (httpResponse == null)
- {
- Debug.Log("[CamFiManager]请求无响应...[Error]");
- return;
- }
- if (httpResponse.StatusCode == 200)
- {
- Debug.Log("[CamFiManager]拍照成功...[Done]");
- if (OnTakePicture != null)
- {
- OnTakePicture();
- }
- }
- else
- {
- Debug.Log("[CamFiManager]拍照失败...[Done]");
- }
- return;
- }
- // ==================================================
- // 查询照片列表
- // 通常会获取最新照片index-1
- // 请勿使用
- public event UnityAction<string> OnGetFileList;
- public void CamFiGetFileList()
- {
- HTTPRequest request = new HTTPRequest(
- new Uri(CamFiStorage.GetFileList),
- HTTPMethods.Get,
- GetFileListCallback);
- request.Send();
- return;
- }
- private void GetFileListCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
- {
- if (httpResponse == null)
- {
- Debug.Log("[CamFiManager]请求无响应...[Error]");
- return;
- }
- if (httpResponse.StatusCode == 200)
- {
- string pictureListText = httpResponse.DataAsText.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\"", string.Empty);
- string[] pictureList = pictureListText.Split(",");
- foreach (string pictureName in pictureList)
- {
- Debug.Log($"[CamFiManager]{pictureName}...[Debug]");
- }
- Debug.Log("[CamFiManager]获取照片列表成功...[Done]");
- if (OnGetFileList != null) // 从订阅传出最新照片
- {
- OnGetFileList(pictureList[pictureList.Length - 1]);
- }
- }
- else
- {
- Debug.Log("[CamFiManager]获取照片列表失败...[Done]");
- }
- return;
- }
- // ==================================================
- // 获取照片原图
- public event UnityAction<Texture2D> OnGetRawFile;
- public void CamFiGetRawFile(string fileName)
- {
- fileName = fileName.Replace("/", "%2F");
- HTTPRequest request = new HTTPRequest(
- new Uri(CamFiStorage.GetRawFile + fileName),
- HTTPMethods.Get,
- GetRawFileCallback);
- request.Send();
- return;
- }
- private void GetRawFileCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
- {
- if (httpResponse == null)
- {
- Debug.Log("[CamFiManager]请求无响应...[Error]");
- return;
- }
- if (httpResponse.StatusCode == 200)
- {
- Debug.Log("[CamFiManager]获取照片成功...[Done]");
- if (OnGetRawFile != null) // 从订阅传出最新照片
- {
- OnGetRawFile(httpResponse.DataAsTexture2D);
- }
- }
- else
- {
- Debug.Log("[CamFiManager]获取照片失败...[Done]");
- }
- return;
- }
- // ==================================================
- // 实时取景
- public event UnityAction<bool> OnLiveView;
- public void CamFiLiveView(bool value)
- {
- string liveViewState;
- if (value)
- {
- liveViewState = CamFiStorage.StartLiveView;
- }
- else
- {
- liveViewState = CamFiStorage.StopLiveView;
- }
- HTTPRequest request = new HTTPRequest(
- new Uri(liveViewState),
- HTTPMethods.Get,
- LiveViewCallback);
- request.Send();
- return;
- }
- private void LiveViewCallback(HTTPRequest httpRequest, HTTPResponse httpResponse)
- {
- if (httpResponse == null)
- {
- Debug.Log("[CamFiManager]请求无响应...[Error]");
- return;
- }
- if (httpResponse.StatusCode == 200)
- {
- Debug.Log("[CamFiManager]取景视频流开启/关闭成功...[Done]");
- CamFiLiveViewManager.Instance.StartLiveView();
- if (OnLiveView != null)
- {
- OnLiveView(true);
- }
- }
- else
- {
- Debug.Log("[CamFiManager]取景视频流开启/关闭失败...[Done]");
- }
- return;
- }
- // ==================================================
- // oi,别tm睡着了
- private IEnumerator KeepCameraAlive()
- {
- HTTPRequest httpRequest = new HTTPRequest(
- new Uri(CamFiStorage.Live),
- HTTPMethods.Get);
- httpRequest.Send();
- yield return new WaitForSeconds(60f);
- StartCoroutine("KeepCameraAlive");
- yield break;
- }
- // ==================================================
- // // 参考
- // private IEnumerator KeepCameraAlive()
- // {
- // HTTPRequest httpRequest = new HTTPRequest(
- // new Uri($"{camFiAddress}{CamFiStorage.Live}"),
- // HTTPMethods.Get,
- // (HTTPRequest httpRequest, HTTPResponse httpResponse) =>
- // {
- // // Debug.Log(httpResponse.StatusCode);
- // });
- // yield return httpRequest.Send();
- // Debug.Log(httpRequest.Response.StatusCode);
- // yield return new WaitForSeconds(60f);
- // StartCoroutine("KeepCameraAlive");
- // yield break;
- // }
- // http://192.168.50.101/raw/%2Fstore_00020001%2FDCIM%2F100EOS5D%2F6A0A7754.JPG
- }
- }
|