Esempio n. 1
0
        public async Task <Response <Device> > AddDeviceAsync(Device device)
        {
            Microsoft.Azure.Devices.Device requestDevice;
            try
            {
                //Console.WriteLine("New device:");

                requestDevice = new Microsoft.Azure.Devices.Device(device.Id);

                requestDevice = await registryManager.AddDeviceAsync(requestDevice);

                return(new Response <Device>()
                {
                    Success = true,
                    ResponseObject = device
                });
            }
            catch (DeviceAlreadyExistsException ex)
            {
                List <AzureException> exception = new List <AzureException>()
                {
                    new AzureException()
                    {
                        Exception = ex, Message = ex.Message
                    }
                };
                return(new Response <Device>()
                {
                    Success = true,
                    ResponseObject = device,
                    Exceptions = exception.ToArray()
                });
            }
        }
        public Response <Device> AddDeviceAsync(Device device)
        {
            Microsoft.Azure.Devices.Device requestDevice;
            try
            {
                //Console.WriteLine("New device:");

                requestDevice = new Microsoft.Azure.Devices.Device(device.Name);
                Task task = Task.Run(() =>
                {
                    requestDevice = registryManager.AddDeviceAsync(requestDevice).Result;
                });
                task.Wait();
                device.Name = requestDevice.Id;
                return(new Response <Device>()
                {
                    Success = true,
                    ResponseObject = device
                });
            }
            catch (DeviceAlreadyExistsException ex)
            {
                List <AzureException> exception = new List <AzureException>()
                {
                    new AzureException()
                    {
                        Exception = ex, Message = ex.Message
                    }
                };
                return(new Response <Device>()
                {
                    Success = true,
                    ResponseObject = device,
                    Exceptions = exception.ToArray()
                });
            }
        }