Advertisement
Corbinhol

DispenserController

Sep 22nd, 2022 (edited)
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.08 KB | None | 0 0
  1.     --Tablet
  2.     local filePath = "basalt.lua" --here you can change the file path that it installs to. default: /basalt.lua
  3.     if not(fs.exists(filePath))then
  4.         shell.run("pastebin run ESs1mg7P packed true "..filePath:gsub(".lua", "")) -- this is an alternative to the wget command
  5.     end
  6.     local basalt = require(filePath:gsub(".lua", "")) -- here you can change the variablename in any variablename you want default: basalt
  7.  
  8.     basalt.setTheme(
  9.         { -- The default main theme for basalt!
  10.     BasaltBG = colors.white,
  11.     BasaltText = colors.black,
  12.     FrameBG = colors.gray,
  13.     FrameText = colors.black,
  14.     ButtonBG = colors.cyan,
  15.     ButtonText = colors.white,
  16.     CheckboxBG = colors.gray,
  17.     CheckboxText = colors.black,
  18.     InputBG = colors.gray,
  19.     InputText = colors.black,
  20.     TextfieldBG = colors.gray,
  21.     TextfieldText = colors.black,
  22.     ListBG = colors.gray,
  23.     ListText = colors.black,
  24.     MenubarBG = colors.gray,
  25.     MenubarText = colors.black,
  26.     DropdownBG = colors.gray,
  27.     DropdownText = colors.black,
  28.     RadioBG = colors.gray,
  29.     RadioText = colors.black,
  30.     SelectionBG = colors.black,
  31.     SelectionText = colors.lightGray,
  32.     GraphicBG = colors.black,
  33.     ImageBG = colors.black,
  34.     PaneBG = colors.black,
  35.     ProgramBG = colors.black,
  36.     ProgressbarBG = colors.gray,
  37.     ProgressbarText = colors.black,
  38.     ProgressbarActiveBG = colors.black,
  39.     ScrollbarBG = colors.lightGray,
  40.     ScrollbarText = colors.gray,
  41.     ScrollbarSymbolColor = colors.black,
  42.     SliderBG = false,
  43.     SliderText = colors.gray,
  44.     SliderSymbolColor = colors.black,
  45.     SwitchBG = colors.lightGray,
  46.     SwitchText = colors.gray,
  47.     SwitchBGSymbol = colors.black,
  48.     SwitchInactive = colors.red,
  49.     SwitchActive = colors.green,
  50.     LabelBG = false,
  51.     LabelText = colors.black
  52.    
  53. }
  54.     )
  55.  
  56.     term.clear();
  57.     term.setCursorPos(1,1);
  58.     local locked = {};
  59.     local parent = {};
  60.  
  61.     local mainFrame = basalt.createFrame()
  62.     locked[mainFrame] = false;
  63.    
  64.     rednet.open("back");
  65.     local api = {};
  66.  
  67.  
  68.     local function newMessageBox(frame)
  69.         local out = mainFrame:addFrame():setBackground(colors.gray):setSize(26, 5):setPosition(1, 7):setShadow(colors.black):hide()
  70.         return out;
  71.     end
  72.  
  73.     function showMessageBox(frame, text)
  74.         if locked[frame] == false then
  75.             locked[frame] = true;
  76.             local messageBox = newMessageBox(frame);
  77.             local messageBoxLabel = messageBox:addLabel():setText("Lowering Elevator..."):setPosition(2, 2):setForeground(colors.white):setFontSize(1);
  78.             local messageBoxButton = messageBox:addButton():setText("Ok"):setPosition(11,4):setSize(6, 1):setForeground(colors.white):setBackground(colors.blue);
  79.             messageBox:show();
  80.  
  81.             messageBoxButton:onClick(
  82.                 function()
  83.                     messageBoxButton:setBackground(colors.lightGray);
  84.                 end
  85.             )
  86.  
  87.             messageBoxButton:onClickUp(
  88.                 function()
  89.                     messageBoxButton:setBackground(colors.blue);
  90.                     messageBox:hide():disable()
  91.                     messageBox = nil;
  92.                     locked[frame] = false;
  93.                 end
  94.             )
  95.         end
  96.     end
  97.    
  98.     function onElevatorButtonClick()
  99.         if locked[mainFrame] == false then
  100.             if api["ElevatorData"] == "Idle" then
  101.                 local apiProtocol = "API"
  102.                 local elevatorAddress = rednet.lookup(apiProtocol, "elevator");
  103.                 rednet.send(elevatorAddress, "lowerElevator", apiProtocol);
  104.                 locked[mainFrame] = true;
  105.                 local elevatorResponse = newMessageBox(mainFrame);
  106.                 local elevatorResponseLabel = elevatorResponse:addLabel():setText("Lowering Elevator..."):setPosition(2, 2):setForeground(colors.white):setFontSize(1);
  107.                 local elevatorResponseButton = elevatorResponse:addButton():setText("Ok"):setPosition(11,4):setSize(6, 1):setForeground(colors.white):setBackground(colors.blue);
  108.                 elevatorResponse:show();
  109.                
  110.                 elevatorResponseButton:onClick(
  111.                     function()
  112.                         elevatorResponseButton:setBackground(colors.lightGray);
  113.                     end
  114.                 )
  115.  
  116.                 elevatorResponseButton:onClickUp(
  117.                     function()
  118.                         elevatorResponseButton:setBackground(colors.blue);
  119.                         elevatorResponse:hide():disable()
  120.                         elevatorResponse = nil;
  121.                         locked[mainFrame] = false;
  122.                     end
  123.                 )
  124.             end
  125.         end
  126.     end
  127.  
  128.     function onSafeButtonClick()
  129.         if locked[mainFrame] == false then
  130.             if api["SafeData"] ~= "Offline" then
  131.                 local safeProtocol = "SAFE";
  132.                 local safeAddress = rednet.lookup(safeProtocol, "safeController");
  133.                 local safeLoadingWindow = newMessageBox(mainFrame);
  134.                 -- local safeLoadingWindowLabel = messageBox:addLabel()setPosition(2, 2):setForeground(colors.white):setFontSize(1);
  135.                 -- if api["SafeData"] == "Unlocked" then
  136.                 --     safeLoadingWindowLabel:setText("Closing Safe...");
  137.                 -- elseif api["SafeData"] == "Locked" then
  138.                 --     safeLoadingWindowLabel:setText("Opening Safe...");
  139.                 -- end
  140.                 safeLoadingWindow:show();
  141.             end    
  142.         end
  143.     end
  144.  
  145.  
  146.     local elevatorButton = mainFrame:addButton():setText("Call Elevator"):setPosition(2, 17):setSize(24, 3):onClickUp(onElevatorButtonClick);
  147.     local safeButton = mainFrame:addButton():setText("Open Safe"):setPosition(2, 13):setSize(24, 3):onClickUp(onSafeButtonClick)
  148.  
  149.     parent[elevatorButton] = mainFrame;
  150.     parent[safeButton] = mainFrame;
  151.    
  152.     mainFrame:addPane():setSize(24, 10):setPosition(2,2)
  153.     mainFrame:addLabel():setText("Furnace Status: "):setPosition(3, 3):setForeground(colors.white)
  154.     local furnaceStatusLabel = mainFrame:addLabel():setText("Offline"):setPosition(18, 3):setForeground(colors.red);
  155.     mainFrame:addLabel():setText("Trade Stations: "):setPosition(3,4):setForeground(colors.white);
  156.     mainFrame:addLabel():setText("Online"):setPosition(18, 4):setForeground(colors.green);
  157.  
  158.     mainFrame:addLabel():setText("Elevator: "):setPosition(3,9):setForeground(colors.white);
  159.     local elevatorStatusLabel = mainFrame:addLabel():setText("Offline"):setPosition(13, 9):setForeground(colors.red);
  160.     mainFrame:addLabel():setText("Safe: "):setPosition(3,10):setForeground(colors.white);
  161.     local safeStatusLabel = mainFrame:addLabel():setText("Offline"):setPosition(9, 10):setForeground(colors.red);
  162.  
  163.     local function setupButtonColoring(self, event, button, x, y)
  164.         self:onClick(function()
  165.             if locked[parent[self]] == false then
  166.             self:setBackground(colors.blue)
  167.             self:setForeground(colors.white)
  168.             end
  169.         end)
  170.         self:onClickUp(function()
  171.             self:setBackground(colors.cyan)
  172.             self:setForeground(colors.white)
  173.         end)
  174.         self:onLoseFocus(function()
  175.             self:setBackground(colors.cyan)
  176.             self:setForeground(colors.white)
  177.         end)
  178.     end
  179.     --Gets All the data from each api server.
  180.     function getDataFromAPIs()
  181.         local apiProtocol = "API";
  182.  
  183.         local furnaceAddress = rednet.lookup(apiProtocol, "FurnaceSystem")
  184.         local elevatorAddress = rednet.lookup(apiProtocol, "elevator");
  185.         local safeAddress = rednet.lookup("SAFE", "safeController");
  186.  
  187.         while true do
  188.             --Get Furnace Data
  189.             local tempFurnaceData = "Offline";
  190.             for i=0,5,1 do
  191.                 if furnaceAddress == nil then local furnaceAddress = rednet.lookup(apiProtocol, "FurnaceSystem") end
  192.                 if furnaceAddress ~= nil then
  193.                     rednet.send(furnaceAddress, "getFurnaceData", apiProtocol);
  194.                     local from, message = rednet.receive(apiProtocol, 1);
  195.                     if message ~= nil then
  196.                         tempFurnaceData = message;
  197.                         break;
  198.                     end
  199.                 else
  200.                         break;
  201.                 end
  202.             end
  203.             api["FurnaceSystem"] = tempFurnaceData;
  204.             --Get Elevator Data
  205.             local tempElevatorData = "Offline";
  206.             for i=0,5,1 do
  207.                 if elevatorAddress == nil then local elevatorAddress = rednet.lookup(apiProtocol, "elevator"); end
  208.                 if elevatorAddress ~= nil then
  209.                     rednet.send(elevatorAddress, "getData", apiProtocol);
  210.                     local from, message = rednet.receive(apiProtocol);
  211.                     if message ~= nil and from == elevatorAddress then
  212.                         tempElevatorData = message;
  213.                         break;
  214.                     end
  215.                 else
  216.                     break;
  217.                 end
  218.             end
  219.             api["ElevatorData"] = tempElevatorData;
  220.             --Get Safe Data
  221.             local tempSafeData = "Offline";
  222.             for i=0,5,1 do
  223.                 if safeAddress == nil then local safeAddress = rednet.lookup("SAFE", "safeController"); end
  224.                 if safeAddress ~= nil then
  225.                     rednet.send(safeAddress, "getDoorStatus", "SAFE");
  226.                     local from, message = rednet.receive("SAFE", 1);
  227.                     if message ~= nil and from == safeAddress then
  228.                         tempSafeData = message;
  229.                         break;
  230.                     end
  231.                 else
  232.                     break;
  233.                 end
  234.             end
  235.             api["SafeData"] = tempSafeData;
  236.             sleep(0);
  237.         end
  238.     end
  239.    
  240.     function updateScreen()
  241.         while true do
  242.             --Print Furnace System Status
  243.             if api["FurnaceSystem"] == nil then api["FurnaceSystem"] = "Offline"; end
  244.             if api["FurnaceSystem"] == "Offline" then
  245.                 furnaceStatusLabel:setText("Offline"):setForeground(colors.red);
  246.             elseif api["FurnaceSystem"] == "Partially Online" then
  247.                 furnaceStatusLabel:setText("Partial"):setForeground(colors.yellow);
  248.             elseif api["FurnaceSystem"] == "Online" then
  249.                 furnaceStatusLabel:setText("Online"):setForeground(colors.green);
  250.             end
  251.             --Print Elevator System Status
  252.             if api["ElevatorData"] == nil then api["ElevatorData"] = "Offline"; end
  253.             if api["ElevatorData"] == "Offline" then
  254.                 elevatorStatusLabel:setText("Offline"):setForeground(colors.red);
  255.             elseif api["ElevatorData"] == "Idle" then
  256.                 elevatorStatusLabel:setText("Idle"):setForeground(colors.orange);
  257.             elseif api["ElevatorData"] == "Lowering..." then
  258.                 elevatorStatusLabel:setText("Lowering..."):setForeground(colors.green);
  259.             elseif api["ElevatorData"] == "Waiting..." then
  260.                 elevatorStatusLabel:setText("Waiting..."):setForeground(colors.green);
  261.             elseif api["ElevatorData"] == "Raising..." then
  262.                 elevatorStatusLabel:setText("Raising..."):setForeground(colors.green);
  263.             elseif api["ElevatorData"] == "Starting Up" then
  264.                 elevatorStatusLabel:setText("Starting Up"):setForeground(colors.orange);
  265.             end
  266.  
  267.             --Print Safe System Status
  268.             if api["SafeData"] == "Offline" then
  269.                 safeStatusLabel:setText("Offline"):setForeground(colors.red);
  270.             elseif api["SafeData"] == false then
  271.                 safeStatusLabel:setText("Locked"):setForeground(colors.red);
  272.             elseif api["SafeData"] == true then
  273.                 safeStatusLabel:setText("Unlocked"):setForeground(colors.green);
  274.             end
  275.             sleep(0);
  276.         end
  277.     end
  278.  
  279.     setupButtonColoring(elevatorButton)
  280.     setupButtonColoring(safeButton)
  281.  
  282.     parallel.waitForAll(basalt.autoUpdate, getDataFromAPIs, updateScreen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement