diff --git a/mdot/mDotServer.censis/mDotServer.censis/.vs/HIE/v14/.suo b/mdot/mDotServer.censis/mDotServer.censis/.vs/HIE/v14/.suo index 8c8e1f5..81bd0a6 100644 Binary files a/mdot/mDotServer.censis/mDotServer.censis/.vs/HIE/v14/.suo and b/mdot/mDotServer.censis/mDotServer.censis/.vs/HIE/v14/.suo differ diff --git a/mdot/mDotServer.censis/mDotServer.censis/app.js b/mdot/mDotServer.censis/mDotServer.censis/app.js index d53cc76..d89e5a5 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/app.js +++ b/mdot/mDotServer.censis/mDotServer.censis/app.js @@ -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: '^'}); diff --git a/mdot/mDotServer.censis/mDotServer.censis/app/js/mdot.js b/mdot/mDotServer.censis/mDotServer.censis/app/js/mdot.js index 61d4e50..5c1a420 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/app/js/mdot.js +++ b/mdot/mDotServer.censis/mDotServer.censis/app/js/mdot.js @@ -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 = '�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})}); diff --git a/mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js b/mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js index 480809a..a6472ef 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js +++ b/mdot/mDotServer.censis/mDotServer.censis/app/js/websocket.js @@ -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; diff --git a/mdot/mDotServer.censis/mDotServer.censis/bower.json b/mdot/mDotServer.censis/mDotServer.censis/bower.json index 5bd378f..00775d2 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/bower.json +++ b/mdot/mDotServer.censis/mDotServer.censis/bower.json @@ -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", diff --git a/mdot/mDotServer.censis/mDotServer.censis/gulpfile.js b/mdot/mDotServer.censis/mDotServer.censis/gulpfile.js index dd9255e..2b1d80c 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/gulpfile.js +++ b/mdot/mDotServer.censis/mDotServer.censis/gulpfile.js @@ -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')) diff --git a/mdot/mDotServer.censis/mDotServer.censis/process.json b/mdot/mDotServer.censis/mDotServer.censis/process.json index 0626314..8d7eafa 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/process.json +++ b/mdot/mDotServer.censis/mDotServer.censis/process.json @@ -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" + ] + } ] } diff --git a/mdot/mDotServer.censis/mDotServer.censis/views/graph-release.ejs b/mdot/mDotServer.censis/mDotServer.censis/views/graph-release.ejs index a162cf2..966dd34 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/views/graph-release.ejs +++ b/mdot/mDotServer.censis/mDotServer.censis/views/graph-release.ejs @@ -48,7 +48,7 @@
- +
SMART
Building @@ -111,7 +111,10 @@
+ style="width:100%; margin-bottom: 25px;"> + + +
diff --git a/mdot/mDotServer.censis/mDotServer.censis/views/test2.ejs b/mdot/mDotServer.censis/mDotServer.censis/views/test2.ejs index 6cd4832..c53607b 100644 --- a/mdot/mDotServer.censis/mDotServer.censis/views/test2.ejs +++ b/mdot/mDotServer.censis/mDotServer.censis/views/test2.ejs @@ -186,7 +186,7 @@
- +
SMART
Building @@ -249,7 +249,10 @@
+ style="width:100%; margin-bottom: 25px;"> + + +
diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/.cfignore b/mdot/mqttArchiver.censis/mqttArchiver.censis/.cfignore new file mode 100644 index 0000000..7bca568 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/.cfignore @@ -0,0 +1,2 @@ +node_modules +/sql/ diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/.editorconfig b/mdot/mqttArchiver.censis/mqttArchiver.censis/.editorconfig new file mode 100644 index 0000000..e86f5fa --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/.editorconfig @@ -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 diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/.gitignore b/mdot/mqttArchiver.censis/mqttArchiver.censis/.gitignore new file mode 100644 index 0000000..3b6a41e --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/.gitignore @@ -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 diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/.jscsrc b/mdot/mqttArchiver.censis/mqttArchiver.censis/.jscsrc new file mode 100644 index 0000000..480fcc4 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/.jscsrc @@ -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": "'" +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/.jshintrc b/mdot/mqttArchiver.censis/mqttArchiver.censis/.jshintrc new file mode 100644 index 0000000..375085b --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/.jshintrc @@ -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 +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/README.md b/mdot/mqttArchiver.censis/mqttArchiver.censis/README.md new file mode 100644 index 0000000..0d6d067 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/README.md @@ -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. + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/app.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/app.js new file mode 100644 index 0000000..e85a5cd --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/app.js @@ -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); +}); + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/bower.json b/mdot/mqttArchiver.censis/mqttArchiver.censis/bower.json new file mode 100644 index 0000000..b0e93cc --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/bower.json @@ -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" + } +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/gulpfile.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/gulpfile.js new file mode 100644 index 0000000..01696c7 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/gulpfile.js @@ -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'); + }); diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/historian/emulator.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/historian/emulator.js new file mode 100644 index 0000000..b2f1e71 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/historian/emulator.js @@ -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); +}; + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mailer.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mailer.js new file mode 100644 index 0000000..b456f2a --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mailer.js @@ -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 ' + } +}); + +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 = '

MQTT ' + prefix + ' Archiver Alert

Alert logged at ' + dateFormat(now, "dddd, mmmm dS, yyyy, HH:MM:ss") + '

' + contents + '
'; + + mailer.sendHTML(email, msg, function(err){ + if(err) { + logger.error(err); + throw err; + } + logger.info('email sent!'); + }); + + + } +}; diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/IoTFconnector.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/IoTFconnector.js new file mode 100644 index 0000000..146f228 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/IoTFconnector.js @@ -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 = '
The selected device is not currently sending events to the Internet of Things Foundation

