Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem", rednet.open);
- local rsSidePower = "left"
- local rsSideUnclog = "bottom"
- local rsSideWaterInput = "right"
- local rsSidePotionOutput = "back"
- local errorName = "MIXER_CONTROLLER"
- -- different protocols used by this program
- local logging = "Potion_Brewer_LOGGER"
- local mixerControlProtocol = "Potion_Brewer_Mixer_Control_Protocol_id"
- local mixerControlProtocolRespond = "Potion_Brewer_Mixer_Control_Protocol_Respond_id"
- local mixerRequestUpdate = "Potion_Brewer_Mixer_Request_Update_id"
- local mixerRequestUpdateRespond = "Potion_Brewer_Mixer_Request_Update_Respond_id"
- local dispenserRequestProtocol = "Potion_Brewer_Dispenser_Request_v001"
- local dispenserConfirmProtocol = "Potion_Brewer_Dispenser_Confirm_v001"
- local scannerProtocol = "Potion_Brewer_SCANNER"
- local timeoutErrorProtocol = "Potion_Brewer_ERROR_PROTOCOL_TIMEOUT"
- -- different event names used by this program
- local eventScanner = "EVENT_SCANNER_mixer"
- local idFilename = "id"
- local id = 0
- local timeoutTime = 600
- local pumpTime = 4
- local mixTime = 8
- local pumpWaitTime = 10
- local inUse = false
- local error = false
- local incomingRequest = nil
- function log(string)
- print(string)
- rednet.broadcast("[MIXER_MODULE_"..id.."] "..string, logging)
- end
- function watchdogTimeout(funcName)
- -- wait for timeout
- sleep(timeoutTime)
- log(funcName.." Timed out! Took longer than " .. timeoutTime .. " seconds!")
- rednet.broadcast(errorName, timeoutErrorProtocol)
- error = true
- end
- function setPower(enabled)
- log("power: "..tostring(enabled))
- redstone.setOutput(rsSidePower, not enabled)
- end
- function setUnclog(enabled)
- log("unclog: "..tostring(enabled))
- redstone.setOutput(rsSideUnclog, not enabled)
- end
- function setWaterInput(enabled)
- log("water input: "..tostring(enabled))
- redstone.setOutput(rsSideWaterInput, not enabled)
- end
- function setPotionOutput(enabled)
- log("potion output: "..tostring(enabled))
- redstone.setOutput(rsSidePotionOutput, not enabled)
- end
- function requestItems(request)
- log("validating request")
- if(request ~= nil and request.length ~= nil and request.items ~= nil) then
- parallel.waitForAny(
- function()
- log("requesting items")
- request.id = id
- rednet.broadcast(textutils.serialize(request), dispenserRequestProtocol)
- -- count incoming items
- for i = 1,request.length,1 do
- while true do
- local senderID, message = rednet.receive(scannerProtocol)
- if message == eventScanner..id then
- log("item received")
- break
- end
- end
- end
- log("all items received")
- end, function()
- watchdogTimeout("requestItems")
- end)
- end
- end
- function broadcastInfo()
- local response = {}
- response.error = error
- response.inUse = inUse
- local responseMessage = textutils.serialize(response)
- rednet.broadcast(responseMessage, mixerRequestUpdateRespond..id)
- end
- function errorCheck()
- if error == true then
- log("[ERROR] ERROR ENCOUNTERED!!!")
- error = false
- return true
- end
- return false
- end
- function init()
- local idFile = fs.open(idFilename, "r")
- if idFile then
- id = tonumber(idFile.readAll())
- idFile.close()
- else
- log("failed to open id file: "..idFilename)
- end
- log("ID file loaded, this mixer is: "..id)
- errorName = "MIXER_CONTROLLER_"..id
- setPower(false)
- setUnclog(false)
- setWaterInput(false)
- setPotionOutput(false)
- parallel.waitForAny(
- function()
- while true do
- if incomingRequest ~= nil then
- inUse = true
- log("processing request: " .. textutils.serialize(incomingRequest))
- -- power on
- setPower(true)
- -- fill with water
- setWaterInput(true)
- sleep(pumpTime)
- setWaterInput(false)
- -- request items
- requestItems(incomingRequest)
- -- wait for mixer to finnish
- sleep(mixTime)
- -- drain and unclog
- setPotionOutput(true)
- setUnclog(true)
- sleep(pumpTime)
- setPotionOutput(false)
- sleep(pumpTime)
- setUnclog(false)
- -- power off
- setPower(false)
- sleep(pumpWaitTime)
- incomingRequest = nil
- inUse = false
- end
- error = false
- sleep(0.2)
- end
- end,
- function()
- while true do
- -- respond to status requests
- rednet.receive(mixerRequestUpdate..id)
- broadcastInfo()
- end
- end,
- function()
- while true do
- local senderID, message = rednet.receive(mixerControlProtocol..id)
- if not inUse then
- incomingRequest = textutils.unserialize(message)
- end
- broadcastInfo()
- end
- end
- )
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement