From f9bc633570dcb553182afd97aa60a028e97f02a1 Mon Sep 17 00:00:00 2001 From: Thorsten Zoerner Date: Fri, 1 Mar 2024 00:42:11 +0100 Subject: [PATCH] Increment: Allow Powerfox/Poweropti or other reading devices to be used as meter points via external REST API --- .gitmodules | 2 +- framework/docs | 2 +- framework/package-lock.json | 10 +-- framework/package.json | 1 + framework/services/httppull.service.js | 61 +++++++++++++------ ...ull_model.js => httppull_model.service.js} | 0 6 files changed, 49 insertions(+), 27 deletions(-) rename framework/services/{httppull_model.js => httppull_model.service.js} (100%) diff --git a/.gitmodules b/.gitmodules index 973fcb2..99f1ee3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "framework/docs"] path = framework/docs - url = https://github.com/energychain/STROMDAO_EAFs.wiki.git + url = git@github.com:energychain/STROMDAO_EAFs.wiki.git diff --git a/framework/docs b/framework/docs index 813c182..d00fa68 160000 --- a/framework/docs +++ b/framework/docs @@ -1 +1 @@ -Subproject commit 813c182a80c5b1e60a0cfe13f650b8f1610c3808 +Subproject commit d00fa688d6cf6c22b369238ea455934b4cc10a68 diff --git a/framework/package-lock.json b/framework/package-lock.json index 9addd9a..405adb7 100644 --- a/framework/package-lock.json +++ b/framework/package-lock.json @@ -13,6 +13,7 @@ "@tensorflow/tfjs": "^4.15.0", "axios": "^1.6.2", "dotenv": "^16.3.1", + "handlebars": "^4.7.8", "jsonwebtoken": "^9.0.2", "moleculer": "^0.14.26", "moleculer-auto-openapi": "^1.1.6", @@ -6536,7 +6537,6 @@ "version": "4.7.8", "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz", "integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==", - "dev": true, "dependencies": { "minimist": "^1.2.5", "neo-async": "^2.6.2", @@ -10146,8 +10146,7 @@ "node_modules/neo-async": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz", - "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", - "dev": true + "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==" }, "node_modules/nerf-dart": { "version": "1.0.0", @@ -15294,7 +15293,6 @@ }, "node_modules/source-map": { "version": "0.6.1", - "dev": true, "license": "BSD-3-Clause", "engines": { "node": ">=0.10.0" @@ -16063,7 +16061,6 @@ "version": "3.17.4", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", "integrity": "sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==", - "dev": true, "optional": true, "bin": { "uglifyjs": "bin/uglifyjs" @@ -16364,8 +16361,7 @@ "node_modules/wordwrap": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz", - "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==", - "dev": true + "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==" }, "node_modules/wrap-ansi": { "version": "7.0.0", diff --git a/framework/package.json b/framework/package.json index 7104f9a..4d95854 100644 --- a/framework/package.json +++ b/framework/package.json @@ -70,6 +70,7 @@ "@tensorflow/tfjs": "^4.15.0", "axios": "^1.6.2", "dotenv": "^16.3.1", + "handlebars": "^4.7.8", "jsonwebtoken": "^9.0.2", "moleculer": "^0.14.26", "moleculer-auto-openapi": "^1.1.6", diff --git a/framework/services/httppull.service.js b/framework/services/httppull.service.js index 696cca3..8a2035d 100644 --- a/framework/services/httppull.service.js +++ b/framework/services/httppull.service.js @@ -43,20 +43,22 @@ module.exports = { throw new ApiGateway.Errors.UnAuthorizedError(ApiGateway.Errors.ERR_INVALID_TOKEN); } } - let results = await ctx.call("clearings_model.find",{ + let results = await ctx.call("httppull_model.find",{ query: { - meterId: ctx.params.meterId - }, - sort:"-epoch" + requestId: ctx.params.requestId + } }); + if((typeof results !== 'undefined') && (results !== null) && (results.length == 1)) { + const res = await axios(results[0].fetch); + return res.data; + } else return null; - return results; } }, process: { rest: { method: "GET", - path: "/fetch" + path: "/process" }, params: { requestId: { @@ -65,23 +67,46 @@ module.exports = { } }, async handler(ctx) { - if((typeof ctx.meta.user !== 'undefined') && (typeof ctx.meta.user.meterId !== 'undefined')) { - // Ensure Authenticated Token is authorized - if(ctx.meta.user.meterId !== ctx.params.meterId) { - throw new ApiGateway.Errors.UnAuthorizedError(ApiGateway.Errors.ERR_INVALID_TOKEN); - } - } - let results = await ctx.call("clearings_model.find",{ + let json = await ctx.call("httppull.fetch",{requestId:ctx.params.requestId}); + if(json !== null) { + const results = await ctx.call("httppull_model.find",{ + query: { + requestId: ctx.params.requestId + } + }); + const rule = results[0].processor; + const Handlebars = require('handlebars'); + + function convertObject(obj, rulesTemplate) { + const template = Handlebars.compile(rulesTemplate); + const context = { json: obj }; + const convertedData = JSON.parse(template(context)); + return convertedData; + } + + return convertObject(json, rule); + } else return null; + } + }, + updateReading: { + rest: { + method: "GET", + path: "/updateReading" + }, + async handler(ctx) { + const results = await ctx.call("httppull_model.find",{ query: { meterId: ctx.params.meterId - }, - sort:"-epoch" + } }); - - return results; + + let json = await ctx.call("httppull.process",{requestId:results[0].requestId}); + if(json !== null) { + json.meterId = results[0].meterId; + return await ctx.call("metering.updateReading",json); + } else return {}; } } - }, /** diff --git a/framework/services/httppull_model.js b/framework/services/httppull_model.service.js similarity index 100% rename from framework/services/httppull_model.js rename to framework/services/httppull_model.service.js