147 lines
2.9 KiB
Lua
147 lines
2.9 KiB
Lua
-- Websocker: Used for connection between the controle webserver and Units(computers)
|
|
Core = {};
|
|
Core.__index = Core;
|
|
|
|
function Core:new()
|
|
local self = setmetatable({}, Core);
|
|
return self;
|
|
end
|
|
|
|
function Core:connect()
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
ws, err = assert(http.websocket("ws://192.168.5.162:8080"));
|
|
|
|
if not ws then
|
|
term.setTextColor(colors.red);
|
|
print(err);
|
|
term.setTextColor(colors.white);
|
|
end
|
|
|
|
self.ws = ws;
|
|
end
|
|
|
|
function Core:send(package)
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
if not package then
|
|
return false, "Missing package";
|
|
end
|
|
|
|
if not self.ws then
|
|
return false, "Websocket not initiated";
|
|
end
|
|
|
|
self.ws.send(textutils.serialiseJSON(package));
|
|
end
|
|
|
|
function Core:read()
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
if not self.ws then
|
|
return false, "Websocket not initiated";
|
|
end
|
|
|
|
local data, success = self.ws.receive()
|
|
|
|
return true, textutils.unserialiseJSON(data)
|
|
end
|
|
|
|
function Core:download(file)
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
if not self.ws then
|
|
return false, "Websocket not initiated";
|
|
end
|
|
|
|
self:send({type = "REQUEST_FILE_INFO", file = file});
|
|
|
|
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_INFO") then
|
|
return false, "Invalid package type: "..textutils.serialiseJSON(data);
|
|
end
|
|
|
|
if not data.exists then
|
|
return false, "File is missing from server"
|
|
end
|
|
|
|
self:send({type = "REQUEST_FILE_DATA", file = file})
|
|
|
|
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
|
|
|
|
if fs.exists(file) then
|
|
fs.delete(file);
|
|
end
|
|
|
|
local fileStream = io.open(file, "w");
|
|
|
|
if not fileStream then
|
|
return false, "Failed to open fileStream"
|
|
end
|
|
|
|
fileStream:write(data.data);
|
|
fileStream:close();
|
|
|
|
return true, "Success"
|
|
end
|
|
|
|
function Core:tick()
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
if not self.ws then
|
|
return false, "Not initilized"
|
|
end
|
|
|
|
self:send({type = "TICK"});
|
|
|
|
local success, data = self:read();
|
|
|
|
if not success then
|
|
return false, "Failed to do anything: "..data;
|
|
end
|
|
|
|
print("Server expects Action: "..textutils.serialiseJSON(data))
|
|
end
|
|
|
|
function Core:disconnect()
|
|
if not self then
|
|
error("No instance!")
|
|
end
|
|
|
|
if not self.ws then
|
|
return "Not initilized"
|
|
end
|
|
|
|
self.ws.close();
|
|
end |