Jump to content

IntelligentShutdown.vbs


Recommended Posts

Here is a small piece of vbscript that I wrote to suit my needs to prevent my dedicated HTPC from shutting down if I press the power button on my remote while recording service or DVBViewer is recording. The script comes in three parts. First part is the script itself that does the checking of ongoing or soon to start recordings. The second part is command.vbs which is placed in DVBViewer's scripts folder and it checks for remote power button press. Third part is a new action to DVBViewer's actions.ini which defines a new action number for the shutdown script.

 

Here are the three parts, first the action for actions.ini. Add the following line at the end of actions.ini and save the file. You also need to adjust your remote settings to bind that action 25000 to your remote's power button. How to do it depends on what remote you use and whether you use DVBViewer's internal remote input or Girder like I do or something else, so I am not going into detail with that.

 

IntelligentShutDown=25000

 

 

Next add this piece of script to Command.vbs in DVBViewer's scripts folder. If command.vbs already exists then leave out Sub and End Sub definitions and add the rest of the code between existing Sub..End Sub definitions. If Command.vbs does not exist in your system then just create the file with the code below. Remember to set the path in execnewprocess to point to your scripts directory where you save the actual IntelligentShutdown.vbs script.

 

Sub main(command)

set oDVBV=getobject(,"dvbviewerserver.DVBViewer")

If command = 25000 then 
oDVBV.osd.execnewprocess "c:\windows\system32\wscript.exe","C:\Users\tv\Desktop\DVBViewer\scripts\IntelligentShutdown.vbs",false,false

End If
End Sub

 

 

Third, here is the actual code that checks the recordings and decides whether to justshut down the monitor or put the computer in standby:

 


Dim DVBVRecording
Dim DVBVActive
Dim DVBVRecordingSoon

set oDVBV=getobject(,"dvbviewerserver.DVBViewer")

'=======================
' ongoing recording check
'=======================

DVBVRecording = FALSE

For i = 0 to oDVBV.timermanager.count-1
If oDVBV.timermanager(i).recording Then
	DVBVRecording = TRUE ' recording going on 
End If
Next

' ==============
' Next recording
' ==============

DVBVRecordingSoon = FALSE

NextRecordingStart = oDVBV.timermanager.nextrecordingtime 

If NextRecordingStart <> 0 AND NextRecordingStart < dateadd("n",15,now) Then
DVBVRecordingSoon = TRUE ' recording starting in 15 minutes
End If

' =====================================================================================
' check both ongoing recording and a recording starting in 15 mins and act accordingly
' =====================================================================================

If DVBVRecording OR DVBVRecordingSoon Then
oDVBV.OSD.ShowInfo "SHUTDOWN STATUS","Recording active! Will just shutdown monitor.","",10,""
oDVBV.SendCommand(16383) ' close graph 
oDVBV.SendCommand(12328) ' shutdown monitor
Else
oDVBV.SendCommand(12324) ' standby computer 
End If

 

That's it, restart DVBViewer and see how the script works. If I forgot some important steps then forgive me, it is already past midnight here...

 

patti

Link to comment

Yes.

And because of that - some errorhandling after

oDVBV=getobject(,"dvbviewerserver.DVBViewer") 

would be good. :blush:

Edited by nuts
Link to comment

Works only if Client is started (in other words COM must be registred), yes?

 

Yes, but that is not a problem for me as I use only remote to shut the HTPC down and my remote doesn't work without DVBViewer. As I said, this is for a dedicated HTPC, for mixed use it might not be as useful.

 

Also the recommended error handling would not do much good because if DVBViewer is not running then the whole script does not execute and thus there is no need for error checking :)

 

patti

Edited by patti
Link to comment

Yes, but that is not a problem for me as I use only remote to shut the HTPC down and my remote doesn't work without DVBViewer. As I said, this is for a dedicated HTPC, for mixed use it might not be as useful.

 

Also the recommended error handling would not do much good because if DVBViewer is not running then the whole script does not execute and thus there is no need for error checking :)

 

patti

Ok, but if folks wanna use your script for automating purposes there would be great if there is some error handling, cuz if client is not running the script fails accordingly. This is how I would solve it:

 

On Error Resume Next
strComputer = "."
arrTargetProcs = Array("DVBViewer.exe")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each strTargetProc In arrTargetProcs
   Set colProcesses = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_Process WHERE Name='" & strTargetProc & "'")
   If colProcesses.Count = 0 Then
       Set WshShell = WScript.CreateObject("WScript.Shell")
       Return = WshShell.Run("""C:\Program\DVBViewer\DVBViewer.exe""", 1, true)
       Set WshShell = Nothing
   End If
