Advertisement
iMajesticButter

Untitled

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