123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- public class RemoteCam : MonoBehaviour
- {
- public const string CAM_URI_INTERNAL = "http://192.168.1.254";
- public const string CAM_REC_START = "/?custom=1&cmd=2001&par=1";
- public const string CAM_REC_STOP = "/?custom=1&cmd=2001&par=0";
- public static bool waitingRemoteCamAnswer = false;
- public static Dictionary<uint, string> camQueryBuffer = new Dictionary<uint, string>();
- public static string camUriExternal = "";
- public Text camIPInputField;
- //public Button camConnectButton, camDisconnectButton, camRecStartButton, camRecStopButton;
- void Start()
- {
- //camConnectButton.onClick.AddListener(CamConnectButton_OnClick);
- //camDisconnectButton.onClick.AddListener(CamDisconnectButton_OnClick);
- //camRecStartButton.onClick.AddListener(CamRecStartButton_OnClick);
- //camRecStopButton.onClick.AddListener(CamRecStopButton_OnClick);
- }
- private void CamConnectButton_OnClick()
- {
- string camIP = "http://" + camIPInputField.text;
- if (camIPInputField.text != "")
- {
- Debug.Log("Connecting to remote camera, address: " + camIP);
- //ANutils.Main.RemoteCamConnection("CONNECT", camIP);
- }
- else
- Debug.Log("Incorrect remote camera address: " + camIP);
- }
- private void CamDisconnectButton_OnClick()
- {
- }
- private void CamRecStartButton_OnClick()
- {
- CamRecStart();
- }
- private void CamRecStopButton_OnClick()
- {
- CamRecStop();
- }
- public void CamRecStart()
- {
- if (!waitingRemoteCamAnswer)
- {
- camQueryBuffer.Add(0, CAM_REC_START);
- RemoteCamAPI(camUriExternal, camQueryBuffer);
- }
- else
- Debug.Log("CamRecStart: query aborted, another query in process...");
- }
- public void CamRecStop()
- {
- if (!waitingRemoteCamAnswer)
- {
- camQueryBuffer.Add(0, CAM_REC_STOP);
- RemoteCamAPI(camUriExternal, camQueryBuffer);
- }
- else
- Debug.Log("CamRecStop: query aborted, another query in process...");
- }
- public void CamRecStopStart()
- {
- if (!waitingRemoteCamAnswer)
- {
- camQueryBuffer.Add(0, CAM_REC_STOP);
- camQueryBuffer.Add(1, CAM_REC_START);
- RemoteCamAPI(camUriExternal, camQueryBuffer);
- }
- else
- Debug.Log("CamRecStopStart: query aborted, another query in process...");
- }
- public void RemoteCamAPI(string camURI, Dictionary<uint, string> queryBuffer)
- {
- StartCoroutine(RemoteCamAPIAsync(camURI, queryBuffer));
- }
- private IEnumerator RemoteCamAPIAsync(string camURI, Dictionary<uint, string> queryBuffer)
- {
- if (camURI != "")
- {
- foreach (KeyValuePair<uint, string> camCommand in queryBuffer)
- {
- Debug.Log("RemoteCam Send: " + camURI + camCommand.Value);
- waitingRemoteCamAnswer = true;
- using (UnityWebRequest uwr = UnityWebRequest.Get("http://" + camURI + camCommand.Value))
- {
- yield return uwr.SendWebRequest();
- if (uwr.isNetworkError || uwr.isHttpError)
- {
- Debug.Log("RemoteCam Error: " + uwr.error);
- waitingRemoteCamAnswer = false;
- camQueryBuffer.Clear();
- yield break;
- }
- else
- {
- Debug.Log("RemoteCam Response: " + uwr.downloadHandler.text);
- waitingRemoteCamAnswer = false;
- }
- }
- }
- }
- else
- Debug.Log("RemoteCam: IP address is empty.");
- camQueryBuffer.Clear();
- }
- }
|