public void ThirdPhase(DatabaseWrapper conn, string[] serials)
        {
            Dictionary <long, DimRule> rules = conn.GetRules().ToDictionary(x => x.GetKeyValue(), x => x);
            DateTime from = DateTime.MinValue;
            DateTime to   = DateTime.Now;

            Dictionary <string, FactActivity> activities = conn.GetActivities(from, to)
                                                           .GroupBy(x => x.Evttime.ToString() + x.Serial)
                                                           .Select(x => {
                var list = x.ToList();
                if (list.Count == 1)
                {
                    return(list.First());
                }
                if (list[0].Evttype == "ON_THE_MOVE" && list[1].Evttype == "STOP")
                {
                    return(list[1]);
                }
                else if (list[1].Evttype == "ON_THE_MOVE" && list[0].Evttype == "STOP")
                {
                    return(list[0]);
                }
                else if (list[0].Evttype == "START")
                {
                    return(list[1]);
                }
                else
                {
                    return(list[0]);
                }
            })
                                                           .ToDictionary(x => x.Evttime.ToString() + x.Serial, x => x);
            Dictionary <string, FactAlert> dbObjs = conn.GetAlerts().ToDictionary(x => x.GetKeyValue(), x => x);
            List <FactAlert> sensolusData         = api.GetAlerts(serials, activities, rules);
            List <FactAlert> toAdd    = new List <FactAlert>();
            List <FactAlert> toUpdate = new List <FactAlert>();

            foreach (var alert in sensolusData)
            {
                string alertKey = alert.GetKeyValue();
                if (!dbObjs.ContainsKey(alertKey))
                {
                    toAdd.Add(alert);
                }
                else if (!dbObjs[alertKey].Alertclear.Equals(alert.Alertclear))
                {
                    toUpdate.Add(alert);
                }
            }
            conn.Insert(toAdd);
            conn.Update(toUpdate);
        }