Advertisement
XileHorizon

lumia test 5

Feb 21st, 2025 (edited)
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. -- numeric_relays.lua
  2.  
  3. -- 1) Map the incoming number string to a specific relay and side
  4. local numberToRelay = {
  5.   ["1"] = { relayName = "redstone_relay_0", side = "front" },
  6.   ["2"] = { relayName = "redstone_relay_1", side = "front" },
  7.   ["3"] = { relayName = "redstone_relay_2", side = "front" },
  8.   ["4"] = { relayName = "redstone_relay_3", side = "front" },
  9.   ["5"] = { relayName = "redstone_relay_4", side = "front" },
  10.   ["6"] = { relayName = "redstone_relay_5", side = "front" }
  11. }
  12.  
  13. -- (Adjust these names and sides to match your actual setup)
  14.  
  15. -- 2) Turn off all relays at startup
  16. local function turnOffAll()
  17.   for _, info in pairs(numberToRelay) do
  18.     local relay = peripheral.wrap(info.relayName)
  19.     if relay then
  20.       relay.setOutput(info.side, false)
  21.     end
  22.   end
  23. end
  24.  
  25. turnOffAll()
  26.  
  27. -- Keep track of the currently active number so we can turn it off
  28. local currentNumber = nil
  29.  
  30. local function activateNumber(newNumber)
  31.   -- If there's a current number active, turn that off first
  32.   if currentNumber and numberToRelay[currentNumber] then
  33.     local oldInfo = numberToRelay[currentNumber]
  34.     local oldRelay = peripheral.wrap(oldInfo.relayName)
  35.     if oldRelay then
  36.       oldRelay.setOutput(oldInfo.side, false)
  37.     end
  38.   end
  39.  
  40.   -- Turn on the newly received number's relay
  41.   local info = numberToRelay[newNumber]
  42.   if info then
  43.     local relay = peripheral.wrap(info.relayName)
  44.     if relay then
  45.       relay.setOutput(info.side, true)
  46.       currentNumber = newNumber
  47.       print("Activated number: "..newNumber)
  48.     else
  49.       print("No relay found for: "..newNumber)
  50.     end
  51.   else
  52.     print("Unknown number: "..newNumber)
  53.   end
  54. end
  55.  
  56. -- 3) Connect to the Node.js WebSocket server
  57. local wsUrl = "ws://kev.unoriginal.studio:8080"
  58. print("Connecting to "..wsUrl)
  59. http.websocketAsync(wsUrl)
  60.  
  61. -- 4) Event loop for handling WebSocket messages
  62. while true do
  63.   local event, p1, p2, p3 = os.pullEvent()
  64.  
  65.   if event == "websocket_success" then
  66.     local ws = p2
  67.     print("Connected to the server!")
  68.     -- Optionally send a message from CC
  69.     ws.send("Hello from CC!")
  70.  
  71.   elseif event == "websocket_failure" then
  72.     print("Failed to connect:", p2)
  73.     break
  74.  
  75.   elseif event == "websocket_message" then
  76.     -- p2 = ws handle, p3 = the text message
  77.     local incoming = p3   -- e.g. "1", "2", ...
  78.     print("Received: "..incoming)
  79.     activateNumber(incoming)  -- pass it directly, so "1" stays "1" (string)
  80.  
  81.   elseif event == "websocket_closed" then
  82.     print("WebSocket closed.")
  83.     break
  84.   end
  85. end
  86.  
  87. print("Exiting program.")
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement