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

Auto-disable module

Scheduled Pinned Locked Moved Development
12 Posts 4 Posters 7.4k 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.
  • Y Offline
    yo-less Module Developer
    last edited by Aug 11, 2016, 12:09 AM

    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 :)

    M 1 Reply Last reply Aug 13, 2016, 12:32 PM Reply Quote 0
    • Y Offline
      yo-less Module Developer
      last edited by Aug 11, 2016, 12:15 AM

      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
      • K Offline
        KirAsh4 Moderator
        last edited by Aug 11, 2016, 1:11 AM

        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
        • Y Offline
          yo-less Module Developer
          last edited by Aug 11, 2016, 1:12 AM

          Nice solution, thanks man!

          1 Reply Last reply Reply Quote 0
          • Y Offline
            yo-less Module Developer
            last edited by Aug 11, 2016, 1:54 AM

            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 Aug 11, 2016, 3:14 AM Aug 11, 2016, 3:13 AM

              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();
                    }
              }
              
              Y 1 Reply Last reply Aug 11, 2016, 3:15 AM Reply Quote 0
              • Y Offline
                yo-less Module Developer @ronny3050
                last edited by Aug 11, 2016, 3:15 AM

                @ronny3050 Impressive response, thank you!

                R 1 Reply Last reply Aug 11, 2016, 3:15 AM Reply Quote 1
                • R Offline
                  ronny3050 Module Developer @yo-less
                  last edited by Aug 11, 2016, 3:15 AM

                  @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
                  • K Offline
                    KirAsh4 Moderator
                    last edited by Aug 11, 2016, 7:44 AM

                    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 Aug 11, 2016, 8:30 AM Reply Quote 0
                    • R Offline
                      ronny3050 Module Developer @KirAsh4
                      last edited by Aug 11, 2016, 8:30 AM

                      @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
                      1 / 2
                      • First post
                        1/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