auto-update
This commit is contained in:
parent
4a4fe37e8e
commit
29a6fd486e
2 changed files with 79 additions and 8 deletions
|
@ -1,10 +1,13 @@
|
||||||
ipaddr = "68.46.126.104:48228"
|
local ipaddr = "68.46.126.104"
|
||||||
--ipaddr = "localhost:48228"
|
local port = "48228"
|
||||||
|
|
||||||
|
local endpoint = "http://" .. ipaddr .. ":" .. port
|
||||||
|
|
||||||
local idfile = fs.open("id", "r")
|
local idfile = fs.open("id", "r")
|
||||||
|
|
||||||
local id = nil
|
local id = nil
|
||||||
local command = nil
|
local command = nil
|
||||||
|
local backoff = 0;
|
||||||
|
|
||||||
if not idfile then
|
if not idfile then
|
||||||
local fuel = turtle.getFuelLevel()
|
local fuel = turtle.getFuelLevel()
|
||||||
|
@ -27,7 +30,7 @@ if not idfile then
|
||||||
}
|
}
|
||||||
-- TODO: get from boot floppy
|
-- TODO: get from boot floppy
|
||||||
local turtleinfo = http.post(
|
local turtleinfo = http.post(
|
||||||
"http://" .. ipaddr .. "/turtle/new",
|
endpoint .. "/turtle/new",
|
||||||
textutils.serializeJSON(info),
|
textutils.serializeJSON(info),
|
||||||
{ ["Content-Type"] = "application/json" }
|
{ ["Content-Type"] = "application/json" }
|
||||||
)
|
)
|
||||||
|
@ -44,4 +47,64 @@ else
|
||||||
idfile.close()
|
idfile.close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
repeat
|
||||||
print(command)
|
print(command)
|
||||||
|
if command == "Wait" then
|
||||||
|
sleep(5)
|
||||||
|
elseif command == "Forward" then
|
||||||
|
turtle.forward()
|
||||||
|
elseif command == "Backward" then
|
||||||
|
turtle.backward()
|
||||||
|
elseif command == "Left" then
|
||||||
|
turtle.left()
|
||||||
|
elseif command == "Right" then
|
||||||
|
turtle.right()
|
||||||
|
elseif command == "Update" then
|
||||||
|
local req = http.get(endpoint .. "/turtle/client.lua")
|
||||||
|
local update = req.readAll()
|
||||||
|
req.close()
|
||||||
|
local startup = fs.open("startup", "w")
|
||||||
|
startup.write(update)
|
||||||
|
startup.close()
|
||||||
|
os.reboot()
|
||||||
|
end
|
||||||
|
|
||||||
|
local ahead = "minecraft:air"
|
||||||
|
local above = "minecraft:air"
|
||||||
|
local below = "minecraft:air"
|
||||||
|
|
||||||
|
local a,b = turtle.inspect()
|
||||||
|
if a then
|
||||||
|
ahead = b.name
|
||||||
|
end
|
||||||
|
|
||||||
|
local a,b = turtle.inspectUp()
|
||||||
|
if a then
|
||||||
|
above = b.name
|
||||||
|
end
|
||||||
|
|
||||||
|
local a,b = turtle.inspectDown()
|
||||||
|
if a then
|
||||||
|
below = b.name
|
||||||
|
end
|
||||||
|
local info = {
|
||||||
|
fuel = turtle.getFuelLevel(),
|
||||||
|
ahead = ahead,
|
||||||
|
above = above,
|
||||||
|
below = below
|
||||||
|
}
|
||||||
|
|
||||||
|
local rsp = http.post(
|
||||||
|
endpoint .. "/turtle/update/" .. id,
|
||||||
|
textutils.serializeJSON(info),
|
||||||
|
{ ["Content-Type"] = "application/json" }
|
||||||
|
)
|
||||||
|
if rsp then
|
||||||
|
backoff = 0
|
||||||
|
command = textutils.unserialiseJSON(rsp.readAll())
|
||||||
|
else
|
||||||
|
print("C&C server offline, waiting " .. backoff .. " seconds")
|
||||||
|
sleep(backoff)
|
||||||
|
backoff = backoff + 1
|
||||||
|
end
|
||||||
|
until command == "Poweroff"
|
||||||
|
|
|
@ -78,8 +78,6 @@ type SharedControl = Arc<RwLock<ControlState>>;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Error> {
|
async fn main() -> Result<(), Error> {
|
||||||
println!("{}", names::Name::from_num(args().nth(1).unwrap().parse().unwrap()).to_str());
|
|
||||||
|
|
||||||
let state = match fs::File::open("state.json") {
|
let state = match fs::File::open("state.json") {
|
||||||
Ok(file) => {
|
Ok(file) => {
|
||||||
serde_json::from_reader(file)?
|
serde_json::from_reader(file)?
|
||||||
|
@ -97,6 +95,7 @@ async fn main() -> Result<(), Error> {
|
||||||
let serv = Router::new()
|
let serv = Router::new()
|
||||||
.route("/turtle/new", post(create_turtle))
|
.route("/turtle/new", post(create_turtle))
|
||||||
.route("/turtle/update/:id", post(command))
|
.route("/turtle/update/:id", post(command))
|
||||||
|
.route("/turtle/client.lua", get(client))
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:48228").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:48228").await.unwrap();
|
||||||
|
@ -114,7 +113,9 @@ async fn create_turtle(
|
||||||
let id = (turtles.len() + 1) as u32;
|
let id = (turtles.len() + 1) as u32;
|
||||||
turtles.push(Turtle::new(id, req.position, req.facing, req.fuel));
|
turtles.push(Turtle::new(id, req.position, req.facing, req.fuel));
|
||||||
|
|
||||||
Json(TurtleResponse {name: Name::from_num(id).to_str(), command: TurtleCommand::Wait})
|
println!("turt {id}");
|
||||||
|
|
||||||
|
Json(TurtleResponse {name: Name::from_num(id).to_str(), id, command: TurtleCommand::Wait})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn command(
|
async fn command(
|
||||||
|
@ -126,7 +127,7 @@ async fn command(
|
||||||
println!("{id}");
|
println!("{id}");
|
||||||
|
|
||||||
|
|
||||||
Json(TurtleCommand::Wait)
|
Json(TurtleCommand::Update)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
@ -142,6 +143,8 @@ enum TurtleCommand {
|
||||||
DigUp,
|
DigUp,
|
||||||
DigDown,
|
DigDown,
|
||||||
TakeInventory,
|
TakeInventory,
|
||||||
|
Update,
|
||||||
|
Poweroff,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
@ -161,5 +164,10 @@ struct TurtleRegister {
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct TurtleResponse {
|
struct TurtleResponse {
|
||||||
name: String,
|
name: String,
|
||||||
|
id: u32,
|
||||||
command: TurtleCommand,
|
command: TurtleCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn client() -> &'static str {
|
||||||
|
include_str!("../../client/client.lua")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue