public void TemperatureSequencer(DatabaseContext context) { var all1 = context.Temperature.Where(t => t.TeplSekvFk == null).ToList(); //var all1 = context.Temperature.ToList(); LimitCheck limitCheck = new LimitCheck(); Hranice_Teplota htep = context.TemperatureLimit.FirstOrDefault(t => t.Hranice_TeplotaId == context.TemperatureLimit.Max(x => x.Hranice_TeplotaId)); foreach (var a in all1) {/*Inicializacia ak ziadna sekvencia neexistuje*/ // System.Diagnostics.Debug.WriteLine("SPRACOVAVAM TEPLOTNU HODNOTU DO SEKVENCIE: " + a.TeplotaId + " " + a.Hodnota + " " + a.TimeStamp); if (!context.TemperatureSekv.Any()) { Teplota_Sekvencia tmps = new Teplota_Sekvencia { TeplSekvId = 1, Sekvencia = a.Hodnota, TimeStart = a.TimeStamp, TimeClose = "", Upozornenie = limitCheck.CheckTemperatureLimits(context, a.Hodnota), //Hranice_Teplota = htep, Hranice_TeplotaFk = htep.Hranice_TeplotaId }; if (tmps.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Teplota", a.Hodnota, a.TimeStamp, tmps.Upozornenie, a.TeplotaId); } Teplota t = context.Temperature.Where(c => c.TeplotaId == a.TeplotaId).First(); t.TeplSekvFk = 1; //System.Diagnostics.Debug.WriteLine("Novasekvencia " + t.TeplotaId + " " + a.TeplotaId + " " + t.Hodnota + " + " + t.TeplSekvFk); context.TemperatureSekv.Add(tmps); context.Temperature.Update(t); try { System.Diagnostics.Debug.WriteLine("SPRACOVAVAM Teplota HODNOTU DO SEKVENCIE: INICIALIZACNA"); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } } else /*Ak existuju sekvencie*/ { /*Najdi otvorenu sekvenciu*/ Teplota_Sekvencia tmps = null; try { tmps = context.TemperatureSekv.FirstOrDefault(t => t.TimeClose == ""); //System.Diagnostics.Debug.WriteLine("otvorena sekvencia " + tmps.TeplSekvId + " " + tmps.Sekvencia); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } double minutes = 0; int seconds = 0; try { //DateTime startTime = DateTime.Parse(tmps.TimeStart); Teplota posl = context.Temperature.Where(p => p.TeplSekvFk == tmps.TeplSekvId).Last(); DateTime startTime = DateTime.Parse(posl.TimeStamp); DateTime endTime = DateTime.Parse(a.TimeStamp); TimeSpan finalTime = endTime - startTime; minutes = finalTime.TotalMinutes; seconds = finalTime.Seconds; //System.Diagnostics.Debug.WriteLine("/////////////////////// TIME DIFF Temperature" + 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.CheckTemperatureLimits(context, a.Hodnota) == tmps.Upozornenie && minutes <= 1) { var allInSekv = context.Temperature.Where(p => p.TeplSekvFk == tmps.TeplSekvId).ToList(); float median = 0; if (tmps.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Teplota", a.Hodnota, a.TimeStamp, tmps.Upozornenie, a.TeplotaId); } /*Aktualizuj hodnotu sekvencie*/ foreach (var allIN in allInSekv) { median += allIN.Hodnota; } median += a.Hodnota; median = median / (allInSekv.Count + 1); a.TeplSekvFk = tmps.TeplSekvId; //naviaz teplotu context.Temperature.Update(a); tmps.Sekvencia = median; context.TemperatureSekv.Update(tmps); try { //System.Diagnostics.Debug.WriteLine("SPRACOVAVAM teplota HODNOTU DO SEKVENCIE: UKLADAM DO AKTUALNEJ " + a.TeplotaId + " " + a.Hodnota + " " + a.TimeStamp + " " + tmps.TeplSekvId + " " + tmps.Sekvencia); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } } else /*Spracovavana hodnota nepatri do aktualne otvorenej sekvencie*/ { /* Zatvor poslednu sekvenciu*/ //najdi poslednu Teplota_Sekvencia ts = context.TemperatureSekv.FirstOrDefault(t => t.TeplSekvId == context.TemperatureSekv.Max(x => x.TeplSekvId)); var allInSekv1 = context.Temperature.Where(p => p.TeplSekvFk == ts.TeplSekvId).ToList(); // najdi list pre poslednu var last = allInSekv1[allInSekv1.Count - 1]; //zober posledny //ts.TimeClose = a.TimeStamp; // nastav konecny TS podla TS posledneho v sekvencii / edit-podla novej hodnoty aby som pokril celu casovu os 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; } int index = ts.TeplSekvId + 1; /* Boolean isLong = true; * try * { * DateTime startTime = DateTime.Parse(ts.TimeStart); * DateTime endTime = DateTime.Parse(ts.TimeClose); * TimeSpan finalTime = endTime - startTime; * if (finalTime.TotalMinutes <= LENGTH_BORDER) * { * isLong = false; * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++IS LONG TS FALSE: " + finalTime.Minutes + " " + finalTime + " " + finalTime.TotalMinutes); * * } * * } * catch (Exception e) * { * System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); * * } * if(isLong == false) * { * int tsPrevID = ts.TeplSekvId - 1; * if (tsPrevID == 0) tsPrevID = 1; * Teplota_Sekvencia prevTemp = context.TemperatureSekv.FirstOrDefault(p => p.TeplSekvId == tsPrevID); * * Boolean isLongPrev = true; * try * { * DateTime startTime = DateTime.Parse(prevTemp.TimeStart); * DateTime endTime = DateTime.Parse(prevTemp.TimeClose); * TimeSpan finalTime = endTime - startTime; * if (finalTime.TotalMinutes <= LENGTH_BORDER) * { * isLongPrev = false; * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++IS LONG prevTemp FALSE: " + finalTime.Minutes + " " + finalTime + " " + finalTime.TotalMinutes); * * } * * } * catch (Exception e) * { * System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); * * } * * if(isLongPrev == false) * { * float hodnota = (ts.Sekvencia + prevTemp.Sekvencia) / 2; * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++BOTH ARE SHORT - VALUE: " + prevTemp.Sekvencia + " " + ts.Sekvencia + " " + hodnota); * * Teplota_Sekvencia newTemp = new Teplota_Sekvencia * { * TeplSekvId = prevTemp.TeplSekvId, * Sekvencia = hodnota, * TimeStart = prevTemp.TimeStart, * TimeClose = ts.TimeClose, * Hranice_TeplotaFk = ts.Hranice_TeplotaFk, * Upozornenie = findMax(ts.Upozornenie, prevTemp.Upozornenie) * * }; * * index = ts.TeplSekvId; * prevTemp.Sekvencia = hodnota; * prevTemp.TimeClose = ts.TimeClose; * prevTemp.Hranice_TeplotaFk = ts.Hranice_TeplotaFk; * prevTemp.Upozornenie = findMax(ts.Upozornenie, prevTemp.Upozornenie); * * context.TemperatureSekv.Remove(ts); * context.TemperatureSekv.Update(prevTemp); * context.Temperature.RemoveRange(allInSekv1); * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++CONCAT SEQ: " + prevTemp.TeplSekvId + " " + ts.TeplSekvId + " " + a.Hodnota + " " + a.TeplotaId); * * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++BOTH ARE SHORT - NEW TIME: " + prevTemp.TimeStart + " " + prevTemp.TimeClose); * * } * else * { * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++SECOND IS LONG "); * * context.TemperatureSekv.Update(ts); * context.Temperature.RemoveRange(allInSekv1); * * } * * * * } * else * { * System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++FIRST IS LONG "); * * context.TemperatureSekv.Update(ts); * context.Temperature.RemoveRange(allInSekv1); * } */ context.TemperatureSekv.Update(ts); context.Temperature.RemoveRange(allInSekv1); /*Vytvor novu sekvenciu*/ Teplota_Sekvencia tmps1 = new Teplota_Sekvencia { TeplSekvId = index, Sekvencia = a.Hodnota, TimeStart = a.TimeStamp, TimeClose = "", Upozornenie = limitCheck.CheckTemperatureLimits(context, a.Hodnota), //Hranice_Teplota = htep, Hranice_TeplotaFk = htep.Hranice_TeplotaId }; context.TemperatureSekv.Add(tmps1); a.TeplSekvFk = index; //naviaz pulz na sekvenciu context.Temperature.Update(a); try { // System.Diagnostics.Debug.WriteLine("SPRACOVAVAM PULZ HODNOTU DO SEKVENCIE: VYTVATAM NOVU SEKVENCIU" + a.TeplotaId + " " + a.Hodnota + " " + a.TimeStamp + " " + tmps1.TeplSekvId + " " + tmps1.Sekvencia); context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } if (tmps1.Upozornenie != 0) { new NotificationGenerator().GenerateNotification("Teplota", a.Hodnota, a.TimeStamp, tmps1.Upozornenie, a.TeplotaId); } } } } }
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 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 void concatTemperatureSequences(int sekvId) { DatabaseContext context = new DatabaseContext(); Teplota_Sekvencia prevTemp = new Teplota_Sekvencia(); int prevSekvid = sekvId - 1; try { prevTemp = context.TemperatureSekv.Where(p => p.TeplSekvId == prevSekvid).First(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } bool isLong = true; try { DateTime startTime = DateTime.Parse(prevTemp.TimeStart); DateTime endTime = DateTime.Parse(prevTemp.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; } Teplota_Sekvencia actTemp = new Teplota_Sekvencia(); try { actTemp = context.TemperatureSekv.Where(p => p.TeplSekvId == sekvId).First(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } float hodnota = (prevTemp.Sekvencia + actTemp.Sekvencia) / 2; Teplota_Sekvencia newTemp = new Teplota_Sekvencia { TeplSekvId = prevSekvid, Sekvencia = hodnota, TimeStart = prevTemp.TimeStart, TimeClose = actTemp.TimeClose, Hranice_TeplotaFk = actTemp.Hranice_TeplotaFk, Upozornenie = findMax(actTemp.Upozornenie, prevTemp.Upozornenie) }; context.TemperatureSekv.Remove(prevTemp); context.TemperatureSekv.Remove(actTemp); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SekvenceConcat) + " " + e.ToString()); } context.TemperatureSekv.Add(newTemp); try { context.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception: " + nameof(SequenceCreator) + " " + e.ToString()); } }
public Boolean DetectCooking(DatabaseContext context, String roomName, DateTime start, DateTime close, int sekvCount, List <Tep_Sekvencia> tep, List <Teplota_Sekvencia> teplota) { if (!roomName.Equals("Kuchyňa") && !roomName.Equals("Kuchyna")) { return(false); } if (sekvCount < 2) { return(false); } //List<Tep_Sekvencia> tep = new List<Tep_Sekvencia>(); 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(); if (tep.Count == 0) { return(false); } foreach (var a in tep) { if (a.Upozornenie > 1) { return(false); } } //int id = tep.First().TepSekvId + 1; // Tep_Sekvencia before = context.PulseSekv.Where(a => a.TepSekvId == id).First(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("cook detection" + 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(); if (teplota.Count == 0) { return(false); } double sumTemp = 0; foreach (var a in teplota) { sumTemp += a.Sekvencia; } int id = 0; if (teplota.Last().TimeClose.Equals("")) { if (teplota[teplota.Count - 1].TeplSekvId - teplota[teplota.Count - 2].TeplSekvId == 1) { id = teplota.Last().TeplSekvId - 1; } else { id = teplota[teplota.Count - 2].TeplSekvId - 1; } } else { 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("cook detection" + e.ToString()); } return(true); }