Esempio n. 1
0
        private MeasureBackgroundTask()
        {
            lg.AppendLog(Log.CreateLog("Measure Service starting...", LogType.Information));

            userSettings = new UserSettings();

            userSettings = userSettingsManager.GetUserSettings();

            lg.AppendLog(Log.CreateLog("UserSettings retreived", LogType.Information));

            //Disable Diagnostic Mode on Restart
            if (userSettings.isDiagnosticModeEnable)
            {
                userSettings.isDiagnosticModeEnable = false;
                userSettingsManager.SyncUserSettings(userSettings);
            }

            bw.WorkerSupportsCancellation = true;
            bw.WorkerReportsProgress      = false;
            bw.DoWork             += Bw_DoWork;
            bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
        }
        public void Run()
        {
            var watch           = Stopwatch.StartNew();
            var captureDateTime = DateTime.Now;

            var clockManager = ClockManager.GetInstance;
            var userSettings = userSettingsManager.GetUserSettings();

            lg.AppendLog(Log.CreateLog("[UTEST00] Sigfox Integration Test start", LogType.Information));
            if (clockManager != null)
            {
                Task.Run(async() =>
                {
                    clockManager.InitClock();
                    await Task.Delay(3000);
                }).Wait();

                captureDateTime = clockManager.ReadDate();

                clockManager.Dispose();
            }

            sigfoxManager = SigfoxInterfaceManager.GetInstance;

            lg.AppendLog(Log.CreateLog("[UTEST01] Sigfox Init started", LogType.Information));

            if (userSettings.connectivityType == ConnectivityType.Sigfox)
            {
                var taskSigfox = Task.Run(async() =>
                {
                    sigfoxManager.InitInterface();
                    await Task.Delay(1000);
                });
                taskSigfox.Wait();
            }

            sensorManager = AtlasSensorManager.GetInstance;

            lg.AppendLog(Log.CreateLog("[UTEST03] Atlas Sensors init", LogType.Information));
            var taskSensor = Task.Run(async() =>
            {
                sensorManager.InitSensors(false);
                await Task.Delay(1000);
            });

            taskSensor.Wait();

            lg.AppendLog(Log.CreateLog("[UTEST04] Air Temp/Hum Sensor init", LogType.Information));
            var humTempManager = HumidityTemperatureManager.GetInstance;

            var taskHumManager = Task.Run(async() =>
            {
                humTempManager.Connect();
                await Task.Delay(1000);
            });

            taskHumManager.Wait();

            try
            {
                var elapsedMs = watch.ElapsedMilliseconds;

                TimeSpan t = TimeSpan.FromMilliseconds(elapsedMs);

                var watchMesures = Stopwatch.StartNew();

                if (sensorManager.isSensorOnline(SensorTypeEnum.waterTemperature))
                {
                    lg.AppendLog(Log.CreateLog("[UTEST10] Water Temperature capturing", LogType.Information));

                    decimal capturedValue = 0;
                    capturedValue = sensorManager.RecordSensorsMeasure(SensorTypeEnum.waterTemperature, false);

                    if (capturedValue > -20 && capturedValue < 80)
                    {
                        sensorManager.SetWaterTemperatureForPHSensor(capturedValue);

                        var task = Task.Run(async() =>
                        {
                            await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.waterTemperature);
                        });
                        task.Wait();

                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST11] Water Temperature captured : {0}", capturedValue), LogType.Information));
                        var status = sensorManager.GetSensorStatus(SensorTypeEnum.waterTemperature, false);
                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST12] Water Temperature status : {0}", status), LogType.System));
                    }
                    else
                    {
                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST13] Water Temperature value out of range - {0}", capturedValue), LogType.Warning));
                    }
                }

                if (sensorManager.isSensorOnline(SensorTypeEnum.pH))
                {
                    lg.AppendLog(Log.CreateLog("[UTEST13] PH capturing", LogType.Information));

                    decimal capturedValue = 0;
                    capturedValue = sensorManager.RecordpHMeasure(false);

                    if (capturedValue > 1 && capturedValue < 12)
                    {
                        var task = Task.Run(async() =>
                        {
                            await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.pH);
                        });
                        task.Wait();

                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST14] PH captured : {0}", capturedValue), LogType.Information));
                        var status = sensorManager.GetSensorStatus(SensorTypeEnum.pH, false);
                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST15] PH status : {0}", status), LogType.System));
                    }
                    else
                    {
                        lg.AppendLog(Log.CreateLog(String.Format("PH value out of range - {0}", capturedValue), LogType.Warning));
                    }
                }

                try
                {
                    lg.AppendLog(Log.CreateLog("[UTEST24] Air Temperature capturing", LogType.Information));

                    decimal capturedValue = (decimal)humTempManager.Temperature;

                    if (capturedValue > 0 && capturedValue < 100)
                    {
                        var taskTemp = Task.Run(async() =>
                        {
                            await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.airTemperature);
                        });
                        taskTemp.Wait();

                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST25] Air Temperature captured : {0}", capturedValue), LogType.Information));
                    }
                    else
                    {
                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST26] Air Temperature out of range - {0}", capturedValue), LogType.Warning));
                    }
                }
                catch (Exception ex)
                {
                    lg.AppendLog(Log.CreateErrorLog("Exception on Air Temperature Sensor", ex));
                }

                try
                {
                    lg.AppendLog(Log.CreateLog("[UTEST27] Humidity capturing", LogType.Information));

                    decimal capturedValue = (decimal)humTempManager.Humidity;

                    if (capturedValue > 0 && capturedValue < 100)
                    {
                        var taskHum = Task.Run(async() =>
                        {
                            await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.humidity);
                        });
                        taskHum.Wait();

                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST28] Air Humidity captured : {0}", capturedValue), LogType.Information));
                    }
                    else
                    {
                        lg.AppendLog(Log.CreateLog(String.Format("[UTEST29] Air Humidity out of range - {0}", capturedValue), LogType.Warning));
                    }
                }
                catch (Exception ex)
                {
                    lg.AppendLog(Log.CreateErrorLog("Exception on Air Humidity Sensor", ex));
                }

                lg.AppendLog(Log.CreateLog(String.Format("[UTEST30] Measures captured in {0} sec.", watchMesures.ElapsedMilliseconds / 1000), LogType.System));

                watchMesures.Restart();

                string messageSignature = String.Empty;

                var taskSig = Task.Run(async() =>
                {
                    messageSignature = await databaseModel.GetLastMesureSignature();
                });
                taskSig.Wait();

                if (userSettings.connectivityType == ConnectivityType.Wifi && NetworkInterface.GetIsNetworkAvailable())
                {
                    Task.Run(async() =>
                    {
                        await HttpClientHelper.SendMessage(userSettings.hubMessageAPI,
                                                           messageSignature,
                                                           userSettings.productionSiteId);
                    }).Wait();
                }
                else
                {
                    sigfoxManager.SendMessage(messageSignature, userSettings.sigfoxVersion);
                }

                lg.AppendLog(Log.CreateLog(String.Format("[UTEST31] Data sent to Azure via Sigfox in {0} sec.", watchMesures.ElapsedMilliseconds / 1000), LogType.System));
            }
            catch (Exception ex)
            {
                lg.AppendLog(Log.CreateErrorLog("Exception on Sigfox Integration Test", ex));
            }

            watch.Stop();
        }
