public void AddLogicalDeviceTest()
        {
            try
            {
                // construct a Jetstream service client
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the request object
                AddLogicalDeviceRequest request = new AddLogicalDeviceRequest();
                request.DeviceSerialNumber = JetstreamConfiguration.GetDeviceSerailNumber();
                request.LogicalDeviceId = LogicalDeviceId;
                request.Region = Regions.US;
                request.DeviceDefinitionId = "5b6da680-fe54-41e3-968a-bd2ba1e23ebf";

                // call the Jetstream AddLogicalDevice ReST endpoint
                AddLogicalDeviceResponse response = client.AddLogicalDevice(request);

                if (response.Id != null)
                {
                    //  logicalDeviceId = response.Id;
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        /// <summary>
        /// Displays view to add new device
        /// </summary>
        /// <returns></returns>
        public ActionResult AddDevice()
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    // construct a Jetstream service client
                    JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                    // create the GetDeviceDefinitions request
                    GetDeviceDefinitionsRequest request = new GetDeviceDefinitionsRequest();

                    // call the Jetstream GetDeviceDefinitions ReST endpoint
                    GetDeviceDefinitionsResponse response = client.GetDeviceDefinitions(request);

                    if (response.Body != null)
                    {
                        //Get the device definations for binding to model dropdown
                        List<Models> modelList = response.DeviceDefinitionList.Select(model => new Models
                        {
                            ModelId = model.Id,
                            Name = model.Name
                        }).ToList();

                        ViewBag.ModelList = new SelectList(modelList.OrderBy(x => x.Name), "ModelId", "Name");
                    }
                    return View("AddDevice");
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : AddDevice() :: 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 : AddDevice() :: 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>
 /// Task for deleting messages from Jetstream Ground.
 /// </summary>
 /// <param name="state"></param>
 private void DeleteTask(CancellationToken ct, string batchId)
 {
     try
     {
         JetstreamServiceClient client  = new JetstreamServiceClient(this.JetstreamUrl, this.UserAccessKey);
         RemoveEventsRequest    request = new RemoveEventsRequest();
         request.BatchId = batchId;
         RemoveEventsResponse response = client.RemoveEvents(request);
     }
     catch (Exception ex)
     {
         EventLog.WriteEntry("JetstreamSDK",
                             ex.Message + "\n" + ex.StackTrace,
                             EventLogEntryType.Error);
     }
 }
        public void GetConfigurationTest()
        {
            try
            {
                // construct a Jetstream service client
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the GetConfiguration request
                GetConfigurationRequest request = new GetConfigurationRequest();

                // call the Jetstream GetConfiguration ReST endpoint
                client.GetConfiguration(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        /// <summary>
        /// Task for Receiving messages from Jetstream Ground.
        /// </summary>
        private string ReceiveTask(CancellationToken ct)
        {
            try
            {
                JetstreamServiceClient client   = new JetstreamServiceClient(this.JetstreamUrl, this.UserAccessKey);
                GetEventsRequest       request  = new GetEventsRequest();
                GetEventsResponse      response = client.GetEvents(request);
                string currentBatchId           = response.BatchId;
                List <TersoSolutions.Jetstream.SDK.Application.Messages.JetstreamEvent> messages = new List <TersoSolutions.Jetstream.SDK.Application.Messages.JetstreamEvent>();
                foreach (TersoSolutions.Jetstream.SDK.Application.Messages.JetstreamEvent message in response.Events)
                {
                    messages.Add(message);
                }

                // now add the Messages to the data store for the window thread
                if (!ct.IsCancellationRequested)
                {
                    lock (_setLock)
                    {
                        for (int i = 0; i < messages.Count; i++)
                        {
                            if (ct.IsCancellationRequested)
                            {
                                break;
                            }
                            _set.Add(messages[i]);
                        }
                    }
                }
                return(currentBatchId);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("JetstreamSDK",
                                    ex.Message + "\n" + ex.StackTrace,
                                    EventLogEntryType.Error);
            }
            return(string.Empty);
        }
        public void RemoveEventsTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                RemoveEventsRequest request = new RemoveEventsRequest();
                //string arerays
                string[] events = { "adf23b38-5d0f-4665-8624-e104ed1456e2", "35a170cc-7a06-41a6-aa60-db1f5f9e2534", "67f6aef6-35a2-46bb-bc7e-836f094bf7c8" };

                request.EventIds = events;

                // call the Jetstream ReST endpoint 
                client.RemoveEvents(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        /// <summary>
        /// Task for Receiving messages from Jetstream Ground.
        /// </summary>
        private string ReceiveTask(CancellationToken ct)
        {
            try
            {
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamUrl, UserAccessKey);
                GetEventsRequest request = new GetEventsRequest();
                GetEventsResponse response = client.GetEvents(request);
                string currentBatchId = response.BatchId;
                List<JetstreamEvent> messages = response.Events.ToList();

                // now add the Messages to the data store for the window thread
                if (ct.IsCancellationRequested)
                {
                    return currentBatchId;
                }

                lock (_setLock)
                {
                    foreach (JetstreamEvent t in messages)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                        _set.Add(t);
                    }
                }
                return currentBatchId;
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(EventLogSource, ex.Message + "\n" + ex.StackTrace, EventLogEntryType.Error);
            }
            return String.Empty;
        }
        public void DeviceSpecificCommandTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                GetPassesRequest request = new GetPassesRequest();
                request.LogicalDeviceId = LogicalDeviceId;

                // call the Jetstream ReST endpoint 
                client.DeviceSpecificCommand(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
 /// <summary>
 /// Task for deleting messages from Jetstream.
 /// <param name="batchId">The events batch ID to delete</param>
 /// </summary>
 private void DeleteTask(string batchId)
 {
     try
     {
         JetstreamServiceClient client = new JetstreamServiceClient(JetstreamUrl, UserAccessKey);
         RemoveEventsRequest request = new RemoveEventsRequest { BatchId = batchId };
         client.RemoveEvents(request);
     }
     catch (Exception ex)
     {
         EventLog.WriteEntry(EventLogSource, ex.Message + "\n" + ex.StackTrace, EventLogEntryType.Error);
     }
 }
        /// <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");
            }
        }
        public ActionResult AddDevice(DeviceModel deviceMl, string command)
        {
            StringBuilder objStringBuilderError = new StringBuilder();

            try
            {
                if (Session["UserName"] != null && string.Equals(command, JetstreamResource.AddCommand) && deviceMl != null)
                {
                    var duplicateLogicalDeviceId = CheckForDuplicateId(deviceMl);
                    var duplicateSerial = CheckForDuplicateSerial(deviceMl);

                    if (duplicateLogicalDeviceId == 1 || duplicateSerial == 1)
                    {
                        Warning(JetstreamResource.DuplicateAddDeviceMessage, true);
                        return RedirectToAction("RedirectToAddDevice", new RouteValueDictionary(deviceMl));
                    }
                    else
                    {
                        if (ModelState.IsValid)
                        {
                            // construct a Jetstream service client
                            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                            //Make logical device request object
                            AddLogicalDeviceRequest request = new AddLogicalDeviceRequest
                            {
                                DeviceDefinitionId = deviceMl.DeviceGuid,
                                DeviceSerialNumber = deviceMl.SerialNumber,
                                LogicalDeviceId = deviceMl.LogicalDeviceId,
                                Region = (Regions)Enum.Parse(typeof(Regions), deviceMl.Region)
                            };

                            // Add device to jetstream cloud
                            AddLogicalDeviceResponse response = client.AddLogicalDevice(request);

                            using (JetstreamClient objMainServiceClient = new JetstreamClient())
                            {
                                objMainServiceClient.SaveDevice(deviceMl);
                            }
                            Success(string.Format(JetstreamResource.DeviceSaveMessage, deviceMl.LogicalDeviceId, response.Id), true);
                            return RedirectToAction("DeviceDetail", "Device");
                        }
                        else
                        {
                            return RedirectToAction("RedirectToAddDevice", new RouteValueDictionary(deviceMl));
                        }
                    }
                }
                else if (string.Equals(command, JetstreamResource.CancelCommand))
                {
                    return RedirectToAction("DeviceDetail", "Device");
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : AddDevice(DeviceModel deviceMl, string command) :: 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 : AddDevice(DeviceModel deviceMl, string command) :: 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>
        /// Method to process the pass syncronization event
        /// </summary>
        private void ProcessPassSynchronizationEvent()
        {
            try
            {
                StringBuilder sb = new StringBuilder();

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

                // create the GetDeviceDefinitions request
                GetDeviceDefinitionsRequest deviceRequest = new GetDeviceDefinitionsRequest();

                // call the Jetstream GetDeviceDefinitions ReST endpoint
                GetDeviceDefinitionsResponse deviceResponse = client.GetDeviceDefinitions(deviceRequest);

                List<Device> devices = JetstreamEventRepository.GetDevices();

                //Get the passrfids to be added or updated
                List<User> users = JetstreamEventRepository.GetUser();
                List<string> passesAdd = users.Where(x => x.StatusId == 1).Select(x => x.PassRFID).Distinct().ToList();
                List<string> passesRemove = users.Where(x => x.StatusId == 0).Select(x => x.PassRFID).Distinct().ToList();

                foreach (var device in devices)
                {
                    var deviceModel = deviceResponse.DeviceDefinitionList.Where(x => x.Id == device.DeviceGuid).ToList();

                    if (deviceModel.Count > 0)
                    {
                        var jetstreamGetDeviceDefinitionsResponseDeviceDefinition = deviceModel.FirstOrDefault();
                        bool updatePass = jetstreamGetDeviceDefinitionsResponseDeviceDefinition != null && jetstreamGetDeviceDefinitionsResponseDeviceDefinition.CommandList.DeviceSpecificCommandList.DeviceSpecificCommand.ToList().Select(x => x.CommandName).Contains("UpdatePasses");

                        if (updatePass)
                        {
                            GetPassesRequest getPassRequest = new GetPassesRequest
                            {
                                LogicalDeviceId = device.LogicalDeviceId
                            };
                            DeviceSpecificCommandResponse getPassResponse = client.DeviceSpecificCommand(getPassRequest);

                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.LoadXml(getPassResponse.Body);

                            string xpath = System.Configuration.ConfigurationManager.AppSettings["xpath"];
                            var nodes = xmlDoc.SelectNodes(xpath);

                            if (nodes != null)
                                foreach (XmlNode childrenNode in nodes)
                                {
                                    var user = users.FirstOrDefault(x => x.PassRFID == childrenNode.Value);
                                    if (user == null)
                                    {
                                        passesRemove.Add(childrenNode.Value);
                                    }
                                    else
                                    {
                                        passesAdd.Remove(childrenNode.Value);
                                    }
                                }

                            UpdatePassesRequest updateRequest = new UpdatePassesRequest
                            {
                                LogicalDeviceId = device.LogicalDeviceId,
                                Add = passesAdd,
                                Remove = passesRemove
                            };

                            // call the Jetstream ResT endpoint 
                            client.DeviceSpecificCommand(updateRequest);
                        }
                    }
                    sb.AppendLine("ProcessPassSynchronizationEvent for Logical Device " + device.LogicalDeviceId);
                }

                // write the collected data to the event log
                EventLog.WriteEntry("JetstreamSDK ", sb.ToString());
            }
            catch (Exception)
            {
                // ignored
            }
        }
        public void GetConfigValuesCommandTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                GetConfigValuesCommandRequest request = new GetConfigValuesCommandRequest();
                request.LogicalDeviceId = LogicalDeviceId;

                List<String> lstParam = new List<String>();
                lstParam.Add("IP_SubNet_Gateway");
                request.Parameters = lstParam;

                // call the Jetstream ReST endpoint 
               client.GetConfigValuesCommand(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        public void GetEventsTest()
        {

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

            try
            {
                // create and configure the request object
                GetEventsRequest request = new GetEventsRequest();
                request.Limit = 2;
                // call the Jetstream ReST endpoint 
                client.GetEvents(request);
            }
            catch (Exception ex)
            {
               Assert.Fail(ex.ToString());
            }
        }
        public void AddDeviceToPolicyTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                AddDeviceToPolicyRequest request = new AddDeviceToPolicyRequest();
                request.LogicalDeviceId = LogicalDeviceId;

                request.PolicyId = _policyId;
                var param = new List<Tuple<string, string>>
                {
                    Tuple.Create("DNS", "192.168.92.1"),
                    Tuple.Create("Gateway", "192.168.92.100"),
                    Tuple.Create("IP", "192.168.92.101"),
                    Tuple.Create("Subnet", "255.255.255.0")
                };

                request.OverrideParameters = param;

                // call the Jetstream ReST endpoint 
                client.AddDeviceToPolicy(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        public void SetConfigValuesCommandTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                SetConfigValuesCommandRequest request = new SetConfigValuesCommandRequest();
                request.LogicalDeviceId = LogicalDeviceId;

                //Parameters
                var param = new List<Tuple<string, string>>
                    {
                    Tuple.Create( "IP", "10.6.20.241" ),
                    Tuple.Create( "SubNet", "255.255.255.0")    
                    };
                request.Parameters = param;

                // call the Jetstream ReST endpoint 
                client.SetConfigValuesCommand(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        public void AddPolicyTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                AddPolicyRequest request = new AddPolicyRequest();
                request.DeviceDefinitionId = "10f2bdb2-aa2f-44ea-8b74-0990f22b71c8";

                request.Name = JetstreamConfiguration.GetPolicyName();
                var param = new List<Tuple<string, string>>
                {
                    Tuple.Create( "aggregateeventscancount", "2" ),
                    Tuple.Create( "aggregateeventscantime", "10"),
                    Tuple.Create( "antenna1rxsensitivity", "50"),
                    Tuple.Create( "antenna1txpower", "30"),

                    Tuple.Create( "antenna2rxsensitivity", "50" ),
                    Tuple.Create( "antenna2txpower", "30"),
                    Tuple.Create( "antenna3rxsensitivity", "50"),
                    Tuple.Create( "antenna3txpower", "30"), 

                    Tuple.Create( "antenna4rxsensitivity", "50" ),
                    Tuple.Create( "antenna4txpower", "30"),
                    Tuple.Create( "commandpollinterval", "60"),
                    Tuple.Create( "dns", "0.0.0.0"),

                    Tuple.Create( "dooropentimelimit", "300" ),
                    Tuple.Create( "gateway", "0.0.0.0"),
                    Tuple.Create( "ip", "0.0.0.0"),
                    Tuple.Create( "jetstreamdeviceurl", "https://us-device.tersosolutions.com/v1.0/device/"),

                    Tuple.Create( "lockdownhightemp", "127" ),
                    Tuple.Create( "lockdownonacpowerfailure", "0"),
                    Tuple.Create( "lockdownonreaderfailure", "0"),
                    Tuple.Create( "lockdownonhightemp", "0"),

                    Tuple.Create( "logentryeventhightemp", "127" ),
                    Tuple.Create( "logentryeventlowtemp", "-128"),
                    Tuple.Create( "numberofantennas", "4"),
                    Tuple.Create( "logentrylevel", "warning"),

                    Tuple.Create( "objecteventscancount", "2" ),
                    Tuple.Create( "objecteventscantime", "10"),
                    Tuple.Create( "Subnet", "0.0.0.0")
                };

                request.Parameters = param;
                // call the Jetstream ReST endpoint 
                AddPolicyResponse response = client.AddPolicy(request);
                _policyId = response.Id;
                // display the successful result
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        public void UpdateFirmwareCommandTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                UpdateFirmwareCommandRequest request = new UpdateFirmwareCommandRequest();
                request.LogicalDeviceId = LogicalDeviceId;
                request.Component = Components.Agent;
                request.NewDeviceDefinitionId = Guid.NewGuid().ToString();

                // call the Jetstream ReST endpoint 
               client.UpdateFirmwareCommand(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        /// <summary>
        /// Method to update passes to all the device
        /// </summary>
        /// <param name="userMl"></param>
        private static void UpdatePasses(UsersModel userMl)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(userMl.PassRFID))
                {
                    // construct a Jetstream service client
                    JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url,
                        JetstreamConfiguration.ApplicationAccessKey);

                    // create the GetDeviceDefinitions request
                    GetDeviceDefinitionsRequest deviceRequest = new GetDeviceDefinitionsRequest();

                    // call the Jetstream GetDeviceDefinitions ReST endpoint
                    GetDeviceDefinitionsResponse deviceResponse = client.GetDeviceDefinitions(deviceRequest);

                    //List the user passRFId
                    List<string> passes = new List<string> { userMl.PassRFID.ToUpper() };

                    //Get the list of Devices from local database
                    IEnumerable<DeviceModel> devices;
                    using (JetstreamClient objMainServiceClient = new JetstreamClient())
                    {
                        devices = objMainServiceClient.GetDeviceList();
                    }

                    //Itterate through the list of devices
                    foreach (var device in devices)
                    {
                        //get the device defination list for checking if device allow pass update
                        var deviceModel =
                            deviceResponse.DeviceDefinitionList.Where(x => x.Id == device.DeviceGuid).ToList();

                        if (deviceModel.Count > 0)
                        {
                            //Check if device allow pass update
                            var jetstreamGetDeviceDefinitionsResponseDeviceDefinition = deviceModel.FirstOrDefault();
                            bool updatePass = jetstreamGetDeviceDefinitionsResponseDeviceDefinition != null &&
                                              jetstreamGetDeviceDefinitionsResponseDeviceDefinition.CommandList
                                                  .DeviceSpecificCommandList.DeviceSpecificCommand.ToList()
                                                  .Select(x => x.CommandName)
                                                  .Contains("UpdatePasses");

                            if (updatePass)
                            {
                                //Update Pass to device
                                UpdatePassesRequest updateRequest = new UpdatePassesRequest
                                {
                                    LogicalDeviceId = device.LogicalDeviceId
                                };
                                if (userMl.Status != null && userMl.Status.Value)
                                {
                                    updateRequest.Add = passes;
                                }
                                else
                                {
                                    updateRequest.Remove = passes;
                                }

                                client.DeviceSpecificCommand(updateRequest);
                            }
                        }
                    }
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : UpdatePasses(UsersModel userMl) :: UserController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", fex.Detail.ErrorMessage, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, fex.Detail.ErrorDetails);
                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : UpdatePasses(UsersModel userMl) :: UserController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());
            }
        }
        public void RemovePolicyTest()
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            try
            {
                // create and configure the request object
                RemovePolicyRequest request = new RemovePolicyRequest();
                request.PolicyId = _policyId;

                // call the Jetstream ReST endpoint 
                client.RemovePolicy(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        } 
        /// <summary>
        /// Edit current Device
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public ActionResult EditCurrentDevice(int deviceId)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                DeviceModel deviceMl;
                using (JetstreamClient objMainServiceClient = new JetstreamClient())
                {
                    deviceMl = objMainServiceClient.GetDeviceByDeviceId(deviceId);
                }

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

                // create the GetDeviceDefinitions request
                GetDeviceDefinitionsRequest request = new GetDeviceDefinitionsRequest();

                // call the Jetstream GetDeviceDefinitions ReST endpoint
                GetDeviceDefinitionsResponse response = client.GetDeviceDefinitions(request);

                if (!string.IsNullOrEmpty(response.Body))
                {
                    //Get the device definations for binding to model dropdown
                    var modelList = response.DeviceDefinitionList.Select(model => new Models
                    {
                        ModelId = model.Id,
                        Name = model.Name
                    }).ToList();

                    ViewBag.ModelList = new SelectList(modelList.OrderBy(x => x.Name), "ModelId", "Name", deviceMl.DeviceGuid);
                }

                return View("EditCurrentDevice", deviceMl);
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : EditCurrentDevice(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 : EditCurrentDevice(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());
            }
        }
        /// <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 GetDeviceDefinitionsTest()
        {
            try
            {   // construct a Jetstream service client
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the GetDeviceDefinitions request
                GetDeviceDefinitionsRequest request = new GetDeviceDefinitionsRequest();

                // call the Jetstream GetDeviceDefinitions ReST endpoint
                GetDeviceDefinitionsResponse response = client.GetDeviceDefinitions(request);

                if (response.Body != null)
                {
                    //contain data
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        /// <summary>
        /// Redirect to add device screen
        /// </summary>
        /// <param name="deviceMl"></param>
        /// <returns></returns>
        public ActionResult RedirectToAddDevice(DeviceModel deviceMl)
        {
            // construct a Jetstream service client
            JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

            // create the GetDeviceDefinitions request
            GetDeviceDefinitionsRequest request = new GetDeviceDefinitionsRequest();

            // call the Jetstream GetDeviceDefinitions ReST endpoint
            GetDeviceDefinitionsResponse response = client.GetDeviceDefinitions(request);

            if (response.Body != null)
            {
                //Get the device defination for binding to model dropdown
                List<Models> modelList = response.DeviceDefinitionList.Select(model => new Models
                {
                    ModelId = model.Id,
                    Name = model.Name
                }).ToList();

                ViewBag.ModelList = new SelectList(modelList.OrderBy(x => x.Name), "ModelId", "Name");
            }

            return View("AddDevice", deviceMl);
        }
        public void GetEpcListCommandTest()
        {
            try
            {
                JetstreamServiceClient client = new JetstreamServiceClient(JetstreamConfiguration.Url, JetstreamConfiguration.ApplicationAccessKey);

                // create the GetEPCListCommandRequest request   
                GetEpcListCommandRequest request = new GetEpcListCommandRequest();
                request.LogicalDeviceId = LogicalDeviceId;

                // call the Jetstream GetEPCListCommand ReST endpoint   
                client.GetEpcListCommand(request);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }