Esempio n. 1
0
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var watch = Stopwatch.StartNew();

            var messageSignature = new StringBuilder("AAAAAAAAAAAAAAAAAAAAAAAA", 24);

            if (userSettings.measureFrequency >= 60000)
            {
                TICKSPERCYCLE = userSettings.measureFrequency;
            }

            var clockManager = ClockManager.GetInstance;

            var captureDateTime = DateTime.Now;

            try
            {
                sigfoxManager = SigfoxInterfaceManager.GetInstance;

                if (userSettings.connectivityType == ConnectivityType.Sigfox)
                {
                    sigfoxManager.InitInterface();
                    sigfoxManager.SendMessage(messageSignature.ToString(), userSettings.sigfoxVersion);
                }

                if (userSettings.connectivityType == ConnectivityType.Wifi && NetworkInterface.GetIsNetworkAvailable())
                {
                    Task.Run(async() =>
                    {
                        await HttpClientHelper.SendMessage(userSettings.hubMessageAPI,
                                                           messageSignature.ToString(),
                                                           userSettings.productionSiteId);
                    }).Wait();
                }
            }
            catch (Exception ex)
            {
                lg.AppendLog(Log.CreateErrorLog("Exception on Connectivity Init", ex));
            }

            try
            {
                if (clockManager != null)
                {
                    Task.Run(async() =>
                    {
                        clockManager.InitClock();
                        await Task.Delay(2000);
                        captureDateTime = clockManager.ReadDate();
                        clockManager.Dispose();
                    }).Wait();
                }

                userSettings = userSettingsManager.GetUserSettings();

                if (userSettings.isDiagnosticModeEnable)
                {
                    TICKSPERCYCLE = TICKSPERCYCLE_DIAGNOSTIC_MODE;
                }

                sensorManager = AtlasSensorManager.GetInstance;

                sensorManager.InitSensors(userSettings.isSleepModeEnable);

                sensorManager.SetDebugLedMode(userSettings.isDebugLedEnable);

                humTempManager = HumidityTemperatureManager.GetInstance;

                internalTemperatureManager = InternalTemperatureManager.GetInstance;

                if (userSettings.isTempHumiditySensorEnable)
                {
                    humTempManager.Connect();
                }
            }
            catch (Exception ex)
            {
                lg.AppendLog(Log.CreateErrorLog("Exception on Init Hardware", ex));
                sigfoxManager.SendMessage("FFFFFFFFFFFFFFFFFFFFFFFF", userSettings.sigfoxVersion);
            }

            while (!bw.CancellationPending)
            {
                var elapsedMs = watch.ElapsedMilliseconds;

                try
                {
                    if (elapsedMs % TICKSPERCYCLE == 0)
                    {
                        captureDateTime = captureDateTime.AddMilliseconds(TICKSPERCYCLE);

                        TimeSpan t = TimeSpan.FromMilliseconds(elapsedMs);

                        string logDescription = string.Format("[ {0:d} - {0:t} ] Service running since {1:D2}d:{2:D2}h:{3:D2}m:{4:D2}s",
                                                              captureDateTime,
                                                              t.Days,
                                                              t.Hours,
                                                              t.Minutes,
                                                              t.Seconds,
                                                              t.Milliseconds);

                        lg.AppendLog(Log.CreateLog(logDescription, LogType.Information));

                        var watchMesures = Stopwatch.StartNew();

                        if (sensorManager.isSensorOnline(SensorTypeEnum.waterTemperature))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                lg.AppendLog(Log.CreateLog("Water Temperature capturing", LogType.Information));
                            }

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

                            if (capturedValue > 0 && capturedValue < 80)
                            {
                                messageSignature[4] = '0';
                                messageSignature[5] = capturedValue.ToString()[0];
                                messageSignature[6] = capturedValue.ToString()[1];
                                messageSignature[7] = capturedValue.ToString()[3];

                                if (!userSettings.isDiagnosticModeEnable)
                                {
                                    sensorManager.SetWaterTemperatureForPHSensor(capturedValue);
                                }

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    lg.AppendLog(Log.CreateLog(String.Format("Water Temperature captured : {0}", capturedValue), LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.waterTemperature, userSettings.isSleepModeEnable);
                                    lg.AppendLog(Log.CreateLog(String.Format("Water Temperature status : {0}", status), LogType.System));
                                }
                            }
                            else
                            {
                                lg.AppendLog(Log.CreateLog(String.Format("Water Temperature value out of range - {0}", capturedValue), LogType.Warning));
                                messageSignature[4] = 'B';
                                messageSignature[5] = 'B';
                                messageSignature[6] = 'B';
                                messageSignature[7] = 'B';
                            }
                        }

                        if (sensorManager.isSensorOnline(SensorTypeEnum.pH))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                lg.AppendLog(Log.CreateLog("PH capturing", LogType.Information));
                            }

                            decimal capturedValue = 0;
                            capturedValue = sensorManager.RecordpHMeasure(userSettings.isSleepModeEnable);

                            if (capturedValue > 1 && capturedValue < 12)
                            {
                                messageSignature[0] = '0';
                                messageSignature[1] = '0';
                                messageSignature[2] = capturedValue.ToString()[0];
                                messageSignature[3] = capturedValue.ToString()[2];

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    lg.AppendLog(Log.CreateLog(String.Format("PH captured : {0}", capturedValue), LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.pH, userSettings.isSleepModeEnable);
                                    lg.AppendLog(Log.CreateLog(String.Format("PH status : {0}", status), LogType.System));
                                }
                            }
                            else
                            {
                                lg.AppendLog(Log.CreateLog(String.Format("PH value out of range - {0}", capturedValue), LogType.Warning));
                                messageSignature[0] = 'B';
                                messageSignature[1] = 'B';
                                messageSignature[2] = 'B';
                                messageSignature[3] = 'B';
                            }
                        }

                        if (userSettings.isTempHumiditySensorEnable)
                        {
                            try
                            {
                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    lg.AppendLog(Log.CreateLog("Air Temperature Humidity capturing", LogType.Information));
                                }

                                decimal capturedValue = 0;

                                Task.Run(async() =>
                                {
                                    using (await asyncLock.LockAsync())
                                    {
                                        await Task.Delay(1000);
                                        capturedValue = Math.Round((decimal)humTempManager.Temperature, 1);
                                        Console.WriteLine("Temp" + capturedValue);
                                        await Task.Delay(1000);
                                        await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.airTemperature);

                                        messageSignature[16] = '0';
                                        messageSignature[17] = capturedValue.ToString()[0];
                                        messageSignature[18] = capturedValue.ToString()[1];
                                        messageSignature[19] = capturedValue.ToString()[3];

                                        if (userSettings.isDiagnosticModeEnable)
                                        {
                                            lg.AppendLog(Log.CreateLog(String.Format("Air Temperature captured : {0}", capturedValue), LogType.Information));
                                        }

                                        await Task.Delay(1000);
                                        capturedValue = Math.Round((decimal)humTempManager.Humidity, 1);
                                        Console.WriteLine("Hum" + capturedValue);
                                        await Task.Delay(1000);
                                        await databaseModel.AddMesure(captureDateTime, capturedValue, SensorTypeEnum.humidity);

                                        messageSignature[20] = '0';
                                        messageSignature[21] = capturedValue.ToString()[0];
                                        messageSignature[22] = capturedValue.ToString()[1];
                                        messageSignature[23] = capturedValue.ToString()[3];

                                        if (userSettings.isDiagnosticModeEnable)
                                        {
                                            lg.AppendLog(Log.CreateLog(String.Format("Air Humidity captured : {0}", capturedValue), LogType.Information));
                                        }
                                    }
                                }).Wait();
                            }
                            catch (Exception ex)
                            {
                                lg.AppendLog(Log.CreateErrorLog("Exception on Air Temperature Humidity Sensor", ex));
                                messageSignature[16] = 'C';
                                messageSignature[17] = 'C';
                                messageSignature[18] = 'C';
                                messageSignature[19] = 'C';
                                messageSignature[20] = 'C';
                                messageSignature[21] = 'C';
                                messageSignature[22] = 'C';
                                messageSignature[23] = 'C';
                            }
                        }

                        try
                        {
                            var temp = internalTemperatureManager.GetInternalTemperatureSignature();
                            messageSignature[12] = temp[0];
                            messageSignature[13] = temp[1];
                            messageSignature[14] = temp[2];
                            messageSignature[15] = temp[3];
                        }
                        catch (Exception ex)
                        {
                            lg.AppendLog(Log.CreateErrorLog("Exception on Internal Temperature Sensor", ex));
                            messageSignature[12] = 'C';
                            messageSignature[13] = 'C';
                            messageSignature[14] = 'C';
                            messageSignature[15] = 'C';
                        }

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

                        if (userSettings.connectivityType == ConnectivityType.Sigfox && sigfoxManager.isInitialized && TICKSPERCYCLE >= 600000)
                        {
                            watchMesures.Restart();

                            Task.Run(async() =>
                            {
                                sigfoxManager.SendMessage(messageSignature.ToString(), userSettings.sigfoxVersion);
                                await Task.Delay(2000);
                            }).Wait();

                            lg.AppendLog(Log.CreateLog(String.Format("Data sent to Azure via Sigfox in {0} sec.", watchMesures.ElapsedMilliseconds / 1000), LogType.System));
                        }

                        if (userSettings.connectivityType == ConnectivityType.Wifi && NetworkInterface.GetIsNetworkAvailable())
                        {
                            Task.Run(async() =>
                            {
                                await HttpClientHelper.SendMessage(userSettings.hubMessageAPI,
                                                                   messageSignature.ToString(),
                                                                   userSettings.productionSiteId);
                            }).Wait();
                        }
                    }
                }
                catch (Exception ex)
                {
                    lg.AppendLog(Log.CreateErrorLog("Exception on Measures", ex));
                    sigfoxManager.SendMessage("CCCCCCCCCCCCCCCCCCCCCCCC", userSettings.sigfoxVersion);
                }
            }
            watch.Stop();
        }
        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
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var watch = Stopwatch.StartNew();

            if (userSettings.measureFrequency >= 60000)
            {
                TICKSPERCYCLE = userSettings.measureFrequency;
            }

            var clockManager = ClockManager.GetInstance;

            var captureDateTime = DateTime.Now;

            if (clockManager != null)
            {
                var taskClock = Task.Run(async() =>
                {
                    await clockManager.Connect();
                });
                taskClock.Wait();

                captureDateTime = clockManager.ReadDate();

                clockManager.Dispose();
            }

            var taskUser = Task.Run(async() => { userSettings = await userSettingsModel.GetUserSettingsAsync(); });

            taskUser.Wait();

            sigfoxManager = SigfoxInterfaceManager.GetInstance;

            if (userSettings.isSigFoxComEnable)
            {
                var taskSigfox = Task.Run(async() => { await sigfoxManager.InitSensors(); });
                taskSigfox.Wait();
            }

            if (userSettings.isDiagnosticModeEnable)
            {
                TICKSPERCYCLE = TICKSPERCYCLE_DIAGNOSTIC_MODE;

                if (userSettings.isSigFoxComEnable && sigfoxManager.isInitialized)
                {
                    sigfoxManager.SendMessage("AAAAAAAAAAAAAAAAAAAAAAAA");
                }
            }

            sensorManager = AtlasSensorManager.GetInstance;

            var taskSensor = Task.Run(async() => { await sensorManager.InitSensors(userSettings.isSleepModeEnable); });

            taskSensor.Wait();

            sensorManager.SetDebugLedMode(userSettings.isDebugLedEnable);

            var humTempManager = HumidityTemperatureManager.GetInstance;

            if (userSettings.isTempHumiditySensorEnable)
            {
                var taskHumManager = Task.Run(async() =>
                {
                    await humTempManager.Connect();
                });
                taskHumManager.Wait();
            }

            while (!bw.CancellationPending)
            {
                var elapsedMs = watch.ElapsedMilliseconds;

                try
                {
                    if (elapsedMs % TICKSPERCYCLE == 0)
                    {
                        captureDateTime = captureDateTime.AddMilliseconds(TICKSPERCYCLE);

                        TimeSpan t = TimeSpan.FromMilliseconds(elapsedMs);

                        string logDescription = string.Format("[ {0:d} - {0:t} ] Service running since {1:D2}d:{2:D2}h:{3:D2}m:{4:D2}s",
                                                              captureDateTime,
                                                              t.Days,
                                                              t.Hours,
                                                              t.Minutes,
                                                              t.Seconds,
                                                              t.Milliseconds);

                        logModel.AppendLog(Log.CreateLog(logDescription, Log.LogType.Information));

                        var watchMesures = Stopwatch.StartNew();

                        if (sensorManager.isSensorOnline(SensorTypeEnum.waterTemperature))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                logModel.AppendLog(Log.CreateLog("Water Temperature capturing", Log.LogType.Information));
                            }

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

                            if (capturedValue > -20 && capturedValue < 80)
                            {
                                if (!userSettings.isDiagnosticModeEnable)
                                {
                                    sensorManager.SetWaterTemperatureForPHSensor(capturedValue);
                                }

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("Water Temperature captured : {0}", capturedValue), Log.LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.waterTemperature, userSettings.isSleepModeEnable);
                                    logModel.AppendLog(Log.CreateLog(String.Format("Water Temperature status : {0}", status), Log.LogType.System));
                                }
                            }
                            else
                            {
                                logModel.AppendLog(Log.CreateLog(String.Format("Water Temperature value out of range - {0}", capturedValue), Log.LogType.Warning));
                            }
                        }

                        if (sensorManager.isSensorOnline(SensorTypeEnum.ph))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                logModel.AppendLog(Log.CreateLog("PH capturing", Log.LogType.Information));
                            }

                            decimal capturedValue = 0;
                            capturedValue = sensorManager.RecordPhMeasure(userSettings.isSleepModeEnable);

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("PH captured : {0}", capturedValue), Log.LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.ph, userSettings.isSleepModeEnable);
                                    logModel.AppendLog(Log.CreateLog(String.Format("PH status : {0}", status), Log.LogType.System));
                                }
                            }
                            else
                            {
                                logModel.AppendLog(Log.CreateLog(String.Format("PH value out of range - {0}", capturedValue), Log.LogType.Warning));
                            }
                        }

                        if (sensorManager.isSensorOnline(SensorTypeEnum.orp))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                logModel.AppendLog(Log.CreateLog("ORP capturing", Log.LogType.Information));
                            }

                            decimal capturedValue = 0;
                            capturedValue = sensorManager.RecordSensorsMeasure(SensorTypeEnum.orp, userSettings.isSleepModeEnable);

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("ORP captured : {0}", capturedValue), Log.LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.orp, userSettings.isSleepModeEnable);
                                    logModel.AppendLog(Log.CreateLog(String.Format("ORP status : {0}", status), Log.LogType.System));
                                }
                            }
                            else
                            {
                                logModel.AppendLog(Log.CreateLog(String.Format("ORP value out of range - {0}", capturedValue), Log.LogType.Warning));
                            }
                        }

                        if (sensorManager.isSensorOnline(SensorTypeEnum.dissolvedOxygen))
                        {
                            if (userSettings.isDiagnosticModeEnable)
                            {
                                logModel.AppendLog(Log.CreateLog("DO capturing", Log.LogType.Information));
                            }

                            decimal capturedValue = 0;
                            capturedValue = sensorManager.RecordSensorsMeasure(SensorTypeEnum.dissolvedOxygen, userSettings.isSleepModeEnable);

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

                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("DO captured : {0}", capturedValue), Log.LogType.Information));
                                    var status = sensorManager.GetSensorStatus(SensorTypeEnum.dissolvedOxygen, userSettings.isSleepModeEnable);
                                    logModel.AppendLog(Log.CreateLog(String.Format("DO status : {0}", status), Log.LogType.System));
                                }
                            }
                            else
                            {
                                logModel.AppendLog(Log.CreateLog(String.Format("DO value out of range - {0}", capturedValue), Log.LogType.Warning));
                            }
                        }

                        if (userSettings.isTempHumiditySensorEnable)
                        {
                            try
                            {
                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog("Air Temperature capturing", Log.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();

                                    if (userSettings.isDiagnosticModeEnable)
                                    {
                                        logModel.AppendLog(Log.CreateLog(String.Format("Air Temperature captured : {0}", capturedValue), Log.LogType.Information));
                                    }
                                }
                                else
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("Air Temperature out of range - {0}", capturedValue), Log.LogType.Warning));
                                }
                            }
                            catch (Exception ex)
                            {
                                logModel.AppendLog(Log.CreateErrorLog("Exception on Air Temperature Sensor", ex));
                            }

                            try
                            {
                                if (userSettings.isDiagnosticModeEnable)
                                {
                                    logModel.AppendLog(Log.CreateLog("Humidity capturing", Log.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();

                                    if (userSettings.isDiagnosticModeEnable)
                                    {
                                        logModel.AppendLog(Log.CreateLog(String.Format("Air Humidity captured : {0}", capturedValue), Log.LogType.Information));
                                    }
                                }
                                else
                                {
                                    logModel.AppendLog(Log.CreateLog(String.Format("Air Humidity out of range - {0}", capturedValue), Log.LogType.Warning));
                                }
                            }
                            catch (Exception ex)
                            {
                                logModel.AppendLog(Log.CreateErrorLog("Exception on Air Humidity Sensor", ex));
                            }
                        }

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

                        if (userSettings.isSigFoxComEnable && sigfoxManager.isInitialized && TICKSPERCYCLE >= 600000)
                        {
                            watchMesures.Restart();

                            string sigFoxSignature = String.Empty;

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

                            sigfoxManager.SendMessage(sigFoxSignature);

                            //if (userSettings.isDiagnosticModeEnable)
                            //    sigfoxManager.Listen();

                            logModel.AppendLog(Log.CreateLog(String.Format("Data sent to Azure via Sigfox in {0} sec.", watchMesures.ElapsedMilliseconds / 1000), Log.LogType.System));
                        }

                        if (!userSettings.isSigFoxComEnable && NetworkInterface.GetIsNetworkAvailable())
                        {
                            string sigFoxSignature = String.Empty;

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

                            using (var client = new HttpClient())
                            {
                                var request = new Message()
                                {
                                    content = sigFoxSignature,
                                    device  = userSettings.productionSiteId,
                                    date    = DateTime.Now.ToString(),
                                };

                                var taskWeb = Task.Run(async() =>
                                {
                                    try
                                    {
                                        var response = await client.PostAsync(userSettings.hubMessageAPI,
                                                                              new StringContent(JsonConvert.SerializeObject(request).ToString(),
                                                                                                Encoding.UTF8, "application/json"));

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

                                taskWeb.Wait();
                            }
                        }

                        bw.ReportProgress(33);
                    }
                }
                catch (Exception ex)
                {
                    logModel.AppendLog(Log.CreateErrorLog("Exception on Measures", ex));
                }
            }
            watch.Stop();
        }