Next

 

Tested and works for XP. Place code first in script. Code checks if client exist...if not it is started.

Edited by majstang
Link to comment

Ok, but if folks wanna use your script for automating purposes there would be great if there is some error handling, cuz if client is not running the script fails accordingly. This is how I would solve it:

 

What kind of automating purposes do you have in mind?

 

I have another script that is still unfinished and I check the existence of DVBViewer this way:

 

Const DVBVPath = "C:\users\tv\desktop\DVBViewer\beta\DVBViewer.exe"

On Error Resume Next
set oDVBV=getobject(,"dvbviewerserver.DVBViewer")

if Err <> 0 Then
set oShell = WScript.CreateObject("Wscript.Shell")
Result = oShell.Run(DVBVPath,1,FALSE)
Err.Clear
Wscript.Sleep(10000) ' delay to let DVBV start before trying to create oDVBV again
set oDVBV=getobject(,"dvbviewerserver.DVBViewer")
end if

 

patti

Edited by patti
Link to comment

What kind of automating purposes do you have in mind?

 

I have another script that is still unfinished and I check the existence of DVBViewer this way:

 

Well, i thought it would be neat to have a post recording standby/hibernation script which behaves in different ways depending on if a number of variables comes back as true or false. Most parts of your excellent script can be used for that purpose. A goal for me is to fully automate things as far as possible.

 

You had problems before executing your COM scripts (if I remember it right) through recording service....how did that go...did you find some solution?

Edited by majstang
Link to comment

Well, i thought it would be neat to have a post recording standby/hibernation script which behaves in different ways depending on if a number of variables comes back as true or false. Most parts of your excellent script can be used for that purpose. A goal for me is to fully automate things as far as possible.

 

You had problems before executing your COM scripts (if I remember it right) through reording service....how did that go...did you find some solution?

 

My second script is just what you describe above: "post recording standby/hibernation script which behaves in different ways depending on if a number of variables comes back as true or false." I want DVBViewer to behave intelligently depending whether it is a scheduled unattended recording or if the recording stops while I am watching TV. I hate it when DVBViewer tells me "Recording service is going to shut down the computer in XX seconds". It usually happens when I am away from my sofa and cannot react to it. So what I have in mind is a script that checks whether I am actually watching tv or a media file (=graph active) and then post recording task would jsut stop the recording and do nothing else. When the graph is not present during a recording it means that it is an unattended scheduled recording or I have shut down the monitor via above script when going to sleep for example and TV needs to shut down after the recording. Is this sort of what you are trying to do too?

 

Yes, I had problems with rec. svc's post recording tasks accessing DVBViewer's COM interface to read the information needed for what I just described above. The explanation and reason of my problems can be found here: http://www.DVBViewer.tv/forum/topic/40427-why-isnt-my-script-executing-as-a-rec-svc-task/page__pid__300256#entry300256 As a solution I have been thinking of running rec svc as a session 1 application through the use of psexec from Sysinternals which can define which session to use. I have not really got into it yet but somehow I will start the service in session 1 some day :) This session 0/1 problem only affects OS'es starting from Win Vista. Win XP service can interact with DVBViewer no problem since they run in the same session 0.

 

patti

 

edit: Hmm, I just read the WHOLE document that is included in my above link and realised it offers my solution. I had only read the session 0/1 part of it before and never the whole document. I'll try the methods desscribed in the document right a way...

Edited by patti
Link to comment

Is this sort of what you are trying to do too?

Thankfully i have XP, so i dont have to deal with the Win7 issues you are describing. So, my script kind of reminds of yours, but includes more variables. Such as one behaviour during weekdays and another during weekends. Time of the day variable. Hibernation if no recording is due within 30 minutes between 01.00-16.00. No hibernation between 16.00-01.00. If RS is recording or not.

 

With that many variables it is getting a little too complicated I think :wacko:

Link to comment

With that many variables it is getting a little too complicated I think :wacko:

 

Well if it works it doesn't matter if it is complicated or not :)

 

It would be nice if recording service had scripting support to read it's state without the need to start DVBViewer/COM for that. Your solution of reading recording service's recording status through taskbar icon is an ingenious one! :thumbsup:

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...