”2016-09-29”

This commit is contained in:
Martin Donnelly 2016-09-29 16:45:04 +01:00
parent 22f04e2c28
commit 5d7b75e9e9
39 changed files with 3889 additions and 99 deletions

View File

@ -79,7 +79,11 @@ var heartBeat = function() {
var app = express();
app.set('port', process.env.PORT || 4545);
var port = (process.env.VCAP_APP_PORT || 3010);
var host = (process.env.VCAP_APP_HOST || 'localhost');
app.set('port', port);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(morgan('combined'));
@ -140,8 +144,6 @@ if (isProduction) {
heartBeat();
}
var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost');
app.get('*', function(req, res) {
res.status(404).render('404',{delimiter: '^'});

View File

@ -162,6 +162,8 @@
skipOccupancy = true;
_(events.data).each(function(i) {
var _occupancy = 0;
@ -175,15 +177,20 @@
}
}
var recordedOccupancy = this.findOccupancy(i.timestamp, events.occupancy);
tempCollection.add({
dt: Date.create(i.timestamp).addHours(1),
lux: i.lux,
temp: i.temp,
co2: i.co2,
humid: i.humidity,
noise: i.sound,
occupancy: _occupancy
});
//Dt: Date.create(i.timestamp).addHours(1),
//dt: new Date(i.timestamp).addHours(1),
dt: i.timestamp,
lux: i.lux,
temp: i.temp,
co2: i.co2,
humid: i.humidity,
noise: i.sound,
occupancy: _occupancy,
recordedOccupancy: recordedOccupancy
});
if (_occupancy !== 0) {
const mm = minmaxes[_occupancy.toString()];
@ -305,16 +312,16 @@
if (this.model.has('device')) {
var http = location.protocol;
console.log(location);
var slashes = http.concat("//");
var slashes = http.concat('//');
var host = slashes.concat(window.location.hostname);
if (location.port !== "") {
if (location.port !== '') {
host = host + ':' + location.port;
}
host = 'https://mdotserver.mybluemix.net';
host = ['https://mdotserver.mybluemix.net','http://52.211.111.57'];
this.collection.url = host + '/apiv2/mdot/' + this.model.get('device');
this.collection.url = host[1] + '/apiv2/mdot/' + this.model.get('device');
$('#output').empty();
this.collection.fetch(fetchObj);
} else {
@ -361,11 +368,20 @@
doChartV2: function(chartData) {
console.time('doChartV2');
var chart = new AmCharts.AmStockChart();
chart.categoryAxesSettings.minPeriod = '15mm';
chart.categoryAxesSettings.minPeriod = 'ss';
chart.categoryAxesSettings.minorGridEnabled = true;
chart.theme = 'dark';
chart.categoryAxesSettings.parseDates = true;
//Chart.categoryAxesSettings.dataDateFormats = "DD-MM-YYYY,JJ:NN:SS";
//chart.categoryAxesSettings.dateFormats = "DD-MM-YYYY";
//chart.categoryAxis = new AmCharts.CategoryAxis();
//chart.categoryAxis.parseDates = true;
//chart.categoryAxis.minPeriod = "hh";
chart.theme = 'dark';
var chartColors = ['#ffff66','#ff9933', '#ff66b3', '#80dfff', '#33ffad', '#ff0063']; //Cs1, cs2, cs3, cs4, cs5, cs6,
// DATASETS //////////////////////////////////////////
// create data sets first
var dataSet1 = new AmCharts.DataSet();
@ -389,11 +405,16 @@
{
fromField: 'occupancy',
toField: 'occupancy'
}];
},
{
fromField: 'recordedOccupancy',
toField: 'recordedOccupancy'
}];
dataSet1.dataProvider = chartData;
dataSet1.categoryField = 'date';
// Set data sets to the chart
chart.dataSets = [dataSet1];
@ -404,20 +425,21 @@
stockPanel1.title = 'Environment';
stockPanel1.percentHeight = 60;
// add value axes
// Add value axes
var valueAxis1 = new AmCharts.ValueAxis();
valueAxis1.axisColor = '#b3b3ff';
valueAxis1.color = '#b3b3ff';
valueAxis1.axisColor = chartColors[0];
valueAxis1.color = chartColors[0];
valueAxis1.offset = 0;
valueAxis1.minorGridEnabled = true;
valueAxis1.minorTickLength = 8;
// ValueAxis1.title = '<27>c';
// valueAxis1.titleColor = chartColors[0];
stockPanel1.addValueAxis(valueAxis1);
var valueAxis2 = new AmCharts.ValueAxis();
valueAxis2.position = 'right';
valueAxis2.axisColor = '#ff66b3';
valueAxis2.color = '#ff66b3';
valueAxis2.axisColor = chartColors[1];
valueAxis2.color = chartColors[1];
valueAxis2.offset = 0;
valueAxis2.minorGridEnabled = true;
valueAxis2.minorTickLength = 8;
@ -425,8 +447,8 @@
var valueAxis3 = new AmCharts.ValueAxis();
valueAxis3.position = 'right';
valueAxis3.axisColor = '#e600e6';
valueAxis3.color = '#e600e6';
valueAxis3.axisColor = chartColors[2];
valueAxis3.color = chartColors[2];
valueAxis3.offset = 50;
valueAxis3.minorGridEnabled = true;
valueAxis3.minorTickLength = 8;
@ -434,8 +456,8 @@
var valueAxis4 = new AmCharts.ValueAxis();
valueAxis4.position = 'left';
valueAxis4.axisColor = '#80dfff';
valueAxis4.color = '#80dfff';
valueAxis4.axisColor = chartColors[3];
valueAxis4.color = chartColors[3];
valueAxis4.offset = 50;
valueAxis4.minorGridEnabled = true;
valueAxis4.minorTickLength = 8;
@ -445,12 +467,19 @@
var graph1 = new AmCharts.StockGraph();
graph1.title = 'CO2';
graph1.valueField = 'co2';
graph1.type = 'smoothedLine';
graph1.lineThickness = 2;
graph1.lineColor = '#b3b3ff';
graph1.lineColor = chartColors[0];
graph1.fillColor = chartColors[0];
graph1.fillAlphas = 0.1;
graph1.useDataSetColors = false;
graph1.valueAxis = valueAxis1;
stockPanel1.addStockGraph(graph1);
stockPanel1.categoryAxis.parseDates = true;
stockPanel1.categoryAxis.color = '#ffffff';
// Create stock legend
stockPanel1.stockLegend = new AmCharts.StockLegend();
stockPanel1.stockLegend.color = '#f0f5f5';
@ -459,8 +488,11 @@
var graph2 = new AmCharts.StockGraph();
graph2.title = 'Humidity';
graph2.valueField = 'humid';
graph2.type = 'smoothedLine';
graph2.lineThickness = 2;
graph2.lineColor = '#ff66b3';
graph2.lineColor = chartColors[1];
graph2.fillColor = chartColors[1];
graph2.fillAlphas = 0.1;
graph2.useDataSetColors = false;
graph2.valueAxis = valueAxis2;
stockPanel1.addStockGraph(graph2);
@ -468,8 +500,11 @@
var graph3 = new AmCharts.StockGraph();
graph3.title = 'LUX';
graph3.valueField = 'lux';
graph3.type = 'smoothedLine';
graph3.lineThickness = 2;
graph3.lineColor = '#e600e6';
graph3.lineColor = chartColors[2];
graph3.fillColor = chartColors[2];
graph3.fillAlphas = 0.1;
graph3.useDataSetColors = false;
graph3.valueAxis = valueAxis3;
stockPanel1.addStockGraph(graph3);
@ -477,12 +512,47 @@
var graph4 = new AmCharts.StockGraph();
graph4.title = 'Temperature';
graph4.valueField = 'temp';
graph4.type = 'smoothedLine';
graph4.lineThickness = 2;
graph4.lineColor = '#80dfff';
graph4.lineColor = chartColors[3];
graph4.fillColor = chartColors[3];
graph4.fillAlphas = 0.1;
graph4.useDataSetColors = false;
graph4.valueAxis = valueAxis4;
stockPanel1.addStockGraph(graph4);
stockPanel1.showCategoryAxis = true;
stockPanel1.allLabels = [{
x: 60,
y: 0,
text: 'ppm',
size: 12,
color: chartColors[0]
},
{
x: 760,
y: 0,
text: '%',
size: 12,
color: chartColors[1]
},
{
x: 810,
y: 0,
text: 'LUX',
size: 12,
color: chartColors[2]
},
{
x: 0,
y: 0,
text: 'Celsius',
size: 12,
color: chartColors[3]
}];
// Set panels to the chart
chart.panels = [stockPanel1];
@ -490,28 +560,42 @@
var stockPanel2 = new AmCharts.StockPanel();
stockPanel2.showCategoryAxis = false;
stockPanel2.title = 'Occupancy';
stockPanel2.percentHeight = 60;
stockPanel2.percentHeight = 30;
//StockPanel2.height = 20;
var valueAxis5 = new AmCharts.ValueAxis();
valueAxis5.position = 'left';
valueAxis5.axisColor = '#33ffad';
valueAxis5.color = '#33ffad';
valueAxis5.axisColor = chartColors[4];
valueAxis5.color = chartColors[4];
valueAxis5.offset = 0;
valueAxis5.minorGridEnabled = true;
valueAxis5.minorTickLength = 8;
stockPanel2.addValueAxis(valueAxis5);
var graph5 = new AmCharts.StockGraph();
graph5.title = 'Count';
graph5.type = 'column';
graph5.title = 'Calculated';
graph5.type = 'step';
graph5.valueField = 'occupancy';
graph5.cornerRadiusTop = 1;
graph5.fillAlphas = 1;
graph5.lineColor = '#33ffad';
//Graph5.cornerRadiusTop = 1;
graph5.fillAlphas = 0.2;
graph5.fillColor = chartColors[4];
graph5.lineColor = chartColors[4];
graph5.useDataSetColors = false;
stockPanel2.addStockGraph(graph5);
var graph6 = new AmCharts.StockGraph();
graph6.title = 'Recorded';
graph6.type = 'step';
graph6.valueField = 'recordedOccupancy';
graph6.cornerRadiusTop = 1;
graph6.fillAlphas = 0.2;
graph6.lineColor = chartColors[5];
graph6.fillColor = chartColors[5];
graph6.useDataSetColors = false;
stockPanel2.addStockGraph(graph6);
// Create stock legend
stockPanel2.stockLegend = new AmCharts.StockLegend();
stockPanel2.stockLegend.color = '#f0f5f5';
@ -564,7 +648,8 @@
lux: i.get('lux'),
noise: i.get('noise'),
temp: i.get('temp'),
occupancy: i.get('occupancy')
occupancy: i.get('occupancy'),
recordedOccupancy: i.get('recordedOccupancy')
};
if (filter.length >= max) {
@ -663,16 +748,7 @@
views.co2Widget = new Widget({model: webSocketModel, id: 'widget-co2', el: $('#widget-co2')});
views.humidityWidget = new Widget({model: webSocketModel, id: 'widget-humidity', el: $('#widget-humidity')});
views.luxWidget = new Widget({model: webSocketModel, id: 'widget-lux', el: $('#widget-lux')});
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
views.weather = new Weather({model: new WeatherModel({lat: latitude, long: longitude})});
},
function(e) {console.error(e.code + ' / ' + e.message);});
views.weather = new Weather({model: new WeatherModel({lat: 55.864237, long: -4.251806})});

