Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local userID = nil;
- local computerName = nil;
- local isAdvanced = nil;
- local isTurtle = nil;
- local leftItem = nil
- local rightItem = nil;
- function InputHandler()
- write("Enter User ID: ");
- userID = read();
- write("Enter Computer Name: ");
- computerName = read();
- end
- function TurtleHandler()
- isTurtle = turtle ~= nil;
- if (isTurtle) then
- local emptySlot = nil;
- for i = 1, 16 do
- local item = turtle.getItemDetail(i);
- if (not item) then emptySlot = i break end;
- end
- if (emptySlot) then
- turtle.equipLeft();
- local left = turtle.getItemDetail(emptySlot);
- if (left) then
- turtle.equipLeft();
- turtle.leftItem = left.name;
- end
- turtle.equipRight();
- local right = turtle.getItemDetail(emptySlot);
- if (right) then
- turtle.equipRight();
- turtle.rightItem = right.name;
- end
- end
- end
- end
- function Main()
- local url ="http://192.168.0.69:5050/api/computers";
- local headers = { ["content-type"]="application/json" };
- local body = {};
- body.userID = userID;
- body.computerName = computerName;
- body.isTurtle = isTurtle;
- body.leftItem = turtle and turtle.leftItem;
- body.rightItem = turtle and turtle.rightItem;
- body.isAdvanced = term.isColor();
- local res, error, errorRes = http.post(url, textutils.serialiseJSON(body), headers);
- local resBody;
- if (res) then
- resBody = textutils.unserialiseJSON(res.readAll());
- local id = resBody["computerID"];
- if (id) then
- settings.set("computerID", id);
- settings.save(".settings");
- end
- elseif (errorRes) then
- resBody = textutils.unserialiseJSON(errorRes.readAll());
- end
- if (resBody) then
- if (resBody.message) then
- print(resBody.message);
- end
- end
- end
- function WriteStartup()
- local startup = fs.open("/startup.lua", "w");
- startup.write([[local ws;
- local function connectWS()
- ws = http.websocket("ws://localhost:5050", {["computerID"]=settings.get("computerID")});
- if (ws) then print("Connnected to controller.")
- else print("WebSocket not available.") end
- end
- connectWS();
- while true do
- if (ws) then
- print("Listening for message.")
- local status, msg = pcall(ws.receive);
- if (status) then
- if (msg ~= nil) then
- print("Received message, executing. " .. msg);
- local success, error = pcall(loadstring(msg))
- if (success) then
- ws.send('{"success": true}')
- print("Execution succesful.")
- else
- print("Execution failed, error: \n" .. error)
- error = error:gsub('"', "'")
- ws.send('{"success": false, "error": "'.. error ..'"}')
- end
- end
- else
- connectWS();
- os.sleep(5)
- end
- else
- connectWS();
- os.sleep(5)
- end
- end
- ws.close();]]);
- startup.close();
- os.reboot();
- end
- parallel.waitForAll(InputHandler, TurtleHandler);
- Main();
- WriteStartup();
Add Comment
Please, Sign In to add comment