Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem", rednet.open);
- local logging = "Potion_Brewer_LOGGER"
- local errorName = "DISPENSER_CONTROLLER"
- -- different protocols used by this program
- local dispenserRequestProtocol = "Potion_Brewer_Dispenser_Request_v001"
- local dispenserConfirmProtocol = "Potion_Brewer_Dispenser_Confirm_v001"
- local dispenserCompleteProtocol = "Potion_Brewer_Dispenser_Complete_v001"
- local dispenserNodeProtocol = "Potion_Brewer_Ingredient_Dispenser_Node_v001"
- local beltSelectorProtocol = "Potion_Brewer_BELT_SELECTOR"
- local scannerProtocol = "Potion_Brewer_SCANNER"
- local timeoutErrorProtocol = "Potion_Brewer_ERROR_PROTOCOL_TIMEOUT"
- -- different event names used by this program (lets just use protocols in future)
- local eventDispenseItem = "EVENT_DISPENSE_"
- local eventDispenseItemStarted = "EVENT_DISPENSE_STARTED_"
- local eventSelectBelt = "EVENT_BELT_SELECT_"
- local eventSelectBeltFinnished = "EVENT_BELT_SELECT_FINNISHED_"
- local eventScanner = "EVENT_SCANNER_"
- local scannerDispenser = "dispenser"
- local beltSelectorDispenser = "dispenser"
- local timeoutTime = 10
- local error = false
- -- the queue of requests
- local qfirst = 0
- local qlast = -1
- local queue = {}
- queue.push = function(item)
- qlast = qlast + 1
- queue[qlast] = item
- end
- queue.pop = function()
- if qfirst <= qlast then
- local value = queue[qfirst]
- queue[qfirst] = nil
- qfirst = qfirst + 1
- return value
- end
- end
- queue.length = function()
- return (qlast + 1) - qfirst
- end
- function log(string)
- print(string)
- rednet.broadcast("[" .. errorName .. "] " ..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 dispenseItem(string)
- parallel.waitForAny(
- function()
- log("attempting to dispense " .. string)
- -- request that the item is dispensed
- rednet.broadcast(eventDispenseItem .. string, dispenserNodeProtocol)
- local message = ""
- local senderID = 0
- log("dispense request sent to nodes")
- -- wait for the response from the dispenser node
- while message ~= eventDispenseItemStarted .. string do
- senderID, message = rednet.receive(dispenserNodeProtocol)
- end
- log("response received")
- message = ""
- -- wait for the item to be detected on the conveyor
- while message ~= eventScanner .. scannerDispenser do
- senderID, message = rednet.receive(scannerProtocol)
- end
- log("item detected! dispense successful!")
- end,
- function()
- watchdogTimeout("dispenseItem")
- end
- )
- end
- function selectBelt(beltSelector, beltNumber)
- parallel.waitForAny(
- function()
- log("setting belt to "..beltNumber)
- -- request a bet change
- local eventName = eventSelectBelt..beltSelector.." "
- rednet.broadcast(eventName..beltNumber, beltSelectorProtocol)
- log("belt change request sent to belt selectors")
- -- wait for the response from the belt selector
- local message = ""
- local senderID = 0
- while message ~= eventSelectBeltFinnished..beltSelector.." "..beltNumber do
- senderID, message = rednet.receive(beltSelectorProtocol)
- end
- log("Belt change confirmed! Successful!")
- end,
- function()
- watchdogTimeout("selectBelt")
- end
- )
- end
- function errorCheck()
- if error == true then
- log("[ERROR] ERROR ENCOUNTERED!!!")
- error = false
- return true
- end
- return false
- end
- function main()
- parallel.waitForAny(
- function()
- while true do
- if queue.length() > 0 then
- local request = queue.pop()
- if(request ~= nil and request.data ~= nil and request.data.id ~= nil and request.data.length ~= nil and request.data.items ~= nil) then
- log("Processing request: " .. request.data.id)
- -- select the beltNumber
- selectBelt(beltSelectorDispenser, request.data.id)
- if errorCheck() == false then
- local numItems = request.data.length
- -- loop over items
- for i = 0,numItems,1 do
- local item = request.data.items[i]
- if item ~= nil then
- log("Processing request item: " .. item)
- dispenseItem(item)
- if errorCheck() == true then
- break
- end
- end
- end
- end
- end
- end
- os.sleep(0.5)
- end
- end,
- function()
- while true do
- -- wait for requests
- local senderID, message = rednet.receive(dispenserRequestProtocol)
- print(message)
- log("request received")
- -- add it to the queue
- local request = {}
- request.data = textutils.unserialize(message)
- queue.push(request)
- -- respond with a confirmation
- rednet.broadcast(request.data.id, dispenserConfirmProtocol)
- end
- end
- )
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement