16
16
17
17
import cwiid , time , StringIO , sys , asyncore , socket , os
18
18
import subprocess
19
+ import logging
19
20
from math import log , floor , atan , sqrt , cos , exp
21
+ logger = logging .getLogger ("mythpywii" )
20
22
21
23
# Note to self - list of good documentation:
22
24
# cwiid: http://flx.proyectoanonimo.com/proyectos/cwiid/
@@ -46,18 +48,16 @@ def __init__(self, owner, host="localhost"):
46
48
self .create_socket (socket .AF_INET , socket .SOCK_STREAM )
47
49
self .connect ((host , 6546 ))
48
50
def handle_close (self ):
49
- print "Mythfrontend connection closed"
51
+ logger . info ( "Mythfrontend connection closed" )
50
52
self .owner .socket_disconnect ()
51
53
self .close ()
52
54
def handle_read (self ):
53
55
try :
54
56
self .data = self .data + self .recv (8192 )
55
57
except :
56
- print """
57
- [ERROR] The connection to Mythfrontend failed - is it running?
58
- If so, do you have the socket interface enabled?
59
- Please follow the instructions at http://www.benjiegillam.com/mythpywii-installation/
60
- """
58
+ logger .error ("The connection to Mythfrontend failed - is it running?" )
59
+ logger .error ("If so, do you have the socket interface enabled?" )
60
+ logger .error ("Please follow the instructions at http://www.benjiegillam.com/mythpywii-installation/" )
61
61
self .handle_close ()
62
62
return
63
63
while len (self .data )> 0 :
@@ -67,12 +67,12 @@ def handle_read(self):
67
67
result = self .data [:a ]
68
68
self .data = self .data [a + len (self .prompt ):]
69
69
if not self .firstData :
70
- print ">>>" , result
70
+ logger . debug ( ">>> %s" % result )
71
71
cb = self .callbacks .pop (0 )
72
72
if cb :
73
73
cb (result )
74
74
else :
75
- print "Logged in to MythFrontend"
75
+ logger . info ( "Logged in to MythFrontend" )
76
76
self .firstData = False
77
77
else :
78
78
break ;
@@ -81,7 +81,7 @@ def writable(self):
81
81
def handle_write (self ):
82
82
a = self .buffer .find ("\n " )
83
83
sent = self .send (self .buffer [:a + 1 ])
84
- print "<<<" , self .buffer [:sent - 1 ]
84
+ logger . debug ( "<<< %s" % self .buffer [:sent - 1 ])
85
85
self .buffer = self .buffer [sent :]
86
86
self .oktosend = False
87
87
def cmd (self , data , cb = None ):
@@ -124,7 +124,7 @@ def wii_rel(self, v, axis):
124
124
def socket_disconnect (self ):
125
125
if self .wm is not None :
126
126
#self.wm.led = cwiid.LED2_ON | cwiid.LED3_ON
127
- print "About to close connection to the Wiimote"
127
+ logger . info ( "About to close connection to the Wiimote" )
128
128
self .wm .close ()
129
129
self .wm = None
130
130
return
@@ -133,7 +133,7 @@ def rumble(self,delay=0.2): # rumble unit - default = 0.2 seconds
133
133
time .sleep (delay )
134
134
self .wm .rumble = 0
135
135
def wmconnect (self ):
136
- print "Please open Mythfrontend and then press 1&2 on the wiimote..."
136
+ logger . info ( "Please open Mythfrontend and then press 1&2 on the wiimote..." )
137
137
try :
138
138
self .wm = cwiid .Wiimote ()
139
139
except :
@@ -144,7 +144,7 @@ def wmconnect(self):
144
144
return None
145
145
if self .ms is None :
146
146
self .ms = MythSocket (self , self .host )
147
- print "Connected to a wiimote :)"
147
+ logger . info ( "Connected to a wiimote" )
148
148
self .lastaction = time .time ()
149
149
self .rumble ()
150
150
# Wiimote calibration data (cache this)
@@ -156,7 +156,7 @@ def wmcb(self, messages, extra=None):
156
156
if message [0 ] == cwiid .MESG_BTN :
157
157
state ["buttons" ] = message [1 ]
158
158
#elif message[0] == cwiid.MESG_STATUS:
159
- # print "\nStatus: ", message[1]
159
+ # logger.debug( "\nStatus: ", message[1])
160
160
elif message [0 ] == cwiid .MESG_ERROR :
161
161
if message [1 ] == cwiid .ERROR_DISCONNECT :
162
162
self .wm = None
@@ -165,11 +165,11 @@ def wmcb(self, messages, extra=None):
165
165
self .ms = None
166
166
continue
167
167
else :
168
- print "ERROR: " , message [1 ]
168
+ logger . error ( message [1 ])
169
169
elif message [0 ] == cwiid .MESG_ACC :
170
170
state ["acc" ] = message [1 ]
171
171
else :
172
- print "Unknown message!" , message
172
+ logger . warn ( "Unknown message! %s" % message )
173
173
laststate = self .laststate
174
174
if ('buttons' in laststate ) and (laststate ['buttons' ] <> state ['buttons' ]):
175
175
if state ['buttons' ] == 0 :
@@ -216,7 +216,7 @@ def wmcb(self, messages, extra=None):
216
216
if (X > 0 ): roll += 3.14159
217
217
else : roll -= 3.14159
218
218
pitch = atan (Y / Z * cos (roll ))
219
- #print "X: %f, Y: %f, Z: %f; R: %f, P: %f; B: %d \r" % (X, Y, Z, roll, pitch, state["buttons"]),
219
+ #logger.debug( "X: %f, Y: %f, Z: %f; R: %f, P: %f; B: %d \r" % (X, Y, Z, roll, pitch, state["buttons"]) ),
220
220
sys .stdout .flush ()
221
221
if state ["buttons" ] & cwiid .BTN_B and state ["buttons" ] & cwiid .BTN_LEFT :
222
222
self .ms .cmd ('play seek beginning' )
@@ -256,7 +256,7 @@ def wmcb(self, messages, extra=None):
256
256
cmd += "key ,\n "
257
257
if speed <> 0 :
258
258
cmd += "key " + str (abs (speed )- 1 )+ "\n "
259
- #print cmd
259
+ #logger.debug( cmd)
260
260
elif laststate ['BTN_B' ]<> speed :
261
261
self .rumble (.05 )
262
262
if speed == 0 :
@@ -297,20 +297,20 @@ def main(self):
297
297
try :
298
298
subprocess .call (['xset dpms force on' .split (' ' )])
299
299
except :
300
- print "Subrocess failed to call xset"
300
+ logger . error ( "Subrocess failed to call xset" )
301
301
else :
302
- print "Forcing on the display"
302
+ logger . info ( "Forcing on the display" )
303
303
else :
304
- print "Retrying... "
305
- print
304
+ logger . warn ( "Retrying..." )
305
+
306
306
asyncore .loop (timeout = 0 , count = 1 )
307
307
if self .lastaction < time .time () - 2100 :
308
308
#2100 seconds is 35 minutes
309
309
#1200 seconds is 20 minutes
310
310
self .socket_disconnect ()
311
- print "35 minutes has passed since last action, disconnecting Wiimote"
311
+ logger . info ( "35 minutes has passed since last action, disconnecting Wiimote" )
312
312
time .sleep (0.05 )
313
- print "Exited Safely"
313
+ logger . info ( "Exited Safely" )
314
314
315
315
316
316
def readcfg (config ):
@@ -348,6 +348,12 @@ def readcfg(config):
348
348
dest = "config" , default = defaults ['config' ])
349
349
(options , args ) = parser .parse_args ()
350
350
351
+ logger .setLevel (logging .DEBUG )
352
+ handler = logging .StreamHandler ()
353
+ formatter = logging .Formatter ("%(asctime)s - [%(levelname)s] %(funcName)s:%(lineno)s[%(process)d] - %(message)s" )
354
+ handler .setFormatter (formatter )
355
+ logger .addHandler (handler )
356
+
351
357
myth_buttons = readcfg (options .config )
352
358
inst = WiiMyth (options .mythtv , myth_buttons )
353
359
inst .main ()
0 commit comments