Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("CustomerID,AuthKey,Date")] Device device)
        {
            Device deviceBuilder = device;

            deviceBuilder.Date    = DateTime.Now;
            deviceBuilder.AuthKey = "temp";

            if (ModelState.IsValid)
            {
                // Create device in DB
                db.Add(deviceBuilder);
                await db.SaveChangesAsync();

                Device localDevice = await db.Device.LastAsync();

                // Create device in cloud
                Task <bool> t1 = cloud.CreateDevice(localDevice);
                Task.WaitAll(t1);

                // When creation is complete on cloud-side
                if (t1.Result)
                {
                    // Key from Azure IOT
                    Task <string> t2 = cloud.GetConnectionString(localDevice);
                    Task.WaitAll(t2);

                    localDevice.AuthKey = t2.Result;

                    db.Update(localDevice);
                    await db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            //Not successful
            return(RedirectToAction(nameof(Index)));
        }