forked from nheidloff/node-mqtt-for-anki-overdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
252 lines (223 loc) · 7.82 KB
/
controller.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
//------------------------------------------------------------------------------
// Copyright IBM Corp. 2016
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------
var config = require('./config-wrapper.js')();
var async = require('async');
var noble = require('noble');
var mqtt = require('mqtt');
var readline = require('readline');
var receivedMessages = require('./receivedMessages.js')();
var prepareMessages = require('./prepareMessages.js')();
var readCharacteristic;
var writeCharacteristic;
var car;
var lane;
var deviceId;
var accel = 12500;
config.read(process.argv[2], function(carId, startlane, mqttClient, deviceId) {
this.deviceId = deviceId;
console.log('Device: ' + deviceId);
if (!carId) {
console.log('Define carid in a properties file and pass in the name of the file as argv');
process.exit(0);
}
lane = startlane;
noble.startScanning();
setTimeout(function() {
noble.stopScanning();
}, 2000);
noble.on('discover', function(peripheral) {
if (peripheral.id === carId) {
noble.stopScanning();
var advertisement = peripheral.advertisement;
var serviceUuids = JSON.stringify(peripheral.advertisement.serviceUuids);
if(serviceUuids.indexOf("be15beef6186407e83810bd89c4d8df4") > -1) {
console.log('Car discovered. ID: ' + peripheral.id);
// if (mqttClient) {
// mqttClient.publish(subscriptionnamePrefix + deviceId + '/connected', '' + peripheral.id, function () {});
// }
car = peripheral;
setUp(car);
}
}
});
function setUp(peripheral) {
peripheral.on('disconnect', function() {
console.log('Car has been disconnected');
process.exit(0);
});
peripheral.connect(function(error) {
peripheral.discoverServices([], function(error, services) {
var service = services[0];
service.discoverCharacteristics([], function(error, characteristics) {
var characteristicIndex = 0;
async.whilst(
function () {
return (characteristicIndex < characteristics.length);
},
function(callback) {
var characteristic = characteristics[characteristicIndex];
async.series([
function(callback) {
if (characteristic.uuid == 'be15bee06186407e83810bd89c4d8df4') {
readCharacteristic = characteristic;
readCharacteristic.notify(true, function(err) {
});
characteristic.on('read', function(data, isNotification) {
receivedMessages.handle(data, mqttClient);
});
}
if (characteristic.uuid == 'be15bee16186407e83810bd89c4d8df4') {
writeCharacteristic = characteristic;
init(startlane);
// this characterstic doesn't seem to be used for receiving data
characteristic.on('read', function(data, isNotification) {
console.log('Data received - writeCharacteristic', data);
});
}
callback();
},
function() {
characteristicIndex++;
callback();
}
]);
},
function(error) {
}
);
});
});
});
}
mqttClient.on('error', function(err) {
console.error('MQTT client error ' + err);
mqttClient = null;
});
mqttClient.on('close', function() {
console.log('MQTT client closed');
mqttClient = null;
});
mqttClient.on('message', function(topic, message, packet) {
console.log("topic: " + topic);
var actions = topic.split('/');
if (actions.length == 4) {
var action = actions[3];
console.log("Action: " + action);
if (action == 'speed') {
invokeCommand("s " + message + " " + accel);
}
else if (action == 'changelane') {
invokeCommand("c " + message);
}
else if (action == 'quit') {
invokeCommand("q");
}
else if (action == 'ping') {
invokeCommand("ping");
}
else if (action == 'version') {
invokeCommand("ver");
}
else if (action == 'battery') {
invokeCommand("bat");
}
else if (action == 'light') {
// invokeCommand("l");
console.log("error in light command")
}
else if (action == 'lightpattern') {
// invokeCommand("lp");
console.log("error in lightpattern command")
}
else {
// console.log("something else happened");
}
}
});
});
function init(startlane) {
// turn on sdk and set offset
var initMessage = new Buffer(4);
initMessage.writeUInt8(0x03, 0);
initMessage.writeUInt8(0x90, 1);
initMessage.writeUInt8(0x01, 2);
initMessage.writeUInt8(0x01, 3);
writeCharacteristic.write(initMessage, false, function(err) {
if (!err) {
var initialOffset = 0.0;
if (startlane) {
if (startlane == '1') initialOffset = 68.0;
if (startlane == '2') initialOffset = 23.0;
if (startlane == '3') initialOffset = -23.0;
if (startlane == '4') initialOffset = -68.0;
if (startlane == '5') initialOffset = 0.0;
}
initMessage = new Buffer(6);
initMessage.writeUInt8(0x05, 0);
initMessage.writeUInt8(0x2c, 1);
initMessage.writeFloatLE(initialOffset, 2);
writeCharacteristic.write(initMessage, false, function(err) {
if (!err) {
console.log('Initialization was successful');
console.log('Enter a command: help, s (speed), c (change lane), e (end/stop), l (lights), lp (lights pattern), o (offset), sdk, ping, bat, ver, q (quit)');
}
else {
console.log('Initialization error');
}
});
}
else {
console.log('Initialization error');
}
});
}
function invokeCommand(cmd) {
var message = prepareMessages.format(cmd);
if (message) {
console.log("Command: " + cmd, message);
if (writeCharacteristic) {
writeCharacteristic.write(message, false, function(err) {
if (!err) {
//console.log('Command sent');
}
else {
console.log('Error sending command');
}
});
} else {
console.log('Error sending command');
}
}
}
var cli = readline.createInterface({
input: process.stdin,
output: process.stdout
});
cli.on('line', function (cmd) {
if (cmd == "help") {
console.log(prepareMessages.doc());
}
else {
invokeCommand(cmd);
}
});
process.stdin.resume();
function exitHandler(options, err) {
if (car) car.disconnect();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));