going direct to socket

This commit is contained in:
Martin Donnelly 2017-01-07 18:21:24 +00:00
parent a00d110f51
commit 7f37f2f19d
3 changed files with 50 additions and 23 deletions

View File

@ -4,7 +4,7 @@ const request = require('request');
let util = require('util');
const logger = require('log4js').getLogger();
// var db = require('../server/db-connector').dbConnection;
// Var db = require('../server/db-connector').dbConnection;
// var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
// var nano = require('nano')('http://localhost:5984');
@ -17,7 +17,7 @@ let globalMode = 'FanOff';
let lastDispatch = 0;
const mqttClient = function (events) {
const mqttClient = function(events) {
const orgId = '';
const userName = '';
@ -37,16 +37,18 @@ const mqttClient = function (events) {
const d = new Date();
this.lastMsg = d.getTime();
events.on('sendIFTTT', function (mode) {
events.on('sendIFTTT', function(mode) {
let d;
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
if (mode !== globalMode) {
logger.info('Sending..');
globalMode = mode;
request(url, function (error, response, body) {
/* Request(url, function (error, response, body) {
logger.debug(response, body);
});
}); */
events.emit('changeState', mode === 'FanOn' ? 1:0);
d = new Date();
lastDispatch = d.getTime();
@ -55,7 +57,7 @@ const mqttClient = function (events) {
}
});
this.storeData = function (data, alt) {
this.storeData = function(data, alt) {
if (!this.first) {
this.first = true;
return [];
@ -71,7 +73,7 @@ const mqttClient = function (events) {
this.livingRoom.data = target;
};
this.logTemp = function () {
this.logTemp = function() {
const now = new Date;
const mod = 60000 - (now.getTime() % 60000);
this.storeData(this.livingRoom.temp);
@ -81,7 +83,7 @@ const mqttClient = function (events) {
setTimeout(this.logTemp.bind(this), mod + 10);
};
this.fanTimer = function () {
this.fanTimer = function() {
let n;
const now = new Date;
@ -139,7 +141,7 @@ const mqttClient = function (events) {
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
this.client.on('connect', function () {
this.client.on('connect', function() {
this.connected = true;
logger.info('Connected to Silvr Broker');
this.fanTimer();
@ -148,7 +150,7 @@ const mqttClient = function (events) {
this.client.subscribe('livingroomTemp');
this.client.on('message', function (topic, message) {
this.client.on('message', function(topic, message) {
const json = JSON.parse(message.toString());
logger.debug(json);
@ -169,7 +171,7 @@ const mqttClient = function (events) {
}.bind(this));
this.isConnected = function () {
this.isConnected = function() {
return this.connected;
};

View File

@ -15,7 +15,7 @@ const errorhandler = require('errorhandler');
const log4js = require('log4js');
const logger = log4js.getLogger();
let authentication = require('basic-authentication');
const Wemo = require('wemo-client');
const Events = require('events');
const busEmitter = new Events.EventEmitter();
@ -28,12 +28,37 @@ let webSocket = new SocketHandler(busEmitter, wss);
let mqttClient = require('./lib/mqtt/mqttClient');
//var mqttConnect = require('./lib/mqtt/mqttConnect');
//Var mqttConnect = require('./lib/mqtt/mqttConnect');
let wemoClient = null;
let wemo = new Wemo();
wemo.discover(function(deviceInfo) {
logger.info('Wemo Device Found: %j', deviceInfo);
// Get the client for the found device
wemoClient = wemo.client(deviceInfo);
// Handle BinaryState events
wemoClient.on('binaryState', function(value) {
logger.debug('Binary State changed to: %s', value);
});
// Turn the switch on
wemoClient.setBinaryState(0);
});
busEmitter.on('changeState', function(mode) {
logger.info('Changing state..');
wemoClient.setBinaryState(0);
});
let mqtt = new mqttClient.mqttClient(busEmitter);
require('sugar-date');
let isProduction = false;
@ -46,20 +71,20 @@ if (process.env.NODE_ENV === 'production') {
logger.warn('isProduction:', isProduction);
const heartBeat = function () {
const heartBeat = function() {
this.pingTimer = 0;
this.count = 0;
this.rate = 90000;
this.setupPing = function () {
this.setupPing = function() {
logger.warn('Starting heartbeat...');
this.pingTimer = setTimeout(function () {
this.pingTimer = setTimeout(function() {
this.ping();
}.bind(this), 10000);
};
this.ping = function () {
this.ping = function() {
const now = new Date;
const mod = this.rate - (now.getTime() % this.rate);
this.count++;
@ -72,7 +97,7 @@ const heartBeat = function () {
logger.info(_newDots.join(''));
busEmitter.emit('sendSocket', {tick: new Date().getTime()});
this.pingTimer = setTimeout(function () {
this.pingTimer = setTimeout(function() {
this.ping();
}.bind(this), mod);
@ -92,7 +117,7 @@ app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(morgan('combined'));
app.use(cookieparser('your secret here'));
/*app.use(session({
/*App.use(session({
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
}));*/
app.use(sessionLW());
@ -125,7 +150,7 @@ function originIsAllowed(origin) {
// Glue routes
//if (isProduction) {
heartBeat();
heartBeat();
//}

View File

@ -16,5 +16,5 @@ wemo.discover(function(deviceInfo) {
});
// Turn the switch on
client.setBinaryState(1);
client.setBinaryState(0);
});