Select to view historical data or select a different device.
Chart'; + +}; + + + + +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); +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttClient.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttClient.js new file mode 100644 index 0000000..e3244f6 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttClient.js @@ -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; diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttConnect.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttConnect.js new file mode 100644 index 0000000..75dd618 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttConnect.js @@ -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; + + } + + + }; + + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttSocket.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttSocket.js new file mode 100644 index 0000000..4c784a0 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttSocket.js @@ -0,0 +1,7 @@ +/** + * + * User: Martin Donnelly + * Date: 2016-07-28 + * Time: 14:48 + * + */ diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttws31.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttws31.js new file mode 100644 index 0000000..a1eca98 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/mqtt/mqttws31.js @@ -0,0 +1,1791 @@ +"use strict"; +exports.Messaging = function (global) { + + // Private variables below, these are only visible inside the function closure + // which is used to define the module. + + var version = "0.0.0.0"; + var buildLevel = "p000-L130522.1"; + + /** + * Unique message type identifiers, with associated + * associated integer values. + * @private + */ + var MESSAGE_TYPE = { + CONNECT: 1, + CONNACK: 2, + PUBLISH: 3, + PUBACK: 4, + PUBREC: 5, + PUBREL: 6, + PUBCOMP: 7, + SUBSCRIBE: 8, + SUBACK: 9, + UNSUBSCRIBE: 10, + UNSUBACK: 11, + PINGREQ: 12, + PINGRESP: 13, + DISCONNECT: 14 + }; + + // Collection of utility methods used to simplify module code + // and promote the DRY pattern. + + /** + * Validate an object's parameter names to ensure they + * match a list of expected variables name for this option + * type. Used to ensure option object passed into the API don't + * contain erroneous parameters. + * @param {Object} obj User options object + * @param {key:type, key2:type, ...} valid keys and types that may exist in obj. + * @throws {Error} Invalid option parameter found. + * @private + */ + var validate = function(obj, keys) { + for(key in obj) { + if (obj.hasOwnProperty(key)) { + if (keys.hasOwnProperty(key)) { + if (typeof obj[key] !== keys[key]) + throw new Error(format(ERROR.INVALID_TYPE, [typeof obj[key], key])); + } else { + var errorStr = "Unknown property, " + key + ". Valid properties are:"; + for (key in keys) + if (keys.hasOwnProperty(key)) + errorStr = errorStr+" "+key; + throw new Error(errorStr); + } + } + } + }; + + /** + * Return a new function which runs the user function bound + * to a fixed scope. + * @param {function} User function + * @param {object} Function scope + * @return {function} User function bound to another scope + * @private + */ + var scope = function (f, scope) { + return function () { + return f.apply(scope, arguments); + }; + }; + + /** + * Unique message type identifiers, with associated + * associated integer values. + * @private + */ + var ERROR = { + OK: {code:0, text:"AMQJSC0000I OK."}, + CONNECT_TIMEOUT: {code:1, text:"AMQJSC0001E Connect timed out."}, + SUBSCRIBE_TIMEOUT: {code:2, text:"AMQJS0002E Subscribe timed out."}, + UNSUBSCRIBE_TIMEOUT: {code:3, text:"AMQJS0003E Unsubscribe timed out."}, + PING_TIMEOUT: {code:4, text:"AMQJS0004E Ping timed out."}, + INTERNAL_ERROR: {code:5, text:"AMQJS0005E Internal error."}, + CONNACK_RETURNCODE: {code:6, text:"AMQJS0006E Bad Connack return code:{0} {1}."}, + SOCKET_ERROR: {code:7, text:"AMQJS0007E Socket error:{0}."}, + SOCKET_CLOSE: {code:8, text:"AMQJS0008I Socket closed."}, + MALFORMED_UTF: {code:9, text:"AMQJS0009E Malformed UTF data:{0} {1} {2}."}, + UNSUPPORTED: {code:10, text:"AMQJS0010E {0} is not supported by this browser."}, + INVALID_STATE: {code:11, text:"AMQJS0011E Invalid state {0}."}, + INVALID_TYPE: {code:12, text:"AMQJS0012E Invalid type {0} for {1}."}, + INVALID_ARGUMENT: {code:13, text:"AMQJS0013E Invalid argument {0} for {1}."}, + UNSUPPORTED_OPERATION: {code:14, text:"AMQJS0014E Unsupported operation."}, + INVALID_STORED_DATA: {code:15, text:"AMQJS0015E Invalid data in local storage key={0} value={1}."}, + INVALID_MQTT_MESSAGE_TYPE: {code:16, text:"AMQJS0016E Invalid MQTT message type {0}."}, + MALFORMED_UNICODE: {code:17, text:"AMQJS0017E Malformed Unicode string:{0} {1}."}, + }; + + /** CONNACK RC Meaning. */ + var CONNACK_RC = { + 0:"Connection Accepted", + 1:"Connection Refused: unacceptable protocol version", + 2:"Connection Refused: identifier rejected", + 3:"Connection Refused: server unavailable", + 4:"Connection Refused: bad user name or password", + 5:"Connection Refused: not authorized" + }; + + /** + * Format an error message text. + * @private + * @param {error} ERROR.KEY value above. + * @param {substitutions} [array] substituted into the text. + * @return the text with the substitutions made. + */ + var format = function(error, substitutions) { + var text = error.text; + if (substitutions) { + for (var i=0; i 0) { + var part1 = text.substring(0,start); + var part2 = text.substring(start+field.length); + text = part1+substitutions[i]+part2; + } + } + } + return text; + }; + + //MQTT protocol and version 6 M Q I s d p 3 + var MqttProtoIdentifier = [0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03]; + + /** + * @ignore + * Construct an MQTT wire protocol message. + * @param type MQTT packet type. + * @param options optional wire message attributes. + * + * Optional properties + * + * messageIdentifier: message ID in the range [0..65535] + * payloadMessage: Application Message - PUBLISH only + * connectStrings: array of 0 or more Strings to be put into the CONNECT payload + * topics: array of strings (SUBSCRIBE, UNSUBSCRIBE) + * requestQoS: array of QoS values [0..2] + * + * "Flag" properties + * cleanSession: true if present / false if absent (CONNECT) + * willMessage: true if present / false if absent (CONNECT) + * isRetained: true if present / false if absent (CONNECT) + * userName: true if present / false if absent (CONNECT) + * password: true if present / false if absent (CONNECT) + * keepAliveInterval: integer [0..65535] (CONNECT) + * + * @private + */ + var WireMessage = function (type, options) { + this.type = type; + for(name in options) { + if (options.hasOwnProperty(name)) { + this[name] = options[name]; + } + } + }; + + WireMessage.prototype.encode = function() { + // Compute the first byte of the fixed header + var first = ((this.type & 0x0f) << 4); + + /* + * Now calculate the length of the variable header + payload by adding up the lengths + * of all the component parts + */ + + remLength = 0; + topicStrLength = new Array(); + + // if the message contains a messageIdentifier then we need two bytes for that + if (this.messageIdentifier != undefined) + remLength += 2; + + switch(this.type) { + // If this a Connect then we need to include 12 bytes for its header + case MESSAGE_TYPE.CONNECT: + remLength += MqttProtoIdentifier.length + 3; + remLength += UTF8Length(this.clientId) + 2; + if (this.willMessage != undefined) { + remLength += UTF8Length(this.willMessage.destinationName) + 2; + // Will message is always a string, sent as UTF-8 characters with a preceding length. + var willMessagePayloadBytes = this.willMessage.payloadBytes; + if (!(willMessagePayloadBytes instanceof Uint8Array)) + willMessagePayloadBytes = new Uint8Array(payloadBytes); + remLength += willMessagePayloadBytes.byteLength +2; + } + if (this.userName != undefined) + remLength += UTF8Length(this.userName) + 2; + if (this.password != undefined) + remLength += UTF8Length(this.password) + 2; + break; + + // Subscribe, Unsubscribe can both contain topic strings + case MESSAGE_TYPE.SUBSCRIBE: + first |= 0x02; // Qos = 1; + for ( var i = 0; i < this.topics.length; i++) { + topicStrLength[i] = UTF8Length(this.topics[i]); + remLength += topicStrLength[i] + 2; + } + remLength += this.requestedQos.length; // 1 byte for each topic's Qos + // QoS on Subscribe only + break; + + case MESSAGE_TYPE.UNSUBSCRIBE: + first |= 0x02; // Qos = 1; + for ( var i = 0; i < this.topics.length; i++) { + topicStrLength[i] = UTF8Length(this.topics[i]); + remLength += topicStrLength[i] + 2; + } + break; + + case MESSAGE_TYPE.PUBLISH: + if (this.payloadMessage.duplicate) first |= 0x08; + first = first |= (this.payloadMessage.qos << 1); + if (this.payloadMessage.retained) first |= 0x01; + destinationNameLength = UTF8Length(this.payloadMessage.destinationName); + remLength += destinationNameLength + 2; + var payloadBytes = this.payloadMessage.payloadBytes; + remLength += payloadBytes.byteLength; + if (payloadBytes instanceof ArrayBuffer) + payloadBytes = new Uint8Array(payloadBytes); + else if (!(payloadBytes instanceof Uint8Array)) + payloadBytes = new Uint8Array(payloadBytes.buffer); + break; + + case MESSAGE_TYPE.DISCONNECT: + break; + + default: + ; + } + + // Now we can allocate a buffer for the message + + var mbi = encodeMBI(remLength); // Convert the length to MQTT MBI format + var pos = mbi.length + 1; // Offset of start of variable header + var buffer = new ArrayBuffer(remLength + pos); + var byteStream = new Uint8Array(buffer); // view it as a sequence of bytes + + //Write the fixed header into the buffer + byteStream[0] = first; + byteStream.set(mbi,1); + + // If this is a PUBLISH then the variable header starts with a topic + if (this.type == MESSAGE_TYPE.PUBLISH) + pos = writeString(this.payloadMessage.destinationName, destinationNameLength, byteStream, pos); + // If this is a CONNECT then the variable header contains the protocol name/version, flags and keepalive time + + else if (this.type == MESSAGE_TYPE.CONNECT) { + byteStream.set(MqttProtoIdentifier, pos); + pos += MqttProtoIdentifier.length; + var connectFlags = 0; + if (this.cleanSession) + connectFlags = 0x02; + if (this.willMessage != undefined ) { + connectFlags |= 0x04; + connectFlags |= (this.willMessage.qos<<3); + if (this.willMessage.retained) { + connectFlags |= 0x20; + } + } + if (this.userName != undefined) + connectFlags |= 0x80; + if (this.password != undefined) + connectFlags |= 0x40; + byteStream[pos++] = connectFlags; + pos = writeUint16 (this.keepAliveInterval, byteStream, pos); + } + + // Output the messageIdentifier - if there is one + if (this.messageIdentifier != undefined) + pos = writeUint16 (this.messageIdentifier, byteStream, pos); + + switch(this.type) { + case MESSAGE_TYPE.CONNECT: + pos = writeString(this.clientId, UTF8Length(this.clientId), byteStream, pos); + if (this.willMessage != undefined) { + pos = writeString(this.willMessage.destinationName, UTF8Length(this.willMessage.destinationName), byteStream, pos); + pos = writeUint16(willMessagePayloadBytes.byteLength, byteStream, pos); + byteStream.set(willMessagePayloadBytes, pos); + pos += willMessagePayloadBytes.byteLength; + + } + if (this.userName != undefined) + pos = writeString(this.userName, UTF8Length(this.userName), byteStream, pos); + if (this.password != undefined) + pos = writeString(this.password, UTF8Length(this.password), byteStream, pos); + break; + + case MESSAGE_TYPE.PUBLISH: + // PUBLISH has a text or binary payload, if text do not add a 2 byte length field, just the UTF characters. + byteStream.set(payloadBytes, pos); + + break; + +// case MESSAGE_TYPE.PUBREC: +// case MESSAGE_TYPE.PUBREL: +// case MESSAGE_TYPE.PUBCOMP: +// break; + + case MESSAGE_TYPE.SUBSCRIBE: + // SUBSCRIBE has a list of topic strings and request QoS + for (var i=0; i> 4; + var messageInfo = first &= 0x0f; + var pos = 1; + + + // Decode the remaining length (MBI format) + + var digit; + var remLength = 0; + var multiplier = 1; + do { + digit = input[pos++]; + remLength += ((digit & 0x7F) * multiplier); + multiplier *= 128; + } while ((digit & 0x80) != 0); + + var wireMessage = new WireMessage(type); + switch(type) { + case MESSAGE_TYPE.CONNACK: + wireMessage.topicNameCompressionResponse = input[pos++]; + wireMessage.returnCode = input[pos++]; + break; + + case MESSAGE_TYPE.PUBLISH: + var qos = (messageInfo >> 1) & 0x03; + + var len = readUint16(input, pos); + pos += 2; + var topicName = parseUTF8(input, pos, len); + pos += len; + // If QoS 1 or 2 there will be a messageIdentifier + if (qos > 0) { + wireMessage.messageIdentifier = readUint16(input, pos); + pos += 2; + } + + var message = new Messaging.Message(input.subarray(pos)); + if ((messageInfo & 0x01) == 0x01) + message.retained = true; + if ((messageInfo & 0x08) == 0x08) + message.duplicate = true; + message.qos = qos; + message.destinationName = topicName; + wireMessage.payloadMessage = message; + break; + + case MESSAGE_TYPE.PUBACK: + case MESSAGE_TYPE.PUBREC: + case MESSAGE_TYPE.PUBREL: + case MESSAGE_TYPE.PUBCOMP: + case MESSAGE_TYPE.UNSUBACK: + wireMessage.messageIdentifier = readUint16(input, pos); + break; + + case MESSAGE_TYPE.SUBACK: + wireMessage.messageIdentifier = readUint16(input, pos); + pos += 2; + wireMessage.grantedQos = input.subarray(pos); + break; + + default: + ; + } + + return wireMessage; + } + + function writeUint16(input, buffer, offset) { + buffer[offset++] = input >> 8; //MSB + buffer[offset++] = input % 256; //LSB + return offset; + } + + function writeString(input, utf8Length, buffer, offset) { + offset = writeUint16(utf8Length, buffer, offset); + stringToUTF8(input, buffer, offset); + return offset + utf8Length; + } + + function readUint16(buffer, offset) { + return 256*buffer[offset] + buffer[offset+1]; + } + + /** + * Encodes an MQTT Multi-Byte Integer + * @private + */ + function encodeMBI(number) { + var output = new Array(1); + var numBytes = 0; + + do { + var digit = number % 128; + number = number >> 7; + if (number > 0) { + digit |= 0x80; + } + output[numBytes++] = digit; + } while ( (number > 0) && (numBytes<4) ); + + return output; + } + + /** + * Takes a String and calculates its length in bytes when encoded in UTF8. + * @private + */ + function UTF8Length(input) { + var output = 0; + for (var i = 0; i 0x7FF) + { + // Surrogate pair means its a 4 byte character + if (0xD800 <= charCode && charCode <= 0xDBFF) + { + i++; + output++; + } + output +=3; + } + else if (charCode > 0x7F) + output +=2; + else + output++; + } + return output; + } + + /** + * Takes a String and writes it into an array as UTF8 encoded bytes. + * @private + */ + function stringToUTF8(input, output, start) { + var pos = start; + for (var i = 0; i>6 & 0x1F | 0xC0; + output[pos++] = charCode & 0x3F | 0x80; + } else if (charCode <= 0xFFFF) { + output[pos++] = charCode>>12 & 0x0F | 0xE0; + output[pos++] = charCode>>6 & 0x3F | 0x80; + output[pos++] = charCode & 0x3F | 0x80; + } else { + output[pos++] = charCode>>18 & 0x07 | 0xF0; + output[pos++] = charCode>>12 & 0x3F | 0x80; + output[pos++] = charCode>>6 & 0x3F | 0x80; + output[pos++] = charCode & 0x3F | 0x80; + }; + } + return output; + } + + function parseUTF8(input, offset, length) { + var output = ""; + var utf16; + var pos = offset; + + while (pos < offset+length) + { + var byte1 = input[pos++]; + if (byte1 < 128) + utf16 = byte1; + else + { + var byte2 = input[pos++]-128; + if (byte2 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16),""])); + if (byte1 < 0xE0) // 2 byte character + utf16 = 64*(byte1-0xC0) + byte2; + else + { + var byte3 = input[pos++]-128; + if (byte3 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16)])); + if (byte1 < 0xF0) // 3 byte character + utf16 = 4096*(byte1-0xE0) + 64*byte2 + byte3; + else + { + var byte4 = input[pos++]-128; + if (byte4 < 0) + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); + if (byte1 < 0xF8) // 4 byte character + utf16 = 262144*(byte1-0xF0) + 4096*byte2 + 64*byte3 + byte4; + else // longer encodings are not supported + throw new Error(format(ERROR.MALFORMED_UTF, [byte1.toString(16), byte2.toString(16), byte3.toString(16), byte4.toString(16)])); + } + } + } + + if (utf16 > 0xFFFF) // 4 byte character - express as a surrogate pair + { + utf16 -= 0x10000; + output += String.fromCharCode(0xD800 + (utf16 >> 10)); // lead character + utf16 = 0xDC00 + (utf16 & 0x3FF); // trail character + } + output += String.fromCharCode(utf16); + } + return output; + } + + /** @ignore Repeat keepalive requests, monitor responses.*/ + var Pinger = function(client, window, keepAliveInterval) { + this._client = client; + this._window = window; + this._keepAliveInterval = keepAliveInterval*1000; + this.isReset = false; + + var pingReq = new WireMessage(MESSAGE_TYPE.PINGREQ).encode(); + + var doTimeout = function (pinger) { + return function () { + return doPing.apply(pinger); + }; + }; + + /** @ignore */ + var doPing = function() { + if (!this.isReset) { + this._client._trace("Pinger.doPing", "Timed out"); + this._client._disconnected( ERROR.PING_TIMEOUT.code , format(ERROR.PING_TIMEOUT)); + } else { + this.isReset = false; + this._client._trace("Pinger.doPing", "send PINGREQ"); + this._client.socket.send(pingReq); + this.timeout = this._window.setTimeout(doTimeout(this), this._keepAliveInterval); + } + } + + this.reset = function() { + this.isReset = true; + this._window.clearTimeout(this.timeout); + if (this._keepAliveInterval > 0) + this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval); + } + + this.cancel = function() { + this._window.clearTimeout(this.timeout); + } + }; + + /** @ignore Monitor request completion. */ + var Timeout = function(client, window, timeoutSeconds, action, args) { + this._window = window; + if (!timeoutSeconds) + timeoutSeconds = 30; + + var doTimeout = function (action, client, args) { + return function () { + return action.apply(client, args); + }; + }; + this.timeout = setTimeout(doTimeout(action, client, args), timeoutSeconds * 1000); + + this.cancel = function() { + this._window.clearTimeout(this.timeout); + } + }; + + /* + * Internal implementation of the Websockets MQTT V3.1 client. + * + * @name Messaging.ClientImpl @constructor + * @param {String} host the DNS nameof the webSocket host. + * @param {Number} port the port number for that host. + * @param {String} clientId the MQ client identifier. + */ + var ClientImpl = function (host, port, clientId) { + // Check dependencies are satisfied in this browser. + if (!("WebSocket" in global && global["WebSocket"] !== null)) { + throw new Error(format(ERROR.UNSUPPORTED, ["WebSocket"])); + } + if (!("localStorage" in global && global["localStorage"] !== null)) { + throw new Error(format(ERROR.UNSUPPORTED, ["localStorage"])); + } + if (!("ArrayBuffer" in global && global["ArrayBuffer"] !== null)) { + throw new Error(format(ERROR.UNSUPPORTED, ["ArrayBuffer"])); + } + + this._trace("Messaging.Client", host, port, clientId); + + this.host = host; + this.port = port; + this.clientId = clientId; + + // Local storagekeys are qualified with the following string. + this._localKey=host+":"+port+":"+clientId+":"; + + // Create private instance-only message queue + // Internal queue of messages to be sent, in sending order. + this._msg_queue = []; + + // Messages we have sent and are expecting a response for, indexed by their respective message ids. + this._sentMessages = {}; + + // Messages we have received and acknowleged and are expecting a confirm message for + // indexed by their respective message ids. + this._receivedMessages = {}; + + // Internal list of callbacks to be executed when messages + // have been successfully sent over web socket, e.g. disconnect + // when it doesn't have to wait for ACK, just message is dispatched. + this._notify_msg_sent = {}; + + // Unique identifier for SEND messages, incrementing + // counter as messages are sent. + this._message_identifier = 1; + + // Used to determine the transmission sequence of stored sent messages. + this._sequence = 0; + + + // Load the local state, if any, from the saved version, only restore state relevant to this client. + for(key in localStorage) + if ( key.indexOf("Sent:"+this._localKey) == 0 + || key.indexOf("Received:"+this._localKey) == 0) + this.restore(key); + }; + + // Messaging Client public instance members. + ClientImpl.prototype.host; + ClientImpl.prototype.port; + ClientImpl.prototype.clientId; + + // Messaging Client private instance members. + ClientImpl.prototype.socket; + /* true once we have received an acknowledgement to a CONNECT packet. */ + ClientImpl.prototype.connected = false; + /* The largest message identifier allowed, may not be larger than 2**16 but + * if set smaller reduces the maximum number of outbound messages allowed. + */ + ClientImpl.prototype.maxMessageIdentifier = 65536; + ClientImpl.prototype.connectOptions; + ClientImpl.prototype.hostIndex; + ClientImpl.prototype.onConnectionLost; + ClientImpl.prototype.onMessageDelivered; + ClientImpl.prototype.onMessageArrived; + ClientImpl.prototype._msg_queue = null; + ClientImpl.prototype._connectTimeout; + /* Send keep alive messages. */ + ClientImpl.prototype.pinger = null; + + ClientImpl.prototype._traceBuffer = null; + ClientImpl.prototype._MAX_TRACE_ENTRIES = 100; + + ClientImpl.prototype.connect = function (connectOptions) { + var connectOptionsMasked = this._traceMask(connectOptions, "password"); + this._trace("Client.connect", connectOptionsMasked, this.socket, this.connected); + + if (this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["already connected"])); + if (this.socket) + throw new Error(format(ERROR.INVALID_STATE, ["already connected"])); + + this.connectOptions = connectOptions; + + if (connectOptions.hosts) { + this.hostIndex = 0; + this._doConnect(connectOptions.hosts[0], connectOptions.ports[0]); + } else { + this._doConnect(this.host, this.port); + } + + }; + + ClientImpl.prototype.subscribe = function (filter, subscribeOptions) { + this._trace("Client.subscribe", filter, subscribeOptions); + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + + var wireMessage = new WireMessage(MESSAGE_TYPE.SUBSCRIBE); + wireMessage.topics=[filter]; + if (subscribeOptions.qos != undefined) + wireMessage.requestedQos = [subscribeOptions.qos]; + else + wireMessage.requestedQos = [0]; + + if (subscribeOptions.onSuccess) { + wireMessage.callback = function() {subscribeOptions.onSuccess({invocationContext:subscribeOptions.invocationContext});}; + } + if (subscribeOptions.timeout) { + wireMessage.timeOut = new Timeout(this, window, subscribeOptions.timeout, subscribeOptions.onFailure + , [{invocationContext:subscribeOptions.invocationContext, + errorCode:ERROR.SUBSCRIBE_TIMEOUT.code, + errorMessage:format(ERROR.SUBSCRIBE_TIMEOUT)}]); + } + + // All subscriptions return a SUBACK. + this._requires_ack(wireMessage); + this._schedule_message(wireMessage); + }; + + /** @ignore */ + ClientImpl.prototype.unsubscribe = function(filter, unsubscribeOptions) { + this._trace("Client.unsubscribe", filter, unsubscribeOptions); + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + + var wireMessage = new WireMessage(MESSAGE_TYPE.UNSUBSCRIBE); + wireMessage.topics = [filter]; + + if (unsubscribeOptions.onSuccess) { + wireMessage.callback = function() {unsubscribeOptions.onSuccess({invocationContext:unsubscribeOptions.invocationContext});}; + } + if (unsubscribeOptions.timeout) { + wireMessage.timeOut = new Timeout(this, window, unsubscribeOptions.timeout, unsubscribeOptions.onFailure + , [{invocationContext:unsubscribeOptions.invocationContext, + errorCode:ERROR.UNSUBSCRIBE_TIMEOUT.code, + errorMessage:format(ERROR.UNSUBSCRIBE_TIMEOUT)}]); + } + + // All unsubscribes return a SUBACK. + this._requires_ack(wireMessage); + this._schedule_message(wireMessage); + }; + + ClientImpl.prototype.send = function (message) { + this._trace("Client.send", message); + + if (!this.connected) + throw new Error(format(ERROR.INVALID_STATE, ["not connected"])); + + wireMessage = new WireMessage(MESSAGE_TYPE.PUBLISH); + wireMessage.payloadMessage = message; + + if (message.qos > 0) + this._requires_ack(wireMessage); + else if (this.onMessageDelivered) + this._notify_msg_sent[wireMessage] = this.onMessageDelivered(wireMessage.payloadMessage); + this._schedule_message(wireMessage); + }; + + ClientImpl.prototype.disconnect = function () { + this._trace("Client.disconnect"); + + if (!this.socket) + throw new Error(format(ERROR.INVALID_STATE, ["not connecting or connected"])); + + wireMessage = new WireMessage(MESSAGE_TYPE.DISCONNECT); + + // Run the disconnected call back as soon as the message has been sent, + // in case of a failure later on in the disconnect processing. + // as a consequence, the _disconected call back may be run several times. + this._notify_msg_sent[wireMessage] = scope(this._disconnected, this); + + this._schedule_message(wireMessage); + }; + + ClientImpl.prototype.getTraceLog = function () { + if ( this._traceBuffer !== null ) { + this._trace("Client.getTraceLog", new Date()); + this._trace("Client.getTraceLog in flight messages", this._sentMessages.length); + for (key in this._sentMessages) + this._trace("_sentMessages ",key, this._sentMessages[key]); + for (key in this._receivedMessages) + this._trace("_receivedMessages ",key, this._receivedMessages[key]); + + return this._traceBuffer; + } + }; + + ClientImpl.prototype.startTrace = function () { + if ( this._traceBuffer === null ) { + this._traceBuffer = []; + } + this._trace("Client.startTrace", new Date(), version); + }; + + ClientImpl.prototype.stopTrace = function () { + delete this._traceBuffer; + }; + + ClientImpl.prototype._doConnect = function (host, port) { + // When the socket is open, this client will send the CONNECT WireMessage using the saved parameters. + if (this.connectOptions.useSSL) + wsurl = ["wss://", host, ":", port, "/mqtt"].join(""); + else + wsurl = ["ws://", host, ":", port, "/mqtt"].join(""); + this.connected = false; + this.socket = new WebSocket(wsurl, 'mqttv3.1'); + this.socket.binaryType = 'arraybuffer'; + this.socket.onopen = scope(this._on_socket_open, this); + this.socket.onmessage = scope(this._on_socket_message, this); + this.socket.onerror = scope(this._on_socket_error, this); + this.socket.onclose = scope(this._on_socket_close, this); + + this.pinger = new Pinger(this, window, this.connectOptions.keepAliveInterval); + + this._connectTimeout = new Timeout(this, window, this.connectOptions.timeout, this._disconnected, [ERROR.CONNECT_TIMEOUT.code, format(ERROR.CONNECT_TIMEOUT)]); + }; + + + // Schedule a new message to be sent over the WebSockets + // connection. CONNECT messages cause WebSocket connection + // to be started. All other messages are queued internally + // until this has happened. When WS connection starts, process + // all outstanding messages. + ClientImpl.prototype._schedule_message = function (message) { + this._msg_queue.push(message); + // Process outstanding messages in the queue if we have an open socket, and have received CONNACK. + if (this.connected) { + this._process_queue(); + } + }; + + ClientImpl.prototype.store = function(prefix, wireMessage) { + storedMessage = {type:wireMessage.type, messageIdentifier:wireMessage.messageIdentifier, version:1}; + + switch(wireMessage.type) { + case MESSAGE_TYPE.PUBLISH: + if(wireMessage.pubRecReceived) + storedMessage.pubRecReceived = true; + + // Convert the payload to a hex string. + storedMessage.payloadMessage = {}; + var hex = ""; + var messageBytes = wireMessage.payloadMessage.payloadBytes; + for (var i=0; i= 2) { + var x = parseInt(hex.substring(0, 2), 16); + hex = hex.substring(2, hex.length); + byteStream[i++] = x; + } + var payloadMessage = new Messaging.Message(byteStream); + + payloadMessage.qos = storedMessage.payloadMessage.qos; + payloadMessage.destinationName = storedMessage.payloadMessage.destinationName; + if (storedMessage.payloadMessage.duplicate) + payloadMessage.duplicate = true; + if (storedMessage.payloadMessage.retained) + payloadMessage.retained = true; + wireMessage.payloadMessage = payloadMessage; + + break; + + default: + throw Error(format(ERROR.INVALID_STORED_DATA, [key, value])); + } + + if (key.indexOf("Sent:"+this._localKey) == 0) { + this._sentMessages[wireMessage.messageIdentifier] = wireMessage; + } else if (key.indexOf("Received:"+this._localKey) == 0) { + this._receivedMessages[wireMessage.messageIdentifier] = wireMessage; + } + }; + + ClientImpl.prototype._process_queue = function () { + var message = null; + // Process messages in order they were added + var fifo = this._msg_queue.reverse(); + + // Send all queued messages down socket connection + while ((message = fifo.pop())) { + this._socket_send(message); + // Notify listeners that message was successfully sent + if (this._notify_msg_sent[message]) { + this._notify_msg_sent[message](); + delete this._notify_msg_sent[message]; + } + } + }; + + /** + * @ignore + * Expect an ACK response for this message. Add message to the set of in progress + * messages and set an unused identifier in this message. + */ + ClientImpl.prototype._requires_ack = function (wireMessage) { + var messageCount = Object.keys(this._sentMessages).length; + if (messageCount > this.maxMessageIdentifier) + throw Error ("Too many messages:"+messageCount); + + while(this._sentMessages[this._message_identifier] !== undefined) { + this._message_identifier++; + } + wireMessage.messageIdentifier = this._message_identifier; + this._sentMessages[wireMessage.messageIdentifier] = wireMessage; + if (wireMessage.type === MESSAGE_TYPE.PUBLISH) { + this.store("Sent:", wireMessage); + } + if (this._message_identifier === this.maxMessagIdentifier) { + this._message_identifier = 1; + } + }; + + /** + * @ignore + * Called when the underlying websocket has been opened. + */ + ClientImpl.prototype._on_socket_open = function () { + // Create the CONNECT message object. + var wireMessage = new WireMessage(MESSAGE_TYPE.CONNECT, this.connectOptions); + wireMessage.clientId = this.clientId; + this._socket_send(wireMessage); + }; + + /** + * @ignore + * Called when the underlying websocket has received a complete packet. + */ + ClientImpl.prototype._on_socket_message = function (event) { + this._trace("Client._on_socket_message", event.data); + + // Reset the ping timer. + this.pinger.reset(); + var byteArray = new Uint8Array(event.data); + try { + var wireMessage = decodeMessage(byteArray); + } catch (error) { + this._disconnected(ERROR.INTERNAL_ERROR.code , format(ERROR.INTERNAL_ERROR, [error.message])); + return; + } + this._trace("Client._on_socket_message", wireMessage); + + switch(wireMessage.type) { + case MESSAGE_TYPE.CONNACK: + this._connectTimeout.cancel(); + + // If we have started using clean session then clear up the local state. + if (this.connectOptions.cleanSession) { + for (key in this._sentMessages) { + var sentMessage = this._sentMessages[key]; + localStorage.removeItem("Sent:"+this._localKey+sentMessage.messageIdentifier); + } + this._sentMessages = {}; + + for (key in this._receivedMessages) { + var receivedMessage = this._receivedMessages[key]; + localStorage.removeItem("Received:"+this._localKey+receivedMessage.messageIdentifier); + } + this._receivedMessages = {}; + } + // Client connected and ready for business. + if (wireMessage.returnCode === 0) { + this.connected = true; + // Jump to the end of the list of hosts and stop looking for a good host. + if (this.connectOptions.hosts) + this.hostIndex = this.connectOptions.hosts.length; + } else { + this._disconnected(ERROR.CONNACK_RETURNCODE.code , format(ERROR.CONNACK_RETURNCODE, [wireMessage.returnCode, CONNACK_RC[wireMessage.returnCode]])); + break; + } + + // Resend messages. + var sequencedMessages = new Array(); + for (var msgId in this._sentMessages) { + if (this._sentMessages.hasOwnProperty(msgId)) + sequencedMessages.push(this._sentMessages[msgId]); + } + + // Sort sentMessages into the original sent order. + var sequencedMessages = sequencedMessages.sort(function(a,b) {return a.sequence - b.sequence;} ); + for (var i=0, len=sequencedMessages.length; i + * Other programming languages, + * Java, + * C. + *

