add format argument to sensors API
authorgregor herrmann <gregor@toastfreeware.priv.at>
Wed, 13 Jun 2018 16:30:56 +0000 (18:30 +0200)
committergregor herrmann <gregor@toastfreeware.priv.at>
Wed, 13 Jun 2018 16:33:13 +0000 (18:33 +0200)
with "c3" option for delivering data in the format c3.js expects

web/seepark_web.py

index 340c7b6..9ad59f1 100644 (file)
@@ -79,6 +79,18 @@ def select_sensordata(initial_where, initial_sql_args):
             # TODO: filter out samples from 'result'
             # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
 
+    format = request.args.get('format', 'default')
+    if format == 'c3':
+        c3result = dict()
+        for row in result:
+            if not row['sensor_id'] in c3result:
+                c3result[row['sensor_id']] = list()
+            c3result[row['sensor_id']].append(row['value'])
+            if not row['sensor_id'] + '_x' in c3result:
+                c3result[row['sensor_id'] + '_x'] = list()
+            dt = row['timestamp'].strftime('%Y-%m-%d %H:%M:%S')
+            c3result[row['sensor_id'] + '_x'].append(dt)
+        result = c3result
     return result
 
 
@@ -100,6 +112,7 @@ def sensorid(version, sensor_id):
     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
     end=<datetime>, optional, format like "2018-05-19T21:07:53"
     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
     """
     result = select_sensordata('sensor_id=%s', sensor_id)
     return jsonify(result)
@@ -113,6 +126,7 @@ def sensortype(version, sensor_type):
     begin=<datetime>, optional, format like "2018-05-19T21:07:53"
     end=<datetime>, optional, format like "2018-05-19T21:07:53"
     mode=<full|consolidated>, optional. return all rows (default) or with lower resolution (for charts)
+    format=<default|c3>, optional. return result as returned by sqlalchemy (default) or formatted for c3.js
     """
     result = select_sensordata('value_type=%s', sensor_type)
     return jsonify(result)