mirror of
https://gitlab.silvrtree.co.uk/martind2000/mdot_server.git
synced 2025-02-11 10:09:16 +00:00
tracking added
This commit is contained in:
parent
5ca1e2cfb6
commit
fc077bd8e1
2
app.js
2
app.js
@ -30,6 +30,7 @@ var isProduction = false;
|
|||||||
|
|
||||||
var mdotApi = require('./lib/mdot/api.js');
|
var mdotApi = require('./lib/mdot/api.js');
|
||||||
var mdotApiV2 = require('./lib/mdot/apiv2.js');
|
var mdotApiV2 = require('./lib/mdot/apiv2.js');
|
||||||
|
var trackApi = require('./lib/mdot/track.js');
|
||||||
|
|
||||||
|
|
||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||||
@ -81,6 +82,7 @@ function originIsAllowed(origin) {
|
|||||||
// glue routes
|
// glue routes
|
||||||
mdotApi(app);
|
mdotApi(app);
|
||||||
mdotApiV2(app);
|
mdotApiV2(app);
|
||||||
|
trackApi(app);
|
||||||
|
|
||||||
//app.get('/api/mdot/:id', mDot.getData);
|
//app.get('/api/mdot/:id', mDot.getData);
|
||||||
|
|
||||||
|
50
lib/mdot/track.js
Normal file
50
lib/mdot/track.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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 data = {};
|
||||||
|
if (!req.body.hasOwnProperty('locationid') || !req.body.hasOwnProperty('count') || !req.body.hasOwnProperty('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({});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use('/apiv2/track/', mdotRouter);
|
||||||
|
};
|
||||||
|
|
51
lib/server/db-track.js
Normal file
51
lib/server/db-track.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
console.log('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.addNewTrack = function(data) {
|
||||||
|
console.log('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)=> {
|
||||||
|
console.log('Postgres returns', d);
|
||||||
|
return resolve({reply: 'track inserted'});
|
||||||
|
})
|
||||||
|
.catch((err)=> {
|
||||||
|
return reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return module;
|
||||||
|
};
|
||||||
|
|
54
server/dbconfig.js
Normal file
54
server/dbconfig.js
Normal 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();
|
17
server/insert_track.sql
Normal file
17
server/insert_track.sql
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
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;
|
Loading…
Reference in New Issue
Block a user