public void TemperatureHistory_WithSessionNotNull_ExpectOk()
        {
            //Arrange
            String expectedView = "TemperatureHistory";
            _controllerContext.Setup(cc => cc.HttpContext.Session["UserName"]).Returns(string.Format("{0} {1}", "FName", "LName"));
            _temperatureController.ControllerContext = _controllerContext.Object;
            TemperatureSearchModel temperatureSearchMl = new TemperatureSearchModel
            {
                DeviceId = 21,
                LogicalDeviceId = "ChetuTestDeviceId",
                ReadingTimeFrom = "2015-08-04 11:01:17",
                ReadingTimeTo = "2016-02-02 11:01:17"
            };
            SortingPagingInfo info = new SortingPagingInfo
            {
                SortField = Enums.TemperatureSortField.ReadingTime.ToString(),
                SortDirection = Constants.Ascending,
            };

            //Act
            var actionResult = _temperatureController.TemperatureHistory(temperatureSearchMl, info,"") as ViewResult;

            //Assert
            Assert.IsNotNull(actionResult, "Temperature action result should not be null");
            Assert.AreEqual(actionResult.ViewName, expectedView, "View name should be TemperatureHistory");
        }
        /// <summary>
        /// Displays the view
        /// </summary>
        /// <returns></returns>
        public ActionResult Temperature()
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    TemperatureSearchModel temperatureSearchMl = new TemperatureSearchModel();
                    SortingPagingInfo info = new SortingPagingInfo
                    {
                        SortField = Enums.TemperatureSortField.ReadingTime.ToString(),
                        SortDirection = Constants.Ascending,
                    };
                    ShowTemperatureGraph(temperatureSearchMl);
                    temperatureSearchMl.ReadingTimeFrom = DateTime.Now.AddDays(-6).ToShortDateString();
                    temperatureSearchMl.ReadingTimeTo = DateTime.Now.ToShortDateString();

                    if (temperatureSearchMl.TemperatureModels != null)
                        temperatureSearchMl.TemperatureModels = temperatureSearchMl.TemperatureModels.OrderByDescending(x => x.ReadingTime).Take(Constants.PagingPageSize).ToList();
                    ViewBag.SortingPagingInfo = info;
                    return View("TemperatureHistory", temperatureSearchMl);
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : Temperature() :: TemperatureController");
                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 : Temperature() :: TemperatureController");
                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 TemperatureHistory_WithSessionIsNull_ExpectUserLogin()
        {
            //Arrange
            String expectedController = "Login";
            String expectedAction = "UserLogin";
            _controllerContext.Setup(cc => cc.HttpContext.Session["UserName"]).Returns(null);
            _temperatureController.ControllerContext = _controllerContext.Object;
            TemperatureSearchModel temperatureSearchMl = new TemperatureSearchModel
            {
                DeviceId = 21,
                LogicalDeviceId = "ChetuTestDeviceId",
                ReadingTimeFrom = "2015-08-04 11:01:17",
                ReadingTimeTo = "2016-02-02 11:01:17"
            };
            SortingPagingInfo info = new SortingPagingInfo
            {
                SortField = Enums.TemperatureSortField.ReadingTime.ToString(),
                SortDirection = Constants.Ascending,
            };

            //Act
            var redirectToRouteResult = _temperatureController.TemperatureHistory(temperatureSearchMl, info,"") as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(redirectToRouteResult, "Not a redirect result");
            Assert.IsFalse(redirectToRouteResult.Permanent); // Or IsTrue if you use RedirectToActionPermanent
            Assert.AreEqual(expectedAction, redirectToRouteResult.RouteValues["Action"]);
            Assert.AreEqual(expectedController, redirectToRouteResult.RouteValues["Controller"]);
        }
        /// <summary>
        /// Search the temperature history by date and model
        /// </summary>
        /// <param name="temperatureSearchMl"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public ActionResult TemperatureHistory(TemperatureSearchModel temperatureSearchMl, SortingPagingInfo info, string command)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                if (Session["UserName"] != null)
                {
                    if (!string.IsNullOrEmpty(command) && command.Equals(JetstreamResource.SearchCommand) && !CheckDate(temperatureSearchMl.ReadingTimeFrom))
                    {
                        temperatureSearchMl.TemperatureModels = null;
                        ReloadGraph(temperatureSearchMl, info);
                        Warning(JetstreamResource.FromDateValidation, true);
                        return View("TemperatureHistory", temperatureSearchMl);
                    }

                    if (!string.IsNullOrEmpty(command) && command.Equals(JetstreamResource.SearchCommand) && !CheckDate(temperatureSearchMl.ReadingTimeTo))
                    {
                        temperatureSearchMl.TemperatureModels = null;
                        ReloadGraph(temperatureSearchMl, info);
                        Warning(JetstreamResource.ToDateValidation, true);
                        return View("TemperatureHistory", temperatureSearchMl);
                    }

                    DateTime? fromReadingTime = !string.IsNullOrEmpty(temperatureSearchMl.ReadingTimeFrom) ? Convert.ToDateTime(temperatureSearchMl.ReadingTimeFrom) : (DateTime?)null;
                    DateTime? toReadingTime = !string.IsNullOrEmpty(temperatureSearchMl.ReadingTimeTo) ? Convert.ToDateTime(temperatureSearchMl.ReadingTimeTo) : (DateTime?)null;

                    if (!string.IsNullOrEmpty(command) && command.Equals(JetstreamResource.SearchCommand) && fromReadingTime > toReadingTime)
                    {
                        temperatureSearchMl.TemperatureModels = null;
                        ReloadGraph(temperatureSearchMl, info);
                        Warning(JetstreamResource.DateValidation, true);
                        return View("TemperatureHistory", temperatureSearchMl);
                    }

                    ShowTemperatureGraph(temperatureSearchMl);
                    if (temperatureSearchMl.TemperatureModels != null)
                    {
                        temperatureSearchMl.TemperatureModels =
                            ApplySorting(temperatureSearchMl.TemperatureModels, info)
                                .Take(Constants.PagingPageSize)
                                .ToList();
                    }
                    ViewBag.SortingPagingInfo = info;
                    return View("TemperatureHistory", temperatureSearchMl);
                }
                else
                {
                    return RedirectToAction("UserLogin", "Login");
                }
            }
            catch (FaultException<ServiceData> fex)
            {
                objStringBuilderError.AppendLine("In method : TemperatureHistory(TemperatureSearchModel temperatureSearchMl, SortingPagingInfo info, string command) :: TemperatureController");
                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 : TemperatureHistory(TemperatureSearchModel temperatureSearchMl, SortingPagingInfo info, string command) :: TemperatureController");
                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 Show the Temperature Graph
        /// </summary>
        /// <param name="temperatureSearchMl"></param>
        private void ShowTemperatureGraph(TemperatureSearchModel temperatureSearchMl)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                BindDevices();

                using (JetstreamClient objMainServiceClient = new JetstreamClient())
                {
                    var lstTemperatureModel = objMainServiceClient.GetTemperatureList(temperatureSearchMl.LogicalDeviceId, temperatureSearchMl.ReadingTimeFrom, temperatureSearchMl.ReadingTimeTo);
                    TempData["TemperatureModels"] = lstTemperatureModel;
                    if (lstTemperatureModel.Any())
                    {
                        BindTemperatureChart(lstTemperatureModel);
                        temperatureSearchMl.TemperatureModels = lstTemperatureModel;
                    }
                    else
                    {
                        //Create the empty highchart which will be shown when the Temperature screen is loaded
                        var chart = new Highcharts(JetstreamResource.HighChartName)
                            .InitChart(new Chart { DefaultSeriesType = ChartTypes.Line })
                            .SetTitle(new Title { Text = JetstreamResource.HighChartTitleText, Style = JetstreamResource.HighChartStyle })
                            .SetSubtitle(new Subtitle { Text = "" })
                            .SetXAxis(new XAxis { Categories = null, EndOnTick = true })
                            .SetYAxis(new YAxis { Title = new YAxisTitle { Text = JetstreamResource.HighChartYAxisText, Style = JetstreamResource.HighChartStyle } })
                            .SetTooltip(new Tooltip
                            {
                                Enabled = true,
                                Formatter = @"function() {return '<b>' + this .series.name + '<br/><br/>'+this.x+': '+this.y; }"
                            })
                            .SetPlotOptions(new PlotOptions { Line = new PlotOptionsLine { DataLabels = new PlotOptionsLineDataLabels { Enabled = true }, EnableMouseTracking = false } })
                            .SetSeries(new[] { new Series { Name = JetstreamResource.ProbeA }, new Series { Name = JetstreamResource.ProbeB } });
                        ViewData["chart"] = chart;
                    }
                }
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : ShowTemperatureGraph(TemperatureSearchModel temperatureSearchMl) :: TemperatureController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());                
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="temperatureSearchMl"></param>
        /// <param name="info"></param>
        private void ReloadGraph(TemperatureSearchModel temperatureSearchMl, SortingPagingInfo info)
        {
            StringBuilder objStringBuilderError = new StringBuilder();
            try
            {
                BindDevices();
                //Create the empty highchart which will be shown when the Temperature screen is loaded
                var chart = new Highcharts(JetstreamResource.HighChartName)
                    .InitChart(new Chart { DefaultSeriesType = ChartTypes.Line })
                    .SetTitle(new Title { Text = JetstreamResource.HighChartTitleText, Style = JetstreamResource.HighChartStyle })
                    .SetSubtitle(new Subtitle { Text = "" })
                    .SetXAxis(new XAxis { Categories = null, EndOnTick = true })
                    .SetYAxis(new YAxis { Title = new YAxisTitle { Text = JetstreamResource.HighChartYAxisText, Style = JetstreamResource.HighChartStyle } })
                    .SetTooltip(new Tooltip
                    {
                        Enabled = true,
                        Formatter = @"function() {return '<b>' + this .series.name + '<br/><br/>'+this.x+': '+this.y; }"
                    })
                    .SetPlotOptions(new PlotOptions { Line = new PlotOptionsLine { DataLabels = new PlotOptionsLineDataLabels { Enabled = true }, EnableMouseTracking = false } })
                    .SetSeries(new[] { new Series { Name = JetstreamResource.ProbeA }, new Series { Name = JetstreamResource.ProbeB } });
                ViewData["chart"] = chart;
                ViewBag.SortingPagingInfo = info;
            }
            catch (Exception ex)
            {
                objStringBuilderError.AppendLine("In method : ReloadGraph(TemperatureSearchModel temperatureSearchMl, SortingPagingInfo info) :: TemperatureController");
                objStringBuilderError.AppendFormat("ErrorMessage::{0} {1}", ex.Message, Environment.NewLine);
                objStringBuilderError.AppendFormat("ErrorDetails::{0} {1}", Environment.NewLine, ex.ToString());

                SaveLogger.SaveLoggerError(objStringBuilderError.ToString());                
            }
        }