cool-iot – Blame information for rev 7

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