コード例 #1
0
        private async Task ProcessTwinUpdate(ConsumeContext <IDeviceCreateOrUpdateRequested> context)
        {
            if (JsonConvert.DeserializeObject <DeviceTwinModel>(context.Message.Data) is DeviceTwinModel obj)
            {
                StripIntervalProperties(obj?.Properties?.Desired);
                StripIntervalProperties(obj?.Properties?.Reported);

                obj.DeviceId = context.Message.DeviceId;

                var dictionary = JsonConvert.DeserializeObject <IDictionary <string, object> >(context.Message.Data);

                if (dictionary.ContainsKey("Tags"))
                {
                    var tagsDictionary = JsonConvert.DeserializeObject <IDictionary <string, object> >(dictionary["Tags"].ToString());

                    DeviceModel existing = await _deviceRestService.GetDevice(obj.DeviceId);

                    SetTagPropertiesThatExist(tagsDictionary, existing, obj);

                    if (tagsDictionary.ContainsKey("Geolocation"))
                    {
                        var geoLocationDictionary = JsonConvert.DeserializeObject <IDictionary <string, object> >(tagsDictionary["Geolocation"].ToString());
                        obj.Tags.Geolocation.Latitude  = GetValueIfProvided("Latitude", dm => dm.Geolocation.Latitude, geoLocationDictionary, existing);
                        obj.Tags.Geolocation.Longitude = GetValueIfProvided("Longitude", dm => dm.Geolocation.Longitude, geoLocationDictionary, existing);
                    }
                }

                DeviceModel device = await _deviceRestService.CreateOrUpdateDevice(obj);

                if (device != null && device.DeviceId != Guid.Empty)
                {
                    //Push message to Service bus queue
                    _logger.LogDebug($"DeviceCreateOrUpdateRequestedConsumer: Device created/updated with device id '{context.Message.DeviceId}'.");
                    await context.Publish(new DeviceCreatedOrUpdatedEvent()
                    {
                        Device = device, CorrelationId = context.Message.CorrelationId, NotifyUI = true
                    });

                    return;
                }
                _logger.LogError("DeviceCreateOrUpdateRequestedConsumer: The device could not be updated.");
                throw new Exception("The device could not be updated.");
            }
            _logger.LogError($"DeviceCreateOrUpdateRequestedConsumer: Error while deserializing message '{context.Message.DeviceId}'.");
            throw new Exception($"Error while deserializing message from device '{context.Message.DeviceId}'.");
        }
コード例 #2
0
 public async Task <DeviceModel> GetDevice(Guid deviceId)
 {
     return(await deviceRestService.GetDevice(deviceId));
 }