mirror of
https://gitlab.silvrtree.co.uk/martind2000/mdot_mqtt.git
synced 2025-01-11 00:05:08 +00:00
Prep for bluemix deployment
This commit is contained in:
parent
9d93f54621
commit
dad0dc4a46
15
.project
Normal file
15
.project
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>mdotmqtt</name>
|
||||
<comment>MQTT listener for mDot service</comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.nodeclipse.ui.NodeNature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
254
app.js
254
app.js
@ -1,18 +1,5 @@
|
||||
/**
|
||||
* Created by Martin on 08/02/2016.
|
||||
*/
|
||||
'use strict';
|
||||
var express = require('express');
|
||||
var path = require('path');
|
||||
var http = require('http');
|
||||
var ejs = require('ejs');
|
||||
var morgan = require('morgan');
|
||||
var cookieparser = require('cookie-parser');
|
||||
var session = require('express-session');
|
||||
var methodoverride = require('method-override');
|
||||
var bodyparser = require('body-parser');
|
||||
var errorhandler = require('errorhandler');
|
||||
var mqttConnect = require('./lib/mqtt/mqttConnect');
|
||||
var cfenv = require('cfenv');
|
||||
|
||||
var log4js = require('log4js');
|
||||
var logger = log4js.getLogger();
|
||||
|
||||
@ -22,241 +9,8 @@ var WebSocketServer = require('websocket').server;
|
||||
var EventEmitter = require('events');
|
||||
var busEmitter = new EventEmitter();
|
||||
|
||||
var apn = require('apn');
|
||||
|
||||
require('sugar-date');
|
||||
var mqttClient = require('./lib/mqtt/mqttClient');
|
||||
|
||||
var calendar = require('./lib/office/officeController.js');
|
||||
var cal = new calendar.officeController(busEmitter);
|
||||
var mqtt = new mqttClient.mqttClient(busEmitter);
|
||||
|
||||
var lighting_v1 = require('./routes/lighting_v1');
|
||||
var heating_v1 = require('./routes/heating_v1');
|
||||
var projector_v1 = require('./routes/projector_v1');
|
||||
var isProduction = false;
|
||||
|
||||
|
||||
function restartTimer() {
|
||||
// Ibm / mqtt things go a bit weird if left on for too long so restart the server
|
||||
// at 7:30 ever morning before anyone is using it.
|
||||
var tom = Date.create('tomorrow at 7:30am');
|
||||
|
||||
var ms = tom.getTime() - Date.create('now').getTime();
|
||||
|
||||
|
||||
console.log('Restarting in: ', ms);
|
||||
setTimeout(function() {process.exit(1);}, ms);
|
||||
|
||||
}
|
||||
mqttConnect.setEmitter(busEmitter);
|
||||
mqttConnect.doConnection();
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
isProduction = true;
|
||||
}
|
||||
|
||||
logger.debug('isProduction:', isProduction);
|
||||
|
||||
var app = express();
|
||||
|
||||
app.set('port', process.env.PORT || 4545);
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'ejs');
|
||||
app.use(morgan('combined'));
|
||||
app.use(cookieparser('your secret here'));
|
||||
app.use(session({
|
||||
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
|
||||
}));
|
||||
/* 'default', 'short', 'tiny', 'dev' */
|
||||
app.use(methodoverride());
|
||||
|
||||
app.use(bodyparser.urlencoded({extended: false}));
|
||||
|
||||
// Parse application/json
|
||||
app.use(bodyparser.json());
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
|
||||
next();
|
||||
});
|
||||
|
||||
// Run npm start --production to use dist
|
||||
var staticDir = isProduction ? 'dist' : 'app';
|
||||
|
||||
app.use(express.static(path.join(__dirname, staticDir)));
|
||||
app.use(errorhandler({dumpExceptions: true, showStack: true}));
|
||||
|
||||
lighting_v1.use(mqttConnect);
|
||||
heating_v1.use(mqttConnect);
|
||||
projector_v1.use(mqttConnect);
|
||||
|
||||
// Calendar handler
|
||||
|
||||
busEmitter.on('lightingOn', lighting_v1.doLightsOn);
|
||||
busEmitter.on('lightingOff', lighting_v1.doLightsOff);
|
||||
|
||||
busEmitter.on('heatingOn', mqttConnect.heatingOn);
|
||||
busEmitter.on('heatingOff', mqttConnect.heatingOff);
|
||||
busEmitter.on('projectorOn', projector_v1.doProjectorOn);
|
||||
busEmitter.on('projectorOff', projector_v1.doProjectorOff);
|
||||
|
||||
|
||||
busEmitter.on('connectWS', mqttConnect.connectWS);
|
||||
busEmitter.on('restartMQTTSocket', mqttConnect.restartMQTTSocket);
|
||||
|
||||
mqttConnect.setupPing();
|
||||
cal.startController(busEmitter);
|
||||
|
||||
app.get('/stop', function(request, response) {
|
||||
cal.stopController();
|
||||
response.sendStatus(200);
|
||||
});
|
||||
|
||||
app.get('/start', function(request, response) {
|
||||
|
||||
cal.startController();
|
||||
response.sendStatus(200);
|
||||
});
|
||||
|
||||
app.get('/api/calendar', function(req, res) {
|
||||
var calJson = cal.returnCalendar();
|
||||
logger.info(calJson);
|
||||
res.json(calJson);
|
||||
});
|
||||
|
||||
app.post('/api/calendar/extend', function(req, res) {
|
||||
res.json({});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// Events and sockets
|
||||
|
||||
busEmitter.on('clientConnected', (socketSet) => {
|
||||
logger.info('-=-=-=-=-');
|
||||
logger.info(socketSet.getClientStatus());
|
||||
heating_v1.setsocket(socketSet).subscribe();
|
||||
lighting_v1.setsocket(socketSet).subscribe();
|
||||
projector_v1.setsocket(socketSet).subscribe();
|
||||
});
|
||||
|
||||
busEmitter.on('clientStatusUpdated', (v) => {
|
||||
logger.info(v);
|
||||
});
|
||||
|
||||
|
||||
|
||||
logger.info('Configuring WebSocket Listener...');
|
||||
var server = http.createServer(function(request, response) {
|
||||
logger.info((new Date()) + ' Received request for ' + request.url);
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
});
|
||||
|
||||
server.listen(3001, function() {
|
||||
logger.info((new Date()) + ' Server is listening on port 3001');
|
||||
});
|
||||
var wsServer = new WebSocketServer({
|
||||
httpServer: server, // You should not use autoAcceptConnections for production
|
||||
// applications, as it defeats all standard cross-origin protection
|
||||
// facilities built into the protocol and the browser. You should
|
||||
// *always* verify the connection's origin and decide whether or not
|
||||
// to accept it.
|
||||
autoAcceptConnections: false
|
||||
});
|
||||
|
||||
function originIsAllowed(origin) {
|
||||
// Put logic here to detect whether the specified origin is allowed.
|
||||
return true;
|
||||
}
|
||||
|
||||
wsServer.on('request', function(request) {
|
||||
|
||||
if (!originIsAllowed(request.origin)) {
|
||||
// Make sure we only accept requests from an allowed origin
|
||||
request.reject();
|
||||
logger.info((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
|
||||
return;
|
||||
}
|
||||
|
||||
var connection = request.accept('stream', request.origin);
|
||||
logger.info((new Date()) + ' Connection accepted.');
|
||||
|
||||
var sendSocketHandler = (obj) => {
|
||||
try {
|
||||
connection.sendUTF(JSON.stringify(obj));
|
||||
}
|
||||
catch (err) {
|
||||
logger.error(err);
|
||||
logger.warn('Offending object: ', obj);
|
||||
}
|
||||
};
|
||||
|
||||
busEmitter.on('sendSocket', sendSocketHandler);
|
||||
|
||||
connection.on('message', function(message) {
|
||||
if (message.type === 'utf8') {
|
||||
logger.info('Received Message: ' + message.utf8Data);
|
||||
connection.sendUTF(message.utf8Data);
|
||||
} else if (message.type === 'binary') {
|
||||
logger.info('Received Binary Message of ' + message.binaryData.length + ' bytes');
|
||||
connection.sendBytes(message.binaryData);
|
||||
}
|
||||
});
|
||||
|
||||
connection.on('close', function(reasonCode, description) {
|
||||
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
||||
busEmitter.removeListener('sendSocket', sendSocketHandler);
|
||||
});
|
||||
});
|
||||
|
||||
mqttConnect.connectWS(function() {
|
||||
logger.info('Ready to plug in sockets...');
|
||||
});
|
||||
|
||||
app.post('/api/v1/lighting/off', lighting_v1.turnoff);
|
||||
app.post('/api/v1/lighting/on', lighting_v1.turnon);
|
||||
app.post('/api/v1/lighting/cmd', lighting_v1.command);
|
||||
|
||||
app.post('/api/v1/heating/off', heating_v1.turnoff);
|
||||
app.post('/api/v1/heating/on', heating_v1.turnon);
|
||||
|
||||
app.post('/api/v1/projector/off', projector_v1.turnoff);
|
||||
app.post('/api/v1/projector/on', projector_v1.turnon);
|
||||
app.post('/api/v1/projector/cmd', projector_v1.command);
|
||||
|
||||
app.post('/api/v1/register/ios', function(req, res) {
|
||||
|
||||
var body = req.body, registrationId;
|
||||
|
||||
logger.debug(body);
|
||||
|
||||
if (body.hasOwnProperty('registrationId')) {
|
||||
registrationId = body['registrationId'];
|
||||
logger.debug(registrationId);
|
||||
|
||||
cal.registeriOSToken(registrationId);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/v1/extend', function(req, res) {
|
||||
|
||||
var body = req.body, data;
|
||||
|
||||
if (body.hasOwnProperty('extendBy') && body.hasOwnProperty('uid')) {
|
||||
data = req.body;
|
||||
|
||||
cal.extendMeeting(data);
|
||||
res.sendStatus(200);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
app.listen(3000, function() {
|
||||
logger.info('Express listening on 3000');
|
||||
restartTimer();
|
||||
});
|
||||
|
28
index.js
28
index.js
@ -1,28 +0,0 @@
|
||||
var log4js = require('log4js');
|
||||
var logger = log4js.getLogger();
|
||||
|
||||
|
||||
var WebSocketServer = require('websocket').server;
|
||||
|
||||
var EventEmitter = require('events');
|
||||
var busEmitter = new EventEmitter();
|
||||
|
||||
|
||||
|
||||
var mqttClient = require('./lib/mqtt/mqttClient');
|
||||
//var mqttSocket = require('./lib/mqtt/mqttSocket');
|
||||
|
||||
|
||||
//busEmitter.on('startWSConnection', startWSConnection);
|
||||
|
||||
var mqtt = new mqttClient.mqttClient(busEmitter);
|
||||
|
||||
/*
|
||||
|
||||
mqttConnect.setEmitter(busEmitter);
|
||||
mqttConnect.doConnection();
|
||||
|
||||
|
||||
mqttConnect.connectWS();
|
||||
*/
|
||||
|
@ -1,23 +1,14 @@
|
||||
var mqtt = require('mqtt');
|
||||
var request = require('request');
|
||||
var util = require('util');
|
||||
var logger = require('log4js').getLogger();
|
||||
|
||||
var EventEmitter = require('events');
|
||||
|
||||
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
|
||||
var nano = require('nano')('http://localhost:5984');
|
||||
var busEmitter = new EventEmitter();
|
||||
|
||||
var db_name = 'mdot';
|
||||
var dbCouch = nano.use(db_name);
|
||||
|
||||
var db = require('../server/db-connector').dbConnection;
|
||||
|
||||
var dbSave = require('../server/db-save')(db);
|
||||
|
||||
|
||||
|
||||
function dataBuilder(obj) {
|
||||
'use strict';
|
||||
var now = new Date();
|
||||
|
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var newId = require('uuid-pure').newId;
|
||||
var atob = require('atob');
|
||||
|
||||
module.exports = function(db) {
|
||||
|
8
manifest.yml
Normal file
8
manifest.yml
Normal file
@ -0,0 +1,8 @@
|
||||
applications:
|
||||
- path: .
|
||||
memory: 256M
|
||||
instances: 1
|
||||
domain: mybluemix.net
|
||||
name: mdotmqtt
|
||||
host: mdotmqtt
|
||||
disk_quota: 1024M
|
21491
mdot_data.sql
21491
mdot_data.sql
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -8,23 +8,14 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"atob": "^2.0.3",
|
||||
"body-parser": "^1.15.1",
|
||||
"cookie-parser": "*",
|
||||
"ejs": "*",
|
||||
"errorhandler": "*",
|
||||
"exec": "^0.2.1",
|
||||
"express": "^4.13.4",
|
||||
"express-session": "*",
|
||||
"htmlparser": "^1.7.7",
|
||||
"method-override": "*",
|
||||
"minibus": "^3.1.0",
|
||||
"morgan": "*",
|
||||
"events": "^1.1.1",
|
||||
"mqtt": "^1.10.0",
|
||||
"mqtt_over_websockets": "0.0.1",
|
||||
"node-localstorage": "^1.1.2",
|
||||
"pg-promise": "^5.2.7",
|
||||
"request": "^2.72.0",
|
||||
"websocket": "^1.0.22"
|
||||
"websocket": "^1.0.22",
|
||||
"cfenv": "1.0.x",
|
||||
"log4js": "^0.6.36"
|
||||
},
|
||||
"devDependencies": {
|
||||
"after": "^0.8.1",
|
||||
@ -36,7 +27,6 @@
|
||||
"clone": "^1.0.2",
|
||||
"del": "^2.2.0",
|
||||
"elapsed": "0.0.7",
|
||||
"events": "^1.1.1",
|
||||
"gulp": "^3.9.1",
|
||||
"gulp-autoprefixer": "^3.1.0",
|
||||
"gulp-cache": "^0.4.5",
|
||||
@ -58,7 +48,6 @@
|
||||
"gulp-uglify": "^2.0.0",
|
||||
"jshint": "^2.9.2",
|
||||
"jsonfile": "^2.3.1",
|
||||
"log4js": "^0.6.36",
|
||||
"mocha": "^3.0.2",
|
||||
"mqtt-ws": "^0.2.0",
|
||||
"nano": "^6.2.0",
|
||||
|
Loading…
Reference in New Issue
Block a user