RemoveLogicalDevice removes a logical device from the Jetstream. Devices removed will no longer publish events or allow commands to be queued. Request class for RemoveLogicalDeviceRequest
Inheritance: JetstreamRequest
        /// <summary>
        /// Update devices to database
        /// </summary>
        /// <param name="deviceMl"></param>
        /// <returns></returns>
        public ActionResult UpdateDevice(DeviceModel deviceMl)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    var duplicateLogicalDeviceId = CheckForDuplicateId(deviceMl);
                    var duplicateSerial = CheckForDuplicateSerial(deviceMl);

                    if (duplicateLogicalDeviceId == 1 || duplicateSerial == 1)
                    {
                        Warning(JetstreamResource.DuplicateAddDeviceMessage, true);
                        return RedirectToAction("RedirectToEditDevice", new RouteValueDictionary(deviceMl));
                    }
                    else
                    {
                        if (ModelState.IsValid)
                        {
                            string oldLogicalDeviceId;
                            using (JetstreamClient objMainServiceClient = new JetstreamClient())
                            {
                                DeviceModel dm = objMainServiceClient.GetDeviceByDeviceId(deviceMl.DeviceId);
                                oldLogicalDeviceId = dm.LogicalDeviceId;
                            }

                            // construct a Jetstream service client
                            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                            RemoveLogicalDeviceRequest removeRequest = new RemoveLogicalDeviceRequest
                            {
                                LogicalDeviceId = oldLogicalDeviceId
                            };

                            //Remove device from jetstream
                            client.RemoveLogicalDevice(removeRequest);

                            using (JetstreamClient objMainServiceClient = new JetstreamClient())
                            {
                                objMainServiceClient.UpdateDevice(deviceMl);
                            }

                            AddLogicalDeviceRequest addRequest = new AddLogicalDeviceRequest
                            {
                                DeviceDefinitionId = deviceMl.DeviceGuid,
                                DeviceSerialNumber = deviceMl.SerialNumber,
                                LogicalDeviceId = deviceMl.LogicalDeviceId,
                                Region = (Regions)Enum.Parse(typeof(Regions), deviceMl.Region)
                            };

                            // call the Jetstream GetDeviceDefinitions ReST endpoint
                            AddLogicalDeviceResponse addResponse = client.AddLogicalDevice(addRequest);

                            Success(string.Format(JetstreamResource.DeviceSaveMessage, deviceMl.LogicalDeviceId, addResponse.Id), true);
                            return RedirectToAction("DeviceDetail", "Device");
                        }
                        else
                        {
                            return RedirectToAction("RedirectToEditDevice", new RouteValueDictionary(deviceMl));
                        }
                    }
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : UpdateDevice(DeviceModel deviceMl) :: DeviceController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : UpdateDevice(DeviceModel deviceMl) :: DeviceController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
        }
        /// <summary>
        /// Delete device from database
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public ActionResult DeleteDevice(int deviceId)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    using (JetstreamClient objMainServiceClient = new JetstreamClient())
                    {
                        var device = objMainServiceClient.GetDeviceById(deviceId);
                        objMainServiceClient.DeleteDevice(deviceId);

                        // construct a Jetstream service client
                        JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                        RemoveLogicalDeviceRequest removeRequest = new RemoveLogicalDeviceRequest
                        {
                            LogicalDeviceId = device.LogicalDeviceId
                        };

                        //Remove device from jetstream
                        client.RemoveLogicalDevice(removeRequest);

                    }
                    Success(JetstreamResource.DeviceDeleteMessage, true);
                    return RedirectToAction("DeviceDetail", "Device");
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : DeleteDevice(int deviceId) :: DeviceController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : DeleteDevice(int deviceId) :: DeviceController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
                return View("Error");
            }
        }
        public void RemoveLogicalDeviceTest()
        {
            try
            {
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the RemoveLogicalDevice request   
                RemoveLogicalDeviceRequest removeRequest = new RemoveLogicalDeviceRequest();
                removeRequest.LogicalDeviceId = LogicalDeviceId;

                // call the Jetstream RemoveLogicalDevice ReST endpoint   
                client.RemoveLogicalDevice(removeRequest);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }