Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- this is the startup script for the central cpu at spawn
- ----| config |----------------------------
- local MODEM_SIDE = "top"
- ----| setup |----------------------------
- os.loadAPI("TrainSignalSystem")
- local actionEnum = {
- GET_SIGNAL_STATE = "GET_SIGNAL_STATE",
- SENSOR_CHANGE = "SENSOR_CHANGE",
- }
- ----| functions |----------------------------
- function onGetSignalState(id)
- local trainSignalSystem = TrainSignalSystem.findFromComputerId(id)
- if trainSignalSystem then
- local isOn = trainSignalSystem.isOn
- rednet.send(trainSignalSystem.signalId, isOn)
- end
- end
- function onSensorChange(id)
- local trainSignalSystem = TrainSignalSystem.findFromComputerId(id)
- if trainSignalSystem then
- if id == trainSignalSystem.onSensorId and trainSignalSystem.isOn == false then -- activate the signal
- print("Activating signal...")
- trainSignalSystem.isOn = true
- rednet.send(trainSignalSystem.signalId, true) -- send a message to the computer that handles the signal light
- elseif id == trainSignalSystem.offSensorId and trainSignalSystem.isOn == true then -- deactivate the signal
- print("Deactivating signal...")
- trainSignalSystem.isOn = false
- rednet.send(trainSignalSystem.signalId, false) -- send a message to the computer that handles the signal light
- end
- end
- end
- function receiveSignal() -- waits for commands from central cpu
- while true do
- print("Waiting for a message...")
- local id, message = rednet.receive()
- print("Received message from computer " .. id)
- -- proceed based on the action given in the message
- if message == actionEnum.SENSOR_CHANGE then
- onSensorChange(id)
- elseif message == actionEnum.GET_SIGNAL_STATE then
- onGetSignalState(id)
- end
- end
- end
- ----| main |----------------------------
- rednet.open(MODEM_SIDE)
- print("Rednet opened.")
- receiveSignal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement