-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
159 lines (148 loc) · 3.99 KB
/
server.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
var koa = require("koa");
var server = require("koa-static");
var SocketServer = require("socket.io");
var Drone = require("./lib/drone");
// env
require("./config");
var port = process.env.PORT || 8000;
var sockPort = process.env.SOCKPORT || 3000;
var droneUUID = process.env.DRONE;
// koa server
var app = koa();
// // logger
// app.use(function*(next) {
// var start = new Date;
// yield next;
// var ms = new Date - start;
// console.log("%s %s - %s", this.method, this.url, ms);
// });
// static files
app.use(server("./app/public", {}));
// response
app.use(function*() {
this.body = "Hello world from koa app server";
});
// Socket.io sockets server
app.socketServer = new SocketServer(app);
var drones = app.socketServer.of("/drones");
drones.on("connection", function(socket){
console.log("something connected to /drones");
// create/connect to drone
var drone = new Drone(droneUUID);
var droneSignalPoller;
socket.on("disconnect", function(){
console.log("socket disconnect");
// kill the signal polling timer on drone disconnect
clearInterval(droneSignalPoller);
drone.land(function() {
console.log("client disconnected, drone landed and about to disconnect");
drone.disconnect();
});
});
drone.connect();
// poll drone every few seconds and send signal updates
droneSignalPoller = setInterval(drone
.signalStrength, 7 * 1000, function(err, rssi) {
if(err) {
return err;
}
// emit drone data update on the callback exec
else {
// console.log("signal", rssi);
drones.emit("data", {signalStrength: rssi});
}
});
// allow the drone to send battery updates
drone.addBatteryEmitter(function(){
drones.emit("data", {batteryStatus: drone.status.battery});
});
// allow the drone to send status updates
drone.addStatusEmitter(function(){
drones.emit("data", {status: drone.status});
});
// send initial state to the client
drones.emit("data", {
// TODO: get UUID from an actual drone rather than what we use to
// connect to is
uuid: droneUUID,
status: drone.status,
flightOptions: drone.flightOptions
});
// provide feedback to the front-end that commands came in
socket.on("fly", function() {
drone.fly(function(statusData) {
drones.emit("data", {status: statusData});
});
drones.emit("command", "fly");
});
socket.on("land", function() {
drone.land(function(statusData) {
drones.emit("data", {status: statusData});
});
drones.emit("command", "land");
});
socket.on("faster", function() {
drone.faster();
drones.emit("command", "faster");
drones.emit("data", {flightOptions: drone.flightOptions});
});
socket.on("slower", function() {
drone.slower();
drones.emit("command", "slower");
drones.emit("data", {flightOptions: drone.flightOptions});
});
socket.on("longer", function() {
drone.longer();
drones.emit("command", "longer");
drones.emit("data", {flightOptions: drone.flightOptions});
});
socket.on("shorter", function() {
drone.shorter();
drones.emit("command", "shorter");
drones.emit("data", {flightOptions: drone.flightOptions});
});
socket.on("turnRight", function() {
drone.turnRight();
drones.emit("command", "turnRight");
});
socket.on("turnLeft", function() {
drone.turnLeft();
drones.emit("command", "turnLeft");
});
socket.on("up", function() {
drone.up();
drones.emit("command", "up");
});
socket.on("down", function() {
drone.down();
drones.emit("command", "down");
});
socket.on("forward", function() {
drone.forward();
drones.emit("command", "forward");
});
socket.on("back", function() {
drone.back();
drones.emit("command", "back");
});
socket.on("left", function() {
drone.left();
drones.emit("command", "left");
});
socket.on("right", function() {
drone.right();
drones.emit("command", "right");
});
socket.on("flip", function() {
drone.flip();
drones.emit("command", "flip");
});
});
if (require.main === module) {
app.listen(port);
app.socketServer.listen(sockPort);
console.log("server listening on port", port);
}
else {
module.exports = app;
}