Advertisement
_DudeWhat_

Refined Storage Bridge

Jan 10th, 2022 (edited)
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. local rs = peripheral.find("rsBridge")
  2. if not rs then error("No RS Bridge attached!") end
  3.  
  4. local function chunkString(value, chunkSize)
  5.     if not chunkSize then chunkSize = 10000 end
  6.     local length = value:len()
  7.     local total = math.ceil(length / chunkSize)
  8.     local chunks = {}
  9.     local i = 1
  10.     for i=1,total do
  11.         local pos = 1 + ((i - 1) * chunkSize)
  12.         chunks[i] = value:sub(pos, pos + chunkSize - 1)
  13.     end
  14.     return total, chunks
  15. end
  16.  
  17. local function main()
  18.     print("Connecting to the socket server...")
  19.     local socket, reason = http.websocket("ws://ws.cnml.de:8080")
  20.     if not socket then error("Socket server could not be reached: "..reason) end
  21.     print("Connection successful!")
  22.     socket.send("{\"role\":\"rs\"}")
  23.  
  24.     local function handleRequest(parsed)
  25.         if parsed.method == "energyusage" then
  26.             return true, tostring(rs.getEnergyUsage())
  27.         elseif parsed.method == "energystorage" then
  28.             return true, tostring(rs.getEnergyStorage())
  29.         elseif parsed.method == "listitems" then
  30.             return true, textutils.serializeJSON(rs.listItems())
  31.         elseif parsed.method == "listfluids" then
  32.             return true, textutils.serializeJSON(rs.listFluids())
  33.         end
  34.        
  35.         return false, ("Invalid method: "..parsed.method.."!")
  36.     end
  37.  
  38.     local function sendResponse(id, result)
  39.         local total, chunks = chunkString(result)
  40.         for i, chunk in pairs(chunks) do
  41.             socket.send(textutils.serializeJSON({ id = id, result = chunk, chunk = i, total = total }))
  42.         end
  43.     end
  44.  
  45.     local function handleMessage(message)
  46.         local parsed, reason = textutils.unserializeJSON(message)
  47.         if not parsed then print("Deserialization failed: "..reason..", in messsage "..message) return end
  48.        
  49.         if parsed.type == "request" then
  50.             local success, result = handleRequest(parsed)
  51.             if success then
  52.                 sendResponse(parsed.id, result)
  53.             else
  54.                 printError("Error when answering", parsed.method, "method:", result)
  55.             end
  56.             return
  57.         end
  58.        
  59.         printError("No handler for messages of type", parsed.type)
  60.     end
  61.  
  62.     while true do
  63.         local message, binary = socket.receive(1)
  64.         if not not message and not binary then
  65.             handleMessage(message)
  66.         end
  67.     end
  68. end
  69.  
  70. while true do
  71.     local status, err = pcall(main)
  72.     if not status then
  73.         printError("Uncaught error:", err)
  74.         print("Sleeping for 5 seconds and retrying!")
  75.         sleep(5)
  76.     end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement