Initial commit

This commit is contained in:
Arnaud Nelissen
2021-07-16 10:18:13 +02:00
commit 3af7ddab06
5894 changed files with 590836 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
const services = require('../services')
const { data } = services
const postSetData = async (req, res, next) => {
try {
var values = { data: req.body.data, msn: req.body.msn, id: req.params.id }
const code = await data.createData(values)
res.send(code)
} catch (e) {
console.log(e.message)
res.sendStatus(500)
}
}
module.exports = {
postSetData,
}

View File

@@ -0,0 +1,30 @@
const services = require('../services')
const { firmware } = services
const postLookFirmware = async (req, res, next) => {
const { serialNumber, hash, version, type, hardwareIndex, hardwareVersion } = req.body
try {
const obj = await firmware.lookFirmware(serialNumber, hash, version, type, hardwareIndex, hardwareVersion)
res.send(obj)
} catch (e) {
console.log(e.message)
res.sendStatus(500)
}
}
const postSyncFirmware = async (req, res, next) => {
const { serialNumber, hash, version, type, hardwareIndex, hardwareVersion } = req.body
try {
const obj = await firmware.syncFirmware(serialNumber, hash, version, type, hardwareIndex, hardwareVersion)
res.send(obj)
} catch (e) {
console.log(e.message)
res.sendStatus(500)
}
}
module.exports = {
postLookFirmware,
postSyncFirmware
}

View File

@@ -0,0 +1,67 @@
const services = require('../services')
const { device } = services
const postGetRequestToDo = async (req, res, next) => {
///// Emit event to server
try {
await device.postRequestToDo(req.body.relayMsn, req.body);
await device.getRequestToDo(req.body.relayMsn);
var response = { todo: [{ serialNumber: req.body.relayMsn, configuration: 1 }] };
console.log(req.body);
res.send(response);
} catch (error) {
res.send(500);
}
}
const getModuleConfiguration = async (req, res, next) => {
///// Emit event to server
var response = {
"name": "LRBST-R-TARACE",
"inventory": [
"7400040670330001"
],
"wakeUpTimes": [
60,
480,
540,
600,
660,
720,
780,
840,
900,
960,
1020,
1080
]
};
res.send(response);
}
const setModuleConfiguration = async (req, res, next) => {
}
const getModulePrograms = async (req, res, next) => {
}
const setModulePrograms = async (req, res, next) => {
}
const reportModuleDataSent = async (req, res, next) => {
res.send(200);
}
module.exports = {
postGetRequestToDo,
getModuleConfiguration,
setModuleConfiguration,
getModulePrograms,
setModulePrograms,
reportModuleDataSent,
}

17
controllers/index.js Normal file
View File

@@ -0,0 +1,17 @@
const status = require('./status.controller')
const data = require('./data.controller')
const firmware = require('./firmware.controller')
const ipx = require('./ipx.controller')
const journal = require('./journal.controller')
const longpolling = require('./longpolling.controller')
const grtd = require('./grtd.controller')
module.exports = {
status,
data,
firmware,
ipx,
journal,
longpolling,
grtd,
}

View File

@@ -0,0 +1,18 @@
const services = require('../services')
const { ipx } = services
const postIpxData = async (req, res, next) => {
try {
var values = { ipxModules: req.body.ipxModules, id: req.params.id }
const code = await ipx.createIpxData(values)
res.send(code)
} catch (e) {
console.log(e.message)
res.sendStatus(500)
}
}
module.exports = {
postIpxData,
}

View File

@@ -0,0 +1,18 @@
const services = require('../services')
const { journal } = services
const postSetJournal = async (req, res, next) => {
try {
var values = { events: req.body.events, msn: req.body.msn, id: req.params.id }
const code = await journal.createEvents(values)
res.send(code)
} catch (e) {
console.log(e.message)
res.sendStatus(500)
}
}
module.exports = {
postSetJournal,
}

View File

@@ -0,0 +1,109 @@
const services = require('../services')
const events = require('events');
const random_bytes = require('random-bytes');
const emitter = new events.EventEmitter();
const { longpolling } = services
const poll_server_timeout = 20000;
const poll_client_timeout = 60000;
poll_request_event_identifier = function (id) { return ('poll-request-' + id).toLowerCase(); }
poll_response_event_identifier = function (qid) { return ('poll-response-' + qid).toLowerCase(); }
poll_request = function (method, action, id, device, data, callback) {
function request_listener(message) {
///// Stop timeout
clearTimeout(timeout);
callback(null, message);
};
///// Create object
object = {
method: method,
nsip: device || '',
action: action || '/',
data: JSON.stringify(data).replace(/"/g, "'") || '{}',
qid: random_bytes.sync(16).toString('hex')
};
///// Wait long polling to open
emitter.emit(poll_request_event_identifier(id), object);
///// Start timeout
var timeout = setTimeout(() => {
///// Remove listener
emitter.removeListener(poll_response_event_identifier(object.qid), request_listener);
///// Send timeout
console.log(id + ' did not long poll (' + object.qid + ')');
callback('timeout', null);
}, poll_client_timeout);
///// Listen for event
emitter.once(poll_response_event_identifier(object.qid), request_listener);
}
const postPeriodic = async (req, res, next) => {
function listener(message) {
///// Stop timeout
clearTimeout(timeout);
///// Send response
res.send(message);
};
///// Start timeout
var timeout = setTimeout(() => {
///// Remove listener
emitter.removeListener(poll_request_event_identifier(req.params.id), listener);
///// Send timeout
res.sendStatus(408);
console.log('Sending timeout to client ' + req.ip + ' (' + req.params.id + ')');
}, poll_server_timeout);
///// Listen for event
emitter.once(poll_request_event_identifier(req.params.id), listener);
}
const postResponse = async (req, res, next) => {
///// Emit event to client
emitter.emit(poll_response_event_identifier(req.params.qid), req.body);
///// Response OK
res.sendStatus(200);
}
const getModuleRequest = async (req, res, next) => {
///// Emit event to server
url_encoded = req.query;
poll_request(req.method, req.params.route, req.params.id, req.params.module, url_encoded, (error, body) => {
if (error) { res.send(error); }
else { res.send(body); }
});
req.on("close", function () {
console.log("Request cancelled by client");
});
}
const postModuleRequest = async (req, res, next) => {
///// Emit event to server
poll_request(req.method, req.params.route, req.params.id, req.params.module, req.body, (error, body) => {
if (error) { res.send(error); }
else { res.send(body); }
});
req.on("close", function () {
console.log("Request cancelled by client");
});
}
module.exports = {
postPeriodic,
postResponse,
getModuleRequest,
postModuleRequest
}

View File

@@ -0,0 +1,27 @@
const services = require('../services')
const { status } = services
const postSetStatus = async (req, res, next) => {
try {
var code = 500;
if (req.body.status) {
const values = { inputsAlerts: req.body.status.inputsAlerts || [], msn: req.body.msn, id: req.params.id }
code = await status.createAlerts(values)
} else if (req.body.radioProducts) {
for (const product of req.body.radioProducts) {
const values = { inputsAlerts: product.inputsAlerts || [], msn: product.msn, id: req.params.id }
code = await status.createAlerts(values)
}
}
res.send(code)
} catch (e) {
res.sendStatus(500)
}
}
module.exports = {
postSetStatus,
}