Пример #1
0
        public async Task CanUpdateReportedProperties()
        {
            ICloudProxy cloudProxy = await this.GetCloudProxyWithConnectionStringKey("device2ConnStrKey");

            IMessage message = await cloudProxy.GetTwinAsync();

            JObject twin    = JObject.Parse(System.Text.Encoding.UTF8.GetString(message.Body));
            int     version = (int)twin.SelectToken("reported.$version");
            int     counter = (int?)twin.SelectToken("reported.bvtCounter") ?? 0;

            IMessage updateReportedPropertiesMessage = new EdgeMessage.Builder(Encoding.UTF8.GetBytes($"{{\"bvtCounter\":{counter + 1}}}")).Build();
            await cloudProxy.UpdateReportedPropertiesAsync(updateReportedPropertiesMessage);

            message = await cloudProxy.GetTwinAsync();

            twin = JObject.Parse(System.Text.Encoding.UTF8.GetString(message.Body));
            int nextVersion = (int)twin.SelectToken("reported.$version");
            var nextCounter = (int?)twin.SelectToken("reported.bvtCounter");

            Assert.NotNull(nextCounter);
            Assert.Equal(version + 1, nextVersion);
            Assert.Equal(counter + 1, nextCounter);

            bool disconnectResult = await cloudProxy.CloseAsync();

            Assert.True(disconnectResult);
        }
Пример #2
0
        static async Task RunGetTwin(ICloudProxy cloudProxy, int count)
        {
            for (int i = 0; i < count; i++)
            {
                IMessage twin = await cloudProxy.GetTwinAsync();

                Assert.NotNull(twin);
            }
        }
Пример #3
0
        public async Task CanGetTwin()
        {
            ICloudProxy cloudProxy = await this.GetCloudProxyWithConnectionStringKey("device2ConnStrKey");

            IMessage result = await cloudProxy.GetTwinAsync();

            string actualString = System.Text.Encoding.UTF8.GetString(result.Body);

            Assert.StartsWith("{", actualString);
            bool disconnectResult = await cloudProxy.CloseAsync();

            Assert.True(disconnectResult);
        }
Пример #4
0
        internal async Task <TwinInfo> GetTwinInfoWhenCloudOnlineAsync(string id, ICloudProxy cp, bool sendDesiredPropertyUpdate)
        {
            TwinCollection diff = null;
            // Used for returning value to caller
            TwinInfo cached;

            using (await this.twinLock.LockAsync())
            {
                IMessage twinMessage = await cp.GetTwinAsync();

                Twin cloudTwin = this.twinConverter.FromMessage(twinMessage);
                Events.GotTwinFromCloudSuccess(id, cloudTwin.Properties.Desired.Version, cloudTwin.Properties.Reported.Version);
                var newTwin = new TwinInfo(cloudTwin, null);
                cached = newTwin;

                IEntityStore <string, TwinInfo> twinStore = this.TwinStore.Expect(() => new InvalidOperationException("Missing twin store"));

                await twinStore.PutOrUpdate(
                    id,
                    newTwin,
                    t =>
                {
                    // If the new twin is more recent than the cached twin, update the cached copy.
                    // If not, reject the cloud twin
                    if (t.Twin == null ||
                        cloudTwin.Properties.Desired.Version > t.Twin.Properties.Desired.Version ||
                        cloudTwin.Properties.Reported.Version > t.Twin.Properties.Reported.Version)
                    {
                        if (t.Twin != null)
                        {
                            Events.UpdateCachedTwin(
                                id,
                                t.Twin.Properties.Desired.Version,
                                cloudTwin.Properties.Desired.Version,
                                t.Twin.Properties.Reported.Version,
                                cloudTwin.Properties.Reported.Version);
                            cached = new TwinInfo(cloudTwin, t.ReportedPropertiesPatch);
                            // If the device is subscribed to desired property updates and we are refreshing twin as a result
                            // of a connection reset or desired property update, send a patch to the downstream device
                            if (sendDesiredPropertyUpdate)
                            {
                                Option <IReadOnlyDictionary <DeviceSubscription, bool> > subscriptions = this.connectionManager.GetSubscriptions(id);
                                subscriptions.ForEach(
                                    s =>
                                {
                                    if (s.TryGetValue(DeviceSubscription.DesiredPropertyUpdates, out bool hasDesiredPropertyUpdatesSubscription) &&
                                        hasDesiredPropertyUpdatesSubscription)
                                    {
                                        Events.SendDesiredPropertyUpdateToSubscriber(
                                            id,
                                            t.Twin.Properties.Desired.Version,
                                            cloudTwin.Properties.Desired.Version);
                                        diff = new TwinCollection(JsonEx.Diff(t.Twin.Properties.Desired, cloudTwin.Properties.Desired));
                                    }
                                });
                            }
                        }
                    }
                    else
                    {
                        Events.PreserveCachedTwin(id,
                                                  t.Twin.Properties.Desired.Version, cloudTwin.Properties.Desired.Version,
                                                  t.Twin.Properties.Reported.Version, cloudTwin.Properties.Reported.Version);
                        cached = t;
                    }
                    return(cached);
                });
            }
            if ((diff != null) && (diff.Count != 0))
            {
                Events.SendDiffToDeviceProxy(diff.ToString(), id);
                IMessage message = this.twinCollectionConverter.ToMessage(diff);
                await this.SendDesiredPropertiesToDeviceProxy(id, message);
            }
            return(cached);
        }