going direct to socket
This commit is contained in:
parent
a00d110f51
commit
7f37f2f19d
@ -4,7 +4,7 @@ const request = require('request');
|
|||||||
let util = require('util');
|
let util = require('util');
|
||||||
const logger = require('log4js').getLogger();
|
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://martind2000:1V3D4m526i@localhost:5984');
|
||||||
// var nano = require('nano')('http://localhost:5984');
|
// var nano = require('nano')('http://localhost:5984');
|
||||||
@ -17,7 +17,7 @@ let globalMode = 'FanOff';
|
|||||||
let lastDispatch = 0;
|
let lastDispatch = 0;
|
||||||
|
|
||||||
|
|
||||||
const mqttClient = function (events) {
|
const mqttClient = function(events) {
|
||||||
|
|
||||||
const orgId = '';
|
const orgId = '';
|
||||||
const userName = '';
|
const userName = '';
|
||||||
@ -37,16 +37,18 @@ const mqttClient = function (events) {
|
|||||||
const d = new Date();
|
const d = new Date();
|
||||||
this.lastMsg = d.getTime();
|
this.lastMsg = d.getTime();
|
||||||
|
|
||||||
events.on('sendIFTTT', function (mode) {
|
events.on('sendIFTTT', function(mode) {
|
||||||
let d;
|
let d;
|
||||||
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
||||||
|
|
||||||
if (mode !== globalMode) {
|
if (mode !== globalMode) {
|
||||||
logger.info('Sending..');
|
logger.info('Sending..');
|
||||||
globalMode = mode;
|
globalMode = mode;
|
||||||
request(url, function (error, response, body) {
|
/* Request(url, function (error, response, body) {
|
||||||
logger.debug(response, body);
|
logger.debug(response, body);
|
||||||
});
|
}); */
|
||||||
|
|
||||||
|
events.emit('changeState', mode === 'FanOn' ? 1:0);
|
||||||
|
|
||||||
d = new Date();
|
d = new Date();
|
||||||
lastDispatch = d.getTime();
|
lastDispatch = d.getTime();
|
||||||
@ -55,7 +57,7 @@ const mqttClient = function (events) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.storeData = function (data, alt) {
|
this.storeData = function(data, alt) {
|
||||||
if (!this.first) {
|
if (!this.first) {
|
||||||
this.first = true;
|
this.first = true;
|
||||||
return [];
|
return [];
|
||||||
@ -71,7 +73,7 @@ const mqttClient = function (events) {
|
|||||||
this.livingRoom.data = target;
|
this.livingRoom.data = target;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.logTemp = function () {
|
this.logTemp = function() {
|
||||||
const now = new Date;
|
const now = new Date;
|
||||||
const mod = 60000 - (now.getTime() % 60000);
|
const mod = 60000 - (now.getTime() % 60000);
|
||||||
this.storeData(this.livingRoom.temp);
|
this.storeData(this.livingRoom.temp);
|
||||||
@ -81,7 +83,7 @@ const mqttClient = function (events) {
|
|||||||
setTimeout(this.logTemp.bind(this), mod + 10);
|
setTimeout(this.logTemp.bind(this), mod + 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.fanTimer = function () {
|
this.fanTimer = function() {
|
||||||
|
|
||||||
let n;
|
let n;
|
||||||
const now = new Date;
|
const now = new Date;
|
||||||
@ -139,7 +141,7 @@ const mqttClient = function (events) {
|
|||||||
|
|
||||||
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
|
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
|
||||||
|
|
||||||
this.client.on('connect', function () {
|
this.client.on('connect', function() {
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
logger.info('Connected to Silvr Broker');
|
logger.info('Connected to Silvr Broker');
|
||||||
this.fanTimer();
|
this.fanTimer();
|
||||||
@ -148,7 +150,7 @@ const mqttClient = function (events) {
|
|||||||
|
|
||||||
this.client.subscribe('livingroomTemp');
|
this.client.subscribe('livingroomTemp');
|
||||||
|
|
||||||
this.client.on('message', function (topic, message) {
|
this.client.on('message', function(topic, message) {
|
||||||
|
|
||||||
const json = JSON.parse(message.toString());
|
const json = JSON.parse(message.toString());
|
||||||
logger.debug(json);
|
logger.debug(json);
|
||||||
@ -169,7 +171,7 @@ const mqttClient = function (events) {
|
|||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
this.isConnected = function () {
|
this.isConnected = function() {
|
||||||
return this.connected;
|
return this.connected;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
47
server.js
47
server.js
@ -15,7 +15,7 @@ const errorhandler = require('errorhandler');
|
|||||||
const log4js = require('log4js');
|
const log4js = require('log4js');
|
||||||
const logger = log4js.getLogger();
|
const logger = log4js.getLogger();
|
||||||
let authentication = require('basic-authentication');
|
let authentication = require('basic-authentication');
|
||||||
|
const Wemo = require('wemo-client');
|
||||||
const Events = require('events');
|
const Events = require('events');
|
||||||
const busEmitter = new Events.EventEmitter();
|
const busEmitter = new Events.EventEmitter();
|
||||||
|
|
||||||
@ -28,12 +28,37 @@ let webSocket = new SocketHandler(busEmitter, wss);
|
|||||||
|
|
||||||
|
|
||||||
let mqttClient = require('./lib/mqtt/mqttClient');
|
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);
|
let mqtt = new mqttClient.mqttClient(busEmitter);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require('sugar-date');
|
require('sugar-date');
|
||||||
|
|
||||||
let isProduction = false;
|
let isProduction = false;
|
||||||
@ -46,20 +71,20 @@ if (process.env.NODE_ENV === 'production') {
|
|||||||
|
|
||||||
logger.warn('isProduction:', isProduction);
|
logger.warn('isProduction:', isProduction);
|
||||||
|
|
||||||
const heartBeat = function () {
|
const heartBeat = function() {
|
||||||
this.pingTimer = 0;
|
this.pingTimer = 0;
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
this.rate = 90000;
|
this.rate = 90000;
|
||||||
|
|
||||||
this.setupPing = function () {
|
this.setupPing = function() {
|
||||||
logger.warn('Starting heartbeat...');
|
logger.warn('Starting heartbeat...');
|
||||||
|
|
||||||
this.pingTimer = setTimeout(function () {
|
this.pingTimer = setTimeout(function() {
|
||||||
this.ping();
|
this.ping();
|
||||||
}.bind(this), 10000);
|
}.bind(this), 10000);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.ping = function () {
|
this.ping = function() {
|
||||||
const now = new Date;
|
const now = new Date;
|
||||||
const mod = this.rate - (now.getTime() % this.rate);
|
const mod = this.rate - (now.getTime() % this.rate);
|
||||||
this.count++;
|
this.count++;
|
||||||
@ -72,7 +97,7 @@ const heartBeat = function () {
|
|||||||
logger.info(_newDots.join(''));
|
logger.info(_newDots.join(''));
|
||||||
|
|
||||||
busEmitter.emit('sendSocket', {tick: new Date().getTime()});
|
busEmitter.emit('sendSocket', {tick: new Date().getTime()});
|
||||||
this.pingTimer = setTimeout(function () {
|
this.pingTimer = setTimeout(function() {
|
||||||
this.ping();
|
this.ping();
|
||||||
}.bind(this), mod);
|
}.bind(this), mod);
|
||||||
|
|
||||||
@ -92,7 +117,7 @@ app.set('views', __dirname + '/views');
|
|||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
app.use(morgan('combined'));
|
app.use(morgan('combined'));
|
||||||
app.use(cookieparser('your secret here'));
|
app.use(cookieparser('your secret here'));
|
||||||
/*app.use(session({
|
/*App.use(session({
|
||||||
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
|
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
|
||||||
}));*/
|
}));*/
|
||||||
app.use(sessionLW());
|
app.use(sessionLW());
|
||||||
@ -125,7 +150,7 @@ function originIsAllowed(origin) {
|
|||||||
// Glue routes
|
// Glue routes
|
||||||
|
|
||||||
//if (isProduction) {
|
//if (isProduction) {
|
||||||
heartBeat();
|
heartBeat();
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user