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(); }