C Controller - TCP Communication 

Page / - last modified by csp on 2014/08/19 13:20 

TCP Communication 
last modified by csp
on 2014/08/19 13:20

Table of Contents
Messages sent by Controller
Log messages
Error messages
Status messages
Messages received by controller
Valid commands

•	Messages sent by Controller
•	Log messages
•	Error messages
•	Status messages
•	Messages received by controller
•	Valid commands
Both the controller and the client GUI send their data as JSON Objects.
In JSON every Object is an unordered set of key/value pairs. The values of these pairs can be a JSON Object, a JSON Array or a primitive value (integer, string, boolean or null).
For more information or a complete grammar see http://www.json.org/.
Messages sent by Controller
•	Every message must provide the keys msgType and data.
•	Valid values for msgType are log, error or status.
Log messages
For log messages the data segment consists of a JSON Array, containing zero or more strings.
Error messages
For error messages the data segment consists of one single string, describing the error.
Status messages
In status messages the data segment is a JSON Object which must contain the following key/value pairs.
•	debug - true if the controller is set to verbose debug output, false otherwise
•	cleanup - true after cleanup mode has been engaged, false before that point
•	pause - true while the controller is paused, false otherwise
•	trainCount - an integer with the number of active trains on the railway
•	trains - a JSON Array with the data of all trains, each entry is a JSON Object with these values* state - integer value for current train state (0 = travelling, 1 = waiting, 2 = travelling until next station, 3 = forced wait, 4 = no schedule)
•	waitTime - (integer) remaining milliseconds until departure (0 if not waiting) 
•	spdSlow - the integer PWM value of this train while driving slow
•	spdCaution - the integer PWM value of this train while driving caution
•	spdNormal - the integer PWM value of this train while driving normally
•	stationTimes - a flat array of the form [station1_minTime, station1_maxTime, station2_minTime, station2_maxTime, ...]. The order of the stations is defined by the order in the table below but the KH (reverse) tracks are omitted.
•	currentIndex - an integer describing the current position in the schedule
•	schedule - a JSON Array of integer values, representing the station tracks, encoded in the following fashion
•	currentDestination - the current destination track, encoded in the following fashion 
Number	Track	Number	Track	Number	Track	Number	Track	
9	KH_ST_1	17	KH_ST_1 (reverse)	25	IC_ST_1	33	OC_ST_1	
10	KH_ST_2	18	KH_ST_2 (reverse)	26	IC_ST_2	34	OC_ST_2	
11	KH_ST_3	19	KH_ST_3 (reverse)	27	IC_ST_3	35	OC_ST_3	
12	KH_ST_4	20	KH_ST_4 (reverse)	 	 	 	 	
13	KH_ST_5	21	KH_ST_5 (reverse)	 	 	 	 	
•	locks - a JSON Array containing the current value of the track permissions, ordered by the default encoding (Hoermann API)
•	tickTime - Time consumption of tick function in milliseconds. 3 element double-array [min,avg,max]. 
•	overallTime -  Time consumption of overall controller loop in milliseconds. 3 element double-array [min,avg,max]. 
 
