+ return 'mysql+mysqldb://{}:{}@{}/{}'.format(user, pwd, host, db)
+
+
+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(sensor_id, sensor_type, begin, end, mode):
+ query = Sensors.query
+ if sensor_id is not None:
+ query = query.filter(Sensors.sensor_id == sensor_id)
+ if sensor_type is not None:
+ query = query.filter(Sensors.value_type == sensor_type)
+ if begin is not None:
+ query = query.filter(Sensors.timestamp >= begin)
+ if end is not None:
+ query = query.filter(Sensors.timestamp <= end)
+ if mode == 'consolidated' and begin is None and end is None:
+ # copied from munin/master/_bin/munin-cgi-graph.in
+ # interval in seconds for data points
+ 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'
+ # something like
+ # select mean(temperature) from sensors where ... group by mod(timestamp, resolution)
+ # func.avg(...)
+ #
+ # from https://stackoverflow.com/questions/4342370/grouping-into-interval-of-5-minutes-within-a-time-range
+ # SELECT
+ # timestamp, -- not sure about that
+ # name,
+ # count(b.name)
+ # FROM time a, id
+ # WHERE …
+ # GROUP BY
+ # UNIX_TIMESTAMP(timestamp) DIV 300, name
+ return query.all()
+
+
+def convert_to_c3(result):
+ 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)
+ return c3result
+
+
+def sensordata(sensor_id=None, sensor_type=None):
+ begin = request.args.get('begin', None, parse_datetime)
+ end = request.args.get('end', None, parse_datetime)
+ mode = request.args.get('mode', 'full')
+ format = request.args.get('format', 'default')
+
+ result = select_sensordata(sensor_id, sensor_type, begin, end, mode)
+
+ if format == 'c3':
+ return convert_to_c3(result)
+ return result
+
+
+def currentairtemperature(apikey, cityid):
+ """Retruns the tuple temperature, datetime (as float, datetime) in case of success, otherwise None, None."""
+ try:
+ url, weatherdata = openweathermap_json(apikey, cityid)
+ return weatherdata['main']['temp'], datetime.datetime.fromtimestamp(weatherdata['dt'])
+ except OpenWeatherMapError:
+ return None, None
+
+
+def currentwatertemperature(sensorid):
+ result = Sensors.query.filter_by(sensor_id=sensorid).order_by(Sensors.timestamp.desc()).first()
+ return result.value, result.timestamp