cool-iot – Blame information for rev 6

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  
2 office 46 message = message.toString()
5 office 47 winston.info('Received message: ' + message)
2 office 48 if(!(message in config.GPIO)) {
49 winston.warn('Request to toggle unknown GPIO: ' + message)
50 return;
51 }
52  
53 var pin = config.GPIO[message]
54 if(pin === -1) {
55 winston.warn('GPIO pin for "' + message + '" is not configured')
56 return;
57 }
58  
59 winston.info('Toggling pin ' + pin + ' for ' + message)
60 COOL[message].writeSync(1)
61 setTimeout(function() {
62 winston.info('Toggled pin ' + pin + ' for ' + message)
63 COOL[message].writeSync(0)
64 }, 1000)
65 })