forked from jgyates/genmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgengpio.py
270 lines (219 loc) · 10.5 KB
/
gengpio.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
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# FILE: gengpio.py
# PURPOSE: genmon.py support program to allow GPIO pints to drive
# status LEDs
#
# AUTHOR: Jason G Yates
# DATE: 05-Apr-2016
#
# MODIFICATIONS:
#-------------------------------------------------------------------------------
import datetime, time, sys, signal, os, threading, socket, json
import atexit, getopt
try:
from genmonlib.mylog import SetupLogger
from genmonlib.myclient import ClientInterface
from genmonlib.mysupport import MySupport
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)
import RPi.GPIO as GPIO
#---------- Signal Handler ----------------------------------------------------
def signal_handler(signal, frame):
GPIO.cleanup()
MyClientInterface.Close()
sys.exit(0)
#------------------- Command-line interface for gengpio ------------------------
if __name__=='__main__': # usage program.py [server_address]
address=ProgramDefaults.LocalHost
try:
console = SetupLogger("gengpio_console", log_file = "", stream = True)
if os.geteuid() != 0:
console.error("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
sys.exit(2)
HelpStr = '\nsudo python gengpio.py -a <IP Address or localhost> -c <path to genmon config file>\n'
try:
ConfigFilePath = ProgramDefaults.ConfPath
opts, args = getopt.getopt(sys.argv[1:],"hc:a:",["help","configpath=","address="])
except getopt.GetoptError:
console.error("Invalid command line argument.")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
console.error(HelpStr)
sys.exit()
elif opt in ("-a", "--address"):
address = arg
elif opt in ("-c", "--configpath"):
ConfigFilePath = arg
ConfigFilePath = ConfigFilePath.strip()
except Exception as e1:
console.error("Error : " + str(e1))
sys.exit(1)
try:
port, loglocation = MySupport.GetGenmonInitInfo(ConfigFilePath, log = console)
log = SetupLogger("client", loglocation + "gengpio.log")
except Exception as e1:
console.error("Error : " + str(e1))
sys.exit(1)
try:
# Set the signal handler
signal.signal(signal.SIGINT, signal_handler)
MyClientInterface = ClientInterface(host = address, port = port, log = log)
#setup GPIO using Board numbering
GPIO.setmode(GPIO.BOARD)
console.info( GPIO.RPI_INFO)
GPIO.setwarnings(False)
# These are the GPIP pins numbers on the Raspberry PI GPIO header
# https://www.element14.com/community/servlet/JiveServlet/previewBody/73950-102-10-339300/pi3_gpio.png
STATUS_READY = 16 # READY GPIO 23 (pin 16)
STATUS_ALARM = 18 # ALARM GPIO 24 (pin 18)
STATUS_SERVICE = 22 # SERVICE DUE GPIO 25 (pin 22)
STATUS_RUNNING = 26 # RUNNING GPIO 7 (pin 26)
STATUS_EXERCISING = 24 # EXERCISING GPIO 8 (pin 24)
STATUS_OFF = 21 # OFF GPIO 9 (pin 21)
# Set additional GPIO based on these error codes
ER_GENMON = 3 # Genmon is reporting errors due to modbus or internal problems GPIO 2(pin3)
ER_INTERNET = 5 # No internet connection GPIO3 (pin 5)
# Overspeed/Underspeed (alarms 1200-1206, 1600-1603) GPIO 5 (pin 29)
ER_SPEED = 29
# Low Oil (alarm 1300) GPIO 6 (pin 31)
ER_LOW_OIL = 31
# High Temp (alarm 1400) GPIO 13 (pin 33
ER_HIGH_TEMP = 33
# RPM Sensor (alarm 1500-1521) GPIO 19 (pin 35)
ER_RPM_SENSE = 35
# Overvoltage/Undervoltage (alarm1800-1803, 1900-1906) GPIO 26 (pin 37)
ER_VOLTAGE = 37
# Overcrank (alarm 1100-1101) GPIO 21 (pin 40)
ER_OVERCRANK = 40
# Overload (alarm 2100-2103) GPIO 20 (pin 38)
ER_OVERLOAD = 38
# Governor (alarm 2500-2502) GPIO 16 (pin 36)
ER_GOVERNOR = 36
# Evolution Air Cooled Warning GPIO 12 (pin 32)
ER_WARNING = 32
GPIO.setup(STATUS_READY, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(STATUS_ALARM, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(STATUS_SERVICE, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(STATUS_RUNNING, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(STATUS_EXERCISING, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(STATUS_OFF, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_GENMON, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_INTERNET, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_SPEED, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_LOW_OIL, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_HIGH_TEMP, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_RPM_SENSE, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_VOLTAGE, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_OVERCRANK, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_OVERLOAD, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_GOVERNOR, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(ER_WARNING, GPIO.OUT, initial=GPIO.LOW)
LastEvent = ""
data = MyClientInterface.ProcessMonitorCommand("generator: monitor")
if "evolution" in data.lower():
Evolution = True
console.info ("Evolution Controller Detected\n")
else:
Evolution = False
console.info ("Non Evolution Controller Detected\n")
while True:
data = MyClientInterface.ProcessMonitorCommand("generator: getbase")
if LastEvent != data:
LastEvent = data
console.info ("State: " + data)
if data == "READY":
GPIO.output(STATUS_READY,GPIO.HIGH)
else:
GPIO.output(STATUS_READY,GPIO.LOW)
if data == "EXERCISING":
GPIO.output(STATUS_EXERCISING,GPIO.HIGH)
else:
GPIO.output(STATUS_EXERCISING,GPIO.LOW)
if data == "RUNNING" or data == "RUNNING-MANUAL":
GPIO.output(STATUS_RUNNING,GPIO.HIGH)
else:
GPIO.output(STATUS_RUNNING,GPIO.LOW)
if data == "ALARM":
GPIO.output(STATUS_ALARM,GPIO.HIGH)
else:
GPIO.output(STATUS_ALARM,GPIO.LOW)
if data == "SERVICEDUE":
GPIO.output(STATUS_SERVICE,GPIO.HIGH)
else:
GPIO.output(STATUS_SERVICE, GPIO.LOW)
if data == "OFF" or data == "MANUAL":
GPIO.output(STATUS_OFF,GPIO.HIGH)
else:
GPIO.output(STATUS_OFF, GPIO.LOW)
if data == "ALARM" and Evolution: # Last Error Code not supported by Nexus
# get last error code
data = MyClientInterface.ProcessMonitorCommand("generator: getregvalue=05f1")
LastErrorCode = int(data,16)
# Overspeed/Underspeed (alarms 1200-1206, 1600-1603)
if 1200 <= LastErrorCode <= 1206 or 1600 <= LastErrorCode <= 1603:
GPIO.output(ER_SPEED,GPIO.HIGH)
# Low Oil (alarm 1300)
if LastErrorCode == 1300:
GPIO.output(ER_LOW_OIL,GPIO.HIGH)
# High Temp (alarm 1400)
if LastErrorCode == 1400:
GPIO.output(ER_HIGH_TEMP,GPIO.HIGH)
# RPM Sensor (alarm 1500-1521)
if 1500 <= LastErrorCode <= 1521:
GPIO.output(ER_RPM_SENSE,GPIO.HIGH)
# Overvoltage/Undervoltage (alarm 1800-1803, 1900-1906)
if 1800 <= LastErrorCode <= 1803 or 1900 <= LastErrorCode <= 1906:
GPIO.output(ER_VOLTAGE,GPIO.HIGH)
# Overcrank (alarm 1100-1101)
if 1100 <= LastErrorCode <= 1101:
GPIO.output(ER_OVERCRANK,GPIO.HIGH)
# Overload (alarm 2100-2103)
if 2100 <= LastErrorCode <= 2103:
GPIO.output(ER_OVERLOAD,GPIO.HIGH)
# Governor (alarm 2500-2502)
if 2500 <= LastErrorCode <= 2502:
GPIO.output(ER_GOVERNOR,GPIO.HIGH)
# Warning (alarm 0000)
if 0000 == LastErrorCode:
GPIO.output(ER_WARNING,GPIO.HIGH)
else:
GPIO.output(ER_SPEED,GPIO.LOW)
GPIO.output(ER_LOW_OIL,GPIO.LOW)
GPIO.output(ER_HIGH_TEMP,GPIO.LOW)
GPIO.output(ER_RPM_SENSE,GPIO.LOW)
GPIO.output(ER_VOLTAGE,GPIO.LOW)
GPIO.output(ER_OVERCRANK,GPIO.LOW)
GPIO.output(ER_OVERLOAD,GPIO.LOW)
GPIO.output(ER_GOVERNOR,GPIO.LOW)
GPIO.output(ER_WARNING,GPIO.LOW)
# Get Genmon status
try:
data = MyClientInterface.ProcessMonitorCommand("generator: monitor_json")
TempDict = {}
TempDict = json.loads(data)
HealthStr = TempDict["Monitor"][0]["Generator Monitor Stats"][0]["Monitor Health"]
if HealthStr.lower() == "ok":
GPIO.output(ER_GENMON,GPIO.LOW)
else:
GPIO.output(ER_GENMON,GPIO.HIGH)
except Exception as e1:
log.error("Error getting monitor health: " +str(e1))
# get Internet Status
try:
data = MyClientInterface.ProcessMonitorCommand("generator: network_status")
if data.lower() == "ok":
GPIO.output(ER_INTERNET,GPIO.LOW)
else:
GPIO.output(ER_INTERNET,GPIO.HIGH)
time.sleep(3)
except Exception as e1:
log.error("Error getting internet status: " +str(e1))
except Exception as e1:
log.error("Error: " + str(e1))
console.error ("Error: " + str(e1))