Added update to sensordata script to add new values to the database.
authorPat Thoyts <Patrick.Thoyts@renishaw.com>
Mon, 9 May 2016 10:52:50 +0000 (11:52 +0100)
committerPat Thoyts <Patrick.Thoyts@renishaw.com>
Mon, 9 May 2016 10:52:50 +0000 (11:52 +0100)
Fix the official mongodb collection name as well.

sensor-hub.wsgi
sensordata.py
static/sensor-hub.html

index 67981e4d396f63fcf44597075bdfc47ef21982f1..991206359bd3208cef5572ddf87f58c5f95ce8d5 100755 (executable)
@@ -50,7 +50,7 @@ class SensorHubService():
         try:
             uri = cherrypy.request.app.config['database']['uri']
             client = MongoClient(uri)
-            db = client.test.sensorlog
+            db = client.sensorhub.sensorlog
             cursor = db.find().sort([("timestamp", pymongo.DESCENDING)]).limit(1)
             item = dict(next(cursor))
             del item['_id']
@@ -86,7 +86,7 @@ class SensorHubService():
     def get_since(self, when, until = None):
         uri = cherrypy.request.app.config['database']['uri']
         client = MongoClient(uri)
-        db = client.test.sensorlog
+        db = client.sensorhub.sensorlog
         selector = [{"timestamp": {"$gt": str(when)}}]
         if not until is None:
             selector.append({"timestamp": {"$lt": str(until)}})
@@ -98,7 +98,7 @@ class SensorHubService():
     def hosts(self):
         uri = cherrypy.request.app.config['database']['uri']
         client = MongoClient(uri)
-        db = client.test.sensorlog
+        db = client.sensorhub.sensorlog
         # groupby("name")
         result = [x['_id'] for x in db.aggregate([{"$group": {"_id": "$name"}} ])]
         return dict(response=result, version=self.version)
@@ -108,7 +108,7 @@ class SensorHubService():
     def sensors(self, hostname):
         uri = cherrypy.request.app.config['database']['uri']
         client = MongoClient(uri)
-        db = client.test.sensorlog
+        db = client.sensorhub.sensorlog
         groups = [x['_id'] for x in db.aggregate([
              {"$match": {"name": hostname}},
              {"$group": {"_id": "$sensors.id"}}
@@ -134,7 +134,7 @@ class SensorHubService():
                     return json.dumps(res).encode(encoding="utf-8")
         except Exception as e:
             cherrypy.response.headers['content-type'] = 'application/json'
-            self.log("error in \"since\": {0}\n {1}".format(str(e), repr(param)))
+            self.log("error in \"since\": {0}\n {1}".format(str(e), repr(cherrypy.request.params)))
             res = dict(response='error', message=str(e))
             res = json.dumps(res).encode(encoding='utf-8')
         return res
index 7b81cc906c4c9b2220453b3fce5e9f96a0647721..813bf5a2f28a27c2531ada20841640f94b9d37cf 100644 (file)
@@ -15,6 +15,13 @@ __author__ = 'Pat Thoyts <patthoyts@users.sourceforge.net>'
 __copyright__ = 'Copyright (c) 2016 Pat Thoyts'
 
 class SensorDataIterator():
+    """Parse a sensor hub ASCII data file into discrete records.
+
+    The sensor hub emits ASCII data with one record per line but with some initial
+    lines prefixed with a # character as a comment and the initial line may get
+    truncated due to serial buffering.
+    The results may have from 1 to 3 temperature values depending on how many sensors
+    have been picked up."""
     def __init__(self, filename):
         self.filename = filename
         self.re = re.compile(r': {(?P<T>.*?)} {(?P<H>.*?)} {(?P<P>.*?)}$')
@@ -96,13 +103,33 @@ def import_logfile(filename, granularity = None):
         granularity = 60 # 1 per minute
     granularity = int(granularity)
     mongo = MongoClient()
-    db = mongo.test.sensorlog
+    db = mongo.sensorhub.sensorlog
     db.drop()
     r = db.insert_many([item for item in GranularData(iter(SensorData(filename)), granularity)])
     print("imported {0} records".format(len(r.inserted_ids)))
     return 0
 
+def update_from_logfile(filename, granularity = None):
+    """Import new records from the sensor-hub logfile into the mongo database."""
+    if granularity is None:
+        granularity = 60 # 1 per minute
+    granularity = int(granularity)
+    mongo = MongoClient()
+    db = mongo.sensorhub.sensorlog
+    # find the last entry in the database
+    cursor = db.find().sort([("timestamp", pymongo.DESCENDING)]).limit(1)
+    last = dict(next(cursor))
+    last_timestamp = int(last['timestamp']) + granularity
+    # skip logdata up to the last database entry
+    # add the remaining entries.
+    r = db.insert_many([x for x in GranularData(iter(SensorData(filename)), granularity)
+                        if int(x['timestamp']) > last_timestamp])
+    print("imported {0} records".format(len(r.inserted_ids)))
+    return 0
+
 def tojson(filename, granularity = None):
+    if granularity is None:
+        granularity = 60 # 1 per minute
     for item in GranularData(iter(SensorData(filename)), granularity):
         print(item)
     return 0
@@ -117,7 +144,7 @@ def testing(*args, **kwargs):
     '''
     uri = 'mongodb://localhost:27017/test'
     client = MongoClient(uri)
-    db = client.test.sensorlog
+    db = client.sensorhub.sensorlog
     #db.insert_one(json) / insert_many (iterable)
     #cursor = db.find({"name": "spd-office"})
     #cursor = db.find({"sensors.id": "office1", "sensors.value": "22.25"})
@@ -140,6 +167,8 @@ if __name__ == '__main__':
     if len(sys.argv) > 1:
         if sys.argv[1] == 'import':
             import_logfile(*sys.argv[2:])
+        elif sys.argv[1] == 'update':
+            update_from_logfile(*sys.argv[2:])
         elif sys.argv[1] == 'json':
             tojson(*sys.argv[2:])
         elif sys.argv[1] == 'test':
index 21bb52f7d3b1432c9854a856aedade1a18bdfd4c..61749069d911642d880c255d3defe47f880e81b9 100644 (file)
@@ -4,7 +4,7 @@
 <meta charset="utf-8"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
 <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.5"/>
-<title>Lab Temperature Monitor</title>
+<title>Office Temperature Monitor</title>
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
 <style type="text/css">
 {margin:0;padding:0}