View File

@ -1,15 +1,16 @@
var WEBSOCKET = function(model) {
var wsUrl = 'ws://localhost';
var wsUrl = ['localhost','mdotserver.mybluemix.net','52.211.111.57'];
var wsPort = 3011;
if ('https:' === document.location.protocol) {
wsUrl = 'wss://mdotserver.mybluemix.net/';
wsPort = '';
} else {
//wsUrl = 'ws://localhost:3001';
wsUrl = 'ws://mdotserver.mybluemix.net/';
wsPort = '';
}
if ('https:' === document.location.protocol) {
wsUrl = 'wss://' + wsUrl[2] + '/';
wsPort = '';
} else {
//wsUrl = 'ws://localhost:3001';
wsUrl = 'ws://' + wsUrl[2] + '/';
wsPort = '';
}
this.socket = null;
this.timer = 0;
@ -18,7 +19,6 @@ var WEBSOCKET = function(model) {
this.startWebSocket = function() {
'use strict';
var url = (wsPort === '') ? wsUrl : wsUrl + ':' + wsPort;
console.log('Starting socket', url);
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;

View File

@ -1,5 +1,5 @@
{
"name": "sodashserver",
"name": "mDotServer",
"description": "Smart Office Dashboard Server",
"main": "index.js",
"authors": [
@ -17,7 +17,7 @@
],
"dependencies": {
"chroma-js": "^1.1.1",
"jquery": "^2.2.3",
"jquery": "^3.1.1",
"mui": "^0.6.8",
"sugarjs-date": "^1.5.1",
"backbone": "^1.3.3",

View File

@ -57,16 +57,16 @@ gulp.task('appJS', function() {
gulp.task('vendor', function() {
return gulp.src(['src/bower_modules/mui/packages/cdn/js/mui.min.js',
'src/bower_modules/jquery/dist/jquery.min.js',
'src/bower_modules/base64/base64.min.js',
'src/bower_modules/underscore/underscore-min.js',
'src/bower_modules/backbone/backbone-min.js',
'src/bower_modules/sugarjs-date/sugar-date.min.js',
'src/bower_modules/notification-js/build/notification.min.js',
'src/bower_modules/amstock3/amcharts/amcharts.js',
'src/bower_modules/amstock3/amcharts/serial.js',
'src/bower_modules/amstock3/amcharts/amstock.js',
return gulp.src(['bower_components/mui/packages/cdn/js/mui.min.js',
'bower_components/jquery/dist/jquery.min.js',
'bower_components/base64/base64.min.js',
'bower_components/underscore/underscore-min.js',
'bower_components/backbone/backbone-min.js',
'bower_components/sugarjs-date/sugar-date.min.js',
'bower_components/notification-js/build/notification.min.js',
'bower_components/amstock3/amcharts/amcharts.js',
'bower_components/amstock3/amcharts/serial.js',
'bower_components/amstock3/amcharts/amstock.js',
'app/lib/themes/dark.js',
'app/lib/dateTime.js'])
.pipe(concat('vendor.js'))

View File

@ -1,20 +1,46 @@
{
"apps": [
{
"name": "SODashServer",
"name": "mDotServer",
"script": "app.js",
"cwd": "/home/azureuser/live",
"cwd": "/home/ubuntu/live/mDotServer",
"watch": true,
"instances": 1,
env: {
"NODE_ENV": "development"
},
env_production : {
"NODE_ENV": "production"
},
"exec_mode": "cluster",
"combine_logs": true,
"max_memory_restart": "150M",
"max_memory_restart": "350M",
"restart_delay": 5000,
"ignore_watch": [
"[\\/\\\\]\\./",
"node_modules",
"server/static"
"dist"
]
}
},{
"name": "mqttArchiver",
"script": "app.js",
"cwd": "/home/ubuntu/live/mqttArchiver",
"watch": true,
"instances": 1,
env: {
"NODE_ENV": "development"
},
env_production : {
"NODE_ENV": "production"
},
"exec_mode": "cluster",
"combine_logs": true,
"max_memory_restart": "350M",
"restart_delay": 5000,
"ignore_watch": [
"[\\/\\\\]\\./",
"node_modules"
]
}
]
}

View File

@ -48,7 +48,7 @@
<div class="mui-col-md-12 ">
<div class=" mui--bg-primary-dark" style="padding-top: 15px;padding-bottom: 7px;display: flex;">
<div class="mui-col-md-3 ">
<i class="material-icons" style="font-size: 66px;color:#00ffb6;">&#xE7F1;</i>
<i class="material-icons" style="font-size: 66px;color:#ff0063;">&#xE7F1;</i>
</div>
<div class="mui-col-md-9 mui--align-middle " style="text-align: center;font-family: 'Ubuntu Condensed', sans-serif;font-size: 30px;line-height: 100%;color:#00ffb6;">
SMART<br>Building
@ -111,7 +111,10 @@
<div class="mui-row ">
<div class="mui-col-md-12" style="height:70%;">
<div class='divfix mui--bg-primary-dark' id="chartdiv"
style="width:100%; margin-bottom: 15px;"></div>
style="width:100%; margin-bottom: 25px;">
</div>
</div>
</div>
</div>

View File

@ -186,7 +186,7 @@
<div class="mui-col-md-12 ">
<div class=" mui--bg-primary-dark" style="padding-top: 15px;padding-bottom: 7px;display: flex;">
<div class="mui-col-md-3 ">
<i class="material-icons" style="font-size: 66px;color:#00ffb6;">&#xE7F1;</i>
<i class="material-icons" style="font-size: 66px;color:#ff0063;">&#xE7F1;</i>
</div>
<div class="mui-col-md-9 mui--align-middle " style="text-align: center;font-family: 'Ubuntu Condensed', sans-serif;font-size: 30px;line-height: 100%;color:#00ffb6;">
SMART<br>Building
@ -249,7 +249,10 @@
<div class="mui-row ">
<div class="mui-col-md-12" style="height:70%;">
<div class='divfix mui--bg-primary-dark' id="chartdiv"
style="width:100%; margin-bottom: 15px;"></div>
style="width:100%; margin-bottom: 25px;">
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,2 @@
node_modules
/sql/

View File

@ -0,0 +1,32 @@
; http://editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.txt]
insert_final_newline = false
trim_trailing_whitespace = false
[*.py]
indent_size = 4
[*.m]
indent_size = 4
[Makefile]
indent_style = tab
indent_size = 8
[*.{js,json}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

View File

@ -0,0 +1,180 @@
# Created by .ignore support plugin (hsz.mobi)
### Archives template
# It's better to unpack these files and commit the raw source because
# git has its own built in compression methods.
*.7z
*.jar
*.rar
*.zip
*.gz
*.bzip
*.bz2
*.xz
*.lzma
*.cab
#packing-only formats
*.iso
*.tar
#package management formats
*.dmg
*.xpi
*.gem
*.egg
*.deb
*.rpm
*.msi
*.msm
*.msp
### Windows template
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
### OSX template
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
*.iml
## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:
# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries
# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml
# Gradle:
# .idea/gradle.xml
# .idea/libraries
# Mongo Explorer plugin:
# .idea/mongoSettings.xml
## File-based project format:
*.ipr
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
### Node template
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
bower_components
### VisualStudioCode template
.settings
### Xcode template
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
build/
DerivedData
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
## Other
*.xccheckout
*.moved-aside
*.xcuserstate
dist

View File

@ -0,0 +1,46 @@
{
"disallowKeywords": ["with"],
"disallowKeywordsOnNewLine": ["else"],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleVarDecl": "exceptUndefined",
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInsideParentheses": true,
"disallowTrailingWhitespace": true,
"maximumLineLength": 160,
"requireCamelCaseOrUpperCaseIdentifiers": false,
"requireCapitalizedComments": true,
"requireCapitalizedConstructors": true,
"requireCurlyBraces": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"typeof"
],
"requireSpaceAfterLineComment": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeObjectValues": true,
"requireSpacesInFunction": {
"beforeOpeningCurlyBrace": true
},
"requireTrailingComma": false,
"requireEarlyReturn": false,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'"
}

View File

@ -0,0 +1,37 @@
{
"predef": [
"Promise",
"$"
],
"globals": {
"$": false,
"MicroEvent": false
},
"node":true,
"browser": true,
"boss": true,
"curly": true,
"debug": false,
"devel": true,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"eqnull": true,
"esnext": true,
"unused": true,
"supernew":true
}

View File

@ -0,0 +1,4 @@
# MQTT Archiver
This app listens to all the available subscriptions for a given MQTT server then archives the raw data in a PGSQL database.

View File

@ -0,0 +1,141 @@
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 sessionLW = require('express-session-lw');
var methodoverride = require('method-override');
var bodyparser = require('body-parser');
var errorhandler = require('errorhandler');
var log4js = require('log4js');
var logger = log4js.getLogger();
var authentication = require('basic-authentication');
var cfenv = require('cfenv');
// var WebSocketServer = require('websocket').server;
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
var mqttClient = require('./lib/mqtt/mqttClient');
var mqtt = new mqttClient.mqttClient(busEmitter);
var historianApi = require('./lib/historian/emulator.js');
var app = express();
var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var isProduction = false;
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (process.env.NODE_ENV === 'production') {
isProduction = true;
}
var restartTimer = 0;
logger.warn('isProduction:', isProduction);
var doRestart = () => {
mqtt = new mqttClient.mqttClient(busEmitter);
};
busEmitter.on('MAJORERROR', () => {
'use strict';
logger.warn('Major error discovered. Restarting MQTT Service in 10 seconds.');
clearTimeout(restartTimer);
restartTimer = setTimeout(doRestart, 10000);
});
var heartBeat = function() {
this.pingTimer = 0;
this.count = 0;
this.rate = 90000;
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(''));
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), mod + 10);
};
this.setupPing();
};
app.set('port', port);
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}));
app.use(sessionLW());
/* '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';
staticDir = 'app';
app.use(express.static(path.join(__dirname, staticDir)));
app.use(errorhandler({dumpExceptions: true, showStack: true}));
// Events and sockets
function originIsAllowed(origin) {
// Put logic here to detect whether the specified origin is allowed.
return true;
}
historianApi(app);
heartBeat();
app.get('*', function(req, res) {
res.status(404).render('404',{delimiter: '^'});
});
app.listen(port, function() {
logger.info('Express listening on ',host, port);
});

View File

@ -0,0 +1,24 @@
{
"name": "sodashserver",
"description": "Smart Office Dashboard Server",
"main": "index.js",
"authors": [
"Martin Donnelly"
],
"license": "ISC",
"homepage": "",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"chroma-js": "^1.1.1",
"jquery": "^2.2.3",
"mui": "^0.5.3",
"sugarjs-date": "^1.5.1"
}
}

View File

@ -0,0 +1,117 @@
'use strict';
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var cssnano = require('gulp-cssnano');
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
var jsmin = require('gulp-jsmin');
var rename = require('gulp-rename');
var concat = require('gulp-concat');
var notify = require('gulp-notify');
var cache = require('gulp-cache');
var livereload = require('gulp-livereload');
var htmlmin = require('gulp-htmlmin');
var inject = require('gulp-inject');
var del = require('del');
var htmlreplace = require('gulp-html-replace');
var googleWebFonts = require('gulp-google-webfonts');
var stripDebug = require('gulp-strip-debug');
var size = require('gulp-size');
var debug = require('gulp-debug');
var filePath = {
build_dir: './dist'
};
gulp.task('scripts', function() {
return gulp.src(['app/js/sowebsocket.js','app/js/colours.js','app/js/appv2.js'])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('app.js'))
// .pipe(stripDebug())
.pipe(jsmin())
.pipe(size({title: 'Scripts'}))
.pipe(gulp.dest('dist/js'));
});
gulp.task('vendor', function() {
return gulp.src(['bower_components/jquery/dist/jquery.min.js',
'bower_components/mui/packages/cdn/js/mui.min.js',
'bower_components/chroma-js/chroma.min.js',
'bower_components/sugar/release/sugar-full.min.js',
'app/lib/skycons.js',
'app/lib/microevent.js'])
.pipe(concat('vendor.js'))
.pipe(uglify({mangle: false}))
.pipe(size({title: 'Vendor'}))
.pipe(gulp.dest('dist/js'));
});
gulp.task('styles', function() {
return gulp.src(['app/css/mui.custom.css','app/css/material-icons.css','app/css/app.css'])
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(cssnano())
.pipe(concat('app.css'))
.pipe(size({title: 'Styles'}))
.pipe(gulp.dest('dist/css'));
});
gulp.task('partials', function() {
// Gulp.src(['app/partials/**/*']).pipe(gulp.dest('dist/partials'));
// gulp.src(['app/libs/ejs_production.js']).pipe(gulp.dest('dist/libs'));
// gulp.src(['app/libs/microevent.js']).pipe(gulp.dest('dist/libs'));
gulp.src(['app/fav/**/*']).pipe(size({title: 'Partials'})).pipe(gulp.dest('dist/fav'));
gulp.src(['app/gfx/censis_logo_white.png']).pipe(gulp.dest('dist/gfx'));
});
gulp.task('migrate', function() {
return gulp.src(['dist/**/*'])
.pipe(debug({title: 'migrate:'}))
.pipe(size({title: 'Migrate'}))
.pipe(gulp.dest('/Users/martin/newdev/SODashApp/www'));
});
gulp.task('index', function() {
var sources = gulp.src(['js/apps.js', 'css/app.css'], {read: false});
return gulp.src(['app/index.html'])
.pipe(htmlreplace({
css: 'css/app.css',
js: 'js/app.js',
vendor: 'js/vendor.js',
fonts: 'fonts/fonts.css'
}))
.pipe(htmlmin({removeComments: true, collapseWhitespace: true, keepClosingSlash: true}))
.pipe(size({title: 'Index'}))
.pipe(gulp.dest('dist/'));
});
var options = { };
gulp.task('fonts', function() {
return gulp.src('./fonts.list')
.pipe(googleWebFonts(options))
.pipe(size({title: 'Fonts'}))
.pipe(gulp.dest('dist/fonts'))
;
});
gulp.task('clean', function() {
return del(['dist']);
});
gulp.task('default', ['clean'], function() {
gulp.start('styles', 'scripts', 'vendor', 'fonts', 'partials', 'index');
});
gulp.task('Cordova', ['clean'], function() {
gulp.start('styles', 'scripts', 'vendor', 'fonts', 'index');
});

View File

@ -0,0 +1,60 @@
/**
*
* User: Martin Donnelly
* Date: 2016-08-31
* Time: 10:03
*
*/
// Historian emulator
'use strict';
var logger = require('log4js').getLogger();
var db = require('../server/db-connector').dbConnection;
var dbHistorian = require('../server/db-historian.js')(db);
module.exports = function(app) {
var express = require('express');
var mdotRouter = express.Router();
mdotRouter.get('/types/:type/devices/:deviceid', function(req, res) {
var data = {};
if (!req.params.type && !req.params.deviceid) {
{
logger.error('Historian','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;
}
data.type = req.params.type;
data.device = req.params.deviceid;
if (data.hasOwnProperty('type') && data.hasOwnProperty('device')) {
dbHistorian.doGet(data)
.then((d) => {
res.json({events:d});
})
.catch((e) => {
logger.error(e);
res.status(500).json({});
});
}
});
app.use('/apiv2/historian/', mdotRouter);
};

View File

@ -0,0 +1,44 @@
/**
*
* User: Martin Donnelly
* Date: 2016-04-08
* Time: 16:35
*
*/
var UltraSES = require('ultrases'), dateFormat = require('dateformat');
var logger = require('log4js').getLogger();
var mailer = new UltraSES({
aws: {
accessKeyId: 'AKIAJWJS75F7WNCGK64A',
secretAccessKey: '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',
region: 'eu-west-1'
}, defaults: {
from: 'Martin Donnelly <martind2000@gmail.com>'
}
});
const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev';
module.exports = {
sendEmailV1: function(contents) {
var now = new Date();
var email = {
to: 'martind2000@gmail.com', subject: 'MQTT ' + prefix + ' Archiver Alert'
};
var msg = '<h1>MQTT ' + prefix + ' Archiver Alert</h1><p>Alert logged at ' + dateFormat(now, "dddd, mmmm dS, yyyy, HH:MM:ss") + '</p><pre>' + contents + '</pre>';
mailer.sendHTML(email, msg, function(err){
if(err) {
logger.error(err);
throw err;
}
logger.info('email sent!');
});
}
};

View File

@ -0,0 +1,130 @@

var IoTFconnector = function (orgId, api_key, auth_token, $rootScope) {
//this.connected = '';
this.clientId = "a:" + orgId + ":" + Date.now();
console.log("clientId: " + this.clientId);
this.hostname = orgId + ".messaging.internetofthings.ibmcloud.com";
this.client = '';
this.initialize = function () {
client = new Messaging.Client(this.hostname, 8883, this.clientId);
client.onMessageArrived = function (msg) {
console.log("Message from :" + msg.destinationName);
};
var connectOptions = new Object();
connectOptions.keepAliveInterval = 3600;
connectOptions.useSSL = true;
connectOptions.userName = api_key;
connectOptions.password = auth_token;
connectOptions.onSuccess = function () {
IoTFconnector.prototype.clientStatus.connected = true;
// $rootScope.$broadcast("clientStatusUpdated", clientStatus);
console.log("MQTT connected to host: " + client.host + " port : " + client.port + " at " + Date.now());
}
connectOptions.onFailure = function (e) {
console.log("MQTT connection failed at " + Date.now() + "\nerror: " + e.errorCode + " : " + e.errorMessage);
}
console.log("about to connect to " + client.host);
client.connect(connectOptions);
//client = new Messaging.Client(this.hostname, 8883, this.clientId);
//// Initialize the Realtime Graph
////var rtGraph = new RealtimeGraph();
//client.onMessageArrived = function(msg) {
// //var topic = msg.destinationName;
// //var payload = JSON.parse(msg.payloadString);
// ////First message, instantiate the graph
// //if (firstMessage) {
// // $('#chart').empty();
// // firstMessage = false;
// // rtGraph.displayChart(null, payload);
// //} else {
// // rtGraph.graphData(payload);
// //}
// console.log("Message from :" + msg.destinationName);
//};
//client.onConnectionLost = function(e) {
// console.log("Connection Lost at " + Date.now() + " : " + e.errorCode + " : " + e.errorMessage);
// this.connect(connectOptions);
//}
//var connectOptions = new Object();
//connectOptions.keepAliveInterval = 3600;
//connectOptions.useSSL = true;
//connectOptions.userName = api_key;
//connectOptions.password = auth_token;
//connectOptions.onSuccess = function() {
// IoTFconnector.prototype.connected = true;
// console.log("MQTT connected to host: " + client.host + " port : " + client.port + " at " + Date.now());
//}
//connectOptions.onFailure = function(e) {
// console.log("MQTT connection failed at " + Date.now() + "\nerror: " + e.errorCode + " : " + e.errorMessage);
//}
//console.log("about to connect to " + client.host);
//client.connect(connectOptions);
}
this.initialize();
//var imageHTML = '<div class="iotdashboardtext">The selected device is not currently sending events to the Internet of Things Foundation</div><br><div class="iotdashboardtext">Select to view historical data or select a different device.</div> <img class="iotimagesMiddle" align="middle" alt="Chart" src="../../images/IOT_Icons_Thing02.svg">';
};
IoTFconnector.prototype.clientStatus = { connected: false, subscribeTopic: '' };;
IoTFconnector.prototype.disconnect = function disconnect() {
client.disconnect();
console.log("Connection closed");
}
IoTFconnector.prototype.connect = function connect() {
// this.initialize();
}
IoTFconnector.prototype.subscribe = function subscribe(deviceId) {
var subscribeOptions = {
qos: 0,
onSuccess: function () {
console.log("subscribed to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
},
onFailure: function () {
console.log("Failed to subscribe to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
console.log("As messages are not available, visualization is not possible");
}
};
if (IoTFconnector.prototype.clientStatus.subscribeTopic != "") {
console.log("Unsubscribing to " + IoTFconnector.prototype.clientStatus.subscribeTopic);
client.unsubscribe(IoTFconnector.prototype.clientStatus.subscribeTopic);
};
IoTFconnector.prototype.clientStatus.subscribeTopic = "iot-2/type/iotsample-ti-cc3200/id/" + deviceId + "/evt/+/fmt/json";
client.subscribe(IoTFconnector.prototype.clientStatus.subscribeTopic, subscribeOptions);
}

View File

@ -0,0 +1,212 @@
'use strict';
var mqtt = require('mqtt');
var logger = require('log4js').getLogger();
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
var requestify = require('requestify');
var db = require('../server/db-connector').dbConnection;
var dbSave = require('../server/db-save')(db);
var pusher = require('../pusher');
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
/*
var nano = require('nano')('http://localhost:5984');
var db_name = 'mqttarchive';
var dbCouch = nano.use(db_name);
*/
//let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message';
const remoteHost = process.env.NODE_ENV === 'production' ? 'http://ec2-52-211-111-57.eu-west-1.compute.amazonaws.com/apiv2/message' : 'http://localhost:3011/apiv2/message';
function dataBuilder(obj) {
var now = new Date();
var newObj = {
device_type: 'mDot', evt_type: 'update', timestamp: {}, evt: {}
};
newObj.device_id = obj.topic.split('/')[4];
newObj.evt = obj;
newObj.timestamp['$date'] = now.getTime();
return newObj;
}
function saveToDB(data) {
// logger.debug('Inserting into couch...');
// Logger.info(util.inspect(obj));
dbCouch.insert(data, function(err, body, header) {
if (err) {
logger.error('Error inserting into couch');
logger.error(err);
return;
}
});
}
var doSendMessage = (obj) => {
'use strict';
requestify.post(remoteHost, obj)
.then(function(response) {
// Get the response body
// logger.debug(response.getBody());
})
.catch(function(e) {
logger.error('doSendMessage', e);
});
};
var doInsertEntry = (obj) => {
// Logger.info('sendSocket: ' + JSON.stringify(obj));
// insertEntry(obj);
dbSave.addNewEvent(obj)
.then(function(d) {
'use strict';
// logger.info('Obj', obj.type);
// saveToDB(obj);
if (obj.type === 'mDot') {
busEmitter.emit('sendMessage', obj);
}
})
.catch(function(e) {
'use strict';
logger.error(e);
});
};
var lastReceived;
var watchDog = 0;
var watchDogHie = 0;
var wdTimedOut = false;
var watchDogTimeout = () => {
'use strict';
var now = new Date();
logger.warn(
'Watchdog timeout. Message has not been received for over 20 minutes.');
logger.warn('Last received:' + lastReceived + 'Now:' + now);
pusher.push(
'Watchdog timeout. Message has not been received for over 20 Minutes. \n\nLast received:' + lastReceived + 'Now:' + now);
wdTimedOut = true;
};
var mqttClient = function() {
var count = 0;
var subscribeTopic;
var orgId = 'qz0da4';
var userName = 'a-qz0da4-dfwwdkmkzr';
var address = '.messaging.internetofthings.ibmcloud.com';
var appKey = '9txJEf3Cjy7hkSOvkv';
var connection = 'mqtt://' + orgId + address;
this.connected = false;
var options = {
keepalive: 10,
clientId: 'a:' + orgId + ':' + Date.now(),
username: userName,
password: new Buffer(appKey),
reconnectPeriod: 1000,
connectTimeout: 30 * 1000
};
logger.info('Connecting to ', connection);
//this.client = mqtt.connect(connection, options);
this.client = mqtt.connect('mqtt://' + orgId + address, options);
this.client.on('connect', function() {
// connected = true;
logger.info('Connected to ', address);
pusher.push('Connected to ' + address);
clearTimeout(watchDog);
watchDog = setTimeout(watchDogTimeout, 1200000);
lastReceived = new Date();
}.bind(this));
this.client.on('connected', function() {
logger.debug('mqttConnect - doConnection - Connected');
});
this.client.on('close', function() {
logger.warn('mqttConnect - Connection closed');
});
this.client.on('offline', function() {
logger.warn('mqttConnect - OFFLINE!');
});
this.client.on('error', function(e) {
logger.error('mqttConnect - error');
logger.error(e);
});
this.client.on('reconnect', function() {
logger.warn('mqttConnect - Attempting to reconnect...');
});
subscribeTopic = 'iot-2/type/+/id/+/evt/+/fmt/json';
//subscribeTopic = 'livingroomTemp';
logger.info('Subscribing:', subscribeTopic);
this.client.subscribe(subscribeTopic);
this.client.on('message', function(topic, message) {
var json = JSON.parse(message.toString());
var topicArray = topic.split('/');
json.topic = topic;
json.type = topicArray[2];
json.device = topicArray[4];
json.event = topicArray[6];
busEmitter.emit('saveData', json);
//console.log(json.type);
if (json.type !== 'Ti-CC3200') {
console.log(json);
clearTimeout(watchDog);
watchDog = setTimeout(watchDogTimeout, 1200000);
if (wdTimedOut) {
var now = new Date();
var s = 'Receiving again :::\n\nLast received: ' + lastReceived + 'Now: ' + now + "\n\n" + message.toString();
logger.warn(s);
pusher.push(s);
wdTimedOut = false;
}
lastReceived = new Date();
}
count++;
}.bind(this));
busEmitter.on('saveData', doInsertEntry);
busEmitter.on('sendMessage', doSendMessage);
this.getCount = function() {
'use strict';
return count;
}
};
module.exports.mqttClient = mqttClient;

View File

@ -0,0 +1,353 @@
/**
* Created by Martin on 08/02/2016.
*/
'use strict';
var mqtt = require('mqtt');
var Messaging = require('mqtt_over_websockets');
var log4js = require('log4js');
var logger = log4js.getLogger();
var mqttConfig = {
orgId: 'qz0da4',
userName: 'martind2000',
appKey: 'MPReoa43',
prefix: 'iot-2/type/Ti-CC3200/id/'
};
function randomString(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
}
/*
Projector: 'ProjectorISP15',
lighting: 'LightingISP15',
heating: 'HeatingISP15',
*/
var live = true;
logger.warn('!!! Live? ', live);
module.exports = {
restartTimer : 0,
pingTimer: 0,
statuses: {},
sockets: null,
wsClient: null,
watches: {},
client: null,
projector: live ? 'Projector' : 'ProjectorISP15',
lighting: live ? 'Lighting' : 'LightingISP15',
heating: live ? 'Heating' : 'HeatingISP15',
myID: 0,
connected: false,
options: {
// Keepalive: 3600,
keepalive: 60,
clientId: 'a:' + mqttConfig.orgId + ':' + Date.now(),
username: mqttConfig.userName,
password: new Buffer(mqttConfig.appKey)
},
lightList: {o: 'frontLight', f: 'frontLight',n: 'backLight', g: 'backLight'},
updateStatus: function(id, packet) {
this.statuses[id] = packet;
logger.info('Statuses:', this.statuses);
},
setEmitter: function(newEmitter) {
this.emitter = newEmitter;
},
doConnection: function(cb) {
console.log('Do connection');
var self = this;
if (this.client === null) {
this.client = mqtt.connect('mqtt://' + 'silvrtree.co.uk', this.options);
this.client.on('connect', function() {
logger.info('Connected to SilvrBroker');
self.connected = this.connected;
});
this.client.on('connected', function() {
logger.debug('mqttConnect - doConnection - Connected');
});
this.client.on('disconnect', function(e) {
logger.error('mqttConnect - doConnection - disconnect');
logger.error(e);
});
this.client.on('error', function(e) {
logger.error('mqttConnect - doConnection - disconnect');
logger.error(e);
});
}
return this;
},
isConnected: function() {
return this.connected;
},
sendCommand: function(deviceID, command) {
var payload = {
id: 'd',
text: command
};
},
projectorOn: function(callback) {
var packet;
console.log('projectorOn:');
var _callback = callback || {};
if (!this.client) {
return -1;
}
packet = {id: this.projector, status: true};
var destinationName = mqttConfig.prefix + this.projector + '/cmd/' + 'ON' + '/fmt/json';
this.client.publish(destinationName, 'ON', _callback);
this.emitter.emit('sendSocket',packet);
logger.debug('Storing status...');
this.updateStatus('projector', packet);
},
projectorOff: function(callback) {
var packet;
var _callback = callback || {};
if (!this.client) {
return -1;
}
packet = {id: this.projector, status: false};
var destinationName = mqttConfig.prefix + this.projector + '/cmd/' + 'OFF' + '/fmt/json';
this.client.publish(destinationName, 'OFF', _callback);
this.emitter.emit('sendSocket',packet);
console.log('Storing status...');
this.updateStatus('projector', packet);
},
projectorCmd: function(id, callback) {
var packet;
var _callback = callback || {};
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.projector + '/cmd/' + id + '/fmt/json';
this.client.publish(destinationName, 'cmd', _callback);
},
heatingOn: function(callback) {
console.log('Turn heating on...');
var _callback = callback || null;
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.heating + '/cmd/' + 'on' + '/fmt/json';
this.client.publish(destinationName, 'ON', _callback);
this.emitter.emit('sendSocket',{id: this.heating, status: true});
},
heatingOff: function(callback) {
var _callback = callback || {};
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.heating + '/cmd/' + 'off' + '/fmt/json';
this.client.publish(destinationName, 'OFF', _callback);
this.emitter.emit('sendSocket',{id: this.heating, status: false});
},
lightingOn: function(id, callback) {
var packet;
console.log('lightingOn:' + id);
var _callback = callback || null;
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.lighting + '/cmd/' + id + '/fmt/json';
this.client.publish(destinationName, 'ON', _callback);
packet = {id: this.lighting, device: id, status: true};
this.emitter.emit('sendSocket',packet);
logger.debug('Storing status...');
this.updateStatus(this.lightList[id], packet);
},
lightingOff: function(id, callback) {
var packet;
var _callback = callback || null;
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.lighting + '/cmd/' + id + '/fmt/json';
this.client.publish(destinationName, 'OFF', _callback);
packet = {id: this.lighting, device: id, status: false};
this.emitter.emit('sendSocket', packet);
this.updateStatus(this.lightList[id], packet);
},
lightingCommand: function(id, callback) {
var packet;
var _callback = callback || null;
if (!this.client) {
return -1;
}
var destinationName = mqttConfig.prefix + this.lighting + '/cmd/' + id + '/fmt/json';
this.client.publish(destinationName, 'cmd', _callback);
},
setupEvents: function() {
this.emitter.on('lightingOn', this.lightingOn);
this.emitter.on('lightingOff', this.lightingOff);
this.emitter.on('heatingOn', this.heatingOn);
this.emitter.on('heatingOff', this.heatingOff);
this.emitter.on('projectorOn', this.projectorOn);
this.emitter.on('projectorOff', this.projectorOff);
},
setupPing: function() {
logger.warn('Starting ping timer...');
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), 10000);
},
ping: function() {
//Logger.error('Ping!');
this.sendRefresh();
var now = new Date;
var mod = 10000 - (now.getTime() % 10000);
setTimeout(function() {this.ping();}.bind(this),mod + 10);
},
sendRefresh: function() {
// logger.debug('+ Send refresh', this.statuses);
for (var item in this.statuses) {
if (this.statuses.hasOwnProperty(item)) {
console.log(this.statuses[item]);
this.emitter.emit('sendSocket', this.statuses[item]);
}
}
// logger.debug('+ Send refresh');
},
preRestartConnection: function() {
logger.debug('Restart connection...');
//this.emitter.emit('restartMQTTSocket', this);
//logger.debug(this);
this.restartMQTTSocket();
},
startMQTTSocket:function() {
logger.warn('Restarting socket?');
this.connectWS();
},
restartMQTTSocket: function() {
this.wsClient.disconnect();
// setTimeout(this.startMQTTSocket.bind(this), 15000);
},
connectWS: function(connectCB) {
logger.debug('Going to connect WS');
var self = this;
var hostname = 'silvrtree.co.uk';
var clientId = 'a:' + 'qz0da4' + ':' + Date.now();
var api_key = 'martind2000';
var auth_token = 'MPReoa43';
this.wsClient =new Messaging.Client(hostname, 8883, clientId);
//var wsClient = new Messaging.Client(hostname, 8883, clientId);
var wsClient = this.wsClient;
var clientStatus = {connected: false, subscribed: false, deviceConnected: false};
var sensorData = {};
this.restartTimer = setTimeout(this.preRestartConnection.bind(this), 60000);
wsClient.onMessageArrived = function(msg) {
//logger.info("Message from :" + msg.destinationName);
clearTimeout(self.restartTimer);
self.restartTimer = setTimeout(self.preRestartConnection.bind(self), 60000);
clientStatus.deviceConnected = true;
sensorData = JSON.parse(msg.payloadString);
// Logger.debug(sensorData);
var temp = msg.destinationName.split('/');
if (self.watches.hasOwnProperty(temp[4])) {
// Logger.info('Emit: ' + JSON.stringify({id:self.watches[temp[4]],sensorData:sensorData}));
self.emitter.emit('sendSocket',{id: self.watches[temp[4]],sensorData: sensorData});
}
};
wsClient.onConnectionLost = function(e) {
logger.error('+ wsClient.onConnectionLost');
logger.error(e);
logger.warn('Going to force a restart of the Socket.. Hold on.');
setTimeout(self.startMQTTSocket.bind(self), 15000);
//self.emitter.emit('sendSocket',{id: 'deviceLost',data: e});
//var wsReconnectTimer = setTimeout(function() {logger.debug('TRYING TO RECONNECT TO MQTT');self.emitter.emit('connectWS');}.bind(this),30000);
logger.error('- wsClient.onConnectionLost');
};
var connectOptions = {};
connectOptions.keepAliveInterval = 3600;
connectOptions.useSSL = true;
connectOptions.userName = api_key;
connectOptions.password = auth_token;
connectOptions.onSuccess = function() {
clientStatus.connected = true;
logger.info('MQTT connected to host: ' + wsClient.host + ' port : ' + wsClient.port + ' at ' + Date.now());
self.emitter.emit('clientStatusUpdated', clientStatus);
self.emitter.emit('clientConnected', self.sockets);
};
connectOptions.onFailure = function(e) {
self.emitter.emit('sendSocket',{id: 'deviceNotConnecting'});
logger.error('wsClient onFailure', e);
logger.error('MQTT connection failed at ' + Date.now() + '\nerror: ' + e.errorCode + ' : ' + e.errorMessage);
//var wsReconnectTimer = setTimeout(function() {logger.debug('TRYING TO RECONNECT TO MQTT');self.emitter.emit('connectWS');}.bind(this),30000);
};
logger.debug('about to connect to ' + wsClient.host);
wsClient.connect(connectOptions);
this.sockets = {
getClientStatus: function() {
return clientStatus;
},
subscribe: function(deviceId, emitterId) {
logger.debug('trying to subscribe');
var subscribeTopic = 'iot-2/type/Ti-CC3200/id/' + deviceId + '/evt/+/fmt/json';
logger.debug(subscribeTopic);
var subscribeOptions = {
qos: 0,
onSuccess: function() {
logger.info('subscribed to ' + subscribeTopic);
clientStatus.subscribed = true;
// $rootScope.$broadcast("clientStatusUpdated", clientStatus);
self.emitter.emit('clientStatusUpdated', clientStatus);
self.watches[deviceId] = emitterId;
},
onFailure: function() {
logger.error('Failed to subscribe to ' + subscribeTopic);
logger.error('As messages are not available, visualization is not possible');
}
};
/*If (IoTFconnector.prototype.clientStatus.subscribeTopic != "") {
console.log("Unsubscribing to " + subscribeTopic);
wsClient.unsubscribe(IoTFconnector.prototype.clientStatus.subscribeTopic);
};*/
wsClient.subscribe(subscribeTopic, subscribeOptions);
}
};
return this.sockets;
}
};

View File

@ -0,0 +1,7 @@
/**
*
* User: Martin Donnelly
* Date: 2016-07-28
* Time: 14:48
*
*/

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
/**
*
* User: Martin Donnelly
* Date: 2016-04-08
* Time: 16:35
*
*/
var Pushover = require('node-pushover'), dateFormat = require('dateformat');
var push = new Pushover({
token: "am7tuw221casnhf7uryx8dhxw6zg1t",
user: "BE2vgFxdHJw91lVGMRYvZDDmVa5cCM"
});
var logger = require('log4js').getLogger();
const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev';
const title = 'MQTT ' + prefix + ' Archiver Alert';
module.exports = {
push : function(contents) {
var now = new Date();
var msg = 'Alert logged at ' + dateFormat(now, "dddd, mmmm dS, yyyy, HH:MM:ss") + '\n\n' + contents + '';
push.send(title, msg);
}
};

View File

@ -0,0 +1,34 @@
'uses strict';
/**
*
* User: Martin Donnelly
* Date: 2016-03-11
* Time: 10:22
*
*/
var pgp = require('pg-promise')();
var localCN = {
host: 'localhost',
port: 5432,
database: 'mqttstore',
user: 'postgres',
password: ''
};
// ElephantSql settings
var remoteCN = {
host: 'jumbo.db.elephantsql.com',
port: 5432,
database: 'vmlcokon',
user: 'vmlcokon',
password: 'PQUYLiIW4M6r7SWyZevrES_rRAULYFkp'
};
const cn = process.env.NODE_ENV === 'production' ? remoteCN : localCN;
exports.dbConnection = pgp(cn);

View File

@ -0,0 +1,106 @@
'use strict';
var logger = require('log4js').getLogger();
var Sugar = require('sugar/date');
module.exports = function(db) {
var module = {};
module.sqlGetAllRaw = function(type, deviceId) {
return new Promise(function(resolve, reject) {
db.any('select distinct on (raw.timestamp) * from raw where type=$1 and device=$2 order by timestamp asc;', [type,deviceId])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.sqlGetRangedRaw = function(params) {
return new Promise(function(resolve, reject) {
db.any('select distinct on (raw.timestamp) * from raw where type=$1 and device=$2 and timestamp between $3 and $4 order by timestamp asc;', [params.type, params.device, params.startTS, params.endTS])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.doGet = function(params) {
var self = this;
var _obj = {};
var useRange=false;
return new Promise(function(resolve, reject) {
logger.debug('historian.doGet', params);
_obj.type = params.type;
_obj.device = params.device;
if (params.hasOwnProperty('start') && params.hasOwnProperty('end')) {
try{
_obj.startTS = new Sugar.Date(parseInt(params.start,10)).raw;
_obj.endTS = new Sugar.Date(parseInt(params.end,10)).raw;
useRange = true;
}
catch(err)
{
logger.error(err);
useRange = false;
}
if ((_obj.start === null) || (_obj.end === null))
{
useRange = false;
}
}
if (useRange) {
logger.info('Userange:',_obj);
self.sqlGetRangedRaw(_obj)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
} else {
self.sqlGetAllRaw(_obj.type, _obj.device)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
}
});
};
return module;
};

View File

@ -0,0 +1,130 @@
'use strict';
var atob = require('atob');
module.exports = function(db) {
var module = {};
module.deviceIds = ['CENSIS-LoRa-1','CENSIS-LoRa-2','CENSIS-LoRa-3','CENSIS-LoRa-4','HIE-mobile-1','HIE-demo','HIE-mobile-2','HIE-smart-campus-1','HIE-smart-campus-2','HIE-smart-campus-3','HIE-smart-campus-4','HIE-smart-campus-5','HIE-smart-campus-6','HIE-smart-campus-7','HIE-mDot-1'];
module.sqlInsertRawEvent = function(data) {
let _data = data;
return new Promise(function(resolve, reject) {
db.func('insert_raw',
[_data.timestamp, _data.data.type, _data.data.device, _data.data.event, _data.data])
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
console.error(err);
return reject(err);
});
});
};
module.sqlInsertDecoded = function(data) {
let _data = data;
return new Promise(function(resolve, reject) {
db.func('insert_decoded',
[_data.deviceid, _data.timestamp, _data.lux, _data.co2, _data.temp, _data.humidity, _data.sound])
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
console.error(err);
return reject(err);
});
});
};
module.addNewEvent = function(data) {
var self = this;
return new Promise((resolve, reject) => {
let _data = {};
_data.timestamp = new Date();
_data.data = data;
self.sqlInsertRawEvent(_data)
.then((d)=> {
//console.log('Postgres returns', d);
return resolve({reply: 'raw event inserted'});
})
.catch((err)=> {
console.error(err);
return reject(err);
});
});
};
module.addProcessedEvent = function(data) {
var self = this;
return new Promise((resolve, reject) => {
let _data = self.rawBreaker(data);
self.sqlInsertDecoded(_data)
.then((d)=> {
// console.log('Postgres returns', d);
return resolve({reply: 'Processed event inserted'});
})
.catch((err)=> {
console.error(err);
return reject(err);
});
});
};
module.decoder = function(data) {
var _obj = {};
var _data = atob(data).split('');
var bytes = _data.map(i => i.charCodeAt());
_obj.light = parseInt('0x' + ('0' + bytes[0]).substr(-2) + ('0' + bytes[1]).substr(-2));
_obj.co2 = parseInt(_data[2] + _data[3] + _data[4] + _data[5] + _data[6], 10);
_obj.temp = (parseInt(_data[7] + _data[8] + _data[9] + _data[10] + _data[11], 10) - 1000) / 10;
_obj.humid = (parseInt(_data[12] + _data[13] + _data[14] + _data[15] + _data[16], 10) / 10);
_obj.noise = parseInt('0x' + ('0' + bytes[17]).substr(-2) + ('0' + bytes[18]).substr(-2));
_obj.binData = bytes;
console.log(_obj);
return _obj;
};
module.rawBreaker = function(data) {
var self = this;
var workObj = {};
var device_name = data.topic.split('/')[4];
console.log('Device_name', device_name);
workObj.deviceid = self.deviceIds.indexOf(device_name);
if (data.hasOwnProperty('data')) {
var _data = self.decoder(data.data);
workObj.lux = _data.light;
workObj.co2 = _data.co2;
workObj.temp = _data.temp;
workObj.humidity = _data.humid;
workObj.sound = _data.noise;
workObj.timestamp = new Date();
return workObj;
} else {
console.error('Data does not have base64 data');
return null;
}
};
return module;
};

View File

@ -0,0 +1,8 @@
applications:
- path: .
memory: 256M
instances: 1
domain: mybluemix.net
name: mqttArchiver
host: mqttarchiver
disk_quota: 1024M

View File

@ -0,0 +1,89 @@
{
"name": "mqttArchiver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"atob": "^2.0.3",
"basic-authentication": "^1.6.2",
"body-parser": "^1.15.2",
"cfenv": "1.0.x",
"cookie-parser": "^1.4.3",
"dateformat": "^1.0.12",
"ejs": "^2.5.1",
"errorhandler": "^1.4.3",
"events": "^1.1.1",
"express": "^4.14.0",
"express-session": "^1.14.1",
"express-session-lw": "^1.0.9",
"http": "0.0.0",
"http-post": "^0.1.1",
"log4js": "^0.6.36",
"method-override": "^2.3.6",
"morgan": "^1.7.0",
"mqtt": "^1.10.0",
"mqtt_over_websockets": "0.0.1",
"node-pushover": "^0.2.2",
"path": "^0.12.7",
"pg-promise": "^5.2.7",
"queue": "^4.0.0",
"request": "^2.72.0",
"requestify": "^0.2.3",
"routes": "^2.1.0",
"sugar": "^2.0.1",
"sugar-date": "^2.0.0",
"ultrases": "^0.1.3",
"websocket": "^1.0.22"
},
"devDependencies": {
"after": "^0.8.1",
"apn": "^1.7.8",
"apns": "^0.1.0",
"basic-authentication": "^1.6.2",
"chai": "^3.5.0",
"cheerio": "^0.20.0",
"clone": "^1.0.2",
"del": "^2.2.0",
"elapsed": "0.0.7",
"gulp": "^3.9.1",
"gulp-autoprefixer": "^3.1.0",
"gulp-cache": "^0.4.5",
"gulp-concat": "^2.6.0",
"gulp-cssmin": "^0.1.7",
"gulp-cssnano": "^2.1.2",
"gulp-debug": "^2.1.2",
"gulp-google-webfonts": "0.0.13",
"gulp-html-replace": "^1.5.5",
"gulp-htmlmin": "^2.0.0",
"gulp-inject": "^4.0.0",
"gulp-jshint": "^2.0.1",
"gulp-jsmin": "^0.1.5",
"gulp-livereload": "^3.8.1",
"gulp-notify": "^2.2.0",
"gulp-rename": "^1.2.2",
"gulp-size": "^2.1.0",
"gulp-strip-debug": "^1.1.0",
"gulp-uglify": "^2.0.0",
"jshint": "^2.9.2",
"jsonfile": "^2.3.1",
"mocha": "^3.0.2",
"mqtt-ws": "^0.2.0",
"nano": "^6.2.0",
"node-cron": "^1.1.1",
"require-dir": "^0.3.0",
"should": "^10.0.0",
"string": "^3.3.1",
"superagent": "^2.1.0",
"supertest": "^2.0.0"
},
"author": "Martin Donnelly <martind2000@gmail.com>",
"license": "ISC",
"engines": {
"node": "6.2.1"
}
}

View File

@ -0,0 +1,26 @@
# Allow any user on the local system to connect to any database with
# any database user name using Unix-domain sockets (the default for local
# connections).
#
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
# The same using local loopback TCP/IP connections.
#
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust
# The same as the previous line, but using a separate netmask column
#
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host all all 127.0.0.1 255.255.255.255 trust
# The same over IPv6.
#
# TYPE DATABASE USER ADDRESS METHOD
host all all ::1/128 trust
# The same using a host name (would typically cover both IPv4 and IPv6).
#
# TYPE DATABASE USER ADDRESS METHOD
host all all localhost trust

View File

@ -0,0 +1,20 @@
{
"apps": [
{
"name": "SODashServer",
"script": "app.js",
"cwd": "/home/azureuser/live",
"watch": true,
"instances": 1,
"exec_mode": "cluster",
"combine_logs": true,
"max_memory_restart": "150M",
"restart_delay": 5000,
"ignore_watch": [
"[\\/\\\\]\\./",
"node_modules",
"server/static"
]
}
]
}

View File

@ -0,0 +1,38 @@
/**
*
* User: Martin Donnelly
* Date: 2016-04-04
* Time: 14:46
*
*/
var exec = require('child_process').exec;
function prepare_db() {
exec('psql -Upostgres -h localhost -f ./mdot.sql', function(err) {
if (err !== null) {
console.log('exec error: ' + err);
return -1;
} else {
console.log('Done?');
}
});
}
function createDB() {
'use strict';
exec('createdb -Upostgres -h localhost mdot', function(err) {
if (err !== null) {
console.log('exec error: ' + err);
return -1;
} else {
prepare_db();
}
});
}
prepare_db();

View File

@ -0,0 +1,20 @@
/**
*
* User: Martin Donnelly
* Date: 2016-06-28
* Time: 14:50
*
*/
var nano = require('nano')('http://localhost:5984');
var db_name = 'mqtt';
// Clean up the database we created previously
nano.db.destroy(db_name, function() {
// Create a new database
nano.db.create(db_name, function() {
// Specify the database we are going to use
var newDB = nano.use(db_name);
});
});

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>404</title>
<link href="css/mui.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="mui-container">
<div class="mui-panel">
<div class="mui--text-dark-secondary mui--text-display4">404</div>
<div class="mui--text-dark-secondary mui--text-display2">Not Found</div>
</div>
</div>
</body>
</html>

View File

@ -28,7 +28,7 @@ var historianApi = require('./lib/historian/emulator.js');
var app = express();
var port = (process.env.VCAP_APP_PORT || 3010);
var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var isProduction = false;
@ -57,7 +57,6 @@ busEmitter.on('MAJORERROR', () => {
var heartBeat = function() {
console.log(this);
this.pingTimer = 0;
this.count = 0;
this.rate = 90000;

View File

@ -20,7 +20,8 @@ var pusher = require('../pusher');
var dbCouch = nano.use(db_name);
*/
let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message';
//let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message';
const remoteHost = process.env.NODE_ENV === 'production' ? 'http://ec2-52-211-111-57.eu-west-1.compute.amazonaws.com/apiv2/message' : 'http://localhost:3011/apiv2/message';
function dataBuilder(obj) {
@ -177,7 +178,7 @@ var mqttClient = function() {
json.device = topicArray[4];
json.event = topicArray[6];
//busEmitter.emit('saveData', json);
busEmitter.emit('saveData', json);
//console.log(json.type);
if (json.type !== 'Ti-CC3200') {
console.log(json);

View File

@ -27,18 +27,6 @@ var remoteCN = {
password: 'PQUYLiIW4M6r7SWyZevrES_rRAULYFkp'
};
var amazonCN = {
dbinstance:'CensisMDot',
host: 'jumbo.db.elephantsql.com',
port: 5432,
database: 'vmlcokon',
user: 'censis_Lm5zt9BYD',
password: '1bR9nJoC5cFRcO84lu5mzmA7MeKyRW'
};
const cn = process.env.NODE_ENV === 'production' ? remoteCN : localCN;