diff service/piNode/devices.py @ 1392:83043957c809

pinode devs can now poll in parallel (within one poll step). doesn't help much. Ignore-this: 5a96906a904d5e23100eaaf628fbe6 darcs-hash:b9dbf8e868edabf0020dc31ba74c67685529655d
author drewp <drewp@bigasterisk.com>
date Sat, 06 Jul 2019 00:47:18 -0700
parents e65d7b62364f
children 1249dcc657b8
line wrap: on
line diff
--- a/service/piNode/devices.py	Thu Jun 06 02:30:00 2019 -0700
+++ b/service/piNode/devices.py	Sat Jul 06 00:47:18 2019 -0700
@@ -5,7 +5,9 @@
 
 import time, logging, os
 from rdflib import Namespace, URIRef, Literal
-from twisted.internet import reactor
+from twisted.internet import reactor, threads
+from twisted.internet.defer import inlineCallbacks, returnValue
+from greplin import scales
 
 from devices_shared import RgbPixelsAnimation
 
@@ -375,21 +377,22 @@
             # differently or what.
             s.uri = URIRef(os.path.join(self.uri, 'dev-%s' % s.id))
             log.info('  found temperature sensor %s' % s.uri)
-        
+
+    @inlineCallbacks
     def poll(self):
         try:
             stmts = []
             for sensor in self._sensors:
                 stmts.append((self.uri, ROOM['connectedTo'], sensor.uri))
                 try:
-                    tempF = sensor.get_temperature(sensor.DEGREES_F)
+                    tempF = yield threads.deferToThread(sensor.get_temperature, sensor.DEGREES_F)
                     stmts.append((sensor.uri, ROOM['temperatureF'],
                                   # see round() note in arduinoNode/devices.py
                                   Literal(round(tempF, 2))))
                 except (self.SensorNotReadyError, self.ResetValueError) as e:
                     log.warning(e)
 
-            return stmts
+            returnValue(stmts)
         except Exception as e:
             log.error(e)
             os.abort()
@@ -513,6 +516,12 @@
         return [(self.uri, ROOM['temperatureF']),
                 ]
 
+pixelStats = scales.collection('/rgbPixels',
+                               scales.PmfStat('updateOutput'),
+                               scales.PmfStat('currentColors'),
+                               scales.PmfStat('poll'),
+                               )
+    
 @register
 class RgbPixels(DeviceType):
     """chain of ws2812 rgb pixels on pin GPIO18"""
@@ -529,18 +538,24 @@
     def sendOutput(self, statements):
         self.anim.onStatements(statements)
 
+    @pixelStats.updateOutput.time()
     def updateOutput(self):
         if 0:
             for _, _, sg in self.anim.groups.values():
                 print sg.uri, sg.current
             print list(self.anim.currentColors())
             return
+
+        with pixelStats.currentColors.time():
+            colors = self.anim.currentColors()
         
-        for idx, (r, g, b) in self.anim.currentColors():
-            log.debug('out color %s (%s,%s,%s)', idx, r, g, b)
+        for idx, (r, g, b) in colors:
+            if idx < 4:
+                log.debug('out color %s (%s,%s,%s)', idx, r, g, b)
             self.neo.setPixelColorRGB(idx, r, g, b)
         self.neo.show()
 
+    @pixelStats.poll.time()
     def poll(self):
         self.anim.step()
         return []