--[[
---------------------------------------------------------------------------
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 = "Event Test"
require([[defines]]);
require([[functions]]);

EventCnt = 1;	-- decremented when an event is received
ErrorCnt = 0;

-- Run this script in two PC Windows, first one in PCNo 5 and the second in PCNo 6
--

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print("setup() Running");
	print("This is Program Control Window #" .. PCNo());
	print("This script requires 'event.lua' be running in PC window #1 and #2");
	if (PCNo() < 5 or PCNo() > 6) then
		return Stop("Error - need two PC windows #5 and #6");	-- need two PC windows 5 and 6
		end
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	-- Always call Sleep(X) at least once in this function if it returns true;
	print("loop() Running");
	if (PCNo() == 5) then
		DestPC = 6;
		end;
	if (PCNo() == 6) then
		DestPC = 5;
		end
	print("Sending an event to PC Window #" .. DestPC .. " in 10 seconds");
	Sleep(10);
	res = SetEvent(DestPC, 123456789012345, PCNo(), -876765, 556677);
	if (type(res) ==  "nil") then
		print("SetEvent() returned nil : invalid destination Program Control number - script not running");
		return Stop("DONE");
		end;
	if (type(res) == "boolean") then
		print("SetEvent() returned ", res, " : success");
		end;
	Sleep(20);
	print("Almost Done...");
	return Stop("DONE");
	end
--[[---------------------------------------------------------------------------------------]]
function event(FromPC, Type, P1, P2, P3)
	print("event() - From PC# ", FromPC, " EventType = ", Type, " Parameters = ", P1, " ", P2, " ", P3);
	if (FromPC ~= DestPC) then
		ErrorCnt = ErrorCnt + 1;
		end;
	if (Type ~= 123456789012345) then
		ErrorCnt = ErrorCnt + 1;
		print("Event Type Error");
		end;
	if (P1 ~= DestPC) then
		ErrorCnt = ErrorCnt + 1;
		print("P1 Error");
		end;
	if (P2 ~= -876765) then
		ErrorCnt = ErrorCnt + 1;
		print("P2 Error");
		end;
	if (P3 ~= 556677) then
		ErrorCnt = ErrorCnt + 1;
		print("P3 Error");
		end;
	EventCnt = EventCnt - 1;
	return true;		-- false=event failed, true=event succeeded	
	end

--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print("cleanup() Running");
	if (EventCnt == 1) then
		print("Error: Event not received");
		else
		if (ErrorCnt == 0) then
			print("No Errors");
			else
			print(ErrorCnt, " Error(s)");
			end;
		end;
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
