forked from philipptrenz/sunportal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunportal.py
executable file
·51 lines (42 loc) · 1.29 KB
/
sunportal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python3
"""
sunportal is a web based visualisation tool to display data of SMA solar inverters.
It is based on the database of SBFspot and shows charts daily and monthly power
production. It runs on a Raspberry Pi and can handle multiple inverters in one Speedwire
or Bluetooth(R) network.
"""
import flask
from flask import Flask, render_template, request, jsonify
from flask_expects_json import expects_json
from util.config import Config
from util.database import Database
from util.mail import Mail
config = Config()
db = Database(config=config)
mail = Mail(config, db)
app = Flask(__name__)
schema = {
'type': 'object',
'properties': {
'date': {
'type': 'string',
"pattern": r'^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$'
}
},
'required': ['date']
}
@app.route('/')
def home():
return render_template('index.html')
@app.route('/update', methods=['POST'])
@expects_json(schema)
def update():
if request.headers['Content-Type'] == 'application/json':
content = request.json
if 'date' not in content: flask.abort(400)
date = content['date']
return jsonify(db.get(date))
flask.abort(400)
if __name__ == '__main__':
if mail.is_enabled: mail.start()
app.run(host='0.0.0.0', port=80)