Advertisement
iMajesticButter

Untitled

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