-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
264 lines (226 loc) · 6.54 KB
/
app.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const koa = require("koa");
const router = require("koa-router");
const websockify = require('koa-websocket');
const json = require("koa-json");
const bodyParser = require("koa-bodyparser");
const PoweredUP = require("node-poweredup");
const app = new koa();
const http = router();
const ws = router();
const poweredUP = new PoweredUP.PoweredUP();
const socket = websockify(app);
app.use(json());
app.use(bodyParser());
const COLORS = {
off: 0,
pink: 1,
purple: 2,
blue: 3,
"light-blue": 4,
cyan: 5,
green: 6,
yellow: 7,
orange: 8,
red: 9,
white: 10
};
const HUBTYPES = {
0: "Unknown",
1: "WeDo2 Smart Hub",
2: "Boost Move Hub",
3: "Powered Up Hub",
4: "Powered Up Remote",
5: "Duplo Train Hub"
};
const DEVICETYPES = {
0: "Unknown",
1: "Basic Motor",
2: "Train Motor",
8: "Led Lights",
22: "Boost Led",
34: "WeDo2 Tilt",
35: "WeDo2 Distance",
37: "Boost Distance",
38: "Boost Tacho Motor",
39: "Boost Move Hub Motor",
40: "Boost Tilt",
41: "Duplo Train Base Motor",
42: "Duplo Train Base Speaker",
43: "Duplo Train Base Color",
44: "Duplo Train Base Speedmeter",
55: "Powered Up Remote Button"
};
const PORTS = ["A", "B"];
poweredUP.scan();
console.log("Looking for Hubs...");
poweredUP.on("discover", async (hub) => {
await hub.connect();
console.log(`Connected to ${hub.name} (${hub.uuid})`);
hub.on("disconnect", () => {
console.log(`Hub ${hub.name} (${hub.uuid}) disconnected`);
})
});
http.get("/hubs/", hubs);
function hubInfo(hub) {
const { uuid, batteryLevel, firmwareVersion, current, name, rssi } = hub;
const hubTypeId = hub.getHubType();
const hubType = { name: HUBTYPES[hubTypeId], id: hubTypeId };
let ports = [];
if (hubTypeId != 4) {
PORTS.forEach(port => {
const deviceType = hub.getPortDeviceType(port);
ports.push({ port: port, name: DEVICETYPES[deviceType], id: deviceType });
});
}
const data = {
uuid,
batteryLevel,
firmwareVersion,
current,
name,
rssi,
hubType,
ports
};
return data;
}
async function hubs(ctx) {
const connectedHubs = poweredUP.getConnectedHubs();
let hubs = [];
connectedHubs.forEach(hub => {
hubs.push(hubInfo(hub));
});
ctx.body = { hubs: hubs };
await ctx;
}
http.get("/hubs/:uuid/", hub);
async function hub(ctx) {
const { uuid } = ctx.params;
const { name } = ctx.query;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
if (name) {
hub.setName(name);
}
ctx.body = hubInfo(hub);
await ctx;
}
http.get("/hubs/:uuid/disconnect", hubDisconnect);
async function hubDisconnect(ctx) {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
hub.disconnect();
ctx.body = hubInfo(hub);
await ctx;
}
http.get("/hubs/:uuid/connect", hubConnect);
async function hubConnect(ctx) {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
hub.connect();
ctx.body = hubInfo(hub);
await ctx;
}
http.get("/hubs/:uuid/shutdown", shutdown);
async function shutdown(ctx) {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
hub.shutdown();
ctx.body = hubInfo(hub);
await ctx;
}
http.get("/hubs/:uuid/:port/speed/:speed", speedControl);
async function speedControl(ctx) {
const { uuid, port, speed } = ctx.params;
const { time } = ctx.query;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
const deviceType = hub.getPortDeviceType(port);
ctx.assert(deviceType === 2, 422, "Motor not found on this port");
hub.setMotorSpeed(port, speed, parseInt(time));
ctx.body = { uuid, port, speed, time };
await ctx;
}
http.get("/hubs/:uuid/:port/rampspeed/:fromSpeed/:toSpeed/:time",
rampSpeedControl
);
async function rampSpeedControl(ctx) {
const { uuid, port, fromSpeed, toSpeed, time } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
const deviceType = hub.getPortDeviceType(port);
ctx.assert(deviceType === 2, 422, "Motor not found on this port");
hub.rampMotorSpeed(port, fromSpeed, toSpeed, parseInt(time));
ctx.body = { uuid, port, fromSpeed, toSpeed, time };
await ctx;
}
http.get("/hubs/:uuid/stop", motorStop);
async function motorStop(ctx) {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
PORTS.forEach(port => {
const deviceType = hub.getPortDeviceType(port);
if (deviceType === 2) {
hub.brakeMotor(port);
}
});
ctx.body = { uuid};
await ctx;
}
http.get("/hubs/:uuid/:port/stop", motorStopPort);
async function motorStopPort(ctx) {
const { uuid, port } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
const deviceType = hub.getPortDeviceType(port);
ctx.assert(deviceType === 2, 422, "Motor not found on this port");
hub.hardStopMotor(port);
ctx.body = { uuid, port };
await ctx;
}
http.get("/hubs/:uuid/led/:color/", LEDcolorChange);
async function LEDcolorChange(ctx) {
const { uuid, color } = ctx.params;
const colorValue = COLORS[color];
ctx.assert(colorValue, 422, "Wrong color!");
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.assert(hub, 404, "Hub is not connected!");
hub.setLEDColor(colorValue);
ctx.body = { hub_uuid: uuid, color_value: colorValue };
await ctx;
}
ws.get('/:uuid/sensor/color', async (ctx) => {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.websocket.on('message', (message) => {
hub.on("color", async (port, color) => {
ctx.websocket.send(color);
});
});
});
ws.get('/:uuid/sensor/distance', async (ctx) => {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.websocket.on('message', (message) => {
hub.on("distance", async (port, distance) => {
ctx.websocket.send(distance);
});
});
});
ws.get('/:uuid/sensor/color-distance', async (ctx) => {
const { uuid } = ctx.params;
hub = poweredUP.getConnectedHubByUUID(uuid);
ctx.websocket.on('message', (message) => {
hub.on("colorAndDistance", async (port, color, distance) => {
ctx.websocket.send(distance);
ctx.websocket.send(color);
});
});
});
app.use(http.routes()).use(http.allowedMethods());
app.ws.use(ws.routes()).use(ws.allowedMethods());
app.listen(3000, () => console.log("Server started..."));