aboutsummaryrefslogtreecommitdiffstats
path: root/app.py
blob: c4db499f8c464ba4ffa4e9c08720c754d8c8c620 (plain)
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
52
53
54
55
56
57
58
59
60
import database
import mistune
import mikrotik
import devices
import flask
import os

app = flask.Flask(__name__)
switch = mikrotik.MikroTikSerialDevice()
markdown_renderer = mistune.create_markdown(
    renderer = mistune.HTMLRenderer(),
    plugins = ["strikethrough", "table", "url"]
)

@app.route("/")
def route_index():
    with database.PowerDatabase(host = devices.HOST) as db:
        return flask.render_template(
            "index.html.j2",
            tasmota_devices = [[i[0], markdown_renderer(i[-1])] for i in db.get_tasmota_devices()]
        )

@app.route("/api/mikrotik_devices")
def api_get_mikrotik_devices():
    return flask.jsonify({i[0]: markdown_renderer(i[1]) for i in switch.interfaces.items()})

@app.route("/api/mikrotik_interface/<interface>")
def api_poll_mikrotik_interface(interface):
    try:
        return flask.jsonify(
            {
                "interface": interface,
                "description": switch.interfaces[interface],
                "poe_status": switch.get_poe_info(interface)
            }
        )
    except (IndexError, KeyError):
        return flask.abort(400)

@app.route("/api/mikrotik_plug")
def api_get_mikrotik_plug():
    return flask.jsonify({"parent": os.environ["MIKROTIK_TASMOTA"]})
    
@app.route("/api/plugs")
def api_poll_plugs():
    with database.PowerDatabase(host = devices.HOST) as db:
        return flask.jsonify(db.get_last_plug_readings())

@app.route("/api/daily_chart")
def api_get_watt_chart():
    with database.PowerDatabase(host = devices.HOST) as db:
        return flask.jsonify(db.get_watt_chart())

@app.route("/api/longterm_chart")
def api_get_kwh_chart():
    with database.PowerDatabase(host = devices.HOST) as db:
        return flask.jsonify(db.get_kwh_chart())

if __name__ == "__main__":
    app.run(host = "0.0.0.0", port = int(os.environ["APP_PORT"]), debug = True)