🕤

Code for Being Lazy

assistant.js
/////////////
//VARIABLES//
/////////////
var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://localhost', { port: 1883 });
var relay = require('./lightcontrol.js');
var fan = require('./fancontrol.js');
var everloop = require('./everloop.js');
var snipsUserName = 'vimarsh';
var wakeword = 'hermes/hotword/default/detected';
var sessionEnd = 'hermes/dialogueManager/sessionEnded';
var lightState = 'hermes/intent/'+snipsUserName+':lightVal';    
var fanRequest = 'hermes/intent/'+snipsUserName+':fanVal';
var TimeGet = 'hermes/intent/'+snipsUserName+':TimeSet';

//////////////
//ON CONNECT//
//////////////
client.on('connect', function() {
   console.log('Connected to Snips MQTT server\n');
   client.subscribe('hermes/hotword/default/detected');
   client.subscribe('hermes/dialogueManager/sessionEnded');
   client.subscribe(lightState);
   client.subscribe(fanRequest);
});
//////////////
//ON MESSAGE//
//////////////
client.on('message', function(topic,message) {
   var message = JSON.parse(message);
   switch(topic) {
       // * On Wakeword
       case wakeword:
           everloop.startWaiting();
           console.log('Wakeword Detected');
       break;
       // * On Light State Change
       case lightState:
           // Turn lights On/Off
           try{
               if (message.slots[0].rawValue === 'on'){
                   relay.lightsOn();
                   everloop.stopWaiting();
                   console.log('Lights On');
               }
               else{
                   relay.lightsOff();
                   everloop.stopWaiting();
                   console.log('Lights Off');
               }
           }
           // Expect error if `on` or `off` is not heard
           catch(e){
               console.log('Did not receive an On/Off state')
           }
       break;
       case fanRequest:
           // Turn fan On/half/Off
           try{
            if (message.slots[0].rawValue === 'full'){
                fan.fullOn();
                everloop.stopWaiting();
                console.log('Fan speed if full');
            }
            else if (message.slots[0].rawValue === 'half'){
                fan.halfOn();
                everloop.stopWaiting();
                console.log('Fan speed is half');
            }
            else{
                fan.Off();
                everloop.stopWaiting();
                console.log('Fan is Off');
            }
        }
        // Expect error if `on` or `off` is not heard
        catch(e){
            console.log('Did not receive any fan state')
        }
        break;
        case TimeGet:
           // set alarm time
           try{
               var timeval=message.slots[0].rawValue;
                client.publish('alarm', timeval);
                console.log('alarm set of ',timeval);
            }
        // Expect error if time is not heard
        catch(e){
            console.log('Did not receive any alarm time')
        }
        break;
       // * On Conversation End
       case sessionEnd:
           everloop.stopWaiting();
           console.log('Session Ended\n');
       break;
   }
});
 
 
everloop.js
/////////////
//VARIABLES//
/////////////
var zmq = require('zeromq');// Asynchronous Messaging Framework
var matrix_io = require('matrix-protos').matrix_io;// Protocol Buffers for MATRIX function
var matrix_ip = '127.0.0.1';// Local IP
var matrix_everloop_base_port = 20021;// Port for Everloop driver
var matrix_device_leds = 0;// Holds amount of LEDs on MATRIX device
var methods = {};// Declaration of method controls at the end
var waitingToggle = false;
var counter = 0;
///////////////////
//KEEP ALIVE PORT//
///////////////////
// Create a Pusher socket
var pingSocket = zmq.socket('push')
// Connect Pusher to Keep-alive port
pingSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 1));
// Send a single ping
pingSocket.send('');
//////////////
//ERROR PORT//
//////////////
// Create a Subscriber socket
var errorSocket = zmq.socket('sub');
// Connect Subscriber to Error port
errorSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 2));
// Connect Subscriber to Error port
errorSocket.subscribe('');
// On Message
errorSocket.on('message', function(error_message){
 console.log('Error received: ' + error_message.toString('utf8'));// Log error
});
////////////////////
//DATA UPDATE PORT//
////////////////////
// Create a Subscriber socket
var updateSocket = zmq.socket('sub');
// Connect Subscriber to Data Update port
updateSocket.connect('tcp://' + matrix_ip + ':' + (matrix_everloop_base_port + 3));
// Subscribe to messages
updateSocket.subscribe('');
// On Message
updateSocket.on('message', function(buffer){
 var data = matrix_io.malos.v1.io.EverloopImage.decode(buffer);// Extract message
 matrix_device_leds = data.everloopLength;// Save MATRIX device LED count
});
/////////////
//BASE PORT//
/////////////
// Create a Pusher socket
var configSocket = zmq.socket('push');
// Connect Pusher to Base Port
configSocket.connect('tcp://' + matrix_ip + ':' + matrix_everloop_base_port);
// Create an empty Everloop image
var image = matrix_io.malos.v1.io.EverloopImage.create();
setInterval(function(){
   // Turns off all LEDs
   if (waitingToggle == false) {
       for (var i = 0; i < matrix_device_leds; ++i) {
           // Set individual LED value
           image.led[i] = {
               red: 0,
               green: 0,
               blue: 0,
               white: 0
           };
       }
   };
   // Creates pulsing LED effect
   if (waitingToggle == true) {
       for (var i = 0; i < matrix_device_leds; ++i) {
           // Set individual LED value
           image.led[i] = {         
            red: (Math.round((Math.cos(counter) + 1) * 34) + 10),
            green: (Math.round((Math.sin(counter) + 1) * 78) + 10),
            blue: (Math.round((Math.sin(counter) + 1) * 100) + 10),// Math u$
            white: 0

           };
       }
   };
   counter = counter + 0.2;
   // Store the Everloop image in MATRIX configuration
   var config = matrix_io.malos.v1.driver.DriverConfig.create({
       'image': image
   });
   // Send MATRIX configuration to MATRIX device
   if(matrix_device_leds > 0) {
       configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(config).finish());
   };
},50);
///////////////////
//WAITING METHODS//
///////////////////
methods.startWaiting = function() {
   waitingToggle = true;
};
methods.stopWaiting = function() {
   waitingToggle = false;
};
module.exports = methods;// Export methods in order to make them avaialble to other files
 
 
fancontrol.js
/////////////
//VARIABLES//
/////////////
var zmq = require('zeromq');// Asynchronous Messaging Framework
var matrix_io = require('matrix-protos').matrix_io;// Protocol Buffers for MATRIX function
var matrix_ip = '127.0.0.1';// Local IP
var matrix_gpio_base_port = 20049;// Port for GPIO driver
var methods = {};// Declaration of method controls at the end
var fanPin = 7;// The GPIO pin connected to your fan's mosfet
///////////////////
//KEEP ALIVE PORT//
///////////////////
// Create a Pusher socket
var pingSocket = zmq.socket('push');
// Connect Pusher to Keep-alive port
pingSocket.connect('tcp://' + matrix_ip + ':' + (matrix_gpio_base_port + 1));
// Send initial ping
pingSocket.send('');
// Send ping & toggle pin value every 2 seconds
setInterval(function(){
   pingSocket.send('');// Send ping
}, 2000);
//////////////
//ERROR PORT//
//////////////
// Create a Subscriber socket
var errorSocket = zmq.socket('sub');
// Connect Subscriber to Error port
errorSocket.connect('tcp://' + matrix_ip + ':' + (matrix_gpio_base_port + 2));
// Connect Subscriber to Error port
errorSocket.subscribe('');
// On Message
errorSocket.on('message', function(error_message){
   console.log('Error received: ' + error_message.toString('utf8'));// Log error
});
/////////////
//BASE PORT//
/////////////
// Create a Pusher socket
var configSocket = zmq.socket('push');
// Connect Pusher to Base port
configSocket.connect('tcp://' + matrix_ip + ':' + matrix_gpio_base_port);
// Create driver configuration
var outputConfig = matrix_io.malos.v1.driver.DriverConfig.create({
 // Update rate configuration
   delayBetweenUpdates: 0.01,// 0.01 seconds between updates
   timeoutAfterLastPing: 3.0,// Stop sending updates 3 seconds after pings
   //GPIO Configuration
   gpio: matrix_io.malos.v1.io.GpioParams.create({
       pin: fanPin,// Use pin 7
       mode: matrix_io.malos.v1.io.GpioParams.EnumMode.OUTPUT,// Set as output mode
       value: 0// Set initial pin value as off
   })
});
////////////////////
//ON & OFF METHODS//
////////////////////
methods.fullOn = function() {
   outputConfig.gpio.value = 255;
   configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(outputConfig).finish());
   console.log("Fan is on full speed");
};
methods.halfOn = function() {
    outputConfig.gpio.value = 127;
    configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(outputConfig).finish());
    console.log("fan is at half speed");
 };
