”2016-08-12”

This commit is contained in:
Martin Donnelly 2016-08-12 16:45:04 +01:00
parent 07b57db771
commit d35aff8eeb
13 changed files with 65325 additions and 2 deletions

View File

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

View File

@ -51,7 +51,7 @@ function insertEntry(obj) {
var doInsertEntry = (obj) => {
// Logger.info('sendSocket: ' + JSON.stringify(obj));
insertEntry(obj);
// insertEntry(obj);
dbSave.addNewEvent(obj)
.then(function(d) {

21758
mdot/mdot_mqtt/mdot_mqtt/mdot Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"cookie-parser": "*",
"ejs": "*",
"errorhandler": "*",
"exec": "^0.2.1",
"express": "^4.13.4",
"express-session": "*",
"htmlparser": "^1.7.7",

View File

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

View File

@ -30,6 +30,7 @@ var isProduction = false;
var mdotApi = require('./lib/mdot/api.js');
var mdotApiV2 = require('./lib/mdot/apiv2.js');
var trackApi = require('./lib/mdot/track.js');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
@ -81,6 +82,7 @@ function originIsAllowed(origin) {
// glue routes
mdotApi(app);
mdotApiV2(app);
trackApi(app);
//app.get('/api/mdot/:id', mDot.getData);

View File

@ -291,7 +291,7 @@
}, this);
console.log(chartData);
this.dataSet.fieldMappings = [{
fromField: 'value',
toField: 'value'

View File

@ -0,0 +1,86 @@
/**
*
* User: Martin Donnelly
* Date: 2016-08-12
* Time: 13:41
*
*/
'use strict';
var logger = require('log4js').getLogger();
var mdot = require('./mdot.js');
var db = require('../server/db-connector').dbConnection;
var dbTrack = require('../server/db-track')(db);
module.exports = function(app) {
var express = require('express');
var mdotRouter = express.Router();
mdotRouter.post('/', function(req, res) {
var body = req.body;
var data = {};
logger.debug('body', body);
if (!Object.hasOwnProperty.call(body, 'locationid') || !Object.hasOwnProperty.call(body, 'count') || !Object.hasOwnProperty.call(body, 'total')) {
logger.error('MDot','Missing required parameter');
res.status(400).send({
status: 'error',
error: 'missing required parameter'
});
return;
} else {
data.locationid = req.body.locationid;
data.count = req.body.count;
data.total = req.body.total;
dbTrack.addNewTrack(data)
.then((d) => {
res.json({d: d});
})
.catch((e) => {
logger.error(e);
res.status(500).json({});
});
}
});
mdotRouter.get('/:id', function(req, res) {
console.log(req.headers);
var data = {};
logger.debug('track-GetData');
if (!req.params.id) {
logger.error('Track','Missing required parameter');
res.status(400).send({
status: 'error',
error: 'missing required parameter'
});
return;
}
if (req.params.hasOwnProperty('id')) {
data.locationid = req.params.id;
dbTrack.doGet(data)
.then((d) => {
res.json({tracks:d});
})
.catch((e) => {
logger.error(e);
res.status(500).json({});
});
}
});
app.use('/apiv2/track/', mdotRouter);
};

View File

@ -0,0 +1,84 @@
'use strict';
var logger = require('log4js').getLogger();
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.sqlInsertTrack = function(data) {
let _data = data;
logger.debug('sqlInsertTrack', _data.locationid, _data.timestamp);
return new Promise(function(resolve, reject) {
db.func('insert_track',
[_data.locationid, _data.timestamp, _data.count, _data.total])
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlGetTracksByID = function(locationId) {
logger.debug('track:sqlGetTracksByID', locationId);
return new Promise(function(resolve, reject) {
db.any('select * from track where locationid=$1;', [locationId])
.then(function(d) {
return resolve(d);
})
.catch((err)=> {
logger.error(err);
return reject(err);
});
});
};
module.addNewTrack = function(data) {
logger.debug('addNewTrack');
var self = this;
return new Promise((resolve, reject) => {
let _data = {};
_data.timestamp = new Date();
_data.locationid = data.locationid;
_data.count = data.count;
_data.total = data.total;
self.sqlInsertTrack(_data)
.then((d)=> {
logger.debug('Postgres returns', d);
return resolve({reply: 'track inserted'});
})
.catch((err)=> {
return reject(err);
});
});
};
module.doGet = function(params) {
var self = this;
return new Promise(function(resolve, reject) {
console.log('track.doGet', params);
self.sqlGetTracksByID(params.locationid)
.then(function(d) {
resolve(d);
})
.catch(function(e) {
logger.error(e);
reject(e);
});
});
};
return module;
};

View File

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

View File

@ -0,0 +1,53 @@
-- Sequence: public.track_id_seq
-- DROP SEQUENCE public.track_id_seq;
CREATE SEQUENCE public.track_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE public.track_id_seq
OWNER TO postgres;
-- Table: public.track
-- DROP TABLE public.track;
CREATE TABLE public.track
(
id bigint NOT NULL DEFAULT nextval('track_id_seq'::regclass),
locationid integer,
logged timestamp with time zone,
count smallint,
total integer
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.track
OWNER TO postgres;
--
CREATE OR REPLACE FUNCTION public.insert_track(
_locationid integer,
_logged timestamp with time zone,
_count smallint,
_total integer)
RETURNS void AS
$BODY$
BEGIN
INSERT into track(locationid, logged, count, total) Values( _locationid, _logged, _count, _total);
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.insert_track(integer, timestamp with time zone, smallint, integer)
OWNER TO postgres;