forked from jgyates/genmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgensms.py
158 lines (123 loc) · 4.51 KB
/
gensms.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
#!/usr/bin/env python
#------------------------------------------------------------
# FILE: gensms.py
# PURPOSE: genmon.py support program to allow SMS (txt messages)
# to be sent when the generator status changes
#
# AUTHOR: Jason G Yates
# DATE: 05-Apr-2016
#
# MODIFICATIONS:
#------------------------------------------------------------
import datetime, time, sys, signal, os, threading, socket
import atexit
from genmonlib import mynotify, mylog
try:
from ConfigParser import RawConfigParser
except ImportError as e:
from configparser import RawConfigParser
from twilio.rest import Client
#---------- Signal Handler ------------------------------------------
def signal_handler(signal, frame):
GenNotify.Close()
sys.exit(0)
#---------- OnRun ------------------------------------------
def OnRun(Active):
if Active:
print "Generator Running"
SendNotice("Generator Running")
else:
print "Generator Running End"
#---------- OnRunManual ------------------------------------------
def OnRunManual(Active):
if Active:
print "Generator Running in Manual Mode"
SendNotice("Generator Running in Manual Mode")
else:
print "Generator Running in Manual Mode End"
#---------- OnExercise ------------------------------------------
def OnExercise(Active):
if Active:
print "Generator Exercising"
SendNotice("Generator Exercising")
else:
print "Generator Exercising End"
#---------- OnReady ------------------------------------------
def OnReady(Active):
if Active:
print "Generator Ready"
SendNotice("Generator Ready")
else:
print "Generator Ready End"
#---------- OnOff ------------------------------------------
def OnOff(Active):
if Active:
print "Generator Off"
SendNotice("Generator Off")
else:
print "Generator Off End"
#---------- OnManual ------------------------------------------
def OnManual(Active):
if Active:
print "Generator Manual"
SendNotice("Generator Manual")
else:
print "Generator Manual End"
#---------- OnAlarm ------------------------------------------
def OnAlarm(Active):
if Active:
print "Generator Alarm"
SendNotice("Generator Alarm")
else:
print "Generator Alarm End"
#---------- OnService ------------------------------------------
def OnService(Active):
if Active:
print "Generator Service Due"
SendNotice("Generator Service Due")
else:
print "Generator Servcie Due End"
#---------- SendNotice ------------------------------------------
def SendNotice(Message):
try:
client = Client(account_sid, auth_token)
message = client.messages.create(
to= to_number,
from_ = from_number,
body = Message)
print(message.sid)
except Exception as e1:
log.error("Error: " + str(e1))
print ("Error: " + str(e1))
#------------------- Command-line interface for gengpio -----------------#
if __name__=='__main__': # usage program.py [server_address]
address='127.0.0.1' if len(sys.argv)<2 else sys.argv[1]
# Set the signal handler
signal.signal(signal.SIGINT, signal_handler)
try:
log = mylog.SetupLogger("client", "/var/log/gensms.log")
# read config file
config = RawConfigParser()
# config parser reads from current directory, when running form a cron tab this is
# not defined so we specify the full path
config.read('/etc/gensms.conf')
account_sid = config.get('gensms', 'accountsid')
auth_token = config.get('gensms', 'authtoken')
to_number = config.get('gensms', 'to_number')
from_number = config.get('gensms', 'from_number')
GenNotify = mynotify.GenNotify(
host = address,
onready = OnReady,
onexercise = OnExercise,
onrun = OnRun,
onrunmanual = OnRunManual,
onalarm = OnAlarm,
onservice = OnService,
onoff = OnOff,
onmanual = OnManual,
log = log)
while True:
time.sleep(1)
except Exception as e1:
log.error("Error: " + str(e1))
print ("Error: " + str(e1))