Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using Crestron.SimplSharp;
- using Crestron.SimplSharp.CrestronXml;
- using Crestron.SimplSharp.Net.Https;
- using Crestron.SimplSharp.Net;
- using Crestron.SimplSharpPro; // For Basic SIMPL#Pro classes
- using Crestron.SimplSharpPro.CrestronThread; // For Threading
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- namespace MF.Sources.ClickShare
- {
- public class ClickShare
- {
- JsonCollector JC = new JsonCollector();
- private string baseURL = string.Empty;
- protected string password = "integrator";
- #region Commands
- public enum eFunction { Device = 0, Buttons, Configuration, Display, Network, OnScreenText, ClientAccess, Standby, NULL };
- private string[] sFunction = { "DeviceInfo", "Buttons", "Configuration", "Display", "Network", "OnScreenText", "ClientAccess", "Standby", string.Empty };
- public enum eDisplayMode { Clone = 0, Extended };
- private Dictionary<eDisplayMode, string> displayMode = new Dictionary<eDisplayMode,string>()
- {
- { eDisplayMode.Clone, "value=Clone"},
- { eDisplayMode.Extended, "value=Extended"}
- };
- #endregion
- #region Inits
- /// <summary>
- /// Constructor using default password
- /// </summary>
- /// <param name="unitIP">ClickShare IP Address</param>
- public ClickShare(string unitIP)
- {
- try
- {
- IPAddress.Parse(unitIP); //Make sure it is a proper IP
- baseURL = "https://" + unitIP + ":" + 4001 + "/v1.10/";
- }
- catch (Exception e)
- {
- CrestronConsole.PrintLine("Sources.ClickShare -> Error in Constructor: {0}", e.Message);
- }
- }
- /// <summary>
- /// Constructor using user configured integrator password
- /// </summary>
- /// <param name="unitIP">ClickShare IP Address</param>
- /// <param name="integratorPassword">ClickShare Integrator Account Password</param>
- public ClickShare(string unitIP, string integratorPassword)
- {
- try
- {
- IPAddress.Parse(unitIP); //Make sure it is a proper IP
- baseURL = "https://" + unitIP + ":" + 4001 + "/v1.10/";
- password = integratorPassword;
- }
- catch (Exception e)
- {
- CrestronConsole.PrintLine("Sources.ClickShare -> Error in Constructor: {0}", e.Message);
- }
- }
- #endregion
- public void GetValue(eFunction f)
- {
- //JC.Connect(baseURL + "DeviceInfo", password, RequestType.Get);
- JC.Connect(baseURL + sFunction[(int)f],"", password, RequestType.Get);
- }
- public void Reboot()
- {
- JC.Connect(baseURL + sFunction[(int)eFunction.Configuration] + "/RestartSystem", "value=true", password, RequestType.Put);
- }
- }
- public class JsonCollector
- {
- public void Connect(string url, string content, string password, RequestType type)
- {
- HttpsClient myJsonClient = new HttpsClient(); // Create the client
- HttpsClientResponse myJsonResponse; // Create a holder for the response
- HttpsClientRequest myJsonRequest; // Create the request
- myJsonClient.TimeoutEnabled = true; // HttpClient settings these can be left at default or setup as needed
- myJsonClient.Timeout = 5;
- myJsonClient.KeepAlive = false;
- myJsonClient.UserName = "integrator"; // default username;
- myJsonClient.Password = password; // defined password;
- myJsonClient.AuthenticationMethod = AuthMethod.BASIC; // basic Authentication
- myJsonClient.PeerVerification = false; // ignore SSL certificate peer errors
- myJsonClient.HostVerification = false; // ignore SSL certificate host errors
- myJsonRequest = new HttpsClientRequest(); // Instanciate the request
- myJsonRequest.RequestType = type; // sets the request type
- myJsonRequest.ContentString = content; // sets the content if any
- myJsonRequest.Url.Parse(url); // take the full URL and break it down for the request to decide how to handle it
- try
- {
- CrestronConsole.PrintLine("Request: {0} Content: {1}", url, content);
- myJsonResponse = myJsonClient.Dispatch(myJsonRequest); // This is when we send the message and the response catches the reply.
- CrestronConsole.PrintLine("JSON RESPONSE -> " + myJsonResponse.ContentString);
- Deserialize(myJsonResponse.ContentString);
- }
- catch (Exception e)
- {
- CrestronConsole.PrintLine("JSON REQUEST ERROR -> " + e.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement