Advertisement
stormym

clickshare

Oct 31st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Crestron.SimplSharp;
  8. using Crestron.SimplSharp.CrestronXml;
  9. using Crestron.SimplSharp.Net.Https;
  10. using Crestron.SimplSharp.Net;
  11. using Crestron.SimplSharpPro; // For Basic SIMPL#Pro classes
  12. using Crestron.SimplSharpPro.CrestronThread; // For Threading
  13. using Newtonsoft.Json;
  14. using Newtonsoft.Json.Converters;
  15.  
  16.  
  17. namespace MF.Sources.ClickShare
  18. {
  19. public class ClickShare
  20. {
  21. JsonCollector JC = new JsonCollector();
  22.  
  23. private string baseURL = string.Empty;
  24. protected string password = "integrator";
  25.  
  26. #region Commands
  27. public enum eFunction { Device = 0, Buttons, Configuration, Display, Network, OnScreenText, ClientAccess, Standby, NULL };
  28. private string[] sFunction = { "DeviceInfo", "Buttons", "Configuration", "Display", "Network", "OnScreenText", "ClientAccess", "Standby", string.Empty };
  29.  
  30. public enum eDisplayMode { Clone = 0, Extended };
  31.  
  32. private Dictionary<eDisplayMode, string> displayMode = new Dictionary<eDisplayMode,string>()
  33. {
  34. { eDisplayMode.Clone, "value=Clone"},
  35. { eDisplayMode.Extended, "value=Extended"}
  36. };
  37. #endregion
  38.  
  39.  
  40. #region Inits
  41. /// <summary>
  42. /// Constructor using default password
  43. /// </summary>
  44. /// <param name="unitIP">ClickShare IP Address</param>
  45. public ClickShare(string unitIP)
  46. {
  47. try
  48. {
  49. IPAddress.Parse(unitIP); //Make sure it is a proper IP
  50.  
  51. baseURL = "https://" + unitIP + ":" + 4001 + "/v1.10/";
  52. }
  53. catch (Exception e)
  54. {
  55. CrestronConsole.PrintLine("Sources.ClickShare -> Error in Constructor: {0}", e.Message);
  56. }
  57. }
  58.  
  59. /// <summary>
  60. /// Constructor using user configured integrator password
  61. /// </summary>
  62. /// <param name="unitIP">ClickShare IP Address</param>
  63. /// <param name="integratorPassword">ClickShare Integrator Account Password</param>
  64. public ClickShare(string unitIP, string integratorPassword)
  65. {
  66. try
  67. {
  68. IPAddress.Parse(unitIP); //Make sure it is a proper IP
  69.  
  70. baseURL = "https://" + unitIP + ":" + 4001 + "/v1.10/";
  71. password = integratorPassword;
  72. }
  73. catch (Exception e)
  74. {
  75. CrestronConsole.PrintLine("Sources.ClickShare -> Error in Constructor: {0}", e.Message);
  76. }
  77. }
  78.  
  79. #endregion
  80.  
  81. public void GetValue(eFunction f)
  82. {
  83. //JC.Connect(baseURL + "DeviceInfo", password, RequestType.Get);
  84. JC.Connect(baseURL + sFunction[(int)f],"", password, RequestType.Get);
  85. }
  86.  
  87. public void Reboot()
  88. {
  89. JC.Connect(baseURL + sFunction[(int)eFunction.Configuration] + "/RestartSystem", "value=true", password, RequestType.Put);
  90. }
  91. }
  92.  
  93. public class JsonCollector
  94. {
  95. public void Connect(string url, string content, string password, RequestType type)
  96. {
  97. HttpsClient myJsonClient = new HttpsClient(); // Create the client
  98. HttpsClientResponse myJsonResponse; // Create a holder for the response
  99. HttpsClientRequest myJsonRequest; // Create the request
  100.  
  101. myJsonClient.TimeoutEnabled = true; // HttpClient settings these can be left at default or setup as needed
  102. myJsonClient.Timeout = 5;
  103. myJsonClient.KeepAlive = false;
  104. myJsonClient.UserName = "integrator"; // default username;
  105. myJsonClient.Password = password; // defined password;
  106. myJsonClient.AuthenticationMethod = AuthMethod.BASIC; // basic Authentication
  107. myJsonClient.PeerVerification = false; // ignore SSL certificate peer errors
  108. myJsonClient.HostVerification = false; // ignore SSL certificate host errors
  109.  
  110. myJsonRequest = new HttpsClientRequest(); // Instanciate the request
  111. myJsonRequest.RequestType = type; // sets the request type
  112. myJsonRequest.ContentString = content; // sets the content if any
  113. myJsonRequest.Url.Parse(url); // take the full URL and break it down for the request to decide how to handle it
  114.  
  115. try
  116. {
  117. CrestronConsole.PrintLine("Request: {0} Content: {1}", url, content);
  118. myJsonResponse = myJsonClient.Dispatch(myJsonRequest); // This is when we send the message and the response catches the reply.
  119. CrestronConsole.PrintLine("JSON RESPONSE -> " + myJsonResponse.ContentString);
  120. Deserialize(myJsonResponse.ContentString);
  121. }
  122. catch (Exception e)
  123. {
  124. CrestronConsole.PrintLine("JSON REQUEST ERROR -> " + e.Message);
  125. }
  126.  
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement