-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (165 loc) · 5.45 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
var express = require('express');
var app = express();
/**
* Various vars
*/
var crypto = require('crypto');
var async = require('async');
var _json = require(__dirname + '/lib/escaped-json');
var bodyParser = require('body-parser');
/**
* Drone vars
*/
var drone_ip = '192.168.43.134';
var autonomy = require('ardrone-autonomy');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
/**
* This is the endpoint WC needs to post through a webhook to,
* when a new sale has been made.
*/
app.post('/fly', function (req, res) {
var webhookBody = req.body || {};
var webhookSignature = req.headers['x-wc-webhook-signature'];
console.log('Receiving webhook...');
if (!webhookBody || !webhookSignature) {
console.log('Access denied - invalid request');
return res.send('access denied', 'invalid request', null, 403);
}
async.parallel([
function (next ) {
console.log('thread 1... could do stuff like turn on lights/music');
return next();
}
], function (err) {
console.log('thread 2... doing the drone stuff');
var secret = 'drone'
var data = _json.stringify(webhookBody);
var signature = crypto.createHmac('sha256', secret).update(data).digest('base64');
// Check the webhook signature
if (webhookSignature !== signature) {
console.log('Access denied - invalid signature');
return res.send('access denied', 'invalid signature', null, 403);
}
console.log(webhookBody);
console.log('Webhook received successfully!');
var line_items = webhookBody.order.line_items;
var item1 = line_items[0];
/**
* Determine product position based on name.
* At the moment, position is how far RIGHT it should go.
* Alternatively the product could have some meta for it's coordinates.
*/
var item_position;
switch(item1.name) {
case "Indiana Jones":
item_position = 1;
break;
case "Jaws":
item_position = 2;
break;
case "Mario":
default:
item_position = 3;
break;
}
/**
* Determine the customer location by country.
*/
var country_position;
switch(webhookBody.order.billing_address.country) {
case "AU":
country_position = 1;
break;
case "US":
country_position = 2;
break;
case "ZA":
default:
country_position = 3;
break;
}
console.log('Drone taking off in 2.5 seconds...');
setTimeout(function() {
/**
* Plan the mission. Take flight. Go!
*/
var mission = autonomy.createMission({
ip: drone_ip
});
mission.takeoff()
.zero()
.hover(2500)
// Drone going to item now!
/**
* The drone can go too fast and not very accurate if
* given a large meter distance to travel, so we try
* split it up into actions of 1m.
*
* After going to the item, it will wait for a couple seconds
* and then land. The drone will wait there for 10 seconds,
* before taking off again and going to the customer.
*/
var i;
for (i=0; i < item_position; i++) {
// Drone going right 1m!
mission.right(1)
}
// Drone at item, continuing in 5 seconds!
mission.hover(5000)
// Drone going back to base/start now!
/**
* Go back to 'home'.
*/
var u;
for (u=0; u < item_position; u++) {
// Drone going to base 1m left now!
mission.left(1)
}
// Drone at base, going to go country now!
/**
* We determine customer location by country above,
* and go back that way. This could alternatively
* be determined by coordinates.
*/
var o;
for (o=0; o < country_position; o++) {
// Drone going left 1m!
mission.left(1)
}
// Drone at country, landing now!
mission.hover(2000)
.land();
// Drone landed and done!
// mission go!
var d = new Date();
mission.run(function (err, result) {
if (err) {
console.trace("Oops, something bad happened: %s", err.message);
mission.client().stop();
mission.client().land();
} else {
console.log("Mission success!");
process.exit(0);
}
});
}, 2500);
return res.json({
success: true
});
});
});
/**
* Home page.
*/
app.get('/', function (req, res) {
res.send("Bryce is flying!");
});
/**
* Start the server.
*/
var server = app.listen(80, function () {
var host = '127.0.0.1';
var port = '80';
console.log('WooDrone listening at http://%s:%s', host, port);
});