-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
277 lines (249 loc) · 9.99 KB
/
client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import json
import os
import threading
import requests
import MySQLdb
import datetime
import pytz
import time
import sys
import io
import contextlib
import common
import equipment
# Define the local timezone for timestamp conversion
timezoneLocal = pytz.timezone('Asia/Calcutta')
def universal2local(timeUniversal):
"""
Converts a UTC datetime to local time and formats it as a string.
"""
timeLocal = timeUniversal.replace(tzinfo=pytz.utc).astimezone(timezoneLocal)
return timezoneLocal.normalize(timeLocal).strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')
@contextlib.contextmanager
def stdoutIO(stdout=None):
"""
Context manager for capturing stdout output.
"""
old = sys.stdout
if stdout is None:
stdout = io.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
class configurationS(object):
"""
Class to manage software configuration.
"""
def __init__(self):
super(configurationS, self).__init__()
self.settings = []
self.measurements = []
self.measurementValues = []
self.measurementSets = {}
self.measurementCount = 0
self.servers = {}
self.combinations = {}
self.combinationCount = 0
def cancel(self):
"""
Resets measurement values.
"""
self.measurementValues = []
def load(self, filename):
"""
Loads the configuration from a JSON file and processes it.
"""
with open(filename) as filehandle:
self.settings = json.load(filehandle)
# Extract server configurations, measurement sets, and combinations
self.servers = self.settings['servers']
self.measurementSets = self.settings['measurementSets']
self.combinations = self.settings['combinations']
# Process combinations to gather unique measurements
combination = {}
combinationIndex = 0
while combination is not None:
try:
combination = self.combinations['combination' + str(combinationIndex)]
except KeyError:
combination = None
else:
variables = self.settings['measurementSets'][str(combination['measurementSet'])]
variable = {}
variableIndex = 0
while variable is not None:
try:
variable = variables['variable' + str(variableIndex)]
except KeyError:
variable = None
else:
if str(variable) not in self.measurements:
self.measurements.append(str(variable))
variableIndex += 1
combinationIndex += 1
# Update counts of combinations and measurements
self.combinationCount = combinationIndex
self.measurementCount = len(self.measurements)
class configurationH(object):
"""
Class to manage hardware configuration.
"""
def __init__(self):
super(configurationH, self).__init__()
# Dictionary of supported hardware devices
self.devices = {
0: ('SMA Solar Technology', 'Sunny Boy', 'inverterSunnyBoy'),
1: ('SMA Solar Technology', 'Sunny Web Box', 'loggerSunnyWebBox'),
2: ('Helios Systems', 'HS100', 'inverterHelios'),
3: ('Enertech', '<blank>'),
4: ('Danfoss', '<blank>'),
5: ('Statcon Energiaa', 'SMB096', 'combinerSMB096'),
6: ('Beijing EPSolar Technology', 'TracerA', 'chargerTracerA'),
7: ('Delta Electronics', 'RPI', 'inverterRPI'),
8: ('ABB', 'PVS800', 'inverterPVS800')
}
self.device = ''
self.deviceType = ''
self.manufacturer = ''
self.modelNumber = 0
self.serialNumber = 0
self.identity = {}
self.isLoaded = -1
self.isAttached = -1
self.toStore = ''
def load(self, filename):
"""
Loads the hardware configuration from a JSON file.
"""
with open(filename) as filehandle:
settings = json.load(filehandle)
self.deviceType = settings['type']
self.manufacturer = settings['manufacturer']
self.modelNumber = settings['modelNumber']
self.serialNumber = settings['serialNumber']
self.identity = settings['identity']
self.toStore = settings['toStore']
self.isLoaded = 0
return self.isLoaded
def attach(self):
"""
Attaches the hardware configuration to the software.
"""
if self.isLoaded == -1:
print('Attach Hardware Configuration Fail - Load incorrect')
else:
deviceIndex = -1
for key, value in self.devices.items():
if (value[0] == self.manufacturer) and (value[1] == self.modelNumber):
deviceIndex = key
self.isAttached = 1
if self.isAttached == 1:
with stdoutIO() as s:
exec('self.device = equipment.' + self.devices[deviceIndex][2] + '()')
self.device.attach(self.identity)
self.isAttached = 0
else:
print('Attach Hardware Configuration Fail - Host unrecognized')
return self.isAttached
def read(self, measurementName):
"""
Reads the value of a measurement from the hardware or Raspberry Pi.
"""
try:
measurementIndex = [key for key, value in self.device.labels.items() if value == measurementName][0]
except IndexError:
try:
measurementValue = common.getParameterHandler(measurementName)
except KeyError:
return -1
else:
return measurementValue
else:
return self.device.read(measurementIndex)
def cancel(self):
"""
Cancels the current set of measurements.
"""
self.device.cancel()
if __name__ == '__main__':
cH = configurationH()
cS = configurationS()
# Load software and hardware configurations
cS.load('/home/pi/marshal/cS.json')
cH.load('/home/pi/marshal/cH.json')
# Attach hardware device
cH.attach()
cS.cancel()
cH.cancel()
# Retrieve measurements for all unique variables
for measurementName in cS.measurements:
cS.measurementValues.append(str(cH.read(measurementName)))
# Process each combination and send data to the appropriate server
for combinationIndex in range(cS.combinationCount):
combination = cS.combinations['combination' + str(combinationIndex)]
server = cS.servers[combination['server']]
variables = cS.measurementSets[combination['measurementSet']]
requestPayload = {}
measurementData = {}
# Gather measurement data
variableName = ''
variableIndex = 0
while variableName is not None:
try:
variableName = variables.get('variableAlternate' + str(variableIndex)) if cH.device.sanity == -1 else variables.get('variable' + str(variableIndex))
except KeyError:
variableName = None
else:
if variableName:
measurementData[str(variableName)] = cS.measurementValues[cS.measurements.index(str(variableName))]
variableIndex += 1
hostData = {
'type': cH.deviceType,
'serialNumber': cH.serialNumber,
'manufacturer': cH.manufacturer,
'modelNumber': cH.modelNumber,
'toStore': cH.toStore,
'isOnDemand': 'False',
'isSane': cH.device.sanity
}
requestPayload['t'] = universal2local(datetime.datetime.now()).strip(' IST+0530')
requestPayload['h'] = hostData
requestPayload['m'] = measurementData
if variableIndex == 1:
exit()
if server['protocol'] == 'http':
# Send data via HTTP request
url = f"{server['protocol']}://{server['hostname']}:{server.get('portnumber', '')}{server['path']}"
response = requests.post(url, auth=(server['username'], server['password']), json=requestPayload, verify=server.get('certificate', True))
responsePayload = json.loads(response.text)
measurementData = {}
variableIndex = 0
# Handle on-demand data requests
while variableName is not None:
try:
variableName = responsePayload.get('variable' + str(variableIndex))
except KeyError:
variableName = None
else:
if variableName:
measurementData[str(variableName)] = cH.read(str(variableName))
variableIndex += 1
if variableIndex > 1:
hostData['isOnDemand'] = "True"
requestPayload['t'] = universal2local(datetime.datetime.now()).strip(' IST+0530')
requestPayload['h'] = hostData
requestPayload['m'] = measurementData
url = f"{server['protocol']}://{server['hostname']}:{server.get('portnumber', '')}{server['path']}"
response = requests.post(url, auth=(server['username'], server['password']), json=requestPayload, verify=server.get('certificate', True))
responsePayload = json.loads(response.text)
elif server['protocol'] == 'mysql':
# Insert data into MySQL database
columns = ", ".join([f"`{col}`" for col in measurementData.keys()])
values = ", ".join([f"'{val}'" for val in measurementData.values()])
sqlQuery = f"INSERT INTO {server['table']} ({columns}) VALUES ({values})"
conn = MySQLdb.connect(host=server['hostname'], user=server['username'], passwd=server['password'], db=server['database'])
cursor = conn.cursor()
cursor.execute(sqlQuery)
conn.commit()
cursor.close()
conn.close()