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

Use .JSON as a module config value?

Scheduled Pinned Locked Moved Solved Troubleshooting
6 Posts 2 Posters 386 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.
  • D Offline
    drippyer
    last edited by Apr 2, 2022, 4:01 AM

    Hey there!

    I am trying to automate a super small part of my MagicMirror which involves the MMM-PlexNowPlaying module.

    Basically, I’m trying to take a JSON file from the filesystem, read it to a JS Object, then use that Object as the value for the userNameFilter key within the config Object.


    Code

    This is the code I’ve made and added to my config.js to avoid writing an entire module lol:

    if (typeof module !== "undefined") {
        var plexUsers = require('/home/pi/mmConfig/user_list.json');
    
            let obj = config.modules.find((o, i) => {
                    if (o.module === "MMM-PlexNowPlaying") {
                            config.modules[i].config.userNameFilter = plexUsers;
                            return true; // stop searching
                    }
            });
    
            let test = config.modules.find(o => o.module === "MMM-PlexNowPlaying");
            console.log(test);
    }
    

    It’s located between var config = {}; and
    if (typeof module !== "undefined") {module.exports = config;}


    Confusion

    The two scenarios I have compared against each other are

    1. running the code above without
    2. commenting out let obj = {}; and hardcoding the Object (the normal way)

    And in both cases the output of test is the exact same. Yet the behavior of the Mirror doesn’t seem to reflect that. The standard usage of 2 works as expected, yet 1 does

    Any and all help would be greatly appreciated!

    S 1 Reply Last reply Apr 2, 2022, 11:55 AM Reply Quote 0
    • D Offline
      drippyer @sdetweil
      last edited by drippyer Apr 8, 2022, 7:10 AM Apr 8, 2022, 6:49 AM

      @sdetweil Thanks for the info!

      Ok, I see. That could definitely work.

      I went ahead and scrapped the code from above and converted it to a bash script that is executed before the cd ./MagicMirror (pretty sure I’ve had this setup running since before installers/mm.sh was actually provided lol). I admit the bash script is suuuper rough and was written with my symlinked config files in mind but it gets the job done!

      1. makes an intermediate copy of config.js
      2. grabs the content of the of the json
      3. declares variable containing config variable to be replaced
      4. prepends the variable string to the json content
      5. uses perl/regex to search for the variable in the intermediate file and replace it
      6. makes a backup of config.js just in case
      7. moves intermediate copy to working version
      #!/bin/bash
      
      cp /home/pi/mmConfig/config.js /home/pi/mmConfig/config.inter
      
      mapfile < /home/pi/mmConfig/user_list.json
      
      variableName="userNameFilter"
      
      replace="${variableName}: ${MAPFILE[@]}"
      
      perl -0777 -pi -e "s/${variableName}: {((.|\n)*?)}/${replace}/g" /home/pi/mmConfig/config.inter
      
      mv --backup=numbered /home/pi/mmConfig/config.js /home/pi/mmConfig/config_backups
      
      mv /home/pi/mmConfig/config.inter /home/pi/mmConfig/config.js
      

      And that successfully allows for the data to be read in! Thank you!

      Sadly, it did break pm2 watching config (which kinda makes sense to avoid a loop) and if I’m modifying the config I’m already in the command line and a pm2 restart mm isn’t too bad I suppose ¯\_(ツ)_/¯

      Edit: I’m realizing I should add a check to make sure it doesn’t perform either mv if the file is the same. New to-do lol!

      S 1 Reply Last reply Apr 8, 2022, 11:16 AM Reply Quote 0
      • S Offline
        sdetweil @drippyer
        last edited by Apr 2, 2022, 11:55 AM

        @drippyer I don’t know about putting the code IN config.js,but you can add another step to the script that starts mm and create the updated config.js

        installers/mm.sh is the normal start script

        it does

        #!/bin/bash
        # This file is still here to keep PM2 working on older installations.
        cd ~/MagicMirror
        # add your script here
        node xxx.js
        DISPLAY=:0 npm start
        

        make your original config.js config.model
        and merge your user info to get config.js

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        D 1 Reply Last reply Apr 8, 2022, 6:49 AM Reply Quote 1
        • D Offline
          drippyer @sdetweil
          last edited by drippyer Apr 8, 2022, 7:10 AM Apr 8, 2022, 6:49 AM

          @sdetweil Thanks for the info!

          Ok, I see. That could definitely work.

          I went ahead and scrapped the code from above and converted it to a bash script that is executed before the cd ./MagicMirror (pretty sure I’ve had this setup running since before installers/mm.sh was actually provided lol). I admit the bash script is suuuper rough and was written with my symlinked config files in mind but it gets the job done!

          1. makes an intermediate copy of config.js
          2. grabs the content of the of the json
          3. declares variable containing config variable to be replaced
          4. prepends the variable string to the json content
          5. uses perl/regex to search for the variable in the intermediate file and replace it
          6. makes a backup of config.js just in case
          7. moves intermediate copy to working version
          #!/bin/bash
          
          cp /home/pi/mmConfig/config.js /home/pi/mmConfig/config.inter
          
          mapfile < /home/pi/mmConfig/user_list.json
          
          variableName="userNameFilter"
          
          replace="${variableName}: ${MAPFILE[@]}"
          
          perl -0777 -pi -e "s/${variableName}: {((.|\n)*?)}/${replace}/g" /home/pi/mmConfig/config.inter
          
          mv --backup=numbered /home/pi/mmConfig/config.js /home/pi/mmConfig/config_backups
          
          mv /home/pi/mmConfig/config.inter /home/pi/mmConfig/config.js
          

          And that successfully allows for the data to be read in! Thank you!

          Sadly, it did break pm2 watching config (which kinda makes sense to avoid a loop) and if I’m modifying the config I’m already in the command line and a pm2 restart mm isn’t too bad I suppose ¯\_(ツ)_/¯

          Edit: I’m realizing I should add a check to make sure it doesn’t perform either mv if the file is the same. New to-do lol!

          S 1 Reply Last reply Apr 8, 2022, 11:16 AM Reply Quote 0
          • S Offline
            sdetweil @drippyer
            last edited by Apr 8, 2022, 11:16 AM

            @drippyer you could change the pm2 setup to not watch config.js
            …

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            D 1 Reply Last reply Apr 8, 2022, 2:42 PM Reply Quote 0
            • D Offline
              drippyer @sdetweil
              last edited by Apr 8, 2022, 2:42 PM

              @sdetweil I might have been a bit unclear haha. The break was pm2 no longer watching config.js, not a loop. My bad about that misunderstanding.

              S 1 Reply Last reply Apr 8, 2022, 2:42 PM Reply Quote 0
              • S Offline
                sdetweil @drippyer
                last edited by Apr 8, 2022, 2:42 PM

                @drippyer got it…

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/6
                  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