def sensor(): from_date, to_date = get_range(request) if from_date: db = get_db() cursor = db.cursor() cursor.execute("SELECT humidity, pressure, temperature, timestamp FROM bme WHERE timestamp BETWEEN (?) AND (?)", (from_date.timestamp(), to_date.timestamp())) rows = cursor.fetchall() cursor.connection.close() r = [{ "humidity": row[0], "pressure": row[1], "temperature": row[2], "timestamp": datetime.fromtimestamp(row[3]).isoformat() } for row in rows] return jsonify(r) else: poll = bme280_sensor.poll() return jsonify({ "humidity": poll["humidity"], "pressure": poll["pressure"], "temperature": poll["temperature"], "timestamp": poll["timestamp"].isoformat() })
def snap_image(): print('Snapping image at %s' % datetime.now()) v = camera.snap() print('snap values = %s' % v) db = get_db() cursor = db.cursor() cursor.execute('''INSERT INTO camera(filename, timestamp) VALUES(?, ?)''', (v["filename"], v["timestamp"].timestamp())) db.commit()
def lastImage(): db = get_db() cursor = db.cursor() cursor.execute("SELECT filename, timestamp FROM camera ORDER BY timestamp DESC LIMIT 1") row = cursor.fetchone() cursor.connection.close() print(row[0]) full_filename = "{}/{}".format(config['image_store'], row[0]) return send_file(full_filename)
def collect_status(): print('Collecting status %s' % datetime.now()) v = status.status() print('status values = %s' % v) db = get_db() cursor = db.cursor() cursor.execute( '''INSERT INTO status(size, free, timestamp) VALUES(?, ?, ?)''', (v["size"], v["free"], v["timestamp"].timestamp())) db.commit()
def poll_bme280(): print('Polling bme280 at %s' % datetime.now()) v = bme280_sensor.poll() print('bme values = %s' % v) db = get_db() cursor = db.cursor() cursor.execute( '''INSERT INTO bme(humidity, pressure, temperature, timestamp) VALUES(?, ?, ?, ?)''', (v["humidity"], v["pressure"], v["temperature"], v["timestamp"].timestamp())) db.commit()
def images(): from_date, to_date = get_range(request) if from_date: db = get_db() cursor = db.cursor() cursor.execute("SELECT filename, timestamp FROM camera WHERE timestamp BETWEEN (?) AND (?)", (from_date.timestamp(), to_date.timestamp())) rows = cursor.fetchall() print("+++ files = ", rows) cursor.connection.close() return jsonify([{ "filename": row[0], "timestamp": datetime.fromtimestamp(row[1]).isoformat() } for row in rows])
def stat(): from_date, to_date = get_range(request) if from_date: db = get_db() cursor = db.cursor() cursor.execute("SELECT size, free, timestamp FROM status WHERE timestamp BETWEEN (?) AND (?)", (from_date.timestamp(), to_date.timestamp())) rows = cursor.fetchall() cursor.connection.close() return jsonify([{ "size": row[0], "free": row[1], "timestamp": datetime.fromtimestamp(row[2]).isoformat() } for row in rows]) else: res = status.status() return jsonify({ "size": res["size"], "free": res["free"], "timestamp": res["timestamp"].isoformat() })