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