Skip to content

Commit e93db63

Browse files
Implemented pushing to a MySQL database.
1 parent 7ad0ca9 commit e93db63

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

python/theBMS.py

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44
Copyright © 2019 pro3E - Team4
55
"""
66

7+
from multiprocessing import Process
78
from classes.BMS import BMS
9+
from time import time
10+
import _mysql
811

912
def renew_balancing(bms):
1013
if bms.balancing:
1114
bms.start_balancing()
1215

16+
def push_to_db(query):
17+
host = "hostname"
18+
port = 3306
19+
user = "username"
20+
passwd = "password"
21+
db = "database"
22+
23+
try:
24+
db = _mysql.connect(host=host, port=port, user=user, passwd=passwd, db=db)
25+
db.query(query)
26+
db.close()
27+
except:
28+
pass
29+
1330
def main():
1431
bms = BMS(1, period=100)
1532
counter = 2
@@ -27,21 +44,20 @@ def main():
2744
else:
2845
counter += 1
2946

30-
# Debug
31-
print("#####################")
32-
print("ambient_temp_ok: " + str(bms.ambient_temp_ok))
33-
print("ambient_temp: " + str(bms.ambient_temp))
34-
print("cells_not_oh: " + str(bms.cells_not_oh))
35-
print("temp_ok: " + str(bms.temp_ok))
36-
print("voltages: " + str(bms.voltages))
37-
print("cells_not_ov: " + str(bms.cells_not_ov))
38-
print("cell_not_ov: " + str(bms.cell_not_ov))
39-
print("cells_not_uv: " + str(bms.cells_not_uv))
40-
print("cell_not_uv: " + str(bms.cell_not_uv))
41-
# Debug
47+
# Debug -------------------------------------
48+
# print("##################################")
49+
# print("ambient_temp_ok: " + str(bms.ambient_temp_ok))
50+
# print("ambient_temp: " + str(bms.ambient_temp))
51+
# print("cells_not_oh: " + str(bms.cells_not_oh))
52+
# print("temp_ok: " + str(bms.temp_ok))
53+
# print("voltages: " + str(bms.voltages))
54+
# print("cells_not_ov: " + str(bms.cells_not_ov))
55+
# print("cell_not_ov: " + str(bms.cell_not_ov))
56+
# print("cells_not_uv: " + str(bms.cells_not_uv))
57+
# print("cell_not_uv: " + str(bms.cell_not_uv))
58+
# Debug -------------------------------------
4259

4360
balancing = bms.ambient_temp_ok and bms.cells_not_oh and bms.cells_not_ov and bms.cells_not_uv
44-
# *** Implement check for the state (running or stopped) ***
4561
if balancing:
4662
bms.det_balancing_cmd()
4763
bms.write_balance_cmd(bms.balance_cmd)
@@ -57,5 +73,27 @@ def main():
5773
if bms.running:
5874
bms.stop()
5975

76+
# Compute Query Command
77+
query = "UPDATE bms SET timestamp = '" + str(int(time()))
78+
query += "', balancing = '" + str(int(bms.balancing))
79+
query += "', temp = '{:.1f}".format(bms.ambient_temp)
80+
query += "', v1 = '{:.3f}".format(bms.voltages[0])
81+
query += "', v2 = '{:.3f}".format(bms.voltages[1])
82+
query += "', v3 = '{:.3f}".format(bms.voltages[2])
83+
query += "', v4 = '{:.3f}".format(bms.voltages[3])
84+
query += "', v5 = '{:.3f}".format(bms.voltages[4])
85+
query += "', v6 = '{:.3f}".format(bms.voltages[5])
86+
query += "', m1 = '" + str(int(bms.temp_ok[0]))
87+
query += "', m2 = '" + str(int(bms.temp_ok[1]))
88+
query += "', m3 = '" + str(int(bms.temp_ok[2]))
89+
query += "', m4 = '" + str(int(bms.temp_ok[3]))
90+
query += "', m5 = '" + str(int(bms.temp_ok[4]))
91+
query += "', m6 = '" + str(int(bms.temp_ok[5]))
92+
query += "', balancecmd = '" + str(bms.balance_cmd)
93+
query += "' WHERE id = '0'"
94+
95+
p = Process(target=push_to_db, args=[query])
96+
p.start()
97+
6098
if __name__ == "__main__":
6199
main()

0 commit comments

Comments
 (0)