sendNotification & notificationReceived does not seem to work, am I missing something.



  • Ok,

    I am making a module that wants to use the current temperature from the default Currentweather module. In the README file for modules it appears that I can just have the currentweather module call the sendNotification() function and my module will receive it with the notificationReceived() function. For example,

    In the currentweather.js file:

    this.sendNotification('Temperature', {temperature:this.temperature});
    

    In the receiving .js file:

    notificationReceived: function(notification, payload) {
    	if(notification === 'Temperature')
    			this.temp=notification;
    },
    

    Am I missing something? I dont think I would have to create a node_helper to have one module send a payload to another. The issue I am getting is that this.temp remains undefined and I don’t know why this is happening.

    Thanks!



  • @Squawk09 you’re right you don’t need a node_helper for it. Did you log if you’re receiving a notification, maybe you don’t or you try to access it before you get the notification? A guess in the dark is, that you loose the scope of this with using a callback function. Can you please provide also the snippet where you trying to access your variable?



  • @Squawk09

    The first (sendNotification) step looks right, depending on where in the currentweather.js file you have added that line. To test it, I placed it the processWeather function just before the this.loaded = true; line, e.g

    	processWeather: function(data) {
    
     		// EXISTING CODE REMOVED FOR BREVITY ON THIS FORUM     
    
    		this.sendNotification('Temperature', {temperature:this.temperature});
    
    		this.loaded = true;
    		this.updateDom(this.config.animationSpeed);
    	},
    

    However, I think the second (notificationReceived) step needs a little tweaking. Try this

        notificationReceived(notification, payload, sender) {
            if (notification === 'Temperature' && sender.name === 'currentweather') {
                var currentweather = payload;
                if ( currentweather && currentweather.hasOwnProperty('temperature') ) {
                    this.temp = currentweather.temperature;
                    Log.log('The current temperature is ' + currentweather.temperature);
                }
            }
        },
    

    This first checks whether a temperature notification has been received from the currentweather module and then checks that the payload has the temperature property before assigning it to the this.temp variable.

    Do note however that any modifications to the core files and modules will need to be reapplied after upgrading the mirror. To get round this, you might want to consider creating a pull request for your modification in the hope that it will be incorporated within the central code



  • @strawberry-3.141 Yes, pasted below are the two files. I am new to JS modules and node.js servers so I was not aware you had to log if you’re receiving a notification. What are you notifying and where do you declare the notification? Do you know of any good documentation on this? I pasted in the code ianperrin suggested to see if it would work but it still displays this.temp as undefined. I must be missing something basic. Thanks for the help!

    /* global Module */
    
    /* Magic Mirror
     * Module: Bike
    
     * MIT Licensed.
     */
    
    Module.register("bike",{	
    
    // Default module config.
    defaults: {
    	text: "Hello good!"	
    },
    start: function() {
    	this.temp = "This should change";
    },
    notificationReceived:function(notification, payload, sender) {
        if (notification === 'Temperature' && sender.name === 'currentweather') {
            var currentweather = payload;
            if ( currentweather && currentweather.hasOwnProperty('temperature') ) {
                this.temp = currentweather.temperature;
                Log.log('The current temperature is ' + currentweather.temperature);
            }
        }
    },	
    // Override dom generator.
    getDom: function() {
    	var wrapper = document.createElement("div");
    	var myTemp = document.createElement("div");
    	myTemp.innerHTML=this.temp;
    	
    	wrapper.innerHTML = this.config.text;
    	wrapper.appendChild(myTemp);
    	return wrapper;
      }
    });
    

    Here is the current weather function that contains the submission.

    processWeather: function(data) {
    	this.temperature = this.roundValue(data.main.temp);
    	
    	if (this.config.useBeaufort){
    		this.windSpeed = this.ms2Beaufort(this.roundValue(data.wind.speed));
    	}else {
    		this.windSpeed = parseFloat(data.wind.speed).toFixed(0);
    	}
    
    
    	this.windDirection = this.deg2Cardinal(data.wind.deg);
    	this.weatherType = this.config.iconTable[data.weather[0].icon];
    
    	var now = new Date();
    	var sunrise = new Date(data.sys.sunrise * 1000);
    	var sunset = new Date(data.sys.sunset * 1000);
    
    	// The moment().format('h') method has a bug on the Raspberry Pi.
    	// So we need to generate the timestring manually.
    	// See issue: https://github.com/MichMich/MagicMirror/issues/181
    	var sunriseSunsetDateObject = (sunrise < now && sunset > now) ? sunset : sunrise;
    	var timeString = moment(sunriseSunsetDateObject).format('HH:mm');
    	if (this.config.timeFormat !== 24) {
    		//var hours = sunriseSunsetDateObject.getHours() % 12 || 12;
    		if (this.config.showPeriod) {
    			if (this.config.showPeriodUpper) {
    				//timeString = hours + moment(sunriseSunsetDateObject).format(':mm A');
    				timeString = moment(sunriseSunsetDateObject).format('h:mm A');
    			} else {
    				//timeString = hours + moment(sunriseSunsetDateObject).format(':mm a');
    				timeString = moment(sunriseSunsetDateObject).format('h:mm a');
    			}
    		} else {
    			//timeString = hours + moment(sunriseSunsetDateObject).format(':mm');
    			timeString = moment(sunriseSunsetDateObject).format('h:mm');
    		}
    	}
    
    	this.sunriseSunsetTime = timeString;
    	this.sunriseSunsetIcon = (sunrise < now && sunset > now) ? "wi-sunset" : "wi-sunrise";
    
    	this.sendNotification('Temperature', {temperature:this.temperature});
    
    	this.loaded = true;
    	this.updateDom(this.config.animationSpeed);
    },


  • @Squawk09 - it is not a requirement to log when receiving notifications, but it can be useful when debugging. To prove you are receiving the notification, change the line to Log.log('The current temperature is ' + this.temp); and look in the browsers console and you should see the temperature displayed when it is sent from the current weather module.

    Once you have confirmed that your module is receiving the notifications, you can then move on to displaying the temperature.

    The trick is to understand the sequence of events that occur and how they affect your module. In your case the functions in your modules are currently fired in the following order:

    • start
    • notificationReceived (ALL_MODULES_STARTED)
    • getDom
    • notificationReceived (DOM_OBJECTS_CREATED)
    • notificationReceived (Temperature)

    The result is that your getDom function is called before the Temperature notification is received, so this.temp hasn’t been set yet.

    Since you cannot control the order in which the modules are loaded, or when the Temperature notification is sent by the current weather module, you need to tell your module to call the getDom function again, after the Temperature notification has been received.

    To do this, change your notificationReceived function to

    notificationReceived: function(notification, payload, sender) {
        if (notification === 'Temperature' && sender.name === 'currentweather') {
            var currentweather = payload;
            if ( currentweather && currentweather.hasOwnProperty('temperature') ) {
                this.temp = currentweather.temperature;
                this.updateDom();
                Log.log('The current temperature is ' + this.temp);
            }
        }
    },
    

    Check out the wiki for documentation, and of course keep asking questions here in these forums :)


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.