From 3ff1b73cd2119c1c7bc36552323b4563eab9b621 Mon Sep 17 00:00:00 2001 From: Arnaud Nelissen Date: Thu, 25 Sep 2025 18:41:59 +0200 Subject: [PATCH] Ajout routes API PUT/DEL --- app.js | 18 ++- controllers/api.controller.js | 24 ++-- package-lock.json | 12 ++ package.json | 5 +- services/device.service.js | 261 +++++++++++++++++++++++++++++++++- 5 files changed, 297 insertions(+), 23 deletions(-) diff --git a/app.js b/app.js index 1eafeb1..734a14b 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,8 @@ const fs = require('fs'); const http = require('http'); const https = require('https'); +require('dotenv').config() + const certificate = fs.readFileSync(process.env.CLIENT_CRT || 'cert/client.crt', 'utf8'); const privateKey = fs.readFileSync(process.env.CLIENT_KEY || 'cert/client.key', 'utf8'); @@ -115,15 +117,17 @@ app.use(function (req, res, next) { cbor.decodeFirst(req.body, (err, decoded) => { ///// Check for error if (err) { res.sendStatus(418); return; } + if (cbor_length > 0) { + ///// Assign decoded data + req.body = decoded; + json_length = JSON.stringify(decoded).length - ///// Assign decoded data - req.body = decoded; - json_length = JSON.stringify(decoded).length + ///// Next handler + console.log('Decoded:'); + console.log(req.body) + console.log(cbor_length + ' Bytes (CBOR) -> ' + json_length + ' Bytes (JSON) -> ' + Math.round((cbor_length / json_length - 1) * 100) + '% reduction'); + } - ///// Next handler - console.log('Decoded:'); - console.log(req.body) - console.log(cbor_length + ' Bytes (CBOR) -> ' + json_length + ' Bytes (JSON) -> ' + Math.round((cbor_length / json_length - 1) * 100) + '% reduction'); next(); }); diff --git a/controllers/api.controller.js b/controllers/api.controller.js index 759ba39..15c045a 100644 --- a/controllers/api.controller.js +++ b/controllers/api.controller.js @@ -24,7 +24,7 @@ const postPrograms = async (req, res, next) => { const putPrograms = async (req, res, next) => { try { - var programs = await device.postPrograms(req.params.msn, req.body.programs, req.body.timestamp); + var programs = await device.putPrograms(req.params.msn, req.body.programs, req.body.timestamp); res.send(programs); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -34,7 +34,7 @@ const putPrograms = async (req, res, next) => { const deletePrograms = async (req, res, next) => { ///// Emit event to server try { - const programs = await device.postPrograms(req.params.msn, [], req.body.timestamp); + const programs = await device.deletePrograms(req.params.msn); res.send(programs); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -63,7 +63,7 @@ const postConfiguration = async (req, res, next) => { const putConfiguration = async (req, res, next) => { try { - var configuration = await device.postConfiguration(req.params.msn, req.body.configuration, req.body.timestamp); + var configuration = await device.putConfiguration(req.params.msn, req.body.configuration, req.body.timestamp); res.send(configuration); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -73,7 +73,7 @@ const putConfiguration = async (req, res, next) => { const deleteConfiguration = async (req, res, next) => { ///// Emit event to server try { - const configuration = await device.postConfiguration(req.params.msn, [], req.body.timestamp); + const configuration = await device.deleteConfiguration(req.params.msn); res.send(configuration); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -102,7 +102,7 @@ const postSlots = async (req, res, next) => { const putSlots = async (req, res, next) => { try { - var slots = await device.postSlots(req.params.msn, req.body.slots, req.body.timestamp); + var slots = await device.putSlots(req.params.msn, req.body.slots, req.body.timestamp); res.send(slots); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -112,7 +112,7 @@ const putSlots = async (req, res, next) => { const deleteSlots = async (req, res, next) => { ///// Emit event to server try { - const slots = await device.postSlots(req.params.msn, [], req.body.timestamp); + const slots = await device.deleteSlots(req.params.msn); res.send(slots); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -141,7 +141,7 @@ const postManualCommand = async (req, res, next) => { const putManualCommand = async (req, res, next) => { try { - var manualCommand = await device.postManualCommand(req.params.msn, req.body.manualCommand); + var manualCommand = await device.putManualCommand(req.params.msn, req.body.manualCommand); res.send(manualCommand); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -151,7 +151,7 @@ const putManualCommand = async (req, res, next) => { const deleteManualCommand = async (req, res, next) => { ///// Emit event to server try { - const manualCommand = await device.postManualCommand(req.params.msn, null); + const manualCommand = await device.deleteManualCommand(req.params.msn); res.send(manualCommand); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -181,7 +181,7 @@ const postStatusCommand = async (req, res, next) => { const putStatusCommand = async (req, res, next) => { try { - var statusCommand = await device.postStatusCommand(req.params.msn, req.body.statusCommand); + var statusCommand = await device.putStatusCommand(req.params.msn, req.body.statusCommand); res.send(statusCommand); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -191,7 +191,7 @@ const putStatusCommand = async (req, res, next) => { const deleteStatusCommand = async (req, res, next) => { ///// Emit event to server try { - const statusCommand = await device.postStatusCommand(req.params.msn, null); + const statusCommand = await device.deleteStatusCommand(req.params.msn); res.send(statusCommand); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -221,7 +221,7 @@ const postAcknowledgedAlerts = async (req, res, next) => { const putAcknowledgedAlerts = async (req, res, next) => { try { - var acknowledgedAlerts = await device.postAcknowledgedAlerts(req.params.msn, req.body.acknowledgedAlerts); + var acknowledgedAlerts = await device.putAcknowledgedAlerts(req.params.msn, req.body.acknowledgedAlerts); res.send(acknowledgedAlerts); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); @@ -231,7 +231,7 @@ const putAcknowledgedAlerts = async (req, res, next) => { const deleteAcknowledgedAlerts = async (req, res, next) => { ///// Emit event to server try { - const acknowledgedAlerts = await device.postAcknowledgedAlerts(req.params.msn, null); + const acknowledgedAlerts = await device.deleteAcknowledgedAlerts(req.params.msn); res.send(acknowledged); } catch (error) { res.status(500).send({message: error.message || "unknown_error"}); diff --git a/package-lock.json b/package-lock.json index 090afbb..aaec6b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "cbor-body-parser": "^1.0.3", "chokidar": "^3.6.0", "crc": "^4.3.2", + "dotenv": "^17.2.2", "events": "^3.2.0", "express": "^4.17.1", "express-session": "^1.17.2", @@ -375,6 +376,17 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "node_modules/dotenv": { + "version": "17.2.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.2.tgz", + "integrity": "sha512-Sf2LSQP+bOlhKWWyhFsn0UsfdK/kCWRv1iuA2gXAwt3dyNabr6QSj00I2V10pidqz69soatm9ZwZvpQMTIOd5Q==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/package.json b/package.json index dd68273..5dc14a4 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,15 @@ "cbor-body-parser": "^1.0.3", "chokidar": "^3.6.0", "crc": "^4.3.2", + "dotenv": "^17.2.2", "events": "^3.2.0", "express": "^4.17.1", "express-session": "^1.17.2", "joi": "^17.4.0", "moment": "^2.29.1", "mongodb": "^3.6.2", + "oauth2-server": "^3.1.1", "random-bytes": "^1.0.0", - "underscore": "^1.13.6", - "oauth2-server": "^3.1.1" + "underscore": "^1.13.6" } } diff --git a/services/device.service.js b/services/device.service.js index a17b2ea..673a4da 100644 --- a/services/device.service.js +++ b/services/device.service.js @@ -19,12 +19,15 @@ const getRequestToDo = async function (msn) { try { var todos = [] const children = await dbcontroller.getDeviceChildren(msn) + var parent = await dbcontroller.getDevice(msn) + children.push(parent) for (const child of children) { ///// Retrieve child - const device = await dbcontroller.getDevice(child.msn) + const msn = child.msn || child.relayMsn || null + const device = await dbcontroller.getDevice(msn) - var todo = { msn : child.msn } + var todo = { serialNumber : msn } ///// Programs if (device.programs != undefined) { @@ -138,6 +141,47 @@ const postPrograms = async function (msn, programs, timestamp) { } } +const putPrograms = async function (msn, programs, timestamp) { + try { + ///// Retrieve device + var device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + if (programs) { + device.programs = { timestamp: timestamp || util.unixTimestamp(), programs: { ...device.programs.programs, ...programs } } + } else { + device.programmingTimestamp = device.programs.timestamp; + // device.programs = undefined + } + + ///// Update device + await dbcontroller.updateDevice(device) + + return device.programs + } catch (e) { + throw new Error(e.message) + } +} + +const deletePrograms = async function (msn) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + device.programs = { timestamp: 0, programs: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.programs + } catch (e) { + throw new Error(e.message) + } +} + const getConfiguration = async function (msn) { try { ///// Retrieve device @@ -173,6 +217,47 @@ const postConfiguration = async function (msn, configuration, timestamp) { } } +const putConfiguration = async function (msn, configuration, timestamp) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + if (configuration) { + device.configuration = { timestamp: timestamp || util.unixTimestamp(), configuration: { ...device.configuration.configuration, ...configuration }} + } else { + device.configurationTimestamp = device.configuration.timestamp; + // device.configuration = undefined + } + + ///// Update device + await dbcontroller.updateDevice(device) + + return device.configuration + } catch (e) { + throw new Error(e.message) + } +} + +const deleteConfiguration = async function (msn) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + device.configuration = { timestamp: 0, configuration: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.configuration + } catch (e) { + throw new Error(e.message) + } +} + const getSlots = async function (msn) { try { ///// Retrieve device @@ -208,6 +293,47 @@ const postSlots = async function (msn, slots, timestamp) { } } +const putSlots = async function (msn, slots, timestamp) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + if (slots) { + device.slots = { timestamp: timestamp || util.unixTimestamp(), slots: { ...device.slots.slots, ...slots } } + } else { + device.programmingTimestamp = device.slots.timestamp; + // device.slots = undefined + } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.slots + } catch (e) { + throw new Error(e.message) + } +} + +const deleteSlots = async function (msn) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variables + device.slots = { timestamp: 0, slots: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.slots + } catch (e) { + throw new Error(e.message) + } +} + const getManualCommand = async function (msn) { try { @@ -243,6 +369,46 @@ const postManualCommand = async function (msn, manualCommand) { } } +const putManualCommand = async function (msn, manualCommand) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + if (manualCommand) { + device.manualCommand = { timestamp: util.unixTimestamp(), manualCommand: { ...device.manualCommand.manualCommand, ...manualCommand } } + } else { + device.manualCommand = undefined + } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.manualCommand + } catch (e) { + throw new Error(e.message) + } +} + +const deleteManualCommand = async function (msn, manualCommand) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + device.manualCommand = { timestamp: 0, manualCommand: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.manualCommand + } catch (e) { + throw new Error(e.message) + } +} + const getStatusCommand = async function (msn) { try { ///// Retrieve device @@ -277,6 +443,46 @@ const postStatusCommand = async function (msn, statusCommand) { } } +const putStatusCommand = async function (msn, statusCommand) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + if (statusCommand) { + device.statusCommand = { timestamp: util.unixTimestamp(), statusCommand: { ...device.statusCommand.statusCommand, ...statusCommand } } + } else { + device.statusCommand = undefined + } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.statusCommand + } catch (e) { + throw new Error(e.message) + } +} + +const deleteStatusCommand = async function (msn) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + device.statusCommand = { timestamp: 0, statusCommand: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.statusCommand + } catch (e) { + throw new Error(e.message) + } +} + const getAcknowledgedAlerts = async function (msn) { try { ///// Retrieve device @@ -310,6 +516,45 @@ const postAcknowledgedAlerts = async function (msn, acknowledgedAlerts) { } } +const putAcknowledgedAlerts = async function (msn, acknowledgedAlerts) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + if (acknowledgedAlerts) { + device.acknowledgedAlerts = { timestamp: util.unixTimestamp(), acknowledgedAlerts: { ...device.acknowledgedAlerts.acknowledgedAlerts, ...acknowledgedAlerts } } + } else { + device.acknowledgedAlerts = undefined + } + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.acknowledgedAlerts + } catch (e) { + throw new Error(e.message) + } +} + +const deleteAcknowledgedAlerts = async function (msn) { + try { + ///// Retrieve device + const device = await dbcontroller.getDevice(msn) + if (!device) { throw({message: "module_not_found_for_this_identifier"}) } + + ///// Set variable + device.acknowledgedAlerts = { timestamp: 0, acknowledgedAlerts: undefined } + + ///// Update device + await dbcontroller.updateDevice(device, msn) + + return device.acknowledgedAlerts + } catch (e) { + throw new Error(e.message) + } +} + module.exports = { getStatus, @@ -318,14 +563,26 @@ module.exports = { getRequestToDo, getPrograms, postPrograms, + putPrograms, + deletePrograms, getConfiguration, postConfiguration, + putConfiguration, + deleteConfiguration, getSlots, postSlots, + putSlots, + deleteSlots, getManualCommand, postManualCommand, + putManualCommand, + deleteManualCommand, getStatusCommand, postStatusCommand, + putStatusCommand, + deleteStatusCommand, getAcknowledgedAlerts, postAcknowledgedAlerts, + putAcknowledgedAlerts, + deleteAcknowledgedAlerts, }