2015-04-15 18:32:56 +00:00
///
2021-06-24 06:05:13 +00:00
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
2015-04-15 18:32:56 +00:00
///
using System ;
2014-07-28 07:54:35 +00:00
using System.Collections.Generic ;
using log4net ;
2022-01-23 18:56:15 +00:00
using Config.Entities ;
2021-10-26 09:23:57 +00:00
using TBF.Rig.GenericDevices ;
2014-07-31 15:04:09 +00:00
using TBF.Boxes ;
2014-07-28 07:54:35 +00:00
2021-10-26 09:23:57 +00:00
namespace TBF.Rig.Elde.RegulValve
2014-07-28 07:54:35 +00:00
{
public class SetFlowOp : IOperation
{
private static readonly ILog log = LogManager . GetLogger ( typeof ( SetFlowOp ) ) ;
public override string ToString ( )
{
2019-05-10 14:26:58 +00:00
return string . Format ( "SetFlowOp({0}, Qfrom={1}, Qto={2})" , regulValve . Name , requiredFlowLo , requiredFlowHi ) ;
2014-07-28 07:54:35 +00:00
}
2021-06-24 06:05:13 +00:00
public const double RqrdFlowRangeRatio = 0.5 ;
2014-07-28 07:54:35 +00:00
/// Set by the constructor
readonly ControlBoardDev controlBoard ;
2021-01-08 09:02:39 +00:00
readonly RegulValve regulValve ;
2014-07-28 07:54:35 +00:00
readonly int regulValveNr ;
2017-12-14 14:10:17 +00:00
readonly IFlowMeter flowMeter ;
2019-04-12 15:30:20 +00:00
readonly double requiredFlowLo ;
readonly double requiredFlowHi ;
readonly double reqFlowAve ;
2019-05-02 07:36:33 +00:00
readonly int timeout ;
2014-07-28 07:54:35 +00:00
readonly bool leaveMeasurementRunning ;
2016-10-13 11:41:00 +00:00
readonly bool reTryCommands ;
2014-07-28 07:54:35 +00:00
2019-04-12 15:30:20 +00:00
readonly double nominalFlow ;
readonly double maximalFlow ;
2017-10-19 08:34:10 +00:00
///
2015-09-22 16:39:00 +00:00
/// Internal states of this operation
///
2014-07-28 07:54:35 +00:00
enum OpState
{
Idle = 0 ,
2015-10-15 17:49:42 +00:00
ValveMoveToPosition1 , /// Wait 1 takt
ValveMoveToPosition2 , /// This is actual move to position
ValveMoveToPosition3 , /// Wait 1 takt
2016-10-13 11:41:00 +00:00
CheckStatePosition , /// Verify
2014-07-28 07:54:35 +00:00
SettingPosition ,
2015-10-15 17:49:42 +00:00
ValveMoveToFlow1 , /// Wait 1 takt
ValveMoveToFlow2 , /// This is actual move to flow
ValveMoveToFlow3 , /// Wait 1 takt
2016-10-13 11:41:00 +00:00
CheckStateFlow , /// Verify
2014-07-28 07:54:35 +00:00
SettingFlow ,
FlowReached ,
2015-10-15 17:49:42 +00:00
SendCommandAgain ,
2014-07-28 07:54:35 +00:00
}
OpState opState ;
2019-04-12 15:30:20 +00:00
double currentReqFlowLo ;
double currentReqFlowHi ;
2014-07-28 07:54:35 +00:00
float targetPositionLo ;
float targetPositionHi ;
2019-03-08 13:52:02 +00:00
int startTime ;
2014-07-28 07:54:35 +00:00
int expireTime ;
2017-12-14 14:10:17 +00:00
DoubleBox flowBox ;
2014-09-08 18:24:46 +00:00
int flowWithinBoundsTime ;
2014-07-28 07:54:35 +00:00
/// <summary>
/// Set required water flow. Conditionally leave the measurement running.
/// Events: FlowSet, FlowTimeOut
/// </summary>
/// <param name="controlBoard">Control board device</param>
2017-12-14 14:10:17 +00:00
/// <param name="regulValve">Regulation valve component</param>
2014-07-28 07:54:35 +00:00
/// <param name="flowMeter">Flowmeter component</param>
/// <param name="requiredFlowLo">Lower limit of the flow to be achieved in [m3/h]</param>
/// <param name="requiredFlowHi">Upper limit of the flow to be achieved in [m3/h]</param>
2015-01-03 20:17:06 +00:00
/// <param name="pidCoef">PID coefficient (float)</param>
2014-07-28 07:54:35 +00:00
/// <param name="timeout">Timeout for the flow setting in [s]</param>
/// <param name="leaveMeasurementRunning">true = Leave the measurement running after op. stop</param>
/// <remarks>Only Elde.Valve flow are used, other flow on the lists are ignored</remarks>
2021-09-23 09:46:40 +00:00
public SetFlowOp ( ControlBoardDev controlBoard , IRegValve regulValve , IFlowMeter flowMeter , double requiredFlowLo , double requiredFlowHi ,
2019-05-10 14:26:58 +00:00
DoubleBox flowBox , bool reTryCmds , int timeout , bool leaveMeasurementRunning )
2014-07-28 07:54:35 +00:00
{
if ( controlBoard = = null ) throw new ArgumentNullException ( "ctrlBoard" ) ;
this . controlBoard = controlBoard ;
2021-01-08 09:02:39 +00:00
this . regulValve = regulValve as RegulValve ;
2017-12-14 14:10:17 +00:00
if ( this . regulValve = = null ) throw new ArgumentNullException ( "regValve is null or not Elde" ) ;
2015-02-03 09:30:24 +00:00
regulValveNr = this . regulValve . Idx1 ;
2014-07-28 07:54:35 +00:00
2017-12-14 14:10:17 +00:00
if ( flowMeter = = null ) throw new ArgumentNullException ( "flowMeter" ) ;
this . flowMeter = flowMeter ;
2014-09-10 11:43:33 +00:00
2019-04-12 15:30:20 +00:00
nominalFlow = flowMeter . NominalFlow ;
maximalFlow = nominalFlow * 1.25 ;
2014-07-28 07:54:35 +00:00
2022-01-23 18:56:15 +00:00
double rdrdFlowLoBeforeCorr = requiredFlowLo - MeasurementCorrection . GetCorrection ( requiredFlowLo , flowMeter . Corrections ) ;
double rdrdFlowHiBeforeCorr = requiredFlowHi - MeasurementCorrection . GetCorrection ( requiredFlowHi , flowMeter . Corrections ) ;
2021-06-24 06:05:13 +00:00
this . reqFlowAve = ( rdrdFlowLoBeforeCorr + rdrdFlowHiBeforeCorr ) / 2 ;
this . requiredFlowLo = ( RqrdFlowRangeRatio * rdrdFlowLoBeforeCorr ) + ( ( 1 - RqrdFlowRangeRatio ) * this . reqFlowAve ) ; /// Move the lower limit a bit of the range up
this . requiredFlowHi = ( RqrdFlowRangeRatio * rdrdFlowHiBeforeCorr ) + ( ( 1 - RqrdFlowRangeRatio ) * this . reqFlowAve ) ; /// Move the upper limit a bit of the range down
2017-12-14 14:10:17 +00:00
if ( flowBox = = null ) throw new ArgumentNullException ( "flowBox" ) ;
this . flowBox = flowBox ;
2016-10-13 11:41:00 +00:00
this . reTryCommands = reTryCmds ;
2014-07-28 07:54:35 +00:00
this . timeout = timeout ;
this . leaveMeasurementRunning = leaveMeasurementRunning ;
log . Debug ( this . ToString ( ) ) ;
}
/// <summary>
/// Set required water flow - Do not leave the measurement running.
/// Events: FlowSet
/// </summary>
2021-09-23 09:46:40 +00:00
public SetFlowOp ( ControlBoardDev controlBoard , IRegValve regValve , IFlowMeter flowMeter , double requiredFlowLo , double requiredFlowHi ,
2019-05-10 14:26:58 +00:00
DoubleBox flowbox , bool reTryCmds , int timeout )
: this ( controlBoard , regValve , flowMeter , requiredFlowLo , requiredFlowHi , flowbox , reTryCmds , timeout , false )
2014-07-28 07:54:35 +00:00
{
}
/// <summary>
/// Set required water flow - No timeout.
/// Events: FlowSet
/// </summary>
2021-09-23 09:46:40 +00:00
public SetFlowOp ( ControlBoardDev controlBoard , IRegValve regValve , IFlowMeter flowMeter , double requiredFlowLo , double requiredFlowHi ,
2019-05-10 14:26:58 +00:00
DoubleBox flowbox , bool reTryCmds )
: this ( controlBoard , regValve , flowMeter , requiredFlowLo , requiredFlowHi , flowbox , reTryCmds , int . MaxValue , false )
2014-07-28 07:54:35 +00:00
{
}
/// <summary>
/// Fetch target position limits from the dictionary or return false
/// </summary>
/// <param name="avgReqFlow">Average of the flow targer range (input)</param>
/// <param name="positionLo">Target valve position low limit (output)</param>
/// <param name="positionHi">Target valve position high limit (output)</param>
/// <returns>true when positions for the target flow are stored in the memory, otherwise return false</returns>
2019-04-12 15:30:20 +00:00
bool FetchTargetPosition ( double avgReqFlow , out float positionLo , out float positionHi )
2014-07-28 07:54:35 +00:00
{
float targetPosition ;
if ( regulValve . Dict . TryGetValue ( avgReqFlow , out targetPosition ) )
{
positionLo = Math . Max ( targetPosition * 0.95f , 0.0f ) ;
positionHi = Math . Min ( targetPosition * 1.05f , 100.0f ) ;
return true ;
}
else
{
positionLo = 0.0f ;
positionHi = 0.0f ;
return false ;
}
}
2019-04-12 15:30:20 +00:00
void StoreTargetPosition ( double avgReqFlow , float actPosition )
2014-07-28 07:54:35 +00:00
{
2018-11-08 08:54:40 +00:00
if ( ! regulValve . Dict . ContainsKey ( reqFlowAve ) )
2014-07-28 07:54:35 +00:00
{
2019-04-12 15:30:20 +00:00
regulValve . Dict . Add ( new KeyValuePair < double , float > ( avgReqFlow , actPosition ) ) ;
2014-07-28 07:54:35 +00:00
}
return ;
}
/// <summary>Start this operation</summary>
public void Start ( )
{
2019-03-08 13:52:02 +00:00
startTime = StateMachine . Time ;
expireTime = StateMachine . Time + timeout ;
if ( expireTime < 0 ) expireTime = int . MaxValue ;
2017-12-14 14:10:17 +00:00
double flowMtrFreq = flowMeter . ReadFrequency ( ) ;
double flow = flowMeter . ReadFlow ( ) ;
2018-06-05 09:16:13 +00:00
currentReqFlowLo = requiredFlowLo ; /// Default lower limit
currentReqFlowHi = requiredFlowHi ; /// Default upper limit
2021-06-24 06:05:13 +00:00
//if (flow != 0)
//{
// flowBox.Val = flow;
// if (flow >= reqFlowAve) currentReqFlowHi = reqFlowAve; /// Decrease upper limit
// if (flow <= reqFlowAve) currentReqFlowLo = reqFlowAve; /// Increase lower limit
//}
2017-10-19 08:34:10 +00:00
2014-09-08 18:24:46 +00:00
flowWithinBoundsTime = 0 ;
2018-11-08 08:54:40 +00:00
if ( regulValve . RegulValveCfg . StoredPositionReuse & & FetchTargetPosition ( reqFlowAve , out targetPositionLo , out targetPositionHi ) )
2014-07-28 07:54:35 +00:00
{
2016-04-20 10:43:59 +00:00
log . InfoFormat ( "SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3} FETCHED: posLo={4} posHi={5}" ,
2017-12-14 14:10:17 +00:00
regulValveNr , flowMeter . Idx1 , currentReqFlowLo , currentReqFlowHi , targetPositionLo , targetPositionHi ) ;
2014-07-28 07:54:35 +00:00
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToPosition1 ;
2014-07-28 07:54:35 +00:00
}
else
{
2016-04-20 10:43:59 +00:00
log . InfoFormat ( "SetFlowOp:Start() rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}" ,
2017-12-14 14:10:17 +00:00
regulValveNr , flowMeter . Idx1 , currentReqFlowLo , currentReqFlowHi ) ;
2014-07-28 07:54:35 +00:00
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToFlow1 ;
2014-07-28 07:54:35 +00:00
}
2019-10-24 10:57:37 +00:00
/// Start flow measurement
controlBoard . SendCommand ( Command . Start , flowMeter . Idx1 , int . MaxValue , int . MaxValue , 0 , 0 ) ; /// TestMethods=0, StopDevs=0
2014-07-28 07:54:35 +00:00
}
/// <summary>Run this operation</summary>
/// <returns>
2019-10-15 08:03:28 +00:00
/// Event.OpArgumentError
/// Event.Starting
/// Event.Busy
/// Event.FlowReached
/// Event.RegulValveTimeOut
2014-07-28 07:54:35 +00:00
/// </returns>
public Event Run ( )
{
float rvPosition = controlBoard . RValvePosition ( regulValveNr ) ;
2017-12-14 14:10:17 +00:00
double flowMtrFreq = flowMeter . ReadFrequency ( ) ;
double flow = flowMeter . ReadFlow ( ) ;
2014-07-28 07:54:35 +00:00
2017-12-14 14:10:17 +00:00
if ( flow ! = 0 )
2016-10-13 11:41:00 +00:00
{
2017-12-14 14:10:17 +00:00
flowBox . Val = flow ;
2016-10-13 11:41:00 +00:00
}
2014-07-28 07:54:35 +00:00
2018-06-05 09:16:13 +00:00
log . InfoFormat ( "SetFlowOp:Run(T={0}) rv#={1} pos={2}% freq={3} flow={4} opState={5}" ,
2019-03-08 13:52:02 +00:00
StateMachine . Time - startTime , regulValveNr , rvPosition . ToString ( "F1" ) , flowMtrFreq , flow , opState ) ;
2015-10-15 17:49:42 +00:00
2018-06-05 09:16:13 +00:00
if ( StateMachine . Time > expireTime )
{
return Event . RegulValveTimeOut ;
}
else if ( opState = = OpState . SendCommandAgain )
2015-09-18 05:46:57 +00:00
{
2015-10-15 17:49:42 +00:00
flowWithinBoundsTime = 0 ;
2018-11-08 08:54:40 +00:00
if ( regulValve . RegulValveCfg . StoredPositionReuse & & FetchTargetPosition ( reqFlowAve , out targetPositionLo , out targetPositionHi ) )
2015-10-15 17:49:42 +00:00
{
2016-05-31 09:07:11 +00:00
log . InfoFormat ( "Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3} FETCHED: posLo={4} posHi={5}" ,
2017-12-14 14:10:17 +00:00
regulValveNr , flowMeter . Idx1 , currentReqFlowLo , currentReqFlowHi , targetPositionLo , targetPositionHi ) ;
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToPosition1 ;
}
else
{
2016-05-31 09:07:11 +00:00
log . InfoFormat ( "Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}" ,
2017-12-14 14:10:17 +00:00
regulValveNr , flowMeter . Idx1 , currentReqFlowLo , currentReqFlowHi ) ;
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToFlow1 ;
}
2019-10-24 10:57:37 +00:00
/// Start flow measurement
controlBoard . SendCommand ( Command . Start , flowMeter . Idx1 , int . MaxValue , int . MaxValue , 0 , 0 ) ; /// TestMethods=0, StopDevs=0
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-09-18 05:46:57 +00:00
}
2015-10-15 17:49:42 +00:00
else if ( opState = = OpState . ValveMoveToPosition1 )
2014-07-28 07:54:35 +00:00
{
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToPosition2 ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
else if ( opState = = OpState . ValveMoveToPosition2 ) /// Move to position
{
/// Issues the appropriate ValveMove(...) command
2014-07-28 07:54:35 +00:00
controlBoard . ValveMove ( regulValveNr , RegulValveMode . TargetPosition ,
2014-09-05 12:17:12 +00:00
new float [ 2 ] { targetPositionLo , targetPositionHi } ,
2015-10-15 17:49:42 +00:00
regulValve . StableTime ) ;
2014-07-28 07:54:35 +00:00
opState = OpState . SettingPosition ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2014-07-28 07:54:35 +00:00
}
2015-10-15 17:49:42 +00:00
else if ( opState = = OpState . ValveMoveToPosition3 )
2015-09-18 05:46:57 +00:00
{
2016-10-13 11:41:00 +00:00
if ( reTryCommands )
{
opState = OpState . CheckStatePosition ;
}
else
{
opState = OpState . SettingPosition ;
}
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-09-18 05:46:57 +00:00
}
2016-10-13 11:41:00 +00:00
else if ( opState = = OpState . CheckStatePosition ) /// Verify
2015-10-15 17:49:42 +00:00
{
2015-10-21 06:00:44 +00:00
if ( ( ( ulong ) controlBoard . StatusP & ( ulong ) StatusP . RefPulsesMsrmnt ) = = 0 )
2015-10-15 17:49:42 +00:00
{
opState = OpState . SendCommandAgain ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
2018-06-05 09:16:13 +00:00
else if ( controlBoard . RegulValveState ( regulValveNr ) ! = RegulValveState . DacValueRegul )
2015-10-15 17:49:42 +00:00
{
/// Repeat appropriate ValveMove(...) command
controlBoard . ValveMove ( regulValveNr , RegulValveMode . TargetPosition ,
new float [ 2 ] { targetPositionLo , targetPositionHi } ,
regulValve . StableTime ) ;
2014-07-28 07:54:35 +00:00
2016-04-20 10:43:59 +00:00
log . InfoFormat ( "SetFlowOp: ValveMove({0}, Position, {1}, {2}, {3})" ,
regulValveNr , targetPositionLo , targetPositionHi , regulValve . StableTime ) ;
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToPosition3 ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
else
{
opState = OpState . SettingPosition ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
}
else if ( opState = = OpState . SettingPosition )
{
/// Repeated untill position is reached
if ( ( targetPositionLo < = rvPosition ) & & ( rvPosition < = targetPositionHi ) )
{
opState = OpState . ValveMoveToFlow1 ;
}
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
else if ( opState = = OpState . ValveMoveToFlow1 )
2015-09-18 05:46:57 +00:00
{
opState = OpState . ValveMoveToFlow2 ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-09-18 05:46:57 +00:00
}
2015-10-15 17:49:42 +00:00
else if ( opState = = OpState . ValveMoveToFlow2 ) /// Move to flow
2015-05-21 14:38:27 +00:00
{
///
/// Done once, issues an appropriate ValveMove(...) command
///
2019-04-12 15:30:20 +00:00
if ( ( currentReqFlowLo > = currentReqFlowHi ) | | ( currentReqFlowLo > maximalFlow ) | | ( currentReqFlowHi < = 0 ) )
2015-05-21 14:38:27 +00:00
{
return Event . OpArgumentError ;
}
log . InfoFormat ( "Run(): rv#={0} flowMtr#={1} TARGET: flowLo={2} flowHi={3}" ,
2017-12-14 14:10:17 +00:00
regulValveNr , flowMeter . Idx1 , currentReqFlowLo , currentReqFlowHi ) ;
2015-05-21 14:38:27 +00:00
2019-04-12 15:30:20 +00:00
double freqLo = 2000.0 * currentReqFlowLo / nominalFlow ;
double freqHi = 2000.0 * currentReqFlowHi / nominalFlow ;
2015-05-21 14:38:27 +00:00
controlBoard . ValveMove ( regulValveNr , RegulValveMode . TargetFrequency ,
2019-04-12 15:30:20 +00:00
new float [ 2 ] { ( float ) freqLo , ( float ) freqHi } ,
2015-05-21 14:38:27 +00:00
regulValve . StableTime ) ;
2015-10-15 17:49:42 +00:00
opState = OpState . ValveMoveToFlow3 ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
else if ( opState = = OpState . ValveMoveToFlow3 )
{
2016-10-13 11:41:00 +00:00
if ( reTryCommands )
{
opState = OpState . CheckStateFlow ;
}
else
{
opState = OpState . SettingFlow ;
}
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-05-21 14:38:27 +00:00
}
2016-10-13 11:41:00 +00:00
else if ( opState = = OpState . CheckStateFlow ) /// Verify the flow setting
2015-10-15 17:49:42 +00:00
{
2018-06-05 09:16:13 +00:00
if ( ( ( ulong ) controlBoard . StatusP & ( ulong ) StatusP . RefPulsesMsrmnt ) = = 0 | |
( controlBoard . RegulValveState ( regulValveNr ) & RegulValveState . PwOrFreqRegul ) ! = RegulValveState . PwOrFreqRegul )
2015-10-15 17:49:42 +00:00
{
opState = OpState . SendCommandAgain ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2015-10-15 17:49:42 +00:00
}
2018-06-05 09:16:13 +00:00
else if ( controlBoard . RegulValveState ( regulValveNr ) ! = RegulValveState . PwOrFreqRegul )
2016-04-10 19:30:24 +00:00
{
/// Done once, issues an appropriate ValveMove(...) command
2019-04-12 15:30:20 +00:00
double freqLo = 2000.0 * currentReqFlowLo / nominalFlow ;
double freqHi = 2000.0 * currentReqFlowHi / nominalFlow ;
2016-04-10 19:30:24 +00:00
controlBoard . ValveMove ( regulValveNr , RegulValveMode . TargetFrequency ,
2019-04-12 15:30:20 +00:00
new float [ 2 ] { ( float ) freqLo , ( float ) freqHi } ,
2016-04-10 19:30:24 +00:00
regulValve . StableTime ) ;
2016-04-20 10:43:59 +00:00
log . InfoFormat ( "SetFlowOp: ValveMove({0}, Frequency, {1}, {2}, {3})" ,
regulValveNr , freqLo , freqHi , regulValve . StableTime ) ;
opState = OpState . ValveMoveToFlow3 ;
2019-10-15 08:03:28 +00:00
return Event . Starting ;
2016-04-10 19:30:24 +00:00
}
2015-10-15 17:49:42 +00:00
else
{
opState = OpState . SettingFlow ;
2019-10-15 08:03:28 +00:00
return Event . Busy ;
2015-10-15 17:49:42 +00:00
}
}
2014-07-28 07:54:35 +00:00
else if ( opState = = OpState . SettingFlow )
{
2019-09-11 13:44:05 +00:00
/// Regulation valve is setting flow to the required value
if ( ( currentReqFlowLo < = flow ) & & ( flow < = currentReqFlowHi ) )
2014-07-28 07:54:35 +00:00
{
2019-09-11 13:44:05 +00:00
/// Flow is within range
if ( flowWithinBoundsTime + + > = regulValve . FlowStableSec )
2015-10-15 17:49:42 +00:00
{
2019-09-11 13:44:05 +00:00
/// Flow is within range for sufficiently long time
if ( regulValve . RegulValveCfg . StoredPositionReuse )
2015-10-15 17:49:42 +00:00
{
float storedPosition ;
2018-11-08 08:54:40 +00:00
if ( regulValve . Dict . TryGetValue ( reqFlowAve , out storedPosition ) )
2015-10-15 17:49:42 +00:00
{
/// update the stored valve position
2019-09-11 13:44:05 +00:00
StoreTargetPosition ( reqFlowAve , ( rvPosition + storedPosition ) / 2.0f ) ;
2015-10-15 17:49:42 +00:00
log . InfoFormat ( "Run(): rv#={0} reqFlow={1} pos={2}% stored={3} <--- Updating a stored position" ,
2018-11-08 08:54:40 +00:00
regulValveNr , reqFlowAve , rvPosition . ToString ( "F1" ) , storedPosition ) ;
2015-10-15 17:49:42 +00:00
}
else
{
/// store the valve position
2018-11-08 08:54:40 +00:00
StoreTargetPosition ( reqFlowAve , rvPosition ) ;
2015-10-15 17:49:42 +00:00
log . InfoFormat ( "Run(): rv#={0} reqFlow={1} pos={2}% <--- Storing a new position" ,
2018-11-08 08:54:40 +00:00
regulValveNr , reqFlowAve , rvPosition . ToString ( "F1" ) ) ;
2015-10-15 17:49:42 +00:00
}
}
2016-02-07 21:27:32 +00:00
2017-10-19 08:34:10 +00:00
log . InfoFormat ( "flow = {0} m3/h (lo={1}, hi={2}, REACHED)" , flow , currentReqFlowLo , currentReqFlowHi ) ;
2016-02-07 21:27:32 +00:00
2015-10-15 17:49:42 +00:00
opState = OpState . FlowReached ;
return Event . FlowReached ;
}
else
{
2017-10-19 08:34:10 +00:00
log . InfoFormat ( "flow = {0} m3/h (lo={1}, hi={2}, FLOW_OK_TIMER={3}s)" , flow , currentReqFlowLo , currentReqFlowHi , flowWithinBoundsTime ) ;
2019-10-15 08:03:28 +00:00
return Event . Busy ;
2015-10-15 17:49:42 +00:00
}
2014-07-28 07:54:35 +00:00
}
2014-08-01 06:36:17 +00:00
else if ( StateMachine . Time > expireTime )
2014-07-28 07:54:35 +00:00
{
return Event . RegulValveTimeOut ;
}
else
{
2017-10-19 08:34:10 +00:00
log . InfoFormat ( "flow = {0} m3/h (lo={1}, hi={2})" , flow , currentReqFlowLo , currentReqFlowHi ) ;
2015-10-15 17:49:42 +00:00
flowWithinBoundsTime = 0 ;
2019-10-15 08:03:28 +00:00
return Event . Busy ;
2014-07-28 07:54:35 +00:00
}
}
else /// opState == OpState.FlowReached
{
2017-10-19 08:34:10 +00:00
log . InfoFormat ( "flow = {0} m3/h (lo={1}, hi={2}, REACHED)" , flow , currentReqFlowLo , currentReqFlowHi ) ;
2014-07-28 07:54:35 +00:00
return Event . FlowReached ;
}
}
/// <summary>Start this operation</summary>
public void Stop ( )
{
2019-10-24 10:57:37 +00:00
if ( ! leaveMeasurementRunning )
{
/// Stop measurement
controlBoard . SendCommand ( Command . Stop , flowMeter . Idx1 , 50000 , 50000 , 0 , StopDevs . RegValveRegulation ) ; /// TestMethods=0
}
opState = OpState . Idle ;
2014-07-28 07:54:35 +00:00
}
}
}