Advertisement
JakobM7

ControlStation.lua

Apr 17th, 2025 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. local modem = peripheral.wrap("top")
  2. local monitor = peripheral.wrap("left")
  3.  
  4. modem.open(537) -- channel for receiving data from power Points
  5. modem.open(538) --channel for sending commadns to power Points
  6. local timeout = 4 -- seconds to wait for a response from the power point
  7. local updateInterval = 1
  8.  
  9.  
  10. local nolimit = math.huge
  11. BusyFG = false
  12. BusyBG = false
  13.  
  14. -- Priority : 5: Highest, 1: Very Low, 0: Shutdown
  15.  
  16. -- Format : { "DeviceName", current Rate, current Limit, Priority, type, optional: storageAmount, maxStorageAmount}
  17.  
  18. -- type: 1: consumer, 10: consumer with Buffer,
  19. -- 2: producer, 20: producer with Buffer,
  20. -- 0: storage(In+Out), 3: none
  21.  
  22. -- Units: FE/t, FE
  23. Devices = {
  24.   {deviceName = "ME-System", rate=0, limit=nolimit, priority=5, type=10, storageAmount=0, maxStorageAmount=0},
  25. }
  26.  
  27.  
  28. --Exptected message format:
  29. -- {pointName, "getTransferRate"}
  30. -- {pointName, "getTransferRateLimit"}
  31. -- {pointName, "setLimit", limit}
  32.  
  33. function split(str, sep)
  34.   if sep == nil then
  35.     sep = "%s"
  36.   end
  37.   local t = {}
  38.   for token in string.gmatch(str, "([^" .. sep .. "]+)") do
  39.     table.insert(t, token)
  40.   end
  41.   return t
  42. end
  43.  
  44.  
  45.  
  46. function CheckTransferRate(deviceName)
  47.   modem.transmit(538, 537, {deviceName, "getTransferRate"})
  48.   os.startTimer(timeout)
  49.   local event, side, channel, replyChannel, message = os.pullEvent({"modem_message","timer"})
  50.  
  51.   if event == "timer" then
  52.     print("Timeout waiting for transfer rate limit response")
  53.     return nil
  54.   end
  55.  
  56.   if event == "modem_message" and channel == 537 and type(message) then
  57.     if message[1] == deviceName then
  58.       --print("Received transfer rate: " .. message[2])
  59.       return tonumber(message[2])
  60.     end
  61.   end
  62. end
  63.  
  64. function CheckTransferRateLimit(deviceName)
  65.   modem.transmit(538, 537, {deviceName, "getTransferRateLimit"})
  66.   os.startTimer(timeout)
  67.   local event, side, channel, replyChannel, message = os.pullEvent({"modem_message","timer"})
  68.  
  69.   if event == "timer" then
  70.     print("Timeout waiting for transfer rate limit response")
  71.     return nil
  72.   end
  73.  
  74.   if event == "modem_message" and channel == 537 and type(message) then
  75.     if message[1] == deviceName then
  76.       --print("Received transfer rate: " .. message[2])
  77.       return tonumber(message[2])
  78.     end
  79.   end
  80. end
  81.  
  82. function CheckEnergy(deviceName)
  83.   modem.transmit(538, 537, {deviceName, "getEnergy"})
  84.   os.startTimer(timeout)
  85.   local event, side, channel, replyChannel, message = os.pullEvent("modem_message","timer")
  86.  
  87.   if event == "timer" then
  88.     print("Timeout waiting for energy response")
  89.     return nil
  90.   end
  91.  
  92.   if event == "modem_message" and channel == 537 and type(message) then
  93.     if message[1] == deviceName then
  94.       --print("Received transfer rate: " .. message[2])
  95.       return tonumber(message[2])
  96.     end
  97.   end
  98. end
  99.  
  100. function CheckMaxEnergy(deviceName)
  101.   modem.transmit(538, 537, {deviceName, "getMaxEnergy"})
  102.   os.startTimer(timeout)
  103.   local event, side, channel, replyChannel, message = os.pullEvent("modem_message","timer")
  104.  
  105.   if event == "timer" then
  106.     print("Timeout waiting for energy response")
  107.     return nil
  108.   end
  109.  
  110.   if event == "modem_message" and channel == 537 and type(message) then
  111.     if message[1] == deviceName then
  112.       --print("Received transfer rate: " .. message[2])
  113.       return tonumber(message[2])
  114.     end
  115.   end
  116. end
  117.  
  118.  
  119.  
  120. function SetLimit(deviceName, limit)
  121.   modem.transmit(538, 537, {deviceName, "setLimit", limit})
  122. end
  123.  
  124. function SetPriority(deviceName, priority)
  125.   print("Setting priority for device: " .. deviceName .. " to " .. priority)
  126.   for i=1, #Devices do
  127.     if Devices[i].deviceName == deviceName then
  128.       Devices[i].priority = priority
  129.     end
  130.   end
  131. end
  132.  
  133.  
  134. function UpdateRates()
  135.   for i = 1, #Devices do
  136.     local deviceName = Devices[i].deviceName
  137.     Devices[i].rate = CheckTransferRate(deviceName) or Devices[i].rate
  138.     Devices[i].limit = CheckTransferRateLimit(deviceName) or Devices[i].limit
  139.     if Devices[i].type == 10 or Devices[i].type == 20 or Devices[i].type == 0 then
  140.       Devices[i].storageAmount = CheckEnergy(deviceName) or Devices[i].storageAmount
  141.       Devices[i].maxStorageAmount = CheckMaxEnergy(deviceName) or Devices[i].maxStorageAmount
  142.     end
  143.   end
  144. end
  145.  
  146. function PrintDevices()
  147.   --print("BusyFG = " .. tostring(BusyFG))
  148.   --print("BusyBG = " .. tostring(BusyBG))
  149.   for i = 1, #Devices do
  150.     print("Device: " .. Devices[i].deviceName .. " type: " .. Devices[i].type .. " priority: " .. Devices[i].priority)
  151.     print("Transfer Rate: " .. Devices[i].rate .. " Limit: " .. Devices[i].limit)
  152.     if Devices[i].type == 20 or  Devices[i].type == 10 or Devices[i].type == 0 then
  153.       print("Energy Storage: " .. Devices[i].storageAmount .. "/" .. Devices[i].maxStorageAmount)
  154.     end
  155.   end
  156. end
  157.  
  158.  
  159. function BackgroundTick()
  160.   BusyBG = true
  161.  
  162.   UpdateRates()
  163.  
  164.   BusyBG = false
  165. end
  166.  
  167. function ForegroundTick()
  168.  
  169.   term.setCursorPos(1, 1)
  170.   term.clear()
  171.   PrintDevices()
  172. end
  173.  
  174.  
  175. function Tick()
  176.  
  177.   while true do
  178.     if not BusyBG then
  179.       BackgroundTick()
  180.     end
  181.     if not BusyFG then
  182.       ForegroundTick()
  183.     end
  184.    
  185.     sleep(updateInterval)
  186.   end
  187.  
  188. end
  189.  
  190.  
  191.  
  192. function AskInput()
  193.   print("Please enter a command:")
  194.   print("1. setLimit <deviceName> <limit>")
  195.   print("2. setPriority <deviceName> <priority>. \n(Priority: 5: Highest, 1: Very Low, 0: Shutdown, -1: manual)")
  196.  
  197.   local input = read()
  198.   local command = split(input, " ")
  199.  
  200.   if #command == 1 then
  201.     print("exiting.")
  202.     return
  203.   end
  204.  
  205.   -- DEBUG
  206.   print("Command: " .. command[1])
  207.   print("Device: " .. command[2])
  208.   print("Value: " .. command[3])
  209.  
  210.   if command[1] == "setLimit" or command[1] == "1" then
  211.     local deviceName = command[2]
  212.     local limit = tonumber(command[3])
  213.     SetLimit(deviceName, limit)
  214.     SetPriority(deviceName, -1)
  215.  
  216.   elseif command[1] == "setPriority" or command[1] == "2" then
  217.     local deviceName = command[2]
  218.     local priority = tonumber(command[3])
  219.     if priority then SetPriority(deviceName, priority)
  220.     else print("invalid value") end
  221.   else
  222.     print("Invalid input, please try again.")
  223.   end
  224.   sleep(1)
  225. end
  226.  
  227. function keyInterruptWait()
  228.   local event, key, is_held = os.pullEvent("key")
  229.   BusyFG = true
  230.   print("Key pressed: " .. key)
  231.  
  232.   if event == "key" then
  233.     AskInput()
  234.   end
  235.  
  236.   BusyFG = false
  237.  
  238. end
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. while true do
  246.  
  247.   parallel.waitForAny(keyInterruptWait, Tick)
  248.  
  249. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement