From: gregor herrmann Date: Tue, 12 Jun 2018 19:39:39 +0000 (+0200) Subject: sensor API: rename '/api//sensor/' X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/commitdiff_plain/bd153e1ffc7014a5b11ba3dd42c19e0fdfa739ce sensor API: rename '/api//sensor/' into '/api//sensor/id/' and add '/api//sensor/type/'. factor out the actual query for both into a function. --- diff --git a/web/seepark_web.py b/web/seepark_web.py index 1b1285a..68929fc 100644 --- a/web/seepark_web.py +++ b/web/seepark_web.py @@ -35,29 +35,11 @@ def open_engine(config): return engine -@app.route('/api//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//sensor/') -def sensor(version, sensor_id): - """Return all sensor data - - URL parameters: - begin=, optional, format like "2018-05-19T21:07:53" - end=, optional, format like "2018-05-19T21:07:53" - mode=, 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)/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//sensor/id/') +def sensorid(version, sensor_id): + """Return all data for a specific sensor + + URL parameters: + begin=, optional, format like "2018-05-19T21:07:53" + end=, optional, format like "2018-05-19T21:07:53" + mode=, 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//sensor/type/') +def sensortype(version, sensor_type): + """Return all data for a specific sensor type + + URL parameters: + begin=, optional, format like "2018-05-19T21:07:53" + end=, optional, format like "2018-05-19T21:07:53" + mode=, 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/", methods=['GET']) def data(timespan):