This repository has been archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGinMon.py
180 lines (156 loc) · 5.16 KB
/
GinMon.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import argparse
import configparser
import json
import os
import sys
import requests
import Exports
# Ginlong output keys
gindict = {"1a": "DCVPV1",
"1b": "DCVPV2",
'1c': "DCVPV3",
'1d': "DCVPV4",
'1j': "DCCUR1",
'1k': "DCCUR2",
'1l': "DCCUR3",
'1m': "DCCUR4",
'1af': "ACVOL1",
'1ag': "ACVOL2",
'1ah': "ACVOL3",
'1ai': "ACCUR1",
'1aj': "ACCUR2",
'1ak': "ACCUR3",
'1ao': "ACWATT",
'1ar': "ACFREQ",
'1bd': "DAYGEN",
'1be': "MONGEN",
'1bf': "ANNGEN",
'1bc': "TOTGEN",
'1df': "INVTMP"}
# Functions
def CheckLogin():
# Retrieve data from config
print("Loading Config")
username = config.get('Ginlong', 'username')
password = config.get('Ginlong', 'password')
InverterID = config.get('Ginlong', 'inverterID')
print("Config Loaded, logging in")
# Build login string
url = BaseURL + "/cpro/login/validateLogin.json"
params = {
"userName": username,
"password": password,
"lan": "2", # lan = 2 == English,
"domain": "m.ginlong.com",
"userType": "C"
}
r = session.post(url, params=params)
rson = r.json()
# Debug
if rson['result'].get('isAccept') == 1:
print("Login Succesfull!")
return InverterID
else:
print("Login Failed!!")
Exit()
def GetData(deviceID):
url = "http://m.ginlong.com/cpro/device/inverter/goDetailAjax.json"
params = {
'deviceId': int(deviceID)
}
cookies = {'language': '2'}
r = session.get(url, params=params, cookies=cookies)
rson = r.json()
dataset = ParseMultiData(rson, amount_inverters)
return dataset
def ParseMultiData(rson, inverters):
for i in range(inverters):
global d, data
results = {'Inverter': i + 1}
# Get data based on inverter generation
if generation == 3:
data = rson['result']['deviceWapper']['data']
d = json.loads(data)
results.update({'Plantname': rson['result']['deviceWapper']['plantName']}) # Plantname
results.update({'Updatetime': rson['result']['deviceWapper']['updateDate']}) # Last update (epoch)
elif generation == 4:
data = rson['result']['paginationAjax']['data'][i]['data']
d = json.loads(data)
results.update({'Plantname': rson['result']['plantInfo']['name']}) # Plantname
results.update(
{'Updatetime': rson['result']['paginationAjax']['data'][0]['updateDate']}) # Last update (epoch)
else:
print("wrong generation entered in config (must be 3 or 4)")
Exit()
# Try to fill in all values declared in gindict
for line in d:
try:
results.update({gindict[line]: d[line]})
except:
pass
# Check for last upload time
i += 1
CheckActivity(results['Updatetime'])
# Print results (for debugging)
# print(results)
return results
def CheckActivity(updatetime):
if not os.path.isfile('lastlog.txt'):
wr = open("lastlog.txt", "w+")
wr.write(str(updatetime))
else:
wr = open("lastlog.txt", 'r+')
lastdata = wr.readline()
if int(lastdata) == updatetime:
print("No new data found on server, sun is probably down")
Exit()
else:
wr.seek(0)
wr.write(str(updatetime))
wr.truncate()
wr.close()
def ExportData(Data, i):
if config.getboolean('PVoutput', 'enabled'):
Exports.PVoutput(Data, i)
else:
print("Data export to PVoutput disabled")
if config.getboolean('MariaDB', 'enabled'):
ip = config.get('MariaDB', 'serverip')
db = config.get('MariaDB', 'database')
table = config.get('MariaDB', 'table')
username = config.get('MariaDB', 'username')
password = config.get('MariaDB', 'password')
Exports.mariaInsert(ip, Data, db, table, username, password)
else:
print("Data export to MariaDB disabled")
def Exit():
print("Bye!!")
sys.exit()
# Base URLs and
BaseURL = "http://m.ginlong.com"
# Project directory
prog_path = os.path.dirname(os.path.abspath(__file__))
# Create session for requests
session = requests.session()
# Import config file
config = configparser.RawConfigParser(allow_no_value=True)
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="Config location")
args = parser.parse_args()
# Check for -c argument, if not load default from project folder
if args.config:
print("config from argument loaded")
print(args.config)
config.read(args.config)
else:
config.read(prog_path + "/config.ini")
if __name__ == "__main__":
print("Welcome to Ginlong monitoring tool v2 by wessel145")
# Set Generation
generation = int(config.get('Ginlong', 'generation'))
amount_inverters = int(config.get('Ginlong', 'Amount_inverters'))
# Actual start commands
InverterID = CheckLogin()
Data = GetData(InverterID)
ExportData(Data, 1)
Exit()