Beispiel #1
0
        public async Task <int> SaveChangesAsync()
        {
            int    modified = 0;
            string twinJson = "";

            foreach (var modifiedDevice in modelDevices.modifiedDevices)
            {
                if (modifiedDevice.State == EntityState.Added || modifiedDevice.State == EntityState.Modified)
                {
                    twinJson = "{\"properties\":{\"desired\":" + modifiedDevice.Device.DesiredPropertiesToJson() + "}}";
                    var test = Newtonsoft.Json.JsonConvert.DeserializeObject(twinJson);
                }
                switch (modifiedDevice.State)
                {
                case EntityState.Added:
                    var newDevice = new Microsoft.Azure.Devices.Device(modifiedDevice.Device.Id);
                    newDevice = await registryManager.AddDeviceAsync(newDevice);

                    var twin = await registryManager.GetTwinAsync(newDevice.Id);

                    await registryManager.UpdateTwinAsync(newDevice.Id, twinJson, twin.ETag);

                    modified++;
                    break;

                case EntityState.Modified:
                    var managedDevice = await registryManager.GetDeviceAsync(modifiedDevice.Device.Id);

                    var manageDeviceTwin = await registryManager.GetTwinAsync(modifiedDevice.Device.Id);

                    string etag = manageDeviceTwin.ETag;
                    await registryManager.UpdateTwinAsync(modifiedDevice.Device.Id, twinJson, etag);

                    modified++;
                    break;

                case EntityState.Deleted:
                    var registered = await registryManager.GetDeviceAsync(modifiedDevice.Device.Id);

                    await registryManager.RemoveDeviceAsync(registered);

                    modified++;
                    break;
                }
            }
            modelDevices.modifiedDevices.Clear();

            return(modified);
        }
Beispiel #2
0
        private async Task <List <TDevice> > LoadIoTHubDevicesAsync()
        {
            List <TDevice> resultSet = new List <TDevice>();

            devices.Clear();
            string devIdQuery = "SELECT deviceId FROM devices";
            var    query      = registryManager.CreateQuery(devIdQuery);

            while (query.HasMoreResults)
            {
                var page = await query.GetNextAsJsonAsync();

                foreach (var twin in page)
                {
                    var jdevice        = Newtonsoft.Json.JsonConvert.DeserializeObject(twin) as Newtonsoft.Json.Linq.JObject;
                    var deviceId       = jdevice.Value <string>("DeviceId");
                    var registedDevice = await registryManager.GetDeviceAsync(deviceId);

                    var registedDeviceTwin = await registryManager.GetTwinAsync(deviceId);

                    var device = new TDevice()
                    {
                        Id = registedDevice.Id
                    };
                    device.SetDesiredProperties(registedDeviceTwin.Properties.Desired.ToJson());

                    // Set Reported Properties.
                    var rpJSON = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(registedDeviceTwin.Properties.Reported.ToJson(Newtonsoft.Json.Formatting.None));
                    rpJSON.Remove("$metadata");
                    rpJSON.Remove("$version");
                    device.Reported = Newtonsoft.Json.JsonConvert.SerializeObject(rpJSON);

                    devices.Add(new IoTDeviceEntry <TDevice>()
                    {
                        Device = device,
                        State  = EntityState.Unchanged
                    });
                    resultSet.Add(device);
                }
            }

            return(resultSet);
        }