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.

    Help needed simple API based module

    Scheduled Pinned Locked Moved Development
    19 Posts 4 Posters 12.2k Views 4 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.
    • W Offline
      washichi
      last edited by

      Hi,
      I just started creating modules, and don’t have any javascript experience.
      I am trying my luck on a really simple API based ISS tracker module.
      but I am doing something wrong.

      here is my code: https://github.com/washichi/MMM-ISS-current-location

      the logs mm-out-0.log &mm-error-0.log don’t give me weird errors.
      and I compiled it with http://liveweave.com/ (can’t find the right plugin for sublime3 ) so I’m doing something wrong in the code.

      I hope someone can help me to see what I did wrong.

      1 Reply Last reply Reply Quote 0
      • cowboysdudeC Offline
        cowboysdude Module Developer
        last edited by

        I’m no programmer [yet] LOL But I will tell you that you also need something like this in there:

        notificationReceived(notification, payload, sender)

        That MagicMirror core has the ability to send notifications to modules. Or even better: the modules have the possibility to send notifications to other modules. When this module is called, it has 3 arguments:

        notification - String - The notification identifier.
        payload - AnyType - The payload of a notification.
        sender - Module - The sender of the notification. If this argument is undefined, the sender of the notififiction is the core system.
        Example:

        notificationReceived: function(notification, payload, sender) {
        if (sender) {
        Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
        } else {
        Log.log(this.name + " received a system notification: " + notification);
        }
        }
        Note: the system sends two notifications when starting up. These notifications could come in handy!

        ALL_MODULES_STARTED - All modules are started. You can now send notifications to other modules.
        DOM_OBJECTS_CREATED - All dom objects are created. The system is now ready to perform visual changes.
        socketNotificationReceived: function(notification, payload)

        When using a node_helper, the node helper can send your module notifications. When this module is called, it has 2 arguments:

        notification - String - The notification identifier.
        payload - AnyType - The payload of a notification.
        Note 1: When a node helper sends a notification, all modules of that module type receive the same notifications.
        Note 2: The socket connection is established as soon as the module sends its first message using sendSocketNotification.

        Example:

        socketNotificationReceived: function(notification, payload) {
        Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
        },

        W 1 Reply Last reply Reply Quote 0
        • W Offline
          washichi @cowboysdude
          last edited by

          @cowboysdude
          mhh okay, thank you! I’ll try to implement it, looking to the default weatherforecast module as example.

          from the modules readme I don’t totally understand why I need this, it sounds like optional . and I don’t have notifications. (or are updating modules seen as notification?)

          1 Reply Last reply Reply Quote 0
          • yawnsY Offline
            yawns Moderator
            last edited by

            Hi, first spotted error:

            Module.register("ISS-current-location",{
            

            This should be

            Module.register("MMM-ISS-current-location",{
            

            Otherwise your module won’t be called.

            W 1 Reply Last reply Reply Quote 1
            • W Offline
              washichi @yawns
              last edited by

              @yawns yeah that solved it, my module is now at least showing :).
              sorry, stupid mistake haha, I though I checked it .
              Thanks for your help

              1 Reply Last reply Reply Quote 0
              • yawnsY Offline
                yawns Moderator
                last edited by

                okay, next thing is your updateISS function. I modified it for you to get you started:

                	updateISS: function() {
                		var self = this;
                		var url = self.config.apiBase;
                		var opennotifyRequest = new XMLHttpRequest();
                		opennotifyRequest.open("GET", url, true);
                		opennotifyRequest.onreadystatechange = function() {
                		if (this.readyState === 4) {
                			if (this.status === 200) {
                				var resp = JSON.parse(this.response);
                				this.latitude = resp.iss_position.latitude;
                				this.longitude = resp.iss_position.longitude;
                				this.timestamp = resp.timestamp;
                				this.message = resp.message;
                			}
                		}
                			
                			self.updateDom();
                		};
                	opennotifyRequest.send();
                	}
                
                W 1 Reply Last reply Reply Quote 0
                • W Offline
                  washichi @yawns
                  last edited by washichi

                  @yawns thank you!
                  the module shows now this.message and refreshes it with the schedule.
                  however I am still doing something wrong in my request:

                  	updateISS: function() {
                  		var url = this.config.apiBase;
                  		var self = this;
                  		var opennotifyRequest = new XMLHttpRequest();
                  		opennotifyRequest.open("GET", url, true);
                  		this.message = "in updateISS";
                  		opennotifyRequest.onreadystatechange = function() {
                  		self.message = "request status: "+ to.String(this.status); // not displayed
                  		if (this.readyState === 4) {
                  			if (this.status === 200) {
                  				var resp = JSON.parse(this.response);
                  				self.latitude = resp.iss_position.latitude;
                  				self.longitude = resp.iss_position.longitude;
                  				self.timestamp = resp.timestamp;
                  				self.message = resp.message;
                  				self.message = "dit gebeurt niet";
                  			}
                  		}
                  		};
                  	opennotifyRequest.send();
                  	self.updateDom();
                  	}
                  

                  I can’t set message in that function, for example line 86. I use self/this wrong, or the function isn’t executing. but the code continous because when I set the message before or after that function it gets displayed.

                  yawnsY 1 Reply Last reply Reply Quote 0
                  • yawnsY Offline
                    yawns Moderator @washichi
                    last edited by

                    @washichi
                    hm. Did you try to output latitude and longitude as well in getDom to see if this is working?

                    W 1 Reply Last reply Reply Quote 0
                    • W Offline
                      washichi @yawns
                      last edited by

                      @yawns No I didn’t, but that should be exactly the same thing.
                      If I hardcode the (any) variable in “issRequest.onreadystatechange = function() {”
                      then it just doesn’t display, so it looks like it isn’t going in that function.
                      but if I hardcode the (any) variable after that function It displays.

                      So I can display the variable (message), but the variable doesn’t get filled with the request.

                      1 Reply Last reply Reply Quote 0
                      • yawnsY Offline
                        yawns Moderator
                        last edited by

                        Simple way to test if it entering the function:

                        Log.error("function entered");

                        Press F12 in browser, check for console output and refresh the website. If the function is entered you will see “function entered” in the console.

                        1 Reply Last reply Reply Quote 0
                        • W Offline
                          washichi
                          last edited by

                          thank you! It’s now working :)
                          I thought that my scheduleupdate was working but it didn’t (only 2 times )
                          with the Log function I could find the problem easy .

                          1 Reply Last reply Reply Quote 1
                          • yawnsY Offline
                            yawns Moderator
                            last edited by

                            great. Now tidy up your code ;) and present your module in modules showcase

                            ooom416354O 1 Reply Last reply Reply Quote 2
                            • ooom416354O Offline
                              ooom416354 @yawns
                              last edited by

                              @washichi @yawns this is great work. I am wondering, would this completed code be kind of a base for making other API calls which return data in JSON format?

                              W cowboysdudeC 2 Replies Last reply Reply Quote 1
                              • W Offline
                                washichi @ooom416354
                                last edited by

                                @ooom416354 Yes, but other modules are also that.
                                I used the default module “weatherforecast” it is an simple api based module that gets the data and displays it in a table.

                                I’m now trying to add a map, and then display the ISS current position on that, but implementing this in a module is a lot harder then normal javascript :p.

                                ooom416354O 1 Reply Last reply Reply Quote 0
                                • yawnsY Offline
                                  yawns Moderator
                                  last edited by

                                  @ooom416354
                                  Yes, kind of. At least with JSON calls without additional headers. The you need to use request and a node_helper

                                  1 Reply Last reply Reply Quote 0
                                  • ooom416354O Offline
                                    ooom416354 @washichi
                                    last edited by

                                    @washichi Yea, I am still not even sure how to translate the returned JSON to an actual visual table on the mirror. Not very good at this lol

                                    cowboysdudeC 1 Reply Last reply Reply Quote 1
                                    • cowboysdudeC Offline
                                      cowboysdude Module Developer @ooom416354
                                      last edited by

                                      @ooom416354 It could be yes ;)

                                      1 Reply Last reply Reply Quote 0
                                      • cowboysdudeC Offline
                                        cowboysdude Module Developer @ooom416354
                                        last edited by

                                        @ooom416354 said in Help needed simple API based module:

                                        @washichi Yea, I am still not even sure how to translate the returned JSON to an actual visual table on the mirror. Not very good at this lol

                                        Me either but won’t stop me from trying :)

                                        ooom416354O 1 Reply Last reply Reply Quote 0
                                        • ooom416354O Offline
                                          ooom416354 @cowboysdude
                                          last edited by

                                          @cowboysdude yup I agree with that!

                                          1 Reply Last reply Reply Quote 1

                                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                          With your input, this post could be even better 💗

                                          Register Login
                                          • 1 / 1
                                          • First post
                                            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