methods.Off = function() {
   outputConfig.gpio.value = 0;
   configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(outputConfig).finish());
   console.log("fan is off");
};
module.exports = methods;// Export methods in order to make them avaialble to other files
 
 
lightcontrol.js
/////////////
//VARIABLES//
/////////////
var zmq = require('zeromq');// Asynchronous Messaging Framework
var matrix_io = require('matrix-protos').matrix_io;// Protocol Buffers for MATRIX function
var matrix_ip = '127.0.0.1';// Local IP
var matrix_gpio_base_port = 20049;// Port for GPIO driver
var methods = {};// Declaration of method controls at the end
var relayPin = 0;// The GPIO pin connected to your relay
///////////////////
//KEEP ALIVE PORT//
///////////////////
// Create a Pusher socket
var pingSocket = zmq.socket('push');
// Connect Pusher to Keep-alive port
pingSocket.connect('tcp://' + matrix_ip + ':' + (matrix_gpio_base_port + 1));
// Send initial ping
pingSocket.send('');
// Send ping & toggle pin value every 2 seconds
setInterval(function(){
   pingSocket.send('');// Send ping
}, 2000);
//////////////
//ERROR PORT//
//////////////
// Create a Subscriber socket
var errorSocket = zmq.socket('sub');
// Connect Subscriber to Error port
errorSocket.connect('tcp://' + matrix_ip + ':' + (matrix_gpio_base_port + 2));
// Connect Subscriber to Error port
errorSocket.subscribe('');
// On Message
errorSocket.on('message', function(error_message){
   console.log('Error received: ' + error_message.toString('utf8'));// Log error
});
/////////////
//BASE PORT//
/////////////
// Create a Pusher socket
var configSocket = zmq.socket('push');
// Connect Pusher to Base port
configSocket.connect('tcp://' + matrix_ip + ':' + matrix_gpio_base_port);
// Create driver configuration
var outputConfig = matrix_io.malos.v1.driver.DriverConfig.create({
 // Update rate configuration
   delayBetweenUpdates: 0.01,// 0.01 seconds between updates
   timeoutAfterLastPing: 3.0,// Stop sending updates <> seconds after pings
   //GPIO Configuration
   gpio: matrix_io.malos.v1.io.GpioParams.create({
       pin: relayPin,// Use pin 0
       mode: matrix_io.malos.v1.io.GpioParams.EnumMode.OUTPUT,// Set as output mode
       value: 0// Set initial pin value as off
   })
});
////////////////////
//ON & OFF METHODS//
////////////////////
methods.lightsOff = function() {
   outputConfig.gpio.value = 0;
   configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(outputConfig).finish());
   console.log("Lights Have Been Turned Off");
};
methods.lightsOn = function() {
   outputConfig.gpio.value = 1;
   configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(outputConfig).finish());
   console.log("Lights Have Been Turned On");
};
module.exports = methods;// Export methods in order to make them avaialble to other files