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