/// <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"); } }