Sign up here and you can log into the forum!

Plugin: SleepTimer + sleep on idle (update 3)

The WDTVExt plugin depot. Plugins/libraries/code only

Plugin: SleepTimer + sleep on idle (update 3)   

Postby dan » Sat Jun 19, 2010 7:05 am

Hi,

I have made a plugin to send the WD Live to sleep after 15, 30, 45, ... 120 minutes. Just press the search button to select the time. You can increase the value at any time by pressing the search button again.

Only one problem remains: You only see the time in movie, music or picture mode. The onPageCreated method is not called in any other mode. Maybe somebody can help with that?

major update 3:
    * new feature: now you can select sleep on idle and your devide will fall asleep after 10 mintues of no activity. So after playing your music or movie the device will go to standby mode automatically.
    * some code cleanup & optimization

Update 2:
    * Plugin is now futur proof and doesn't rely on Content lib anymore

Update:


Setup:

1) enable EIRI by uncommenting the following line in your /boot/S00custon_options
Code: Select all
config_tool -c EIRI=ON


2) add this plugin and reboot your device

SleepTimer.plugin.js
Code: Select all
traceln("SleepTimerPlugin: Start loading VolumeControl plugin");

function SleepTimerPlugin(path) {

    this.debugLogEnable = true
    this.rootPath = path;
    traceln("SleepTimerPlugin:   base path: " + this.rootPath);
    this.timerRun = false
   this.cmd = '';
    this.showOSD = 4
    this.sleepTime = 0
    this.sleepTimeMax = 120
    this.sleepTimeInterval = 15
   this.keyCode = 53 //search button
    this.counter = 0
}

SleepTimerPlugin.prototype = new Plugin();
SleepTimerPlugin.prototype.constructor = SleepTimerPlugin;

SleepTimerPlugin.prototype.debugLog = function (text) {
   if (this.debugLogEnable == true) {
      if (text && text != "") {
         traceln("SleepTimerPlugin: " + text)
      }
   }
}

SleepTimerPlugin.prototype.onPageCreated = function(page) {
        this.debugLog("[ onPageCreated ]")

      var extraContent = <wrapper><text text="@@vc_sleeptime" x="40" y="30" w="300" h="30" fontsize="26" textcolor="0xffffff" align="left"/></wrapper>;
      addExtraContent(page, extraContent);
      
}

SleepTimerPlugin.prototype.onPageTimer = function(page) {
    if (this.timerRun == true) {
        if (this.counter < this.showOSD) {
            this.counter++
        } else {
            this.counter = 0;
            this.timerRun = false;
            this.debugLog("[ onPageTimer ] clearing OSD display")
         this.debugLog("[ onPageTimer ] ### RUN COMMAND ###");
         this.debugLog("  cmd: " + this.cmd)
         system(this.cmd)
         Page.Top.setParam("vc_sleeptime", "")
            Page.redraw(true)
        }
    }
}

SleepTimerPlugin.prototype.setTime = function(time) {
    this.debugLog("[ setTime ] time: " + time)

   var timestring = "";
   
   if (time > (this.sleepTimeMax + this.sleepTimeInterval)) //turn it off
   {
      this.debugLog("[ setTime ] ---> : turn it off");
      this.sleepTime = 0;
      this.cmd = "echo 'if [ -f $0.pid ]; then p=$(cat $0.pid); rm $0.pid; kill -9 $p; fi && killall -9 sleep &' > /tmp/sleeptimer.sh && sh /tmp/sleeptimer.sh &"
      timestring = "Sleep: off";
   }
   else if (time == (this.sleepTimeMax + this.sleepTimeInterval)) //idle mode
   {
      //this.sleepTime = 0;
      this.debugLog("[ setTime ] ---> : idle mode");
      this.cmd = "echo \"if [ -f \\$0.pid ]; then p=\\$(cat \\$0.pid); rm \\$0.pid; kill -9 \\$p; fi; echo \\$\\$ > \\$0.pid; killall -9 sleep; sleeptime=60; idle=0; loop=0; while [ \\$loop -le 10 ]; do t=\\$(top -b -n 1 > /tmp/topdump.txt; cat /tmp/topdump.txt | grep idle | perl -wlne 'print \\$1 if /([0-9]+)\\.[0-9]% idle/'); if [ \\$t == 0 ]; then sleep \\$sleeptime; continue; fi; if [ \\$t -le 75 ]; then loop=0; idle=\\$t; else loop=\\`expr \\$loop + 1\\`; idle=\\`expr \\$idle + \\$t\\`; fi; echo \\$loop; echo \\$idle; sleep \\$sleeptime; done; rm \\$0.pid; \\$(/usr/bin/irsend POWER)\" > /tmp/sleeptimer.sh && sh /tmp/sleeptimer.sh &";
      timestring = "Sleep: on idle";      
   }
   else
   {
      var sleepTime = time * 60; //time in seconds
       this.cmd = "echo 'if [ -f $0.pid ]; then p=$(cat $0.pid); rm $0.pid; kill -9 $p; fi && echo $$ > $0.pid && killall -9 sleep && sleep "+sleepTime+" && rm $0.pid && /usr/bin/irsend POWER' > /tmp/sleeptimer.sh && sh /tmp/sleeptimer.sh &"
      timestring = "Sleep: "+time;
   }

    Page.Top.setParam("vc_sleeptime", timestring)
    Page.redraw(true)
    this.counter = 0
    this.timerRun = true
}

SleepTimerPlugin.prototype.onPageKey = function(page, key)
{
   this.debugLog("[ onPageKey ]")
   this.debugLog("  page: " + page.markupFileName + "  key: " + key)

   if (key == this.keyCode)
   {
      this.sleepTime += this.sleepTimeInterval;
      this.debugLog("  setting sleep time: " + this.sleepTime)
      this.setTime(this.sleepTime)
      return true
   }
}   

SleepTimerPlugin.instance = new SleepTimerPlugin(scriptPath);

Plugin.registerPlugin(SleepTimerPlugin.instance);


function addExtraContent(page, add) {
    if (!page || !add) { return }
    if(!page.extraContent) {
        page.extraContent = ""
    }

    var oldContent = page.extraContent;
    var addContent = add;
    var newContent = <wrapper></wrapper>;

    if (oldContent) {
        if (oldContent.length() > 0) {
            if (oldContent.wrapper.length() > 0) {
                newContent.wrapper += oldContent.wrapper
            } else {
                newContent.wrapper += oldContent
            }
        }
    }

    if (addContent) {
        if (addContent.length() > 0) {
            if (addContent.wrapper.length() > 0) {
                newContent.wrapper += addContent.wrapper
            } else {
                newContent.wrapper += addContent
            }
        }
    }
    page.extraContent = newContent;
    return
}

traceln("SleepTimerPlugin: End loading SleepTimerPlugin plugin");
Last edited by dan on Mon Jun 21, 2010 7:23 am, edited 10 times in total.
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Re: Plugin: SleepTimer   

Postby BlackPuma » Sat Jun 19, 2010 8:06 am

looks great,
but does it also counts when you watch movies?

anyway:

Image
BlackPuma
WDTVer
 
Posts: 32
Joined: Fri Jun 11, 2010 3:22 am
Location: Europe, Belgium

Re: Plugin: SleepTimer   

Postby dan » Sat Jun 19, 2010 8:21 am

Hi BlackPuma,

yes, if you set the sleeptimer once it will send your device to sleep no matter what the deivce is doing in the meantime. So you can fall asleep while watching movies or listening to your favorite music.

Like I said the only problem is that the time display wont show on some views.

Have fun.
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Re: Plugin: SleepTimer   

Postby BlackPuma » Sat Jun 19, 2010 8:37 am

But I want my device only to go to sleep after the movie is done,

for example:

sleeptimer: 30 min

I'm watching a movie and then after 30 min its goes to sleep? or not, I want after my movie around 92 min for example then 30 minutes from that point it goes to sleep?

when that's not possible maybe you should increase the sleeptimer's maxium, from 120 to 150 or something?
just my idea... tell me what you think
BlackPuma
WDTVer
 
Posts: 32
Joined: Fri Jun 11, 2010 3:22 am
Location: Europe, Belgium

Re: Plugin: SleepTimer   

Postby dan » Sat Jun 19, 2010 8:46 am

The plugin doesn't know anything about the movie length so you have to set it by hand.

You can tweak the values by setting those variables:
Code: Select all
this.sleepTimeMax = 120 //this is the maximal sleep time that can be selected in minutes
this.sleepTimeInterval = 15 // this is the amount of minutes that will be added to the current sleep time if you press the search button


BlackPuma wrote:for example:
sleeptimer: 30 min
I'm watching a movie and then after 30 min its goes to sleep?

Exactly.

BlackPuma wrote:I want after my movie around 92 min for example then 30 minutes from that point it goes to sleep?

Sorry, I don't understand.
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Re: Plugin: SleepTimer (updated)   

Postby dan » Sat Jun 19, 2010 9:25 am

...
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Re: Plugin: SleepTimer   

Postby BlackPuma » Sat Jun 19, 2010 10:22 am

dan wrote:
BlackPuma wrote:I want after my movie around 92 min for example then 30 minutes from that point it goes to sleep?

Sorry, I don't understand.


sorry for being unclear,

but I see where you are going to, it's fine.

I misunderstood the meaning of your timer.

I thought: that the sleeptimer was used to put the device to sleep, when not in use.
And you want the device to go to sleep even when a movie or music is playing.
my mistake. never mind :-)

still BIG THANK YOU!
BlackPuma
WDTVer
 
Posts: 32
Joined: Fri Jun 11, 2010 3:22 am
Location: Europe, Belgium

Re: Plugin: SleepTimer (updated)   

Postby dan » Sat Jun 19, 2010 10:42 am

Ah now I get it: You want the device to go to sleep after the device idles for x minutes, right? Thats a pretty good idea! I will implement this in a future release. Thanks for that hint.
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Re: Plugin: SleepTimer (updated)   

Postby BlackPuma » Sun Jun 20, 2010 12:16 am

yeah :-) now you got it.

Keep it up, I'm using it right now on 150 min, just enough to watch a movie, and when I forget to turn it of it will do it automatically :-)
BlackPuma
WDTVer
 
Posts: 32
Joined: Fri Jun 11, 2010 3:22 am
Location: Europe, Belgium

Re: Plugin: SleepTimer + sleep on idle (update 3)   

Postby dan » Sun Jun 20, 2010 11:16 am

Hi PlackPuma,

I have added sleep on idle support. Please update your SleepTimer.plugin.js with the new code of the first post.
dan
Developer
 
Posts: 40
Joined: Fri May 28, 2010 11:35 pm

Next

Return to WDTVExt plugins

Who is online

Users browsing this forum: No registered users and 1 guest