Auto-disable module


  • Module Developer

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


  • Module Developer

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


  • Moderator

    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.


  • Module Developer

    Nice solution, thanks man!


  • Module Developer

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


  • Module Developer

    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();
          }
    }
    

  • Module Developer

    @ronny3050 Impressive response, thank you!


  • Module Developer

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


  • Moderator

    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.


  • Module Developer

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


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.