-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (112 loc) · 2.76 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { device } from "aws-iot-device-sdk";
import { getDeviceList } from "usb";
const ML_DOWN = 0x01;
const ML_UP = 0x02;
const ML_LEFT = 0x04;
const ML_RIGHT = 0x08;
const ML_FIRE = 0x10;
const ML_STOP = 0x20;
const ML_UP_LEFT = ML_UP | ML_LEFT;
const ML_DOWN_LEFT = ML_DOWN | ML_LEFT;
const ML_UP_RIGHT = ML_UP | ML_RIGHT;
const ML_DOWN_RIGHT = ML_DOWN | ML_RIGHT;
const MISSILE_LAUNCHER_VENDOR_ID = 0x2123;
const MISSILE_LAUNCHER_ID = 0x1010;
const IOT_KEY_PATH = process.env.IOT_KEY_PATH;
const IOT_CERT_PATH = process.env.IOT_CERT_PATH;
const IOT_CLIENT_ID = process.env.MISSILE_LAUNCHER_IOT_CLIENT_ID;
const IOT_HOST = process.env.MISSILE_LAUNCHER_IOT_HOST;
const IOT_CA_PATH = "root-CA.crt";
const devices = getDeviceList().filter(
d =>
d.deviceDescriptor.idVendor == MISSILE_LAUNCHER_VENDOR_ID &&
d.deviceDescriptor.idProduct == MISSILE_LAUNCHER_ID
);
devices.forEach(d => {
console.log("Opening", d.deviceAddress);
d.open();
const interface = d.interfaces[0];
if (interface.isKernelDriverActive()) {
console.log("Detaching kernel driver", interface.descriptor.iInterface);
interface.detachKernelDriver();
}
});
const iotDevice = device({
keyPath: IOT_KEY_PATH,
certPath: IOT_CERT_PATH,
caPath: IOT_CA_PATH,
clientId: IOT_CLIENT_ID,
host: IOT_HOST
});
iotDevice.on("connect", function() {
iotDevice.subscribe("topic_1");
});
iotDevice.on("message", function(topic, payload) {
const action = payload.toString();
switch (action) {
case "left":
transfer(ML_LEFT);
break;
case "up":
transfer(ML_UP);
break;
case "right":
transfer(ML_RIGHT);
break;
case "down":
transfer(ML_DOWN);
break;
case "upLeft":
transfer(ML_UP_LEFT);
break;
case "downLeft":
transfer(ML_DOWN_LEFT);
break;
case "upRight":
transfer(ML_UP_RIGHT);
break;
case "downRight":
transfer(ML_DOWN_RIGHT);
break;
case "stop":
transfer(ML_STOP);
break;
case "fire":
transfer(ML_FIRE);
break;
}
console.log(topic, payload.toString());
});
function transfer(command) {
const devicePromises = devices.map(
d =>
new Promise((resolve, reject) => {
d.controlTransfer(
0x21,
0x09,
0,
0,
Buffer.from([0x02, command, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
(err, data) => {
if (!err) {
resolve(data);
} else {
reject(err);
}
}
);
})
);
Promise.all(devicePromises);
}
process.on("exit", _ => {
devices.forEach(d => {
console.log(
"Releaseing and resetting device",
d.busNumber,
d.deviceAddress
);
d.interfaces[0].release();
d.reset();
});
});