alexatts – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 1 Rev 2
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 const picoSpeaker = require('pico-speaker')
12 13
13 // Load configuration file. 14 // Load configuration file.
-   15 const config = YAML.load('config.yml')
-   16  
-   17 // Define configuration for pico TTS.
-   18 var picoConfig = {
-   19 AUDIO_DEVICE: config.card,
-   20 LANGUAGE: config.language
-   21 };
-   22  
-   23 // Initialize with config
14 const config = YAML.load('config.yml'); 24 picoSpeaker.init(picoConfig)
15 25  
16 // Generate GPIO pins for configuration. 26 // Generate GPIO pins for configuration.
17 var COOL = {}; 27 var ATTS = {};
18 for(var i in config.GPIO) { 28 for(var i in config.GPIO) {
19 if(!config.GPIO.hasOwnProperty(i)) 29 if(!config.GPIO.hasOwnProperty(i))
20 continue; 30 continue;
21 31
22 if(config.GPIO[i] === -1) 32 if(config.GPIO[i] === -1)
23 continue; 33 continue;
24 34
25 COOL[i] = new Gpio(config.GPIO[i], 'out') 35 ATTS[i] = new Gpio(config.GPIO[i], 'out')
26 } 36 }
27 37
28 // Set up logger. 38 // Set up logger.
29 winston.add(winston.transports.File, {filename: config.log}) 39 winston.add(winston.transports.File, {filename: config.log})
30 40
31 // Initiate connection to MQTT. 41 // Initiate connection to MQTT.
32 const client = mqtt.connect(config.mqtt.url, {queueQoSZero: false}) 42 const client = mqtt.connect(config.mqtt.url, {queueQoSZero: false})
33 43
34 client.on('connect', function () { 44 client.on('connect', function () {
35 winston.info('Connected to MQTT server') 45 winston.info('Connected to MQTT server')
36 client.subscribe(config.mqtt.topic) 46 client.subscribe(config.mqtt.topic)
37 }) 47 })
38 48
39 client.on('message', function (topic, message) { 49 client.on('message', function (topic, message) {
40 if(message.length === 0) 50 if(message.length === 0)
41 return; 51 return;
42 52
43 // Remove any retained message. 53 // Remove any retained message.
44 client.publish(topic, "", {retain: true}) 54 client.publish(topic, "", {retain: true})
45 -  
46 message. 55
47 message = message.toString() 56 message = message.toString()
48 winston.info('Received message: ' + message) -  
49 if(!(message in config.GPIO)) { -  
50 winston.warn('Request to toggle unknown GPIO: ' + message) -  
51 return; -  
52 } 57 winston.info('Received message: ' + message)
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) 58  
61 COOL[message].write(1, (err) => { 59 ATTS["ptt"].write(1, (err) => {
62 if(err) { 60 if(err) {
63 winston.err('Unable to toggle pin ' + pin + ' error message received is: ' + err.message) 61 winston.err('Unable to press push-to-talk button: ' + err.message)
64 return; 62 return;
65 } 63 }
66 64  
-   65 // Send the message.
67 setTimeout(function() { 66 picoSpeaker.speak(config.alexa + ", Simon says, " + message).then(function() {
-   67 winston.info('Message ' + message + ' sent to Alexa.')
68 winston.info('Toggled pin ' + pin + ' for ' + message) 68  
69 COOL[message].write(0) 69 ATTS["ptt"].write(0)
70 }, 1000) 70 }.bind(this));
71 }) 71 })
72 }) -  
73   72 })
74   73