Exemple #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)
                 *      {
                 *
                 *      }
                 *  }
                 * }*/
            });
        }
Exemple #2
0
        public IStorageSensor AddSensor(string sensor_name, string sensor_units,
                                        IList <Guid> sensor_channels, IList <IStorageAlert> sensor_alerts)
        {
            if (!sensor_name.IsVaildSensorName())
            {
                throw new ArgumentException(
                          string.Format("Invalid sensor name: {0}", sensor_name));
            }
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();
            IStorageSensor sensor = new IStorageSensor()
            {
                DeviceId = device.Id,
                Name     = sensor_name,
                Unit     = sensor_units,
                Channels = sensor_channels,
                Alerts   = sensor_alerts
            };

            if (device.Sensors.Keys.Contains(sensor_name))
            {
                device.Sensors.Remove(sensor_name);
            }
            device.Sensors.Add(sensor_name, sensor);
            try
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.UpdateDevice);
                this.UpdateDevice(device);
                Log.WriteTableSuccess(string.Format("Added sensor {0} to device entity: Id: {1}, Token: {2}",
                                                    sensor.Name, device.Id.ToUrn(), device.Token));
                return(sensor);
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to read table for device: Id: {0}, Token: {1}.",
                                                   device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddSensor);
            }
        }