--[[
---------------------------------------------------------------------------
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 = "Dispatcher - P&LERR"
--
-- Dispatcher runs on PC1
--
require("defines");
require("functions");
-- Counter Labels
Counter01Label = "Engines Detected";
Counter02Label = "Cycles";
Counter03Label = "Tags Detected";
Counter04Label = "State";
Counter05Label = "OS Reports";

Tags = {};		-- table to hold all of the tags detected in sequence
TagCount = 0;
-- state machine states
IDLE = 0;
SM = IDLE;
-- Form A and Train Orders
FormAAliquippa = false;
FormACollege = false;
FormAStruthers = false;
FormAJL = false;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- 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()");
		
	print("TIU : " .. TIU);
	assert((TIU >= 1 and TIU <= 5),"TIU Number out of range");
	
	-- 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
	MyEngineNo1 = Engine;	-- Engine number selected on the RTC Main window
	
	SM = IDLE;
	SetCounter(COUNTER04, SM);
	print(string.format("RunTime = %.2f seconds", RunTime()));
	return true;		-- false=setup failed, true=setup succeeded
	end

--[[---------------------------------------------------------------------------------------]]
	
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
		
	print(string.format("cleanup() Complete at %.2f", RunTime()));
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
ALIQUIPPA = 1;
COLLEGE = 2;
STRUTHERS = 3;
J_L = 4;
--[[---------------------------------------------------------------------------------------]]
function FormAA(Eng, TIU)
	print("Form A Aliquippa Button()");
	Resp = InputQuery("Engine # approaching Aliquippa", "Enter Engine Number", MyEngineNo1);
	if (Resp == false or Resp == nil) then	-- user pressed [Cancel]
		return false;	-- false=setup failed, true=setup succeeded
		end;
	Enum = tonumber(Resp);
	if (Enum ~= nil) then	-- if the result is a number, then use it, otherwise, ignore it
		MyEngineNo1 = Enum;
		end
	print("Engine Number = " .. MyEngineNo1);
	--SetGlobalInt(3, MyEngineNo1);	
	FormAAliquippa = true;
	res = SetEvent(2,ALIQUIPPA, MyEngineNo1);
	if (res == nil) then
		print("Engine ", MyEngineNo1, " to Aliquippa - script not running");
		return false;
		end;
	return true;
end
Function01Name, Function01Label = FormAA, "Form A Aliquippa";
--[[---------------------------------------------------------------------------------------]]
function FormAC(Eng, TIU)
	print("Form A College Button()");
	Resp = InputQuery("Engine # approaching College", "Enter Engine Number", MyEngineNo1);
	if (Resp == false or Resp == nil) then	-- user pressed [Cancel]
		return false;	-- false=setup failed, true=setup succeeded
		end;
	Enum = tonumber(Resp);
	if (Enum ~= nil) then	-- if the result is a number, then use it, otherwise, ignore it
		MyEngineNo1 = Enum;
		end
	print("Engine Number = " .. MyEngineNo1);
	--SetGlobalInt(4, MyEngineNo1);	
	FormACollege = true;
	res = SetEvent(3,COLLEGE, MyEngineNo1);
	if (res == nil) then
		print("Engine ", MyEngineNo1, " to College - script not running");
		return false;
		end;
	return true;
end
Function02Name, Function02Label = FormAC, "Form A College";
--[[---------------------------------------------------------------------------------------]]
function FormAS(Eng, TIU)
	print("Train Order Struthers Button()");
	Resp = InputQuery("Engine # approaching Struthers", "Enter Engine Number", MyEngineNo1);
	if (Resp == false or Resp == nil) then	-- user pressed [Cancel]
		return false;	-- false=setup failed, true=setup succeeded
		end;
	Enum = tonumber(Resp);
	if (Enum ~= nil) then	-- if the result is a number, then use it, otherwise, ignore it
		MyEngineNo1 = Enum;
		end
	print("Engine Number = " .. MyEngineNo1);
	--SetGlobalInt(5, MyEngineNo1);	
	FormAStruthers = true;
	res = SetEvent(4,STRUTHERS, MyEngineNo1);
	if (res == nil) then
		print("Engine ", MyEngineNo1, " to Struthers - script not running");
		return false;
		end;	
	return true;
end
Function06Name, Function06Label = FormAS, "Form A Struthers";
--[[---------------------------------------------------------------------------------------]]
function FormAJL(Eng, TIU)
	print("Train Order J&L Button()");
	Resp = InputQuery("Engine # approaching J&L", "Enter Engine Number", MyEngineNo1);
	if (Resp == false or Resp == nil) then	-- user pressed [Cancel]
		return false;	-- false=setup failed, true=setup succeeded
		end;
	Enum = tonumber(Resp);
	if (Enum ~= nil) then	-- if the result is a number, then use it, otherwise, ignore it
		MyEngineNo1 = Enum;
		end
	print("Engine Number = " .. MyEngineNo1);
	--SetGlobalInt(6, MyEngineNo1);	
	FormAJL = true;
	res = SetEvent(5,J_L, MyEngineNo1);
	if (res == nil) then
		print("Engine ", MyEngineNo1, " to J&L - script not running");
		return false;
		end;	
	return true;
end
Function07Name, Function07Label = FormAJL, "Form A J&L";
--[[---------------------------------------------------------------------------------------]]