Sample status
{"msgType":"status",
"data":{
 "debug":false,
 "cleanup":false,
 "pause":false,
 "trainCount":8,
 "trains":[
    {"state":3,"waitTime":0,"spdSlow":40,"spdCaution":60,"spdNormal":100,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":1,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":9},
    {"state":1,"waitTime":2500,"spdSlow":41,"spdCaution":61,"spdNormal":101,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":2,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},
    {"state":0,"waitTime":0,"spdSlow":42,"spdCaution":62,"spdNormal":102,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":3,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},
    {"state":0,"waitTime":0,"spdSlow":43,"spdCaution":63,"spdNormal":103,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":4,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},
    {"state":0,"waitTime":0,"spdSlow":44,"spdCaution":64,"spdNormal":104,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":5,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":27},
    {"state":1,"waitTime":10000,"spdSlow":45,"spdCaution":65,"spdNormal":105,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":6,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":9},
    {"state":2,"waitTime":0,"spdSlow":46,"spdCaution":66,"spdNormal":106,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":7,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":10},
    {"state":0,"waitTime":0,"spdSlow":47,"spdCaution":67,"spdNormal":107,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":4,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":21}
  ],
 "locks":[-1,-1,-1,-1,-1,0,-1,-1,2,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,-1,-1,-1,-1,5,-1,-1,-1,3,3,7,7,-1,-1,-1,-1,-1,-1,6,-1,-1],
 "tickTime":[50,60,70],
 "overallTime":[70,80,90]}}
[One line to test via netcat]
{"msgType":"status","data":{"debug":false,"cleanup":false,"pause":false,"trainCount":8,"trains":[{"state":3,"waitTime":0,"spdSlow":40,"spdCaution":60,"spdNormal":100,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":1,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":9},{"state":1,"waitTime":2500,"spdSlow":41,"spdCaution":61,"spdNormal":101,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":2,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},{"state":0,"waitTime":0,"spdSlow":42,"spdCaution":62,"spdNormal":102,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":3,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},{"state":0,"waitTime":0,"spdSlow":43,"spdCaution":63,"spdNormal":103,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":4,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":13},{"state":0,"waitTime":0,"spdSlow":44,"spdCaution":64,"spdNormal":104,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":5,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":27},{"state":1,"waitTime":10000,"spdSlow":45,"spdCaution":65,"spdNormal":105,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":6,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":9},{"state":2,"waitTime":0,"spdSlow":46,"spdCaution":66,"spdNormal":106,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":7,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":10},{"state":0,"waitTime":0,"spdSlow":47,"spdCaution":67,"spdNormal":107,"stationTimes":[5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10,5,10],"currentIndex":4,"schedule":[9,13,17,21,25,27,33,35],"currentDestination":21}],"locks":[-1,-1,-1,-1,-1,0,-1,-1,2,-1,-1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,-1,-1,-1,-1,5,-1,-1,-1,3,3,7,7,-1,-1,-1,-1,-1,-1,6,-1,-1],"tickTime":[50,60,70],"overallTime":[70,80,90]}}
Messages received by controller
•	Every message must contain the keys command and data.
•	Valid values for command are the following uppercase commands.
•	Depending on the command, the data-object contains the described infos.
•	If a command doesn't require any additional information, the data-object may be omitted or empty.
Valid commands
•	STATUS - Poll for the current status, should trigger a status reply message
•	SHUTDOWN - Causing the controller to end the program and close all sockets
•	LOGOUT - Closing the connection, leaving the controller running
•	PAUSE - suspend or resume the controller, must supply additional payload
•	state - boolean value, true if controller should be suspended
•	CLEANUP - Start the cleanup procedure, sending the trains back to their home tacks
•	DEBUG - enable or disable verbose output, must supply additional payload
•	state - boolean value, true if verbose output should be activated
•	ECHO - Prints message as log message on the server (will be broadcasted to all clients)
•	message - string, message text
•	LIGHT - activate or deactivate the lights on the railway, must supply additional payload
•	state - boolean value, true if lights should be lit
•	WAIT - Force a train to wait in the next station, must supply additional payload
•	train - integer train identification
•	START - Force a waiting train to immediately abort the waiting timer, must supply additional payload
•	train - integer train identification
•	SCHEDULE - Set the new schedule for a train, needs additional payload
•	train - integer train identification
•	currentIndex - current integer position in the schedule array
•	tracks - a JSON Array of integer values, representing the station tracks in encoding above
•	SPEED - Change the speed settings of a train, needs additional payload
•	train - integer train identification
•	speeds - JSON Array with three integer values spdSlow, spdCaution, spdNormal
•	TIME - Change the waiting times for a train
•	train - integer train identification
times - a flat array of the form [station1_minTime, station1_maxTime, station2_minTime, station2_maxTime, ...]. The order of the stations is defined by the order in the table above but the KH (reverse) tracks are omitted.