Пример #1
0
        public async Task RemoveDeviceAsync(Thermostat thermostat)
        {
            ThermostatClient thermostatClient = deviceList.SingleOrDefault(d => d.Thermostat.Id == thermostat.Id);

            if (thermostatClient == null)
            {
                return;
            }

            deviceList.Remove(thermostatClient);

            await registry.RemoveDeviceAsync(thermostat.Id);
        }
Пример #2
0
 private async Task RunThermostatAsync(Thermostat thermostat, CancellationTokenSource cts)
 {
     try
     {
         while (true)
         {
             await thermostat.ReadTemperatureAsync(cts != null?cts.Token : CancellationToken.None);
         }
     }
     catch (OperationCanceledException)
     {
         OnReadCanceled(thermostat);
     }
 }
Пример #3
0
        public async Task AddDeviceAsync(Thermostat thermostat, int runForMilliseconds = -1)
        {
            bool exists = deviceList.SingleOrDefault(d => d.Id == thermostat.Id) != null;

            if (exists)
            {
                return;
            }

            // Add the device to the IoT Hub
            Device device;

            try
            {
                device = await registry.AddDeviceAsync(new Device(thermostat.Id));
            }
            catch (DeviceAlreadyExistsException)
            {
                device = await registry.GetDeviceAsync(thermostat.Id);
            }

            // Creates a device client for communication with the IoT Hub
            DeviceClient client = DeviceClient.Create(this.hostName,
                                                      new DeviceAuthenticationWithRegistrySymmetricKey(
                                                          device.Id,
                                                          device.Authentication.SymmetricKey.PrimaryKey));

            // Register the event handler for when a new temperature is measured
            thermostat.NewMeasure += (s, e) =>
            {
                SendMessagesAsync(s, e);
            };

            ThermostatClient thermostatClient = new ThermostatClient
            {
                Thermostat         = thermostat,
                Device             = device,
                Client             = client,
                RunForMilliseconds = runForMilliseconds
            };

            deviceList.Add(thermostatClient);
        }
Пример #4
0
        private async void SendMessagesAsync(Thermostat sender, NewMeasureEventArgs e)
        {
            ThermostatClient thermostatClient = deviceList.SingleOrDefault(d => d.Thermostat.Id == sender.Id);

            if (thermostatClient == null)
            {
                return;
            }

            var telemetryDataPoint = new
            {
                deviceId    = sender.Id,
                temperature = e.Measure
            };

            var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
            var message       = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));

            await thermostatClient.Client.SendEventAsync(message);
        }
Пример #5
0
 protected virtual void OnReadCanceled(Thermostat sender)
 {
     ReadCanceled?.Invoke(sender, EventArgs.Empty);
 }