/// <summary> /// Constructor which allows to override <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server. /// </summary> /// <param name="connectionInfo">An instance of <see cref="DeviceHiveConnectionInfo" /> class which provides DeviceHive connection information.</param> /// <param name="restClient">An instance of <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server.</param> public DeviceHiveClient(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } _restClient = restClient ?? new RestClient(connectionInfo); // default channels: WebSocket, LongPolling SetAvailableChannels(new Channel[] { #if !EXCLUDE_WEB_SOCKET new WebSocketChannel(connectionInfo, _restClient), #endif new LongPollingChannel(connectionInfo, _restClient) }); #if !PORTABLE && !NETFX_CORE // allow at least 10 concurrent outbound connections if (ServicePointManager.DefaultConnectionLimit < 10) { ServicePointManager.DefaultConnectionLimit = 10; } #endif }
/// <summary> /// Constructor which allows to set custom <see cref="HttpMessageHandler" /> for handling HTTP requests. /// </summary> /// <param name="connectionInfo">An instance of <see cref="DeviceHiveConnectionInfo" /> class which provides DeviceHive connection information.</param> /// <param name="httpMessageHandler">An instance of <see cref="HttpMessageHandler"/> class.</param> public RestClient(DeviceHiveConnectionInfo connectionInfo, HttpMessageHandler httpMessageHandler) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } _httpClient = httpMessageHandler != null ? new HttpClient(httpMessageHandler) : new HttpClient(); _httpClient.BaseAddress = new Uri(connectionInfo.ServiceUrl.TrimEnd('/') + "/"); _httpClient.DefaultRequestHeaders.Add("ClientVersion", "2.1"); _httpClient.DefaultRequestHeaders.Accept.Clear(); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (connectionInfo.AccessKey != null) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", connectionInfo.AccessKey); } else if (connectionInfo.Login != null) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( Encoding.UTF8.GetBytes(string.Format("{0}:{1}", connectionInfo.Login, connectionInfo.Password)))); } _jsonSettings = new JsonSerializerSettings(); _jsonSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.ffffff" }); _jsonSettings.NullValueHandling = NullValueHandling.Ignore; _jsonSettings.ContractResolver = new JsonContractResolver(); }
ClientService(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) : base(connectionInfo, restClient) { // use only WebSocketChannel, not LongPollingChannel SetAvailableChannels(new Channel[] { new WebSocketChannel(connectionInfo, restClient) }); }
static async Task VirtualLedClientRoutine() { // create a DeviceHiveConnectionInfo object // insert your assigned DeviceHive service URL, username and password here var connectionInfo = new DeviceHiveConnectionInfo("http://localhost/DeviceHive.API", "dhadmin", "dhadmin_#911"); // create a DeviceHiveClient object used to communicate with the DeviceHive service var client = new DeviceHiveClient(connectionInfo); // get information about the VirtualLed device var deviceGuid = "E50D6085-2ABA-48E9-B1C3-73C673E414BE"; var device = await client.GetDevice(deviceGuid); if (device == null) { Console.WriteLine("VirtualLed device does not exist on the server, please run VirtualLed device first!"); Console.ReadKey(); return; } Console.WriteLine("Found VirtualLed device with status: " + device.Status); // get information about current LED state var equipmentState = await client.GetEquipmentState(device.Id); var ledEquipmentState = equipmentState.FirstOrDefault(e => e.Id == LED_CODE); if (ledEquipmentState != null) { Console.WriteLine("Current state of the VirtualLed: " + ledEquipmentState.GetParameter<int>("state")); } // subscribe to device notifications var subscription = await client.AddNotificationSubscription(new[] { deviceGuid }, null, HandleNotification); // read user input to send corresponding commands to the VirtualLed device Console.WriteLine("\nPlease enter a desired state of the led (either 0 or 1) or ESC to exit\n"); while (true) { var key = Console.ReadKey(true); if (key.Key == ConsoleKey.Escape) break; if (key.KeyChar == '0' || key.KeyChar == '1') { // send a command to the VirtualLed device to switch the LED state Console.WriteLine(string.Format("Sending UpdateLedState command with state: {0}", key.KeyChar)); var command = new Command("UpdateLedState"); command.Parameter("equipment", LED_CODE); command.Parameter("state", key.KeyChar); await client.SendCommand(device.Id, command); } } // unsubscribe from notifications and dispose the client await client.RemoveSubscription(subscription); client.Dispose(); }
/// <summary> /// Default constructor. /// </summary> /// <param name="connectionInfo">DeviceHive connection information.</param> /// <param name="restClient">IRestClient implementation.</param> protected Channel(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } State = ChannelState.Disconnected; ConnectionInfo = connectionInfo; RestClient = restClient ?? new RestClient(connectionInfo); }
/// <summary> /// Default constructor. /// </summary> /// <param name="connectionInfo">An instance of the DeviceHiveConnectionInfo class providing DeviceHive connection information.</param> public DeviceHiveClient(DeviceHiveConnectionInfo connectionInfo) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); _connectionInfo = connectionInfo; _restClient = new RestClient(connectionInfo); // default channels: WebSocket, LongPolling SetAvailableChannels(new Channel[] { new WebSocketChannel(connectionInfo), new LongPollingChannel(connectionInfo) }); // allow at least 10 concurrent outbound connections if (ServicePointManager.DefaultConnectionLimit < 10) ServicePointManager.DefaultConnectionLimit = 10; }
public async Task<ActionResult> Index() { // if DeviceHive access key is uavailable - offer OAuth authentication if (AccessKey == null) return View(); // otherwise, read and display a list of DeviceHive devices try { var connectionInfo = new DeviceHiveConnectionInfo(DeviceHiveUrl, AccessKey); var client = new DeviceHiveClient(connectionInfo); var devices = await client.GetDevicesAsync(); return View("Devices", devices); } catch (DeviceHiveUnauthorizedException) { // credentials are invalid or expired return View(); } }
/// <summary> /// Constructor which allows to override <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server. /// </summary> /// <param name="connectionInfo">An instance of <see cref="DeviceHiveConnectionInfo" /> class which provides DeviceHive connection information.</param> /// <param name="restClient">An instance of <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server.</param> public DeviceHiveClient(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); _restClient = restClient ?? new RestClient(connectionInfo); // default channels: WebSocket, LongPolling SetAvailableChannels(new Channel[] { #if !EXCLUDE_WEB_SOCKET new WebSocketChannel(connectionInfo, _restClient), #endif new LongPollingChannel(connectionInfo, _restClient) }); #if !PORTABLE && !NETFX_CORE // allow at least 10 concurrent outbound connections if (ServicePointManager.DefaultConnectionLimit < 10) ServicePointManager.DefaultConnectionLimit = 10; #endif }
/// <summary> /// Default constructor. /// </summary> /// <param name="connectionInfo">An instance of <see cref="DeviceHiveConnectionInfo" /> class which provides DeviceHive connection information.</param> public DeviceHiveClient(DeviceHiveConnectionInfo connectionInfo) : this(connectionInfo, null) { }
/// <summary> /// Default constructor. /// </summary> /// <param name="connectionInfo">DeviceHive connection information.</param> public LongPollingChannel(DeviceHiveConnectionInfo connectionInfo) : this(connectionInfo, null) { }
/// <summary> /// Constructor which allows to override <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server. /// </summary> /// <param name="connectionInfo">DeviceHive connection information.</param> /// <param name="restClient">IRestClient implementation.</param> public LongPollingChannel(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) : base(connectionInfo, restClient) { CommandUpdatePollTimeout = TimeSpan.FromSeconds(30); }
/// <summary> /// Default constructor. /// </summary> /// <param name="connectionInfo">DeviceHive connection information.</param> public WebSocketChannel(DeviceHiveConnectionInfo connectionInfo) : this(connectionInfo, null) { }
/// <summary> /// Constructor which allows to override <see cref="IRestClient" /> which makes HTTP requests to the DeviceHive server. /// </summary> /// <param name="connectionInfo">DeviceHive connection information.</param> /// <param name="restClient">IRestClient implementation.</param> public WebSocketChannel(DeviceHiveConnectionInfo connectionInfo, IRestClient restClient) : base(connectionInfo, restClient) { Timeout = 30000; }