Read the statement by Michael Teeuw here.
Making my first module: issue with notifications
-
Hi all,
I tried to follow the following instructions First module to make my first module.
I have it running on my Raspberry PI, but it results in showing only the original count value of 100000 and doesn’t count down. My question is: what am I doing wrong?
Javascript file
/* Magic Mirror * Module: name of module * */ //Register module Module.register("MMM-DutchGarbageCalendar", { defaults: { foo: "I'm alive!" }, //Start function start: function (){ count = 0 }, //getDom Function getDom: function() { var element = document.createElement("div") element.className = "myContent" element.innerHTML = "Hello, World! " + this.config.foo var subElement = document.createElement("p") subElement.id = "COUNT" element.appendChild(subElement) return element }, //NotificationReceived notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ this.sendSocketNotification("DO_YOUR_JOB", this.count) console.log("notificationReceived: DO_YOUR_JOB:") this.count++ }, 1000) break } }, //socketNotificationReceived socketNotificationReceived: function(notification, payload) { switch(notification) { case "I_DID": var elem = document.getElementById("COUNT") elem.innerHTML = "Count:" + payload console.log("socketReceived:" + payload) break } }, })
node_helper
var NodeHelper = require('node_helper'); var request = require('request'); module.exports = NodeHelper.create({ start: function() { this.countDown = 100000 }, socketNotificationReceived: function(notification, payload) { console.log("Payload: =" + this.payload); switch(notification){ case "DO_YOUR_JOB": this.sendSocketNotification("I_DID", (this.countDown - payload)) console.log("Payload 2: = " + payload); break } }, });
Who can help me out?
-
tiny things
module file
was no ‘count’ variable
also needs to be ‘this.count’ (variable of object instance)/* Magic Mirror * Module: name of module * */ //Register module Module.register("MMM-DutchGarbageCalendar", { defaults: { foo: "I'm alive!" }, count:0, //Start function start: function (){ this.count = 0 }, //getDom Function getDom: function() { var element = document.createElement("div") element.className = "myContent" element.innerHTML = "Hello, World! " + this.config.foo var subElement = document.createElement("p") subElement.id = "COUNT" element.appendChild(subElement) return element }, //NotificationReceived notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ console.log("send socket notificationR: DO_YOUR_JOB:"+this.count) this.sendSocketNotification("DO_YOUR_JOB", this.count) this.count++ }, 1000) break } }, //socketNotificationReceived socketNotificationReceived: function(notification, payload) { switch(notification) { case "I_DID": var elem = document.getElementById("COUNT") elem.innerHTML = "Count:" + payload console.log("socketReceived:" + payload) break } }, })
node helper
NOT use this… this.payloadvar NodeHelper = require('node_helper'); var request = require('request'); module.exports = NodeHelper.create({ countdown: 0, start: function() { this.countDown = 100000 }, socketNotificationReceived: function(notification, payload) { console.log("Payload: =" + JSON.stringify(payload)); // < --- not this. is a parm passed to function switch(notification){ case "DO_YOUR_JOB": let new_payload = this.countDown - payload) this.sendSocketNotification("I_DID", new_payload) console.log("Payload 2: = " + new_payload); break } }, });
-
This post is deleted! -
did u open anther browser window with the MM loaded into it? it will start another instance of the the module.js (with its own counter)
-
@sdetweil Thank you!
It was because I opened one tab and then closed it, open a new one but the timer was still running indeed. When in the future I want to do something with the mechanisme I need to solve this ;)Now I now how notifications work in practice, I can finish my own module :)
-
@rico24 u can also look at my sample module, which accesses config, does notifications,. etc
https://github.com/sdetweil/SampleModule
to fix the problem, you need to send the module identifier in the payload, and have different counters, stored by identifier… when u send back from the helper EVERY module gets notified at once,
so they need to look at the identifier returned to make sure the message is for them, and ignore others -
this is what that would look like… as u want pages insulated from each other as well as module instances
module
/* Magic Mirror * Module: name of module * */ //Register module Module.register("MMM-DutchGarbageCalendar", { defaults: { foo: "I'm alive!" }, ourpage: Math.ceil(Math.random() * 100), count:{}, //Start function start: function (){ this.count[this.ourpage] = 0 }, //getDom Function getDom: function() { var element = document.createElement("div") element.className = "myContent" element.innerHTML = "Hello, World! " + this.config.foo var subElement = document.createElement("p") subElement.id = "COUNT" element.appendChild(subElement) return element }, //NotificationReceived notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ Log.log("send socket notificationR: DO_YOUR_JOB:"+this.count[this.ourpage]) this.sendSocketNotification("DO_YOUR_JOB", {identifier: this.identifier+this.ourpage, value:this.count[this.ourpage]}) this.count[this.ourpage]++; }, 5000) break } }, //socketNotificationReceived socketNotificationReceived: function(notification, payload) { switch(notification) { case "I_DID": if(payload.identifier== this.identifier+this.ourpage){ var elem = document.getElementById("COUNT") elem.innerHTML = "Count:" + payload.value Log.log("socketReceived:" + JSON.stringify(payload)) } else Log.log("notification not for us"); break } }, })
helper
var NodeHelper = require('node_helper'); var request = require('request'); module.exports = NodeHelper.create({ initial_counter:10000, start: function() { this.countDown = {} }, socketNotificationReceived: function(notification, payload) { console.log("Payload: =" + JSON.stringify(payload)); switch(notification){ case "DO_YOUR_JOB": if(!this.countDown.hasOwnProperty(payload.identifier+this.ourpage)){ this.countDown[payload.identifier]=this.initial_counter; } let return_payload={identifier: payload.identifier, value:(this.countDown[payload.identifier] - payload.value)} this.sendSocketNotification("I_DID",return_payload ) console.log("Payload 2: = " + JSON.stringify(return_payload)); break } }, });
-
@rico24 and here is an updated node_helper, that rejects connecting clients if they don’t send the right kind of identifier (ends with a digit)
var NodeHelper = require('node_helper'); var request = require('request'); module.exports = NodeHelper.create({ initial_counter:10000, start: function() { this.countDown = {} }, isDigit: function(x){ return (x>='0' && x< ='9') // watch out for spaces after < , forum hides it all }, socketNotificationReceived: function(notification, payload) { console.log("Payload: =" + JSON.stringify(payload)); switch(notification){ case "DO_YOUR_JOB": if(this.isDigit(payload.identifier.slice(-1))){ if(!this.countDown.hasOwnProperty(payload.identifier+this.ourpage)){ this.countDown[payload.identifier]=this.initial_counter; } let return_payload={identifier: payload.identifier, value:(this.countDown[payload.identifier] - payload.value)} this.sendSocketNotification("I_DID",return_payload ) console.log("Payload 2: = " + JSON.stringify(return_payload)); } else this.sendSocketNotification("I_DID_REJECTED_INVALID_IDENTIFIER",payload ) break } }, });