Exemple #1
0
        public int CheckTemperatureLimits(DatabaseContext context, float hodnota)
        {
            Hranice_Teplota hranice = context.TemperatureLimit.FirstOrDefault(t => t.Hranice_TeplotaId == context.TemperatureLimit.Max(x => x.Hranice_TeplotaId));

            if ((hodnota >= hranice.Hranica_Slabe_Min) && (hodnota <= hranice.Hranica_Slabe_Max))
            {
                return(0);
            }
            else if (hodnota < hranice.Hranica_Slabe_Min)
            {
                return(-1);
            }
            else if ((hodnota > hranice.Hranica_Slabe_Max) && (hodnota <= hranice.Hranica_Stredne))
            {
                return(1);
            }
            else if ((hodnota > hranice.Hranica_Stredne) && (hodnota <= hranice.Hranica_Vysoke))
            {
                return(2);
            }
            else if (hodnota > hranice.Hranica_Vysoke)
            {
                return(3);
            }
            else
            {
                return(404);
            }
        }
        private void save()
        {
            var lastBordersPulse = context.PulseLimit.FirstOrDefault(e => e.Hranica_TepId == context.PulseLimit.Max(o => o.Hranica_TepId));

            Hranice_Tep ht = new Hranice_Tep
            {
                Hranica_TepId     = lastBordersPulse.Hranica_TepId + 1,
                TimeStamp         = DateTime.Now.ToString(),
                Hranica_Slabe_Min = LowDown,
                Hranica_Slabe_Max = LowUp,
                Hranica_Stredne   = Middle,
                Hranica_Vysoke    = High
            };

            context.PulseLimit.Add(ht);

            var lastBordersTemp = context.TemperatureLimit.FirstOrDefault(a => a.Hranice_TeplotaId == context.TemperatureLimit.Max(p => p.Hranice_TeplotaId));

            Hranice_Teplota htemp = new Hranice_Teplota
            {
                Hranice_TeplotaId = lastBordersTemp.Hranice_TeplotaId + 1,
                TimeStamp         = DateTime.Now.ToString(),
                Hranica_Slabe_Min = LowDownTemp,
                Hranica_Slabe_Max = LowUpTemp,
                Hranica_Stredne   = MiddleTemp,
                Hranica_Vysoke    = HighTemp
            };

            context.TemperatureLimit.Add(htemp);

            Hranice_Pohyb limit = context.MovementLimit.FirstOrDefault(t => t.HranicePohybId == context.MovementLimit.Max(x => x.HranicePohybId));

            int index = limit.HranicePohybId;

            index++;

            Hranice_Pohyb hp = new Hranice_Pohyb()
            {
                HranicePohybId = index,
                LimitCas       = TimeLimit.ToString(),
                OkruhHranica   = Okruh,
                Xhranica       = SettingsController.MaxX,
                Yhranica       = SettingsController.MaxY,
                TimeStamp      = DateTime.Now.ToString()
            };

            context.MovementLimit.Add(hp);



            try
            {
                context.SaveChanges();
            }catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception: " + nameof(EditBordersViewModel) + " " + e.ToString());
            }

            Application.Current.MainPage.Navigation.PopAsync();
        }
Exemple #3
0
        public void LoadTemperatureLimits(DatabaseContext context)
        {
            HelpMethods helpm = new HelpMethods();

            Hranice_Teplota hrantep = new Hranice_Teplota
            {
                Hranica_Slabe_Min = 35.5f,
                Hranica_Slabe_Max = 37.0f,
                Hranica_Stredne   = 38.2f,
                Hranica_Vysoke    = 39.0f,
                TimeStamp         = DateTime.Now.ToShortTimeString()
            };

            context.TemperatureLimit.Add(hrantep);

            try
            {
                context.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        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);
                        }
                    }
                }
            }
        }
