-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
405 lines (304 loc) · 10.4 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* IoT Hub Raspberry Pi NodeJS - Microsoft Sample Code - Copyright (c) 2017 - Licensed MIT
*/
'use strict';
var piface = {};
try {
piface = require('piface-node-12');
}
catch(e) {
piface.init = function() {};
piface.digital_read = function(input) {
return fauxInputs[input];
};
piface.digital_write = function(output,state) {
fauxInputs[output] = state;
}
}
const Client = require('azure-iot-device').Client;
const ConnectionString = require('azure-iot-device').ConnectionString;
const Message = require('azure-iot-device').Message;
const Protocol = require('azure-iot-device-mqtt').Mqtt;
// DPS and connection stuff
const iotHubTransport = require('azure-iot-device-mqtt').Mqtt;
var ProvisioningTransport = require('azure-iot-provisioning-device-mqtt').Mqtt;
var SymmetricKeySecurityClient = require('azure-iot-security-symmetric-key').SymmetricKeySecurityClient;
var ProvisioningDeviceClient = require('azure-iot-provisioning-device').ProvisioningDeviceClient;
var provisioningHost = 'global.azure-devices-provisioning.net';
const keypress = require('keypress');
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
console.log('got "keypress"', key.ctrl + ' ' + key.name + ' ' + key.ch);
if (key && key.ctrl && key.name == 'c') {
process.exit();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
const INPUT_COUNT = 8;
const BASE_INPUT = 1;
const BUTTON_COUNT = 4;
const OUTPUT_COUNT = 8;
const BASE_OUTPUT = 1;
var client;
var config;
var connect;
var sendingMessage = true;
var fauxInputs = [0, 0, 0, 0, 0, 0, 0, 0];
var lastInputs = [undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined];
function updateInputStatus(telemetry) {
if (!sendingMessage) { return; }
var rawMessage = JSON.stringify(telemetry);
var message = new Message(rawMessage);
if (config.infoOutboundMessages)
console.info('Sending Input Status Update to Azure IoT Hub');
if (config.debugOutboundMessages)
console.debug(rawMessage);
client.sendEvent(message, (err) => {
if (err) {
console.error('Failed to send message to Azure IoT Hub');
} else {
if (config.infoOutboundMessages)
console.info('Message sent to Azure IoT Hub');
}
});
}
function onSetOutput(request, response) {
sendingMessage = true;
var pin = request.payload.pin;
var state = request.payload.state;
if (config.infoMethods)
console.info("SetOutput : pin = " + pin + " state " + state);
if (pin !== undefined && pin >= BASE_OUTPUT && pin <= OUTPUT_COUNT) {
var digitalState = 1;
if (state == false || state == 0)
digitalState = 0;
setOutput(pin, digitalState);
response.send(200, 'Successfully set output ' + pin + ' to ' + digitalState, function (err) {
if (err) {
console.error('Unable to respond to SetOutput method request');
}
});
}
else {
response.send(400, 'Invalid pin : must be between 1 and 8', function (err) {
if (err) {
console.error('Unable to respond to SetOutput method request');
}
});
}
}
function readAllInputs() {
var inputs = [];
var input;
for (input = BASE_INPUT; input <= INPUT_COUNT; input++) {
var state = piface.digital_read(input-1);
inputs[input-1] = state;
}
return inputs;
}
function onPulseOutput(request, response) {
sendingMessage = true;
var pin = request.payload.pin;
var duration = request.payload.duration;
if (config.infoMethods)
console.info("PulseOutput : pin = " + pin + " duration " + duration);
if (pin !== undefined && pin >= BASE_OUTPUT && pin <= OUTPUT_COUNT) {
pulseOutput(pin, duration);
response.send(200, 'Successfully pulsed output ' + pin, function (err) {
if (err) {
console.error('Unable to respond to PulseOutput method request');
}
});
}
else {
response.send(400, 'Invalid pin : must be between 1 and 8', function (err) {
if (err) {
console.error('Unable to respond to PulseOutput method request');
}
});
}
}
function onBlinkOutput(request, response) {
sendingMessage = true;
var pin = request.payload.pin;
var duration = request.payload.duration;
var count = request.payload.count;
if (config.infoMethods)
console.info("BlinkOutput : pin = " + pin + " duration " + duration + " count " + count);
if (pin !== undefined && pin >= BASE_OUTPUT && pin <= OUTPUT_COUNT) {
blinkOutput(pin, duration, count);
response.send(200, 'Successfully blinked Output ' + pin, function (err) {
if (err) {
console.error('Unable to respond to BlinkOutput method request');
}
});
}
else {
response.send(400, 'Invalid pin : must be between 1 and 8', function (err) {
if (err) {
console.error('Unable to respond to BlinkOutput method request');
}
});
}
}
function onStart(request, response) {
if (config.infoMethods)
console.info('Try to invoke method start(' + request.payload || '' + ')');
sendingMessage = true;
response.send(200, 'Successully start sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function onStop(request, response) {
if (config.infoMethods)
console.info('Try to invoke method stop(' + request.payload || '' + ')')
sendingMessage = false;
response.send(200, 'Successully stop sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function onReceiveMessage(msg) {
var message = msg.getData().toString('utf-8');
client.complete(msg, () => {
if (config.infoInboundMessages)
console.info('Incoming Message Received');
if (config.debugInboundMessages)
console.debug(message);
});
}
function blinkOutput(pin, duration, count) {
if (duration == undefined || isNaN(duration) || duration < 0 || duration > 60000)
duration = 500;
if (count == undefined || isNaN(count) || count < 0 || count > 60)
count = 1;
var currentCount = 0;
// Always run once
pulseOutput(pin, duration);
++currentCount;
if (currentCount < count) {
var intervalID = setInterval(function () {
pulseOutput(pin, duration);
++currentCount;
if (currentCount >= count)
clearInterval(intervalID);
}, 2 * duration);
}
}
function pulseOutput(pin, duration) {
if (duration == undefined || isNaN(duration) || duration < 0 || duration > 60000)
duration = 500;
setOutput(pin, 1);
setTimeout(function () {
setOutput(pin, 0);
}, duration);
}
function setOutput(pin, state) {
piface.digital_write(pin-1, state);
}
function initBindings() {
// set C2D callback
client.onDeviceMethod('start', onStart);
client.onDeviceMethod('stop', onStop);
client.on('message', onReceiveMessage);
// Init device methods
client.onDeviceMethod('SetOutput', onSetOutput);
client.onDeviceMethod('PulseOutput', onPulseOutput);
client.onDeviceMethod('BlinkOutput', onBlinkOutput);
}
function initLogic() {
// Setup input polling
setInterval(() => {
var changed = false;
var inputs = readAllInputs();
var telemetry = {};
var input;
for (input = BASE_INPUT; input <= INPUT_COUNT; input++) {
if(lastInputs[input-1] == undefined) {
telemetry["input" + input] = inputs[input-1].toString();
lastInputs[input-1] = inputs[input-1];
changed = true;
}
else {
if(lastInputs[input-1] !== inputs[input-1]) {
lastInputs[input-1] = inputs[input-1];
changed = true;
telemetry["input" + input] = inputs[input-1].toString();
if(input <= BUTTON_COUNT) {
if(inputs[input-1] === 0) {
telemetry["button" + input + "released"] = "Released";
}
else {
telemetry["button" + input + "pressed"] = "Pressed";
}
}
}
}
}
if(changed) {
updateInputStatus(telemetry);
}
}, config.interval);
}
function initDevice() {
// set up wiring
piface.init();
}
function initClient() {
// Start the device (connect it to Azure IoT Central).
try {
var provisioningSecurityClient = new SymmetricKeySecurityClient(connect.deviceId, connect.symmetricKey);
var provisioningClient = ProvisioningDeviceClient.create(provisioningHost, connect.idScope, new ProvisioningTransport(), provisioningSecurityClient);
provisioningClient.register((err, result) => {
if (err) {
console.log('error registering device: ' + err);
} else {
console.log('registration succeeded');
console.log('assigned hub=' + result.assignedHub);
console.log('deviceId=' + result.deviceId);
var connectionString = 'HostName=' + result.assignedHub + ';DeviceId=' + result.deviceId + ';SharedAccessKey=' + connect.symmetricKey;
client = Client.fromConnectionString(connectionString, iotHubTransport);
client.open((err) => {
if (err) {
console.error('[IoT hub Client] Connect error: ' + err.message);
return;
}
else {
console.log('[IoT hub Client] Connected Successfully');
}
initBindings();
initLogic();
});
}
});
}
catch(err) {
console.log(err);
}
}
// Read in configuration from config.json
try {
config = require('./config.json');
} catch (err) {
config = {};
console.error('Failed to load config.json: ' + err.message);
return;
}
// Read in connection details from connect.json
try {
connect = require('./connect.json');
} catch (err) {
connect = {};
console.error('Failed to load connect.json: ' + err.message);
return;
}
// Perform any device initialization
initDevice();
// Initialize Azure IoT Client
initClient();