async public Task <List <AnalyticsLog> > GetAnalyticsAPI() //Get workerrequest forcing update from database { //Determine max ID already pulled from server var internallogs = _connection.Table <AnalyticsLog>().ToListAsync().Result; int maxID; try { maxID = internallogs.Select(c => c.EntryID).Max(); } catch { maxID = 0; } //Pull only new entries from server IEnumerable <AnalyticsLog> tempA = await APIServer.GetAllAnalyticsLogs(maxID.ToString()); await _connection.InsertAllAsync(tempA); //insert database entries into local db (if any) return(await _connection.Table <AnalyticsLog>().ToListAsync()); }
//Refresh and Seed entire database async public Task RefreshDatabase() { Unit unit = new Unit(); Vessel vessel = new Vessel(); Worker worker = new Worker(); EntryLog newlog = new EntryLog(); AnalyticsLog Alog = new AnalyticsLog(); Random rnd = new Random(); //Add list of workers List <string> workerfirst = new List <string> { "Carl", "Sam", "Nisbit", "Johnny", "Earl", "Pete", "Douglas", "Cara", "Avril", "Betty-Sue", "Dougy", "Alex", "Tom", "Tara", "Indy", "Brad", "Stu", "Eddy", "Nero", "Carl", "Sam", "Nisbit", "Johnny", "Earl", "Pete", "Douglas", "Cara", "Avril", "Betty-Sue", "Dougy", "Alex", "Tom", "Tara", "Indy", "Brad", "Stu", "Eddy", "Nero" }; List <string> workerlast = new List <string> { "Johnson", "Black", "Trout", "Smith", "Rogers", "Jury", "Slick", "Salty", "Hardy", "Bolton", "Carter", "Silversmith", "Snake", "Black", "White", "Wattson", "Ericson", "Edge", "Winter", "Ruderson-Jergen", "Johnson", "Black", "Trout", "Smith", "Rogers", "Jury", "Slick", "Salty", "Hardy", "Bolton", "Carter", "Silversmith", "Snake", "Black", "White", "Wattson", "Ericson", "Edge" }; List <string> companyname = new List <string> { "IOL", "IOL", "IOL", "IOL", "CEDA", "CEDA", "TEAM", "TEAM", "TEAM", "TAMS", "TAMS", "TAMS", "TAMS", "Curren", "Curren", "Curren", "Safway", "Safway", "Safway", "IOL", "IOL", "IOL", "IOL", "CEDA", "CEDA", "TEAM", "TEAM", "TEAM", "TAMS", "TAMS", "TAMS", "TAMS", "Curren", "Curren", "Curren", "Safway", "Safway", "Safway" }; List <string> nfclist = new List <string> { "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138" }; string DB = "QQQ"; for (int i = 0; i <= workerfirst.Count - 1; i++) { worker.FirstName = workerfirst[i]; worker.LastName = workerlast[i]; worker.Company = companyname[i]; worker.CreatedTime = DateTime.Now; worker.Activated = 1; Globals.NFCtempcount++; worker.ReferenceNFC = DB + "_" + nfclist[i]; await AddWorker(worker); } string[] unitname = new string[] { "Office Building", "Plant 1", "Construction Site" }; foreach (string t in unitname) { //Add Unit from unitname list unit.Name = t; await App.Database.AddUnit(unit); //Add a random vessel tag for each unit (x3-6) int VperU = rnd.Next(3, 7); for (int i = 1; i <= VperU; i++) { int VorT = (int)rnd.Next(1, 3); string Vtype; if (VorT == 1) { Vtype = "Entrance"; } else { Vtype = "Checkpoint"; } int Vnum = rnd.Next(100, 400); vessel.Name = Vtype + "-" + Vnum; vessel.Unitname = t; await AddVessel(vessel); //50% Chance for active entry to be present on added vessel int status = rnd.Next(0, 2); if (status == 1) { int Logs = 6; int EntriesV = rnd.Next(0, Logs + 1); //Random number of entries per vessel //Create worker name temp lists to allow temporary removal as 'workers enter vessels' List <string> Ctemp = new List <string>(); Ctemp.AddRange(companyname); List <string> Ftemp = new List <string>(); Ftemp.AddRange(workerfirst); List <string> Ltemp = new List <string>(); Ltemp.AddRange(workerlast); List <string> Ntemp = new List <string>(); Ntemp.AddRange(nfclist); for (int k = 0; k < EntriesV; k++) { //Randomize worker selection and datetime stamp int W = rnd.Next(0, workerfirst.Count - k); int timestamp = rnd.Next(1, 10); /* //Create workerID Redundent due to NFC tie to worker * newlog.WorkerID = W+1; * newlog.Company = Ctemp[W]; * newlog.FirstName = Ftemp[W]; * newlog.LastName = Ltemp[W];*/ newlog.ReferenceNFC = DB + "_" + Ntemp[W]; newlog.TimeLog = DateTime.Now.AddHours(-timestamp); newlog.VesselName = vessel.Name; newlog.UnitName = vessel.Unitname; await AddLog(newlog); //Create analytics log (z days of data) for (int z = 1; z < 4; z++) { //adding time randomizers int xhour = rnd.Next(1, 8); int xhour2 = rnd.Next(1, 8); int xmin = rnd.Next(0, 59); int xmin2 = rnd.Next(0, 59); DateTime xtime = new DateTime(); //Create entry log Alog.ReferenceNFC = DB + "_" + Ntemp[W]; Alog.InOut = 1; xtime = DateTime.Now.AddHours(-(24 * z + xhour)); xtime = xtime.AddMinutes(-xmin); Alog.TimeLog = xtime; Alog.VesselName = vessel.Name; Alog.UnitName = vessel.Unitname; await APIServer.AddRecord(Alog); //create exit log Alog.ReferenceNFC = DB + "_" + Ntemp[W]; Alog.InOut = -1; xtime = DateTime.Now.AddHours(-(24 * z - xhour2)); xtime = xtime.AddMinutes(+xmin2); Alog.TimeLog = xtime; Alog.VesselName = vessel.Name; Alog.UnitName = vessel.Unitname; await APIServer.AddRecord(Alog); } // matching most recent entry of Entrylog Alog.ReferenceNFC = DB + "_" + Ntemp[W]; Alog.InOut = 1; Alog.TimeLog = DateTime.Now.AddHours(-timestamp); Alog.VesselName = vessel.Name; Alog.UnitName = vessel.Unitname; await APIServer.AddRecord(Alog); //Removing worker from list to avoid duplication Ctemp.RemoveAt(W); Ftemp.RemoveAt(W); Ltemp.RemoveAt(W); Ntemp.RemoveAt(W); } //Populate analytics list with database IEnumerable <AnalyticsLog> tempA = await APIServer.GetAllAnalyticsLogs("0"); _connection.DropTableAsync <AnalyticsLog>().Wait(); _connection.CreateTableAsync <AnalyticsLog>().Wait(); await _connection.InsertAllAsync(tempA); } } } return;// Task.CompletedTask; }