Skip to content

Commit

Permalink
Increment: Allow Powerfox/Poweropti or other reading devices to be us…
Browse files Browse the repository at this point in the history
…ed as meter points via external REST API
  • Loading branch information
zoernert committed Feb 29, 2024
1 parent a300d71 commit f9bc633
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion framework/docs
Submodule docs updated from 813c18 to d00fa6
10 changes: 3 additions & 7 deletions framework/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 43 additions & 18 deletions framework/services/httppull.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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 {};
}
}

},

/**
Expand Down
File renamed without changes.

0 comments on commit f9bc633

Please sign in to comment.