Пример #1
0
        public async Task <float> ReadTemperatureAsync(CancellationToken ct)
        {
            float temperature = await Task.Run <float>(async() =>
            {
                // Simulate measuring temperature
                float baseTemperature = 10.0F;
                Random rand           = new Random();
                float newTemperature  = baseTemperature + (float)rand.NextDouble() * 4 - 2;

                await Task.Delay(1000, ct);
                ct.ThrowIfCancellationRequested();

                return(newTemperature);
            }, ct);

            NewMeasureEventArgs eventArgs = new NewMeasureEventArgs
            {
                Measure    = temperature,
                MeasuredAt = DateTime.Now
            };

            OnNewMeasure(eventArgs);

            return(temperature);
        }
Пример #2
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);
        }
Пример #3
0
 protected virtual void OnNewMeasure(NewMeasureEventArgs e)
 {
     NewMeasure?.Invoke(this, e);
 }