In this post I’ll be showing you how I got the picture from Wolfram Alpha and showed it on the mirror.
The newly created MMM-infodisplay module accepts a notification called INFO-DISPLAY. All it does is pass along the notification and the payload to the node helper using the sendSocketNotification method.
Inside the node helper of the MMM-infodisplay I created a new function called getValues. Essentially, all it does is loop through the keys of a JSON object. If the property is an object, it will recursively loop through its contents as well. It will push the results in to an array and return it.
In the socketNotificationReceived function I’m handling the notifications passed a long from the module. First, the URL is built. You can find all of the info on how to build up the URL on the Wolfram Alpha site here.
The encodeURIComponent function will take care of the characters that aren’t supported in an URL like a slash or a space. I’m just passing on the content part of the payload. This is set in the Lambda function in one of my earlier posts. Other than that I’m asking to get the result in a JSON format, and only retain the part of the output containing an image.
I’m using the request module to get an URL and parse the body using a call back function. Im parsing out the JSON from the body and use the getValues function to get the “src” attribute. If you investigate the result of the API call well you can see that the URL is contained in this attribute. If I find something in the returned array I return it using the “display-image” notification in the sendSocketNotification function sending the info back to the MMM-infodisplay module.
Here’s the code I do when receiving this notification. Just set the source of the image to the payload and set the image to visible.
Time to test. I just uploaded a SHOW-PICTURE command with content “Michael Jackson” using the debug hub.
Let’s see the result :
Works like a charm!