Linux Delay shutdown when SSH or Samba connections are opened
#16
Hmm, this is weird. Do you have the last version from github? Ports set in the "remote ports" field should only be checked if remote address is not equal to local address.

Your workaround will be Ok, unless you ever want to check local connections.

I'm not at my XBMC machine, I'll try to fix it next weekend.
Reply
#17
i downloaded your addon today from git... so this one should be ok.

What I found out in digging a bit deeper... after some restarts, it behaves like it should (I added a log line, if 127.0.0.1 is blocking). So 127.0.0.1 now is not blocking suspend anymore. weird... I will have a look at this the next days...

what am I doing right now:
Integrating TVHeadend timer to prevent standby, if an upcoming record in the next "x" minutes is found. For now, I do this by using a shell script with passing exit-code to your python script. Unfortunately, this won't prevent the remote power-button.
Reply
#18
@cube
can't figure out, when my system reports that 127.0.0.1 is blocking suspend. It seems to be random after some restarts

I also did some enhancements for TVHeadend backend users. I added this to your script (just calling a second script to check scheduled recordings):

new function
Code:
def check_rec():
        upcoming = subprocess.check_output(['/path/to/pvr_hts.py'])
        if "1" in upcoming:
                log("Found upcoming recordings")
                return True

        log("No upcoming recordings found")
        return False

if added, of course you also need to edit this line
Code:
while not xbmc.abortRequested:
    if (check_rec() or check_services()):
        xbmc.executebuiltin('InhibitIdleShutdown(true)')

the second script, called "pvr_hts.py"
Code:
#!/usr/bin/python

import time
import glob
import sys

current_time = int(time.time())
prevent_time = 300

rec_start = 0
rec_start_extra = 0
rec_stop = 0
rec_stop_extra = 0

recordings = glob.glob(/path/to/.hts/tvheadend/dvr/log/*')
for record in recordings:
        rec_file = open(record, 'r')

        for line in rec_file:
                tmp_line = line.strip()
                items = tmp_line.split(':')
                if items[0] == "\"start\"":
                        tmp_rec_start = int(items[1].strip().strip(','))
                if items[0] == "\"start_extra\"":
                        tmp_rec_start_extra = int(items[1].strip().strip(','))
                if items[0] == "\"stop\"":
                        tmp_rec_stop = int(items[1].strip().strip(','))
                if items[0] == "\"stop_extra\"":
                        tmp_rec_stop_extra = int(items[1].strip().strip(','))
        rec_file.close()

        if (tmp_rec_stop+(tmp_rec_stop_extra*60) > current_time) and (tmp_rec_start > current_time):
                if (rec_start == 0) or (tmp_rec_start < rec_start):
                        rec_start = tmp_rec_start
                        rec_stop = tmp_rec_stop
                        rec_start_extra = tmp_rec_start_extra*60
                        rec_stop_extra = tmp_rec_stop_extra*60

rec_start = rec_start-rec_start_extra
rec_stop = rec_stop+rec_stop_extra

if (rec_start != 0) and (0 < (rec_start-current_time) < prevent_time):
        print 1
        sys.exit(1)
print 0
sys.exit(0)

I also wanted to check, if there is a recording running. Just for having the 100% Wink but seems to work without

to complete this, I used this script from the lonelycoder wiki: https://www.lonelycoder.com/redmine/proj...iki/Wakeup

let me know, if this is useful for merging into your plugin.

for those, wanting their remote also prevent standby on remote power-button:
you can call the python script from /etc/pm/sleep.d/1_your_script (the 1 is important, so suspend is cancelled before other scripts are triggered). just check the error value. 1==prevent standby, 0==allow standby

now it's time to Sleepy
Chris
Reply
#19
I don't like that part using subprocess, import + a normal function call would be nicer when the other script is in python as well.

And about the thing as whole -- I thought XBMC did something like this by itself, doesn't it? I'm also using TvHeadend with XBMC and I think I've seen some options about wake up scripts in XBMC config, but I've never really explored the recording part so I'm not sure of this.
Reply
#20
I havent't found builtin options for waking up on upcoming records... Perhaps I search the wrong thing? just found howtos for placing scripts here and there in XBMCbuntu special folders (e.g. sleep.d).

why I made a subprocess:
It's my first python script Tongue
I thought about implementing a function for each pvr supported by xbmc. So I thought about placin python scripts in your addon-folder beside your main script. The second script can be included/imported in your script and only call the function. I didn't manage to get the folder, your script is running in programmatically to load my script in the same folder without defining an absolute path. I try to get this managed today, so a function for each supported pvr can be loaded programmatically by its own python file.
Reply
#21
I just have an idea why it's not workin at random -- it could be some newlines or other beasts like that in the netstat output.
Try changing
Code:
log("Found connection from {} to {}:{}".format(remote_addr, local_addr, local_port))
to
Code:
log("Found connection from {} to {}:{}".format(repr(remote_addr), repr(local_addr), local_port))
and let me know what it logs when the bug appears again.
Reply
#22
here are some logs for the "problem", with the modifications you told:
Code:
11:49:19 T:2665659200  NOTICE: service.inhibit_shutdown: Watching for remote connections to ports 9982, 445, 22 and for local connections to ports , sleep time is 60.00 s.
11:49:20 T:2665659200  NOTICE: service.inhibit_shutdown: No upcoming recordings found
11:49:20 T:2665659200  NOTICE: service.inhibit_shutdown: localhost connection from '127.0.0.1' to '127.0.1.1':9982
11:49:20 T:2665659200  NOTICE: service.inhibit_shutdown: Found connection from '10.5.1.109' to '10.5.1.170':22

1st line: yours
2nd line: my scheduled recordings check
3rd line: the problem catched by (remote_addr == "127.0.0.1")
4th line: yours

edit:
it looks like, that this happens after resume from suspend (S3)... but not 100% sure about this
Reply
#23
this task makes fun Tongue

so here it is, fully implemented in python and integrated in cube's plugin:

plugin, needed imports:
Code:
import pvr_hts

plugin, extra function:
Code:
def check_rec():
        if pvr_hts.check_hts():
                log("Found upcoming recordings")
                return True

        log("No upcoming recordings found")
        return False

seperated python script for checking tvheadend, named pvr_hts.py in plugin root folder (the same folder as inhibit_shutdown.py):
Code:
#!/usr/bin/python

import time
import glob
import os

def check_hts():
        current_time = int(time.time())
        prevent_time = 300

        rec_start = 0
        rec_start_extra = 0
        rec_stop = 0
        rec_stop_extra = 0

        recordings = glob.glob(os.path.expanduser('~') + '/.hts/tvheadend/dvr/log/*')

        for record in recordings:
                rec_file = open(record, 'r')

                for line in rec_file:
                        tmp_line = line.strip()
                        items = tmp_line.split(':')
                        if items[0] == "\"start\"":
                                tmp_rec_start = int(items[1].strip().strip(','))
                        if items[0] == "\"start_extra\"":
                                tmp_rec_start_extra = int(items[1].strip().strip(','))
                        if items[0] == "\"stop\"":
                                tmp_rec_stop = int(items[1].strip().strip(','))
                        if items[0] == "\"stop_extra\"":
                                tmp_rec_stop_extra = int(items[1].strip().strip(','))
                rec_file.close()

                if (tmp_rec_stop+(tmp_rec_stop_extra*60) > current_time) and (tmp_rec_start > current_time):
                        if (rec_start == 0) or (tmp_rec_start < rec_start):
                                rec_start = tmp_rec_start
                                rec_stop = tmp_rec_stop
                                rec_start_extra = tmp_rec_start_extra*60
                                rec_stop_extra = tmp_rec_stop_extra*60

        rec_start = rec_start-rec_start_extra
        rec_stop = rec_stop+rec_stop_extra

        if (rec_start != 0) and (0 < (rec_start-current_time) < prevent_time):
                return True

        return False

plugin, extra check:
Code:
while not xbmc.abortRequested:
    if (check_rec() or check_services()):
        xbmc.executebuiltin('InhibitIdleShutdown(true)')
    else:
        xbmc.executebuiltin('InhibitIdleShutdown(false)')
    xbmc.sleep(sleep_time)

this will prevent standby if scheduled recording will record in prevent_time (seconds, here: 5minutes). So my system won't go automatically to suspend if a recording is upcoming in the next prevent_time seconds.
Of course, this can be more user friendly by implementing GUI options for prevent_time and the backend used (pass some variables to my function). But due to the lack of installed backends and hardly freetime, I just implemented this for TVHeadend.

Feel free to implement a more general way of checking schedules.

why I did this:
my old mediacenter software had these functions included and I think they are very useful, e.g. the system supsends 5 seconds before a recording will start, mine won't wake up automatically... so it seems good to have a prevent for some systems.

Also, this is my first python script. If there is a way to do things better, please tell us...
Reply
#24
Lightbulb 
Rofl I have completely ignored the option in XBMCs LiveTV-Settings... Rofl

seems that my work is a bit waste of time, because this feature is already integrated in XBMC itself Blush

EDIT:
but it seems not to work... upcoming schedule in 10minutes... configured prewakeup to 15min and backend idle to 15min in XBMC-settings -> system is allowed to go to suspend. have tp spend more time on this. will report back soon.
Reply
#25
Could you add a feature to ping remote machines and keep the master alive if they respond? This would save me months of trying to learn .pl Blush

[Edit1], i as actually able to solve my problem by checking for the pvr-client connection. But still, somebody else might need the addition.
Thank you so much for this addon!

[Edit2], Oh, wrong again - the pvr-client does not make a connection before you press live-tv in xbmc.

[Third edit], When using 127.0.0.1 for the local pvr plugin the video stream sometimes gets some nasty problems. Hence I and a probably a few others are using the ip of the local system. This will fools your script. I've made a very (VERY!!) crude modification to fix my little problem. (grep -v 10.0.0.10)

[Edit again] The did not work... ofcause. It filtered all the connections. I'll just have to go back to using 127.0.0.1 i guess Smile

Sorry for all the edits. I don't tend to think things all the way through....

The right way might be to make a slave addon which keeps an open connection to the master. This master/slave thing i essential for any multiroom setup.
Reply
#26
Hi, I'm currrently a little short on time ... but I'll try to add that sometime.
Or if anyone feels like writing it and sending a pull request on github, you are more than wellcome :-)
Reply
#27
I'll try.... If i get anything to work, it will need some cleanup for sure!
Reply
#28
Hi, I'm completely new to XBMC. I use my XBMC-PC as a Samba share. The clients wake the XBMC-PC up with wake on lan during the boot.

I've installed your plugin but the computer doesn't shutdown on it's own. For testing, i haven't changed the settings for watched ports (22,445) or sleep time (60). What am I doing wrong?

Thanks.
Reply
#29
I often have ssh connected and turn off the htpc the delay is zero. Not in Linux anyway.

uNi
Reply
#30
(2013-03-28, 10:58)wh!t3f!5h Wrote: Hi, I'm completely new to XBMC. I use my XBMC-PC as a Samba share. The clients wake the XBMC-PC up with wake on lan during the boot.

I've installed your plugin but the computer doesn't shutdown on it's own. For testing, i haven't changed the settings for watched ports (22,445) or sleep time (60). What am I doing wrong?

Thanks.

The script only temporarily disables xbmc's own sleep. This means that you have to set sleep timer in the XBMC settings separately ... I'm not sure where in settings it is exactly.
Reply

Logout Mark Read Team Forum Stats Members Help
Delay shutdown when SSH or Samba connections are opened0
This forum uses Lukasz Tkacz MyBB addons.