• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Making my first module: issue with notifications

Scheduled Pinned Locked Moved Unsolved Troubleshooting
notificationsmodulemagicmirror
8 Posts 2 Posters 1.1k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    rico24 Module Developer
    last edited by rico24 Jan 15, 2020, 7:16 PM Jan 15, 2020, 7:14 PM

    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?

    1 Reply Last reply Reply Quote 0
    • S Offline
      sdetweil
      last edited by sdetweil Jan 15, 2020, 7:57 PM Jan 15, 2020, 7:55 PM

      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.payload

      var 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
                      }
              },
      });
      

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      R 1 Reply Last reply Jan 15, 2020, 8:13 PM Reply Quote 0
      • R Offline
        rico24 Module Developer @sdetweil
        last edited by rico24 Jan 15, 2020, 8:35 PM Jan 15, 2020, 8:13 PM

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil
          last edited by Jan 15, 2020, 8:41 PM

          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)

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          R 1 Reply Last reply Jan 15, 2020, 8:59 PM Reply Quote 0
          • R Offline
            rico24 Module Developer @sdetweil
            last edited by Jan 15, 2020, 8:59 PM

            @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 :)

            S 1 Reply Last reply Jan 15, 2020, 9:02 PM Reply Quote 0
            • S Offline
              sdetweil @rico24
              last edited by sdetweil Jan 16, 2020, 12:25 AM Jan 15, 2020, 9:02 PM

              @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

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • S Offline
                sdetweil
                last edited by sdetweil Jan 16, 2020, 6:33 PM Jan 16, 2020, 6:31 PM

                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
                                }
                        },
                });
                

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                S 1 Reply Last reply Jan 16, 2020, 10:38 PM Reply Quote 1
                • S Offline
                  sdetweil @sdetweil
                  last edited by sdetweil Jan 26, 2020, 5:47 PM Jan 16, 2020, 10:38 PM

                  @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
                                  }
                          },
                  });
                  

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  1 / 1
                  • First post
                    1/8
                    Last post
                  Enjoying MagicMirror? Please consider a donation!
                  MagicMirror created by Michael Teeuw.
                  Forum managed by Sam, technical setup by Karsten.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy