--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "Engine - P&LERR"

require("defines");
require("functions");

local MyEngineNo, MyTIUNo;

-- Counter Labels
Counter04Label = "State";

Tags = {};		-- table to hold all of the tags detected in sequence
-- state machine states
IDLE = 0;
SM = IDLE;	
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, MyTIUNo)
	-- this function is called once when the [Run Script] button is pushed
	-- Engine : Engine number selected on the RTC Main window
	-- TIU : TIU number selected on the RTC Main window
	print("setup()");
	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	-- make sure that RFID is connected
	TIUSerialPortConnected, TIUConnected, RFIDSerialPortConnected = CommStatus();	
	--print("TIUSerialPortConnected : " .. tostring(TIUSerialPortConnected) .. " - TIUConnected : " .. tostring(TIUConnected) .. " - RFIDSerialPortConnected : " .. tostring(RFIDSerialPortConnected));
	if (not RFIDSerialPortConnected) then
		print("RFID Port must be enabled for this script");
		return false;	-- false=setup failed
		end
	print("TIU : " .. MyTIUNo);
	assert((MyTIUNo >= 1 and MyTIUNo <= 5),"TIU Number out of range");
	
	MyEngineNo = GetGlobalInt(PCNo());	-- Engine to run stored in PCNo location in Global Integers
	if (MyEngineNo < 1 or MyEngineNo > 99) then
		print("MyEngineNo Number out of range (did you run dispatcher.lua?) : " .. MyEngineNo);
		return Stop("Script Terminated");
		end
		
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	
	print("Engine Number = " .. MyEngineNo);
	-- initialize the layout
	print(string.format("Startup Engine : %d", MyEngineNo));
	EngineStartUp(MyEngineNo, MyTIUNo);
	Sleep(25);			

	if (IsEngineRunning(MyEngineNo)) then	-- IsEngineRunning() REQUIRES use of StartUpActiveEngines() or EngineStartUp()
		print(string.format("Initalizing Engine %d", MyEngineNo));
		-- Set smoke off
		Smoke(OFF, 0, MyEngineNo, MyTIUNo);
		-- Cab Chatter Off
		CabChat(OFF, 0, MyEngineNo, MyTIUNo);
		-- print odometer information
		LastDTO = DTO(MyEngineNo, MyTIUNo);
		LastDOD = DOD(MyEngineNo, MyTIUNo);
		LastRunTime = RunTime();
		print(string.format(" Run Time = %.2f seconds", LastRunTime)); 
		print(string.format(" Trip Odometer = %.6f Smiles", LastDTO)); 
		print(string.format(" Odometer = %.6f Smiles", LastDOD));
		else
		print(string.format("Startup Failed for Engine : %d", MyEngineNo));
		return false;	-- false=setup failed, true=setup succeeded
		end
		
	print(string.format("setup(): Debug level %d", Debug())); 
	print("setup(): Engine Number " .. MyEngineNo .. " TIU Number " .. MyTIUNo);
	--
	--iSpeed = 20;
	--print(string.format("SetSpeed %d Smph for Engine : %d", iSpeed, MyEngineNo));
	--SetSpeed(iSpeed, 0, MyEngineNo, MyTIUNo);
	--Sleep(20);
	SM = IDLE;	
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	print("loop()");
	-- check signal and process it
	
	-- if no more signals to process, wait a second and try again
	Sleep(1);
	
	-- if signal is green, start moving or continue moving
	
--[[---------------------------------------------------------------------------------------]]
-- STATE MACHINE
--[[---------------------------------------------------------------------------------------]]	
	SetCounter(COUNTER04, SM);
--[[---------------------------------------------------------------------------------------]]	
	if (SM == IDLE) then	-- do nothing 
		-- continue in this state
		return true;
		end
--[[---------------------------------------------------------------------------------------]]		
	if (SM == IDLE) then

		-- continue in this state
		return true;
		end
	
	
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup()
	-- this function is called once when the user presses the [STOP] button
	print("cleanup()");

	print("All Newly Detected Tags:");
	for k,v in  pairs(Tags) do		-- dump out the Tags table
		print(k .. "  " .. v);
		end
	if (MyEngineNo > 0) then
		print("Shutdown Engine " .. MyEngineNo);
		EngineShutdown(MyEngineNo, MyTIUNo);
		end
	
	print(string.format("Script Finished at %.2f", RunTime()));
	--
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function tick()
	-- this is called once per second while waiting for signals to change
	-- Do not call Sleep() from this function
	if (Debug() > 4) then print(string.format("RunTime = %.2f seconds", RunTime())); end;
	return true;
	end
--[[---------------------------------------------------------------------------------------]]
