Example #1
0
        // GET: Devices/Create
        public ActionResult Create()
        {
            //return View();
            int    count  = db.Devices.Count <Device>();
            Device entity = null;
            int    maxId  = count;

            if (count > 0)
            {
                entity = db.Devices.OrderByDescending(x => x.DeviceId).First();
                if (!int.TryParse(entity.DeviceId.Substring(6), out maxId))
                {
                    maxId = count;
                }
            }

            string deviceId = "Device" + (maxId + 1).ToString("D3");

            // Add the specified device to IoT Hub
            var task = Task.Run(async() =>
            {
                await DeviceManagement.AddDeviceAsync(deviceId);
            });

            task.Wait();

            Device device = new Device
            {
                DeviceId         = deviceId,
                DeviceKey        = DeviceManagement.GetDeviceKey(),
                ConnectionString = DeviceManagement.GetIothubConnectionString(),
                IsUsed           = false,
                IsActive         = false,
                TimeStamp        = DateTime.UtcNow
            };

            // Add the specified device to [Devices] table
            db.Devices.Add(device);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }