PROGRAM Control
(* CURRENT TIME IN SECONDS... this doesn't really correlate to anything other than a second counter *)
(* time_CurrentTime := ANY_TO_DINT( __SYSVA_CYCLEDATE) / 1000;*)
CurrentTimeRTC(TRUE);
time_CurrentTime := ANY_TO_DINT(CurrentTimeRTC.Epoch);

(* Test mode does not rely on feedback from devices *)
IF Test_Mode THEN
	Contactor_Status := cmd_Run;
	VoltageOK := TRUE;
END_IF;

(* First Scan Variable Setting*)
IF (__SYSVA_FIRST_SCAN) THEN
	Shutdown_Time := time_CurrentTime;
	Device_Status:= 'Startup';
	Device_Status_INT := 1;
	Restart_Command := FALSE;
	Restart_Allowed := FALSE;
	Run_Permissive := FALSE;
	Start_Permissive := FALSE;
	cfg_READ := TRUE;
	sts_TimerRunTimeSet := FALSE;
	sts_TimerWaitTimeSet := FALSE;
	sts_TimerCycleActive := FALSE;
	sts_TripCountIncreased := FALSE;
	val_OverloadTripCount := 0;
	val_TripCount := 0;
	
	(* Start up automatically if powering up in auto mode *)
	RTrig_AutoMode(Auto_Mode);
	IF RTrig_AutoMode.Q THEN
		Restart_Allowed := TRUE;
		Shutdown_Time := time_CurrentTime;
	END_IF;
END_IF;

(* Disable downhole shutdowns if no downhole tool *)
IF NOT Downhole_Tool_Enabled THEN 
	Pressure_Shutdown_Enabled := FALSE;
	Pressure_Startup_Enabled := FALSE;
	Temp_Shutdown_Enabled := FALSE;
	Temp_Startup_Enabled := FALSE;
END_IF;

(* Stop the pump if run permissive goes false while running *)
IF (NOT Run_Permissive AND cmd_Run) THEN
	Device_Status := 'Lost Run Permissive';
	Device_Status_INT := 4;
	Shutdown_Time := time_CurrentTime;
	
	IF NOT sts_TripCountIncreased THEN
		IF OverloadTrip THEN
			val_OverloadTripCount := val_OverloadTripCount + 1;
		ELSE
			val_TripCount := val_TripCount + 1;
		END_IF;
		sts_TripCountIncreased := TRUE;
	END_IF;
END_IF;

IF val_OverloadTripCount > cfg_OverloadTripCountLimit THEN
	Restart_Allowed := FALSE;
	Device_Status := 'Not able to restart - Overload Limit';
	Device_Status_INT := 5;
	sts_TripLimitOLReached := TRUE;
ELSE
	sts_TripLimitOLReached := FALSE;
END_IF;

IF val_TripCount > cfg_TripCountLimit THEN
	Restart_Allowed := FALSE;
	Device_Status := 'Not able to restart - Trip Limit';
	Device_Status_INT := 6;
	sts_TripLimitReached := TRUE;
ELSE
	sts_TripLimitReached := FALSE;
END_IF;

(* If the pump hasn't been started by the user, don't allow it to auto-restart *)
IF (Start_Command OR Start_Button) AND Start_Permissive THEN
	Restart_Allowed := TRUE;
	
	IF cfg_TimerModeEnabled THEN
		sts_TimerCycleActive := TRUE;
		cmd_TimerRun := TRUE;
	END_IF;
END_IF;


(* If run permissive goes false, do not allow a restart for a specified amount of time *)
IF (Auto_Mode AND 
	(NOT OverloadTrip) AND 
	(NOT cmd_Run) AND 
	((sts_TimerCycleActive AND cmd_TimerRun) OR (NOT sts_TimerCycleActive)) AND 
	Restart_Allowed) THEN
	
	IF (time_CurrentTime - Shutdown_Time) > Downtime_Time_Parameter THEN 
		IF (NOT Restart_Command) THEN
			TripResetCmd := TRUE;
			Restart_Command := TRUE;
			IF cfg_TimerModeEnabled THEN
				cmd_TimerRun := TRUE;
				sts_TimerCycleActive := TRUE;
			END_IF;
		END_IF;
	ELSE
		IF cfg_TimerModeEnabled THEN
			cmd_TimerRun := FALSE;
		END_IF;
		sts_TimerCycleActive := FALSE;
		Restart_Command := FALSE;
		Time_Until_Startup := Downtime_Time_Parameter - (time_CurrentTime - Shutdown_Time);
		Time_Until_Startup_Min := Time_Until_Startup / 60;
		Time_Until_Startup_Sec := Time_Until_Startup - (Time_Until_Startup_Min * 60);
		Time_Until_Startup_String := ANY_TO_STRING(Time_Until_Startup_Min) + ':' + RIGHT('0' + ANY_TO_STRING(Time_Until_Startup_Sec),2);
		IF Auto_Mode THEN
			Device_Status := 'Waiting to attempt restart: ' + Time_Until_Startup_String;
			Device_Status_INT := 7;
		END_IF;
		
	END_IF;
ELSIF (Auto_Mode AND 
	OverloadTrip AND 
	(NOT cmd_Run) AND 
	Restart_Allowed) THEN
	
	IF (time_CurrentTime - Shutdown_Time) > Downtime_Time_Parameter_OL THEN 
		IF (NOT Restart_Command) THEN
			Restart_Command := True;
			IF cfg_TimerModeEnabled THEN
				cmd_TimerRun := TRUE;
				sts_TimerCycleActive := TRUE;
			END_IF;
		END_IF;
	ELSE
		IF cfg_TimerModeEnabled THEN
			cmd_TimerRun := FALSE;
		END_IF;
		sts_TimerCycleActive := FALSE;
		Restart_Command := FALSE;
		Time_Until_Startup := Downtime_Time_Parameter_OL - (time_CurrentTime - Shutdown_Time);
		Time_Until_Startup_Min := Time_Until_Startup / 60;
		Time_Until_Startup_Sec := Time_Until_Startup - (Time_Until_Startup_Min * 60);
		Time_Until_Startup_String := ANY_TO_STRING(Time_Until_Startup_Min) + ':' + RIGHT('0' + ANY_TO_STRING(Time_Until_Startup_Sec),2);
		IF Auto_Mode THEN
			Device_Status := 'Waiting to restart (OL Timer):' + Time_Until_Startup_String;
			Device_Status_INT := 8;
		END_IF;
	END_IF;
END_IF;


(*  Run only if commanded to start and start permissive is met or already running and not being told to stop *)
IF ((((Start_Button OR Start_Command OR cmd_TimerRun OR (Restart_Allowed AND Restart_Command)) AND Start_Permissive) OR cmd_Run) AND 
	(NOT Stop_Command) AND (cmd_TimerRun OR NOT cfg_TimerModeEnabled) AND Run_Permissive) THEN
	cmd_Run := TRUE;
	Restart_Command:= FALSE;
	IF NOT Start_Time_Set THEN
		Start_Time := time_CurrentTime;
		Start_Time_Set := TRUE;
		Device_Status := 'Running';
		Device_Status_INT := 9;
		sts_TripCountIncreased := FALSE;
	END_IF;
		
	IF Contactor_Status THEN
		Run_Time := time_CurrentTime - Start_Time;
	END_IF;
ELSE
	cmd_Run := FALSE;
END_IF;

UNSTICK_STARTCOMMAND(Start_Command, StartCommandTime);
IF UNSTICK_STARTCOMMAND.Q  THEN
	Start_Command := FALSE;
END_IF;


IF cmd_Run AND NOT Contactor_Status THEN
	IF NOT E300_Output_Enable THEN
		IF E300_Output_Toggled THEN
			E300_Output_Enable := TRUE;
		END_IF;
		E300_Output_Toggled := TRUE;
	ELSE
		IF NOT E300_Output_Toggled THEN
			E300_Output_Enable := FALSE;
		END_IF;
	END_IF;
END_IF;

IF Contactor_Status AND (Run_Time > cfg_ClearTripCountAfter) THEN
	val_OverloadTripCount := 0;
	val_TripCount := 0;
END_IF;


IF NOT Auto_Mode THEN
	Restart_Command := FALSE;
	Restart_Allowed := FALSE;
END_IF;


IF Stop_Command THEN
	Restart_Allowed := FALSE;
	Device_Status := 'User Stopped';
	Device_Status_INT := 10;
	sts_TimerCycleActive := FALSE;
	val_OverloadTripCount := 0;
	val_TripCount := 0;
	cmd_TimerRun := FALSE;
	sts_TimerRunTimeSet := FALSE;
	IF NOT Contactor_Status THEN
		Stop_Command := FALSE;
	END_IF;
END_IF;

IF (NOT Hand_Mode) AND (NOT Auto_Mode) THEN
	val_OverloadTripCount := 0;
	val_TripCount := 0;
	cmd_TimerRun := FALSE;
	sts_TimerCycleActive := FALSE;
	sts_TimerRunTimeSet := FALSE;
	(*Restart_Allowed := FALSE;*)
END_IF;

IF NOT Contactor_Status THEN
	Start_Time_Set := FALSE;
END_IF;

(* TIMER MODE *)
IF cfg_TimerModeEnabled AND sts_TimerCycleActive THEN
	IF cmd_TimerRun THEN
		IF NOT sts_TimerRunTimeSet THEN
			sts_TimerRunTimeSet := TRUE;
			time_TimerRunTime := time_CurrentTime;
		END_IF;
	
		timer_RunTimeLeft := (cfg_TimerRunTime * 60) - (time_CurrentTime - time_TimerRunTime);
		IF timer_RunTimeLeft <= 0 THEN
			cmd_TimerRun := FALSE;
			sts_TimerRunTimeSet := FALSE;
		END_IF;
		time_RunTimeLeftMin := timer_RunTimeLeft / 60;
		time_RunTimeLeftSec := timer_RunTimeLeft - (time_RunTimeLeftMin * 60);
		
	ELSE
		IF NOT sts_TimerWaitTimeSet THEN
			sts_TimerWaitTimeSet := TRUE;
			time_TimerWaitTime := time_CurrentTime;
			Device_Status := 'Waiting to start (Timer Mode)';
			Device_Status_INT := 11;
		END_IF;
	
		timer_WaitTimeLeft := (cfg_TimerWaitTime * 60) - (time_CurrentTime - time_TimerWaitTime);
		IF timer_WaitTimeLeft <= 0 THEN
			cmd_TimerRun := TRUE;
			sts_TimerWaitTimeSet := FALSE;
		END_IF;
		time_WaitTimeLeftMin := timer_WaitTimeLeft / 60;
		time_WaitTimeLeftSec := timer_WaitTimeLeft - (time_WaitTimeLeftMin * 60);
	END_IF;
END_IF;

IF NOT cfg_TimerModeEnabled OR Stop_Command THEN
	cmd_TimerRun := FALSE;
	sts_TimerRunTimeSet := FALSE;
	sts_TimerWaitTimeSet := FALSE;
END_IF;


IF TripStatus OR OverloadTrip THEN
	IF MOD(time_CurrentTime, cfg_ResetCycle) = 0 THEN
		TripResetCmd := TRUE;
	END_IF;
END_IF;


END_PROGRAM