Esempio n. 1
0
 private async void timerCallback(object state)
 {
     if (_board != null)
     {
         if (_board.ButtonPressed)
             SwitchLight();
         await IoC.Instance.Resolve<IDispatcherService>().RunAsync(() =>
         {
             Humidity = _board.Humidity;
             Temperature = _board.Temperature;
             ExternalTemperature = _board.ExternalTemperature;
         });
     }
     //Every 5 seconds
     if (DateTime.Now.Second % 5 != 0)
         return;
     if (_client == null)
         return;
     //Send message
     if (_telemetry)
     {
         var data = new DeviceMonitoringData();
         data.DeviceId = SettingsViewModel.Default.DeviceId;
         data.Humidity = Humidity;
         data.Temperature = Temperature;
         data.ExternalTemperature = ExternalTemperature;
         var content = JsonConvert.SerializeObject(data);
         await _client.SendEventAsync(new Message(Encoding.UTF8.GetBytes(content)));
     }
     //receive messages
     Message message;
     while ((message = await _client.ReceiveAsync()) != null)
     {
         var content = Encoding.ASCII.GetString(message.GetBytes());
         Debug.WriteLine("{0}> Received message: {1}", DateTime.Now.ToLocalTime(), content);
         var command = JsonConvert.DeserializeObject<DeviceCommand>(content);
         if (command != null)
         {
             if (command.Name == "SwitchLight")
             {
                 await IoC.Instance.Resolve<IDispatcherService>().RunAsync(() => SwitchLight());
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "LightColor")
             {
                 if (command.Parameters.Count == 1)
                 {
                     var color = LEDColor.Black;
                     if (Enum.TryParse<LEDColor>(command.Parameters[0].Value, out color))
                     {
                         await IoC.Instance.Resolve<IDispatcherService>().RunAsync(() => LightColor = color);
                         await _client.CompleteAsync(message);
                     }
                     else
                     {
                         await _client.RejectAsync(message);
                     }
                 }
             }
             else if (command.Name == "PingDevice")
             {
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "StartTelemetry")
             {
                 _telemetry = true;
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "StopTelemetry")
             {
                 _telemetry = false;
                 await _client.CompleteAsync(message);
             }
             else
             {
                 await _client.RejectAsync(message);
             }
         }
     }
 }
Esempio n. 2
0
 private async Task SendTelemetryAsync(PayloadModel payload)
 {
     if (!_whiteList.ContainsKey(payload.device))
         return;
     var device = _whiteList[payload.device];
     var connectionString = string.Format("HostName={0}.azure-devices.net;DeviceId={1};SharedAccessKey={2}",
         _serviceSettings.Host, device.DeviceId, device.Key);
     _client = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Http1);
     var MSB = Math.Floor(payload.byte2 / Math.Pow(2, 4));
     var LSB = payload.byte3 % 64;
     var temperature = (((MSB * Math.Pow(2, 6)) + LSB) - 200) / 8;
     var humidity = payload.byte4 * 0.5;
     var data = new DeviceMonitoringData();
     data.DeviceId = payload.device;
     data.Temperature = temperature;
     data.Humidity = humidity;
     var content = JsonConvert.SerializeObject(data);
     AddLog("Device=" + payload.device + " Telemetry=" + content);
     await _client.SendEventAsync(new Message(Encoding.UTF8.GetBytes(content)));
 }
Esempio n. 3
0
 private static async void Callback(object state)
 {
     var keyDown = false;
     if (Keyboard.IsKeyDown(VirtualKey.Numpad4) || Keyboard.IsKeyDown(VirtualKey.Down) || Keyboard.IsKeyDown(VirtualKey.Add))
     {
         _temperature--;
         keyDown = true;
     }
     if (Keyboard.IsKeyDown(VirtualKey.Numpad7) || Keyboard.IsKeyDown(VirtualKey.Up) || Keyboard.IsKeyDown(VirtualKey.Subtract))
     {
         _temperature++;
         keyDown = true;
     }
     else if (Keyboard.IsKeyDown(VirtualKey.Numpad5))
     {
         _humidity--;
         keyDown = true;
     }
     else if (Keyboard.IsKeyDown(VirtualKey.Numpad8))
     {
         _humidity++;
         keyDown = true;
     }
     else if (Keyboard.IsKeyDown(VirtualKey.Numpad6))
     {
         _externalTemperature--;
         keyDown = true;
     }
     else if (Keyboard.IsKeyDown(VirtualKey.Numpad9))
     {
         _externalTemperature++;
         keyDown = true;
     }
     if (keyDown)
         Console.WriteLine("Temperature={0}, Humidity={1}, External temperature={2}", _temperature, _humidity, _externalTemperature);
     if (DateTime.Now.Second % 5 != 0)
         return;
     if (_client == null)
         return;
     //Send message
     if (_telemetry)
     {
         var data = new DeviceMonitoringData();
         data.DeviceId = _deviceId;
         data.Humidity = _humidity;
         data.Temperature = _temperature;
         data.ExternalTemperature = _externalTemperature;
         var content = JsonConvert.SerializeObject(data);
         var msg = new Message(Encoding.UTF8.GetBytes(content));
         //await _client.SendEventAsync(message);
         await _client.SendEventAsync(msg);
     }
     //receive messages
     Message message;
     while ((message = await _client.ReceiveAsync()) != null)
     {
         var content = Encoding.ASCII.GetString(message.GetBytes());
         Console.WriteLine("{0}> Received message: {1}", DateTime.Now.ToLocalTime(), content);
         var command = JsonConvert.DeserializeObject<DeviceCommand>(content);
         if (command != null)
         {
             if (command.Name == "SwitchLight")
             {
                 SwitchLight();
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "LightColor")
             {
                 if (command.Parameters.Count == 1)
                 {
                     _lightColor = command.Parameters[0].Value;
                     await _client.CompleteAsync(message);
                 }
             }
             else if (command.Name == "PingDevice")
             {
                 Console.WriteLine("Ping");
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "StartTelemetry")
             {
                 Console.WriteLine("Start telemetry");
                 _telemetry = true;
                 await _client.CompleteAsync(message);
             }
             else if (command.Name == "StopTelemetry")
             {
                 Console.WriteLine("Stop telemetry");
                 _telemetry = false;
                 await _client.CompleteAsync(message);
             }
             else
             {
                 await _client.RejectAsync(message);
             }
         }
     }
 }