--[[
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------]]
-- Correct operation of this test depends on the processor speed. That is,
-- how fast it can load commands into the Dispatch List
--[[---------------------------------------------------------------------------------------]]

-- Semicolons are not required in Lua code
title = "Dispatch List Test"
require("defines");

local MyEngineNo, MyTIUNo, MyLoopCount;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine)
	print(os.date("setup() %d %b %Y   %X"));

	MyEngineNo = Engine;	-- Engine number selected on the RTC Main window
	MyTIUNo = GetTIUNo(MyEngineNo);	-- TIU associated with MyEngineNo
	MyLoopCount = 100;	-- Loop count then stop
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop()
	-- Always call Sleep() at least once in this function.
	print(os.date("loop() %d %b %Y   %X"));

	AC, IAC = Counts();
	print("TIU" .. MyTIUNo .. ": " .. AC .. " Active Engines - " .. IAC .. " Inactive Engines");
	DL(MyEngineNo, MyTIUNo);
	Sleep(0.5);		-- Sleep a short while to let the dispatch list load
	NumDL = CountDispatchList();
	print("Initial Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 6) then
		print("Error : Initial Dispatch List Count ~= 6");
		end;
	print(os.date("Before Sleep() %d %b %Y   %X"));
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 5) then
		print("Error : Dispatch List Count ~= 5");
		end;
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL.. " Command(s)");
	if (NumDL ~= 4) then
		print("Error : Dispatch List Count ~= 4");
		end;
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 3) then
		print("Error : Dispatch List Count ~= 3");
		end;	
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 2) then
		print("Error : Dispatch List Count ~= 2");
		end;
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
		if (NumDL ~= 1) then
		print("Error : Dispatch List Count ~= 1");
		end;
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 0) then
		print("Error : Dispatch List Count ~= 0");
		end;	
	Sleep(2);
	NumDL = CountDispatchList();
	print("Dispatch List contains " .. NumDL .. " Command(s)");
	if (NumDL ~= 0) then
		print("Error : Dispatch List Count ~= 0");
		end;	
	Sleep(2);
	print("loop() Sleep Returned");

	print(os.date("After Sleep() %d %b %Y   %X"));
	MyLoopCount = MyLoopCount -1;
	if (MyLoopCount > 0) then
		return true;	-- false=do not loop, true=continue to loop
		else
		return Stop("End of Test");
		end
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup()
	-- this function is called once when the user presses the [STOP] button
	print(os.date("cleanup()  %d %b %Y   %X"));
	-- hangs  ClearDispatchList();	-- empty the dispatch list (note - this may leave the whistle sounding
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function DL()

	-- schedule 6 whistle commands (on/off) to add to the Dispatch List
	for i = 1, 9, 4 do
		print("Dispatch Whistle on/off commands for Engine " .. MyEngineNo .. " TIU " .. MyTIUNo .. " when : " .. i .. " seconds");
		retv = Whistle(ON, i, MyEngineNo, MyTIUNo);
		
		if (retv == nil) then
			Beep();
			print("Whistle On returned nil");
			return Stop();
			end;
		
		retv = Whistle(OFF, i+2, MyEngineNo, MyTIUNo);
		
		if (retv == nil) then
			Beep();
			print("Whistle Off returned nil");
			return Stop();
			end;		
		end
	return true;
end
Function01Name, Function01Label = DL, "Fill DL";
--[[---------------------------------------------------------------------------------------]]