Esempio n. 3
0
        public BackgroundManager()
        {
            var log = LogManager.GetInstance;

            log.AppendLog(Log.CreateLog("Background Worker Engaged", LogType.Information));

            var to = HumidityTemperatureManager.GetInstance;

            to.Connect();

            decimal capt = 0;

            for (int i = 0; i < 30; i++)
            {
                Task.Run(async() =>
                {
                    //await Task.Delay(1000);
                    capt = (decimal)to.Temperature;
                    Console.WriteLine(capt);
                    await Task.Delay(1000);
                    capt = (decimal)to.Humidity;
                    Console.WriteLine(capt);
                    //to.Dispose();
                }).Wait();
            }

            var clock = ClockManager.GetInstance;

            Task.Run(async() =>
            {
                clock.InitClock();
                await Task.Delay(3000);
                clock.SetDate(DateTime.Now);
            }).Wait();

            DateTime date = DateTime.Now;

            for (int i = 0; i < 20; i++)
            {
                Task.Run(async() =>
                {
                    clock.InitClock();
                    await Task.Delay(1000);
                    date = clock.ReadDate();
                    Console.WriteLine(date.ToLongDateString());
                    clock.Dispose();
                }).Wait();
            }
            ;

            string setDay = String.Format(@"date +%Y%m%d -s ""{0}""", date.ToShortDateString());

            string setHour = String.Format(@"date +%T -s ""{0}""", date.ToShortDateString());

            var tt1 = setDay.Bash();

            Console.WriteLine(tt1);

            var tt11 = setHour.Bash();

            Console.WriteLine(tt1);

            Console.WriteLine("date".Bash());

            var allValues = Enum.GetValues(typeof(SensorTypeEnum));

            var sg = SigfoxInterfaceManager.GetInstance;

            sg.InitInterface();
            sg.SendMessage("00730285AAAAAAAA02410914", SigfoxVersion.v2);

            var atls = AtlasSensorManager.GetInstance;

            atls.InitSensors(false);

            //var ph = atls.RecordPhMeasure(false);
            //Console.WriteLine(ph);
            //var water = atls.RecordSensorsMeasure(SensorTypeEnum.waterTemperature,false);
            //Console.WriteLine(water);

            try
            {
                var rslt3 = "sudo /opt/vc/bin/vcgencmd measure_temp".Bash();
                Console.WriteLine(rslt3.Substring(5, 2));
                rslt3 = rslt3.Substring(5, 2);

                StringBuilder rslt2 = new StringBuilder("EEEE");
                rslt2[2] = rslt3[0];
                rslt2[3] = rslt3[1];
                Console.WriteLine(rslt2.ToString());

                var tt2 = "sudo iwlist wlan0 scanning | grep ESSID".Bash();
                Console.WriteLine(tt2);
            } catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

            Console.WriteLine("Test Database Entities");
            var db = DatabaseModel.GetInstance;

            db.AddMesure(DateTime.Now, 7, SensorTypeEnum.pH).Wait();
            db.AddMesure(DateTime.Now, 8, SensorTypeEnum.pH).Wait();
            db.AddMesure(DateTime.Now, 9, SensorTypeEnum.pH).Wait();

            var rslt = new List <Measure>();

            Task.Run(() =>
            {
                rslt = db.GetLastDayMesures(SensorTypeEnum.pH).Result;
            }).Wait(1000);

            Console.WriteLine(rslt.Count);

            UserSettingsManager mod = UserSettingsManager.GetInstance;

            var tt = mod.GetUserSettings();

            Console.WriteLine(tt.hubMessageAPI);

            using (var client = new HttpClient())
            {
                var request = new Message()
                {
                    content = "XXXXXXXXXXXX",
                    device  = "DDDDDD",
                    date    = "01-01-2020",
                    data    = "Wifi"
                };

                var taskWeb = Task.Run(async() =>
                {
                    try
                    {
                        var response = await client.PostAsync("https://hub.myfood.eu/api/Messages/",
                                                              new StringContent(JsonSerializer.Serialize(request),
                                                                                Encoding.UTF8, "application/json"));

                        Console.WriteLine(response.ReasonPhrase);
                        Console.WriteLine(response.Content);

                        if (response.IsSuccessStatusCode)
                        {
                            //lg.AppendLog(Log.CreateLog("Measures sent to Azure via Internet", LogType.Information));
                        }
                    }
                    catch (Exception ex)
                    {
                        //lg.AppendLog(Log.CreateErrorLog("Exception on Measures to Azure", ex));
                    }
                });

                taskWeb.Wait();
            }
        }