Advertisement
Mojokojo69

elevator server

Aug 27th, 2023 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2.  
  3. local state = "idle"
  4. local currentFloor = 1
  5. local lastUpDownTime = nil
  6. local computerID = os.getComputerID()
  7.  
  8. -- Configuration
  9. local configFileName = "elevator_server_config.txt"
  10. local config = {channel = "69", computerID = "0"}
  11.  
  12. -- Save Config to file
  13. local function saveConfig(config)
  14. local configData = textutils.serializeJSON(config)
  15. local file = fs.open(configFileName, "w")
  16. file.write(configData)
  17. file.close()
  18. end
  19.  
  20. -- Load Config from file
  21. local function loadConfig()
  22. if fs.exists(configFileName) then
  23. local file = fs.open(configFileName, "r")
  24. local configData = file.readAll()
  25. file.close()
  26. local loadedConfig = textutils.unserializeJSON(configData)
  27. config = loadedConfig
  28. end
  29. end
  30.  
  31.  
  32. local function controlElevator(direction)
  33. if direction == "up" or direction == "down" then
  34. lastUpDownTime = os.epoch('utc')
  35. end
  36.  
  37. if direction == "up" then
  38. redstone.setOutput("left", true)
  39. redstone.setOutput("right", false)
  40. elseif direction == "down" then
  41. redstone.setOutput("left", false)
  42. redstone.setOutput("right", false)
  43. elseif direction == "stop" then
  44. redstone.setOutput("left", false)
  45. redstone.setOutput("right", true)
  46. else
  47. print("Debug: Invalid direction")
  48. end
  49. end
  50.  
  51. local function stateMachine()
  52. print("Current state: " .. state)
  53. if state == "idle" then
  54. controlElevator("stop")
  55. elseif state == "up" then
  56. controlElevator("up")
  57. elseif state == "down" then
  58. controlElevator("down")
  59. else
  60. print("Debug: Invalid state")
  61. end
  62. end
  63.  
  64. local function handleModemMessages(config)
  65. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  66. if event == "modem_message" then
  67. if senderChannel == tonumber(config.channel) then
  68. if string.sub(message, 1, 5) == "idle:" then
  69. if lastUpDownTime == nil or (os.epoch('utc') - lastUpDownTime) >= 1000 then
  70. state = "idle"
  71. local newFloor = tonumber(string.sub(message, 6))
  72. print("Elevator floor: " .. newFloor)
  73. currentFloor = newFloor
  74. end
  75. else
  76. state = message
  77. end
  78. end
  79. end
  80. end
  81.  
  82. -- Main Loop
  83. local function mainLoop(config)
  84. modem.open(tonumber(config.channel))
  85. while true do
  86. handleModemMessages(config)
  87. stateMachine()
  88. end
  89. end
  90.  
  91. -- Initialization
  92. local function initializeClient()
  93. loadConfig()
  94. if not config.channel or config.channel == "" then
  95. print("Please enter the channel to use:")
  96. config.channel = tonumber(read())
  97. end
  98. if not config.computerID then
  99. config.computerID = computerID
  100. end
  101. saveConfig(config)
  102. mainLoop(config)
  103. end
  104.  
  105. -- Entry Point
  106. initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement