Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("top")
- local monitor = peripheral.wrap("left")
- modem.open(537) -- channel for receiving data from power Points
- modem.open(538) --channel for sending commadns to power Points
- local timeout = 4 -- seconds to wait for a response from the power point
- local updateInterval = 1
- local nolimit = math.huge
- BusyFG = false
- BusyBG = false
- -- Priority : 5: Highest, 1: Very Low, 0: Shutdown
- -- Format : { "DeviceName", current Rate, current Limit, Priority, type, optional: storageAmount, maxStorageAmount}
- -- type: 1: consumer, 10: consumer with Buffer,
- -- 2: producer, 20: producer with Buffer,
- -- 0: storage(In+Out), 3: none
- -- Units: FE/t, FE
- Devices = {
- {deviceName = "ME-System", rate=0, limit=nolimit, priority=5, type=10, storageAmount=0, maxStorageAmount=0},
- }
- --Exptected message format:
- -- {pointName, "getTransferRate"}
- -- {pointName, "getTransferRateLimit"}
- -- {pointName, "setLimit", limit}
- function split(str, sep)
- if sep == nil then
- sep = "%s"
- end
- local t = {}
- for token in string.gmatch(str, "([^" .. sep .. "]+)") do
- table.insert(t, token)
- end
- return t
- end
- function CheckTransferRate(deviceName)
- modem.transmit(538, 537, {deviceName, "getTransferRate"})
- os.startTimer(timeout)
- local event, side, channel, replyChannel, message = os.pullEvent({"modem_message","timer"})
- if event == "timer" then
- print("Timeout waiting for transfer rate limit response")
- return nil
- end
- if event == "modem_message" and channel == 537 and type(message) then
- if message[1] == deviceName then
- --print("Received transfer rate: " .. message[2])
- return tonumber(message[2])
- end
- end
- end
- function CheckTransferRateLimit(deviceName)
- modem.transmit(538, 537, {deviceName, "getTransferRateLimit"})
- os.startTimer(timeout)
- local event, side, channel, replyChannel, message = os.pullEvent({"modem_message","timer"})
- if event == "timer" then
- print("Timeout waiting for transfer rate limit response")
- return nil
- end
- if event == "modem_message" and channel == 537 and type(message) then
- if message[1] == deviceName then
- --print("Received transfer rate: " .. message[2])
- return tonumber(message[2])
- end
- end
- end
- function CheckEnergy(deviceName)
- modem.transmit(538, 537, {deviceName, "getEnergy"})
- os.startTimer(timeout)
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message","timer")
- if event == "timer" then
- print("Timeout waiting for energy response")
- return nil
- end
- if event == "modem_message" and channel == 537 and type(message) then
- if message[1] == deviceName then
- --print("Received transfer rate: " .. message[2])
- return tonumber(message[2])
- end
- end
- end
- function CheckMaxEnergy(deviceName)
- modem.transmit(538, 537, {deviceName, "getMaxEnergy"})
- os.startTimer(timeout)
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message","timer")
- if event == "timer" then
- print("Timeout waiting for energy response")
- return nil
- end
- if event == "modem_message" and channel == 537 and type(message) then
- if message[1] == deviceName then
- --print("Received transfer rate: " .. message[2])
- return tonumber(message[2])
- end
- end
- end
- function SetLimit(deviceName, limit)
- modem.transmit(538, 537, {deviceName, "setLimit", limit})
- end
- function SetPriority(deviceName, priority)
- print("Setting priority for device: " .. deviceName .. " to " .. priority)
- for i=1, #Devices do
- if Devices[i].deviceName == deviceName then
- Devices[i].priority = priority
- end
- end
- end
- function UpdateRates()
- for i = 1, #Devices do
- local deviceName = Devices[i].deviceName
- Devices[i].rate = CheckTransferRate(deviceName) or Devices[i].rate
- Devices[i].limit = CheckTransferRateLimit(deviceName) or Devices[i].limit
- if Devices[i].type == 10 or Devices[i].type == 20 or Devices[i].type == 0 then
- Devices[i].storageAmount = CheckEnergy(deviceName) or Devices[i].storageAmount
- Devices[i].maxStorageAmount = CheckMaxEnergy(deviceName) or Devices[i].maxStorageAmount
- end
- end
- end
- function PrintDevices()
- --print("BusyFG = " .. tostring(BusyFG))
- --print("BusyBG = " .. tostring(BusyBG))
- for i = 1, #Devices do
- print("Device: " .. Devices[i].deviceName .. " type: " .. Devices[i].type .. " priority: " .. Devices[i].priority)
- print("Transfer Rate: " .. Devices[i].rate .. " Limit: " .. Devices[i].limit)
- if Devices[i].type == 20 or Devices[i].type == 10 or Devices[i].type == 0 then
- print("Energy Storage: " .. Devices[i].storageAmount .. "/" .. Devices[i].maxStorageAmount)
- end
- end
- end
- function BackgroundTick()
- BusyBG = true
- UpdateRates()
- BusyBG = false
- end
- function ForegroundTick()
- term.setCursorPos(1, 1)
- term.clear()
- PrintDevices()
- end
- function Tick()
- while true do
- if not BusyBG then
- BackgroundTick()
- end
- if not BusyFG then
- ForegroundTick()
- end
- sleep(updateInterval)
- end
- end
- function AskInput()
- print("Please enter a command:")
- print("1. setLimit <deviceName> <limit>")
- print("2. setPriority <deviceName> <priority>. \n(Priority: 5: Highest, 1: Very Low, 0: Shutdown, -1: manual)")
- local input = read()
- local command = split(input, " ")
- if #command == 1 then
- print("exiting.")
- return
- end
- -- DEBUG
- print("Command: " .. command[1])
- print("Device: " .. command[2])
- print("Value: " .. command[3])
- if command[1] == "setLimit" or command[1] == "1" then
- local deviceName = command[2]
- local limit = tonumber(command[3])
- SetLimit(deviceName, limit)
- SetPriority(deviceName, -1)
- elseif command[1] == "setPriority" or command[1] == "2" then
- local deviceName = command[2]
- local priority = tonumber(command[3])
- if priority then SetPriority(deviceName, priority)
- else print("invalid value") end
- else
- print("Invalid input, please try again.")
- end
- sleep(1)
- end
- function keyInterruptWait()
- local event, key, is_held = os.pullEvent("key")
- BusyFG = true
- print("Key pressed: " .. key)
- if event == "key" then
- AskInput()
- end
- BusyFG = false
- end
- while true do
- parallel.waitForAny(keyInterruptWait, Tick)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement