Author: duncan
Date: Tue Oct  3 16:30:51 2006
New Revision: 8305

Added:
   branches/rel-1-5/freevo/src/plugins/upsoon.py
Modified:
   branches/rel-1-5/freevo/src/helpers/recordserver.py

Log:
[ 1567895 ] Interrupt live tv
Added to recordserver these functions
progsTimeCompare
findNextProgram
isPlayerRunning
Removed the cvs log and changed _debug_


Modified: branches/rel-1-5/freevo/src/helpers/recordserver.py
==============================================================================
--- branches/rel-1-5/freevo/src/helpers/recordserver.py (original)
+++ branches/rel-1-5/freevo/src/helpers/recordserver.py Tue Oct  3 16:30:51 2006
@@ -6,66 +6,6 @@
 # $Id$
 #
 # -----------------------------------------------------------------------
-# $Log$
-# Revision 1.59.2.3  2004/10/29 18:22:05  dischi
-# removed unicode bug
-#
-# Revision 1.59.2.2  2004/10/20 18:41:09  dischi
-# add missing :
-#
-# Revision 1.59.2.1  2004/10/20 18:35:34  dischi
-# fix favorite recording bug and set favorite priority below manual scheduled
-#
-# Revision 1.59  2004/07/16 19:39:57  dischi
-# store recording timestamp
-#
-# Revision 1.58  2004/07/12 22:33:17  mikeruelle
-# need the sharp part of the shebang line
-#
-# Revision 1.57  2004/07/12 14:52:32  outlyer
-# More debug. If no one objects, I'll delete these completely rather than
-# commenting them out.
-#
-# Revision 1.56  2004/07/11 13:54:33  dischi
-# cache scheduledRecordings in memory
-#
-# Revision 1.55  2004/07/10 12:33:39  dischi
-# header cleanup
-#
-# Revision 1.54  2004/07/09 21:05:46  rshortt
-# Add warnings in case the recording plugin isn't there.
-#
-# Revision 1.53  2004/07/09 16:20:54  outlyer
-# Remove the request logging for 0-level debug. Exceptions will still be
-# logged, but standard requests will not.
-#
-# (i.e. this removes the Apache-style access logging for DEBUG = 0)
-#
-# Revision 1.52  2004/07/09 11:19:21  dischi
-# fix unicode crash
-#
-# Revision 1.51  2004/07/09 02:28:53  outlyer
-# If the automatic caching fails (as was happening for me) then just leave
-# the png file in place for Freevo's OSD to pickle on access (rather than
-# pre-pickled as before)
-#
-# If your system was caching the image properly before, then this shouldn't
-# make any difference and the code probably won't even be called.
-#
-# Revision 1.50  2004/07/01 19:10:45  dischi
-# add TV_RECORD_SERVER_GID
-#
-# Revision 1.49  2004/06/29 03:46:54  outlyer
-# Hide some print statements. If these were supposed to go into debug, I can
-# change that.
-#
-# Revision 1.48  2004/06/28 20:40:16  dischi
-# make it possible to switch uid
-#
-# Revision 1.47  2004/06/23 21:20:10  dischi
-# put snapshot in again with a try except
-#
-# -----------------------------------------------------------------------
 # Freevo - A Home Theater PC framework
 # Copyright (C) 2002 Krister Lagerstrom, et al. 
 # Please see the file freevo/Docs/CREDITS for a complete list of authors.
@@ -120,15 +60,18 @@
 import util.tv_util as tv_util
 import plugin
 import util.popen3
-from   util.videothumb import snapshot
+from util.videothumb import snapshot
+from event import *
 
+dbglvl=1
 
-from event import *
+def _debug_(text, level=1):
+    if config.DEBUG >= level:
+        try:
+            log.debug(String(text))
+        except:
+            log.debug('Failed to log a message')
 
-def _debug_(text):
-    if config.DEBUG:
-        log.debug(String(text))
-        
 _debug_('PLUGIN_RECORD: %s' % config.plugin_record)
 
 appname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
@@ -161,6 +104,65 @@
 
 class RecordServer(xmlrpc.XMLRPC):
 
+    def progsTimeCompare(self, first, second):
+        t1 = first.split(':')[-1]
+        t2 = second.split(':')[-1]
+        try:
+            return int(t1) - int(t2)
+        except ArithmeticError:
+            pass
+        return 0
+
+    def findNextProgram(self):
+        _debug_('in findNextProgram', dbglvl+3)
+
+        progs = self.getScheduledRecordings().getProgramList()
+        now = time.time()
+
+        next_program = None
+        proglist = list(progs)
+        proglist.sort(self.progsTimeCompare)
+        for progitem in proglist:
+            prog = progs[progitem]
+            _debug_('%s:%s chan=%s %s->%s' % (prog.title, prog.sub_title, \
+                prog.channel_id, time.localtime(prog.start), 
time.localtime(prog.stop)), dbglvl+1)
+
+            try:
+                recording = prog.isRecording
+            except:
+                recording = False
+            _debug_('%s: recording=%s' % (prog.title, recording))
+
+            if now >= prog.stop + config.TV_RECORD_PADDING_POST:
+                _debug_('%s: prog.stop=%s, now=%s' % (prog.title, \
+                    time.localtime(prog.stop+config.TV_RECORD_PADDING_POST), 
now), dbglvl+1)
+                continue
+            _debug_('%s: prog.stop=%s' % (prog.title, 
time.localtime(prog.stop)), dbglvl)
+
+            if not recording:
+                next_program = prog
+                break
+
+        self.next_program = next_program
+        if next_program == None:
+            _debug_('No program scheduled to record', dbglvl)
+            return None
+
+        _debug_('\"%s\" %s %s->%s' % (next_program.title, 
next_program.channel_id, \
+            time.localtime(next_program.start), 
time.localtime(next_program.stop)), dbglvl)
+        return next_program
+
+
+    def isPlayerRunning(self):
+        '''
+        returns the state of a player, mplayer, xine, etc.
+        TODO:
+            real player running test, check /dev/videoX.
+            this could go into the upsoon client
+        '''
+        _debug_('in isPlayerRunning', dbglvl+3)
+        return (os.path.exists(config.FREEVO_CACHEDIR + '/playing'))
+
     # note: add locking and r/rw options to get/save funs
     def getScheduledRecordings(self):
         file_ver = None
@@ -411,7 +413,7 @@
 
         now = time.time()
         for prog in progs.values():
-            _debug_('checkToRecord: progloop = %s' % String(prog))
+            _debug_('checkToRecord: progloop=%s' % String(prog))
 
             try:
                 recording = prog.isRecording
@@ -729,6 +731,16 @@
     #  Start XML-RPC published methods.                             #
     #################################################################
 
+    def xmlrpc_isPlayerRunning(self):
+        status = self.isPlayerRunning()
+        message = status and 'player is running' or 'player is not running'
+        return (status, message)
+
+    def xmlrpc_findNextProgram(self):
+        response = self.findNextProgram()
+        status = response != None
+        return (status, jellyToXML(response))
+
     def xmlrpc_getScheduledRecordings(self):
         return (TRUE, jellyToXML(self.getScheduledRecordings()))
 