Esempio n. 4
0
        public void Run()
        {
            var watch           = Stopwatch.StartNew();
            var captureDateTime = DateTime.Now;

            var clockManager = ClockManager.GetInstance;

            logModel.AppendLog(Log.CreateLog("[UTEST00] Sigfox Integration Test start", Log.LogType.Information));
            if (clockManager != null)
            {
                var taskClock = Task.Run(async() =>
                {
                    await clockManager.Connect();
                });
                taskClock.Wait();

                captureDateTime = clockManager.ReadDate();

                clockManager.Dispose();
            }

            sigfoxManager = SigfoxInterfaceManager.GetInstance;

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

            var taskSigfox = Task.Run(async() => { await sigfoxManager.InitSensors(); });

            taskSigfox.Wait();

            sensorManager = AtlasSensorManager.GetInstance;

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

            taskSensor.Wait();

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

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

            taskHumManager.Wait();

            try
            {
                var elapsedMs = watch.ElapsedMilliseconds;

                TimeSpan t = TimeSpan.FromMilliseconds(elapsedMs);

                var watchMesures = Stopwatch.StartNew();

                if (sensorManager.isSensorOnline(SensorTypeEnum.waterTemperature))
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST10] Water Temperature capturing", Log.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();

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

                if (sensorManager.isSensorOnline(SensorTypeEnum.ph))
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST13] PH capturing", Log.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();

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

                if (sensorManager.isSensorOnline(SensorTypeEnum.orp))
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST16] ORP capturing", Log.LogType.Information));

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

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

                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST17] ORP captured : {0}", capturedValue), Log.LogType.Information));
                        var status = sensorManager.GetSensorStatus(SensorTypeEnum.orp, false);
                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST18] ORP status : {0}", status), Log.LogType.System));
                    }
                    else
                    {
                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST19] ORP value out of range - {0}", capturedValue), Log.LogType.Warning));
                    }
                }

                if (sensorManager.isSensorOnline(SensorTypeEnum.dissolvedOxygen))
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST20] DO capturing", Log.LogType.Information));

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

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

                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST21] DO captured : {0}", capturedValue), Log.LogType.Information));
                        var status = sensorManager.GetSensorStatus(SensorTypeEnum.dissolvedOxygen, false);
                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST22] DO status : {0}", status), Log.LogType.System));
                    }
                    else
                    {
                        logModel.AppendLog(Log.CreateLog(String.Format("[UTEST23] DO value out of range - {0}", capturedValue), Log.LogType.Warning));
                    }
                }

                try
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST24] Air Temperature capturing", Log.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();

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

                try
                {
                    logModel.AppendLog(Log.CreateLog("[UTEST27] Humidity capturing", Log.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();

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

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

                watchMesures.Restart();

                string sigFoxSignature = String.Empty;

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

                sigfoxManager.SendMessage(sigFoxSignature);

                logModel.AppendLog(Log.CreateLog(String.Format("[UTEST31] Data sent to Azure via Sigfox in {0} sec.", watchMesures.ElapsedMilliseconds / 1000), Log.LogType.System));
            }

            catch (Exception ex)
            {
                logModel.AppendLog(Log.CreateErrorLog("Exception on Sigfox Integration Test", ex));
            }

            watch.Stop();
        }