Advertisement
Corbinhol

Elevator Call Button

Apr 3rd, 2023 (edited)
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --Elevator Call Button
  2. --Version 2.2
  3. local updateCode = "7EusNXVa";
  4.  
  5. local elevatorNumberFile = fs.open("elevatorNumber.dat", "r"); --Gets the elevator number from file.
  6. local elevatorNumber = tonumber(elevatorNumberFile.readAll());
  7. elevatorNumberFile.close();
  8.  
  9. local floorNumberFile = fs.open("floorNumber.dat", "r"); --Gets the floor number from file.
  10. local floorNumber = tonumber(floorNumberFile.readAll());
  11. floorNumberFile.close();
  12.  
  13. local protocol = "ELEVATOR" .. elevatorNumber;
  14. local hostname = "callbutton" .. floorNumber;
  15. local elevatorController;
  16.  
  17. local uptime = 0;
  18. while true do
  19.     rednet.open("bottom");
  20.     elevatorController = rednet.lookup(protocol, "elevator_controller");
  21.     if elevatorController ~= nil then
  22.         break;
  23.     end
  24. end
  25.  
  26. term.clear();
  27. term.setCursorPos(1,1);
  28.  
  29.  
  30. function update()
  31.     shell.run("pastebin", "get", updateCode, "download");
  32.     if fs.exists("download") then
  33.         fs.delete("startup.lua");
  34.         fs.copy("download", "startup.lua");
  35.         fs.delete("download");
  36.         return true;
  37.     else
  38.         return false;
  39.     end
  40. end
  41.  
  42. local status = "idle";
  43.  
  44. function exit()
  45.     error("Exiting Program.")
  46. end
  47.  
  48. function formatTime() --Format seconds into readable time
  49.     local days = math.floor(uptime/86400)
  50.     local hours = math.floor((uptime % 86400)/3600)
  51.     local minutes = math.floor((uptime % 3600)/60)
  52.     local seconds = math.floor((uptime % 60))
  53.     return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
  54. end
  55.  
  56. function countTime() --Function keeping track of uptime.
  57.     while true do
  58.         uptime = uptime + 1;
  59.         sleep(1);
  60.     end
  61. end
  62.  
  63. function log(inputText) --print out message with timestamp
  64.     print("[" .. formatTime(uptime) .. "] " .. inputText);
  65. end
  66.  
  67. function alertLight()
  68.     redstone.setOutput("back", true);
  69.     sleep(.5);
  70.     redstone.setOutput("back", false);
  71.     sleep(.5);
  72. end
  73.  
  74. function mainLoop()
  75.     while true do
  76.         if status == "idle" then
  77.             if redstone.getInput("back") == true then
  78.                 status = "active";
  79.                 rednet.send(elevatorController, floorNumber, protocol);
  80.             end
  81.         end
  82.         sleep(.5);
  83.     end
  84. end
  85.  
  86. function network()
  87.     rednet.host(protocol, hostname)
  88.     log("Started.")
  89.     while true do
  90.         local from, message = rednet.receive(protocol);
  91.         if message == "status" then
  92.             log("Recieved Request for Status.")
  93.             rednet.send(from, true, protocol);
  94.         elseif message == "idle" then
  95.             log("Recieved Request to set to idle")
  96.             status = "idle";
  97.             rednet.send(from, true, protocol);
  98.         elseif message == "active" then
  99.             log("Recieved Request to set to active")
  100.             status = "active";
  101.             rednet.send(from, true, protocol);
  102.         elseif message == "update" then
  103.             rednet.send(from, true, protocol);
  104.             rednet.send(from, update(), protocol);
  105.             os.reboot();
  106.         elseif message == "restart" then
  107.             os.reboot();
  108.         end
  109.     end
  110. end
  111.  
  112. parallel.waitForAll(mainLoop, network, countTime);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement