Advertisement
iMajesticButter

Untitled

Feb 16th, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 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.data.id = id
  76. rednet.broadcast(request, dispenserRequestProtocol)
  77.  
  78. -- count incoming items
  79. for i = 0,request.data.length,1 do
  80. rednet.receive(eventScanner .. id)
  81. log("item received")
  82. end
  83.  
  84. log("all items received")
  85.  
  86. end, function()
  87. watchdogTimeout("requestItems")
  88. end)
  89. end
  90. end
  91.  
  92. function broadcastInfo()
  93. local response = {}
  94. response.error = error
  95. response.inUse = inUse
  96.  
  97. local responseMessage = textutils.serialize(response)
  98. rednet.broadcast(responseMessage, mixerRequestUpdateRespond..id)
  99. end
  100.  
  101. function errorCheck()
  102. if error == true then
  103. log("[ERROR] ERROR ENCOUNTERED!!!")
  104. error = false
  105. return true
  106. end
  107. return false
  108. end
  109.  
  110. function init()
  111.  
  112. local idFile = fs.open(idFilename, "r")
  113. if idFile then
  114. id = tonumber(idFile.readAll())
  115. idFile.close()
  116. else
  117. log("failed to open id file: "..idFilename)
  118. end
  119.  
  120. log("ID file loaded, this mixer is: "..id)
  121. errorName = "MIXER_CONTROLLER_"..id
  122.  
  123. setPower(false)
  124. setUnclog(false)
  125. setWaterInput(false)
  126. setPotionOutput(false)
  127.  
  128. parallel.waitForAny(
  129. function()
  130. while true do
  131. if incomingRequest ~= nil then
  132. inUse = true
  133.  
  134. log("processing request: " .. textutils.serialize(incomingRequest))
  135.  
  136. -- power on
  137. setPower(true)
  138.  
  139. -- fill with water
  140. setWaterInput(true)
  141. sleep(pumpTime)
  142. setWaterInput(false)
  143.  
  144. -- request items
  145. requestItems(incomingRequest)
  146.  
  147. -- wait for mixer to finnish
  148. sleep(mixTime)
  149.  
  150. -- drain and unclog
  151. setPotionOutput(true)
  152. setUnclog(true)
  153. sleep(pumpTime)
  154. setUnclog(false)
  155. setPotionOutput(false)
  156.  
  157. -- power off
  158. setPower(false)
  159.  
  160. incomingRequest = nil
  161. inUse = false
  162. end
  163.  
  164. error = false
  165. sleep(0.2)
  166. end
  167. end,
  168. function()
  169. while true do
  170. -- respond to status requests
  171. rednet.receive(mixerRequestUpdate..id)
  172. broadcastInfo()
  173. end
  174. end,
  175. function()
  176. while true do
  177. local senderID, message = rednet.receive(mixerControlProtocol..id)
  178. incomingRequest = textutils.unserialize(message)
  179.  
  180. broadcastInfo()
  181. end
  182. end
  183. )
  184.  
  185. end
  186.  
  187. init()
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement