sensor API: rename '/api/<version>/sensor/<sensor_id>'
authorgregor herrmann <gregor@toastfreeware.priv.at>
Tue, 12 Jun 2018 19:39:39 +0000 (21:39 +0200)
committergregor herrmann <gregor@toastfreeware.priv.at>
Tue, 12 Jun 2018 19:39:39 +0000 (21:39 +0200)
into '/api/<version>/sensor/id/<sensor_id>' and add
'/api/<version>/sensor/type/<sensor_type>'.

factor out the actual query for both into a function.

web/seepark_web.py

index 1b1285aebe41459e4b2cb789387cb28b8a45c5d9..68929fc8154afdaf09b44c25c4d4b70de4278f63 100644 (file)
@@ -35,29 +35,11 @@ def open_engine(config):
     return engine
 
 
-@app.route('/api/<version>/sensors/')
-def sensors(version):
-    """List all sensors found in the database"""
+def select_sensordata(initial_where, initial_sql_args):
     engine = open_engine(config)
     with engine.connect() as conn:
-        cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
-        result = [dict(row) for row in cursor]
-        return jsonify(result)
-
-
-@app.route('/api/<version>/sensor/<sensor_id>')
-def sensor(version, sensor_id):
-    """Return all sensor data
-
-    URL parameters:
-    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)
-    """
-    engine = open_engine(config)
-    with engine.connect() as conn:
-        where = ['sensor_id=%s']
-        sql_args = [sensor_id]
+        where = [initial_where]
+        sql_args = [initial_sql_args]
         begin = None
         end = None
         if 'begin' in request.args:
@@ -97,9 +79,45 @@ def sensor(version, sensor_id):
                 # TODO: filter out samples from 'result'
                 # like loop over results and skip if timestamp(n+1)-timestamp(n)<resolution
 
+    return result
+
+
+@app.route('/api/<version>/sensors/')
+def sensors(version):
+    """List all sensors found in the database"""
+    engine = open_engine(config)
+    with engine.connect() as conn:
+        cursor = conn.execute('select distinct sensor_id, sensor_name, value_type from sensors')
+        result = [dict(row) for row in cursor]
         return jsonify(result)
 
 
+@app.route('/api/<version>/sensor/id/<sensor_id>')
+def sensorid(version, sensor_id):
+    """Return all data for a specific sensor
+
+    URL parameters:
+    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)
+    """
+    result = select_sensordata('sensor_id=%s', sensor_id)
+    return jsonify(result)
+
+
+@app.route('/api/<version>/sensor/type/<sensor_type>')
+def sensortype(version, sensor_type):
+    """Return all data for a specific sensor type
+
+    URL parameters:
+    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)
+    """
+    result = select_sensordata('value_type=%s', sensor_type)
+    return jsonify(result)
+
+
 @app.route('/data/', defaults={'timespan': 1})
 @app.route("/data/<int:timespan>", methods=['GET'])
 def data(timespan):