Exemple #5
0
        public void createLimits()
        {
            List <IGrouping <int, int> > occurrDicta = createOccurTable().ToList();

            int slabe_min = 0, slabe_max, stredne, vysoke;
            int maxOccKey = 0, maxOccValue = 0;

            /*Najdi hodnotu s najcastejsim vyskytom*/
            foreach (var grp in occurrDicta)
            {
                if (grp.Count() > maxOccValue)
                {
                    maxOccValue = grp.Count();
                    maxOccKey   = grp.Key;
                }
            }

            /*Skontroluj ci na zaciatku nie je outlier
             * ak ano = zmaz ho
             * Po odstraneni uloz najnizsiu ako hodnotu pre slabe_min
             */
            string pom;

            for (int i = 0; i < occurrDicta.Count; i++)
            {
                if ((occurrDicta.ElementAt(i).Count() == 1) && ((occurrDicta.ElementAt(i).Key * 1.1) < occurrDicta.ElementAt(i + 1).Key))
                {
                    // pom = "true";
                    // Console.WriteLine("seruuuusssssssssssss {0} {1} {2}", occurrDicta.ElementAt(i).Key, occurrDicta.ElementAt(i).Count(), pom);

                    occurrDicta.RemoveAt(i);
                    i--;
                }
                else
                {
                    //pom = "false";
                    //Console.WriteLine("seruuuusssssssssssss {0} {1} {2}", occurrDicta.ElementAt(i).Key, occurrDicta.ElementAt(i).Count(), pom);
                    slabe_min = Convert.ToInt32(occurrDicta.ElementAt(i).Key *0.9);
                    if (slabe_min < 50)
                    {
                        slabe_min = 50;
                    }
                    break;
                }
            }

            /*
             * K najcastejsej pripocitaj rozdiel so slabe_min
             */
            int low = maxOccKey + (maxOccKey - slabe_min);

            slabe_max = Convert.ToInt32(maxOccKey + ((maxOccKey - slabe_min) * 0.5));

            foreach (var grp in occurrDicta)
            {
                if ((grp.Key > low) && (grp.Count() > 2))
                {
                    int tmpPom = grp.Key - slabe_max;

                    low       = Convert.ToInt32(grp.Key + tmpPom * 0.3);
                    slabe_max = low;
                }
            }

            /*
             * Vytvor ostatne hranice
             */
            stredne = Convert.ToInt32(slabe_max * 1.1);
            vysoke  = Convert.ToInt32(slabe_max * 1.2);
            Console.WriteLine("hranice {0} {1} {2} {3} ", slabe_min, slabe_max, stredne, vysoke);


            /*Vytvor zaznam pre hranice tepu v databaze*/
            DatabaseContext context = context = new DatabaseContext();

            int index = 1;

            if (!context.PulseLimit.Any())
            {
                index = 1;
            }
            else
            {
                Hranice_Tep tmp = context.PulseLimit.FirstOrDefault(t => t.Hranica_TepId == context.PulseLimit.Max(x => x.Hranica_TepId));
                index = tmp.Hranica_TepId;
            }

            Hranice_Tep ht = new Hranice_Tep
            {
                Hranica_TepId     = index,
                Hranica_Slabe_Min = slabe_min,
                Hranica_Slabe_Max = slabe_max,
                Hranica_Stredne   = stredne,
                Hranica_Vysoke    = vysoke,
                TimeStamp         = DateTime.Now.ToString("h:mm:ss tt")
            };

            context.PulseLimit.Add(ht);

            /*Vytvor zaznam pre hranice teploty v databaze*/
            index = 1;
            if (!context.TemperatureLimit.Any())
            {
                index = 1;
            }
            else
            {
                Hranice_Teplota tmp = context.TemperatureLimit.FirstOrDefault(t => t.Hranice_TeplotaId == context.TemperatureLimit.Max(x => x.Hranice_TeplotaId));
                index = tmp.Hranice_TeplotaId;
            }

            Hranice_Teplota htemp = new Hranice_Teplota
            {
                Hranice_TeplotaId = index,
                TimeStamp         = DateTime.Now.ToString("h:mm:ss tt"),
                Hranica_Slabe_Min = 35.5F,
                Hranica_Slabe_Max = 37.0F,
                Hranica_Stredne   = 38.2F,
                Hranica_Vysoke    = 39.0F
            };

            context.TemperatureLimit.Add(htemp);

            var allPulse = context.Pulse.ToList();

            context.Pulse.RemoveRange(allPulse);

            try
            {
                context.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }

            /*Kontrolny vypis*/
            var all = context.TemperatureLimit.ToList();

            foreach (var a in all)
            {
                System.Diagnostics.Debug.WriteLine("teplota hranice " + a.Hranice_TeplotaId + " " + a.TimeStamp + " " + a.Hranica_Slabe_Min + " " + a.Hranica_Slabe_Max + " " + a.Hranica_Stredne + " " + a.Hranica_Vysoke);
            }

            var all1 = context.PulseLimit.ToList();

            foreach (var a in all1)
            {
                System.Diagnostics.Debug.WriteLine("tep hranice " + a.Hranica_TepId + " " + a.TimeStamp + " " + a.Hranica_Slabe_Min + " " + a.Hranica_Slabe_Max + " " + a.Hranica_Stredne + " " + a.Hranica_Vysoke);
            }
        }