Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Floor Controller
- --Version 2.5
- local updateCode = "4pCENLV4";
- local elevatorNumberFile = fs.open("elevatorNumber.dat", "r"); --Gets the elevator number from file.
- local elevatorNumber = tonumber(elevatorNumberFile.readAll());
- elevatorNumberFile.close();
- local floorNumberFile = fs.open("floorNumber.dat", "r"); --Gets the floor number from file.
- local floorNumber = tonumber(floorNumberFile.readAll());
- floorNumberFile.close();
- local hostname = "floor_controller" .. floorNumber; --Ex: elevator1_controller1;
- os.setComputerLabel(hostname);
- local protocol = "ELEVATOR" .. elevatorNumber;
- local stopOnThisFloor = false;
- local elevatorControllerAddress;
- term.clear();
- term.setCursorPos(1,1);
- local uptime = 0;
- local doorStatus = true;
- function formatTime()
- local days = math.floor(uptime/86400)
- local hours = math.floor((uptime % 86400)/3600)
- local minutes = math.floor((uptime % 3600)/60)
- local seconds = math.floor((uptime % 60))
- return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
- end
- function log(inputText)
- print("[" .. formatTime(uptime) .. "] " .. inputText);
- end
- function update()
- shell.run("pastebin", "get", updateCode, "download");
- if fs.exists("download") then
- fs.delete("startup.lua");
- fs.copy("download", "startup.lua");
- fs.delete("download");
- return true;
- else
- return false;
- end
- end
- function send(address, message)
- rednet.send(address, message, protocol);
- end
- --Controller Functions
- function openDoor()
- redstone.setOutput("front", false);
- end
- function closeDoor()
- redstone.setOutput("front", true);
- end
- function ding() --Ding the bell
- redstone.setOutput("back", true);
- sleep(0.3);
- redstone.setOutput("back", false)
- end
- function checkElevator() --Check to see if elevator is on floor.
- return redstone.getInput("left");
- end
- --Looping Functions
- function countTime() --Function keeping track of uptime.
- while true do
- uptime = uptime + 1;
- sleep(1);
- end
- end
- function clean()
- log("Resetting Floor.")
- if checkElevator() then
- stopOnThisFloor = true;
- lock();
- else
- stopOnThisFloor = false;
- unlock();
- end
- end
- function lock()
- log("Locking Elevator.");
- redstone.setOutput("right", true);
- sleep(0.5);
- ding();
- sleep(0.3);
- openDoor();
- if elevatorControllerAddress ~= nil then
- send(elevatorControllerAddress, "locked");
- end
- end
- function unlock()
- log("Unlocking Elevator.")
- ding();
- sleep(0.3);
- closeDoor();
- sleep(1);
- redstone.setOutput("right", false);
- end
- function network()
- rednet.open("top");
- rednet.host(protocol, hostname);
- if fs.exists("elevatorControllerAddress.dat") then --gets the elevator controller address from file.
- local file = fs.open("elevatorControllerAddress.dat", "r");
- elevatorControllerAddress = tonumber(file.readAll());
- file.close();
- log("Loaded Elevator Controller Address.")
- else
- while true do
- log("Unable to find Elevator Controller Address Saved...Searching...");
- while true do
- elevatorControllerAddress = rednet.lookup(protocol, "elevator_controller");
- if elevatorControllerAddress ~= nil then
- log("Found.");
- local file = fs.open("elevatorControllerAddress.dat", "w");
- file.write(elevatorControllerAddress);
- file.close();
- break;
- end
- end
- end
- end
- while true do
- local from, message = rednet.receive(protocol);
- if message == "status" then
- send(from, true);
- elseif message == "doorStatus" then
- send(from, doorStatus);
- elseif message == "stop on floor" then
- log("Requested to stop elevator on floor. Accepting.")
- ding();
- send(from, true);
- stopOnThisFloor = true;
- elseif message == "no longer on floor" then
- log("Requested to release elevator. Accepting.")
- send(from, true);
- stopOnThisFloor = false;
- elseif message == "is on floor?" then
- log("Requested information on floor. Sending.")
- send(from, checkElevator());
- elseif message == "update" then
- send(from, true);
- log("Recieved command to update. Updating.");
- if update() then
- send(from, true);
- os.reboot();
- else
- send(from, false);
- end
- elseif message == "reset" then
- send(from, true);
- clean();
- elseif message == "get door status" then
- send(from, stopOnThisFloor);
- elseif message == "restart" then
- send(from, true);
- os.reboot();
- end
- end
- end
- function main()
- clean();
- while true do
- if stopOnThisFloor then
- if checkElevator() then --Checks to see if elevator is on this floor
- if redstone.getOutput("right") == false then
- log("Elevator On Floor. Locking");
- lock(); --Lock the elevator.
- end
- else
- if redstone.getOutput("right") == true then
- log("Unlocking Elevator because not on floor yet.")
- unlock();
- end
- end
- else
- if redstone.getOutput("right") == true then
- log("Elevator set to move. Unlocking");
- unlock(); --If Elevator is Not on this floor, but it needs to be on this floor, unlock it.
- end
- end
- sleep(0);
- end
- end
- parallel.waitForAll(network, main, countTime);
Advertisement
Add Comment
Please, Sign In to add comment