• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

MMM-MealViewer

Scheduled Pinned Locked Moved Development
48 Posts 6 Posters 26.1k Views 6 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.
  • G Offline
    gonzonia
    last edited by Nov 12, 2019, 12:16 PM

    The module works great on the initial load. The problem is that it doesn’t seem to be updating. After 12pm it’s supposed to start showing the next days meals but doesn’t. Even the next day it doesn’t update. I’ll try to dig into the core aspects of developing a module and see if I can find out why, but if anyone with more experience has any ideas where I should start looking I’d appreciate it.

    1 Reply Last reply Reply Quote 0
    • G Offline
      gonzonia
      last edited by Nov 12, 2019, 10:37 PM

      Okay. I’ve identified the problem. In the code below, everything gets setup in the “start” function. Then it calls getMenuData. That function ends by setting up a timer to call itself but isn’t supplying the object necessary. This means it won’t work. If I add the _this object to pass it causes all sorts of problems.
      I’m not a js expert. I’m sure a way to re-factor this will eventually occur to me, but right now I’m just not seeing it.

      start:  function() {
          Log.log('Starting module: ' + this.name);
      
          // Set up the local values
          var today = moment();
          this.loaded = false;
          this.urls = [];
          this.results = [];
      
          // Uses now are today if before noon and tomorrow as today if after noon
          if (today.hour() >= 12) {
            var todayFormatted = today.add(1, 'day').format('MM-DD-YYYY');
          }
          else {
            var todayFormatted = today.format('MM-DD-YYYY');
          }
      
          // Currently set to only pull one day's data so endDayFormatted = todayFormatted
          //var endDay = today.add(config.showDays, 'days');
          //var endDayFormatted = endDay.format('MM-DD-YYYY');
          var endDayFormatted = todayFormatted;
      
          // Construct the url array for the schools
          for (var i in this.config.schools) {
      			this.urls.push({school: this.config.schools[i], url: 'https://api.mealviewer.com/api/v4/school/' + this.config.schools[i] + '/' + todayFormatted + '/' + endDayFormatted + '/'});
          }
      
          // Trigger the first request
          this.getMenuData(this);
        },
      
        getMenuData: function(_this) {
          // Make the initial request to the helper then set up the timer to perform the updates
          _this.sendSocketNotification('GET-MENU-DATA', _this.urls);
          setTimeout(_this.getMenuData(), _this.config.interval, _this);
        },
      
      
      
      S 1 Reply Last reply Nov 13, 2019, 12:01 AM Reply Quote 0
      • S Do not disturb
        sdetweil @gonzonia
        last edited by Nov 13, 2019, 12:01 AM

        @gonzonia said in MMM-MealViewer:

        getMenuData: function(_this) {
        // Make the initial request to the helper then set up the timer to perform the updates
        _this.sendSocketNotification(‘GET-MENU-DATA’, _this.urls);
        setTimeout(_this.getMenuData(), _this.config.interval, _this);
        },

        do this

         getMenuData: function(_this) {
            // Make the initial request to the helper then set up the timer to perform the updates
            _this.sendSocketNotification('GET-MENU-DATA', _this.urls);
            setTimeout(function() {_this.getMenuData(_this)}, _this.config.interval);
          },
        

        this creates a little inline function which CAN pass parms on to the next routine

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        G 1 Reply Last reply Nov 13, 2019, 1:19 AM Reply Quote 0
        • G Offline
          gonzonia @sdetweil
          last edited by Nov 13, 2019, 1:19 AM

          @sdetweil Thanks! I’ll make the change and see what happens when it should update mid-day tomorrow.

          1 Reply Last reply Reply Quote 0
          • C Offline
            cowboysdude Module Developer
            last edited by cowboysdude Nov 13, 2019, 2:34 AM Nov 13, 2019, 2:33 AM

            Or you could do this:

            scheduleUpdate: function() {
                   setInterval(() => {
                       this.getMenuData();
                   }, this.config.updateInterval); 
               },
                getMenuData: function() {
                   this.sendSocketNotification('GET-MENU-DATA');
               }
            

            At the top of the main js you set the interval:

            updateInterval: 15 * 60 * 1000,
            

            Then in the start section you call it:

            // Define start sequence.
               start: function() {
                   Log.info("Starting module: " + this.name); 
                   this.scheduleUpdate();
               },
            

            Either the way Sam showed you or the above will work :)

            G 1 Reply Last reply Nov 13, 2019, 7:40 PM Reply Quote 0
            • G Offline
              gonzonia @cowboysdude
              last edited by gonzonia Nov 13, 2019, 7:41 PM Nov 13, 2019, 7:40 PM

              @cowboysdude Thanks! I might switch to that. I’ve had to rework it a little because the URL wasn’t getting set except at the first call. I just moved setup for that into the getMenuData and I think it’s all working now. I’ll give it a day to run and see what happens tomorrow before I try messing with it again and update the file on github.

              -updated to fix grammar

              1 Reply Last reply Reply Quote 1
              • C Offline
                cowboysdude Module Developer
                last edited by cowboysdude Nov 14, 2019, 2:43 AM Nov 14, 2019, 2:42 AM

                Sam is a REALLY smart guy so he gives you the smart guy answer…
                I’m not as smart so I give you my dumb guy answer LOL

                It will work either way … so now you def have an answer that works :)

                S 1 Reply Last reply Nov 14, 2019, 3:06 AM Reply Quote 1
                • S Do not disturb
                  sdetweil @cowboysdude
                  last edited by Nov 14, 2019, 3:06 AM

                  @cowboysdude said in MMM-MealViewer:

                  Sam is a REALLY smart guy so he gives you the smart guy answer

                  nah, Sam is the LAZY guy… change as LITTLE as possible. especially for folks just starting out

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 1
                  • G Offline
                    gonzonia
                    last edited by Nov 14, 2019, 9:29 PM

                    Well, thanks to both of you! I can confirm it is finally working.

                    Now to update github. Someday I should write some documentation too.

                    1 Reply Last reply Reply Quote 0
                    • htilburgsH Offline
                      htilburgs
                      last edited by Dec 13, 2019, 5:30 PM

                      @cowboysdude, I’m looking for the part where you “change” the XML data to JSON data. I’m trying to write a new MMM, but the data is presented in XML. I like the have it in JSON, so I can use it.

                      Maybe I read over it, but I cannot figure out where you do this.

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

                      S 1 Reply Last reply Dec 13, 2019, 5:37 PM Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 4
                      • 5
                      • 3 / 5
                      • 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