public Boolean DetectRest(DatabaseContext context, String roomName, DateTime start, DateTime close, int sekvCount, List <Tep_Sekvencia> tep) { if (!roomName.Equals("Obývačka") && !roomName.Equals("Obyvacka")) { return(false); } if (sekvCount > 2) { return(false); } try { //tep = context.PulseSekv.Where(p => DateTime.Parse(p.TimeStart) >= start && DateTime.Parse(p.TimeStart) <= close || DateTime.Parse(p.TimeStart) <= start && DateTime.Parse(p.TimeClose) > start || DateTime.Parse(p.TimeStart) < close && DateTime.Parse(p.TimeClose) >= close).ToList(); double sumPulse = 0; double sumAlert = 0; foreach (var a in tep) { sumPulse += a.Sekvencia; sumAlert += a.Upozornenie; } sumAlert = sumAlert / tep.Count; if (sumAlert > 1) { return(false); } int id = tep.First().TepSekvId - 1; Tep_Sekvencia before = context.PulseSekv.Where(a => a.TepSekvId == id).First(); sumPulse = sumPulse / tep.Count; if (before.Sekvencia < sumPulse) { return(false); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("rest pulse" + e.ToString()); } return(true); }
public void PulseSequencer(DatabaseContext context) { var all1 = context.Pulse.Where(t => t.TepSekvId == null).ToList(); LimitCheck limitCheck = new LimitCheck(); Hranice_Tep ht = context.PulseLimit.FirstOrDefault(h => h.Hranica_TepId == context.PulseLimit.Max(x => x.Hranica_TepId)); foreach (var a in all1) { //System.Diagnostics.Debug.WriteLine("SPRACOVAVAM PULZ HODNOTU DO SEKVENCIE: " + a.TepId + " " + a.Hodnota + " " + a.TimeStamp); /*Inicializacia ak ziadna sekvencia neexistuje*/ if (!context.PulseSekv.Any()) { Tep_Sekvencia tepS = new Tep_Sekvencia { TepSekvId = 1, Sekvencia = (int)Math.Round(a.Hodnota), TimeStart = a.TimeStamp, TimeClose = "", Upozornenie = limitCheck.CheckPulseLimits(context, (int)Math.Round(a.Hodnota)), //Hranice_Tep = ht, Hranica_TepFK = ht.Hranica_TepId, }; if (tepS.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Tep", a.Hodnota, a.TimeStamp, tepS.Upozornenie, a.TepId); } Tep t = context.Pulse.Where(c => c.TepId == a.TepId).First(); t.TepSekvId = 1; //System.Diagnostics.Debug.WriteLine("Novasekvencia " + t.TepId + " " + a.TepId + " " + t.Hodnota + " + " + t.TepSekvId); context.PulseSekv.Add(tepS); context.Pulse.Update(t); try { //System.Diagnostics.Debug.WriteLine("SPRACOVAVAM PULZ HODNOTU DO SEKVENCIE: INICIALIZACNA"); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Nemozem najst otvorenu sekvenciu"); } } /*Ak existuju sekvencie*/ else { /*Najdi otvorenu sekvenciu*/ Tep_Sekvencia tepS = null; try { tepS = context.PulseSekv.FirstOrDefault(t => t.TimeClose == ""); //System.Diagnostics.Debug.WriteLine("otvorena sekvencia " + tepS.TepSekvId + " " + tepS.Sekvencia); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Nemozem najst otvorenu sekvenciu"); } double minutes = 0; try { Tep posl = context.Pulse.Where(p => p.TepSekvId == tepS.TepSekvId).Last(); DateTime startTime = DateTime.Parse(posl.TimeStamp); //DateTime startTime = DateTime.Parse(tepS.TimeStart); DateTime endTime = DateTime.Parse(a.TimeStamp); TimeSpan finalTime = endTime - startTime; minutes = finalTime.TotalMinutes; // System.Diagnostics.Debug.WriteLine("/////////////////////// TIME DIFF pulse" + finalTime.Hours + ":" + finalTime.Minutes + ":" + finalTime.Seconds + ":" + finalTime.Milliseconds); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } /*Spracovavana hodnota patri do aktualne otvorenej sekvencie*/ if (limitCheck.CheckPulseLimits(context, (int)Math.Round(a.Hodnota)) == tepS.Upozornenie && minutes <= 1) { var allInSekv = context.Pulse.Where(p => p.TepSekvId == tepS.TepSekvId).ToList(); int median = 0; if (tepS.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Tep", a.Hodnota, a.TimeStamp, tepS.Upozornenie, a.TepId); } /*Aktualizuj hodnotu sekvencie*/ foreach (var allIN in allInSekv) { median += (int)Math.Round(allIN.Hodnota); } median += (int)Math.Round(a.Hodnota); median = median / (allInSekv.Count + 1); a.TepSekvId = tepS.TepSekvId; //naviaz tep //a.Tep_Sekvencia = tepS; context.Pulse.Update(a); tepS.Sekvencia = median; context.PulseSekv.Update(tepS); try { ///System.Diagnostics.Debug.WriteLine("SPRACOVAVAM PULZ HODNOTU DO SEKVENCIE: UKLADAM DO AKTUALNEJ " + a.TepId + " " + a.Hodnota +" " +a.TimeStamp + " " + tepS.TepSekvId + " " + tepS.Sekvencia); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Nemozem najst otvorenu sekvenciu"); } } else /*Spracovavana hodnota nepatri do aktualne otvorenej sekvencie*/ { /* Zatvor poslednu sekvenciu*/ Tep_Sekvencia ts = context.PulseSekv.FirstOrDefault(t => t.TepSekvId == context.PulseSekv.Max(x => x.TepSekvId)); //najdi poslednu var allInSekv1 = context.Pulse.Where(p => p.TepSekvId == ts.TepSekvId).ToList(); // najdi list pre poslednu var last = allInSekv1[allInSekv1.Count - 1]; //zober posledny pulz if (minutes <= 1) { ts.TimeClose = a.TimeStamp; // nastav konecny TS podla TS posledneho v sekvencii / edit-podla novej hodnoty aby som pokril celu casovu os } else { ts.TimeClose = last.TimeStamp; } context.PulseSekv.Update(ts); context.Pulse.RemoveRange(allInSekv1); /*Vytvor novu sekvenciu*/ Tep_Sekvencia tepS1 = new Tep_Sekvencia { TepSekvId = ts.TepSekvId + 1, Sekvencia = (int)Math.Round(a.Hodnota), TimeStart = a.TimeStamp, TimeClose = "", Upozornenie = limitCheck.CheckPulseLimits(context, (int)Math.Round(a.Hodnota)), //Hranice_Tep = ht, Hranica_TepFK = ht.Hranica_TepId }; context.PulseSekv.Add(tepS1); if (tepS1.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Tep", a.Hodnota, a.TimeStamp, tepS1.Upozornenie, a.TepId); } a.TepSekvId = ts.TepSekvId + 1; //naviaz pulz na sekvenciu //a.Tep_Sekvencia = tepS1; context.Pulse.Update(a); try { //System.Diagnostics.Debug.WriteLine("SPRACOVAVAM PULZ HODNOTU DO SEKVENCIE: VYTVATAM NOVU SEKVENCIU" + a.TepId + " " + a.Hodnota + " " + a.TimeStamp + " " + tepS1.TepSekvId + " " + tepS1.Sekvencia); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Nemozem najst otvorenu sekvenciu"); } } } } }
public void setAlertListValue() { if (_context.PulseSekv.Any()) { var all = _context.PulseSekv.OrderBy(x => x.TimeStart).ToList(); all.Reverse(); Tep_Sekvencia ts = null; foreach (var a in all) { ts = a; if (a.Upozornenie != 0) { break; } } // Tep_Sekvencia ts = _context.PulseSekv.FirstOrDefault(t => t.TepSekvId == _context.PulseSekv.Max(x => x.TepSekvId)); Helpers.SekvenceHelper.LimitCheck loader = new Helpers.SekvenceHelper.LimitCheck(); if (ts.Upozornenie != 0) { DateTime convertedDate = DateTime.Parse(ts.TimeStart); AlertSequenceObj myObj = new AlertSequenceObj { Name = "Tep", Value = ts.Sekvencia + " BPM", Date = convertedDate.ToShortDateString(), Time = convertedDate.ToLongTimeString(), Alert = loader.getStringValuePulseAndTempLimit(ts.Upozornenie) }; Alerts.Add(myObj); } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Tep", Alert = "Neexistuje upozornenie" }; Alerts.Add(myObj); } //String tepSekvString = "Tep " + ts.Sekvencia + " " + ts.TimeStart + " " + loader.getStringValuePulseAndTempLimit(ts.Upozornenie); } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Tep", Alert = "Neexistuje sekvencia" }; Alerts.Add(myObj); } if (_context.TemperatureSekv.Any()) { var all = _context.TemperatureSekv.OrderBy(x => x.TimeStart).ToList(); all.Reverse(); Teplota_Sekvencia ts = null; foreach (var a in all) { ts = a; if (a.Upozornenie != 0) { break; } } Helpers.SekvenceHelper.LimitCheck loader = new Helpers.SekvenceHelper.LimitCheck(); if (ts.Upozornenie != 0) { DateTime convertedDate = DateTime.Parse(ts.TimeStart); AlertSequenceObj myObj = new AlertSequenceObj { Name = "Teplota", Value = ts.Sekvencia.ToString("n2") + " °C", Date = convertedDate.ToShortDateString(), Time = convertedDate.ToLongTimeString(), Alert = loader.getStringValuePulseAndTempLimit(ts.Upozornenie) }; Alerts.Add(myObj); } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Teplota", Alert = "Neexistuje upozornenie" }; Alerts.Add(myObj); } } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Teplota", Alert = "Neexistuje sekvencia" }; Alerts.Add(myObj); } if (_context.MovementSekv.Any()) { var all = _context.MovementSekv.OrderBy(x => x.TimeStamp).ToList(); all.Reverse(); Pohyb_Sekvencia ps = null; foreach (var a in all) { if (a.Upozornenie_Cas != 0) { ps = a; break; } if (a.Upozornenie_Hranica != 0) { ps = a; break; } } if (ps != null) { DateTime convertedDate = DateTime.Parse(ps.TimeStamp); if (ps.Upozornenie_Cas != 0) { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pohyb", Date = convertedDate.ToShortDateString(), Time = convertedDate.ToLongTimeString(), Alert = "Čas" }; Alerts.Add(myObj); } else if (ps.Upozornenie_Hranica != 0) { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pohyb", Date = convertedDate.ToShortDateString(), Time = convertedDate.ToLongTimeString(), Alert = "Hranica" }; Alerts.Add(myObj); } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pohyb", Alert = "Neexistuje upozornenie" }; Alerts.Add(myObj); } } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pohyb", Alert = "Neexistuje upozornenie" }; Alerts.Add(myObj); } } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pohyb", Alert = "Neexistuje sekvencia" }; Alerts.Add(myObj); } if (_context.Akcelerometers.Any()) { var pad = _context.Akcelerometers.FirstOrDefault(t => t.AkcelerometerID == _context.Akcelerometers.Max(x => x.AkcelerometerID)); DateTime convertedDate = DateTime.Parse(pad.TimeStamp); RoomsDetection roomsDetection = new RoomsDetection(); Izby izba = roomsDetection.findRoomByCoord(pad.Xhodnota, pad.Yhodnota); string izbameno = "Vonku"; try { izbameno = izba.Nazov; }catch (Exception e) { System.Diagnostics.Debug.WriteLine("EXCEPTION " + e.ToString()); } AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pád", Date = convertedDate.ToShortDateString(), Time = convertedDate.ToLongTimeString(), Value = izbameno, Alert = "Nastal pád" }; Alerts.Add(myObj); } else { AlertSequenceObj myObj = new AlertSequenceObj { Name = "Pád", Alert = "Pád nenastal" }; Alerts.Add(myObj); } //TODO: ZOBRAZENIE POHYBU }
public void concatPulseSequences(int sekvId) { DatabaseContext context = new DatabaseContext(); Tep_Sekvencia prevPulse = new Tep_Sekvencia(); int prevSekvid = sekvId - 1; try { prevPulse = context.PulseSekv.Where(p => p.TepSekvId == prevSekvid).First(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } bool isLong = true; try { DateTime startTime = DateTime.Parse(prevPulse.TimeStart); DateTime endTime = DateTime.Parse(prevPulse.TimeClose); TimeSpan finalTime = endTime - startTime; if (finalTime.Minutes <= LENGTH_BORDER) { isLong = false; } else { return; } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } if (isLong == true) { return; } Tep_Sekvencia actPulse = new Tep_Sekvencia(); try { actPulse = context.PulseSekv.Where(p => p.TepSekvId == sekvId).First(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } int hodnota = (prevPulse.Sekvencia + actPulse.Sekvencia) / 2; Tep_Sekvencia newPulse = new Tep_Sekvencia { TepSekvId = prevSekvid, Sekvencia = hodnota, TimeStart = prevPulse.TimeStart, TimeClose = actPulse.TimeClose, Hranica_TepFK = actPulse.Hranica_TepFK, Upozornenie = findMax(actPulse.Upozornenie, prevPulse.Upozornenie) }; context.PulseSekv.Remove(prevPulse); context.PulseSekv.Remove(actPulse); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } context.PulseSekv.Add(newPulse); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } }
public Boolean DetectCleaning(DatabaseContext context, DateTime start, DateTime close, int sekvCount, List <Tep_Sekvencia> tep, List <Teplota_Sekvencia> teplota) { if (sekvCount < 5) { return(false); } //tep = context.PulseSekv.Where(p => DateTime.Parse(p.TimeStart) >= start && DateTime.Parse(p.TimeStart) <= close || DateTime.Parse(p.TimeStart) <= start && DateTime.Parse(p.TimeClose) > start || DateTime.Parse(p.TimeStart) < close && DateTime.Parse(p.TimeClose) >= close).ToList(); double sumPulse = 0; foreach (var a in tep) { sumPulse += a.Sekvencia; if (a.Upozornenie > 1) { return(false); } } int id = tep.First().TepSekvId - 1; try { Tep_Sekvencia before = context.PulseSekv.Where(a => a.TepSekvId == id).First(); sumPulse = sumPulse / tep.Count; if (before.Sekvencia > sumPulse) { return(false); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("cleaning detection pulse" + e.ToString()); return(false); } //teplota = context.TemperatureSekv.Where(p => DateTime.Parse(p.TimeStart) >= start && DateTime.Parse(p.TimeStart) <= close || DateTime.Parse(p.TimeStart) <= start && DateTime.Parse(p.TimeClose) > start || DateTime.Parse(p.TimeStart) < close && DateTime.Parse(p.TimeClose) >= close).ToList(); double sumTemp = 0; foreach (var a in teplota) { sumTemp += a.Sekvencia; } id = teplota.Last().TeplSekvId - 1; try { Teplota_Sekvencia before = context.TemperatureSekv.Where(a => a.TeplSekvId == id).First(); sumTemp = sumTemp / teplota.Count; if (before.Sekvencia > sumTemp) { return(false); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("cleaning detection temperature" + e.ToString()); } return(true); }
public Boolean DetectSleep(DatabaseContext context, String roomName, DateTime start, DateTime close, int sekvCount, List <Tep_Sekvencia> tep) { if (!roomName.Equals("Spálňa") && !roomName.Equals("Spalna") && !roomName.Equals("Obývačka") && !roomName.Equals("Obyvacka")) { return(false); } if (sekvCount > 3) { return(false); } SettingsController.SleepTime = new TimeSpan(0, 20, 0, 0).ToString(); string wakeTime = new TimeSpan(0, 7, 0, 0).ToString(); /*SettingsController.SleepTime = new TimeSpan(0, 8, 0, 0).ToString(); * string wakeTime = new TimeSpan(0, 20, 0, 0).ToString();*/ string sleepTime = SettingsController.SleepTime; if (sleepTime == string.Empty) { return(false); } if (start.TimeOfDay <= TimeSpan.Parse(sleepTime) && start.TimeOfDay > TimeSpan.Parse(wakeTime)) { return(false); } //tep = context.PulseSekv.Where(p => DateTime.Parse(p.TimeStart) >= start && DateTime.Parse(p.TimeStart) <= close || DateTime.Parse(p.TimeStart) <= start && DateTime.Parse(p.TimeClose) > start || DateTime.Parse(p.TimeStart) < close && DateTime.Parse(p.TimeClose) >= close).ToList(); if (tep.Count == 0) { return(false); } double sumPulse = 0; foreach (var a in tep) { sumPulse += a.Sekvencia; if (a.Upozornenie > 0) { if (a.Sekvencia > 60) { return(false); } } } try { int id = tep.Last().TepSekvId - 1; Tep_Sekvencia before = context.PulseSekv.Where(a => a.TepSekvId == id).First(); sumPulse = sumPulse / tep.Count; if (before.Sekvencia <= sumPulse) { return(false); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("sleeep detection " + e.ToString()); } return(true); }