public void SecondPhase(DatabaseWrapper conn, int interval, List <DimTracker> trackersList)
        {
            Dictionary <string, DimTracker> trackers = trackersList.ToDictionary(x => x.GetKeyValue(), x => x);
            DateTime from = DateTime.Now.AddMinutes(-interval * 2);
            DateTime to   = DateTime.Now;
            Dictionary <long, FactActivity> dbData       = conn.GetActivities(from, to).ToDictionary(x => x.GetKeyValue(), x => x);
            List <FactActivity>             sensolusData = api.GetActivities(from, to, trackers);
            List <FactActivity>             toAdd        = new List <FactActivity>();
            List <FactActivity>             toUpdate     = new List <FactActivity>();

            foreach (var obj in sensolusData)
            {
                var key = obj.GetKeyValue();
                if (!dbData.ContainsKey(key))
                {
                    toAdd.Add(obj);
                }
                else if (!dbData[key].Equals(obj))
                {
                    toUpdate.Add(obj);
                }
            }
            conn.Insert(toAdd);
            conn.Update(toUpdate);
        }
        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);
        }