public async Task <ActionResult> EquipmentClassDashboard()
        {
            ViewBag.Version = Global._sfAdminVersion;
            int    EquipmentClassId = 0, DashboardId = 0;
            string endPoint = "", EquipmentId = "";

            ViewBag.WidgetOutput          = "<H2>Nothing Found.</H2>";
            ViewBag.WidgetUpdateFunctions = "";
            EmployeeSession empSession = null;

            if (Session["empSession"] != null)
            {
                empSession = EmployeeSession.LoadByJsonString(Session["empSession"].ToString());
            }
            ViewBag.PermissionList = empSession.permissions;

            if (Request.QueryString["equipmentId"] != null || Request.Form["equipmentId"] != null)
            {
                try
                {
                    RestfulAPIHelper apiHelper = new RestfulAPIHelper();
                    if (Request.QueryString["equipmentId"] != null)
                    {
                        EquipmentId = Request.QueryString["equipmentId"].ToString();
                    }
                    else
                    {
                        EquipmentId = Request.Form["equipmentId"].ToString();
                    }
                    endPoint = Global._equipmentEndPoint + "/" + EquipmentId;
                    string equipmentString = await apiHelper.callAPIService("GET", endPoint, null);

                    endPoint = Global._dashboardInCompanyEndPoint + "?type=equipmentclass";
                    string equipmentClassDashboardJson = await apiHelper.callAPIService("GET", endPoint, null);

                    try
                    {
                        dynamic equipmentObj = JObject.Parse(equipmentString);
                        EquipmentClassId    = equipmentObj.EquipmentClassId;
                        ViewBag.EquipmentId = (string)equipmentObj.EquipmentId;

                        dynamic dashboardObjs = JsonConvert.DeserializeObject(equipmentClassDashboardJson);
                        foreach (var dashboard in dashboardObjs)
                        {
                            if (dashboard.EquipmentClassId == EquipmentClassId)
                            {
                                DashboardId = dashboard.Id;
                                break;
                            }
                        }
                        if (DashboardId > 0)
                        {
                            ViewBag.WidgetCatalogList = await apiHelper.callAPIService("GET", Global._widgetCatalogInCompanyEndPoint + "?level=equipment", null);

                            ViewBag.DashboardId = DashboardId;
                            endPoint            = Global._widgetInDashboardEndPoint + "/" + DashboardId;
                            string widgetJson = await apiHelper.callAPIService("GET", endPoint, null);

                            if (!string.IsNullOrEmpty(widgetJson))
                            {
                                DashboardModel dashboardModel = new DashboardModel();
                                dashboardModel.GenerateWidgetHTMLContent(equipmentString, widgetJson);
                                ViewBag.WidgetOutput               = dashboardModel.GetWidgetHTMLContent();
                                ViewBag.WidgetUpdateFunctions      = dashboardModel.GetWidgetJavaScriptFunction();
                                ViewBag.AlarmWidgetUpdateFunctions = dashboardModel.GetAlarmWidgetJavaScriptFunction();
                            }
                        }
                    }
                    catch (Exception)
                    {
                        ;
                    }
                    ViewBag.CompanyId = empSession.companyId;
                }
                catch (Exception ex)
                {
                    LoginMsgSession loginMsgSession = new LoginMsgSession();
                    if (ex.Message.ToLower() == "invalid session")
                    {
                        loginMsgSession.toastLevel = "warning";
                        loginMsgSession.message    = "[[[Please Login]]]";
                    }
                    else
                    {
                        loginMsgSession.toastLevel = "error";
                        loginMsgSession.message    = "[[[Authentication Fail]]].";
                        StringBuilder logMessage = new StringBuilder();
                        logMessage.AppendLine("audit: Authentication Fail.");
                        logMessage.AppendLine("email:" + empSession.email);
                        logMessage.AppendLine("password:"******"loginMsgSession"] = loginMsgSession.Serialize();
                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View());
        }
        public async Task <ActionResult> FactoryDashboard()
        {
            ViewBag.Version         = Global._sfAdminVersion;
            ViewBag.WidgetOutput    = "<H2>Nothing Found.</H2>";
            ViewBag.GoogleMapAPIKey = Global._sfGoogleMapAPIKey;
            ViewBag.BaiduMapAPIKey  = Global._sfBaiduMapAPIKey;

            CompanyModel   companyModel = new CompanyModel();
            CompanySession compSession  = await companyModel.GetCompanySessionData();

            EmployeeSession empSession = null;

            if (Session["empSession"] != null)
            {
                empSession = EmployeeSession.LoadByJsonString(Session["empSession"].ToString());
            }

            ViewBag.PermissionList = empSession.permissions;


            if (Request.QueryString["factoryId"] != null || Request.Form["factoryId"] != null)
            {
                string factoryId = Request.QueryString["factoryId"] != null ? Request.QueryString["factoryId"] : Request.Form["factoryId"];
                try
                {
                    RestfulAPIHelper apiHelper     = new RestfulAPIHelper();
                    string           factoryString = await apiHelper.callAPIService("GET", Global._factoryEndPoint + "/" + factoryId, null);

                    ViewBag.Factory = factoryString;
                    dynamic factoryObj = JObject.Parse(factoryString);

                    string EquipmentString = await apiHelper.callAPIService("GET", Global._equipmentInFactoryEndPoint + "/" + factoryId, null);

                    dynamic EquipmentObjs = JsonConvert.DeserializeObject(EquipmentString);

                    String WidgetCatalog = await apiHelper.callAPIService("GET", Global._widgetCatalogInCompanyEndPoint + "?level=factory", null);

                    ViewBag.WidgetCatalogList = WidgetCatalog.Replace("\r\n", "").Replace("\\\\", "\\\\\\\\");


                    /* Construct an New JSON String which contain all equipment under the Factory;  */
                    /* Using ExpandoObject to add couple new element into JSON object, which will be use on JavaScript */

                    List <ExpandoObject> Equipments = new List <ExpandoObject>();
                    foreach (var equipmentObj in EquipmentObjs)
                    {
                        ExpandoObject newEquipmentObj = JsonConvert.DeserializeObject <ExpandoObject>(JsonConvert.SerializeObject(equipmentObj), new ExpandoObjectConverter());
                        AddExpandoObjectProperty(newEquipmentObj, "msgTimestamp", "");
                        AddExpandoObjectProperty(newEquipmentObj, "alarmMsgTimestamp", "");
                        Equipments.Add(newEquipmentObj);
                    }

                    ViewBag.EquipmentList = JsonConvert.SerializeObject(Equipments);
                    /* End JSON Construct */

                    string AlarmMessageString = "[]";
                    try
                    {
                        string endPoint = Global._alarmMessageInFactoryEndPoint + "/" + factoryId + "?hours=24&top=100&order=asc";
                        AlarmMessageString = await apiHelper.callAPIService("GET", endPoint, null);

                        dynamic alarmObjs = JsonConvert.DeserializeObject(AlarmMessageString);
                        ViewBag.AlarmMessageCount = alarmObjs.Count;
                        //AlarmMessageString = AlarmMessageString.Replace("\\\"", "");
                    }
                    catch (Exception ex)
                    {
                        ViewBag.AlarmMessageCount = 0;
                        StringBuilder logMessage = new StringBuilder();
                        logMessage.AppendLine("Error on retrieve AlarmMessage from DocDB");
                        logMessage.AppendLine("Exeption:" + ex.Message);
                        Global._sfAppLogger.Error(logMessage);
                    }

                    //ViewBag.AlarmMessageList = AlarmMessageString;

                    /* Get Factory Widget */
                    string factoryDashboardJson = await apiHelper.callAPIService("GET", Global._factoryDashboard + "/" + factoryId, null);

                    try
                    {
                        dynamic factoryDashboardObj = JsonConvert.DeserializeObject(factoryDashboardJson);

                        int DashboardId = (int)factoryDashboardObj[0].Id;

                        if (DashboardId > 0)
                        {
                            string widgetJson = await apiHelper.callAPIService("GET", Global._widgetInDashboardEndPoint + "/" + DashboardId, null);

                            if (!string.IsNullOrEmpty(widgetJson))
                            {
                                DashboardModel dashboardModel = new DashboardModel();
                                dashboardModel.GenerateFactoryWidgetHTMLContent(factoryObj, widgetJson, compSession, EquipmentString);
                                ViewBag.WidgetOutput          = dashboardModel.GetWidgetHTMLContent();
                                ViewBag.WidgetUpdateFunctions = dashboardModel.GetWidgetJavaScriptFunction();
                                ViewBag.DashboardId           = DashboardId;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        ;
                    }


                    /* Setup Company Name and Company Photo on Page */
                    ViewBag.CompanyId       = compSession.id;
                    ViewBag.CompanyName     = compSession.shortName;
                    ViewBag.CompanyPhotoURL = compSession.photoURL;

                    /* Setup Menu Item Active */
                    ViewBag.MenuNavigation = "";
                    ViewBag.MenuItem       = "";
                }
                catch (Exception ex)
                {
                    // EmployeeSession empSession = null;
                    //if (Session["empSession"] != null)
                    //  empSession = EmployeeSession.LoadByJsonString(Session["empSession"].ToString());
                    LoginMsgSession loginMsgSession = new LoginMsgSession();
                    if (ex.Message.ToLower() == "invalid session")
                    {
                        loginMsgSession.toastLevel = "warning";
                        loginMsgSession.message    = "[[[Please Login]]]";
                    }
                    else
                    {
                        loginMsgSession.toastLevel = "error";
                        loginMsgSession.message    = "[[[Authentication Fail]]].";
                        StringBuilder logMessage = new StringBuilder();
                        logMessage.AppendLine("audit: Authentication Fail.");
                        logMessage.AppendLine("email:" + empSession.email);
                        logMessage.AppendLine("password:"******"loginMsgSession"] = loginMsgSession.Serialize();
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View());
        }