Advertisement
iMajesticButter

Untitled

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