Beispiel #1
0
        public async Task <TableStorageResponse <UserSetting> > SetUserSettingValueAsync(UserSetting setting)
        {
            var incomingEntity =
                new UserSettingTableEntity()
            {
                PartitionKey = _settingsTablePartitionKey,
                RowKey       = setting.Key,
                SettingValue = setting.Value
            };

            if (!string.IsNullOrWhiteSpace(setting.Etag))
            {
                incomingEntity.ETag = setting.Etag;
            }

            TableStorageResponse <UserSetting> result = await AzureTableStorageHelper.DoTableInsertOrReplaceAsync <UserSetting, UserSettingTableEntity>(incomingEntity, (tableEntity) =>
            {
                if (tableEntity == null)
                {
                    return(null);
                }

                var updatedSetting = new UserSetting()
                {
                    Key   = tableEntity.RowKey,
                    Value = tableEntity.SettingValue,
                    Etag  = tableEntity.ETag
                };

                return(updatedSetting);
            }, _storageAccountConnectionString, _settingsTableName);

            return(result);
        }
        public override async Task ProcessItem(dynamic eventData)
        {
            // Ensure this is a correctly-formatted event for ML; ignore it otherwise
            if (eventData == null || eventData.deviceid == null || eventData.cycle == null ||
                eventData.sensor9 == null || eventData.sensor11 == null || eventData.sensor14 == null || eventData.sensor15 == null)
            {
                return;
            }

            // The experiment theoretically supports multiple inputs at once,
            // even though we only get one value at a time, so the request
            // requires an array of inputs
            MLRequest mlRequest = new MLRequest(ML_REQUEST_COLUMNS, new string[, ]
            {
                {
                    // The id is required to be numeric, so we hash the actual device id
                    eventData.deviceid.ToString().GetHashCode().ToString(),
                    // The remaining entries are string representations of the numeric values
                    eventData.cycle.ToString(),
                    eventData.sensor9.ToString(),
                    eventData.sensor11.ToString(),
                    eventData.sensor14.ToString(),
                    eventData.sensor15.ToString()
                }
            }
                                                );

            HttpClient http = new HttpClient();

            http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _configurationProvider.GetConfigurationSettingValue("MLApiKey"));
            http.BaseAddress = new Uri(_configurationProvider.GetConfigurationSettingValue("MLApiUrl") + ML_ENDPOINT);

            HttpResponseMessage response = await http.PostAsJsonAsync("", mlRequest);

            if (response.IsSuccessStatusCode)
            {
                MLResponse result = JsonConvert.DeserializeObject <MLResponse>(await response.Content.ReadAsStringAsync());

                RulTableEntity entry = new RulTableEntity
                {
                    PartitionKey = eventData.deviceid.ToString(),
                    RowKey       = eventData.cycle.ToString(),
                    // Extract the single relevant RUL value from the JSON output
                    Rul = result.Results["data"].value.Values[0, RUL_COLUMN],
                    // Since the simulator might replay data, ensure we can overwrite table values
                    ETag = "*"
                };

                // We don't need a data model to represent the result of this operation,
                // so we use a stub table/model convertor
                await AzureTableStorageHelper.DoTableInsertOrReplaceAsync <object, RulTableEntity>(entry, (RulTableEntity e) => null,
                                                                                                   _configurationProvider.GetConfigurationSettingValue("eventHub.StorageConnectionString"),
                                                                                                   _configurationProvider.GetConfigurationSettingValue("MLResultTableName"));
            }
            else
            {
                throw new Exception(string.Format("The ML request failed with status code: {0}", response.StatusCode));
            }
        }
        public static async Task Write(string device, string state, string connectionString, string table)
        {
            StateTableEntity entry = new StateTableEntity
            {
                PartitionKey = device,
                RowKey       = "State", // Arbitrary constant; we're only storing one value per device
                State        = state,
                ETag         = "*"
            };

            // We don't need a data model to represent the result of this operation,
            // so we use a stub table/model convertor
            await AzureTableStorageHelper.DoTableInsertOrReplaceAsync <object, StateTableEntity>(entry, (StateTableEntity e) => { return(null); }, connectionString, table);
        }
        /// <summary>
        /// Save a Device Rule to the server. This may be either a new rule or an update to an existing rule.
        /// </summary>
        /// <param name="updateContainer"></param>
        /// <returns></returns>
        public async Task <TableStorageResponse <DeviceRule> > SaveDeviceRuleAsync(DeviceRule updatedRule)
        {
            DeviceRuleTableEntity incomingEntity = BuildTableEntityFromRule(updatedRule);

            TableStorageResponse <DeviceRule> result =
                await AzureTableStorageHelper.DoTableInsertOrReplaceAsync <DeviceRule, DeviceRuleTableEntity>(incomingEntity, BuildRuleFromTableEntity,
                                                                                                              _storageAccountConnectionString, _deviceRulesNormalizedTableName);

            if (result.Status == TableStorageResponseStatus.Successful)
            {
                // Build up a new blob to push up for ASA job ref data
                List <DeviceRuleBlobEntity> blobList = await BuildBlobEntityListFromTableRows();
                await PersistRulesToBlobStorageAsync(blobList);
            }

            return(result);
        }
Beispiel #5
0
        public override async Task ProcessItem(dynamic eventData)
        {
            // Ensure this is a correctly-formatted event for ML; ignore it otherwise
            if (eventData == null || eventData.deviceid == null || eventData.cycle == null ||
                eventData.sensor9 == null || eventData.sensor11 == null || eventData.sensor14 == null || eventData.sensor15 == null)
            {
                return;
            }

            string result = await _mlServiceInvoker.GetRULAsync(
                // The id is required to be numeric, so we hash the actual device id
                eventData.deviceid.ToString().GetHashCode().ToString(),
                // The remaining entries are string representations of the numeric values
                eventData.cycle.ToString(),
                eventData.sensor9.ToString(),
                eventData.sensor11.ToString(),
                eventData.sensor14.ToString(),
                eventData.sensor15.ToString()
                );

            Trace.TraceInformation($"RUL Result: {result}");

            RulTableEntity entry = new RulTableEntity
            {
                PartitionKey = eventData.deviceid.ToString(),
                RowKey       = eventData.cycle.ToString(),
                // Extract the single relevant RUL value from the JSON output
                Rul = result,
                // Since the simulator might replay data, ensure we can overwrite table values
                ETag = "*"
            };

            // We don't need a data model to represent the result of this operation,
            // so we use a stub table/model convertor
            await AzureTableStorageHelper.DoTableInsertOrReplaceAsync <object, RulTableEntity>(entry, (RulTableEntity e) => null,
                                                                                               _configurationProvider.GetConfigurationSettingValue("eventHub.StorageConnectionString"),
                                                                                               _configurationProvider.GetConfigurationSettingValue("MLResultTableName"));
        }