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

read csv-data and put it in an array

Scheduled Pinned Locked Moved Utilities
313 Posts 3 Posters 358.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.
  • S Offline
    sdetweil @sdetweil
    last edited by sdetweil May 2, 2020, 1:44 PM May 2, 2020, 12:11 PM

    @perl here is the updated perlchamp.js, shows date, persons name and age (no formatting)

    Module.register("perlchamp", {
    
    	defaults: {
    		language: "de",
    		dimmEntries: true,   // true: dims entries and the associated 
    							//       symbol when the date has expired.
    							// false: delete entries and the associated 
    							//        symbol when the date has expired.
                     // used if look forward is desired, add to module config  in config.js
                     days_forward:0,
    	},
    	// place to save birthdays to display
        active_birthdays : {},
    
    
    	init: function(){
    		Log.log(this.name + " is in init!");
    	},
    
    	start: function(){
    		Log.log("Starting module: " + this.name);
    
    		// Set locale
    		moment.locale(config.language);
    	},
    
    	loaded: function(callback) {
    		Log.log(this.name + " is loaded!");
    		callback();
    	},
    
    	// return list of other functional scripts to use, if any (like require in node_helper)
    	getScripts: function() {
    		return	["moment.js"];
    	}, 
    
    	// return list of stylesheet files to use if any
    	getStyles: function() {
    		return 	[this.data.path + "/css/perlchamp.css"];
    	},
    
    /*
    	// return list of translation files to use, if any
    	getTranslations: function() {
    		return {
    			// sample of list of files to specify here, if no files, do not use this routine, , or return empty list
    
    			// en: "translations/en.json",  (folders and filenames in your module folder)
    			// de: "translations/de.json"
    		}
    	},
    */
    
    
    	// only called if the module header was configured in module config in config.js
    	getHeader: function() {
    		return this.data.header;
    	},
    
    	// messages received from other modules and the system (NOT from your node helper)
    	// payload is a notification dependent data structure
    	notificationReceived: function(notification, payload, sender) {
    		// once everybody is loaded up
    		if(notification==="ALL_MODULES_STARTED"){
    			// send our config to our node_helper
    			this.sendSocketNotification("CONFIG",this.config);
    		}
    		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);
    		}
    	},
    
    	
    	// messages received from from your node helper (NOT other modules or the system)
    	// payload is a notification dependent data structure, up to you to design between module and node_helper
    	socketNotificationReceived: function(notification, payload) {
    /*
    		Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
    		if(notification === "message_from_helper"){
    			this.config.message = payload;
    			// tell mirror runtime that our data has changed,
    			// we will be called back at GetDom() to provide the updated content
    			this.updateDom(1000);
    		}
    */
    		if(notification === "have data"){
    
    			var self = this
    			Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
    			
    			var now = moment();
    
    			// clear the old list
                            this.active_birthdays={}
    
    			// only calculate in advance once
    			var birthday_time_window=moment().add(self.config.days_forward,'days')
    
    			// loop thru the birthdays  from the file
    			for( var birthday of payload) {
    				// get 1st 5 chars of birthdate, thru month
    				// we will use this as the key in the hash
    				var birth_date=birthday.birth.substring(0,5)
    
    				// get the birthday as a moment in this year, for comparing
    				var birth_date_moment = moment(birth_date+now.year(),"DD.MMYYYY")
    				  
    				// u can add days to a moment object and then compare if the birthdate is before that date (and after now)
    				// so within the next xx days
    
    			  // if the date is the same or later, don't use time of day
    			   if(birth_date_moment.startOf('day').isSameOrAfter(now.startOf('day')) &&
    			   	  birth_date_moment.startOf('day').isSameOrBefore(birthday_time_window.startOf('day'))
    			   		){
    
    			      // birthday is in this month
    			      // check the hash if we've seen anything for today yet
    			      // if we haven't see this date yet
    			      if(self.active_birthdays[birth_date] == undefined){
    			           //  create the holder for its info (array of info) in the hash
    			           self.active_birthdays[birth_date]=[]
    			      }
    			      // save the persons name and age on the list
    			      self.active_birthdays[birth_date].push({'name':birthday.name, 'age': now.diff(moment(birthday.birth,'DD.MM.YYYY'), 'years') })			     
    			   }
    			}
         
    			// tell MM to call and get our content
    			self.updateDom();
    		}     			
    			
    /*			
    			this.config.message = payload;
    			// tell mirror runtime that our data has changed,
    			// we will be called back at GetDom() to provide the updated content
    			this.updateDom(1000)
    			
    			// filters the birthdaylist and output only the birthdays
    			// of the current month, sorted (ascending) by days in 
    			// the following format: 
    			// [{ day: "10", name: ["firstname lastname",...] }]
    
    			// set variables
    			var today_month = moment().format("MM");
    			var today_day_month = moment().format("DD.MM");
    
    			var result = Object.entries(jsonObj
    			.filter(({birth}) => birth.split('.')[1] === today_month)
    			.reduce((a, {birth, name}) => {
    				const day = +birth.split('.')[0];
    				a[day] = [...(a[day] || []), name];
    				return a
    			}, {})
    			).map(([day, name]) => ({day, name})).sort((a, b) => +a.day - b.day);
    			birthdaysArray = result;
    			
    			// for debugging only
    			//console.log("filtered and sorted: ", birthdaysArray);
    */
    			
    	},
    
    	// system notification your module is being hidden
    	// typically you would stop doing UI updates (getDom/updateDom) if the module is hidden
    	suspend: function(){
    
    	},
    
    	// system notification your module is being unhidden/shown
    	// typically you would resume doing UI updates (getDom/updateDom) if the module is shown
    	resume: function(){
    
    	},
    
    	// this is the major worker of the module, it provides the displayable content for this module
    	getDom: function() {
    		var wrapper = document.createElement("div");
    
    // create your table and header here
    
    		for(var birthday of Object.keys(this.active_birthdays)){
    			for(var person of this.active_birthdays[birthday]){
    				var m=document.createElement('div')
    // create tyour table row here				
    				m.innerText=birthday+ ' '+ person.name+' age='+person.age
    				wrapper.appendChild(m)					
    			}
    		}
    
    // create your table footer here	
    
    		// pass the created content back to MM to add to DOM.
    		return wrapper;
    	}
    })
    

    this is the birthdays.csv I was using
    the column names are the field names in the jsonobj

    birth, name
    12.07.1952, Sam
    11.09.1944, Peg
    02.05.1939, Frank 
    09.04.1953, June
    09.04.1953, Jane
    30.12.1955, Joan
    02.05.1939, Sue
    05.05.1955, mary
    

    Sam

    How to add modules

    learning how to use browser developers window for css changes

    1 Reply Last reply Reply Quote 0
    • P Offline
      Perlchamp
      last edited by May 2, 2020, 3:31 PM

      hi sam :-)
      see my module-file. why i don’t get values for the Log.log(); statements ?

      	// this is the major worker of the module, it provides the displayable content for this module
      	getDom: function() {
      		var self = this;
      		var wrapper = document.createElement("div");
      		Log.log("this.active_birthdays >>>>> ", this.active_birthdays);
      
      		for(var birthday of Object.keys(this.active_birthdays)) {
      			for(var name of this.active_birthdays[birthday]) {
      				var m = document.createElement("div");
      				Log.log("BIRTHDAY >> " + birthday);
      				Log.log("NAME >> " + name);
      				m.innerText = birthday + " " + name;
      				wrapper.appendChild(m);
      			}
      		}
      
      		// pass the created content back to MM to add to DOM.
      		return wrapper;
      	}
      
      S 1 Reply Last reply May 2, 2020, 3:44 PM Reply Quote 0
      • S Offline
        sdetweil @Perlchamp
        last edited by sdetweil May 2, 2020, 3:48 PM May 2, 2020, 3:44 PM

        @Perlchamp said in read csv-data and put it in an array:

          for(var birthday of Object.keys(this.active_birthdays)) {
          	for(var name of this.active_birthdays[birthday]) {
        

        that means one of those two for loops didn’t execute, as the list is empty

        do this

         			// tell MM to call and get our content
        Log.log(JSON.stringify(this.active_birthdays))    // add this line
        			self.updateDom();                          // before this line in the notification code..
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • P Offline
          Perlchamp
          last edited by May 2, 2020, 3:48 PM

          and where i have to place it ?

          S 1 Reply Last reply May 2, 2020, 3:49 PM Reply Quote 0
          • S Offline
            sdetweil @Perlchamp
            last edited by May 2, 2020, 3:49 PM

            @Perlchamp see my updated post above

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • P Offline
              Perlchamp
              last edited by May 2, 2020, 3:52 PM

              i get this for each entry (here i only posted one of them):

              [2020-05-02 17:50:36.803] [LOG]    LOOK >>>>>>{} (/home/dirk/MagicMirror/modules/MMM-Logging/node_helper.js:34 Class.socketNotificationReceived)
              

              empty: {}

              S 1 Reply Last reply May 2, 2020, 3:53 PM Reply Quote 0
              • S Offline
                sdetweil @Perlchamp
                last edited by sdetweil May 2, 2020, 3:55 PM May 2, 2020, 3:53 PM

                @Perlchamp ok, so the data collection didn’t work…

                so I would step thru that code

                what is the 1st row of your csv file? the column headers

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0
                • P Offline
                  Perlchamp
                  last edited by May 2, 2020, 3:56 PM

                  before you do this. stop. i make an error. the code you posted is not in the notification section. it’s in the getDom section … so i placed it there. was this wrong ?

                  S 1 Reply Last reply May 2, 2020, 3:57 PM Reply Quote 0
                  • S Offline
                    sdetweil @Perlchamp
                    last edited by May 2, 2020, 3:57 PM

                    @Perlchamp there is NOT an this. updateDom() in the getDom() section…

                    still doesn’t change the result… there are no birthdays found that match 02/05

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      Perlchamp
                      last edited by May 2, 2020, 3:59 PM

                      the columns are:
                      birth, name

                      and is called “birthdays.csv”

                      the data are:
                      23.01.1987,Thomas Roth

                      S 1 Reply Last reply May 2, 2020, 4:00 PM Reply Quote 0
                      • 1
                      • 2
                      • 14
                      • 15
                      • 16
                      • 17
                      • 18
                      • 31
                      • 32
                      • 16 / 32
                      16 / 32
                      • First post
                        154/313
                        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