/
index.js
64 lines (52 loc) · 1.32 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
var util = require('util');
var bleno = require('bleno');
var path = require('path');
var mqtt = require('mqtt');
var BlenoPrimarySerivce = bleno.PrimaryService;
var TopicCharacteristic = require('./topicCharacteristic');
var config;
var cwd = process.cwd();
var args = process.argv;
if (args) {
if (args[0] == '-c') {
if (args[1].indexOf('/') === 0) {
config = require(args[1]);
} else {
config = require(path.join(cwd, args[1]));
}
} else {
config = require(path.join(cwd, 'config.js'));
}
}
if (!config) {
console.log("Must provide a config.js");
process.exit();
}
var client = mqtt.connect(config.broker);
var topicCharacteristic = new TopicCharacteristic(config);
client.on('connect', function(){
client.subscribe(config.topic);
});
client.on('message',function(topic, message){
console.log(message.toString());
topicCharacteristic.update(message);
});
bleno.on('stateChange', function(state){
if (state === 'poweredOn') {
bleno.startAdvertising('mqtt', ['ba42561bb1d2440a8d040cefb43faece']);
} else {
bleno.stopAdvertising();
}
});
bleno.on('advertisingStart', function(error){
if(!error) {
bleno.setServices([
new BlenoPrimarySerivce({
uuid: 'ba42561bb1d2440a8d040cefb43faece',
characteristics: [
topicCharacteristic
]
})
]);
}
});