Changeset - b370618ce723
[Not reviewed]
default
0 1 2
Drew Perttula - 11 years ago 2014-05-30 06:20:01
drewp@bigasterisk.com
split EffectNode out of effecteval
Ignore-this: 38003ca0e42aca8aedff2709e5f44f2b
3 files changed with 57 insertions and 50 deletions:
0 comments (0 inline, 0 general)
bin/effecteval
Show inline comments
 
#!bin/python
 
from run_local import log
 
from twisted.internet import reactor, task
 
from twisted.internet.defer import inlineCallbacks
 
import cyclone.web, cyclone.websocket, cyclone.httpclient
 
import sys, optparse, logging, subprocess, json, re, time, traceback
 
import sys, optparse, logging, subprocess, json, time, traceback
 
from rdflib import URIRef, Literal
 

	
 
sys.path.append(".")
 
from light9 import networking, showconfig, Submaster, dmxclient
 
from light9.rdfdb.syncedgraph import SyncedGraph
 
from light9.curvecalc.curve import Curve
 
from light9.namespaces import L9, DCTERMS, RDF, RDFS
 
from light9.namespaces import L9, RDF, RDFS
 
from light9.rdfdb.patch import Patch
 
from light9.effecteval.effect import EffectNode
 

	
 
sys.path.append("/my/proj/homeauto/lib")
 
sys.path.append("/home/drewp/projects/homeauto/lib")
 
from cycloneerr import PrettyErrorHandler
 

	
 
class EffectEdit(cyclone.web.RequestHandler):
 
    def get(self):
 
        self.write(open("light9/effecteval/effect.html").read())
 

	
 
class SongEffects(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def post(self):
 
        song = URIRef(self.get_argument('uri'))
 
@@ -77,71 +77,24 @@ class EffectUpdates(cyclone.websocket.We
 
    def updateClient(self):
 
        # todo: if client has dropped, abort and don't get any more
 
        # graph updates
 
        self.sendMessage({'code': self.graph.value(self.uri, L9['code'])})
 
        
 
    def connectionLost(self, reason):
 
        log.info("websocket closed")
 

	
 
    def messageReceived(self, message):
 
        log.info("got message %s" % message)
 
        # write a patch back to the graph
 

	
 
def uriFromCode(s):
 
    # i thought this was something a graph could do with its namespace manager
 
    if s.startswith('sub:'):
 
        return URIRef('http://light9.bigasterisk.com/show/dance2014/sub/' + s[4:])
 
    if s.startswith('song1:'):
 
        return URIRef('http://ex/effect/song1/' + s[6:])
 
    if (s[0], s[-1]) == ('<', '>'):
 
        return URIRef(s[1:-1])
 
    raise NotImplementedError
 
        
 
class EffectNode(object):
 
    def __init__(self, graph, uri):
 
        self.graph, self.uri = graph, uri
 
        # this is not expiring at the right time, when an effect goes away
 
        self.graph.addHandler(self.prepare)
 

	
 
    def prepare(self):
 
        self.code = self.graph.value(self.uri, L9['code'])
 
        if self.code is None:
 
            raise ValueError("effect %s has no code" % self.uri)
 
        m = re.match(r'^out = sub\((.*?), intensity=(.*?)\)', self.code)
 
        if not m:
 
            raise NotImplementedError
 
        subUri = uriFromCode(m.group(1))
 
        subs = Submaster.get_global_submasters(self.graph)
 
        self.sub = subs.get_sub_by_uri(subUri)
 
        
 
        intensityCurve = uriFromCode(m.group(2))
 
        self.curve = Curve(uri=intensityCurve)
 

	
 
        # read from disk ok? how do we know to reread? start with
 
        # mtime. the mtime check could be done occasionally so on
 
        # average we read at most one curve's mtime per effectLoop.       
 

	
 
        pts = self.graph.value(intensityCurve, L9['points'])
 
        if pts is None:
 
            log.info("curve %r has no points" % intensityCurve)
 
        else:
 
            self.curve.set_from_string(pts)
 

	
 
        
 
    def eval(self, songTime):
 
        # consider http://waxeye.org/ for a parser that can be used in py and js
 
        level = self.curve.eval(songTime)
 
        scaledSubs = self.sub * level
 
        return scaledSubs
 

	
 
        
 
class EffectEval(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    @inlineCallbacks
 
    def get(self):
 
        # return dmx list for that effect
 
        uri = URIRef(self.get_argument('uri'))
 
        response = yield cyclone.httpclient.fetch(
 
            networking.musicPlayer.path('time'))
 
        songTime = json.loads(response.body)['t']
 

	
 
        node = EffectNode(self.settings.graph, uri)
 
        outSub = node.eval(songTime)
light9/effecteval/__init__.py
Show inline comments
 
new file 100644
 

	
light9/effecteval/effect.py
Show inline comments
 
new file 100644
 
from run_local import log
 
import re
 
from rdflib import URIRef
 
from light9.namespaces import L9
 
from light9.curvecalc.curve import Curve
 
from light9 import Submaster
 

	
 
def uriFromCode(s):
 
    # i thought this was something a graph could do with its namespace manager
 
    if s.startswith('sub:'):
 
        return URIRef('http://light9.bigasterisk.com/show/dance2014/sub/' + s[4:])
 
    if s.startswith('song1:'):
 
        return URIRef('http://ex/effect/song1/' + s[6:])
 
    if (s[0], s[-1]) == ('<', '>'):
 
        return URIRef(s[1:-1])
 
    raise NotImplementedError
 

	
 
class EffectNode(object):
 
    def __init__(self, graph, uri):
 
        self.graph, self.uri = graph, uri
 
        # this is not expiring at the right time, when an effect goes away
 
        self.graph.addHandler(self.prepare)
 

	
 
    def prepare(self):
 
        self.code = self.graph.value(self.uri, L9['code'])
 
        if self.code is None:
 
            raise ValueError("effect %s has no code" % self.uri)
 
        m = re.match(r'^out = sub\((.*?), intensity=(.*?)\)', self.code)
 
        if not m:
 
            raise NotImplementedError
 
        subUri = uriFromCode(m.group(1))
 
        subs = Submaster.get_global_submasters(self.graph)
 
        self.sub = subs.get_sub_by_uri(subUri)
 
        
 
        intensityCurve = uriFromCode(m.group(2))
 
        self.curve = Curve(uri=intensityCurve)
 

	
 
        # read from disk ok? how do we know to reread? start with
 
        # mtime. the mtime check could be done occasionally so on
 
        # average we read at most one curve's mtime per effectLoop.       
 

	
 
        pts = self.graph.value(intensityCurve, L9['points'])
 
        if pts is None:
 
            log.info("curve %r has no points" % intensityCurve)
 
        else:
 
            self.curve.set_from_string(pts)
 

	
 
        
 
    def eval(self, songTime):
 
        # consider http://waxeye.org/ for a parser that can be used in py and js
 
        level = self.curve.eval(songTime)
 
        scaledSubs = self.sub * level
 
        return scaledSubs
0 comments (0 inline, 0 general)