• 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 dates from file and do a compare

Scheduled Pinned Locked Moved Development
12 Posts 2 Posters 2.1k Views 2 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.
  • B Offline
    BravoOscar
    last edited by Feb 1, 2021, 12:26 PM

    Hi,
    I need some help with a module I’m trying do develop. I need to read data from a *.json and compare the data with current date. If one or more of data are equal to current date, it will show a picture otherwise it doesn’t.
    I think the date must be stored in a *.json file and read with NODE_HELPER.JS. But I can’t get it to work.

    Example. Of *.JSON file:

    {
    Case1 [
    “21.02.2021”,
    “25.02.2021”,
    “28.02.2021”
    ]
    Case2 [
    “12.03.2021”,
    “23.03.2021”,
    “29.04.2021”
    ]
    }
    

    Or another example.

    {
    “case1” , “21.02.2021”,
    “case1” , “25.02.2021”,
    “case1” , “28.02.2021”,
    “case2” , “12.03.2021”,
    “case2” , “23.03.2021”,
    “case2” , “29.04.2021
    ]
    }
    

    I have tried to do a read command, but I’m not able to see if it can read the file correct or even find the file that I’m trying to read in.

    So can someone help me with a working example of reading from a file and do the compare?

    S 1 Reply Last reply Feb 1, 2021, 3:01 PM Reply Quote 0
    • S Offline
      sdetweil @BravoOscar
      last edited by sdetweil Feb 2, 2021, 12:04 AM Feb 1, 2021, 3:01 PM

      @bravooscar
      in node_helper.js

      var NodeHelper = require("node_helper");
      const fs = require('fs')
      const path = require('path')
      // add require of other javascripot components here
      // var xxx = require('yyy') here
      
      module.exports = NodeHelper.create({
      
      
      	// handle messages from our module// each notification indicates a different messages
      	// payload is a data structure that is different per message.. up to you to design this
      	socketNotificationReceived(notification, payload) {
      		console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
      		// if config message from module
      		if (notification === "CONFIG") {
      			// save payload config info
      			this.config=payload
      			let p = path.resolve('.','modules',this.name,this.config.filename)
      			console.log("file="+p)
      			fs.readFile(p, (error,filedata)=> {
      			console.log("filedata="+filedata)
      			let data =JSON.parse(filedata)
      			this.sendSocketNotification('filedata', data)
      			})
      		}
      	},
      });
      

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      B 1 Reply Last reply Feb 1, 2021, 10:07 PM Reply Quote 2
      • B Offline
        BravoOscar @sdetweil
        last edited by Feb 1, 2021, 10:07 PM

        @sdetweil

        What do you meen by " need some trigger function"? is that in the node_helper.js or where should it be?

        modulename.js sends node_helper a socket notification w the module config info
        node_helper sends the data back as object

        Should I write some code in the modulename.js that call file read or do you mean that this is the place to make the compare?

        S 2 Replies Last reply Feb 1, 2021, 10:10 PM Reply Quote 0
        • S Offline
          sdetweil @BravoOscar
          last edited by Feb 1, 2021, 10:10 PM

          @bravooscar compare back in modulename.js, it needs to decide.

          trigger
          just say nothing happens in node helper without some event

          this should get the file contents and send to modulename.

          modulename.js sends socketNotification to helper w config info to start the process

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @BravoOscar
            last edited by Feb 2, 2021, 12:05 AM

            @bravooscar

            full node_helper.js above, tested, working
            need to get path to file in module folder (not current folder)

            sends object to module to make decisions on what to display

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            B 1 Reply Last reply Feb 2, 2021, 10:27 PM Reply Quote 0
            • B Offline
              BravoOscar @sdetweil
              last edited by Feb 2, 2021, 10:27 PM

              @sdetweil

              It now works, I can get data into my new object.

              My new json file look like this:

              {
                "RecyclingDates": [
              { "Type": "House", "Date": "02.01.2021"},
              { "Type": "House", "Date": "08.01.2021"},
              { "Type": "House", "Date": "15.01.2021"},
              { "Type": "Glass", "Date": "22.01.2021"},
              { "Type": "Glass", "Date": "29.01.2021"},
              { "Type": "Glass", "Date": "05.02.2021"},
              { "Type": "Garden", "Date": "12.02.2021"},
              { "Type": "Garden", "Date": "19.02.2021"},
              { "Type": "Garden", "Date": "26.02.2021"},
              { "Type": "Garden", "Date": "05.03.2021"},
              { "Type": "Big", "Date": "12.03.2021"},
              { "Type": "Big", "Date": "19.03.2021"},
              { "Type": "Big", "Date": "26.03.2021"}
              ]
              }
              

              I get this object output:

              Object
              RecyclingDates: Array(13)
              0:
              Type: "House"
              Date: "02.01.2021"
              __proto__: Object
              1:
              Type: "House"
              Date: "08.01.2021"
              __proto__: Object
              2: {Type: "House", Date: "15.01.2021"}
              3: {Type: "Glass", Date: "22.01.2021"}
              4: {Type: "Glass", Date: "29.01.2021"}
              5: {Type: "Glass", Date: "05.02.2021"}
              6: {Type: "Garden", Date: "12.02.2021"}
              7: {Type: "Garden", Date: "19.02.2021"}
              8: {Type: "Garden", Date: "26.02.2021"}
              9: {Type: "Garden", Date: "05.03.2021"}
              10: {Type: "Big", Date: "12.03.2021"}
              11: {Type: "Big", Date: "19.03.2021"}
              12: {Type: "Big", Date: "26.03.2021"}
              length: 13
              

              I opend “0” and “1” in the object, so you can see how its stored in the object.

              How do I access these values in my module.js file. I hope to make a loop/compare of the array.
              My plan is to show different pictures according to the Type and if the Date is equale to current date.

              S 1 Reply Last reply Feb 2, 2021, 10:38 PM Reply Quote 0
              • S Offline
                sdetweil @BravoOscar
                last edited by sdetweil Feb 3, 2021, 2:05 AM Feb 2, 2021, 10:38 PM

                @bravooscar

                for( let entry of object.RecylingDates){
                  if (entry.Type ==='House'){
                      if(entry.Date==='........'){
                      }
                   }
                }
                

                you can use moment().format(???)
                where ??? is the layout of yiur date strings to get something for today, tomorrow…

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                B 1 Reply Last reply Feb 3, 2021, 10:24 PM Reply Quote 0
                • B Offline
                  BravoOscar @sdetweil
                  last edited by Feb 3, 2021, 10:24 PM

                  @sdetweil

                  I have this now, and this is working.

                  for(let type of Object.keys(obj)){
                  var date = obj[type];
                  console.log(type, date);
                  }
                  

                  This will show the correct type and date.
                  But I have something in mind, that look like this:

                  if date === currentdate {
                  picture=type+"Div"}
                  
                  ex:  
                  20.02.2021 === 20.02.2021 then var piture= "bigDiv"
                  

                  picture is then displayed.

                  but this have to be a loop, because there can be same dates but with different types(pictures)

                  Does this gives any meaning? its hard to descripe, but I’m realy glad for all the help I get.

                  S 1 Reply Last reply Feb 3, 2021, 10:56 PM Reply Quote 0
                  • S Offline
                    sdetweil @BravoOscar
                    last edited by Feb 3, 2021, 10:56 PM

                    @bravooscar as i said u can use the js library moment().format(‘dd.mm.yyyy’) to get today in string to compare

                    see the online doc for exact syntax

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    B 1 Reply Last reply Feb 4, 2021, 6:18 AM Reply Quote 0
                    • B Offline
                      BravoOscar @sdetweil
                      last edited by Feb 4, 2021, 6:18 AM

                      @sdetweil
                      That I know. I allready have the actual date and the date from the array,
                      Im unsure of how to get all the dates and all the types from the array, so if there are more of the same dates, then there are more types taht has to be displayed.

                      S 1 Reply Last reply Feb 4, 2021, 8:32 AM Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        3/12
                        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