Esempio n. 1
0
        // GET: IsmDevices/StopPreview/5
        public async Task <ActionResult> StopPreview(int id)
        {
            IsmDevice ismDevice = db.IsmDevices.Find(id);

            // Check that id was correct
            if (ismDevice == null)
            {
                return(HttpNotFound());
            }

            Device device = await registryManager.GetDeviceAsync(ismDevice.DeviceId);

            // Command for Command History
            Command cmd = new Command
            {
                Cmd           = CommandType.STOP_PREVIEW,
                Timestamp     = DateTime.Now,
                IsmDeviceId   = id,
                IsmDevice     = ismDevice,
                CommandStatus = CommandStatus.PENDING
            };

            // Write Command to DB
            db.Commands.Add(cmd);
            db.SaveChanges();
            int commandId = db.Entry(cmd).Entity.CommandId;

            await SendCloudToDevicePortalCommandAsync(commandId, device.Id, CommandType.STOP_PREVIEW);

            // Damit alle offenen Portal Clients das Hinzufügen eines neuen Commands mitbekommen
            await signalRHelper.IsmDevicesIndexChangedTask();

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        // GET: IsmDevices/StopPreview/5
        public async Task <ActionResult> StopPreview(int id)
        {
            IsmDevice ismDevice = db.IsmDevices.Find(id);
            Device    device    = await registryManager.GetDeviceAsync(ismDevice.DeviceId);

            // Command for Command History
            Command cmd = new Command();

            cmd.Cmd           = CommandType.STOP_PREVIEW;
            cmd.Timestamp     = DateTime.Now;
            cmd.IsmDeviceId   = id;
            cmd.IsmDevice     = ismDevice;
            cmd.CommandStatus = CommandStatus.PENDING;

            // Write Command to DB
            db.Commands.Add(cmd);
            db.SaveChanges();
            int CommandId = db.Entry(cmd).Entity.CommandId;

            await SendCloudToDevicePortalCommandAsync(CommandId, device.Id, CommandType.STOP_PREVIEW);

            if (signalRHelper.Authenticated)
            {
                // Damit alle offenen Portal Clients das Hinzufügen eines neuen Commands mitbekommen
                await signalRHelper.SignalRHubProxy.Invoke <string>("IsmDevicesIndexChanged").ContinueWith(t =>
                {
                    //Console.WriteLine(t.Result);
                });
            }

            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            // Zuerst aus Identity Registry löschen, dann aus DB
            // Nur wenn erster Schritt gut ging weitermachen und aus DB löschen
            // Vermeidet Device Leak
            IsmDevice ismDevice = db.IsmDevices.Find(id);

            Task deleteFromIoTHubTask =
                Task.Factory.StartNew((stateObj) =>
            {
                //registryManager.RemoveDeviceAsync(db.IsmDevices.Find(id).DeviceId);
                registryManager.RemoveDeviceAsync(((IsmDevice)stateObj).DeviceId);
            }, ismDevice);

            var cts = new CancellationTokenSource();

            return(await deleteFromIoTHubTask
                   .ContinueWith <ActionResult>((ant) =>
            {
                db.IsmDevices.Remove(ismDevice);
                db.SaveChanges();
                cts.Cancel();
                return RedirectToAction("Index");
            }, TaskContinuationOptions.OnlyOnRanToCompletion)
                   .ContinueWith((ant, ct) =>
            {
            }, cts.Token)
                   .ContinueWith <ActionResult>((ant) =>
            {
                return RedirectToAction("Index");
            }, TaskContinuationOptions.NotOnCanceled));
        }
Esempio n. 4
0
 public ActionResult Edit([Bind(Include = "IsmDeviceId,DeviceId,LocationId,SoftwareId,HardwareId")] IsmDevice ismDevice)
 {
     if (ModelState.IsValid)
     {
         db.Entry(ismDevice).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.HardwareId = new SelectList(db.Hardware, "HardwareId", "Board", ismDevice.HardwareId);
     ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Country", ismDevice.LocationId);
     ViewBag.SoftwareId = new SelectList(db.Software, "SoftwareId", "SoftwareVersion", ismDevice.SoftwareId);
     return(View(ismDevice));
 }
Esempio n. 5
0
        // GET: IsmDevices/Provision/5
        public async Task <ActionResult> Provision(int id)
        {
            IsmDevice ismDevice = db.IsmDevices.Find(id);


            // Check that id was correct
            if (ismDevice == null)
            {
                return(HttpNotFound());
            }

            Device device = await registryManager.GetDeviceAsync(ismDevice.DeviceId);

            // Command for Command History
            Command cmd = new Command();

            cmd.Cmd         = CommandType.PROVISION;
            cmd.Timestamp   = DateTime.Now;
            cmd.IsmDeviceId = id;
            cmd.IsmDevice   = ismDevice;

            device.Status = DeviceStatus.Enabled;

            Task t = registryManager.UpdateDeviceAsync(device);

            try
            {
                await t;
            }
            catch (Exception)
            {
                // ignored
            }

            switch (t.Status)
            {
            case TaskStatus.RanToCompletion:
                cmd.CommandStatus = CommandStatus.SUCCESS;
                break;

            default:
                cmd.CommandStatus = CommandStatus.FAILURE;
                break;
            }

            // Write Command to DB
            db.Commands.Add(cmd);
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
Esempio n. 6
0
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            // Zuerst aus Identity Registry löschen, dann aus DB
            // Nur wenn erster Schritt gut ging weitermachen und aus DB löschen
            // Vermeidet Device Leak
            IsmDevice ismDevice = db.IsmDevices.Find(id);
            NewDevice newDevice;

            try
            {
                // Look for device in NewDevices database, since it might still be around.
                newDevice = db.NewDevices.First(d => d.DeviceId.Equals(ismDevice.DeviceId));
            }
            // If database is empty, we'll receive InvalidOperationException
            catch (InvalidOperationException e)
            {
                newDevice = null;
            }

            // Check that id was correct
            if (ismDevice == null)
            {
                return(HttpNotFound());
            }

            Task deleteFromIoTHubTask =
                Task.Factory.StartNew((stateObj) =>
            {
                //registryManager.RemoveDeviceAsync(db.IsmDevices.Find(id).DeviceId);
                registryManager.RemoveDeviceAsync(((IsmDevice)stateObj).DeviceId);
            }, ismDevice);

            var cts = new CancellationTokenSource();

            return(await deleteFromIoTHubTask
                   .ContinueWith <ActionResult>((ant) =>
            {
                if (newDevice != null)
                {
                    db.NewDevices.Remove(newDevice);
                }
                db.IsmDevices.Remove(ismDevice);
                db.SaveChanges();
                cts.Cancel();
                return RedirectToAction("Index");
            }, TaskContinuationOptions.OnlyOnRanToCompletion)
                   .ContinueWith((ant, ct) =>
            {
            }, cts.Token)
                   .ContinueWith <ActionResult>((ant) => RedirectToAction("Index"), TaskContinuationOptions.NotOnCanceled));
        }
        public async Task <ActionResult> Approve(int?id)
        {
            // Check that an ID has been passed
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var newDevice = db.NewDevices.Find(id);

            // Check that device exists
            if (newDevice == null)
            {
                return(HttpNotFound());
            }
            // If device has already been approved, don't go any further
            if (newDevice.Approved)
            {
                return(RedirectToAction("Index"));
            }
            // Create new IsmDevice to add to databasse
            var device = new IsmDevice
            {
                DeviceId     = newDevice.DeviceId,
                HardwareId   = newDevice.HardwareId,
                SoftwareId   = newDevice.ReleaseId,
                LocationId   = newDevice.LocationId,
                UpdateStatus = IsmIoTSettings.UpdateStatus.READY
            };

            // Add to DB
            db.IsmDevices.Add(device);
            db.SaveChanges();
            // Try to add to IoT Hub
            try
            {
                await AddDeviceAsync(device.DeviceId);

                // Approve device if exception hasn't been thrown
                newDevice.Approved = true;
                db.SaveChanges();
            }
            catch (Exception)
            {
                // Anzeigen, dass etwas schief ging
                // Eintrag aus DB entfernen
                db.IsmDevices.Remove(device);
                db.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
Esempio n. 8
0
        // GET: IsmDevices/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            IsmDevice ismDevice = db.IsmDevices.Find(id);

            if (ismDevice == null)
            {
                return(HttpNotFound());
            }
            return(View(ismDevice));
        }
Esempio n. 9
0
        // GET: IsmDevices/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            IsmDevice ismDevice = db.IsmDevices.Find(id);

            if (ismDevice == null)
            {
                return(HttpNotFound());
            }
            ViewBag.HardwareId = new SelectList(db.Hardware, "HardwareId", "Board", ismDevice.HardwareId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Country", ismDevice.LocationId);
            ViewBag.SoftwareId = new SelectList(db.Releases, "SoftwareId", "SoftwareVersion", ismDevice.SoftwareId);
            return(View(ismDevice));
        }
Esempio n. 10
0
        public async Task <ActionResult> Create([Bind(Include = "IsmDeviceId,DeviceId,LocationId,SoftwareId,HardwareId")] IsmDevice ismDevice)
        {
            if (ModelState.IsValid)
            {
                // Zuerst einmal in DB anlegen
                // Es darf nicht passieren, dass man einen eintrag in der Identity Registry anlegt
                // und diesen nicht in der DB registriert (= Device Leak)
                db.IsmDevices.Add(ismDevice);
                db.SaveChanges();

                try
                {
                    await AddDeviceAsync(ismDevice.DeviceId);
                }
                catch (Exception)
                {
                    // Anzeigen, dass etwas schief ging
                    // Eintrag aus DB entfernen
                }

                /*
                 * // Create Dashboard Queue for the new Device
                 * // http://stackoverflow.com/questions/30749945/create-azure-service-bus-queue-shared-access-policy-programmatically
                 * // http://www.cloudcasts.net/devguide/Default.aspx?id=12018
                 * //
                 * // Create a token provider with the relevant credentials.
                 * TokenProvider credentials = TokenProvider.CreateSharedAccessSignatureTokenProvider(IsmIoTSettings.Settings.sbRootSasName, IsmIoTSettings.Settings.sbRootSasKey);
                 * // Create a URI for the service bus.
                 * Uri serviceBusUri = ServiceBusEnvironment.CreateServiceUri("sb", IsmIoTSettings.Settings.sbNamespace, string.Empty);
                 * // Create a NamespaceManager for the specified namespace using the specified credentials.
                 * NamespaceManager namespaceManager = new NamespaceManager(serviceBusUri, credentials);
                 * string queueName = ismDevice.DeviceId; // Queue name equals DeviceId
                 * QueueDescription queueDescription = await namespaceManager.CreateQueueAsync(queueName);
                 */

                return(RedirectToAction("Index"));
            }

            ViewBag.HardwareId = new SelectList(db.Hardware, "HardwareId", "Board", ismDevice.HardwareId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Country", ismDevice.LocationId);
            ViewBag.SoftwareId = new SelectList(db.Software, "SoftwareId", "SoftwareVersion", ismDevice.SoftwareId);
            return(View(ismDevice));
        }
Esempio n. 11
0
        public ActionResult Edit(int?id, [Bind(Include = "IsmDeviceId,DeviceId,LocationId,SoftwareId,HardwareId")] IsmDevice ismDevice)
        {
            // Check Device ID against a whitelist of values to prevent XSS
            if (!IsmIoTSettings.RegexHelper.Text.IsMatch(ismDevice.DeviceId))
            {
                return(HttpNotFound());
            }
            // Check that POST device ID is the same as ID parameter
            if (id == null || id != ismDevice.IsmDeviceId)
            {
                return(HttpNotFound());
            }
            //// Return error if device doesn't exist
            if (!db.IsmDevices.Any(d => d.IsmDeviceId == ismDevice.IsmDeviceId))
            {
                //if (db.IsmDevices.Find(ismDevice.IsmDeviceId) == null)
                return(HttpNotFound());
            }
            // Check that unchangeable data (that is, current software and device ID) has not been tampered with.
            if (!db.IsmDevices.Any(d =>
                                   d.IsmDeviceId == ismDevice.IsmDeviceId &&
                                   d.DeviceId.Equals(ismDevice.DeviceId) &&
                                   d.SoftwareId == ismDevice.SoftwareId))
            {
                ;
            }
            return(HttpNotFound());

            if (ModelState.IsValid)
            {
                db.Entry(ismDevice).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.HardwareId = new SelectList(db.Hardware, "HardwareId", "Board", ismDevice.HardwareId);
            ViewBag.LocationId = new SelectList(db.Locations, "LocationId", "Country", ismDevice.LocationId);
            ViewBag.SoftwareId = new SelectList(db.Releases, "SoftwareId", "SoftwareVersion", ismDevice.SoftwareId);
            return(View(ismDevice));
        }