Doxume

Untitled

Jan 5th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. local State = {ON=1, OFF=2, STARTING=3, STOPPING=4}
  2.  
  3. local config = {
  4. bayDoorBack={color=colors.white, onTime=3, offTime=3},
  5. shutter={color=colors.orange, onTime=2, offTime=2}
  6. }
  7.  
  8. local stateValues = {}
  9. local stateTimers = {}
  10.  
  11. local modemSide = "right"
  12. local rsSide = "back"
  13.  
  14. local hostname = "host"
  15. local protocol = "status"
  16.  
  17.  
  18. function initNetwork()
  19. rednet.open(modemSide)
  20. rednet.host(protocol, hostname)
  21. print("[NET] Running "..protocol.." as "..hostname)
  22. end
  23.  
  24. function initState()
  25. for name, config in pairs(config) do
  26. stateValues[name] = stateFromRs(name)
  27. end
  28.  
  29. end
  30.  
  31. function stateToString(state)
  32. if state == State.ON then
  33. return "ON"
  34. elseif state == State.OFF then
  35. return "OFF"
  36. elseif state == State.STARTING then
  37. return "STARTING"
  38. elseif state == State.STOPPING then
  39. return "STOPPING"
  40. else
  41. return "UNKNOWN"
  42. end
  43.  
  44. end
  45.  
  46. function stateFromRs(stateName)
  47. color = config[stateName].color
  48. if rs.testBundledInput(rsSide, color) then
  49. return State.ON
  50. else
  51. return State.OFF
  52. end
  53. end
  54.  
  55. function getStatePayload(stateName)
  56. return {state=stateName, value=stateValues[stateName]}
  57. end
  58.  
  59. function broadcastChange(stateName)
  60. rednet.broadcast({head="event", payload=getStatePayload(stateName)}, protocol)
  61. print("[EVN] Changed "..stateName.." to "..stateToString(stateValues[stateName]))
  62. end
  63.  
  64. function handleMessage(msg, sender)
  65. if msg.head == "get" then
  66. rednet.send(sender, {head="reply", payload=stateValues}, protocol)
  67. print("[NET] Received GET from sender:"..sender)
  68.  
  69. elseif msg.head == "set" and config[msg.payload.state] then
  70. print("[NET] Received SET "..msg.payload.state.." TO "..stateToString(msg.payload.value).." from sender:"..sender)
  71. newValue = changeState(msg.payload.state, msg.payload.value)
  72. rednet.send(sender, {head="reply", payload={state=msg.payload.state, value=newValue}}, protocol)
  73. end
  74. end
  75.  
  76. function setRsState(stateName, rsValue)
  77. color = config[stateName].color
  78. existing = rs.getBundledInput(rsSide)
  79.  
  80. if rsValue then
  81. rs.setBundledOutput(rsSide, colors.combine(existing, color))
  82. else
  83. rs.setBundledOutput(rsSide, colors.subtract(existing, color))
  84. end
  85. end
  86.  
  87. function setState(stateName, newValue)
  88.  
  89. stateValues[stateName] = newValue
  90.  
  91. if newValue == State.ON or newValue == State.STARTING then
  92. setRsState(stateName, true)
  93.  
  94. elseif newValue == State.OFF or newValue == State.STOPPING then
  95. setRsState(stateName, false)
  96.  
  97. end
  98.  
  99. broadcastChange(stateName)
  100. end
  101.  
  102.  
  103. function changeState(stateName, newValue)
  104.  
  105. oldValue = stateValues[stateName]
  106. if oldValue == newValue then
  107. print("[EVN] Ignoring request - "..stateName.." is already"..stateToString(oldValue))
  108. return oldValue
  109. end
  110.  
  111. if oldValue == State.STARTING or oldValue == State.STOPPING then
  112. print("[EVN] Ignoring request - "..stateName.." is "..stateToString(oldValue))
  113. return oldValue
  114. end
  115.  
  116. if newValue == State.ON then
  117.  
  118. if config[stateName].onTime ~= nil then
  119. stateTimers[os.startTimer(config[stateName].onTime)] = {state=stateName, value=newValue}
  120. print("[TIM] Started timer for "..config[stateName].onTime.."s to change "..stateName.." to "..stateToString(newValue))
  121. setState(stateName, State.STARTING)
  122. else
  123. setState(stateName, State.ON)
  124. end
  125.  
  126. elseif newValue == State.OFF then
  127.  
  128. if config[stateName].offTime ~= nil then
  129. stateTimers[os.startTimer(config[stateName].offTime)] = {state=stateName, value=newValue}
  130. print("[TIM] Started timer for "..config[stateName].offTime.."s to change "..stateName.." to "..stateToString(newValue))
  131. setState(stateName, State.STOPPING)
  132. else
  133. setState(stateName, State.OFF)
  134. end
  135.  
  136. end
  137.  
  138. end
  139.  
  140. function handleTimer(id)
  141.  
  142. if not stateTimers[id] then return end
  143.  
  144. opts = stateTimers[id]
  145. setState(opts.state, opts.value)
  146. stateTimers[id] = nil
  147. print("[TIM] Handled timer for "..opts.state.." setting to "..stateToString(opts.value))
  148. end
  149.  
  150. -- Main program
  151. initNetwork()
  152. initState()
  153.  
  154. while true do
  155.  
  156. local event, p1, p2, p3 = os.pullEvent()
  157.  
  158. if event == "rednet_message" and p3 == protocol then
  159. handleMessage(p2, p1)
  160.  
  161. elseif event == "timer" then
  162. handleTimer(p1)
  163.  
  164. end
  165.  
  166. end
Advertisement
Add Comment
Please, Sign In to add comment