From ec741158211e0fd11047c8efd9d6f7e8588b1544 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 3 Feb 2014 07:55:44 +0000 Subject: [PATCH] Add Xively upload - Also update some settings - Also disconnect on exit --- vis/config.py | 2 +- vis/device.py | 10 +++++--- vis/logger.py | 60 ++++++++++++++++++++++++++++++++++++++++++- vis/main.py | 2 ++ vis/visual.py | 29 ++++++++++----------- vis/visual.ui | 70 ++++++++++++++++++++++++++++++--------------------- 6 files changed, 125 insertions(+), 48 deletions(-) diff --git a/vis/config.py b/vis/config.py index 50c5f77..928ae7d 100644 --- a/vis/config.py +++ b/vis/config.py @@ -15,7 +15,7 @@ class Config: 'xively': { 'feedid': '', 'apikey': '', - 'maxrate': '6', + 'maxrate': '10', } } diff --git a/vis/device.py b/vis/device.py index 385a142..ba46787 100644 --- a/vis/device.py +++ b/vis/device.py @@ -1,5 +1,6 @@ -from re import compile -from serial import Serial +from re import compile +from serial import Serial +from datetime import datetime class State: acc = [None]*3 @@ -7,9 +8,10 @@ class State: touch = [None]*2 light = [None]*1 a2d = [None]*6 + time = None def __init__(self): - pass + self.time = datetime.utcnow() class Device: # Attributes @@ -48,6 +50,8 @@ class Device: items = [] count = 0 limit = 100 + if not self.running(): + return items while self.serial.readable(): try: char = self.serial.read().decode() diff --git a/vis/logger.py b/vis/logger.py index ffd7903..81e3491 100644 --- a/vis/logger.py +++ b/vis/logger.py @@ -1,6 +1,64 @@ +from datetime import datetime + +from xively import XivelyAPIClient +from xively import Datastream +from xively import Datapoint + + class Logger: + api = None + feed = None + queue = [] + last = 0 + + # Methods def __init__(self, config): self.config = config + # Methods def connect(self): - pass + self.api = XivelyAPIClient(self.config.apikey) + self.feed = self.api.feeds.get(self.config.feedid) + self.last = datetime.utcnow() + self.queue = [] + + def disconnect(self): + self.api = None + self.feed = None + + def running(self): + if not self.api or not self.feed: + return False + return True + + def append(self, state): + if not self.feed: + return + self.queue.append(state) + delta = datetime.utcnow() - self.last + if delta.total_seconds() > self.config.maxrate: + self.flush() + + # 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 + + self.feed.datastreams = [ + get(self.queue, 'acc'), + get(self.queue, 'mag'), + get(self.queue, 'touch'), + get(self.queue, 'light'), + get(self.queue, 'a2d'), + ] + self.feed.update() + + self.last = datetime.utcnow() + self.queue = [] diff --git a/vis/main.py b/vis/main.py index 00149ad..7ec1551 100755 --- a/vis/main.py +++ b/vis/main.py @@ -14,6 +14,8 @@ class Main: def main(self): self.visual.run() + self.device.disconnect() + self.logger.disconnect() if __name__ == "__main__": main = Main() diff --git a/vis/visual.py b/vis/visual.py index 6d06ba4..bd52be9 100644 --- a/vis/visual.py +++ b/vis/visual.py @@ -117,21 +117,22 @@ class Visual: print('\tlgt - ' + str(state.light)) print('\ttch - ' + str(state.touch)) print('\ta2d - ' + str(state.a2d)) - - def timer(self): - serial = self.builder.get_object("serial_btn") - status = self.builder.get_object("conn") - if self.device.running(): - serial.set_active(True) - status.set_from_stock(Gtk.STOCK_YES, Gtk.IconSize.BUTTON) - else: - serial.set_active(False) - status.set_from_stock(Gtk.STOCK_NO, Gtk.IconSize.BUTTON) - if self.device.running(): - for item in self.device.process(): - self.update(item) - #self.debug(item) + def timer(self): + def check(state, name): + btn = self.builder.get_object(name + '_btn') + con = self.builder.get_object(name + '_con') + img = Gtk.STOCK_YES if state else Gtk.STOCK_NO + btn.set_active(state) + con.set_from_stock(img, Gtk.IconSize.BUTTON) + + check(self.device.running(), "serial") + check(self.logger.running(), "xively") + + for item in self.device.process(): + self.update(item) + self.logger.append(item) + #self.debug(item) return True diff --git a/vis/visual.ui b/vis/visual.ui index f4795b7..ea9b741 100644 --- a/vis/visual.ui +++ b/vis/visual.ui @@ -2,16 +2,6 @@ - - Serial - gtk-connect - - - - Settings - gtk-preferences - - 100 1 @@ -29,6 +19,22 @@ 1 10 + + 100 + 1 + 1 + 10 + + + Serial + gtk-connect + + + + Xively + gtk-connect + + 6 8 @@ -41,18 +47,17 @@ 1 10 - - 100 - 1 - 1 - 10 - 100 1 10 + + Settings + gtk-preferences + + False 5 @@ -163,7 +168,7 @@ True True baud_adj - + 1 @@ -183,7 +188,7 @@ True True stopb_adj - + 1 @@ -203,7 +208,7 @@ True True datab_adj - + 1 @@ -405,7 +410,7 @@ True True rate_adj - + 1 @@ -454,11 +459,6 @@ 1 10 - - Xively - gtk-connect - - False @@ -474,7 +474,7 @@ both - serial_con + serial_act True False True @@ -486,7 +486,7 @@ - xively_con + xively_act True False True @@ -1175,7 +1175,7 @@ True False - + True False gtk-no @@ -1186,6 +1186,18 @@ 0 + + + True + False + gtk-no + + + False + True + 1 + + True @@ -1195,7 +1207,7 @@ True True - 1 + 2 -- 2.43.2