Advertisement
iMajesticButter

Untitled

Feb 16th, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. local modem = peripheral.find("modem", rednet.open);
  2.  
  3. local rsSidePower = "left"
  4. local rsSideUnclog = "bottom"
  5. local rsSideWaterInput = "right"
  6. local rsSidePotionOutput = "back"
  7.  
  8. local errorName = "MIXER_CONTROLLER"
  9.  
  10. -- different protocols used by this program
  11. local logging = "Potion_Brewer_LOGGER"
  12. local mixerControlProtocol = "Potion_Brewer_Mixer_Control_Protocol_id"
  13. local mixerControlProtocolRespond = "Potion_Brewer_Mixer_Control_Protocol_Respond_id"
  14. local mixerRequestUpdate = "Potion_Brewer_Mixer_Request_Update_id"
  15. local mixerRequestUpdateRespond = "Potion_Brewer_Mixer_Request_Update_Respond_id"
  16. local dispenserRequestProtocol = "Potion_Brewer_Dispenser_Request_v001"
  17. local dispenserConfirmProtocol = "Potion_Brewer_Dispenser_Confirm_v001"
  18. local scannerProtocol = "Potion_Brewer_SCANNER"
  19.  
  20. local timeoutErrorProtocol = "Potion_Brewer_ERROR_PROTOCOL_TIMEOUT"
  21.  
  22. -- different event names used by this program
  23. local eventScanner = "EVENT_SCANNER_mixer"
  24.  
  25. local idFilename = "id"
  26. local id = 0
  27.  
  28. local timeoutTime = 600
  29. local pumpTime = 4
  30. local mixTime = 4
  31.  
  32. local inUse = false
  33. local error = false
  34.  
  35. local incomingRequest = nil
  36.  
  37.  
  38. function log(string)
  39. print(string)
  40. rednet.broadcast("[MIXER_MODULE_"..id.."] "..string, logging)
  41. end
  42.  
  43. function watchdogTimeout(funcName)
  44. -- wait for timeout
  45. sleep(timeoutTime)
  46. log(funcName.." Timed out! Took longer than " .. timeoutTime .. " seconds!")
  47. rednet.broadcast(errorName, timeoutErrorProtocol)
  48. error = true
  49. end
  50.  
  51. function setPower(enabled)
  52. log("power: "..tostring(enabled))
  53. redstone.setOutput(rsSidePower, not enabled)
  54. end
  55. function setUnclog(enabled)
  56. log("unclog: "..tostring(enabled))
  57. redstone.setOutput(rsSideUnclog, not enabled)
  58. end
  59. function setWaterInput(enabled)
  60. log("water input: "..tostring(enabled))
  61. redstone.setOutput(rsSideWaterInput, not enabled)
  62. end
  63. function setPotionOutput(enabled)
  64. log("potion output: "..tostring(enabled))
  65. redstone.setOutput(rsSidePotionOutput, not enabled)
  66. end
  67.  
  68. function requestItems(request)
  69. log("validating request")
  70. if(request ~= nil and request.length ~= nil and request.items ~= nil) then
  71.  
  72. parallel.waitForAny(
  73. function()
  74. log("requesting items")
  75. request.id = id
  76. rednet.broadcast(textutils.serialize(request), dispenserRequestProtocol)
  77.  
  78. -- count incoming items
  79. for i = 1,request.length,1 do
  80. while true do
  81. local senderID, message = rednet.receive(scannerProtocol)
  82. if message == eventScanner..id then
  83. log("item received")
  84. break
  85. end
  86. end
  87. end
  88.  
  89. log("all items received")
  90.  
  91. end, function()
  92. watchdogTimeout("requestItems")
  93. end)
  94. end
  95. end
  96.  
  97. function broadcastInfo()
  98. local response = {}
  99. response.error = error
  100. response.inUse = inUse
  101.  
  102. local responseMessage = textutils.serialize(response)
  103. rednet.broadcast(responseMessage, mixerRequestUpdateRespond..id)
  104. end
  105.  
  106. function errorCheck()
  107. if error == true then
  108. log("[ERROR] ERROR ENCOUNTERED!!!")
  109. error = false
  110. return true
  111. end
  112. return false
  113. end
  114.  
  115. function init()
  116.  
  117. local idFile = fs.open(idFilename, "r")
  118. if idFile then
  119. id = tonumber(idFile.readAll())
  120. idFile.close()
  121. else
  122. log("failed to open id file: "..idFilename)
  123. end
  124.  
  125. log("ID file loaded, this mixer is: "..id)
  126. errorName = "MIXER_CONTROLLER_"..id
  127.  
  128. setPower(false)
  129. setUnclog(false)
  130. setWaterInput(false)
  131. setPotionOutput(false)
  132.  
  133. parallel.waitForAny(
  134. function()
  135. while true do
  136. if incomingRequest ~= nil then
  137. inUse = true
  138.  
  139. log("processing request: " .. textutils.serialize(incomingRequest))
  140.  
  141. -- power on
  142. setPower(true)
  143.  
  144. -- fill with water
  145. setWaterInput(true)
  146. sleep(pumpTime)
  147. setWaterInput(false)
  148.  
  149. -- request items
  150. requestItems(incomingRequest)
  151.  
  152. -- wait for mixer to finnish
  153. sleep(mixTime)
  154.  
  155. -- drain and unclog
  156. setPotionOutput(true)
  157. setUnclog(true)
  158. sleep(pumpTime)
  159. setUnclog(false)
  160. setPotionOutput(false)
  161.  
  162. -- power off
  163. setPower(false)
  164.  
  165. incomingRequest = nil
  166. inUse = false
  167. end
  168.  
  169. error = false
  170. sleep(0.2)
  171. end
  172. end,
  173. function()
  174. while true do
  175. -- respond to status requests
  176. rednet.receive(mixerRequestUpdate..id)
  177. broadcastInfo()
  178. end
  179. end,
  180. function()
  181. while true do
  182. local senderID, message = rednet.receive(mixerControlProtocol..id)
  183. incomingRequest = textutils.unserialize(message)
  184.  
  185. broadcastInfo()
  186. end
  187. end
  188. )
  189.  
  190. end
  191.  
  192. init()
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement