Switched out some small things in the Core.lua system

This commit is contained in:
2025-05-06 13:01:40 +02:00
parent f8da3dce2e
commit 515e982eca

View File

@@ -1,5 +1,6 @@
-- Websocker: Used for connection between the controle webserver and Units(computers) -- Websocker: Used for connection between the controle webserver and Units(computers)
local ws Core = {};
Core.__index = Core;
function Core:connect() function Core:connect()
ws, err = assert(http.websocket("ws://localhost:8080")); ws, err = assert(http.websocket("ws://localhost:8080"));
@@ -9,6 +10,8 @@ function Core:connect()
print(err); print(err);
term.setTextColor(colors.white); term.setTextColor(colors.white);
end end
self.ws = ws;
end end
function Core:send(package) function Core:send(package)
@@ -16,23 +19,23 @@ function Core:send(package)
return false, "Missing package"; return false, "Missing package";
end end
if not ws then if not self.ws then
return false, "Websocket not initiated"; return false, "Websocket not initiated";
end end
ws.send(textutils.serialiseJSON(package)); self.ws:send(textutils.serialiseJSON(package));
end end
function Core:read() function Core:read()
if not ws then if not self.ws then
return false, "Websocket not initiated"; return false, "Websocket not initiated";
end end
return true, textutils.unserialiseJSON(ws.read()) return true, textutils.unserialiseJSON(self.ws:read())
end end
function Core:download(file) function Core:download(file)
if not ws then if not self.ws then
return false, "Websocket not initiated"; return false, "Websocket not initiated";
end end
@@ -60,5 +63,21 @@ function Core:download(file)
local success, data = self:read(); local success, data = self:read();
if not success then
return false, "Failed to read: "+data;
end
if not data.type then
return false, "Invalid package: "+textutils.serialiseJSON(data);
end
if not data.type == "FILE_DATA" then
return false, "Invalid type: "+textutils.serialiseJSON(data)
end
local fileStream = io.open(file, "w");
fileStream:write(data.fileContent);
fileStream:close();
return true, "Success"
end end