예제 #1
0
        public async Task <Models.Twin> GetTwinAsync(string connectionId)
        {
            Console.WriteLine("GetTwinAsync received for " + connectionId);
            var client = objectMap[connectionId];

            Microsoft.Azure.Devices.Shared.Twin t = await client.GetTwinAsync().ConfigureAwait(false);

            Console.WriteLine("Twin Received");
            Console.WriteLine(JsonConvert.SerializeObject(t));
            return(new Models.Twin {
                Desired = t.Properties.Desired,
                Reported = t.Properties.Reported
            });
        }
예제 #2
0
        public async Task PatchModuleTwin(string connectionId, string deviceId, string moduleId, Models.Twin twin)
        {
            Debug.WriteLine("RegistryTwinPatchPutAsync received for {0} with deviceId {1} and moduleId {2}", connectionId, deviceId, moduleId);
            Debug.WriteLine(JsonConvert.SerializeObject(twin));
            var client = objectMap[connectionId] as RegistryManager;

            Debug.WriteLine("Patching twin");
            var registryTwin = new Microsoft.Azure.Devices.Shared.Twin {
                Properties = new TwinProperties {
                    Desired = new TwinCollection(twin.Desired as JObject, null)
                }
            };
            await client.UpdateTwinAsync(deviceId, moduleId, registryTwin, "*").ConfigureAwait(false);

            Debug.WriteLine("patch complete");
        }
        /// <summary>
        /// Converts Json to its equivalent <see cref="Twin"/> representation.
        /// </summary>
        /// <param name="reader">the Json reader.</param>
        /// <param name="objectType">object type</param>
        /// <param name="existingValue">exisiting value</param>
        /// <param name="serializer">the Json serializer.</param>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            var twin = new Twin();

            if (reader.TokenType != JsonToken.StartObject)
            {
                return(null);
            }

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if (reader.TokenType != JsonToken.PropertyName)
                {
                    // TODO: validate that this code is not reached.
                    continue;
                }

                string propertyName = reader.Value as string;
                reader.Read();

                switch (propertyName)
                {
                case DeviceIdJsonTag:
                    twin.DeviceId = reader.Value as string;
                    break;

                case ModelId:
                    twin.ModelId = reader.Value as string;
                    break;

                case ModuleIdJsonTag:
                    twin.ModuleId = reader.Value as string;
                    break;

                case ConfigurationsJsonTag:
                    twin.Configurations = serializer.Deserialize <Dictionary <string, ConfigurationInfo> >(reader);
                    break;

                case CapabilitiesJsonTag:
                    var capabilitiesDictionary = serializer.Deserialize <Dictionary <string, object> >(reader);
                    twin.Capabilities = new DeviceCapabilities
                    {
                        IotEdge = capabilitiesDictionary.ContainsKey(IotEdgeName) && (bool)capabilitiesDictionary[IotEdgeName]
                    };
                    break;

                case ETagJsonTag:
                    twin.ETag = reader.Value as string;
                    break;

                case TagsJsonTag:
                    if (reader.TokenType != JsonToken.StartObject)
                    {
                        throw new InvalidOperationException("Tags Json not a Dictionary.");
                    }
                    twin.Tags = new TwinCollection(JToken.ReadFrom(reader) as JObject);
                    break;

                case PropertiesJsonTag:
                    PopulatePropertiesForTwin(twin, reader);
                    break;

                case VersionTag:
                    twin.Version = (long?)reader.Value;
                    break;

                case StatusTag:
                    string status = reader.Value as string;
                    twin.Status = status?[0] == '\"' ? JsonConvert.DeserializeObject <DeviceStatus>(status) : serializer.Deserialize <DeviceStatus>(reader);
                    break;

                case StatusReasonTag:
                    twin.StatusReason = reader.Value as string;
                    break;

                case StatusUpdateTimeTag:
                    twin.StatusUpdatedTime = ConvertToDateTime(reader.Value);
                    break;

                case ConnectionStateTag:
                    string connectionState = reader.Value as string;
                    twin.ConnectionState = connectionState?[0] == '\"'
                            ? JsonConvert.DeserializeObject <DeviceConnectionState>(connectionState)
                            : serializer.Deserialize <DeviceConnectionState>(reader);
                    break;

                case LastActivityTimeTag:
                    twin.LastActivityTime = ConvertToDateTime(reader.Value);
                    break;

                case CloudToDeviceMessageCountTag:
                    twin.CloudToDeviceMessageCount = serializer.Deserialize <int>(reader);
                    break;

                case AuthenticationTypeTag:
                    string authenticationType = reader.Value as string;
                    twin.AuthenticationType = authenticationType?[0] == '\"'
                            ? JsonConvert.DeserializeObject <AuthenticationType>(authenticationType)
                            : serializer.Deserialize <AuthenticationType>(reader);
                    break;

                case X509ThumbprintTag:
                    twin.X509Thumbprint = serializer.Deserialize <X509Thumbprint>(reader);
                    break;

                case DeviceScope:
                    twin.DeviceScope = serializer.Deserialize <string>(reader);
                    break;

                case ParentScopes:
                    twin.ParentScopes = serializer.Deserialize <IReadOnlyList <string> >(reader);
                    break;

                default:
                    // Ignore unknown fields
                    reader.Skip();
                    break;
                }
            }

            return(twin);
        }