diff --git a/TBF/Rig/RegisterReaders/GenesisRegReader/communication/OptoHeadTest.cs b/TBF/Rig/RegisterReaders/GenesisRegReader/communication/OptoHeadTest.cs index 8a3e13479..f54934ef8 100644 --- a/TBF/Rig/RegisterReaders/GenesisRegReader/communication/OptoHeadTest.cs +++ b/TBF/Rig/RegisterReaders/GenesisRegReader/communication/OptoHeadTest.cs @@ -1438,5 +1438,206 @@ namespace TBF.Rig.RegisterReaders.GenesisRegReader.communication return ex.Message; } } + + public string WriteQ3Calibration() + { + if (genesisHead != null && genesisHead.DebugLevel == DebugMode.Simulate) + { + log.Debug("Connect() - Simulated response"); + return ResultOk; //"Simulated Connect"; + } + return Task.Run(() => WriteQ3Calibration_Async(genesisHead)) + .GetAwaiter() + .GetResult(); + } + + private async Task WriteQ3Calibration_Async(GenesisSmartReader genesisSmartReader) + { + try + { + log.Debug("WriteQ3Calibration_Async called for iHead: " + genesisHead); + + if (genesisHead == null) return string.Empty; + if (string.IsNullOrEmpty(genesisHead.CommInterface)) return string.Empty; + GciBridge gciBridge = genesisHead.CommInterfaceBridge; + if (gciBridge == null) return string.Empty; + + CancellationToken token = default; + + bool areInitialisedData = genesisSmartReader.CalculateQ3Calibration(); + if (!areInitialisedData) + { + log.Error("WriteQ3Calibration_Async() - CalculateQ3Calibration Initialised Data not valid"); + return "CalculateQ3Calibration Initialised Data not valid"; + } + + if (!genesisSmartReader.Q3CalibValid) + { + log.Error("WriteQ3Calibration_Async() - Q3Calibration not valid"); + return "Q3Calibration not valid"; + } + + //Get Activity Status + LedState ledMode = LedState.inactive; // swich on LED + byte valueLed = (ledMode == LedState.active) ? (byte)6 : (byte)0; + UInt16 calibFactor1 = Convert.ToUInt16(genesisSmartReader.Q3Calib_Ch1Value); + UInt16 calibFactor2 = Convert.ToUInt16(genesisSmartReader.Q3Calib_Ch2Value); + UInt16 calibFactor3 = Convert.ToUInt16(genesisSmartReader.Q3Calib_Ch3Value); + UInt16 valueSampleRate = 2; + UInt16 ResetAccumulatorsValue = 0; + UInt16 ForwardArrowValue = 0; + UInt16 StoreCalibrationValue = 1; + + log.Debug( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - Start - Set Led Mode: {valueLed}"); + + var CalFactor1AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor1, calibFactor1, false, false, token); + if (CalFactor1AsyncResult == null || !CalFactor1AsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor1AsyncResult failed. Result: {CalFactor1AsyncResult}"); + return "Failed to set CalFactor1"; + } + + var CalFactor2AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor2, calibFactor2, false, false, token); + if (CalFactor2AsyncResult == null || !CalFactor2AsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor2AsyncResult failed. Result: {CalFactor2AsyncResult}"); + return "Failed to set CalFactor2"; + } + + var CalFactor3AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor3, calibFactor3, false, false, token); + if (CalFactor3AsyncResult == null || !CalFactor3AsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor3AsyncResult failed. Result: {CalFactor3AsyncResult}"); + return "Failed to set CalFactor3"; + } + + log.Info( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - Set DONE CalFactor1: {calibFactor1}, CalFactor2: {calibFactor2}, CalFactor2: {calibFactor3}"); + + var SampleRateAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.SampleRate, valueSampleRate, false, false, token); + if (SampleRateAsyncResult == null || !SampleRateAsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - SampleRateAsyncResult failed. Result: {SampleRateAsyncResult}"); + return "Failed to set SampleRate"; + } + + var LedModeAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.LedMode, valueLed, false, false, token); + if (LedModeAsyncResult == null || !LedModeAsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - LedModeAsyncResult failed. Result: {LedModeAsyncResult}"); + return "Failed to disable Led Mode"; + } + + var ResetAccumulatorsAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.ResetAccumulators, ResetAccumulatorsValue, false, false, token); + if (ResetAccumulatorsAsyncResult == null || !ResetAccumulatorsAsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - ResetAccumulatorsAsyncResult failed. Result: {ResetAccumulatorsAsyncResult}"); + return "Failed set ResetAccumulators to 1"; + } + + var ForwardArrowAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.ForwardArrow, ForwardArrowValue, false, false, token); + if (ForwardArrowAsyncResult == null || !ForwardArrowAsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - ForwardArrowAsyncResult failed. Result: {ForwardArrowAsyncResult}"); + return "Failed set ForwardArrow to 0"; + } + + var StoreCalibrationAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.StoreCalibration, StoreCalibrationValue, false, false, token); + if (StoreCalibrationAsyncResult == null || !StoreCalibrationAsyncResult.Success) + { + log.Error( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - StoreCalibrationAsyncResult failed. Result: {StoreCalibrationAsyncResult}"); + return "Failed set StoreCalibration to 1"; + } + + log.Debug( $"WriteQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - StoreCalibrationAsyncResult Result: " + StoreCalibrationAsyncResult); + + return StoreCalibrationAsyncResult.Success ? ResultOk : ResultNok; + } + catch (Exception ex) + { + log.Error($"WriteQ3Calibration_Async({genesisHead.GetSlotNr}) - Exception:", ex); + return ex.Message; + } + } + + + public string PrepareQ3Calibration() + { + if (genesisHead != null && genesisHead.DebugLevel == DebugMode.Simulate) + { + log.Debug("Connect() - Simulated response"); + return ResultOk;//"Simulated Connect"; + } + return Task.Run(() => PrepareQ3Calibration_Async(genesisHead)) + .GetAwaiter() + .GetResult(); + } + + + private async Task PrepareQ3Calibration_Async(GenesisSmartReader genesisSmartReader) + { + try + { + log.Debug("PrepareQ3Calibration_Async called for iHead: " + genesisHead); + + if (genesisHead == null) return string.Empty; + if (string.IsNullOrEmpty(genesisHead.CommInterface)) return string.Empty; + GciBridge gciBridge = genesisHead.CommInterfaceBridge; + if (gciBridge == null) return string.Empty; + + CancellationToken token = default; + + //Get Activity Status + LedState ledMode = LedState.active; // swich on LED + byte valueLed = (ledMode == LedState.active) ? (byte)6 : (byte)0; + UInt16 valueCalibrate = 15625; + UInt16 valueSampleRate = 10; + + log.Debug( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - Start - Set Led Mode: {valueLed}"); + + var CalFactor1AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor1, valueCalibrate, false, false, token); + if (CalFactor1AsyncResult == null || !CalFactor1AsyncResult.Success) + { + log.Error( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor1AsyncResult failed. Result: {CalFactor1AsyncResult}"); + return "Failed to disable Led Mode"; + } + + var CalFactor2AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor2, valueCalibrate, false, false, token); + if (CalFactor2AsyncResult == null || !CalFactor2AsyncResult.Success) + { + log.Error( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor2AsyncResult failed. Result: {CalFactor2AsyncResult}"); + return "Failed to disable Led Mode"; + } + + var CalFactor3AsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.CalFactor3, valueCalibrate, false, false, token); + if (CalFactor3AsyncResult == null || !CalFactor3AsyncResult.Success) + { + log.Error( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - CalFactor3AsyncResult failed. Result: {CalFactor3AsyncResult}"); + return "Failed to disable Led Mode"; + } + + var SampleRateAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.SampleRate, valueSampleRate, false, false, token); + if (SampleRateAsyncResult == null || !SampleRateAsyncResult.Success) + { + log.Error( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - SampleRateAsyncResult failed. Result: {SampleRateAsyncResult}"); + return "Failed to disable Led Mode"; + } + + var LedModeAsyncResult = await gciBridge.WriteRegisterWithRetryAsync(genesisSmartReader.GetSlotNr, RadioService.LedMode, valueLed, false, false, token); + if (LedModeAsyncResult == null || !LedModeAsyncResult.Success) + { + log.Error( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - LedModeAsyncResult failed. Result: {LedModeAsyncResult}"); + return "Failed to disable Led Mode"; + } + + log.Debug( $"PrepareQ3Calibration_Async( Slot: {genesisSmartReader.GetSlotNr}) - PrepareQ3Calibration_Async Result: " + LedModeAsyncResult); + + return LedModeAsyncResult.Success ? ResultOk : ResultNok; + } + catch (Exception ex) + { + log.Error($"PrepareQ3Calibration_Async({genesisHead.GetSlotNr}) - Exception:", ex); + return ex.Message; + } + } } } \ No newline at end of file diff --git a/TBF/Rig/RegisterReaders/GenesisRegReader/communication/RadioService.cs b/TBF/Rig/RegisterReaders/GenesisRegReader/communication/RadioService.cs index cc5e984f0..6c04e54c4 100644 --- a/TBF/Rig/RegisterReaders/GenesisRegReader/communication/RadioService.cs +++ b/TBF/Rig/RegisterReaders/GenesisRegReader/communication/RadioService.cs @@ -391,7 +391,14 @@ namespace TBF.Rig.RegisterReaders.GenesisRegReader.communication } } - public static readonly String LedMode = "GENESISFLOW_LedMode"; + public static readonly String LedMode = "GENESISFLOW_LedMode"; + public static readonly String SampleRate = "GENESISFLOW_SampleRate"; + public static readonly String CalFactor1 = "GENESISFLOW_CalFactor1"; + public static readonly String CalFactor2 = "GENESISFLOW_CalFactor2"; + public static readonly String CalFactor3 = "GENESISFLOW_CalFactor3"; + public static readonly String ResetAccumulators = "GENESISFLOW_ResetAccumulators"; + public static readonly String ForwardArrow = "GENESISFLOW_ForwardArrow"; + public static readonly String StoreCalibration = "GENESISFLOW_StoreCalibration"; public async Task GetActivityLedStatusMode_Async( GenesisSmartReader iHead, diff --git a/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/GenesisSmartReader.cs b/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/GenesisSmartReader.cs index c93461bb1..81c7eae49 100644 --- a/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/GenesisSmartReader.cs +++ b/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/GenesisSmartReader.cs @@ -3958,6 +3958,24 @@ namespace TBF.Rig.RegisterReaders.GenesisRegReader.implementations void SetQ3Calibration(double[] q3CalibInitial) { this.q3CalibInitial = q3CalibInitial; } + private double refVolume = double.NaN; + private double refTime = double.NaN; + public double RefVolume { get => refVolume; set => refVolume = value; } + public double RefTime { get => refTime; set => refTime = value; } + + + public bool CalculateQ3Calibration() + { + if (Double.IsNaN(refVolume) || Double.IsNaN((refTime))) + { + log.Debug("RefVolume or RefTime is NaN"); + return false; + } + + CalculateQ3Calibration( refVolume, refTime); + return true; + } + public void CalculateQ3Calibration(double refVolume, double refTime) { GetQ3Calibration(refVolume, refTime, q3CalibInitial, ref isChQ3CalibValid, ref q3CalibCh); @@ -4080,18 +4098,6 @@ namespace TBF.Rig.RegisterReaders.GenesisRegReader.implementations log.Debug("=== Q3 CALIBRATION END ==="); } - - - void newPokus() - { - //TODO BUMI implement genesis communication - //volat z GCI Bridge - - //vybere sa component - GCI bridge - // - rozhranie - // - database - - } - + } } \ No newline at end of file diff --git a/TBF/Rig/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs b/TBF/Rig/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs index 7c1e347d7..de47557ad 100644 --- a/TBF/Rig/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs +++ b/TBF/Rig/TestMethods/FlyingStartMassCollection/FlyingStartMassCollectionSeq.cs @@ -1092,6 +1092,11 @@ namespace TBF.Rig.TestMethods.FlyingStartMassCollection CalculateMeterResults(meterRslt, GenesisSmart, tstRslt,GenesisSmart.TimestampSecStart, GenesisSmart.TimestampSecEnd, GenesisSmart.VolumeLtrStart, GenesisSmart.VolumeLtrEnd); + //Init Calculate Calibration + GenesisSmart.RefVolume = meterRslt.VolumeRef; + GenesisSmart.RefTime = meterRslt.TestTime; + + //Store Raw Calibration Data to database WaterMeterParentCopy(i, CH1,testName,meterRslt,GenesisSmart,tstRslt); WaterMeterParentCopy(i, CH2,testName,meterRslt,GenesisSmart,tstRslt); WaterMeterParentCopy(i, CH3,testName,meterRslt,GenesisSmart,tstRslt); diff --git a/TBF/Rig/TestMethods/GenesisCommunication/iPerlCommunicationParams.cs b/TBF/Rig/TestMethods/GenesisCommunication/iPerlCommunicationParams.cs index f7df5b824..ff4edde04 100644 --- a/TBF/Rig/TestMethods/GenesisCommunication/iPerlCommunicationParams.cs +++ b/TBF/Rig/TestMethods/GenesisCommunication/iPerlCommunicationParams.cs @@ -61,6 +61,8 @@ namespace TBF.Rig.TestMethods.GenesisCommunication retVal.Add(iPerlCommunicationForm.SlotSetTestModeStr); retVal.Add(iPerlCommunicationForm.SlotSetActiveModeStr); retVal.Add(iPerlCommunicationForm.SlotDisconnectStr); + retVal.Add(iPerlCommunicationForm.PrepareSlotQ3CalibrationStr); + retVal.Add(iPerlCommunicationForm.WriteSlotQ3CalibrationStr); retVal.Add(iPerlCommunicationForm.ReadConfigurationStr); diff --git a/TBF/Rig/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs b/TBF/Rig/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs index e784a4e34..7613d4624 100644 --- a/TBF/Rig/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs +++ b/TBF/Rig/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs @@ -87,6 +87,8 @@ namespace TBF.Rig.TestMethods.iPerlCommunication public const string SlotSetTestModeStr = "Switch to Test Mode"; public const string SlotSetActiveModeStr ="Switch to Active Mode"; public const string SlotDisconnectStr = "Disconnect Slot"; + public const string PrepareSlotQ3CalibrationStr = "Prepare Q3 Calibration Slot"; + public const string WriteSlotQ3CalibrationStr = "Write Q3 Calibration Slot"; public const string ReadConfigurationStr = "Read configuration"; /// Example: "Read configuration" or "Read configuration if enabled" public const string SetTestModeStr = "Set Test mode"; /// Example: "Set Test mode" or "Set Test mode A0" (hexadecimal number is the required 'testModeConfig' @@ -756,7 +758,9 @@ namespace TBF.Rig.TestMethods.iPerlCommunication else if (currentActivity.ToLower().Equals(SlotGroupedLoginStr.ToLower())) error = SlotGroupedLogin(threadID, ihead, ref resultStr); else if (currentActivity.ToLower().Equals(SlotSetTestModeStr.ToLower())) error = SlotSetTestMode(threadID, ihead, ref resultStr); else if (currentActivity.ToLower().Equals(SlotSetActiveModeStr.ToLower())) error = SlotSetActiveMode(threadID, ihead, ref resultStr); - else if (currentActivity.ToLower().Equals(SlotDisconnectStr.ToLower())) error = SlotDisconnect(threadID, ihead, ref resultStr); + else if (currentActivity.ToLower().Equals(SlotDisconnectStr.ToLower())) error = SlotDisconnect(threadID, ihead, ref resultStr); + else if (currentActivity.ToLower().Equals(PrepareSlotQ3CalibrationStr.ToLower())) error = PrepareSlotQ3Calibration(threadID, ihead, ref resultStr); + else if (currentActivity.ToLower().Equals(WriteSlotQ3CalibrationStr.ToLower())) error = WriteSlotQ3Calibration(threadID, ihead, ref resultStr); else if (currentActivity.ToLower().Contains(ReadSerialNrStr.ToLower())) error = ReadSerialNr(threadID, ihead, ref resultStr); else if (currentActivity.ToLower().Contains(SetTestModeStr.ToLower())) error = SetTestMode(threadID, ihead, ref resultStr); @@ -855,6 +859,42 @@ namespace TBF.Rig.TestMethods.iPerlCommunication } } + private CommErr PrepareSlotQ3Calibration(int threadId, GenesisSmartReader iHead, ref string resultStr) + { + CommErr error = CommErr.FailedLogin; + var gciFullLoginResult = iHead.OptoHeadTest.PrepareQ3Calibration(); + if (!string.IsNullOrEmpty(resultStr) && resultStr.Equals(TBF.Rig.RegisterReaders.GenesisRegReader.communication.OptoHeadTest.ResultOk)) + { + log.Debug("SlotLogin successful"); + resultStr = string.Format($"Write Q3 Calibration: OK"); + //TODO BUMI make note Q3 calibration is done + error = CommErr.None; + } + else + { + resultStr = "Write Q3 Calibration: Failed"; + } + return error; + } + + private CommErr WriteSlotQ3Calibration(int threadId, GenesisSmartReader iHead, ref string resultStr) + { + CommErr error = CommErr.FailedLogin; + var gciFullLoginResult = iHead.OptoHeadTest.WriteQ3Calibration(); + if (!string.IsNullOrEmpty(resultStr) && resultStr.Equals(TBF.Rig.RegisterReaders.GenesisRegReader.communication.OptoHeadTest.ResultOk)) + { + log.Debug("SlotLogin successful"); + resultStr = string.Format($"Write Q3 Calibration: OK"); + //TODO BUMI make note Q3 calibration is done + error = CommErr.None; + } + else + { + resultStr = "Write Q3 Calibration: Failed"; + } + return error; + } + private CommErr SlotGroupedLogin(int threadId, GenesisSmartReader iHead, ref string resultStr) { CommErr error = CommErr.FailedLogin; @@ -869,7 +909,7 @@ namespace TBF.Rig.TestMethods.iPerlCommunication iHead.ConfigStruct.PCBNumberString = gciFullLoginResult?.PcbResult?.Result?.PcbId; } error = CommErr.None; - } + } else { resultStr = "Failed Combined Login";