+ * Most applications will create just one Client object and then call its connect() method, + * however applications can create more than one Client object if they wish. + * In this case the combination of host, port and clientId attributes must be different for each Client object. + *

+ * The send, subscribe and unsubscribe methods are implemented as asynchronous JavaScript methods + * (even though the underlying protocol exchange might be synchronous in nature). + * This means they signal their completion by calling back to the application, + * via Success or Failure callback functions provided by the application on the method in question. + * Such callbacks are called at most once per method invocation and do not persist beyond the lifetime + * of the script that made the invocation. + *

+ * In contrast there are some callback functions most notably onMessageArrived + * that are defined on the Messaging.Client object. + * These may get called multiple times, and aren't directly related to specific method invocations made by the client. + * + * @name Messaging.Client + * + * @constructor + * Creates a Messaging.Client object that can be used to communicate with a Messaging server. + * + * @param {string} host the address of the messaging server, as a DNS name or dotted decimal IP address. + * @param {number} port the port number in the host to connect to. + * @param {string} clientId the Messaging client identifier, between 1 and 23 characters in length. + * + * @property {string} host read only the server's DNS hostname or dotted decimal IP address. + * @property {number} port read only the server's port. + * @property {string} clientId read only used when connecting to the server. + * @property {function} onConnectionLost called when a connection has been lost, + * after a connect() method has succeeded. + * Establish the call back used when a connection has been lost. The connection may be + * lost because the client initiates a disconnect or because the server or network + * cause the client to be disconnected. The disconnect call back may be called without + * the connectionComplete call back being invoked if, for example the client fails to + * connect. + * A single response object parameter is passed to the onConnectionLost callback containing the following fields: + *

    + *
  1. errorCode + *
  2. errorMessage + *
