]> Pileus Git - ~andy/csm213a-hw/blobdiff - vis/logger.py
Split Xively streams and debug upload
[~andy/csm213a-hw] / vis / logger.py
index a1cccf7624ab82e464b704506493c66b501d9d72..80cbd3af8a07c5232412d9ad759a084ecaf53f1c 100644 (file)
@@ -41,39 +41,49 @@ class Logger:
 
        # Private methods
        def flush(self):
-               def isset(state, key):
-                       value = getattr(state, key)
-                       return any([x!=None for x in value])
-               def get(items, key):
-                       points = [ Datapoint(s.time, getattr(s,key)) \
-                               for s in items if isset(s,key) ]
-                       stream = Datastream(id=key)
-                       stream.datapoints = points
-                       return stream
+               def split(items, limit):
+                       split   = {}
+                       count   = 0
+                       sensors = {'acc': ['x','y','z'],
+                                  'mag': ['x','y','z'],
+                                  'tch': ['p','d'],
+                                  'lgt': [''],
+                                  'a2d': ['0','1','2','3','4','5']}
+                       for state in items:
+                               for sns in sensors:
+                                       data = getattr(state, sns)
+                                       for i in range(len(data)):
+                                               if data[i] == None:
+                                                       continue
+                                               if count >= limit:
+                                                       print('upload: cropping upload')
+                                                       return split
+                                               chan  = sns + sensors[sns][i]
+                                               if not split.has_key(chan):
+                                                       split[chan] = []
+                                               point = Datapoint(state.time, data[i])
+                                               split[chan].append(point)
+                                               count += 1
+                       return split
+
 
                if not self.running() or len(self.queue) == 0:
                        return
 
-               # Limit data to 500 samples per upload
-               limit  = 500
-               upload = self.queue
-               if len(upload) > limit:
-                       print("upload: cropping - %d -> %d" %
-                                       (len(upload), limit))
-                       upload = upload[0:limit-1]
+               # Split data and limit to 500 points per upload
+               upload = split(self.queue, 500)
+
+               # Create datastreams
+               self.feed.datastreams = \
+                       [ Datastream(id=chan, datapoints=upload[chan])
+                         for chan in upload.keys() ]
 
-               self.feed.datastreams = [
-                       get(upload, 'acc'),
-                       get(upload, 'mag'),
-                       get(upload, 'tch'),
-                       get(upload, 'lgt'),
-                       get(upload, 'a2d'),
-               ]
+               # Clear queue
+               self.last  = datetime.utcnow()
+               self.queue = []
 
+               # Upload it
                try:
                        self.feed.update()
                except Exception as ex:
                        return str(ex)
-
-               self.last  = datetime.utcnow()
-               self.queue = []