public async void readLineFromDatasetFile()
        {
            try
            {
                DatabaseContext context    = new DatabaseContext();
                IFileSystem     fileSystem = FileSystem.Current;
                IFolder         rootFolder = fileSystem.LocalStorage;

                IFile tempFile = await rootFolder.GetFileAsync("temperatureDataset.txt");

                string newFileText; string line;
                string fileText = await tempFile.ReadAllTextAsync();

                using (System.IO.StringReader reader = new System.IO.StringReader(fileText))
                {
                    line = reader.ReadLine();
                    System.Diagnostics.Debug.WriteLine("READ from file temperature " + line);
                    newFileText = reader.ReadToEnd();
                }

                tempFile.WriteAllTextAsync(newFileText);

                DatasetJson obj = Newtonsoft.Json.JsonConvert.DeserializeObject <DatasetJson>(line);

                int index = 1;
                if (!context.Temperature.Any())
                {
                    index = 1;
                }
                else
                {
                    Teplota tmp = context.Temperature.FirstOrDefault(t => t.TeplotaId == context.Temperature.Max(x => x.TeplotaId));
                    index = tmp.TeplotaId;
                    index++;
                }


                Teplota teplota = new Teplota
                {
                    TeplotaId = index,
                    //TimeStamp = obj.header.creation_date_time.ToString(),
                    TimeStamp = obj.timestamp.ToString(),
                    Hodnota   = obj.value
                };

                context.Temperature.Add(teplota);
                //System.Diagnostics.Debug.WriteLine("****** VLOZENIE TEPLOTY DO DB: " + teplota.TeplotaId + " " + teplota.Hodnota + " " + teplota.TimeStamp);

                context.SaveChanges();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("citanie teplotneho suboru zo zariadenia: " + e.ToString());
            }
        }
예제 #2
0
        public void setTemperatureValue()
        {
            if (_context.Temperature.Any())
            {
                Teplota teplota = _context.Temperature.FirstOrDefault(t => t.TeplotaId == _context.Temperature.Max(x => x.TeplotaId));

                TempValue = teplota.Hodnota.ToString("n2") + " °C";
            }
            else
            {
                TempValue = "NA";
            }
        }
        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 LoadTemperatureDataset(DatabaseContext context)
        {
            var assembly = typeof(LoadTemperature).GetTypeInfo().Assembly;

            /*Definovanie cesty suboru*/
            try
            {
                //Stream stream = assembly.GetManifestResourceStream("Xamarin.Forms_EFCore.temperatureWeek.txt");
                Stream      stream  = assembly.GetManifestResourceStream("Xamarin.Forms_EFCore.temperatureDataset.txt");
                List <Json> objects = new List <Json>();
                /*nahranie dat a sparsovanie*/
                int i     = 0;
                int index = 1;
                if (!context.Temperature.Any())
                {
                    index = 1;
                }
                else
                {
                    Teplota tmp = context.Temperature.FirstOrDefault(t => t.TeplotaId == context.Temperature.Max(x => x.TeplotaId));
                    index = tmp.TeplotaId;
                }

                index++;
                using (StreamReader sr = new StreamReader(stream))
                {
                    while (sr.Peek() >= 0)
                    {
                        DatasetJson obj = Newtonsoft.Json.JsonConvert.DeserializeObject <DatasetJson>(sr.ReadLine());
                        //objects.Add(obj);
                        //System.Diagnostics.Debug.WriteLine(i++ + " pppppp+ " + obj.header.creation_date_time.ToString() + " + " + obj.body.body_temperature.value);


                        Teplota teplota = new Teplota
                        {
                            TeplotaId = index++,
                            TimeStamp = obj.timestamp.ToString(),
                            Hodnota   = obj.value
                        };
                        context.Temperature.Add(teplota);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


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


            /*Vypis tabulky z DB*/

            /*   var all = _context.Temperature.ToList();
             * foreach (var a in all)
             * {
             *     System.Diagnostics.Debug.WriteLine("" + a.TeplotaId + " " + a.TimeStamp + " " + a.Hodnota);
             *
             *
             * }*/
        }