Skip to content

Commit

Permalink
Merge pull request #8 from VDenisyuk/cache
Browse files Browse the repository at this point in the history
stopID caching to prevent excessive findStop calls
  • Loading branch information
skastenholz authored Dec 15, 2022
2 parents f41f758 + 0875222 commit 29f1fe1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
12 changes: 7 additions & 5 deletions MMM-DVB.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@ Module.register("MMM-DVB", {
},

start: function() {
Log.info("Starting module: " + this.name);
this.request(this);
setInterval(this.request, this.config.reload, this);
Log.info("Starting module: " + this.identifier);
this.sendSocketNotification("REGISTER-MODULE", this.identifier);
},

request: function(self) {

var request = {
id: self.identifier,
stopName: self.config.stopName,
Expand All @@ -162,7 +160,11 @@ Module.register("MMM-DVB", {
},

socketNotificationReceived: function(notification, payload) {
if (notification === "DVB-RESPONSE" && payload.id === this.identifier) {
if (notification === "REGISTER-ACK" && payload === this.identifier) {
Log.log("Module registered: " + payload);
this.request(this);
var interval = setInterval(this.request, this.config.reload, this);
} else if (notification === "DVB-RESPONSE" && payload.id === this.identifier) {
Log.log("Response: " + JSON.stringify(payload));
this.dvb_data = payload.connections;
this.updateDom();
Expand Down
54 changes: 41 additions & 13 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
var dvb = require('dvbjs');
var NodeHelper = require("node_helper");
const Log = require("logger");

module.exports = NodeHelper.create({

start: function() {
Log.log("Starting node helper for: " + this.name);
this.stop = {};
},
monitor: function(payload) {
var self = this;
dvb.monitor(this.stop[payload.id].id, payload.timeOffset, this.numberOfRequestedResults(payload)).then((data) => {
var response = {
id: payload.id,
connections: self.connectionsToBeDisplayed(data, payload)
};
self.sendSocketNotification("DVB-RESPONSE", response);
});
},
findStop: function(payload) {
var self = this;
Log.log("Searching stop ID for: " + payload.stopName);
dvb.findStop(payload.stopName).then((data) => {
if (Array.isArray(data) && data.length > 0) {
self.stop[payload.id].id = data[0].id;
self.stop[payload.id].name = payload.stopName;
self.monitor(payload);
}
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'DVB-REQUEST') {
var self = this;
dvb.findStop(payload.stopName).then((data) => {
if (Array.isArray(data) && data.length > 0) {
var stopID = data[0].id;
dvb.monitor(stopID, payload.timeOffset, this.numberOfRequestedResults(payload)).then((data) => {
var response = {
id: payload.id,
connections: self.connectionsToBeDisplayed(data, payload)
};
self.sendSocketNotification("DVB-RESPONSE", response);
});
var self = this;
switch (notification) {
case 'REGISTER-MODULE':
self.stop[payload] = {
id: {},
name: {}
};
self.sendSocketNotification("REGISTER-ACK", payload);
break;
case 'DVB-REQUEST':
if (!self.stop[payload.id].id || self.stop[payload.id].name != payload.stopName) {
self.findStop(payload);
} else {
self.monitor(payload);
}
});
break;
}
},
numberOfRequestedResults: function(payload) {
Expand Down

0 comments on commit 29f1fe1

Please sign in to comment.