Changeset - cce016abe31e
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 6 years ago 2019-06-04 08:13:15
drewp@bigasterisk.com
sort of revive musictime. drop curvecalc time polling.
Ignore-this: 8f2e1f1624f86eb6231321a7fbaeea1f
2 files changed with 15 insertions and 69 deletions:
0 comments (0 inline, 0 general)
bin/musictime
Show inline comments
 
#!/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 @@ class MusicTimeTk(tk.Frame, MusicTime):
 
        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 @@ if __name__ == "__main__":
 
    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()
 

	
light9/ascoltami/musictime_client.py
Show inline comments
 
@@ -19,7 +19,7 @@ class MusicTime(object):
 
    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 @@ class MusicTime(object):
 
        # 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 @@ class MusicTime(object):
 
        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(
0 comments (0 inline, 0 general)