-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFlask_RestfulApi_UPS_Serial.py
273 lines (267 loc) · 9.87 KB
/
Flask_RestfulApi_UPS_Serial.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
#!/usr/bin/python3.6
from flask import Flask
from flask_restful import Resource, Api
from flask import render_template
from decimal import getcontext, Decimal
import serial, time
app = Flask(__name__)
api = Api(app)
serialName = ''
systemMode = 0
inputLine = 0
inputFreq = 0
inputVolt = 0
outputLine = 0
outputFreq = 0
outputVolt = 0
outputWatt = 0
outputAmp = 0
outputPercent = 0
batteryHealth = ''
batteryStatus = ''
batteryCharge_Mode = ''
batteryRemain_Min = ''
batteryRemain_Sec = ''
batteryVolt = 0
batteryTemp = 0
batteryRemain_Percent = 0
lastBattery_Year = 0
lastBattery_Mon = 0
lastBattery_Day = 0
nextBattery_Year = 0
nextBattery_Mon = 0
nextBattery_Day = 0
def connectDevice():
getcontext().prec = 6
with serial.Serial('COM8', 2400, timeout=1) as ser: # select which your protocol & link path on UPS
if(ser.isOpen()):
global serialName
global systemMode
global inputLine, inputFreq, inputVolt
global outputLine, outputFreq, outputVolt, outputWatt, outputAmp, outputPercent
global batteryHealth, batteryStatus, batteryCharge_Mode
global batteryRemain_Min, batteryRemain_Sec, batteryVolt, batteryTemp, batteryRemain_Percent
global lastBattery_Year, lastBattery_Mon, lastBattery_Day
global nextBattery_Year, nextBattery_Mon, nextBattery_Day
serialName = ser.name
print('USB 連接位置 : ' + serialName) # check which port was really used
print('-----------------------------------------')
# --> STI 輸入資料
ser.write(b'~00P000STI') # write a UPS RS232 format string
# ser.write(bytes(b'~00D0101;600;2190')) # Return data format 1 Test
# ser.write(bytes('~00D0101;600;2190', 'UTF-8')) # Return data format 2 Test
s = ser.read(30) # read up to return data 30 bytes (timeout)
# print(s)
countLine = ''
s = s.decode('ascii') # decode UPS return string format
# print(s)
tmp = str(s).split(';') # split data by ';' on data format
# print (tmp)
i = 0
for j in tmp[0]:
if i >= 7:
countLine += str(j)
i = i + 1
i = 0
inputLine = int(countLine)
inputFreq = float(tmp[1])/10
inputVolt = float(tmp[2])/10
print ('輸入線路 : ' + str(inputLine) + ' 號線路')
print ('輸入頻率 : ' + str(inputFreq) + ' Hz')
print ('輸入電壓 : ' + str(inputVolt) + ' V')
print('-----------------------------------------')
time.sleep(1)
# --> STO
ser.write(b'~00P000STO')
# ser.write(b'~00D0230;600;1;2210;;03169;037')
s = ser.read(30)
countMode = ''
s = s.decode('ascii')
# print(s)
tmp = str(s).split(';')
# print (tmp)
i = 0
for j in tmp[0]:
if i >= 7:
countMode += str(j)
i = i + 1
i = 0
# print(countMode)
mode = int(countMode)
systemMode = ''
if mode == 0:
systemMode = 'Normal'
if mode == 1:
systemMode = 'Battery'
if mode == 2:
systemMode = 'Bypass(3phase Reserve Power Path)'
if mode == 3:
psystemMode = 'Reducing'
if mode == 4:
systemMode = 'Boosting'
if mode == 5:
systemMode = 'Manual Bypass'
if mode == 6:
systemMode = 'Other'
if mode == 7:
systemMode = 'No output'
outputFreq = float(tmp[1])/10
outputLine = int(tmp[2])
outputVolt = float(tmp[3])/10
outputWatt = int(tmp[5])
outputAmp = float(outputWatt/outputVolt)
outputAmp = Decimal(outputAmp)*1
outputPercent = int(tmp[6])
print ('輸出狀態 : '+ systemMode)
print ('輸出線路 : ' + str(outputLine) + ' 號線路')
print ('輸出頻率 : ' + str(outputFreq) + ' Hz')
print ('輸出電壓 : %3.1f V' %outputVolt)
print ('輸出電流 : %3.3f A' %outputAmp)
print ('輸出瓦特 : ' + str(outputWatt/1000) + ' KW')
print ('輸出負載比 : ' + str(outputPercent) + ' %')
print('-----------------------------------------')
time.sleep(1)
# --> STB 輸入資料
ser.write(b'~00P000STB')
# ser.write(b'~00D0250;0;1;;;000;2720;;031;100')
s = ser.read(40)
batteryCount = ''
s = s.decode('ascii')
# print(s)
tmp = str(s).split(';')
# print (tmp)
i = 0
for j in tmp[0]:
if i >= 7:
batteryCount += str(j)
i = i + 1
i = 0
if batteryCount == '0':
batteryHealth = 'Good (良好)'
if batteryCount == '1':
batteryHealth = 'Weak (虛弱)'
if batteryCount == '2':
batteryHealth = 'Replace (需更換)'
batteryCount = tmp[1]
if batteryCount == '0':
batteryStatus = 'OK (良好)'
if batteryCount == '1':
batteryStatus = 'Low (低電量)'
if batteryCount == '2':
batteryStatus = 'Depleted (耗盡)'
batteryCount = tmp[1]
if batteryCount == '0':
batteryCharge_Mode = 'Floating charging (微量充電)'
if batteryCount == '1':
batteryCharge_Mode = 'Boost charging (快速充電)'
if batteryCount == '2':
batteryCharge_Mode = 'Resting (休眠)'
if batteryCount == '3':
batteryCharge_Mode = 'Discharging (未充電)'
if tmp[3] != '':
batteryRemain_Sec = tmp[3] + ' sec(秒)'
else:
batteryRemain_Sec = 'None By Charging (充電中)'
if tmp[4] != '':
batteryRemain_Min = tmp[4] + ' min(分)'
else:
batteryRemain_Min = 'None By Charging (充電中)'
batteryVolt = float(tmp[6])/10
batteryVolt = Decimal(batteryVolt)*1
batteryTemp = int(tmp[8])
batteryRemain_Percent = int(tmp[9])
print ('電池健康度 : ' + batteryHealth)
print ('電池狀態 : ' + batteryStatus)
print ('充電模式 : ' + batteryCharge_Mode)
print ('電池電壓 : %3.1f V' %batteryVolt)
print ('輸出剩餘時間(分) : ' + batteryRemain_Min)
print ('輸出剩餘時間(秒) : ' + batteryRemain_Sec)
print ('電量剩餘百分比 : ' + str(batteryRemain_Percent) + ' %')
print ('UPS 內部溫度 : ' + str(batteryTemp) + ' °C')
print('-----------------------------------------')
time.sleep(1)
# --> BRD
ser.write(b'~00P000BRD')
# ser.write(b'~00D01720170322;20200322')
s = ser.read(30)
countLastDate = ''
s = s.decode('ascii')
# print(s)
tmp = str(s).split(';')
# print (tmp)
i = 0
for j in tmp[0]:
if i >= 7:
countLastDate += str(j)
i = i + 1
i = 0
lasteDate = int(countLastDate)
lastBattery_Year = int(lasteDate/10000)
lastBattery_Mon = int(lasteDate/100) - lastBattery_Year*100
lastBattery_Day = lasteDate - lastBattery_Mon*100 - lastBattery_Year*10000
nextDate = int(tmp[1])
nextBattery_Year = int(nextDate/10000)
nextBattery_Mon = int(nextDate/100) - nextBattery_Year*100
nextBattery_Day = nextDate - nextBattery_Mon*100 - nextBattery_Year*10000
print ('電池更換時間 : ' + str(lastBattery_Year) + ' 年 ' + str(lastBattery_Mon) + ' 月 ' + str(lastBattery_Day) + ' 日')
print ('下次更換時間 : ' + str(nextBattery_Year) + ' 年 ' + str(nextBattery_Mon) + ' 月 ' + str(nextBattery_Day) + ' 日')
ser.close() # close port
time.sleep(1)
else:
print('Port Open Error!')
class jsonReturn(Resource):
def get(self):
connectDevice()
global serialName, systemMode
global inputLine, inputFreq, inputVolt
global outputLine, outputFreq, outputVolt, outputWatt, outputAmp, outputPercent
global batteryHealth, batteryStatus, batteryCharge_Mode
global batteryRemain_Min, batteryRemain_Sec, batteryVolt, batteryTemp, batteryRemain_Percent
global lastBattery_Year, lastBattery_Mon, lastBattery_Day
global nextBattery_Year, nextBattery_Mon, nextBattery_Day
return { 'connect' : serialName, \
'input' : [{ 'inputLine' : str(inputLine), 'inputFreq' : str(inputFreq), 'inputVolt' : str(inputVolt) }], \
'output' : [{ 'systemMode' : systemMode, 'outputLine' : str(outputLine), 'outputFreq' : str(outputFreq), 'outputVolt' : str(outputVolt), 'outputAmp' : str(outputAmp), 'outputWatt' : str(outputWatt), 'outputPercent' : str(outputPercent)}], \
'battery' : [{ 'status' : [{ 'batteryHealth' : batteryHealth, 'batteryStatus' : batteryStatus, 'batteryCharge_Mode' : batteryCharge_Mode, 'batteryRemain_Min' : batteryRemain_Min, 'batteryRemain_Sec' : batteryRemain_Sec, 'batteryVolt' : str(batteryVolt), 'batteryTemp' : str(batteryTemp), 'batteryRemain_Percent' : str(batteryRemain_Percent)}]}, \
{ 'lastBattery_Year' : str(lastBattery_Year), 'lastBattery_Mon' : str(lastBattery_Mon), 'lastBattery_Day' : str(lastBattery_Day)}, { 'nextBattery_Year' : str(nextBattery_Year), 'nextBattery_Mon' : str(nextBattery_Mon), 'nextBattery_Day' : str(nextBattery_Day)}]}
api.add_resource(jsonReturn, '/')
@app.route('/show')
def dashBoard():
global serialName, systemMode
global inputLine, inputFreq, inputVolt
global outputLine, outputFreq, outputVolt, outputWatt, outputAmp, outputPercent
global batteryHealth, batteryStatus, batteryCharge_Mode
global batteryRemain_Min, batteryRemain_Sec, batteryVolt, batteryTemp, batteryRemain_Percent
global lastBattery_Year, lastBattery_Mon, lastBattery_Day
global nextBattery_Year, nextBattery_Mon, nextBattery_Day
connectDevice()
return render_template('mainBoard.html', \
serName = serialName, \
inputVolt = inputVolt, \
inputFreq = inputFreq, \
inputLine = inputLine, \
systemMode = str(systemMode), \
outputLine = outputLine, \
outputVolt = outputVolt, \
outputAmp = Decimal(outputAmp)*1, \
outputPercent = outputPercent, \
outputWatt = outputWatt/1000, \
outputFreq = outputFreq, \
batteryHealth = batteryHealth, \
batteryStatus = batteryStatus, \
batteryCharge_Mode = batteryCharge_Mode, \
batteryRemain_Min = batteryRemain_Min, \
batteryRemain_Sec = batteryRemain_Sec, \
batteryVolt = batteryVolt, \
batteryTemp = batteryTemp, \
batteryRemain_Percent = batteryRemain_Percent, \
lastBattery_Year = lastBattery_Year, \
lastBattery_Mon = lastBattery_Mon, \
lastBattery_Day = lastBattery_Day, \
nextBattery_Year = nextBattery_Year, \
nextBattery_Mon = nextBattery_Mon, \
nextBattery_Day = nextBattery_Day, \
)
if __name__ == '__main__':
# app.run(debug = True)
app.run(host = '0.0.0.0')