-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpiotest.js
107 lines (95 loc) · 2.4 KB
/
gpiotest.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
var exec = require('exec');
var dgram = require('dgram');
var os = require('os');
var before = true;
var networkinterfaces = [];
exec('raspi-gpio set 26 pu', function(a, b, c) {
setInterval(function() {
exec('raspi-gpio get 26', function(error, stdout, stderr) {
console.log(stdout);
if (stdout.search('level=1') > -1) {
leds(false);
} else {
leds(true);
}
});
}, 500);
});
function leds(on) {
console.log("-->" + on)
if (before == on) {
return;
}
before = on;
var dgramClient = dgram.createSocket('udp6');
var buf = new Buffer(630);
for (var i = 0; i < buf.length; i++) {
if (!on) {
buf[i] = 180;
} else {
buf[i] = 0;
}
}
console.log(buf);
sendLightCommand(dgramClient, buf, 0);
sendUdpMulticast(on);
}
function sendLightCommand(dgramClient, buf, counter) {
if (counter > 3) {
dgramClient.close();
return;
}
dgramClient.send(buf, 0, buf.length, 2812, '2a01:170:1112:0:bad8:12ff:fe66:fa1', function(err, bytes) {
if (err) {
throw err;
}
// Reuse the message buffer,
// or close client
sendLightCommand(dgramClient, buf, counter + 1);
});
}
function sendUdpMulticast(on) {
var interfaces = os.networkInterfaces();
networkinterfaces = [];
Object.keys(interfaces).forEach(function(ifname) {
console.log(ifname);
networkinterfaces.push(ifname);
});
buzz(on, 0);
}
function buzz(on, i) {
console.log("INTERAFACES:" + networkinterfaces.length);
var message = createMessage(on);
if (i < networkinterfaces.length) {
sendMessage(message, networkinterfaces[i], function() {
buzz(on, i + 1);
});
} else {
}
}
function sendMessage(message, networkinterface, callback) {
console.log(networkinterface);
var dgramServer = dgram.createSocket('udp6');
var destination = 'FF02::6006%' + networkinterface;
console.log(destination);
dgramServer.send(message, 0, message.length, 6006, destination, function(err, bytes) {
if (err) {
console.log("err when sending send UDP Message");
}
callback();
dgramServer.close();
console.log("server close");
});
}
function createMessage(on) {
var date = new Date();
var dateString = date.getTime();
var event = {
eventtime: dateString,
open: !on
};
var message = new Buffer(JSON.stringify(event));
//var message = new Buffer("door");
console.log(message);
return message;
}