changeset 1940:cce016abe31e

sort of revive musictime. drop curvecalc time polling. Ignore-this: 8f2e1f1624f86eb6231321a7fbaeea1f
author drewp@bigasterisk.com
date Tue, 04 Jun 2019 08:13:15 +0000
parents 6f49dc917aa3
children 0dc3715050cf
files bin/musictime light9/ascoltami/musictime_client.py
diffstat 2 files changed, 15 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/bin/musictime	Mon Jun 03 09:50:29 2019 +0000
+++ b/bin/musictime	Tue Jun 04 08:13:15 2019 +0000
@@ -1,28 +1,15 @@
-#!/usr/bin/env python
+#!bin/python
 import run_local  # noqa
 import light9.networking
 
 import tkinter as tk
 import time
-import restkit, jsonlib
-
-
-class MusicTime:
-
-    def __init__(self, url):
-        self.player = restkit.Resource(url)
+import json
+from twisted.internet import reactor, tksupport, task, defer
 
-    def get_music_time(self):
-        playtime = None
-        while not playtime:
-            try:
-                playtime = jsonlib.read(self.player.get("time").body_string(),
-                                        use_float=True)['t']
-            except restkit.RequestError as e:
-                print("Server error %s, waiting" % e)
-                time.sleep(2)
-        return playtime
+from light9.ascoltami.musictime_client import MusicTime
 
+mt = MusicTime()
 
 class MusicTimeTk(tk.Frame, MusicTime):
 
@@ -41,18 +28,17 @@
         self.timelabel.pack(expand=1, fill='both')
 
         def print_time(evt, *args):
-            self.timevar.set(self.get_music_time())
+            self.timevar.set(mt.getLatest().get('t', 0))
             print(self.timevar.get(), evt.keysym)
 
         self.timelabel.bind('<KeyPress>', print_time)
         self.timelabel.bind('<1>', print_time)
         self.timelabel.focus()
-        self.update_time()
-
+        task.LoopingCall(self.update_time).start(.1)
+        
     def update_time(self):
-        self.timevar.set(self.get_music_time())
-        self.after(100, self.update_time)
-
+        t = self.getLatest().get('t', 0)
+        self.timevar.set(t)
 
 if __name__ == "__main__":
     from optparse import OptionParser
@@ -63,7 +49,6 @@
     root = tk.Tk()
     root.title("Time")
     MusicTimeTk(root, options.url).pack(expand=1, fill='both')
-    try:
-        tk.mainloop()
-    except KeyboardInterrupt:
-        root.destroy()
+    tksupport.install(root, ms=20)
+    reactor.run()
+
--- a/light9/ascoltami/musictime_client.py	Mon Jun 03 09:50:29 2019 +0000
+++ b/light9/ascoltami/musictime_client.py	Tue Jun 04 08:13:15 2019 +0000
@@ -19,7 +19,7 @@
     def __init__(self,
                  period=.2,
                  onChange=lambda position: None,
-                 pollCurvecalc=True):
+                 pollCurvecalc='ignored'):
         """period is the seconds between http time requests.
 
         We call onChange with the time in seconds and the total time
@@ -37,10 +37,8 @@
         # driven by our pollCurvecalcTime and also by Gui.incomingTime
         self.lastHoverTime = None  # None means "no recent value"
         self.pollMusicTime()
-        if pollCurvecalc:
-            self.pollCurvecalcTime()
 
-    def getLatest(self, frameTime=None):
+    def getLatest(self, frameTime=None) -> Dict:
         """
         dict with 't' and 'song', etc.
 
@@ -90,43 +88,6 @@
         d.addCallback(cb)
         d.addErrback(eb)  # note this includes errors in cb()
 
-    def pollCurvecalcTime(self):
-        """
-        poll the curvecalc position when music isn't playing, so replay
-        can track it.
-
-        This would be better done via rdfdb sync, where changes to the
-        curvecalc position are written to the curvecalc session and we
-        can pick them up in here
-        """
-        if self.position.get('playing'):
-            # don't need this position during playback
-            self.lastHoverTime = None
-            reactor.callLater(.2, self.pollCurvecalcTime)
-            return
-
-        def cb(response):
-            if response.code == 404:
-                # not hovering
-                self.lastHoverTime = None
-                reactor.callLater(.2, self.pollCurvecalcTime)
-                return
-            if response.code != 200:
-                raise ValueError("%s %s" % (response.code, response.body))
-            self.lastHoverTime = json.loads(response.body)['hoverTime']
-
-            reactor.callLater(self.hoverPeriod, self.pollCurvecalcTime)
-
-        def eb(err):
-            if self.lastHoverTime:
-                log.warn("talking to curveCalc: %s", err.getErrorMessage())
-            self.lastHoverTime = None
-            reactor.callLater(2, self.pollCurvecalcTime)
-
-        d = treq.get(networking.curveCalc.path("hoverTime"))
-        d.addCallback(cb)
-        d.addErrback(eb)  # note this includes errors in cb()
-
     def sendTime(self, t):
         """request that the player go to this time"""
         treq.post(