-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (92 loc) · 2.78 KB
/
app.js
File metadata and controls
107 lines (92 loc) · 2.78 KB
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
var serialport = require('serialport');
var device = require('azure-iot-device');
var con = require('./connection.js');
var SerialPort = serialport.SerialPort;
var serials = [];
connection.iotclient.open(function(x) {
console.log(x);
});
//console.log('Detecting Arduinos ...');
/*
serialport.list(function (err, ports){
ports.forEach(function(port){
if(port.comName.indexOf("AMA") < 0){
var com = new SerialPort(port.comName, { baudrate: 9600});
serials.push({port: port.comName, com: com});
console.log('--' + port.manufacturer + '-' + port.comName + '-' + port.pnpId);
}
});
});
*/
/*
var ard01serial = new SerialPort('/dev/ttyUSB0', {
baudrate: 115200
});
var ard02serial = new SerialPort('/dev/ttyUSB1', {
baudrate: 115200
});*/
var ard03serial = new SerialPort('/dev/ttyUSB2', {
baudrate: 115200
});
/*
ard01serial.on('open', function() {
console.log('Connected to USB2...');
ard01serial.on('data', function(data) {
console.log('....USB1/DATA: ' + data);
try {
var payload = JSON.parse(data);
if (payload.number) {
ard01serial.write("ac:1,r:255,g:255,b:255,sp:0100,so:0\n", function(err, results) {
console.log('Error: ' + err);
});
}
var message = new device.Message(payload);
connection.iotclient.sendEvent(message, printResultFor('send'));
} catch (e){
console.log("Recieved code is no JSON!!");
}
});
});
ard02serial.on('open', function() {
console.log('Connected to USB2...');
ard02serial.on('data', function(data) {
console.log('....USB2/DATA: ' + data);
try {
var payload = JSON.parse(data);
if (payload.number) {
ard02serial.write("ac:1,r:255,g:255,b:255,sp:0100,so:0\n", function(err, results) {
console.log('Error: ' + err);
});
}
var message = new device.Message(payload);
connection.iotclient.sendEvent(message, printResultFor('send'));
} catch (e){
console.log("Recieved code is no JSON!!");
}
});
});
*/
ard03serial.on('open', function() {
console.log('Connected to USB2...');
ard03serial.on('data', function(data) {
console.log('....USB3/DATA: ' + data);
try {
var payload = JSON.parse(data);
if (payload.number) {
ard03serial.write("ac:1,r:255,g:255,b:255,sp:0100,so:0\n", function(err, results) {
console.log('Error: ' + err);
});
}
var message = new device.Message(payload);
connection.iotclient.sendEvent(message, printResultFor('send'));
} catch (e){
console.log("Recieved code is no JSON!!");
}
});
});
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(err + op);
if (res && (res.statusCode !== 204)) console.log(op + " " + res.statusCode + "." + res.statusMessage);
}
}