mirror of
https://gitlab.silvrtree.co.uk/martind2000/censis-archive.git
synced 2025-01-11 03:45:09 +00:00
”2016-09-08”
This commit is contained in:
parent
b7195280a5
commit
146d769405
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Created by Martin on 08/02/2016.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var path = require('path');
|
||||
var http = require('http');
|
||||
@ -16,10 +16,10 @@ var log4js = require('log4js');
|
||||
var logger = log4js.getLogger();
|
||||
var authentication = require('basic-authentication');
|
||||
|
||||
var EventEmitter = require('events');
|
||||
var busEmitter = new EventEmitter();
|
||||
var Events = require('events');
|
||||
var busEmitter = new Events.EventEmitter();
|
||||
|
||||
var socketHandler = require('./lib/socketeer')(busEmitter);
|
||||
var SocketHandler = require('./lib/sockethandler');
|
||||
|
||||
// Var apn = require('apn');
|
||||
|
||||
@ -42,6 +42,38 @@ if (process.env.NODE_ENV === 'production') {
|
||||
|
||||
logger.warn('isProduction:', isProduction);
|
||||
|
||||
var heartBeat = function() {
|
||||
this.pingTimer = 0;
|
||||
this.count = 0;
|
||||
this.rate = 20000;
|
||||
|
||||
this.setupPing = function() {
|
||||
logger.warn('Starting heartbeat...');
|
||||
|
||||
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), 10000);
|
||||
};
|
||||
|
||||
this.ping = function() {
|
||||
var now = new Date;
|
||||
var mod = this.rate - (now.getTime() % this.rate);
|
||||
this.count++;
|
||||
if (this.count > 5) {
|
||||
this.count = 1;
|
||||
}
|
||||
|
||||
var _newDots = ['.','.','.','.','.'];
|
||||
_newDots[this.count - 1] = 'O';
|
||||
logger.info(_newDots.join(''));
|
||||
|
||||
busEmitter.emit('sendSocket',{tick:new Date().getTime()});
|
||||
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), mod);
|
||||
|
||||
};
|
||||
|
||||
this.setupPing();
|
||||
};
|
||||
|
||||
|
||||
var app = express();
|
||||
|
||||
/*App.use(authentication(({
|
||||
@ -102,15 +134,21 @@ app.get('/meeting', function(req, res) {
|
||||
res.render('meeting',{delimiter: '^'});
|
||||
});
|
||||
|
||||
// ****
|
||||
|
||||
|
||||
|
||||
var webSocket = new SocketHandler(busEmitter);
|
||||
|
||||
// Glue routes
|
||||
mdotApi(app);
|
||||
mdotApiV2(app);
|
||||
trackApi(app);
|
||||
meetingApi(app);
|
||||
|
||||
messageApi(app);
|
||||
messageApi(app, busEmitter);
|
||||
|
||||
heartBeat();
|
||||
|
||||
var port = (process.env.VCAP_APP_PORT || 3011);
|
||||
var host = (process.env.VCAP_APP_HOST || 'localhost');
|
||||
@ -123,3 +161,5 @@ app.listen(port, function() {
|
||||
logger.info('Express listening on ',host, port);
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
@ -17,7 +17,20 @@
|
||||
|_| |_|\___/|___/|___|____|___/
|
||||
*/
|
||||
|
||||
var MainModel = Backbone.Model.extend({});
|
||||
var SocketManager = Backbone.Model.extend({
|
||||
initialize: function() {
|
||||
// _.bindAll(this, 'processAdded');
|
||||
this.webSocket = new WEBSOCKET(this);
|
||||
|
||||
this.on('message', function(o) {
|
||||
console.log('Message', o);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var MainModel = Backbone.Model.extend({
|
||||
|
||||
});
|
||||
|
||||
var EventsModel = Backbone.Model.extend({
|
||||
initialize: function() {
|
||||
@ -451,6 +464,8 @@
|
||||
notification.configProfile('global', {
|
||||
stacking: false
|
||||
});
|
||||
var webSocketModel = new SocketManager();
|
||||
|
||||
var DeviceCollection = new Backbone.Collection;
|
||||
var mdotCollection = new mDotCollection();
|
||||
|
||||
|
79
mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js
Normal file
79
mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js
Normal file
@ -0,0 +1,79 @@
|
||||
var WEBSOCKET = function(model) {
|
||||
|
||||
var wsUrl = 'ws://localhost:3001';
|
||||
|
||||
this.socket = null;
|
||||
this.retry = 0;
|
||||
this.timer = 0;
|
||||
|
||||
this.startWebSocket = function() {
|
||||
'use strict';
|
||||
console.log('Starting socket?');
|
||||
|
||||
var url = wsUrl;
|
||||
|
||||
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;
|
||||
this.socket = new wsCtor(url, 'stream');
|
||||
|
||||
this.socket.onopen = this.handleWebsocketOnOpen.bind(this);
|
||||
this.socket.onmessage = this.handleWebsocketMessage.bind(this);
|
||||
this.socket.onclose = this.handleWebsocketClose.bind(this);
|
||||
this.socket.onerror = function(e) {
|
||||
console.error(e);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
this.handleData = function(d) {
|
||||
|
||||
|
||||
model.trigger('message',d);
|
||||
switch (d.id) {
|
||||
/*
|
||||
case 'LightingDataReceived':
|
||||
|
||||
// This.updateLighting(d.sensorData.d);
|
||||
break;
|
||||
*/
|
||||
|
||||
default:
|
||||
console.log('Default:',d);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
this.handleWebsocketOnOpen = function() {
|
||||
'use strict';
|
||||
this.retry = 0;
|
||||
|
||||
console.log('**** Websocket Connected ****');
|
||||
};
|
||||
|
||||
this.handleWebsocketMessage = function(message) {
|
||||
|
||||
console.log('handleWebsocketMessage:',message);
|
||||
var command;
|
||||
try {
|
||||
command = JSON.parse(message.data);
|
||||
}
|
||||
catch (e) { /* Do nothing */
|
||||
}
|
||||
|
||||
|
||||
if (command) {
|
||||
//This.dispatchCommand(command);
|
||||
this.handleData.call(this,command);
|
||||
}
|
||||
};
|
||||
|
||||
this.handleWebsocketClose = function() {
|
||||
console.error('WebSocket Connection Closed.');
|
||||
var self = this;
|
||||
console.log('Waiting ', 5000);
|
||||
this.timer = setTimeout(function() {self.startWebSocket();},5000);
|
||||
};
|
||||
|
||||
this.startWebSocket();
|
||||
|
||||
};
|
21
mdot/mDotServer.censis/mDotServer.censis/app/socket.html
Normal file
21
mdot/mDotServer.censis/mDotServer.censis/app/socket.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<script src="lib/jquery.js"></script>
|
||||
<script src="js/websocket.js"></script>
|
||||
|
||||
<body>
|
||||
<script>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
webSocket = new WEBSOCKET();
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -8,7 +8,7 @@ var db = require('../server/db-connector').dbConnection;
|
||||
var dbSave = require('../server/db-save')(db);
|
||||
|
||||
|
||||
module.exports = function(app) {
|
||||
module.exports = function(app, events) {
|
||||
var express = require('express');
|
||||
var messageRouter = express.Router();
|
||||
|
||||
@ -26,7 +26,12 @@ module.exports = function(app) {
|
||||
.then(function(d) {
|
||||
'use strict';
|
||||
// Logger.info('Finished - Processed');
|
||||
logger.info('emiting sendSocket');
|
||||
events.emit('sendSocket',d);
|
||||
//console.log(events);
|
||||
|
||||
res.status(200).json({});
|
||||
|
||||
})
|
||||
.catch(function(e) {
|
||||
'use strict';
|
||||
@ -36,33 +41,6 @@ module.exports = function(app) {
|
||||
|
||||
}
|
||||
|
||||
/* If (!req.params.id) {
|
||||
logger.error('MDot','Missing required parameter');
|
||||
res.status(400).send({
|
||||
status: 'error',
|
||||
error: 'missing required parameter'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.query.hasOwnProperty('start') && req.query.hasOwnProperty('end')) {
|
||||
data.start = req.query.start;
|
||||
data.end = req.query.end;
|
||||
}
|
||||
|
||||
if (req.params.hasOwnProperty('id')) {
|
||||
data.id = req.params.id;
|
||||
|
||||
dbMdot.doGet(data)
|
||||
.then((d) => {
|
||||
res.json({events:d});
|
||||
})
|
||||
.catch((e) => {
|
||||
logger.error(e);
|
||||
res.status(500).json({});
|
||||
});
|
||||
|
||||
}*/
|
||||
|
||||
});
|
||||
|
||||
|
@ -9,7 +9,6 @@
|
||||
|
||||
var pgp = require('pg-promise')();
|
||||
|
||||
/*
|
||||
var cn = {
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
@ -17,17 +16,16 @@ var cn = {
|
||||
user: 'postgres',
|
||||
password: ''
|
||||
};
|
||||
*/
|
||||
|
||||
// ElephantSql settings
|
||||
|
||||
var cn = {
|
||||
/*var cn = {
|
||||
host: 'jumbo.db.elephantsql.com',
|
||||
port: 5432,
|
||||
database: 'amlrxqev',
|
||||
user: 'amlrxqev',
|
||||
password: 'K11cvCplk0--oNafsYj4ISN-rVQmVS3y'
|
||||
};
|
||||
};*/
|
||||
|
||||
exports.dbConnection = pgp(cn);
|
||||
|
||||
|
@ -93,7 +93,7 @@ module.exports = function(db) {
|
||||
self.sqlInsertDecoded(_data)
|
||||
.then((d)=> {
|
||||
// console.log('Postgres returns', d);
|
||||
return resolve({reply: 'Processed event inserted'});
|
||||
return resolve({reply: 'Processed event inserted',data:_data});
|
||||
})
|
||||
.catch((err)=> {
|
||||
console.error(err);
|
||||
|
@ -0,0 +1,94 @@
|
||||
/**
|
||||
*
|
||||
* User: Martin Donnelly
|
||||
* Date: 2016-09-07
|
||||
* Time: 15:33
|
||||
*
|
||||
*/
|
||||
|
||||
var WebSocketServer = require('websocket').server;
|
||||
var http = require('http');
|
||||
|
||||
var logger = require('log4js').getLogger();
|
||||
|
||||
var Sugar = require('sugar-date');
|
||||
|
||||
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('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
|
||||
});
|
||||
|
||||
module.exports = function(events) {
|
||||
'use strict';
|
||||
logger.debug('creating with events', events);
|
||||
|
||||
function originIsAllowed(origin) {
|
||||
// Put logic here to detect whether the specified origin is allowed.
|
||||
return true;
|
||||
}
|
||||
wsServer.on('request', function(request) {
|
||||
|
||||
logger.info('Creating event for this connection');
|
||||
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) => {
|
||||
logger.debug('sendSocketHandler', obj);
|
||||
try {
|
||||
connection.sendUTF(JSON.stringify(obj));
|
||||
}
|
||||
catch (err) {
|
||||
logger.error(err);
|
||||
logger.warn('Offending object: ', obj);
|
||||
}
|
||||
};
|
||||
|
||||
events.on('sendSocket', sendSocketHandler);
|
||||
/*
|
||||
events.on('sendSocket', function(o){
|
||||
logger.debug('sendSocket!!', o);
|
||||
});
|
||||
*/
|
||||
|
||||
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.');
|
||||
events.removeListener('sendSocket', sendSocketHandler);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
return module;
|
||||
};
|
@ -227,6 +227,7 @@
|
||||
<script src="lib/amstock.js"></script>
|
||||
<!-- endbuild -->
|
||||
<!-- build:js -->
|
||||
<script src="js/websocket.js"></script>
|
||||
<script src="js/mdot.js"></script>
|
||||
<!-- endbuild -->
|
||||
</body>
|
||||
|
@ -15,7 +15,7 @@ var authentication = require('basic-authentication');
|
||||
|
||||
var cfenv = require('cfenv');
|
||||
|
||||
var WebSocketServer = require('websocket').server;
|
||||
// var WebSocketServer = require('websocket').server;
|
||||
|
||||
var EventEmitter = require('events');
|
||||
var busEmitter = new EventEmitter();
|
||||
@ -43,6 +43,7 @@ logger.warn('isProduction:', isProduction);
|
||||
|
||||
|
||||
var heartBeat = function() {
|
||||
console.log(this);
|
||||
this.pingTimer = 0;
|
||||
this.count = 0;
|
||||
this.rate = 90000;
|
||||
|
@ -65,7 +65,7 @@ var doInsertEntry = (obj) => {
|
||||
dbSave.addNewEvent(obj)
|
||||
.then(function(d) {
|
||||
'use strict';
|
||||
//logger.info('Finished - Raw',d);
|
||||
logger.info('Obj',obj.type);
|
||||
saveToDB(obj);
|
||||
if (obj.type === 'mDot') {
|
||||
busEmitter.emit('sendMessage', obj);
|
||||
|
@ -267,7 +267,7 @@ mdotApi(app);
|
||||
|
||||
//app.get('/api/mdot/:id', mDot.getData);
|
||||
|
||||
app.listen(3000, function() {
|
||||
app.listen(3002, function() {
|
||||
logger.info('Express listening on 3000');
|
||||
restartTimer();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user