public int getMaxMovementSekvIndex() { _context = new DatabaseContext(); Pohyb_Sekvencia pohS = _context.MovementSekv.FirstOrDefault(t => t.PohSekvId == _context.MovementSekv.Max(x => x.PohSekvId)); return(pohS.PohSekvId); }
public int checkTimeLimitMovement(DatabaseContext context, Pohyb_Sekvencia pohS) { Hranice_Pohyb hpoh = context.MovementLimit.FirstOrDefault(t => t.HranicePohybId == context.MovementLimit.Max(x => x.HranicePohybId)); int zot = 0; Int32.TryParse(pohS.Cas_Zotrvania, out zot); int hran = 0; Int32.TryParse(hpoh.LimitCas, out hran); if (zot > hran) { return(1); } else { return(0); } }
public bool checkMovementValue(DatabaseContext context, Pohyb p, Pohyb_Sekvencia pohS) { Hranice_Pohyb hpoh = context.MovementLimit.FirstOrDefault(t => t.HranicePohybId == context.MovementLimit.Max(x => x.HranicePohybId)); float hranica = hpoh.OkruhHranica / 10; if (((pohS.Xhodnota - hranica) <= p.Xhodnota) && ((pohS.Xhodnota + hranica) >= p.Xhodnota) && ((pohS.Yhodnota - hranica) <= p.Yhodnota) && ((pohS.Yhodnota + hranica) >= p.Yhodnota)) {//neprekracuje limit vo vzdialenosti //skontroluj ci patri do rovnakej izby Izby izba = new RoomsDetection().findRoom(p); if (izba == null) { if (pohS.IzbyFK == null) { return(true); } else { return(false); } } else { if (pohS.IzbyFK == izba.IzbaID) { return(true); } else { return(false); } } } else { return(false); } }
public void MovementSequencer(DatabaseContext context) { var allMovements = context.Movement.Where(t => t.PohSekvFK == null).ToList(); LimitCheck limitCheck = new LimitCheck(); Hranice_Pohyb hpoh = context.MovementLimit.FirstOrDefault(t => t.HranicePohybId == context.MovementLimit.Max(x => x.HranicePohybId)); foreach (var mov in allMovements) { Izby izba = new RoomsDetection().findRoom(mov); /*Inicializacia ak ziadna sekvencia neexistuje*/ if (!context.MovementSekv.Any()) { Pohyb_Sekvencia pohS = null; if (izba == null) { pohS = new Pohyb_Sekvencia { PohSekvId = 1, Xhodnota = mov.Xhodnota, Yhodnota = mov.Yhodnota, TimeStamp = mov.TimeStamp, Cas_Zotrvania = "", TimeStop = mov.TimeStamp, Upozornenie_Cas = 0, Upozornenie_Hranica = limitCheck.checkIfOutside(context, mov), //Hranice_Pohyb = hpoh, HranicePohybFK = hpoh.HranicePohybId, }; } else { pohS = new Pohyb_Sekvencia { PohSekvId = 1, Xhodnota = mov.Xhodnota, Yhodnota = mov.Yhodnota, TimeStamp = mov.TimeStamp, Cas_Zotrvania = "", TimeStop = mov.TimeStamp, Upozornenie_Cas = 0, Upozornenie_Hranica = limitCheck.checkIfOutside(context, mov), //Hranice_Pohyb = hpoh, HranicePohybFK = hpoh.HranicePohybId, //Izby = izba, IzbyFK = izba.IzbaID }; } if (pohS.Upozornenie_Hranica == 1) { new NotificationGenerator().GenerateNotificationMovement(pohS.TimeStamp, pohS.PohSekvId); } Pohyb p = context.Movement.Where(c => c.PohybId == mov.PohybId).First(); p.PohSekvFK = 1; System.Diagnostics.Debug.WriteLine("POHYB Inicializacia ak ziadna sekvencia neexistuje"); context.MovementSekv.Add(pohS); //context.Movement.Remove(mov); context.Movement.Update(p); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } } /*Ak existuju sekvencie*/ else { /*Najdi otvorenu sekvenciu*/ Pohyb_Sekvencia pohS = null; try { pohS = context.MovementSekv.FirstOrDefault(p => p.PohSekvId == context.MovementSekv.Max(x => x.PohSekvId)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("POHYB EXC Nemozem najst otvorenu sekvenciu"); } double minutes = 0; try { DateTime startTime = DateTime.Parse(pohS.TimeStop); DateTime endTime = DateTime.Parse(mov.TimeStamp); TimeSpan finalTime = endTime - startTime; minutes = finalTime.TotalMinutes; System.Diagnostics.Debug.WriteLine("/////////////////////// TIME DIFF movement " + 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.checkMovementValue(context, mov, pohS) && minutes <= 1) { System.Diagnostics.Debug.WriteLine("**********POHYB Spracovavana hodnota patri do aktualne otvorenej sekvencie" + mov.Xhodnota + " " + mov.Yhodnota + " " + mov.TimeStamp); //mov.Pohyb_Sekvencia = pohS;//naviaz pohyb mov.PohSekvFK = pohS.PohSekvId; //cas zotrvania DateTime convertedDate = DateTime.Parse(pohS.TimeStamp); String durationTime = null; DateTime endtime = DateTime.Parse(pohS.TimeStamp); try { endtime = DateTime.Parse(mov.TimeStamp); durationTime = (endtime - convertedDate).TotalMinutes.ToString(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception parse date " + e.ToString()); durationTime = "NA"; } pohS.Cas_Zotrvania = durationTime; pohS.Upozornenie_Cas = limitCheck.checkTimeLimitMovement(context, pohS); if (pohS.Upozornenie_Hranica == 1) { new NotificationGenerator().GenerateNotificationMovement(pohS.TimeStamp, pohS.PohSekvId); } if (pohS.Upozornenie_Cas == 1) { RoomsDetection roomsDetection = new RoomsDetection(); string roomName = roomsDetection.findRoom(mov).Nazov; //call sleep detection SleepDetection sleepDetection = new SleepDetection(); Boolean sleep = sleepDetection.DetectSleepSeq(context, roomName, convertedDate, endtime, 0); if (sleep = false) { new NotificationGenerator().GenerateNotificationMovementTime(roomName, pohS.TimeStamp, pohS.Cas_Zotrvania, pohS.PohSekvId); } } pohS.TimeStop = mov.TimeStamp; context.MovementSekv.Update(pohS); //context.Movement.Remove(mov); context.Movement.Update(mov); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } } else /*Spracovavana hodnota nepatri do aktualne otvorenej sekvencie*/ { Pohyb_Sekvencia last = context.MovementSekv.FirstOrDefault(t => t.PohSekvId == context.MovementSekv.Max(x => x.PohSekvId)); Pohyb_Sekvencia pohnew = null; if (izba == null) { pohnew = new Pohyb_Sekvencia { PohSekvId = last.PohSekvId + 1, Xhodnota = mov.Xhodnota, Yhodnota = mov.Yhodnota, TimeStamp = mov.TimeStamp, Cas_Zotrvania = "", TimeStop = mov.TimeStamp, Upozornenie_Cas = 0, Upozornenie_Hranica = limitCheck.checkIfOutside(context, mov), //Hranice_Pohyb = hpoh, HranicePohybFK = hpoh.HranicePohybId, }; } else { pohnew = new Pohyb_Sekvencia { PohSekvId = last.PohSekvId + 1, Xhodnota = mov.Xhodnota, Yhodnota = mov.Yhodnota, TimeStamp = mov.TimeStamp, Cas_Zotrvania = "", TimeStop = mov.TimeStamp, Upozornenie_Cas = 0, Upozornenie_Hranica = limitCheck.checkIfOutside(context, mov), //Hranice_Pohyb = hpoh, HranicePohybFK = hpoh.HranicePohybId, //Izby = izba, IzbyFK = izba.IzbaID }; } if (pohS.Upozornenie_Hranica == 1) { new NotificationGenerator().GenerateNotificationMovement(pohS.TimeStamp, pohS.PohSekvId); } if (minutes <= 1) { last.TimeStop = mov.TimeStamp; } //Pohyb p = context.Movement.Where(c => c.PohybId == mov.PohybId).First(); //p.PohSekvFK = last.PohSekvId + 1; //var toDelete = context.Movement.Where(c => c.PohSekvFK == last.PohSekvId).ToList(); //p.Pohyb_Sekvencia = pohnew; System.Diagnostics.Debug.WriteLine("POHYB Spracovavana hodnota nepatri do aktualne otvorenej sekvencie" + mov.Xhodnota + " " + mov.Yhodnota + " " + mov.TimeStamp); context.MovementSekv.Update(last); context.MovementSekv.Add(pohnew); //context.Movement.Remove(mov); //context.RemoveRange(toDelete); //context.Movement.Update(p); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } } } var toDelete = context.Movement.Where(c => c.PohybId == mov.PohybId).First(); context.Movement.Remove(toDelete); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception on value removing: " + nameof(SequenceCreator) + " " + e.ToString()); } } }
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 }