public static async Task Run([ServiceBusTrigger("%DEVICE_LIFECYCLE_QUEUE_NAME%", AccessRights.Listen, Connection = "DEVICE_LIFECYCLE_CONNECTION_STRING")] BrokeredMessage message, TraceWriter log)
        {
            log.Info(String.Format("Starting to create/remove device in Loriot"));
            Stream       stream    = message.GetBody <Stream>();
            StreamReader reader    = new StreamReader(stream);
            dynamic      queueItem = JObject.Parse(reader.ReadToEnd());

            try
            {
                if (message.Properties["opType"].Equals("createDeviceIdentity"))
                {
                    log.Info(String.Format("Register device {0}", queueItem.deviceId));
                    var results = await LoriotClient.RegisterNewDevice(queueItem, log);
                }
                else if (message.Properties["opType"].Equals("deleteDeviceIdentity"))
                {
                    log.Info(String.Format("Remove device {0}", queueItem.deviceId));
                    var results = await LoriotClient.DeleteDevice(queueItem, log);
                }

                log.Info(String.Format("Action completed"));
                await message.CompleteAsync();
            }
            catch (HttpRequestException httpRequestEx)
            {
                message.Abandon();
                log.Error(httpRequestEx.Message, httpRequestEx);
                throw;
            }
        }
Пример #2
0
        public static async Task <long> ImportDevice(TraceWriter log)
        {
            log.Info("Getting starting import");
            long importedItemCount = 0;

            try
            {
                var devices = await LoriotClient.ListDevices(log);

                log.Info("Getting existing azure iot devices");
                var registryManager = RegistryManager.CreateFromConnectionString(System.Environment.GetEnvironmentVariable("IOT_HUB_OWNER_CONNECTION_STRING"));

                if (devices != null)
                {
                    foreach (var device in devices.devices)
                    {
                        string eui         = device._id;
                        Device azureDevice = await registryManager.GetDeviceAsync(eui);

                        if (azureDevice == null)
                        {
                            log.Info($"Device not found in azure iot hub: {eui}");
                            Device createdDevice = await registryManager.AddDeviceAsync(new Device(eui));

                            importedItemCount++;
                            log.Info($"Created a new device: {eui}");
                        }
                        else
                        {
                            log.Info($"Device found in azure iot hub: {eui}");
                        }
                    }
                }
            }
            catch (HttpRequestException httpRequestEx)
            {
                log.Error("Import failed", httpRequestEx);
                throw;
            }

            return(importedItemCount);
        }
Пример #3
0
        private static async Task <long> ImportDevices(TraceWriter log, long importedItemCount, int page = 1)
        {
            var devices = await LoriotClient.ListDevices(log, page);

            log.Info("Getting existing azure iot devices");
            var registryManager = RegistryManager.CreateFromConnectionString(System.Environment.GetEnvironmentVariable("IOT_HUB_OWNER_CONNECTION_STRING"));
            int currentPage     = devices.page;


            if (devices != null)
            {
                JArray devicesList = devices.devices;
                if (devicesList != null && devicesList.Count > 0)
                {
                    foreach (var device in devices.devices)
                    {
                        string eui         = device._id;
                        Device azureDevice = await registryManager.GetDeviceAsync(eui);

                        if (azureDevice == null)
                        {
                            log.Info($"Device not found in azure iot hub: {eui}");
                            Device createdDevice = await registryManager.AddDeviceAsync(new Device(eui));

                            importedItemCount++;
                            log.Info($"Created a new device: {eui}");
                        }
                        else
                        {
                            log.Info($"Device found in azure iot hub: {eui}");
                        }
                    }
                    importedItemCount = await ImportDevices(log, importedItemCount, currentPage + 1);
                }
            }

            return(importedItemCount);
        }