forked from jgyates/genmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genexercise.py
355 lines (301 loc) · 15.8 KB
/
genexercise.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# FILE: genexercise.py
# PURPOSE: genexercise.py add enhanced exercise functionality for Evolution and Nexus cotrollers
#
# AUTHOR: jgyates
# DATE: 02-23-2019
#
# MODIFICATIONS:
#-------------------------------------------------------------------------------
import datetime, time, sys, signal, os, threading, collections, json, ssl
import atexit, getopt
try:
from genmonlib.myclient import ClientInterface
from genmonlib.mylog import SetupLogger
from genmonlib.myconfig import MyConfig
from genmonlib.mysupport import MySupport
from genmonlib.mycommon import MyCommon
from genmonlib.mythread import MyThread
from genmonlib.program_defaults import ProgramDefaults
except Exception as e1:
print("\n\nThis program requires the modules located in the genmonlib directory in the github repository.\n")
print("Please see the project documentation at https://github.com/jgyates/genmon.\n")
print("Error: " + str(e1))
sys.exit(2)
#------------ GenExercise class ------------------------------------------------
class GenExercise(MySupport):
#------------ GenExercise::init---------------------------------------------
def __init__(self,
log = None,
loglocation = ProgramDefaults.LogPath,
ConfigFilePath = MyCommon.DefaultConfPath,
host = ProgramDefaults.LocalHost,
port = ProgramDefaults.ServerPort,
console = None):
super(GenExercise, self).__init__()
self.AccessLock = threading.Lock()
self.log = log
self.console = console
self.MonitorAddress = host
self.PollTime = 2
self.ExerciseActive = False
self.Debug = False
try:
self.config = MyConfig(filename = os.path.join(ConfigFilePath, 'genexercise.conf'), section = 'genexercise', log = self.log)
self.ExerciseType = self.config.ReadValue('exercise_type', default = "Normal")
self.ExerciseHour = self.config.ReadValue('exercise_hour', return_type = int, default = 12)
self.ExerciseMinute = self.config.ReadValue('exercise_minute', return_type = int, default = 0)
self.ExerciseDayOfMonth = self.config.ReadValue('exercise_day_of_month', return_type = int, default = 1)
self.ExerciseDayOfWeek = self.config.ReadValue('exercise_day_of_week', default = "Monday")
self.ExerciseDuration = self.config.ReadValue('exercise_duration', return_type = float, default = 12)
self.ExerciseWarmup = self.config.ReadValue('exercise_warmup', return_type = float, default = 0)
self.ExerciseFrequency = self.config.ReadValue('exercise_frequency', default = "Monthly")
self.MonitorAddress = self.config.ReadValue('monitor_address', default = ProgramDefaults.LocalHost)
self.LastExerciseTime = self.config.ReadValue('last_exercise', default = None)
self.UseGeneratorTime = self.config.ReadValue('use_gen_time', return_type = bool, default = False)
self.Debug = self.config.ReadValue('debug', return_type = bool, default = False)
# Validate settings
if not self.ExerciseType.lower() in ["normal","quiet", "transfer"]:
self.ExerciseType = "normal"
if self.ExerciseHour > 23 or self.ExerciseHour < 0:
self.ExerciseHour = 12
if self.ExerciseMinute > 59 or self.ExerciseMinute < 0:
self.ExerciseMinute = 0
if not self.ExerciseDayOfWeek.lower() in ["monday", "tuesday", "wednesday","thursday","friday","saturday","sunday"]:
self.ExerciseDayOfWeek = "Monday"
if self.ExerciseDayOfMonth > 28 or self.ExerciseDayOfMonth < 1:
self.ExerciseDayOfMonth = 1
if self.ExerciseDuration > 60:
self.ExerciseDuration = 60
if self.ExerciseDuration < 5:
self.ExerciseDuration = 5
if self.ExerciseWarmup > 30:
self.ExerciseWarmup = 30
if self.ExerciseWarmup < 0:
self.ExerciseWarmup = 0
if not self.ExerciseFrequency.lower() in ["weekly","biweekly", "monthly"]:
self.ExerciseFrequency = "Monthly"
if self.MonitorAddress == None or not len(self.MonitorAddress):
self.MonitorAddress = ProgramDefaults.LocalHost
except Exception as e1:
self.LogErrorLine("Error reading " + os.path.join(ConfigFilePath, "genexercise.conf") + ": " + str(e1))
self.console.error("Error reading " + os.path.join(ConfigFilePath, "genexercise.conf") + ": " + str(e1))
sys.exit(1)
try:
self.Generator = ClientInterface(host = self.MonitorAddress, port = port, log = self.log)
if not self.CheckGeneratorRequirement():
self.LogError("Requirements not met. Exiting.")
sys.exit(1)
# start thread monitor time for exercise
self.Threads["ExerciseThread"] = MyThread(self.ExerciseThread, Name = "ExerciseThread", start = False)
self.Threads["ExerciseThread"].Start()
try:
if self.ExerciseFrequency.lower() == "monthly":
DayStr = "Day " + str(self.ExerciseDayOfMonth)
else:
DayStr = str(self.ExerciseDayOfWeek)
self.LogError("Execise: " + self.ExerciseType + ", " + self.ExerciseFrequency + " at "
+ str(self.ExerciseHour) + ":" + str(self.ExerciseMinute) + " on " + DayStr + " for "
+ str(self.ExerciseDuration) + " min. Warmup: " + str(self.ExerciseWarmup))
self.DebugOutput("Debug Enabled")
except Exception as e1:
self.LogErrorLine(str(e1))
signal.signal(signal.SIGTERM, self.SignalClose)
signal.signal(signal.SIGINT, self.SignalClose)
except Exception as e1:
self.LogErrorLine("Error in GenExercise init: " + str(e1))
self.console.error("Error in GenExercise init: " + str(e1))
sys.exit(1)
#---------- GenExercise::SendCommand --------------------------------------
def SendCommand(self, Command):
if len(Command) == 0:
return "Invalid Command"
try:
with self.AccessLock:
data = self.Generator.ProcessMonitorCommand(Command)
except Exception as e1:
self.LogErrorLine("Error calling ProcessMonitorCommand: " + str(Command))
data = ""
return data
#---------- GenExercise::CheckGeneratorRequirement ------------------------
def CheckGeneratorRequirement(self):
try:
data = self.SendCommand("generator: start_info_json")
StartInfo = {}
StartInfo = json.loads(data)
if not "evolution" in StartInfo["Controller"].lower() and not "nexus" in StartInfo["Controller"].lower():
self.LogError("Error: Only Evolution or Nexus controllers are supported for this feature: " + StartInfo["Controller"])
return False
return True
except Exception as e1:
self.LogErrorLine("Error in CheckGeneratorRequirement: " + str(e1))
return False
# ---------- GenExercise::PostWarmup----------------------------------------
def PostWarmup(self):
# check to see if the generator is running
status = self.SendCommand("generator: getbase")
if not status.lower() in ["running", "exercising"]:
self.LogError("WARNING: generator not running post warmup. Transfer switch not activated.")
self.SendCommand("generator: setremote=stop")
return
self.SendCommand("generator: setremote=starttransfer")
self.DebugOutput("Starting transfer exercise cycle (post warmup).")
# set timer to stop
self.StopTimer = threading.Timer(float(self.ExerciseDuration * 60.0), self.StopExercise)
self.StopTimer.start()
# ---------- GenExercise::ReadyToExercise-----------------------------------
def ReadyToExercise(self):
status = self.SendCommand("generator: getbase")
if not status.lower() in ["ready","servicedue"]:
self.LogError("Generator not in Ready state, exercise cycle not started: " + str(status))
return False
return True
# ---------- GenExercise::StartExercise-------------------------------------
def StartExercise(self):
if self.ExerciseActive:
# already active
return
# Start generator
if self.ExerciseType.lower() == "normal" and self.ReadyToExercise():
self.SendCommand("generator: setremote=start")
self.DebugOutput("Starting normal exercise cycle.")
self.StopTimer = threading.Timer(float(self.ExerciseDuration * 60.0), self.StopExercise)
self.StopTimer.start()
elif self.ExerciseType.lower() == "quiet" and self.ReadyToExercise():
self.SendCommand("generator: setremote=startexercise")
self.DebugOutput("Starting quiet exercise cycle.")
self.StopTimer = threading.Timer(float(self.ExerciseDuration * 60.0), self.StopExercise)
self.StopTimer.start()
elif self.ExerciseType.lower() == "transfer" and self.ReadyToExercise():
if self.ExerciseWarmup == 0:
self.SendCommand("generator: setremote=starttransfer")
self.DebugOutput("Starting transfer exercise cycle.")
self.StopTimer = threading.Timer(float(self.ExerciseDuration * 60.0), self.StopExercise)
self.StopTimer.start()
else:
self.SendCommand("generator: setremote=start")
self.DebugOutput("Starting warmup for transfer exercise cycle.")
# start timer for post warmup transition to starttransfer command
self.WarmupTimer = threading.Timer(float(self.ExerciseWarmup * 60.0), self.PostWarmup)
self.WarmupTimer.start()
else:
self.LogError("Invalid mode in StartExercise: " + str(self.ExerciseType))
return
self.WriteLastExerciseTime()
self.ExerciseActive = True
# ---------- GenExercise::StopExercise--------------------------------------
def StopExercise(self):
if self.ExerciseActive:
self.SendCommand("generator: setremote=stop")
self.DebugOutput("Stopping exercise cycle.")
self.ExerciseActive = False
else:
self.DebugOutput("Calling Stop Exercise (not needed)")
# ---------- GenExercise::DebugOutput-----------------------------
def DebugOutput(self, Message):
if self.Debug:
self.LogError(Message)
# ---------- GenExercise::WriteLastExerciseTime-----------------------------
def WriteLastExerciseTime(self):
try:
NowString = datetime.datetime.now().strftime("%A %B %d, %Y %H:%M:%S")
if self.ExerciseFrequency.lower() == "biweekly":
self.config.WriteValue("last_exercise", NowString)
self.config.LastExerciseTime = NowString
self.DebugOutput("Last Exercise Cycle: " + NowString)
except Exception as e1:
self.LogErrorLine("Error in WriteLastExerciseTime: " + str(e1))
# ---------- GenExercise::TimeForExercise-----------------------------------
def TimeForExercise(self):
try:
if self.UseGeneratorTime:
TimeNow = self.GetGeneratorTime()
else:
TimeNow = datetime.datetime.now()
if TimeNow.hour != self.ExerciseHour or TimeNow.minute != self.ExerciseMinute:
return False
weekDays = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
WeekDayString = weekDays[TimeNow.weekday()]
if not self.ExerciseFrequency.lower() in ["weekly", "biweekly", "monthly"]:
self.LogError("Invalid Exercise Frequency in TimeForExercise: " + str(self.ExerciseFrequency))
return False
if self.ExerciseFrequency.lower() == "weekly" and self.ExerciseDayOfWeek.lower() == WeekDayString.lower():
return True
elif self.ExerciseFrequency.lower() == "biweekly" and self.ExerciseDayOfWeek.lower() == WeekDayString.lower():
if self.LastExerciseTime == None:
return True
LastExerciseTime = datetime.datetime.strptime(self.LastExerciseTime, "%A %B %d, %Y %H:%M:%S")
if (TimeNow - LastExerciseTime).days >= 14:
return True
return False
elif self.ExerciseFrequency.lower() == "monthly" and TimeNow.day == self.ExerciseDayOfMonth:
return True
else:
return False
except Exception as e1:
self.LogErrorLine("Error in TimeForExercise: " + str(e1))
return False
# ---------- GenExercise::GetGeneratorTime----------------------------------
def GetGeneratorTime(self):
try:
GenTimeStr = ""
data = self.SendCommand("generator: status_json")
Status = {}
Status = json.loads(data)
TimeDict = self.FindDictValueInListByKey("Time", Status["Status"])
if TimeDict != None:
TimeDictStr = self.FindDictValueInListByKey("Generator Time", TimeDict)
if TimeDictStr != None or not len(TimeDictStr):
GenTimeStr = TimeDictStr
# Format is "Wednesday March 6, 2019 13:10" or " "Friday May 3, 2019 11:11"
GenTime = datetime.datetime.strptime(GenTimeStr, "%A %B %d, %Y %H:%M")
else:
self.LogError("Error getting generator time! Genmon may be starting up.")
GenTime = datetime.datetime.now()
else:
self.LogError("Error getting generator time (2)!")
GenTime = datetime.datetime.now()
return GenTime
except Exception as e1:
self.LogErrorLine("Error in GetGeneratorTime: " + str(e1) + ": " + GenTimeStr)
return datetime.datetime.now()
# ---------- GenExercise::ExerciseThread------------------------------------
def ExerciseThread(self):
time.sleep(1)
while True:
try:
if not self.ExerciseActive:
if self.TimeForExercise():
self.StartExercise()
if self.WaitForExit("ExerciseThread", float(self.PollTime)):
return
except Exception as e1:
self.LogErrorLine("Error in ExerciseThread: " + str(e1))
if self.WaitForExit("ExerciseThread", float(self.PollTime)):
return
# ----------GenExercise::SignalClose----------------------------------------
def SignalClose(self, signum, frame):
self.Close()
sys.exit(1)
# ----------GenExercise::Close----------------------------------------------
def Close(self):
self.KillThread("ExerciseThread")
if self.ExerciseActive:
try:
self.WarmupTimer.cancel()
except:
pass
try:
self.StopTimer.cancel()
except:
pass
self.StopExercise()
self.Generator.Close()
#-------------------------------------------------------------------------------
if __name__ == "__main__":
console, ConfigFilePath, address, port, loglocation, log = MySupport.SetupAddOnProgram("genexercise")
GenExerciseInstance = GenExercise(log = log, loglocation = loglocation, ConfigFilePath = ConfigFilePath, host = address, port = port, console = console)
while True:
time.sleep(0.5)
sys.exit(1)