-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (70 loc) · 1.83 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import dgram from 'node:dgram'
const args = process.argv.slice(2);
const shouldDebug = args.includes('--debug');
let client;
const PORT = 8889;
const DEBUG_PORT = 8890;
const connect = async () => {
client = dgram.createSocket("udp4");
client.bind(shouldDebug ? DEBUG_PORT : PORT);
};
const close = () => {
client.close()
}
const commandResult = (message) => {
return new Promise((resolve) => {
client.once('error', function (e) {
throw e;
});
client.once('message', (msg, info) => {
console.log(msg.toString());
resolve(msg.toString());
});
client.send(message, 0, message.length, PORT, HOST, function (err, bytes) {
if (err) throw err;
});
});
};
//sending msg
async function sendCommand(commandStr) {
try {
console.log(`Sending command: ${commandStr}`);
const result = await commandResult(commandStr);
console.log('Resolved to ' + result + ' for command ' + commandStr);
if (result.includes('error')) {
throw commandErr;
}
return result;
} catch (err) {
throw err;
}
}
async function main() {
const commands = [
'command',
'takeoff',
//'left 30',
//‘cw 90’,
//‘forward 100’,
//'cw 90',
//'down 40', // beginpad
//'forward 320',
// ‘forward 500’,
//'cw 90',
//'up 70',
//'forward 100',
//'cw 90',
//'forward 350',
'land',
];
await connect()
let delay = 1000;
// let commandCode, commandTimeEst;
for (const command of commands) {
await sendCommand(command);
// await Promise(reswolve => setTimeout(resolve, delay))
}
close(); //give time for last command to finish
}
main();
//connect()