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.

    Auto-disable module

    Scheduled Pinned Locked Moved Development
    12 Posts 4 Posters 7.5k 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.
    • yo-lessY Offline
      yo-less Module Developer
      last edited by

      Hey everyone, I’m currently developing a local transport module and I would love for the module to auto-disable itself at night when there are no tram connections, is there any way to tell a module to disable itself when a certain condition is met? Would love to hear your ideas :)

      MichMichM 1 Reply Last reply Reply Quote 0
      • yo-lessY Offline
        yo-less Module Developer
        last edited by

        Alright, I’ve just figured it out, whenever the API returns an empty connections array, I simply don’t start generating a table via getDom, works like a charm :)

        1 Reply Last reply Reply Quote 0
        • KirAsh4K Offline
          KirAsh4 Moderator
          last edited by

          You can also set a timer in it that calls .hide() or .show() at set times during a 24 hour period. This is cleaner as you won’t know if the API will always return an empty array when there are no trains running.

          A Life? Cool! Where can I download one of those from?

          1 Reply Last reply Reply Quote 0
          • yo-lessY Offline
            yo-less Module Developer
            last edited by

            Nice solution, thanks man!

            1 Reply Last reply Reply Quote 0
            • yo-lessY Offline
              yo-less Module Developer
              last edited by

              Could you point me in the right direction as to how I could hide a complete module with a .hide() call?

              1 Reply Last reply Reply Quote 0
              • R Offline
                ronny3050 Module Developer
                last edited by ronny3050

                First send a notification from your main module.js to node_helper.js to ask it to look for the time of day.
                Something like in your main module.js:

                start: function(){
                        this.sendSocketNotification('LookForTime');
                }
                

                In your node_helper.js, you could check for the time. Once you see that it is night, you can send a socket notification to your main module.js file like:

                socketNotificationReceived: function(notification, payload){
                         if(notification === 'LookForTime'){
                                  // Keep checking for time. Once night,
                                 if(night){
                                            this.sendSocketNotification('NIGHT');
                                 }
                       }
                }
                

                In your main module.js:

                socketNotificationReceived: function(notification, payload){
                      if(notification === 'NIGHT'){
                            this.hide();
                      }
                }
                
                yo-lessY 1 Reply Last reply Reply Quote 0
                • yo-lessY Offline
                  yo-less Module Developer @ronny3050
                  last edited by

                  @ronny3050 Impressive response, thank you!

                  R 1 Reply Last reply Reply Quote 1
                  • R Offline
                    ronny3050 Module Developer @yo-less
                    last edited by

                    @yo-less No problem! If you have any issues, I’ll be glad to answer them on chat :)

                    1 Reply Last reply Reply Quote 0
                    • KirAsh4K Offline
                      KirAsh4 Moderator
                      last edited by

                      No need to use node helper for that. All modules can use 'moment.js' as it’s already in the code (for the time display, calendar, and various timers.) So write your code in the main module’s js file.

                      moment.now() gives you the current time.

                      Take a gander at my calendar_monthly module where I keep track of time for the refreshes. The calendar DOM only refreshes once a day, at midnight. However, because browsers tend to get “stuck” every so often during the day, I also keep track of the hours. What that means is, there is a “heartbeat” every hour to stay accurate to the actual time. If the browser got stuck and the clock delayed by a few seconds, the heartbeat will fix that every 60 minutes. Then, only when it reaches midnight, will it actually reload the DOM.

                      In the code, look at the 'start' function where I set the time till next midnight. Then I fire off a scheduleUpdate() which sets a delay to the next hour. When that timer expires, a couple of things happen:

                      1. it checks whether the current time is past the midnight set earlier. If so,
                      • it will reload the DOM (which refreshes the calendar display),
                      • it will calculate the next midnight
                      • it will reset the timer to the next hour
                      • it will loop back to scheduleUpdate()
                        OR
                      1. if the current time is LESS than midnight,
                      • it will reset the timer to the next hour
                      • it will loop back to scheduleUpdate()
                        … and the DOM never gets touched.

                      This way I don’t reload the calendar unnecessarily throughout the day. It only happens once, but the timer fires off every hour, just to keep track of time. If I didn’t do that, at least on my rpi, I noticed the browser’s time will drift by several minutes in a 24 hour period.

                      A Life? Cool! Where can I download one of those from?

                      R 1 Reply Last reply Reply Quote 0
                      • R Offline
                        ronny3050 Module Developer @KirAsh4
                        last edited by

                        @KirAsh4’s solution is much neater and elegant. He never ceases to amaze me! :D

                        1 Reply Last reply 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