This project contains the central server in charge of controlling our trains.
You must have a Bluetooth antenna.
Simply use the Golang CLI: go run ./cmd
There is a few environment variables that you can set to custom the behaviour:
TRAINBERRY_LOG_LEVEL(Default:INFO): Set log levelTRAINBERRY_SANITY_RUN_INTERVAL(Default:5s): Time between two executions of sanity runTRAINBERRY_BLE_DETECTION_RUN_INTERVAL(Default: `2s): Time between two network scan to find new trains
There is only one REST endpoint on this server:
GET /state will return the current state of the server and associated chips. The format is the following:
[
{
"name": "Trainberry::BB22001",
"error_count": 0,
"speed": 0,
"light": true
}
]99% of the trains are controlled through a WebSocket. I made this decision because communicating via WS is way much
quicker (and latency matters a lot!). Websocket is on /ws endpoint.
The following information are sent by the server to the client(s):
added_device: A new device have been added to the serverdeleted_device: A device have been removed after multiple healthcheck failurefail_counter_increment: A device missed a healthcheckfail_counter_reset: A device which was failing is back onlinelight_update: The light setting (on/off) have been updated on this devicespeed_update: The speed setting have been updated on this device
This information are communicated through WS in a JSON format. The format is as it follows:
{
"operation":"deleted_device",
"device": {
"name":"Trainberry::BB22001",
"error_count":0,
"speed":0,
"light":false
}
}The server will always send the full object, except on deletion event, where only the name is sent.
The clients can contact the server to make updates on devices. The operations are:
set_light: set light status for the desired device.- Payload is:
{"operation":"set_light","device":{"name":"Trainberry::BB22001","light":true}}
- Payload is:
set_speed: set speed for the desired device.- Payload is:
{"operation":"set_speed","device":{"name":"Trainberry::BB22001","speed":80}}
- Payload is:
emerg_stop: stop all trains simultaneously.- Payload is:
{"operation":"emerg_stop"}
- Payload is:
This project is licensed under GNU Affero General Public License v3.0.