// GET: Dashboard
        public async Task <ActionResult> Index()
        {
            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;
            try
            {
                RestfulAPIHelper     apiHelper            = new RestfulAPIHelper();
                List <ExpandoObject> FactoryEquipmentList = new List <ExpandoObject>();

                ViewBag.WidgetCatalogList = await apiHelper.callAPIService("GET", Global._widgetCatalogInCompanyEndPoint + "?level=company", null);

                string FactoryString = await apiHelper.callAPIService("GET", Global._factoryInCompanyEndPoint, null);

                dynamic FactoryObjs = JsonConvert.DeserializeObject(FactoryString);

                /* Construct an New JSON String which contain all Factory and link to all equipment under each Factory;  */
                /* Using ExpandoObject to add couple new element into JSON object, which will be use on JavaScript */
                foreach (var factoryOjb in FactoryObjs)
                {
                    string equipmentInFactoryEndPoint = Global._equipmentInFactoryEndPoint + "/" + factoryOjb.Id;
                    string EquipmentString            = await apiHelper.callAPIService("GET", equipmentInFactoryEndPoint, null);

                    dynamic EquipmentObjs = JsonConvert.DeserializeObject(EquipmentString);

                    ExpandoObject newFactoryObj = JsonConvert.DeserializeObject <ExpandoObject>(JsonConvert.SerializeObject(factoryOjb), new ExpandoObjectConverter());
                    AddExpandoObjectProperty(newFactoryObj, "alarm24H", 0);

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

                    AddExpandoObjectProperty(newFactoryObj, "Equipments", Equipments);
                    FactoryEquipmentList.Add(newFactoryObj);
                }
                ViewBag.FactoryEquipmentList = JsonConvert.SerializeObject(FactoryEquipmentList);
                /* End JSON Construct */

                string AlarmMessageString = "[]";
                try
                {
                    string endPoint = Global._alarmMessageInCompanyEndPoint + "?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 Company Widget */
                string companyDashboardJson = await apiHelper.callAPIService("GET", Global._dashboardInCompanyEndPoint + "?type=company", null);

                try
                {
                    dynamic companyDashboardObj = JsonConvert.DeserializeObject(companyDashboardJson);
                    int     DashboardId         = (int)companyDashboardObj[0].Id;

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

                        if (!string.IsNullOrEmpty(widgetJson))
                        {
                            DashboardModel dashboardModel = new DashboardModel();
                            dashboardModel.GenerateCompanyWidgetHTMLContent(widgetJson, compSession);
                            ViewBag.WidgetUpdateFunctions = dashboardModel.GetWidgetJavaScriptFunction();
                            ViewBag.WidgetOutput          = dashboardModel.GetWidgetHTMLContent();
                            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());
        }