+
+config = configparser.ConfigParser()
+config.read(os.environ['SEEPARKINI'])
+apikey = config.get('openweathermap', 'apikey')
+cityid = config.get('openweathermap', 'cityid')
+mainsensor = config.get('temperature', 'mainsensor')
+
+app = Flask(__name__)
+app.json_encoder = JSONEncoder
+app.config['SQLALCHEMY_DATABASE_URI'] = get_sqlalchemy_database_uri(config)
+app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
+db = SQLAlchemy(app)
+db.reflect(app=app)
+
+
+class Sensors(db.Model):
+ __tablename__ = 'sensors'
+
+
+def select_sensordata(initial_where):
+ query = Sensors.query.filter(initial_where)
+ begin = request.args.get('begin', None, parse_datetime)
+ end = request.args.get('end', None, parse_datetime)
+ if begin is not None:
+ query = query.filter(Sensors.timestamp >= begin)
+ if end is not None:
+ query = query.filter(Sensors.timestamp <= end)
+ result = query.all()
+
+ mode = request.args.get('mode', 'full')
+ if mode == 'consolidated':
+ if begin is None or end is None:
+ pass
+ else:
+ # copied from munin/master/_bin/munin-cgi-graph.in
+ resolutions = dict(
+ day = 300,
+ week = 1800,
+ month = 7200,
+ year = 86400,
+ )
+ duration = (end - begin).total_seconds()
+ day = 60 * 60 * 24
+ if duration < day:
+ resolution = resolutions['day']
+ elif duration < 7 * day:
+ resolution = resolutions['week']
+ elif duration < 31 * day:
+ resolution = resolutions['month']
+ else:
+ resolution = resolutions['year']
+ # 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 = defaultdict(list)
+ for row in result:
+ c3result[row.sensor_id].append(row.value)
+ dt = row.timestamp.strftime('%Y-%m-%d %H:%M:%S')
+ c3result[row.sensor_id + '_x'].append(dt)
+ result = c3result