Navigation

    MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    UNSOLVED Making my first module: issue with notifications

    Troubleshooting
    magicmirror module notifications
    2
    8
    376
    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
      rico24 last edited by rico24

      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
        sdetweil last edited by sdetweil

        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
                        }
                },
        });
        
        R 1 Reply Last reply Reply Quote 0
        • R
          rico24 @sdetweil last edited by rico24

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • S
            sdetweil last edited by

            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)

            R 1 Reply Last reply Reply Quote 0
            • R
              rico24 @sdetweil last edited by

              @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 Reply Quote 0
              • S
                sdetweil @rico24 last edited by sdetweil

                @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

                1 Reply Last reply Reply Quote 0
                • S
                  sdetweil last edited by sdetweil

                  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
                                  }
                          },
                  });
                  
                  S 1 Reply Last reply Reply Quote 0
                  • S
                    sdetweil @sdetweil last edited by sdetweil

                    @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
                                    }
                            },
                    });
                    
                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Enjoying MagicMirror? Please consider a donation!
                    MagicMirror created by Michael Teeuw.
                    Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                    This forum is using NodeBB as its core | Contributors
                    Contact | Privacy Policy