forked from stik79/DeltaPVOutput
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmysql.py
executable file
·52 lines (40 loc) · 1.59 KB
/
mysql.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
52
import time, subprocess,serial,MySQLdb
from delta30EUG4TRInv import Delta30EU_G4_TR_Inverter
from time import localtime, strftime
from config import Configuration
class MysqlInserter:
def __init__(self):
pass
def insert(self, inverterId, dcVoltage, dcPower, acPower):
if Configuration.mysqlUser == '':
# Skip if mysql is not configured
return
con = MySQLdb.connect(host=Configuration.mysqlHost,user=Configuration.mysqlUser, passwd=Configuration.mysqlPw,db=Configuration.mysqlDb)
con.query("SELECT count(*) from Measurement")
r = con.store_result()
print(str(r.fetch_row()[0][0]) + " results stored in mysql")
try:
inverterIdNum = int(inverterId)
except:
inverterIdNum = -1
try:
acPowerNum = int(acPower)
except:
acPowerNum = -1
try:
dcPowerNum = int(dcPower)
except:
dcPowerNum = -1
try:
dcVoltageNum = int(dcVoltage)
except:
dcVoltageNum = -1
c = con.cursor()
print("inserting: " + str(inverterIdNum) + str(dcVoltageNum) + str(dcPowerNum) + str(acPowerNum))
try:
c.execute("""INSERT INTO Measurement (inverterId, dcVoltage, dcPower, acPower) VALUES (%s,%s,%s,%s)""",(inverterIdNum, dcVoltageNum, dcPowerNum, acPowerNum))
con.commit()
except Error as e:
con.rollback()
print "Something went wrong inserting into mysql. " + e
con.close()