+ * @property {function} onMessageDelivered called when a message has been delivered. + * All processing that this Client will ever do has been completed. So, for example, + * in the case of a Qos=2 message sent by this client, the PubComp flow has been received from the server + * and the message has been removed from persistent storage before this callback is invoked. + * Parameters passed to the onMessageDelivered callback are: + *
    + *
  1. Messaging.Message that was delivered. + *
+ * @property {function} onMessageArrived called when a message has arrived in this Messaging.client. + * Parameters passed to the onMessageArrived callback are: + *
    + *
  1. Messaging.Message that has arrived. + *
+ */ + var Client = function (host, port, clientId) { + if (typeof host !== "string") + throw new Error(format(ERROR.INVALID_TYPE, [typeof host, "host"])); + if (typeof port !== "number" || port < 0) + throw new Error(format(ERROR.INVALID_TYPE, [typeof port, "port"])); + + var clientIdLength = 0; + for (var i = 0; i 23) + throw new Error(format(ERROR.INVALID_ARGUMENT, [clientId, "clientId"])); + + var client = new ClientImpl(host, port, clientId); + this._getHost = function() { return client.host; }; + this._setHost = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getPort = function() { return client.port; }; + this._setPort = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getClientId = function() { return client.clientId; }; + this._setClientId = function() { throw new Error(format(ERROR.UNSUPPORTED_OPERATION)); }; + + this._getOnConnectionLost = function() { return client.onConnectionLost; }; + this._setOnConnectionLost = function(newOnConnectionLost) { + if (typeof newOnConnectionLost === "function") + client.onConnectionLost = newOnConnectionLost; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnConnectionLost, "onConnectionLost"])); + }; + + this._getOnMessageDelivered = function() { return client.onMessageDelivered; }; + this._setOnMessageDelivered = function(newOnMessageDelivered) { + if (typeof newOnMessageDelivered === "function") + client.onMessageDelivered = newOnMessageDelivered; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageDelivered, "onMessageDelivered"])); + }; + + this._getOnMessageArrived = function() { return client.onMessageArrived; }; + this._setOnMessageArrived = function(newOnMessageArrived) { + if (typeof newOnMessageArrived === "function") + client.onMessageArrived = newOnMessageArrived; + else + throw new Error(format(ERROR.INVALID_TYPE, [typeof newOnMessageArrived, "onMessageArrived"])); + }; + + /** + * Connect this Messaging client to its server. + * + * @name Messaging.Client#connect + * @function + * @param {Object} [connectOptions] attributes used with the connection. + *

+ * Properties of the connect options are: + * @config {number} [timeout] If the connect has not succeeded within this number of seconds, it is deemed to have failed. + * The default is 30 seconds. + * @config {string} [userName] Authentication username for this connection. + * @config {string} [password] Authentication password for this connection. + * @config {Messaging.Message} [willMessage] sent by the server when the client disconnects abnormally. + * @config {Number} [keepAliveInterval] the server disconnects this client if there is no activity for this + * number of seconds. The default value of 60 seconds is assumed if not set. + * @config {boolean} [cleanSession] if true(default) the client and server persistent state is deleted on successful connect. + * @config {boolean} [useSSL] if present and true, use an SSL Websocket connection. + * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback. + * @config {function} [onSuccess] called when the connect acknowledgement has been received from the server. + * A single response object parameter is passed to the onSuccess callback containing the following fields: + *

    + *
  1. invocationContext as passed in to the onSuccess method in the connectOptions. + *
+ * @config {function} [onFailure] called when the connect request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext as passed in to the onFailure method in the connectOptions. + *
  2. errorCode a number indicating the nature of the error. + *
  3. errorMessage text describing the error. + *
+ * @config {Array} [hosts] If present this set of hostnames is tried in order in place + * of the host and port paramater on the construtor. The hosts and the matching ports are tried one at at time in order until + * one of then succeeds. + * @config {Array} [ports] If present this set of ports matching the hosts. + * @throws {InvalidState} if the client is not in disconnected state. The client must have received connectionLost + * or disconnected before calling connect for a second or subsequent time. + */ + this.connect = function (connectOptions) { + connectOptions = connectOptions || {} ; + validate(connectOptions, {timeout:"number", + userName:"string", + password:"string", + willMessage:"object", + keepAliveInterval:"number", + cleanSession:"boolean", + useSSL:"boolean", + invocationContext:"object", + onSuccess:"function", + onFailure:"function", + hosts:"object", + ports:"object"}); + + // If no keep alive interval is set, assume 60 seconds. + if (connectOptions.keepAliveInterval === undefined) + connectOptions.keepAliveInterval = 60; + + if (connectOptions.willMessage) { + if (!(connectOptions.willMessage instanceof Message)) + throw new Error(format(ERROR.INVALID_TYPE, [connectOptions.willMessage, "connectOptions.willMessage"])); + // The will message must have a payload that can be represented as a string. + // Cause the willMessage to throw an exception if this is not the case. + connectOptions.willMessage.stringPayload; + + if (typeof connectOptions.willMessage.destinationName === "undefined") + throw new Error(format(ERROR.INVALID_TYPE, [typeof connectOptions.willMessage.destinationName, "connectOptions.willMessage.destinationName"])); + } + if (typeof connectOptions.cleanSession === "undefined") + connectOptions.cleanSession = true; + if (connectOptions.hosts) { + if (!connectOptions.ports) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + if (!(connectOptions.hosts instanceof Array) ) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"])); + if (!(connectOptions.ports instanceof Array) ) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + if (connectOptions.hosts.length <1 ) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.hosts, "connectOptions.hosts"])); + if (connectOptions.hosts.length != connectOptions.ports.length) + throw new Error(format(ERROR.INVALID_ARGUMENT, [connectOptions.ports, "connectOptions.ports"])); + for (var i = 0; i + * @param {object} [subscribeOptions] used to control the subscription, as follows: + *

+ * @config {number} [qos] the maiximum qos of any publications sent as a result of making this subscription. + * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback. + * @config {function} [onSuccess] called when the subscribe acknowledgement has been received from the server. + * A single response object parameter is passed to the onSuccess callback containing the following fields: + *

    + *
  1. invocationContext if set in the subscribeOptions. + *
+ * @config {function} [onFailure] called when the subscribe request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext if set in the subscribeOptions. + *
  2. errorCode a number indicating the nature of the error. + *
  3. errorMessage text describing the error. + *
+ * @config {number} [timeout] which if present determines the number of seconds after which the onFailure calback is called + * the presence of a timeout does not prevent the onSuccess callback from being called when the MQTT Suback is eventually received. + * @throws {InvalidState} if the client is not in connected state. + */ + this.subscribe = function (filter, subscribeOptions) { + if (typeof filter !== "string") + throw new Error("Invalid argument:"+filter); + subscribeOptions = subscribeOptions || {} ; + validate(subscribeOptions, {qos:"number", + invocationContext:"object", + onSuccess:"function", + onFailure:"function", + timeout:"number" + }); + if (subscribeOptions.timeout && !subscribeOptions.onFailure) + throw new Error("subscribeOptions.timeout specified with no onFailure callback."); + if (typeof subscribeOptions.qos !== "undefined" + && !(subscribeOptions.qos === 0 || subscribeOptions.qos === 1 || subscribeOptions.qos === 2 )) + throw new Error(format(ERROR.INVALID_ARGUMENT, [subscribeOptions.qos, "subscribeOptions.qos"])); + client.subscribe(filter, subscribeOptions); + }; + + /** + * Unsubscribe for messages, stop receiving messages sent to destinations described by the filter. + * + * @name Messaging.Client#unsubscribe + * @function + * @param {string} filter describing the destinations to receive messages from. + * @param {object} [unsubscribeOptions] used to control the subscription, as follows: + *

+ * @config {object} [invocationContext] passed to the onSuccess callback or onFailure callback. + * @config {function} [onSuccess] called when the unsubscribe acknowledgement has been receive dfrom the server. + * A single response object parameter is passed to the onSuccess callback containing the following fields: + *

    + *
  1. invocationContext if set in the unsubscribeOptions. + *
+ * @config {function} [onFailure] called when the unsubscribe request has failed or timed out. + * A single response object parameter is passed to the onFailure callback containing the following fields: + *
    + *
  1. invocationContext if set in the unsubscribeOptions. + *
  2. errorCode a number indicating the nature of the error. + *
  3. errorMessage text describing the error. + *
+ * @config {number} [timeout] which if present determines the number of seconds after which the onFailure callback is called, the + * presence of a timeout does not prevent the onSuccess callback from being called when the MQTT UnSuback is eventually received. + * @throws {InvalidState} if the client is not in connected state. + */ + this.unsubscribe = function (filter, unsubscribeOptions) { + if (typeof filter !== "string") + throw new Error("Invalid argument:"+filter); + unsubscribeOptions = unsubscribeOptions || {} ; + validate(unsubscribeOptions, {invocationContext:"object", + onSuccess:"function", + onFailure:"function", + timeout:"number" + }); + if (unsubscribeOptions.timeout && !unsubscribeOptions.onFailure) + throw new Error("unsubscribeOptions.timeout specified with no onFailure callback."); + client.unsubscribe(filter, unsubscribeOptions); + }; + + /** + * Send a message to the consumers of the destination in the Message. + * + * @name Messaging.Client#send + * @function + * @param {Messaging.Message} message to send. + + * @throws {InvalidState} if the client is not in connected state. + */ + this.send = function (message) { + if (!(message instanceof Message)) + throw new Error("Invalid argument:"+typeof message); + if (typeof message.destinationName === "undefined") + throw new Error("Invalid parameter Message.destinationName:"+message.destinationName); + + client.send(message); + }; + + /** + * Normal disconnect of this Messaging client from its server. + * + * @name Messaging.Client#disconnect + * @function + * @throws {InvalidState} if the client is not in connected or connecting state. + */ + this.disconnect = function () { + client.disconnect(); + }; + + /** + * Get the contents of the trace log. + * + * @name Messaging.Client#getTraceLog + * @function + * @return {Object[]} tracebuffer containing the time ordered trace records. + */ + this.getTraceLog = function () { + return client.getTraceLog(); + } + + /** + * Start tracing. + * + * @name Messaging.Client#startTrace + * @function + */ + this.startTrace = function () { + client.startTrace(); + }; + + /** + * Stop tracing. + * + * @name Messaging.Client#stopTrace + * @function + */ + this.stopTrace = function () { + client.stopTrace(); + }; + }; + + Client.prototype = { + get host() { return this._getHost(); }, + set host(newHost) { this._setHost(newHost); }, + + get port() { return this._getPort(); }, + set port(newPort) { this._setPort(newPort); }, + + get clientId() { return this._getClientId(); }, + set clientId(newClientId) { this._setClientId(newClientId); }, + + get onConnectionLost() { return this._getOnConnectionLost(); }, + set onConnectionLost(newOnConnectionLost) { this._setOnConnectionLost(newOnConnectionLost); }, + + get onMessageDelivered() { return this._getOnMessageDelivered(); }, + set onMessageDelivered(newOnMessageDelivered) { this._setOnMessageDelivered(newOnMessageDelivered); }, + + get onMessageArrived() { return this._getOnMessageArrived(); }, + set onMessageArrived(newOnMessageArrived) { this._setOnMessageArrived(newOnMessageArrived); } + }; + + /** + * An application message, sent or received. + *

+ * Other programming languages, + * Java, + * C. + *

+ * All attributes may be null, which implies the default values. + * + * @name Messaging.Message + * @constructor + * @param {String|ArrayBuffer} payload The message data to be sent. + *

+ * @property {string} payloadString read only The payload as a string if the payload consists of valid UTF-8 characters. + * @property {ArrayBuffer} payloadBytes read only The payload as an ArrayBuffer. + *

+ * @property {string} destinationName mandatory The name of the destination to which the message is to be sent + * (for messages about to be sent) or the name of the destination from which the message has been received. + * (for messages received by the onMessage function). + *

+ * @property {number} qos The Quality of Service used to deliver the message. + *

+ *
0 Best effort (default). + *
1 At least once. + *
2 Exactly once. + *
+ *

+ * @property {Boolean} retained If true, the message is to be retained by the server and delivered + * to both current and future subscriptions. + * If false the server only delivers the message to current subscribers, this is the default for new Messages. + * A received message has the retained boolean set to true if the message was published + * with the retained boolean set to true + * and the subscrption was made after the message has been published. + *

+ * @property {Boolean} duplicate read only If true, this message might be a duplicate of one which has already been received. + * This is only set on messages received from the server. + * + */ + var Message = function (newPayload) { + var payload; + if ( typeof newPayload === "string" + || newPayload instanceof ArrayBuffer + || newPayload instanceof Int8Array + || newPayload instanceof Uint8Array + || newPayload instanceof Int16Array + || newPayload instanceof Uint16Array + || newPayload instanceof Int32Array + || newPayload instanceof Uint32Array + || newPayload instanceof Float32Array + || newPayload instanceof Float64Array + ) { + payload = newPayload; + } else { + throw (format(ERROR.INVALID_ARGUMENT, [newPayload, "newPayload"])); + } + + this._getPayloadString = function () { + if (typeof payload === "string") + return payload; + else + return parseUTF8(payload, 0, payload.length); + }; + + this._getPayloadBytes = function() { + if (typeof payload === "string") { + var buffer = new ArrayBuffer(UTF8Length(payload)); + var byteStream = new Uint8Array(buffer); + stringToUTF8(payload, byteStream, 0); + + return byteStream; + } else { + return payload; + }; + }; + + var destinationName = undefined; + this._getDestinationName = function() { return destinationName; }; + this._setDestinationName = function(newDestinationName) { + if (typeof newDestinationName === "string") + destinationName = newDestinationName; + else + throw new Error(format(ERROR.INVALID_ARGUMENT, [newDestinationName, "newDestinationName"])); + }; + + var qos = 0; + this._getQos = function() { return qos; }; + this._setQos = function(newQos) { + if (newQos === 0 || newQos === 1 || newQos === 2 ) + qos = newQos; + else + throw new Error("Invalid argument:"+newQos); + }; + + var retained = false; + this._getRetained = function() { return retained; }; + this._setRetained = function(newRetained) { + if (typeof newRetained === "boolean") + retained = newRetained; + else + throw new Error(format(ERROR.INVALID_ARGUMENT, [newRetained, "newRetained"])); + }; + + var duplicate = false; + this._getDuplicate = function() { return duplicate; }; + this._setDuplicate = function(newDuplicate) { duplicate = newDuplicate; }; + }; + + Message.prototype = { + get payloadString() { return this._getPayloadString(); }, + get payloadBytes() { return this._getPayloadBytes(); }, + + get destinationName() { return this._getDestinationName(); }, + set destinationName(newDestinationName) { this._setDestinationName(newDestinationName); }, + + get qos() { return this._getQos(); }, + set qos(newQos) { this._setQos(newQos); }, + + get retained() { return this._getRetained(); }, + set retained(newRetained) { this._setRetained(newRetained); }, + + get duplicate() { return this._getDuplicate(); }, + set duplicate(newDuplicate) { this._setDuplicate(newDuplicate); } + }; + + // Module contents. + return { + Client: Client, + Message: Message + }; +}; diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/pusher.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/pusher.js new file mode 100644 index 0000000..c3a0177 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/pusher.js @@ -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); + } +}; diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-connector.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-connector.js new file mode 100644 index 0000000..0b2b92b --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-connector.js @@ -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); + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-historian.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-historian.js new file mode 100644 index 0000000..2bd27a6 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-historian.js @@ -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; +}; + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-save.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-save.js new file mode 100644 index 0000000..a1108de --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/lib/server/db-save.js @@ -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; +}; + diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/manifest.yml b/mdot/mqttArchiver.censis/mqttArchiver.censis/manifest.yml new file mode 100644 index 0000000..cb14492 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/manifest.yml @@ -0,0 +1,8 @@ +applications: +- path: . + memory: 256M + instances: 1 + domain: mybluemix.net + name: mqttArchiver + host: mqttarchiver + disk_quota: 1024M diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/package.json b/mdot/mqttArchiver.censis/mqttArchiver.censis/package.json new file mode 100644 index 0000000..05cdf1f --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/package.json @@ -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 ", + "license": "ISC", + "engines": { + "node": "6.2.1" + } +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/pg_hba.conf b/mdot/mqttArchiver.censis/mqttArchiver.censis/pg_hba.conf new file mode 100644 index 0000000..8954b5f --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/pg_hba.conf @@ -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 diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/process.json b/mdot/mqttArchiver.censis/mqttArchiver.censis/process.json new file mode 100644 index 0000000..0626314 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/process.json @@ -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" + ] + } + ] +} diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/dbconfig.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/dbconfig.js new file mode 100644 index 0000000..a34cc35 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/dbconfig.js @@ -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(); diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/maker.js b/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/maker.js new file mode 100644 index 0000000..3393f6a --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/tools/maker.js @@ -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); + + }); + }); diff --git a/mdot/mqttArchiver.censis/mqttArchiver.censis/views/404.ejs b/mdot/mqttArchiver.censis/mqttArchiver.censis/views/404.ejs new file mode 100644 index 0000000..a38aac7 --- /dev/null +++ b/mdot/mqttArchiver.censis/mqttArchiver.censis/views/404.ejs @@ -0,0 +1,15 @@ + + + + 404 + + + +

+
+
404
+
Not Found
+
+
+ + diff --git a/mdot/mqttArchiver/mqttArchiver/app.js b/mdot/mqttArchiver/mqttArchiver/app.js index fbf5a7c..e85a5cd 100644 --- a/mdot/mqttArchiver/mqttArchiver/app.js +++ b/mdot/mqttArchiver/mqttArchiver/app.js @@ -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; diff --git a/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js b/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js index 5b8140e..e3244f6 100644 --- a/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js +++ b/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js @@ -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); diff --git a/mdot/mqttArchiver/mqttArchiver/lib/server/db-connector.js b/mdot/mqttArchiver/mqttArchiver/lib/server/db-connector.js index 59b4624..0b2b92b 100644 --- a/mdot/mqttArchiver/mqttArchiver/lib/server/db-connector.js +++ b/mdot/mqttArchiver/mqttArchiver/lib/server/db-connector.js @@ -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;