diff --git a/core.lua b/core.lua index 476f50d..891e559 100644 --- a/core.lua +++ b/core.lua @@ -1,5 +1,6 @@ -- Websocker: Used for connection between the controle webserver and Units(computers) -local ws +Core = {}; +Core.__index = Core; function Core:connect() ws, err = assert(http.websocket("ws://localhost:8080")); @@ -9,6 +10,8 @@ function Core:connect() print(err); term.setTextColor(colors.white); end + + self.ws = ws; end function Core:send(package) @@ -16,23 +19,23 @@ function Core:send(package) return false, "Missing package"; end - if not ws then + if not self.ws then return false, "Websocket not initiated"; end - ws.send(textutils.serialiseJSON(package)); + self.ws:send(textutils.serialiseJSON(package)); end function Core:read() - if not ws then + if not self.ws then return false, "Websocket not initiated"; end - return true, textutils.unserialiseJSON(ws.read()) + return true, textutils.unserialiseJSON(self.ws:read()) end function Core:download(file) - if not ws then + if not self.ws then return false, "Websocket not initiated"; end @@ -60,5 +63,21 @@ function Core:download(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 + + local fileStream = io.open(file, "w"); + fileStream:write(data.fileContent); + fileStream:close(); + + return true, "Success" end \ No newline at end of file