Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local State = {ON=1, OFF=2, STARTING=3, STOPPING=4}
- local config = {
- bayDoorBack={color=colors.white, onTime=3, offTime=3},
- shutter={color=colors.orange, onTime=2, offTime=2}
- }
- local stateValues = {}
- local stateTimers = {}
- local modemSide = "right"
- local rsSide = "back"
- local hostname = "host"
- local protocol = "status"
- function initNetwork()
- rednet.open(modemSide)
- rednet.host(protocol, hostname)
- print("[NET] Running "..protocol.." as "..hostname)
- end
- function initState()
- for name, config in pairs(config) do
- stateValues[name] = stateFromRs(name)
- end
- end
- function stateToString(state)
- if state == State.ON then
- return "ON"
- elseif state == State.OFF then
- return "OFF"
- elseif state == State.STARTING then
- return "STARTING"
- elseif state == State.STOPPING then
- return "STOPPING"
- else
- return "UNKNOWN"
- end
- end
- function stateFromRs(stateName)
- color = config[stateName].color
- if rs.testBundledInput(rsSide, color) then
- return State.ON
- else
- return State.OFF
- end
- end
- function getStatePayload(stateName)
- return {state=stateName, value=stateValues[stateName]}
- end
- function broadcastChange(stateName)
- rednet.broadcast({head="event", payload=getStatePayload(stateName)}, protocol)
- print("[EVN] Changed "..stateName.." to "..stateToString(stateValues[stateName]))
- end
- function handleMessage(msg, sender)
- if msg.head == "get" then
- rednet.send(sender, {head="reply", payload=stateValues}, protocol)
- print("[NET] Received GET from sender:"..sender)
- elseif msg.head == "set" and config[msg.payload.state] then
- print("[NET] Received SET "..msg.payload.state.." TO "..stateToString(msg.payload.value).." from sender:"..sender)
- newValue = changeState(msg.payload.state, msg.payload.value)
- rednet.send(sender, {head="reply", payload={state=msg.payload.state, value=newValue}}, protocol)
- end
- end
- function setRsState(stateName, rsValue)
- color = config[stateName].color
- existing = rs.getBundledInput(rsSide)
- if rsValue then
- rs.setBundledOutput(rsSide, colors.combine(existing, color))
- else
- rs.setBundledOutput(rsSide, colors.subtract(existing, color))
- end
- end
- function setState(stateName, newValue)
- stateValues[stateName] = newValue
- if newValue == State.ON or newValue == State.STARTING then
- setRsState(stateName, true)
- elseif newValue == State.OFF or newValue == State.STOPPING then
- setRsState(stateName, false)
- end
- broadcastChange(stateName)
- end
- function changeState(stateName, newValue)
- oldValue = stateValues[stateName]
- if oldValue == newValue then
- print("[EVN] Ignoring request - "..stateName.." is already"..stateToString(oldValue))
- return oldValue
- end
- if oldValue == State.STARTING or oldValue == State.STOPPING then
- print("[EVN] Ignoring request - "..stateName.." is "..stateToString(oldValue))
- return oldValue
- end
- if newValue == State.ON then
- if config[stateName].onTime ~= nil then
- stateTimers[os.startTimer(config[stateName].onTime)] = {state=stateName, value=newValue}
- print("[TIM] Started timer for "..config[stateName].onTime.."s to change "..stateName.." to "..stateToString(newValue))
- setState(stateName, State.STARTING)
- else
- setState(stateName, State.ON)
- end
- elseif newValue == State.OFF then
- if config[stateName].offTime ~= nil then
- stateTimers[os.startTimer(config[stateName].offTime)] = {state=stateName, value=newValue}
- print("[TIM] Started timer for "..config[stateName].offTime.."s to change "..stateName.." to "..stateToString(newValue))
- setState(stateName, State.STOPPING)
- else
- setState(stateName, State.OFF)
- end
- end
- end
- function handleTimer(id)
- if not stateTimers[id] then return end
- opts = stateTimers[id]
- setState(opts.state, opts.value)
- stateTimers[id] = nil
- print("[TIM] Handled timer for "..opts.state.." setting to "..stateToString(opts.value))
- end
- -- Main program
- initNetwork()
- initState()
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" and p3 == protocol then
- handleMessage(p2, p1)
- elseif event == "timer" then
- handleTimer(p1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment