view bin/collector @ 1375:ca5b10d7ecd1

switch N3 to an updated pull/61 version Ignore-this: f2f226d216d70404c2aeda3e16dc3d77
author Drew Perttula <drewp@bigasterisk.com>
date Wed, 08 Jun 2016 05:24:06 +0000
parents f427801da9f6
children ab7b40d20af0
line wrap: on
line source

#!bin/python
from __future__ import division
from rdflib import Graph, URIRef, Literal
from twisted.internet import reactor
from twisted.web.server import Site
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPullConnection
import json
import logging
import klein
import optparse
from greplin import scales
from greplin.scales.twistedweb import StatsResource

from run_local import log
from light9.collector.output import EnttecDmx, Udmx
from light9.collector.collector import Collector
from light9.namespaces import L9
from light9 import networking
from light9.rdfdb.syncedgraph import SyncedGraph
from light9.rdfdb import clientsession

class WebServer(object):
    stats = scales.collection('/webServer',
                              scales.PmfStat('setAttr'))
    app = klein.Klein()
    def __init__(self, collector):
        self.collector = collector
        
    @app.route('/attrs', methods=['PUT'])
    def putAttrs(self, request):
        with WebServer.stats.setAttr.time():
            body = json.load(request.content)
            settings = []
            for device, attr, value in body['settings']:
                settings.append((URIRef(device), URIRef(attr), Literal(value)))
            self.collector.setAttrs(body['client'],
                                    body['clientSession'],
                                    settings)
            request.setResponseCode(202)

    @app.route('/stats', methods=['GET'])
    def getStats(self, request):
        return StatsResource('collector')
        
def startZmq(port, collector):
    stats = scales.collection('/zmqServer',
                              scales.PmfStat('setAttr'))
    
    zf = ZmqFactory()
    e = ZmqEndpoint('bind', 'tcp://*:%s' % port)
    s = ZmqPullConnection(zf, e)
    def onPull(message):
        with stats.setAttrZmq.time():
            # todo: new compressed protocol where you send all URIs up
            # front and then use small ints to refer to devices and
            # attributes in subsequent requests.
            message[0]
            collector.setAttrs()
    s.onPull = onPull

def launch(graph):

    # todo: drive outputs with config files
    outputs = [EnttecDmx(L9['output/dmx0/'], 100, '/dev/dmx0'),
               Udmx(L9['output/udmx/'], 100)]
    c = Collector(graph, outputs)

    server = WebServer(c)
    startZmq(networking.collectorZmq.port, c)
    
    reactor.listenTCP(networking.collector.port,
                      Site(server.app.resource()),
                      interface='::')
    log.info('serving http on %s, zmq on %s', networking.collector.port,
             networking.collectorZmq.port)
    
    
def main():
    parser = optparse.OptionParser()
    parser.add_option("-v", "--verbose", action="store_true",
                      help="logging.DEBUG")
    (options, args) = parser.parse_args()
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)

    graph = SyncedGraph(networking.rdfdb.url, "collector")

    graph.initiallySynced.addCallback(lambda _: launch(graph))
    reactor.run()

if __name__ == '__main__':
    main()