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.

    Multiple URL Querys to fill JSON

    Scheduled Pinned Locked Moved Solved Troubleshooting
    10 Posts 3 Posters 240 Views 3 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.
    • htilburgsH Offline
      htilburgs
      last edited by

      I hope I describe it correct and you understand what I mean.
      I’m writing a new module which has to display the information from 2 different URL’s.
      When I query them seperate than everythink works OK.
      But I want to do query from URL-1 to JSON (MHW-P1) and URL-2 to JSON (MHW-WM).

      Is this possible and how?
      This is what I’ve came up with, but it doesn’t work.

      node_helper.js

      // P1 Meter section
      //
      getMHW_P1: function(urlP1) {
              // Make a GET request using the Fetch API for the P1 Meter
              fetch(urlP1)
                .then(response_P1 => {
                  if (!response_P1.ok) {
                    console.error('MMM-MyHomeWizard: Network response was not ok');
                  }
                  return response_P1.json();
                })
      
                .then(result_P1 => {
                  // Process the retrieved user data
                  console.log(result_P1); // Remove trailing slashes to display data in Console for testing
                  this.sendSocketNotification('MHWP1_RESULT', result_P1);
                })
      
                .catch(error => {
                  console.error('Error:', error);
                });
        },
      
        socketNotificationReceived: function(notification, payload_P1) {
                  if (notification === 'GET_MHWP1') {
                  this.getMHW_P1(payload_P1);
                  }
        },
        
        // Water Meter Section
        //
        getMHW_WM: function(urlWM) {
              // Make a GET request using the Fetch API for the Water Meter
              fetch(urlWM)
                .then(response_WM => {
                  if (!response_WM.ok) {
                    console.error('MMM-MyHomeWizard: Network response was not ok');
                  }
                  return response_WM.json();
                })
      
                .then(result_WM => {
                  // Process the retrieved user data
                  console.log(result_WM); // Remove trailing slashes to display data in Console for testing
                  this.sendSocketNotification('MHWWM_RESULT', result_WM);
                })
      
                .catch(error => {
                  console.error('Error:', error);
                });
        },
      
        socketNotificationReceived: function(notification, payload_WM) {
                  if (notification === 'GET_MHWWM') {
                  this.getMHW_WM(payload_WM);
                  }
        },
      

      MMM-MyModules.js

      // <-- P1 Meter Section -->
      	
      	// This processes your data P1 Meter
      	processMHW_P1: function(data_P1) { 
      		this.MHW_P1 = data_P1; 
      		console.log(JSON.stringify(this.MHW_P1)); // uncomment to see if you're getting data (in dev console)
      		this.loaded = true;
      	},
      
      	// this tells module when to update
      	scheduleUpdate: function() { 
      		setInterval(() => {
      		    	this.getMHW_P1();
      			this.getMHW_WM();
      		}, this.config.updateInterval);
      		this.getMHW_P1();
      		this.getMHW_WM();
      		var self = this;
      	},
      	  
      	// this asks node_helper for data
      	getMHW_P1: function() { 
      		this.sendSocketNotification('GET_MHWP1', this.urlP1);
      	},
      
      	// this gets data from node_helper
      	socketNotificationReceived: function(notification_P1, payload_P1) { 
      		if (notification_P1 === "MHWP1_RESULT") {
      		// this notification doesn't come back on error..
      		this.processMHW_P1(payload_P1);
      		this.updateDom(this.config.initialLoadDelay); 
      		}
      	},
      	
      //<-- Water Meter Section -->
      	
      	// This processes your data Water Meter
      	processMHW_WM: function(data_WM) { 
      		this.MHW_WM = data_WM; 
      		console.log(JSON.stringify(this.MHW_WM)); // uncomment to see if you're getting data (in dev console)
      		this.loaded = true;
      	},
      
      /*	// this tells module when to update
      	scheduleUpdate: function() { 
      		setInterval(() => {
      		    	this.getMHW_WM();
      		}, this.config.updateInterval);
      		this.getMHW_WM();
      		var self = this;
      	},
      */	  
      	// this asks node_helper for data
      	getMHW_WM: function() { 
      		this.sendSocketNotification('GET_MHWWM', this.urlWM);
      	},
      
      	// this gets data from node_helper
      	socketNotificationReceived: function(notification_WM, payload_WM) { 
      		if (notification_WM === "MHWWM_RESULT") {
      		// this notification doesn't come back on error..
      		this.processMHW_WM(payload_WM);
      		this.updateDom(this.config.initialLoadDelay);
      		}
      	},
      
      I hope somebody can help me with this.
      I'm not a programmer, but I try to do my best ;-)
      

      (still trying to learn JS, but not afraid to ask) ☺

      S 1 Reply Last reply Reply Quote 0
      • htilburgsH Offline
        htilburgs @sdetweil
        last edited by

        @sdetweil Wow, it works!
        Just checking, it has to be done in node_helper and the MMM-Module?

        (still trying to learn JS, but not afraid to ask) ☺

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

          @htilburgs you cant have two socketNotificationReceived() functions

          only 1, but it gets both notifications

          if notification=== "type1"
               call this function
          else   if notification=== "type2"
               call that function
          

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          htilburgsH 1 Reply Last reply Reply Quote 0
          • htilburgsH Offline
            htilburgs @sdetweil
            last edited by

            @sdetweil I’ve changed it into this, but still only result from the WaterMeter and not the P1 meter

            	socketNotificationReceived: function(notification, payload) { 
            		if (notification === "MHWP1_RESULT") {
            		// this notification doesn't come back on error..
            		this.processMHW_P1(payload);
            		this.updateDom(this.config.initialLoadDelay); 
            		}
            		else if (notification === "MHWWM_RESULT") {
            		// this notification doesn't come back on error..
            		this.processMHW_WM(payload);
            		this.updateDom(this.config.initialLoadDelay);
            		}
            	},
            

            (still trying to learn JS, but not afraid to ask) ☺

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

              @htilburgs I meant in the node_helper… you have 2 there as well

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              htilburgsH 1 Reply Last reply Reply Quote 0
              • htilburgsH Offline
                htilburgs @sdetweil
                last edited by

                @sdetweil Wow, it works!
                Just checking, it has to be done in node_helper and the MMM-Module?

                (still trying to learn JS, but not afraid to ask) ☺

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

                  @htilburgs the sendSocketNotification(xyz,foo) ------->socketNotificationReceived(notification, payload)
                  in both directions…

                  only one received on each side… if there are multiple, then the last one seen is used.

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  htilburgsH 1 Reply Last reply Reply Quote 0
                  • htilburgsH Offline
                    htilburgs @sdetweil
                    last edited by

                    @sdetweil Thnx, you helped me a lot!!

                    (still trying to learn JS, but not afraid to ask) ☺

                    1 Reply Last reply Reply Quote 0
                    • M Offline
                      MMRIZE
                      last edited by

                      This is not a direct answer to the question, but just for your reference.
                      The fetch API can be executed directly in the browser, not just in a Node.js environment. You can call the fetch API directly in MMM-MyModule.js without needing node_helper.js.

                      Additionally, since the fetch API works asynchronously, you can manage the responses of two fetch calls as follows:

                      • If you need to use the response that arrives first, use Promise.race() or Promise.any().
                      • If you need to wait for both responses to arrive before proceeding, use Promise.all().

                      Using these methods can help you manage data synchronization more efficiently.

                      S htilburgsH 2 Replies Last reply Reply Quote 1
                      • S Offline
                        sdetweil @MMRIZE
                        last edited by

                        @MMRIZE quite true, since fetch is now part of electron.

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        1 Reply Last reply Reply Quote 0
                        • htilburgsH Offline
                          htilburgs @MMRIZE
                          last edited by

                          @MMRIZE Thanks for your reaction.

                          (still trying to learn JS, but not afraid to ask) ☺

                          1 Reply Last reply Reply Quote 0
                          • S sdetweil has marked this topic as solved on
                          • 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