-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainControl.py
More file actions
268 lines (222 loc) · 10.3 KB
/
Copy pathmainControl.py
File metadata and controls
268 lines (222 loc) · 10.3 KB
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
""" Main control and display module."""
import configparser as CP
import os.path
import const
import evsGUI
import popCharge
import popSettings
import sysSettings
import timers
import charger
import utils
SIMULATE_PV_TO_GRID = 0
# restore window position
config = CP.ConfigParser()
config.read(const.C_INI_FILE)
window = evsGUI.window
window.finalize() # activate window
if config.has_option('Window', 'position_xy'):
win_location = config['Window']['position_xy']
win_location = eval(win_location) # re-format to tuple(x,y)
else: # last location will be stored at exit
win_location = window.current_location()
x, y = win_location
window.move(x, y)
evsGUI.SetLED(window, '-LED_SOLAR-', 'grey')
evsGUI.SetLED(window, '-LED_FORCED-', 'grey')
evsGUI.SetLED(window, '-LED_EXTERN-', 'grey')
evsGUI.SetLED(window, '-LED_CHARGER-', 'grey')
evsGUI.SetLED(window, '-LED_CAR-', 'grey')
evsGUI.SetLED(window, '-LED_PV-', 'grey')
ChargeModes = utils.ChargeModes
# create objects
chargeMode = ChargeModes.IDLE # default
oldChargeMode = None # used for detection of state transitions
sysData = utils.SysData
chargeLogTimer = timers.EcTimer()
# initial of module global variables
t100ms = 0
t1s = -1
ExecImmediate = False
pvData = None
pvChargeOn = False
chargeState = 0
exitApp = False
limit_pos = ''
limit = int(0)
limit_scale = 67 / 100
forceFlag = False
firstRun = True
messageTxt = ''
# use existing file or create one with default values
if os.path.isfile(const.C_DEFAULT_SETTINGS_FILE):
settings = sysSettings.readSettings(const.C_DEFAULT_SETTINGS_FILE)
else:
settings = sysSettings.defaultSettings
sysSettings.writeSettings(const.C_DEFAULT_SETTINGS_FILE, settings)
def printMsg(item="", value=""):
"""
Write message into message window and return message.
:param item: First text in message
:param value: optional values to display
:return: composed text
"""
text = f'{item} {value}'
window['-MESSAGE-'].update(text)
return text
messageTxt = printMsg('Power ON')
messageTxt = printMsg('Charger on URL', const.C_CHARGER_WIFI_URL)
go_e = charger.Charger(url= const.C_CHARGER_WIFI_URL)
utils.writeLog(sysData, strMessage=messageTxt, strMode=chargeMode)
# ------------------------------------------------ this is the main control loop -------------------------------------
while not exitApp:
# cyclic check for user action
event, values = window.read(timeout=100)
if event == 'Quit' or window.was_closed():
go_e.stop_charging()
messageTxt = "User Exit App"
utils.writeLog(sysData, strMessage=messageTxt, strMode=chargeMode)
exitApp = True
elif event == 'Force Charge':
forceFlag = True
elif event == 'Stop Charge':
if chargeMode == ChargeModes.FORCED or chargeMode == ChargeModes.EXTERN:
chargeMode = ChargeModes.STOPPED
ExecImmediate = True
elif event == 'PV-Settings' or event == '-LIMIT_VAL-' or event == '-limit-':
done = popSettings.popSettings(batteryLevel=sysData.batteryLevel, pop_location=window.current_location())
if done:
settings = sysSettings.readSettings(const.C_DEFAULT_SETTINGS_FILE)
limit = settings['pv']['chargeLimit']
sysData.batteryLimit = limit
# MAIN LOOP ------------------------------------------------------------------------------------------------------
# main time division for 1s base tick
t100ms += 1
if t100ms > 10:
if ExecImmediate: # todo: check handling of ExecImmediate
t1s = -1
ExecImmediate = False
t1s += 1
t100ms = 0
print('.', end='')
if t1s % 2 == 0: # blink life sign
if chargeMode == ChargeModes.CAR_ERROR:
evsGUI.SetLED(window, '-LED_MSG-', const.C_BLINK_ERROR_COLOR)
else:
evsGUI.SetLED(window, '-LED_MSG-', const.C_BLINK_OK_COLOR)
else:
evsGUI.SetLED(window, '-LED_MSG-', 'grey')
if sysData.scanTimer.read() == 0:
chargeMode = utils.evalChargeMode(chargeMode, sysData, settings)
if sysData.carPlugged and forceFlag == True:
done = popCharge.popCharge(batteryLevel=sysData.batteryLevel, pop_location=window.current_location())
if done:
settings = sysSettings.readSettings(const.C_DEFAULT_SETTINGS_FILE)
limit = int(settings['manual']['chargeLimit'])
sysData.batteryLimit = limit
chargeMode = ChargeModes.FORCE_REQUEST
ExecImmediate = True
forceFlag = False
# ---------------------------------------- update display elements -------------------------------------------
if firstRun == True:
window['Force Charge'].update(disabled=False)
window['Stop Charge'].update(disabled=False)
limit = int(settings['pv']['chargeLimit'])
sysData.batteryLimit = limit
messageTxt = printMsg('found charger', go_e.url)
utils.writeLog(sysData, strMessage=messageTxt, strMode=chargeMode)
# workaround: prevent action before charger is ready
# go_e.set_phase(1)
firstRun = False
limit_pos = int(limit_scale * limit) * ' ' + '▲'
if sysData.batteryLevel >= limit: # display as full / charge limit reached
batt_color = (const.C_BATT_COLORS[4], '#9898A0')
limit_color = 'green1'
else:
batt_color = (const.C_BATT_COLORS[(sysData.batteryLevel // 21)], '#9898A0')
limit_color = 'white'
# event driven displays
if chargeMode != oldChargeMode:
messageTxt = printMsg(const.C_MODE_TXT[chargeMode])
utils.writeLog(sysData, messageTxt, chargeMode)
if chargeMode == ChargeModes.PV_INIT or chargeMode == ChargeModes.PV_EXEC:
limit = int(settings['pv']['chargeLimit'])
print('SOLAR CHARGE')
evsGUI.SetLED(window, '-LED_SOLAR-', 'yellow')
window['-chargeBar-'].update(bar_color=('yellow', '#9898A0'))
elif chargeMode == ChargeModes.FORCED:
limit = int(settings['manual']['chargeLimit'])
limit_pos = int(limit_scale * limit) * ' ' + '▲'
evsGUI.SetLED(window, '-LED_FORCED-', 'white')
window['-chargeBar-'].update(bar_color=('white', '#9898A0'))
elif chargeMode == ChargeModes.EXTERN:
limit = 0
print('EXTERN CHARGE')
evsGUI.SetLED(window, '-LED_EXTERN-', 'blue')
window['-chargeBar-'].update(bar_color=('blue', '#9898A0'))
elif chargeMode == ChargeModes.IDLE or chargeMode == ChargeModes.UNPLUGGED:
limit = int(settings['pv']['chargeLimit'])
sysData.batteryLimit = limit
limit_pos = int(limit_scale * limit) * ' ' + '▲ '
print('IDLE, waiting for event ...')
evsGUI.SetLED(window, '-LED_SOLAR-', 'grey')
evsGUI.SetLED(window, '-LED_FORCED-', 'grey')
evsGUI.SetLED(window, '-LED_EXTERN-', 'grey')
if oldChargeMode == ChargeModes.PV_EXEC or chargeMode == ChargeModes.FORCED:
if sysData.batteryLevel >= limit:
messageTxt = printMsg(const.C_MODE_TXT[chargeMode] + ' Limit reached!')
utils.writeLog(sysData, messageTxt, chargeMode)
elif chargeMode == ChargeModes.CAR_ERROR:
messageTxt = printMsg(const.C_MODE_TXT[chargeMode] + 'Count =', sysData.carErrorCounter)
utils.writeLog(sysData, messageTxt, chargeMode)
oldChargeMode = chargeMode
# Cyclic gui refresh
if sysData.chargerError:
evsGUI.SetLED(window, '-LED_CHARGER-', 'red')
print('ERROR reading charger, code:', sysData.chargerError)
else:
evsGUI.SetLED(window, '-LED_CHARGER-', 'light green')
if sysData.pvError:
evsGUI.SetLED(window, '-LED_PV-', 'red')
else:
evsGUI.SetLED(window, '-LED_PV-', 'light green')
if sysData.carPlugged:
if sysData.carErrorCounter > 0:
evsGUI.SetLED(window, '-LED_CAR-', 'red')
sysData.carState = "Error reading car"
else:
evsGUI.SetLED(window, '-LED_CAR-', 'light green')
if chargeMode == ChargeModes.IDLE:
sysData.carState = "Car ready"
else:
evsGUI.SetLED(window, '-LED_CAR-', 'grey')
sysData.carState = "Car unplugged"
if sysData.chargeActive:
if chargeLogTimer.read() == 0: # cyclig log entry
sysData.carState = "CAR CHARGING"
messageTxt = printMsg(const.C_MODE_TXT[chargeMode])
utils.writeLog(sysData, messageTxt, chargeMode)
chargeLogTimer.set(const.C_SYS_LOG_INTERVAL)
window['-battBar-'].update(current_count=sysData.batteryLevel)
window['-battBar-'].update(bar_color=batt_color)
window['-LIMIT_VAL-'].update(limit_pos)
window['-LIMIT_VAL-'].update(text_color=limit_color)
window['-limit-'].update(int(limit))
window['-batt-'].update(sysData.batteryLevel)
window['-CHARGE_STATE-'].update(sysData.carState)
window['-solarBar-'].update(current_count=sysData.solarPower)
window['-solar-'].update(sysData.solarPower)
window['-chargeBar-'].update(current_count=int(sysData.chargePower))
window['-charge-'].update(sysData.chargePower)
window['-chargeCurr-'].update(sysData.currentL1)
window['-measuredPhases-'].update(sysData.measuredPhases)
window['-toGridBar-'].update(current_count=sysData.pvToGrid)
window['-toGrid-'].update(sysData.pvToGrid)
# store last window position
if event != window.was_closed():
win_location = window.current_location()
iniFile = open(const.C_INI_FILE, 'w')
config['Window'] = {'position_xy': win_location}
config.write(iniFile)
iniFile.close()
window.close()