Changeset - 092967f313e1
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-18 02:56:17
drewp@bigasterisk.com
attempt to make things more typesafe and readable (untested)
1 file changed with 28 insertions and 12 deletions:
0 comments (0 inline, 0 general)
light9/collector/collector.py
Show inline comments
 
import logging
 
import time
 
from typing import cast, List, Dict, Tuple, Optional, Set
 
from typing import Dict, List, Set, Tuple, Type, TypeVar, cast
 

	
 
from rdflib import Graph, Literal
 
from rdfdb.syncedgraph.syncedgraph import SyncedGraph
 
from rdflib import Literal, URIRef
 

	
 
from light9.collector.device import resolve, toOutputAttrs
 
from light9.collector.output import Output as OutputInstance
 
from light9.collector.weblisteners import WebListeners
 
from light9.namespaces import L9, RDF
 
from light9.newtypes import (ClientSessionType, ClientType, DeviceAttr, DeviceClass, DeviceUri, DmxIndex, DmxMessageIndex, OutputAttr, OutputRange, OutputUri,
 
                             OutputValue, UnixTime)
 

	
 
log = logging.getLogger('collector')
 

	
 

	
 
def makeDmxMessageIndex(base: DmxIndex, offset: DmxIndex) -> DmxMessageIndex:
 
    return DmxMessageIndex(base + offset - 1)
 

	
 

	
 
ObjType = TypeVar('ObjType')
 

	
 

	
 
def typedValue(objType: Type[ObjType], graph, subj, pred) -> ObjType:
 
    obj = graph.value(subj, pred)
 
    if obj is None:
 
        raise ValueError()
 
    conv = obj if issubclass(objType, URIRef) else obj.toPython()
 
    return cast(objType, conv)
 

	
 

	
 
def outputMap(graph: SyncedGraph, outputs: List[OutputInstance]) -> Dict[Tuple[DeviceUri, OutputAttr], Tuple[OutputInstance, DmxMessageIndex]]:
 
    """From rdf config graph, compute a map of
 
       (device, outputattr) : (output, index)
 
    that explains which output index to set for any device update.
 
    """
 
    ret = {}
 
@@ -28,26 +43,27 @@ def outputMap(graph: SyncedGraph, output
 

	
 
    for dc in graph.subjects(RDF.type, L9['DeviceClass']):
 
        log.info('mapping DeviceClass %s', dc)
 
        for dev in graph.subjects(RDF.type, dc):
 
            dev = cast(DeviceUri, dev)
 
            log.info('  mapping device %s', dev)
 
            universe = cast(OutputUri, graph.value(dev, L9['dmxUniverse']))
 
            universe = typedValue(OutputUri, graph, dev, L9['dmxUniverse'])
 
            try:
 
                output = outputByUri[universe]
 
            except Exception:
 
                log.warn('dev %r :dmxUniverse %r', dev, universe)
 
                raise
 
            base = graph.value(dev, L9['dmxBase'])
 
            if base is None:
 
            try:
 
                dmxBase = typedValue(DmxIndex, graph, dev, L9['dmxBase'])
 
            except ValueError:
 
                raise ValueError('no :dmxBase for %s' % dev)
 
            dmxBase = DmxIndex(cast(Literal, base).toPython())
 

	
 
            for row in graph.objects(dc, L9['attr']):
 
                outputAttr = cast(OutputAttr, graph.value(row, L9['outputAttr']))
 
                offset = DmxIndex(cast(Literal, graph.value(row, L9['dmxOffset'])).toPython())
 
                index = DmxMessageIndex(dmxBase + offset - 1)
 
                outputAttr = typedValue(OutputAttr, graph, row, L9['outputAttr'])
 
                offset = typedValue(DmxIndex, graph, row, L9['dmxOffset'])
 
                index = makeDmxMessageIndex(dmxBase, offset)
 
                ret[(dev, outputAttr)] = (output, index)
 
                log.debug('    map %s to %s,%s', outputAttr, output, index)
 
    return ret
 

	
 

	
 
class Collector:
 
@@ -78,15 +94,15 @@ class Collector:
 
            for dev in self.graph.subjects(RDF.type, dc):
 
                dev = cast(DeviceUri, dev)
 
                self.allDevices.add(dev)
 
                self.deviceType[dev] = dc
 

	
 
                for remap in self.graph.objects(dev, L9['outputAttrRange']):
 
                    attr = OutputAttr(self.graph.value(remap, L9['outputAttr']))
 
                    start = cast(Literal, self.graph.value(remap, L9['start'])).toPython()
 
                    end = cast(Literal, self.graph.value(remap, L9['end'])).toPython()
 
                    attr = typedValue(OutputAttr, self.graph, remap, L9['outputAttr'])
 
                    start = typedValue(float, self.graph, remap, L9['start'])
 
                    end = typedValue(float, self.graph, remap, L9['end'])
 
                    self.remapOut[(dev, attr)] = OutputRange((start, end))
 

	
 
    def _forgetStaleClients(self, now):
 
        # type: (float) -> None
 
        staleClientSessions = []
 
        for c, (t, _) in self.lastRequest.items():
0 comments (0 inline, 0 general)