-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvserver.py
50 lines (37 loc) · 1.33 KB
/
invserver.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
import psycopg2
from flask import Flask, request
from sql.preparing_cursor import PreparingCursor
app = Flask(__name__)
db_conn = psycopg2.connect(host="127.0.0.1", port=5432, user="smlgr", password="smlgr", dbname="smlgr")
@app.route(
rule="/api/public/v1/data",
methods=["POST"]
)
def data():
request_body = request.json
for item in ["ts", "dc_voltage", "dc_current", "ac_voltage", "ac_current", "power", "frequency"]:
if item not in request_body:
raise ValueError("Invalid data. %s not found" % item)
db_cursor = db_conn.cursor(cursor_factory=PreparingCursor)
db_cursor.prepare("""INSERT INTO data
(dc_voltage, dc_current, ac_voltage, ac_current, power, frequency)
VALUES (
%(dc_voltage)s::integer,
%(dc_current)s::integer,
%(ac_voltage)s::integer,
%(ac_current)s::integer,
%(power)s::integer,
%(frequency)s::integer
);""")
db_cursor.execute({
"dc_voltage": float(data["UDC"]) / 10,
"dc_current": float(data["IDC"]) / 100,
"ac_voltage": float(data["UL1"]) / 10,
"ac_current": float(data["IL1"]) / 100,
"power": float(data["PAC"]) / 10,
"frequency": float(data["TNF"]) / 100
})
db_conn.commit()
return {}
if __name__ == "__main__":
app.run()