Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component");
- local modem = component.modem;
- local event = require("event");
- local serialization = require("serialization");
- local thread = require("thread");
- local network = {};
- network.failedAttempts = 0;
- network.maxFailedAttempts = 3;
- network.STATUS_FLAG_DISCONNECTED = -1;
- network.STATUS_FLAG_IDLE = 1;
- network.STATUS_FLAG_BUILDING = 2;
- network.robotData = {};
- local networkThread;
- function network.init(robotAddress, tryAttempts, waitTimeout)
- print("Initiating Network Handler");
- if tryAttempts == nil then
- tryAttempts =5;
- end
- if waitTimeout == nil then
- waitTimeout = 1;
- end
- modem.open(60);
- network.robotAddress = robotAddress;
- network.status = network.STATUS_FLAG_DISCONNECTED;
- network.tryAttempts = tryAttempts;
- network.waitTimeout = waitTimeout;
- networkThread = thread.create(network.handler);
- return networkThread;
- end
- function network.send(message1, message2, message3)
- modem.send(network.robotAddress, 60, message1, message2, message3);
- end
- function network.recieve()
- local _, _, _, _, _, message1, message2, message3 = event.pull(network.waitTimeout, "modem_message", nil, network.robotAddress);
- return message1, message2, message3;
- end
- function network.requestRobotData()
- local robotData;
- local gotData = false;
- for i = 1, network.tryAttempts do
- network.send("getData");
- local response, data = network.recieve();
- if response == "heres data" then
- robotData = serialization.unserialize(data);
- gotData = true;
- break;
- end
- end
- return gotData, robotData;
- end
- function network.handler()
- print("Network handler started");
- local shouldLoop = true;
- local count = 0;
- --Begin by attempting to connect to the robot
- print("&1Attempting to connect to robot...")
- local gotData, data = network.requestRobotData();
- if gotData == true then
- network.robotData = data;
- network.status = network.STATUS_FLAG_IDLE;
- print("&5Connected to robot!");
- else
- print("&eRobot seems to be down. Will still try to connect in the background.");
- while true do
- local gotData, data = network.requestRobotData();
- if gotData == true then
- network.robotData = data;
- network.status = network.STATUS_FLAG_IDLE;
- print("&5Connected to robot!");
- break;
- end
- end
- end
- shouldLoop = true;
- while shouldLoop do
- if network.status == network.STATUS_FLAG_IDLE then
- local gotData, data = network.requestRobotData();
- if gotData then
- network.robotData = data;
- network.failedAttempts = 0;
- else
- if network.failedAttempts == network.maxFailedAttempts then
- print("&eIt appears that the robot is offline.");
- network.status = network.STATUS_FLAG_DISCONNECTED;
- end
- network.failedAttempts = network.failedAttempts + 1;
- end
- os.sleep(5);
- elseif network.status == network.STATUS_FLAG_DISCONNECTED then
- local gotData, data = network.requestRobotData();
- if gotData then
- print("&5Robot is connected!")
- network.robotData = data;
- network.failedAttempts = 0;
- network.status = network.STATUS_FLAG_IDLE;
- end
- os.sleep(1);
- end
- end
- end
- function network.findRobot()
- local shouldLoop = true;
- modem.open(60);
- while shouldLoop do
- modem.broadcast(60, "WhoIs", "CM_Auto_Robot");
- local _, _, from, _, _, message = event.pull(1, "modem_message", nil, nil, nil, nil, "I am!");
- if message == "I am!" then
- print("Located potential robot. Attempting to pair.")
- for i=1, 5 do
- modem.send(from, 60, "Confirmed. I am server.");
- _, _, _, _, _, message = event.pull(1, "modem_message", nil, from, nil, nil, "Confirmed. Communications locked.");
- if message == "Confirmed. Communications locked." then
- network.robotAddress = from;
- shouldLoop = false;
- break;
- end
- end
- end
- end
- return network.robotAddress;
- end
- return network;
Advertisement
Add Comment
Please, Sign In to add comment