cool-iot – Diff between revs 2 and 5
?pathlinks?
Rev 2 | Rev 5 | |||
---|---|---|---|---|
- | 1 | #!/usr/bin/env nodejs |
||
1 | /////////////////////////////////////////////////////////////////////////// |
2 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 // |
3 | // Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
4 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
5 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
6 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
7 | |
|
7 | const Gpio = require('onoff').Gpio |
8 | const Gpio = require('onoff').Gpio |
|
8 | const mqtt = require('mqtt') |
9 | const mqtt = require('mqtt') |
|
9 | const YAML = require('yamljs'); |
10 | const YAML = require('yamljs'); |
|
10 | const winston = require('winston') |
11 | const winston = require('winston') |
|
11 | |
12 | |
|
12 | // Load configuration file. |
13 | // Load configuration file. |
|
13 | const config = YAML.load('config.yml'); |
14 | const config = YAML.load('config.yml'); |
|
14 | |
15 | |
|
15 | // Generate GPIO pins for configuration. |
16 | // Generate GPIO pins for configuration. |
|
16 | var COOL = {}; |
17 | var COOL = {}; |
|
17 | for(var i in config.GPIO) { |
18 | for(var i in config.GPIO) { |
|
18 | if(!config.GPIO.hasOwnProperty(i)) |
19 | if(!config.GPIO.hasOwnProperty(i)) |
|
19 | continue; |
20 | continue; |
|
20 | |
21 | |
|
21 | if(config.GPIO[i] === -1) |
22 | if(config.GPIO[i] === -1) |
|
22 | continue; |
23 | continue; |
|
23 | |
24 | |
|
24 | COOL[i] = new Gpio(config.GPIO[i], 'out') |
25 | COOL[i] = new Gpio(config.GPIO[i], 'out') |
|
25 | } |
26 | } |
|
26 | |
27 | |
|
27 | // Set up logger. |
28 | // Set up logger. |
|
28 | winston.add(winston.transports.File, {filename: 'cool-iot.log'}) |
29 | winston.add(winston.transports.File, {filename: config.log}) |
|
29 | |
30 | |
|
30 | // Initiate connection to MQTT. |
31 | // Initiate connection to MQTT. |
|
31 | const client = mqtt.connect(config.mqtt.url) |
32 | const client = mqtt.connect(config.mqtt.url) |
|
32 | |
33 | |
|
33 | client.on('connect', function () { |
34 | client.on('connect', function () { |
|
34 | winston.info('Connected to MQTT server') |
35 | winston.info('Connected to MQTT server') |
|
35 | client.subscribe(config.mqtt.topic) |
36 | client.subscribe(config.mqtt.topic) |
|
36 | //client.publish('presence', 'Hello mqtt') |
37 | //client.publish('presence', 'Hello mqtt') |
|
37 | }) |
38 | }) |
|
38 | |
39 | |
|
39 | client.on('message', function (topic, message) { |
40 | client.on('message', function (topic, message) { |
|
40 | // message is Buffer |
41 | // message is Buffer |
|
41 | message = message.toString() |
42 | message = message.toString() |
|
42 | winston.info('Received request: ' + message) |
43 | winston.info('Received message: ' + message) |
|
43 | if(!(message in config.GPIO)) { |
44 | if(!(message in config.GPIO)) { |
|
44 | winston.warn('Request to toggle unknown GPIO: ' + message) |
45 | winston.warn('Request to toggle unknown GPIO: ' + message) |
|
45 | return; |
46 | return; |
|
46 | } |
47 | } |
|
47 | |
48 | |
|
48 | var pin = config.GPIO[message] |
49 | var pin = config.GPIO[message] |
|
49 | if(pin === -1) { |
50 | if(pin === -1) { |
|
50 | winston.warn('GPIO pin for "' + message + '" is not configured') |
51 | winston.warn('GPIO pin for "' + message + '" is not configured') |
|
51 | return; |
52 | return; |
|
52 | } |
53 | } |
|
53 | |
54 | |
|
54 | winston.info('Toggling pin ' + pin + ' for ' + message) |
55 | winston.info('Toggling pin ' + pin + ' for ' + message) |
|
55 | COOL[message].writeSync(1) |
56 | COOL[message].writeSync(1) |
|
56 | setTimeout(function() { |
57 | setTimeout(function() { |
|
57 | winston.info('Toggled pin ' + pin + ' for ' + message) |
58 | winston.info('Toggled pin ' + pin + ' for ' + message) |
|
58 | COOL[message].writeSync(0) |
59 | COOL[message].writeSync(0) |
|
59 | }, 1000) |
60 | }, 1000) |
|
60 | }) |
61 | }) |
|
61 | |
62 | |