Пример #1
0
        public static async Task Run([ServiceBusTrigger("%IotHubQueueName%", AccessRights.Manage, Connection = "AzureWebJobsConnection")] 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"));


                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);
        }