Skip to content

Commit

Permalink
Increment: cron based http fetch for remote meter reading processors.
Browse files Browse the repository at this point in the history
  • Loading branch information
zoernert committed Apr 24, 2024
1 parent 1e495d3 commit 3bc2e3d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
42 changes: 42 additions & 0 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 @@ -74,6 +74,7 @@
"jsonwebtoken": "^9.0.2",
"moleculer": "^0.14.26",
"moleculer-auto-openapi": "^1.1.6",
"moleculer-cron": "^0.0.4",
"moleculer-db": "^0.8.25",
"moleculer-db-adapter-mongo": "^0.4.19",
"moleculer-web": "^0.10.4",
Expand Down
43 changes: 42 additions & 1 deletion framework/services/httppull.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,35 @@
*/
const ApiGateway = require("moleculer-web"); // Included for Invalid Authentication Errors
const axios = require("axios");

const Cron = require("moleculer-cron");

/** @type {ServiceSchema} */
module.exports = {
name: "httppull",
mixins: [Cron],

/**
* Dependencies
*/
dependencies: ["httppull_model"],

crons: [
{
name: "Scheduled HTTP Pulls",
cronTime: '*/5 * * * *',
manualStart: true,
timeZone: 'Europe/Berlin',
onTick: async function() {
console.log('Scheduled HTTP Pulls triggered');
},
runOnInit: function() {
console.log("Scheduled HTTP Pulls is created");
},
onComplete: function() {
console.log("Scheduled HTTP Pulls is finished");
}
}
],
/**
* Actions
*/
Expand Down Expand Up @@ -76,6 +94,10 @@ module.exports = {
}
});
const rule = results[0].processor;
results[0].lastFetch = new Date().getTime();
results[0].id = results[0]._id;
await ctx.call("httppull_model.update",results[0]);

const Handlebars = require('handlebars');

function convertObject(obj, rulesTemplate) {
Expand All @@ -92,6 +114,25 @@ module.exports = {
}
}
},
scheduledFetch: {
rest: {
method: "GET",
path: "/scheduledFetch"
},
async handler(ctx) {
const results = await ctx.call("httppull_model.find",{
$or: [
{lastFetch: { $exists: false }},
{lastFetch: { $gt: new Date().getTime() - 86400000 }}
]
});
let res = [];
for(let i=0;i<results.length;i++) {
res.push(await ctx.call("httppull.process",{requestId:results[i].requestId}));
}
return res;
}
},
updateReading: {
rest: {
method: "GET",
Expand Down
2 changes: 1 addition & 1 deletion framework/services/httppull_model.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
* Settings
*/
settings: {
fields: ["_id", "requestId","fetch","processor","meterId"],
fields: ["_id", "requestId","fetch","processor","meterId","lastFetch"],
},

/**
Expand Down

0 comments on commit 3bc2e3d

Please sign in to comment.