• 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.

Canteen-module // Mensa-Modul

Scheduled Pinned Locked Moved Solved Requests
15 Posts 4 Posters 4.7k 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.
  • K Offline
    k-0
    last edited by k-0 Nov 18, 2019, 9:28 AM Nov 18, 2019, 8:40 AM

    Here is my working code (console-output works…).
    The outcommented code is for the second canteen and has no relevance.

    
    setTimeout(function() {
    var request = require('request');
    var heute;
    if ((new Date().toISOString().substr(11,2) < 16)) {
    	 heute = new Date().toISOString().substr(0,10);
    }
    else
    {
    	 heute = new Date();
    	 heute.setDate(heute.getDate()+1);
    	 heute= heute.toISOString().substr(0,10);
    
    }
    var cnt = 0;
    request({
      url: 'https://openmensa.org/api/v2/canteens/838/days/'+heute+'/meals',
      json: true
    }, function(error, response, body) {
    	 console.log('\nCAFETERIA EAH am ' +  heute.substring(8,10)+ '.'+heute.substring(5,7) +'.'+heute.substring(0,4)+':');
    	if (body.length < 1){
    		console.log('Heute geschlossen!')
    	}
    	else {
    	  while (body.length > cnt) {
    		console.log(body[cnt].name);
    		console.log(body[cnt++].prices.employees.toFixed(2) +'€');
    		}				
    	}
    });
    //var cntr = 0;
    //request({
    //  url: 'https://openmensa.org/api/v2/canteens/76/days/'+heute+'/meals',
    //  json: true
    //}, function(error, response, body) {
    //	 	if (body.length < 1){
    //		console.log('Heute geschlossen!')
    //	}
    //	else {
    //	 console.log('\nMENSA ZEISS am ' +  heute.substring(8,10)+ '.'+heute.substring(5,7) +'.'+heute.substring(0,4)+':');
    //	  while (body.length > cntr) {
    //		console.log(body[cntr].name);
    //		console.log(body[cntr++].prices.employees.toFixed(2)+ ' €');
    //	}}
    //});
    }, 6000);
    

    The output is:

    Z:>node curl.js

    CAFETERIA EAH am 18.11.2019:
    Backleberkäse mit Paprikasoße, Balkangemüse und 1 Beilage
    4.00€
    Seelachsfilet mit Remouladensoße, Pommes frites und Salat
    4.60€
    Orientalische Linsen-Kürbis-Birnen-Suppe mit Brötchen
    4.00€

    L 1 Reply Last reply Nov 18, 2019, 1:37 PM Reply Quote 0
    • R Offline
      retroflex Project Sponsor Module Developer
      last edited by Nov 18, 2019, 1:27 PM

      Where do you put this code? You have to put require in node_helper.js, not the module js file.
      Also check the log files in the developer tools in e.g. Chrome.

      1 Reply Last reply Reply Quote 0
      • L Offline
        lavolp3 Module Developer @k-0
        last edited by lavolp3 Nov 18, 2019, 1:38 PM Nov 18, 2019, 1:37 PM

        @k-0
        I like the module idea very much. Could help some people around here.

        So your problem is handling the asynchrous call? Is that right?
        I think, the node_helper function is ideal to help with this (at least for a beginner).
        What I would do, as someone who has his own problems with asynchronous calls:

        start function:

            start: function() {
                Log.info("Starting module: " + this.name);
                // Set locale.
                this.sendSocketNotification("CONFIG", this.config);
                var self = this;
                setInterval(() => {
                  self.updateDom();
                }, self.config.updateInterval);
            },
        

        in node_helper.js:

        socketNotificationReceived: function(notification, payload) {
            if (notification === 'CONFIG') {
                this.config = payload;
                this.collectData();
                self = this;
                setInterval(function () {
                    self.collectData();
                },  10 * 60 * 1000);
            }
          },
        
          collectData: function () {
            request(url, function (error, response, body) {
              .....
              self.sendSocketNotification("DATA", response);
            });
          }
        
        });
        

        You basically have two separate intervals: The “async call” interval in node_helper and the dom update interval in your main js.
        After the first asynchronous call you have always some data to show in the dom update interval. This data gets overwritten whenever node_helper sends a socketNotification.

        Have a look at my rather short module using an asynchronous call:
        https://github.com/lavolp3/MMM-Celebrations

        If you upload your module on github we can have a better look at the code and make suggestions.

        Hope it helps.

        How to troubleshoot modules
        MMM-soccer v2, MMM-AVStock

        1 Reply Last reply Reply Quote 0
        • K Offline
          k-0
          last edited by Nov 19, 2019, 4:22 PM

          I couldn’t find my old code (node_helper.js and module.js files) from my first attempts, but i created new files and merged the code from @lavolp3.
          Here is the github url: https://github.com/k-0/MMM-Canteen

          Yes, the main problem was handling the asynchrous call. But i’m pretty sure, that there are more problems. :P

          S L 2 Replies Last reply Nov 19, 2019, 4:38 PM Reply Quote 0
          • S Online
            sdetweil @k-0
            last edited by Nov 19, 2019, 4:38 PM

            @k-0 said in Canteen-module // Mensa-Modul:

            the main problem was handling the asynchrous call.

            right… you should NOT do this IN the getDom() function…

            the timer should fire off this routine… and once the request() has returned, save the data in some variable,
            and THEN call updateDom(), which will cause getDom() to be called to format the data

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • L Offline
              lavolp3 Module Developer @k-0
              last edited by Nov 22, 2019, 1:46 PM

              @k-0 Hi k-0.
              So, I liked the idea.
              So sat down and, well, basically wrote a new module :-)
              I’ve sent a PR, let me know your comments.

              There’s still lots to do especially the readme :-)

              And lots of things to configure.
              I can’t use the module, my studies are 10 years back, but I think there may be some people who can benefit from it so I’m interested in bringing this forward.

              Ideas:

              • option to show the whole week
              • option to filter meals (leave out vegetarian food, LOL)

              Cheers,
              Dirk

              How to troubleshoot modules
              MMM-soccer v2, MMM-AVStock

              L 1 Reply Last reply Nov 22, 2019, 1:52 PM Reply Quote 0
              • L Offline
                lavolp3 Module Developer @lavolp3
                last edited by Nov 22, 2019, 1:52 PM

                0_1574430694203_12b674f2-ae5a-43e7-b136-9fcdf761dda5-image.png

                Doesn’t look too shabby but lots of styling to be done I guess

                How to troubleshoot modules
                MMM-soccer v2, MMM-AVStock

                1 Reply Last reply Reply Quote 0
                • K Offline
                  k-0
                  last edited by k-0 Dec 2, 2019, 2:39 PM Dec 2, 2019, 2:35 PM

                  Hi Dirk,

                  I could test it today and I really love it! Thank you so much!

                  0_1575297143322_mmm.PNG

                  I already updated the ReadMe and added the “€” to the price in the .njk-file.

                  It would be nice to have the date in the header on which the meal refers. :P

                  Cheers,
                  Kevin

                  EDIT: I think that a option to show the whole week is nice, especially for ppl who have only one or two meals per day. Otherwise it will be to much informations to show.

                  L 1 Reply Last reply Dec 2, 2019, 7:41 PM Reply Quote 0
                  • L Offline
                    lavolp3 Module Developer @k-0
                    last edited by Dec 2, 2019, 7:41 PM

                    @k-0
                    The “header” from the config is nothing but a html element.
                    You could leave the header column out of the module definition and include it separately in your .njk.
                    Then you would need it to be given a name by the user (via the config) since it is not thrown out by the API.

                    Then just put a

                    tag above the rest and include the canteen name and a date.

                    < header >{{ config.canteenName }}, {{ moment().format("dd, DD.MM.) }} < /header >
                    

                    How to troubleshoot modules
                    MMM-soccer v2, MMM-AVStock

                    1 Reply Last reply Reply Quote 0
                    • K Offline
                      k-0
                      last edited by Dec 20, 2019, 10:49 AM

                      Hi @lavolp3 ,

                      i added

                      <header>{{ config.canteenName }}, {{ moment().format("dddd") }}</header>
                      

                      to my MMM-Canteen.njk. But I always get “undefined”.

                      It works with

                      {{ config.canteenName }}
                      

                      It seems there is a problem with moment() in the header. Do you have an idea?

                      L 1 Reply Last reply Dec 23, 2019, 10:02 PM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      • 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