alexatts – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env nodejs
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')
2 office 12 const picoSpeaker = require('pico-speaker')
1 office 13  
14 // Load configuration file.
2 office 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
24 picoSpeaker.init(picoConfig)
25  
1 office 26 // Generate GPIO pins for configuration.
2 office 27 var ATTS = {};
1 office 28 for(var i in config.GPIO) {
29 if(!config.GPIO.hasOwnProperty(i))
30 continue;
31  
32 if(config.GPIO[i] === -1)
33 continue;
34  
2 office 35 ATTS[i] = new Gpio(config.GPIO[i], 'out')
1 office 36 }
37  
38 // Set up logger.
39 winston.add(winston.transports.File, {filename: config.log})
40  
41 // Initiate connection to MQTT.
42 const client = mqtt.connect(config.mqtt.url, {queueQoSZero: false})
43  
44 client.on('connect', function () {
45 winston.info('Connected to MQTT server')
46 client.subscribe(config.mqtt.topic)
47 })
48  
49 client.on('message', function (topic, message) {
50 if(message.length === 0)
51 return;
52  
53 // Remove any retained message.
54 client.publish(topic, "", {retain: true})
55  
56 message = message.toString()
57 winston.info('Received message: ' + message)
2 office 58  
59 ATTS["ptt"].write(1, (err) => {
1 office 60 if(err) {
2 office 61 winston.err('Unable to press push-to-talk button: ' + err.message)
1 office 62 return;
63 }
2 office 64  
65 // Send the message.
66 picoSpeaker.speak(config.alexa + ", Simon says, " + message).then(function() {
67 winston.info('Message ' + message + ' sent to Alexa.')
68  
69 ATTS["ptt"].write(0)
70 }.bind(this));
1 office 71 })
72 })