RemoteCam.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. public class RemoteCam : MonoBehaviour
  7. {
  8. public const string CAM_URI_INTERNAL = "http://192.168.1.254";
  9. public const string CAM_REC_START = "/?custom=1&cmd=2001&par=1";
  10. public const string CAM_REC_STOP = "/?custom=1&cmd=2001&par=0";
  11. public static bool waitingRemoteCamAnswer = false;
  12. public static Dictionary<uint, string> camQueryBuffer = new Dictionary<uint, string>();
  13. public static string camUriExternal = "";
  14. public Text camIPInputField;
  15. //public Button camConnectButton, camDisconnectButton, camRecStartButton, camRecStopButton;
  16. void Start()
  17. {
  18. //camConnectButton.onClick.AddListener(CamConnectButton_OnClick);
  19. //camDisconnectButton.onClick.AddListener(CamDisconnectButton_OnClick);
  20. //camRecStartButton.onClick.AddListener(CamRecStartButton_OnClick);
  21. //camRecStopButton.onClick.AddListener(CamRecStopButton_OnClick);
  22. }
  23. private void CamConnectButton_OnClick()
  24. {
  25. string camIP = "http://" + camIPInputField.text;
  26. if (camIPInputField.text != "")
  27. {
  28. Debug.Log("Connecting to remote camera, address: " + camIP);
  29. //ANutils.Main.RemoteCamConnection("CONNECT", camIP);
  30. }
  31. else
  32. Debug.Log("Incorrect remote camera address: " + camIP);
  33. }
  34. private void CamDisconnectButton_OnClick()
  35. {
  36. }
  37. private void CamRecStartButton_OnClick()
  38. {
  39. CamRecStart();
  40. }
  41. private void CamRecStopButton_OnClick()
  42. {
  43. CamRecStop();
  44. }
  45. public void CamRecStart()
  46. {
  47. if (!waitingRemoteCamAnswer)
  48. {
  49. camQueryBuffer.Add(0, CAM_REC_START);
  50. RemoteCamAPI(camUriExternal, camQueryBuffer);
  51. }
  52. else
  53. Debug.Log("CamRecStart: query aborted, another query in process...");
  54. }
  55. public void CamRecStop()
  56. {
  57. if (!waitingRemoteCamAnswer)
  58. {
  59. camQueryBuffer.Add(0, CAM_REC_STOP);
  60. RemoteCamAPI(camUriExternal, camQueryBuffer);
  61. }
  62. else
  63. Debug.Log("CamRecStop: query aborted, another query in process...");
  64. }
  65. public void CamRecStopStart()
  66. {
  67. if (!waitingRemoteCamAnswer)
  68. {
  69. camQueryBuffer.Add(0, CAM_REC_STOP);
  70. camQueryBuffer.Add(1, CAM_REC_START);
  71. RemoteCamAPI(camUriExternal, camQueryBuffer);
  72. }
  73. else
  74. Debug.Log("CamRecStopStart: query aborted, another query in process...");
  75. }
  76. public void RemoteCamAPI(string camURI, Dictionary<uint, string> queryBuffer)
  77. {
  78. StartCoroutine(RemoteCamAPIAsync(camURI, queryBuffer));
  79. }
  80. private IEnumerator RemoteCamAPIAsync(string camURI, Dictionary<uint, string> queryBuffer)
  81. {
  82. if (camURI != "")
  83. {
  84. foreach (KeyValuePair<uint, string> camCommand in queryBuffer)
  85. {
  86. Debug.Log("RemoteCam Send: " + camURI + camCommand.Value);
  87. waitingRemoteCamAnswer = true;
  88. using (UnityWebRequest uwr = UnityWebRequest.Get("http://" + camURI + camCommand.Value))
  89. {
  90. yield return uwr.SendWebRequest();
  91. if (uwr.isNetworkError || uwr.isHttpError)
  92. {
  93. Debug.Log("RemoteCam Error: " + uwr.error);
  94. waitingRemoteCamAnswer = false;
  95. camQueryBuffer.Clear();
  96. yield break;
  97. }
  98. else
  99. {
  100. Debug.Log("RemoteCam Response: " + uwr.downloadHandler.text);
  101. waitingRemoteCamAnswer = false;
  102. }
  103. }
  104. }
  105. }
  106. else
  107. Debug.Log("RemoteCam: IP address is empty.");
  108. camQueryBuffer.Clear();
  109. }
  110. }