Ejemplo n.º 1
0
        public async Task <IActionResult> Post()
        {
            var subscriber = new EventGridSubscriber();

            using var ms = new MemoryStream();
            await Request.Body.CopyToAsync(ms); ms.Position = 0;

            using var sr = new StreamReader(ms);
            var message = await sr.ReadToEndAsync();

            ms.Position = 0;
            var events = subscriber.DeserializeEventGridEvents(ms);

            _logger.LogInformation($"{events.Length} Message(s) Received");

            foreach (var evt in events)
            {
                _logger.LogInformation($"Handling {evt.EventType}: {evt.GetType().Name}");
                evt.Validate();
                _ = evt.Data switch
                {
                    SubscriptionValidationEventData validationEvent => HandleValidationEvent(validationEvent),
                    IotHubDeviceTelemetryEventData iotData => await HandleIotData(iotData),
                    _ => HandleUnknown(evt.Data)
                };
            }
            return(Ok());
        }
Ejemplo n.º 2
0
        private async Task <IActionResult> HandleIotData(IotHubDeviceTelemetryEventData iotData)
        {
            var aqEntry = await _aqMapper.Map(iotData);

            if (aqEntry != null)
            {
                _context.AirQualityEntries.Add(aqEntry);
            }

            var wEntry = await _wMapper.Map(iotData);

            if (wEntry != null)
            {
                _context.WeatherEntries.Add(wEntry);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 3
0
        public Task <AirQualitySensorEntry> Map(IotHubDeviceTelemetryEventData iotData)
        {
            if (iotData.SystemProperties["iothub-connection-device-id"] != "80:7d:3a:61:62:2b")
            {
                return(Task.FromResult <AirQualitySensorEntry>(null));
            }

            var data    = Convert.FromBase64String(iotData.Body.ToString());
            var decoded = Encoding.UTF8.GetString(data);

            var body = JsonSerializer.Deserialize <AirQualitySensorData>(decoded, _jsonSerializerOptions);

            _logger.LogInformation($"Air Quality Sensor Data Received: {body.pm25_aqi} from {body.SensorId}");

            var entry = new AirQualitySensorEntry
            {
                DeviceId           = body.SensorId,
                SampleTime         = body.DateTime,
                Uptime             = body.uptime,
                WifiSignalStrength = body.rssi,

                DewPoint_F    = body.current_dewpoint_f,
                Humidity      = body.current_humidity,
                Temperature_F = body.current_temp_f,
                Pressure_mB   = body.pressure,

                PM1_0_ug_A = body.pm10_0_atm,
                PM1_0_ug_B = body.pm10_0_atm_b,

                PM2_5_ug_A = body.pm2_5_atm,
                PM2_5_ug_B = body.pm2_5_atm_b,

                PM10_0_ug_A = body.pm10_0_atm,
                PM10_0_ug_B = body.pm10_0_atm_b,

                P0_3_um_A = body.p_0_3_um,
                P0_3_um_B = body.p_0_3_um_b,

                P0_5_um_A = body.p_0_5_um,
                P0_5_um_B = body.p_0_5_um_b,

                P1_0_um_A = body.p_1_0_um,
                P1_0_um_B = body.p_1_0_um_b,

                P2_5_um_A = body.p_2_5_um,
                P2_5_um_B = body.p_2_5_um_b,

                P5_0_um_A = body.p_5_0_um,
                P5_0_um_B = body.p_5_0_um_b,

                P10_0_um_A = body.p_10_0_um,
                P10_0_um_B = body.p_10_0_um_b,

                PM2_5Index_A = body.pm25_aqi,
                PM2_5Index_B = body.pm25_aqi_b,

                PM2_5IndexColor_A = body.p25aqic,
                PM2_5IndexColor_B = body.p25aqic_b
            };

            return(Task.FromResult(entry));
        }
Ejemplo n.º 4
0
        public Task <WeatherSensorEntry> Map(IotHubDeviceTelemetryEventData iotData)
        {
            if (iotData.SystemProperties["iothub-connection-device-id"] != "001D0A7112D8")
            {
                return(Task.FromResult <WeatherSensorEntry>(null));
            }

            var decoded = iotData.Body.ToString();

            var body = JsonSerializer.Deserialize <WeatherStationData>(decoded, _jsonSerializerOptions);

            _logger.LogInformation($"Weather Sensor Data Received: {body.Temp} from {iotData.SystemProperties["iothub-connection-device-id"]}");

            var entry = new WeatherSensorEntry
            {
                DeviceId          = body.DeviceId,
                SampleTime        = body.DateTime,
                BarAbsolute       = body.BarAbsolute,
                BarSeaLevel       = body.BarSeaLevel,
                BarTrend          = body.BarTrend,
                DataStructureType = body.DataStructureType,
                DewPoint          = body.DewPoint,
                DewPointIn        = body.DewPointIn,
                HeatIndex         = body.HeatIndex,
                HeatIndexIn       = body.HeatIndexIn,
                Hum                        = body.Hum,
                HumIn                      = body.HumIn,
                Lsid                       = body.Lsid,
                RainfallDaily              = body.RainfallDaily,
                RainfallLast15_Min         = body.RainfallLast15_Min,
                RainfallLast24_Hr          = body.RainfallLast24_Hr,
                RainfallLast60_Min         = body.RainfallLast60_Min,
                RainfallMonthly            = body.RainfallMonthly,
                RainfallYear               = body.RainfallYear,
                RainRateHi                 = body.RainRateHi,
                RainRateHiLast15_Min       = body.RainRateHiLast15_Min,
                RainRateLast               = body.RainRateLast,
                RainSize                   = body.RainSize,
                RainStorm                  = body.RainStorm,
                RainStormLast              = body.RainStormLast,
                RainStormLastEndAt         = body.RainStormLastEndAt,
                RainStormLastStartAt       = body.RainStormLastStartAt,
                RainStormStartAt           = body.RainStormStartAt,
                RxState                    = body.RxState,
                SolarRad                   = body.SolarRad,
                Temp                       = body.Temp,
                TempIn                     = body.TempIn,
                ThswIndex                  = body.ThswIndex,
                ThwIndex                   = body.ThwIndex,
                TransBatteryFlag           = body.TransBatteryFlag,
                Txid                       = body.Txid,
                UvIndex                    = body.UvIndex,
                WetBulb                    = body.WetBulb,
                WindChill                  = body.WindChill,
                WindDirAtHiSpeedLast10_Min = body.WindDirAtHiSpeedLast10_Min,
                WindDirAtHiSpeedLast2_Min  = body.WindDirAtHiSpeedLast2_Min,
                WindDirLast                = body.WindDirLast,
                WindDirScalarAvgLast10_Min = body.WindDirScalarAvgLast10_Min,
                WindDirScalarAvgLast1_Min  = body.WindDirScalarAvgLast1_Min,
                WindDirScalarAvgLast2_Min  = body.WindDirScalarAvgLast2_Min,
                WindSpeedAvgLast10_Min     = body.WindSpeedAvgLast10_Min,
                WindSpeedAvgLast1_Min      = body.WindSpeedAvgLast1_Min,
                WindSpeedAvgLast2_Min      = body.WindSpeedAvgLast2_Min,
                WindSpeedHiLast10_Min      = body.WindSpeedHiLast10_Min,
                WindSpeedHiLast2_Min       = body.WindSpeedHiLast2_Min,
                WindSpeedLast              = body.WindSpeedLast
            };

            return(Task.FromResult(entry));
        }