Esempio n. 1
0
        public IStorageUser AddUser(string name, string token, GeoIp geo_ip, string id = null)
        {
            IStorageUser user = new IStorageUser()
            {
                Id       = string.IsNullOrEmpty(id) ? Guid.NewGuid() : id.UrnToGuid(),
                Token    = token,
                UserName = name,
                Devices  = new List <Guid>()
            };
            TableOperation insertOperation = TableOperation.Insert(AzureStorage.CreateUserTableEntity(user));
            TableResult    result;

            try
            {
                result = this.UsersTable.Execute(insertOperation);
                Log.WriteTableSuccess(string.Format("Added user entity: {0}, Id: {1}, Token {2}.",
                                                    user.UserName, user.Id.ToUrn(), user.Token));
                return(user);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add user entity: {0}, Id: {1}, Token {2}.",
                                                    user.UserName, user.Id.ToUrn(), user.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddUser);
            }
        }
Esempio n. 2
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)
                 *      {
                 *
                 *      }
                 *  }
                 * }*/
            });
        }
Esempio n. 3
0
        public IStorageDevice AddDevice(IStorageUser user, string name, string token, GeoIp location,
                                        string id = null)
        {
            IStorageDevice device = new IStorageDevice()
            {
                Id      = string.IsNullOrEmpty(id) ? Guid.NewGuid() : id.UrnToGuid(),
                UserId  = user.Id,
                Token   = token,
                Name    = name,
                Sensors = new Dictionary <string, IStorageSensor>()
            };

            try
            {
                TableOperation insert_device_operation = TableOperation
                                                         .Insert(AzureStorage.CreateDeviceTableEntity(device));
                TableResult result;
                result      = this.DevicesTable.Execute(insert_device_operation);
                device.ETag = result.Etag;
                user.Devices.Add(device.Id);
                TableOperation update_user_operation = TableOperation.Merge(CreateUserTableEntity(user));
                result    = this.UsersTable.Execute(update_user_operation);
                user.ETag = result.Etag;
                Log.WriteTableSuccess(string.
                                      Format("Added device entity: {0}, Id: {1}, Token {2} to Devices table.",
                                             device.Name, device.Id.ToUrn(), device.Token));
                Log.WriteTableSuccess(string.Format("Added device entity: {0}, Id: {1}, to User entity {2}.",
                                                    device.Name, device.Id.ToUrn(), device.Token, user.Id.ToUrn()));
                return(device);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add device entity: {0}, Id: {1}, Token {2}.",
                                                    device.Name, device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddDevice);
            }
        }
Esempio n. 4
0
        public IStorageChannel AddChannel(string channel_name, string channel_description,
                                          string sensor_type, string channel_units, List <IStorageAlert> alerts)
        {
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();

            IStorageChannel channel = new IStorageChannel()
            {
                Id          = Guid.NewGuid(),
                Name        = channel_name,
                Description = channel_description,
                SensorType  = sensor_type,
                Alerts      = alerts
            };

            try
            {
                TableOperation insert_channel_operation = TableOperation
                                                          .Insert(AzureStorage.CreateChannelTableEntity(channel));
                TableResult result;
                result = this.ChannelsTable.Execute(insert_channel_operation);
                Log.WriteTableSuccess(string.Format("Added Channel entity: {0}, Id: {1}.",
                                                    channel.Name, channel.Id.ToUrn()));
                return(channel);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add Channel entity: {0}, Id: {1}.", channel.Name,
                                                    channel.Id), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddChannel);
            }
        }
Esempio n. 5
0
        public static bool AddDeviceReading(DateTime reading_time, Dictionary <string, object> readings)
        {
            AzureStorage storage = new AzureStorage();

            return(true);
        }
        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;
            }
        }