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"); // or use access key instead of login and password //var connectionInfo = new DeviceHiveConnectionInfo("http://playground.devicehive.com/api/rest", "%ACCESS_KEY%"); // 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.GetDeviceAsync(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.GetEquipmentStateAsync(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.AddNotificationSubscriptionAsync(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"); await ConsoleKeyListener(async key => { 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.SendCommandAsync(device.Id, command); } }); // unsubscribe from notifications and dispose the client if (client.ChannelState == ChannelState.Connected) { await client.RemoveSubscriptionAsync(subscription); } client.Dispose(); }
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()); } }