// This function simulates inputs for the flow runs. 
// Thesee values would normally come from IO.

// https://content.helpme-codesys.com/en/LibDevSummary/date_time.html
// get us tick timer. This is noiser than ns timer, because 
// ns is clipped to 100 ns
SysTimeGetUs(Us);
// convert from a ULINT to an int, which preserves the least significant 16 bits. These bits are
// noisy and should always be different on every callback. Convert that number to a real
// then divide by 65535 to get a REAL between -1.0 and 1.0 
Noise := INT_TO_REAL(ULINT_TO_INT(Us)) / 32767.0;
// these two lines gets the time into a structure so that we can extract the components
ErrorCode := SysTimeRtcHighResGet(stUTC_Timestamp); 
ErrorCode := SysTimeRtcConvertHighResToLocal(stUTC_Timestamp, stdNow);
// calculate the seconds today local time 
SecondsToday := (UINT_TO_REAL(stdNow.wHour) * 3600.0) + (UINT_TO_REAL(stdNow.wMinute) * 60.0) + UINT_TO_REAL(stdNow.wSecond);


// use the noise and seconds today to simulate some values
// using the seconds today allows us to ramp values gently
// and using the noise allows us to move the values a bit

// gas
//GasDp := (35.0 + ((SecondsToday / 86400.0) * 20.0) + Noise);
GasDp := (44.0 + ((SecondsToday / 86400.0) * 20.0) + Noise);
GasSp := (1198.0 + ((SecondsToday / 86400.0) * 100.0) + (Noise * 10.0));
GasTemp := (121.0 + ((SecondsToday / 86400.0) * 20.0) + (Noise * 2.0));

// liquid
//LiquidVolumeFlowRate := (440.0 + ((SecondsToday / 86400.0) * 40.0) +  (Noise * 3.0)); // m3/day, at 480 m3/day we get 1 m3 every 3 minutes
LiquidVolumeFlowRate := (440.0 + ((SecondsToday / 86400.0) * 40.0) +  (Noise * 3.0)); // m3/day, at 480 m3/day we get 1 m3 every 3 minutes
LiquidSp := (532.0 + ((SecondsToday / 86400.0) * 50.0) + (Noise * 5.0));
LiquidTemp := (65.0 + ((SecondsToday / 86400.0) * 20.0) + (Noise * 2.0));
LiquidDensity := (650.0 + ((SecondsToday / 86400.0) * 50.0) + (Noise * 2.0));

// water
WaterVolumeFlowRate := (390.0 + ((SecondsToday / 86400.0) * 60.0) + (Noise * 3.0)); // m3/day, 
//WaterSp := (892.0 + ((SecondsToday / 86400.0) * 50.0) + (Noise * 5.0));
WaterSp := (892.0 + ((SecondsToday / 86400.0) * 50.0) + (Noise * 5.0));
WaterTemp := (55.0 + ((SecondsToday / 86400.0) * 20.0) + (Noise * 2.0));

// copy the simulated values to input tags to the apps
// gas
GVL.GF01.Inputs_GF01_dp := GasDp;
GVL.GF01.Inputs_GF01_sp := GasSp;
GVL.GF01.Inputs_GF01_temp := GasTemp;
// liquid
GVL.LF01.Inputs_LF01_volumeFlow := LiquidVolumeFlowRate;
GVL.LF01.Inputs_LF01_staticPressure := LiquidSp;
GVL.LF01.Inputs_LF01_plcTemp := LiquidTemp;
GVL.LF01.Inputs_LF01_plcDensity := LiquidDensity;
// water
GVL.WT01.Inputs_WT01_volumeFlow := WaterVolumeFlowRate;
GVL.WT01.Inputs_WT01_staticPressure := WaterSp;
GVL.WT01.Inputs_WT01_plcTemp := WaterTemp;