changeset 1126:63ab21eb7bbf

if effecteval can't get music time, coast some more Ignore-this: c1638d7dcdc7f5265c5e28527c01a57d
author Drew Perttula <drewp@bigasterisk.com>
date Fri, 13 Jun 2014 07:59:05 +0000
parents 57eb333e6a4c
children b0b9b4616e16
files light9/effecteval/effectloop.py
diffstat 1 files changed, 21 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/light9/effecteval/effectloop.py	Fri Jun 13 07:51:05 2014 +0000
+++ b/light9/effecteval/effectloop.py	Fri Jun 13 07:59:05 2014 +0000
@@ -3,7 +3,7 @@
 import numpy
 import serial
 from twisted.internet import reactor
-from twisted.internet.defer import inlineCallbacks, returnValue, succeed
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed, TimeoutError
 from rdflib import URIRef, Literal
 import cyclone.httpclient
 from light9.namespaces import L9, RDF, RDFS
@@ -50,19 +50,25 @@
     @inlineCallbacks
     def getSongTime(self):
         now = time.time()
-        if now - self.requestTime < self.coastSecs:
-            estimated = self.songTimeFromRequest
-            if self.currentSong is not None and self.currentPlaying:
-                estimated += now - self.requestTime
-            returnValue((estimated, self.currentSong))
-        else:
-            response = json.loads((yield cyclone.httpclient.fetch(
-                networking.musicPlayer.path('time'))).body)
-            self.requestTime = now
-            self.currentPlaying = response['playing']
-            self.songTimeFromRequest = response['t']
-            returnValue(
-                (response['t'], (response['song'] and URIRef(response['song']))))
+
+        if now - self.requestTime > self.coastSecs:
+            try:
+                response = json.loads((yield cyclone.httpclient.fetch(
+                    networking.musicPlayer.path('time'), timeout=.5)).body)
+            except TimeoutError as e:
+                log.warning("%r, using stale time", e)
+            else:
+                self.requestTime = now
+                self.currentPlaying = response['playing']
+                self.songTimeFromRequest = response['t']
+                returnValue(
+                    (response['t'], (response['song'] and URIRef(response['song']))))
+
+        estimated = self.songTimeFromRequest
+        if self.currentSong is not None and self.currentPlaying:
+            estimated += now - self.requestTime
+        returnValue((estimated, self.currentSong))
+
             
     @inlineCallbacks
     def sendLevels(self):
@@ -92,7 +98,7 @@
         except Exception:
             self.stats.errors += 1
             traceback.print_exc()
-            dt = 1
+            dt = .5
 
         reactor.callLater(dt, self.sendLevels)