Added: branches/rel-1-5/freevo/src/plugins/upsoon.py
==============================================================================
--- (empty file)
+++ branches/rel-1-5/freevo/src/plugins/upsoon.py       Tue Oct  3 16:30:51 2006
@@ -0,0 +1,221 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------
+# wakeup client
+# -----------------------------------------------------------------------
+# $Id: upsoon.py $
+#
+# Notes:
+#    To activate, put the following line in local_conf.py:
+#       plugin.activate('upsoon')
+# ToDo:        
+#
+# -----------------------------------------------------------------------
+#
+# Freevo - A Home Theater PC framework
+# Copyright (C) 2002 Krister Lagerstrom, et al. 
+# Please see the file freevo/Docs/CREDITS for a complete list of authors.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
+# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# ----------------------------------------------------------------------- */
+
+
+import plugin
+import config
+import time, sys, socket, traceback, string
+import xmlrpclib
+import rc
+import thread
+from event import *
+from util.marmalade import jellyToXML, unjellyFromXML
+
+# set the base debug level
+dbglvl=1
+
+
+class PluginInterface( plugin.DaemonPlugin ):
+    """
+    plugin to monitor if a recording is about to start and shut down the
+    player if the video device is in use
+
+    Requirements:
+       none
+
+    To activate this plugin, just put the following line at the end of your
+    local_conf.py file:
+
+    plugin.activate('upsoon')
+
+    """
+    __author__           = 'Duncan Webb'
+    __author_email__     = '[EMAIL PROTECTED]'
+    __maintainer__       = __author__
+    __maintainer_email__ = __author_email__
+    __version__          = '$Revision: 20061003 $'
+
+
+    def __init__( self ):
+        """
+        init the upsoon plugin
+        """
+        _debug_('__init__(self)', dbglvl+1)
+        plugin.DaemonPlugin.__init__(self)
+        self.lock = thread.allocate_lock()
+        self.poll_interval = 3000 #30 secs
+        self.poll_menu_only = 0
+        self.event_listener = 1
+        plugin.register( self, 'upsoon' )
+
+        server_string = 'http://%s:%s/' % (config.TV_RECORD_SERVER_IP, 
config.TV_RECORD_SERVER_PORT)
+
+        _debug_('%s' % server_string, dbglvl)
+        self.server = xmlrpclib.Server(server_string, allow_none=1)
+        _debug_('%s' % self.server, dbglvl)
+
+        self.serverup = None
+        self.next_program = self.findNextProgram()
+        _debug_('up=%s %s' % (self.serverup, self.next_program), dbglvl)
+
+
+    def findNextProgram(self):
+        """
+        returns the next program that will be recorded
+        """
+        _debug_('findNextProgram(self)', dbglvl+1)
+        serverup = True
+        try:
+            (status, message) = self.server.findNextProgram()
+            _debug_('status=%s, message=%s' % (status, message), dbglvl+2)
+        except TypeError:
+            _debug_('TypeError exception')
+            status = False
+            pass
+        except Exception, e:
+            serverup = False
+            status = False
+            if sys.exc_type != socket.error:
+                traceback.print_exc()
+                _debug_(e, 0)
+
+        if self.serverup != serverup:
+            self.serverup = serverup
+            if serverup:
+                _debug_('The record server is up')
+            else:
+                _debug_('The record server is down')
+
+        if not status:
+            return None
+
+        next_program = unjellyFromXML(message)
+        return next_program
+
+
+    def isPlayerRunning(self):
+        """
+        Check with the record server if suspended by user
+        """
+        _debug_('isPlayerRunning(self)', dbglvl+1)
+        serverup = True
+        try:
+            (status, message) = self.server.isPlayerRunning()
+            _debug_('status=%s, message=%s' % (status, message), dbglvl+2)
+        except Exception, e:
+            serverup = False
+            message = None
+            if sys.exc_type != socket.error:
+                traceback.print_exc()
+                _debug_(e, 0)
+
+        if self.serverup != serverup:
+            self.serverup = serverup
+            if serverup:
+                _debug_('The record server is up')
+            else:
+                _debug_('The record server is down')
+
+        if message == None:
+            return None
+
+        return status
+
+
+    def getPlayerRunning(self):
+        """
+        Return is a player is running
+        """
+        _debug_('getPlayerRunning(self)', dbglvl+1)
+        return self.is_player_running
+
+
+    def close( self ):
+        """
+        to be called before the plugin exists.
+        It terminates the connection with the server
+        """
+        _debug_('close(self)')
+
+
+    def poll( self ):
+        """
+        Sends a poll message to the record server
+        """
+        _debug_('poll(self)', dbglvl+1)
+
+        self.player_running = self.isPlayerRunning()
+        _debug_('player_running=%s' % self.player_running, dbglvl)
+        self.next_program  = self.findNextProgram()
+        _debug_('next_program=%s' % self.next_program, dbglvl)
+        if self.next_program != None:
+            # may need to ensure message is not too long
+            start_time = time.strftime('%a %H:%M', 
time.localtime(self.next_program.start))
+            message = '%s %s' % (self.next_program.title, start_time)
+            rc.post_event(Event(OSD_MESSAGE, message))
+
+
+    def eventhandler( self, event, menuw=None ):
+        """
+        Processes user events
+        TODO
+            something useful
+        """
+        _debug_('eventhandler(self, %s, %s) name=%s arg=%s context=%s 
handler=%s' % \
+            (event, menuw, event.name, event.arg, event.context, 
event.handler), dbglvl+2)
+
+        _debug_('event name=%s arg=%s' % (event.name, event.arg), dbglvl)
+
+        self.lock.acquire()
+        if event == BUTTON:
+            if event.arg == 'POWER':
+                if self.isPlayerRunning():
+                    rc.post_event(Event(OSD_MESSAGE, arg=_('Playing')))
+                else:
+                    rc.post_event(Event(OSD_MESSAGE, arg=_('Not Playing')))
+        self.lock.release()
+
+        return 0
+
+
+# this won't work
+if __name__ == '__main__':
+    if len(sys.argv) >= 2: 
+        function = sys.argv[1]
+    else:
+        function = 'none'
+
+
+    if function == "isplayerrunning":
+        (result, response) = isPlayerRunning()
+        print response

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to