Beispiel #1
0
        public void IngestSensorValues(IStorageDigestMessage message)
        {
            CloudTable device_channel = this.TableClient.GetTableReference(message.Device.Id.ToDeviceChannelTableName());

            device_channel.CreateIfNotExists();
            Parallel.ForEach(message.SensorValues, sv =>
            {
                TableOperation insert_operation = TableOperation
                                                  .InsertOrMerge(AzureStorage.CreateSensorValueEntity(message.Time, sv.Key, sv.Value));
                TableResult result    = device_channel.Execute(insert_operation);
                IStorageSensor sensor = message.Device.Sensors.Where(s => s.Key == sv.Key).FirstOrDefault().Value;
                Parallel.ForEach(sensor.Channels, c =>
                {
                    Log.Partition();
                    TableOperation channel_insert_operation = TableOperation
                                                              .InsertOrMerge(AzureStorage.CreateChannelItemEntity(message.Time, sv.Key, sv.Value));
                    CloudTable channel = this.TableClient.GetTableReference(c.ToChannelTableName());
                    channel.CreateIfNotExists();
                    TableResult channel_insert_result = channel.Execute(channel_insert_operation);
                });

                /*
                 * foreach (IStorageAlert a in sensor.Alerts)
                 * {
                 *  if (sensor.Name.ToSensorType() == typeof(int))
                 *  {
                 *      int v = (int)sv.Value;
                 *      if (a.IntMinValue < v < a.IntMaxValue)
                 *      {
                 *
                 *      }
                 *  }
                 * }*/
            });
        }
        public IStorageDeviceReading AddDeviceReading(IStorageDevice device, DateTime time, IDictionary <string, object> values)
        {
            if (values.Any(v => !v.Key.IsVaildSensorName()))
            {
                string bad_sensors = values.Where(v => !v.Key.IsVaildSensorName())
                                     .Select(v => v.Key + ":" + v.Value).Aggregate((a, b) => { return(a + " " + b + ","); });
                throw new ArgumentException("Device reading has bad sensor names. {0}", bad_sensors);
            }

            if (values.Any(v => v.Key.ToSensorType() != v.Value.GetType().UnderlyingSystemType))
            {
                string bad_sensors = values.Where(v => v.Key.ToSensorType() !=
                                                  v.Value.GetType().UnderlyingSystemType)
                                     .Select(v => v.Key + ":" + v.Value).Aggregate((a, b) => { return(a + " " + b + ","); });
                throw new ArgumentException(string.Format("Device reading has bad sensor values: {0}",
                                                          bad_sensors));
            }
            IStorageDeviceReading reading = new IStorageDeviceReading()
            {
                DeviceId     = device.Id,
                Time         = time,
                SensorValues = values
            };
            TableOperation insert_operation = TableOperation
                                              .InsertOrMerge(AzureStorage.CreateDeviceReadingEntity(reading));
            TableResult result;

            try
            {
                result       = this.SensorReadingsTable.Execute(insert_operation);
                reading.ETag = result.Etag;
                Log.WriteTableSuccess(string.Format
                                          ("Added device reading entity: Partition: {0}, RowKey: {1}, Sensor values: {2}",
                                          reading.Time.GeneratePartitionKey(),
                                          string.Format(CultureInfo.InvariantCulture, DeviceReadingKeyFormat,
                                                        reading.DeviceId, reading.Time.GetTicks()),
                                          reading.SensorValues
                                          .Select(v => v.Key + ":" + v.Value)
                                          .Aggregate((a, b) => { return(a + "," + b + " "); })));
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format
                                          ("Added device reading entity: Partition: {0}, RowKey: {1}, {2}",
                                          reading.Time.GeneratePartitionKey(),
                                          string.Format(CultureInfo.InvariantCulture, DeviceReadingKeyFormat,
                                                        reading.DeviceId, reading.Time.GetTicks()),
                                          reading.SensorValues
                                          .Select(v => v.Key + ":" + v.Value)
                                          .Aggregate((a, b) => { return(a + "," + b + " "); })), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddDeviceReading);
            }

            try
            {
                IStorageDigestMessage message = new IStorageDigestMessage()
                {
                    Device       = device,
                    Time         = time,
                    SensorValues = reading.SensorValues,
                    ETag         = reading.ETag
                };
                this.DigestQueue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(message, this.jss)));
                Log.WriteQueueSuccess(string.Format
                                          ("Added digest message for device reading entity: Partition: {0}, RowKey: {1}, Sensor values: {2}",
                                          reading.Time.GeneratePartitionKey(),
                                          string.Format(CultureInfo.InvariantCulture, DeviceReadingKeyFormat,
                                                        reading.DeviceId, reading.Time.GetTicks()),
                                          reading.SensorValues
                                          .Select(v => v.Key + ":" + v.Value)
                                          .Aggregate((a, b) => { return(a + "," + b + " "); })));
                return(reading);
            }
            catch (Exception e)
            {
                Log.WriteQueueFailure(string.Format
                                          ("Failed to add digest message for device reading entity: Partition: {0}, RowKey: {1}, {2}",
                                          reading.Time.GeneratePartitionKey(),
                                          string.Format(CultureInfo.InvariantCulture, DeviceReadingKeyFormat,
                                                        reading.DeviceId, reading.Time.GetTicks()),
                                          reading.SensorValues
                                          .Select(v => v.Key + ":" + v.Value)
                                          .Aggregate((a, b) => { return(a + "," + b + " "); })), e);
                throw;
            }
        }