changeset 1552:0aad247a1168

refactor and fix captureDevice Ignore-this: 6698fdc1670318d0e8eb9e3e5aa9d5cf
author Drew Perttula <drewp@bigasterisk.com>
date Mon, 22 May 2017 04:46:41 +0000
parents 60cc3a504377
children 48609cc508e8
files bin/captureDevice light9/paint/capture.py
diffstat 2 files changed, 59 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/bin/captureDevice	Mon May 22 04:37:28 2017 +0000
+++ b/bin/captureDevice	Mon May 22 04:46:41 2017 +0000
@@ -1,6 +1,6 @@
 #!bin/python
 from __future__ import division
-from rdflib import URIRef, Literal
+from rdflib import URIRef
 from twisted.internet import reactor
 from twisted.internet.defer import inlineCallbacks, Deferred
 
@@ -8,7 +8,6 @@
 import optparse
 import os
 import time
-import traceback
 import treq
 import cyclone.web, cyclone.websocket, cyclone.httpclient
 from greplin import scales
@@ -26,12 +25,6 @@
 
 stats = scales.collection('/webServer', scales.PmfStat('setAttr'))
 
-class Attrs(PrettyErrorHandler, cyclone.web.RequestHandler):
-    def put(self):
-        with stats.setAttr.time():
-            client, clientSession, settings, sendTime = parseJsonMessage(self.request.body)
-            self.set_status(202)
-
 
 class Camera(object):
     def __init__(self, imageUrl):
@@ -39,7 +32,8 @@
     
     def takePic(self, uri, writePath):
         log.info('takePic %s', uri)
-        return treq.get(self.imageUrl).addCallbacks(lambda r: self._done(writePath, r), log.error)
+        return treq.get(self.imageUrl).addCallbacks(lambda r: self._done(writePath, r),
+                                                    log.error)
         
     @inlineCallbacks
     def _done(self, writePath, response):
@@ -53,65 +47,81 @@
         log.info('wrote %s', writePath)
 
 
-
-settleTime = .5
-
-camera = Camera('http://dash:8200/picamserve/pic?res=480&resize=480&rotation=180&iso=800&redgain=1.6&bluegain=2&shutter=60000')
+class Capture(object):
+    settleTime = .5
+    def __init__(self, graph):
+        self.graph = graph
+        
+        def steps(a, b, n):
+            return [round(a + (b - a) * i / n, 5) for i in range(n)]
 
-def launch(graph):
-
-    def steps(a, b, n):
-        return [round(a + (b - a) * i / n, 5) for i in range(n)]
-
-    startTime = time.time()
-    toGather = []
+        startTime = time.time()
+        self.captureId = 'cap%s' % (int(startTime) - 1495170000)
+        self.toGather = []
 
-    row = 0
-    for ry in steps(0.85, .92, 6):
-        xSteps = steps(.24, .45, 12)
-        if row % 2:
-            xSteps.reverse()
-        row += 1
-        for rx in xSteps:
-            toGather.append(DeviceSettings(graph, [
-                (L9['device/moving1'], L9['rx'], rx),
-                (L9['device/moving1'], L9['ry'], ry),
-            ]))
+        row = 0
+        for ry in steps(0.85, .92, 6):
+            xSteps = steps(.24, .45, 12)
+            if row % 2:
+                xSteps.reverse()
+            row += 1
+            for rx in xSteps:
+                self.toGather.append(DeviceSettings(graph, [
+                    (L9['device/moving1'], L9['rx'], rx),
+                    (L9['device/moving1'], L9['ry'], ry),
+                ]))
 
-    numPics = [0]
-    settingsCache = set()
+        self.numPics = 0
+        self.settingsCache = set()
+        self.step().addErrback(log.error)
+        
     @inlineCallbacks
-    def step():
-        if not toGather:
+    def step(self):
+        if not self.toGather:
             reactor.stop()
             return
-        settings = toGather.pop()
+        settings = self.toGather.pop()
         
         log.info('move to %r', settings)
         yield sendToCollector(client='captureDevice', session='main', settings=settings)
         
         d = Deferred()
-        reactor.callLater(settleTime, d.callback, None)
+        reactor.callLater(self.settleTime, d.callback, None)
         yield d
         dev = settings.devices()[0]
 
         devTail = dev.rsplit('/')[-1]
-        captureId = 'cap%s' % (int(startTime) - 1495170000)
-        picId = 'pic%s' % numPics[0]
-        path = '/'.join(['capture', devTail, captureId, picId])
-        ctx = URIRef('/'.join([showconfig.showUri(), 'capture', devTail, captureId, 'index']))
-        uri = URIRef('/'.join([showconfig.showUri(), 'capture', devTail, captureId, picId]))
+        
+        picId = 'pic%s' % self.numPics
+        path = '/'.join(['capture', devTail, self.captureId, picId])
+        ctx = URIRef('/'.join([showconfig.showUri(), 'capture', devTail, self.captureId,
+                               'index']))
+        uri = URIRef('/'.join([showconfig.showUri(), 'capture', devTail, self.captureId,
+                               picId]))
                      
         relOutPath = path + '.jpg'
         
         yield camera.takePic(uri, os.path.join(showconfig.root(), relOutPath))
-        numPics[0] += 1
+        self.numPics += 1
 
-        writeCaptureDescription(graph, ctx, uri, dev, relOutPath, settingsCache, settings)
+        writeCaptureDescription(self.graph, ctx, uri, dev, relOutPath,
+                                self.settingsCache, settings)
+        
+        reactor.callLater(0, self.step)
+
         
-        reactor.callLater(0, step)
-    step().addErrback(log.error)
-    
+
+camera = Camera('http://dash:8200/picamserve/pic?res=480&resize=480&rotation=180&iso=800&redgain=1.6&bluegain=2&shutter=60000')
+
+class Attrs(PrettyErrorHandler, cyclone.web.RequestHandler):
+    def put(self):
+        with stats.setAttr.time():
+            client, clientSession, settings, sendTime = parseJsonMessage(self.request.body)
+            self.set_status(202)
+
+def launch(graph):
+
+    cap = Capture(graph)
     reactor.listenTCP(networking.captureDevice.port,
                       cyclone.web.Application(handlers=[
                           (r'/()', cyclone.web.StaticFileHandler,
--- a/light9/paint/capture.py	Mon May 22 04:37:28 2017 +0000
+++ b/light9/paint/capture.py	Mon May 22 04:46:41 2017 +0000
@@ -3,7 +3,7 @@
 from light9 import showconfig
 from light9.rdfdb.patch import Patch
 from light9.namespaces import L9
-from light9.paint.solve import loadNumPy
+from light9.paint.solve import loadNumpy
 
 def writeCaptureDescription(graph, ctx, uri, dev, relOutPath, settingsSubgraphCache, settings):
     graph.patch(Patch(addQuads=settings.statements(
@@ -24,7 +24,7 @@
         if not ip.startswith(showconfig.show()):
             raise ValueError(repr(ip))
         diskPath = os.path.join(showconfig.root(), ip[len(self.show):])
-        return loadNumPy(diskPath, thumb)
+        return loadNumpy(diskPath, thumb)
         
     def devices(self):
         """devices for which we have any captured data"""