-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIOTEventNotification.py
175 lines (126 loc) · 5.46 KB
/
IOTEventNotification.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
#!/usr/bin python
import os
import time
#import logging
#import logging.handlers
from alerts.spark import SparkRoomAlert
from alerts.local import PrintAlertClass
from sensors.weather import WeatherUndergroundSensor
from sensors.airquality import BreezeometerAQISensor
from sensors.simsensor import SimulatedSensor
from sensors.base import GenericSensorClass
from ConfigParser import SafeConfigParser
from alerts.tropo import TropoAlert
from log_conf import LoggerManager
"""
MAIN ROUTINE
=====================================================
This is the main routine of the generic event driven
architecture application.
It is a very simple function that uses a time function
to sleep between polling intervals.
#####################################################
"""
LOGDIR = os.getenv("CAF_APP_LOG_DIR")
LoggerManager.SetPath(LOGDIR)
LoggerManager.InitializeLogger()
LoggerManager.logger.info("IOT Event Notification Starting....")
# Get hold of the configuration file (package_config.ini)
moduledir = os.path.abspath(os.path.dirname(__file__))
BASEDIR = os.getenv("CAF_APP_PATH", moduledir)
# If we are not running with CAF, use the BASEDIR to get cfg file
tcfg = os.path.join(BASEDIR, "package_config.ini")
CONFIG_FILE = os.getenv("CAF_APP_CONFIG_FILE", tcfg)
cfg = SafeConfigParser()
cfg.read(CONFIG_FILE)
# Read the application specific paramters
success_string = cfg.get("application","success_string")
failure_string = cfg.get("application","failure_string")
delay_time = float(cfg.get("application","delay"))
ops = cfg.get("application", "operator")
# Set the Log Level
log_level = int(cfg.get("application","log_level"))
LoggerManager.logger.setLevel(log_level)
LoggerManager.logger.info("Configuration Paramters:")
LoggerManager.logger.info("Delay Time: " + str(delay_time)+" secs.")
LoggerManager.logger.info("Success Message \""+success_string+"\"")
LoggerManager.logger.info("Failure Message \""+failure_string+"\"")
LoggerManager.logger.info("Operator for Comparison: '"+ ops + "'")
# define the sensor object that will be used to retrieve data from the sensor
# This snippet of code will instantiate the Weather Underground Sensor
if (cfg.get("wunderground","enabled") == "True"):
LoggerManager.logger.info("Weatherunderground Sensor Enabled...")
key = cfg.get("wunderground", "api_key")
zip = cfg.get("wunderground", "zipcode")
comparedata = cfg.get("wunderground","compare_data")
sensor = WeatherUndergroundSensor(key,zip)
if (cfg.get("wunderground", "logging") == "True"):
sensor.log = True
sensor.comparedata = comparedata
elif (cfg.get("simulatedsensor","enabled") == "True"):
# This snippet of code will instantiate the Simulated Sensor
LoggerManager.logger.info("Simulated Sensor Enabled...")
comparedata = cfg.get("simulatedsensor","compare_data")
sensor = SimulatedSensor()
if (cfg.get("simulatedsensor", "logging") == "True"):
sensor.log = True
sensor.comparedata = int(comparedata)
elif (cfg.get("breezometeraqi","enabled") == "True"):
# This snippet of code will instantiate the BreezometerAQL Sensor
LoggerManager.logger.info("Breezometer AQI Sensor Enabled...")
comparedata = cfg.get("breezometeraqi","compare_data")
key = cfg.get("breezometeraqi", "api_key")
lat = cfg.get("breezometeraqi", "lat")
long = cfg.get("breezometeraqi", "long")
sensor = BreezeometerAQISensor(key,lat,long)
if (cfg.get("breezometeraqi", "logging") == "True"):
sensor.log = True
sensor.comparedata = int(comparedata)
else:
LoggerManager.logger.error("ERROR: At least one sensor must be defined. Please enable at least one sensor.")
exit (-1)
# Define the alerts we want to use
cancontinue = False
# This code will instantiate the Print Alert Class - Check to see if it is enabled
if (cfg.get("print","enabled") == "True"):
LoggerManager.logger.info("Print Alert Enabled for output...")
screen = PrintAlertClass()
if (cfg.get("print","logging") == "True"):
screen.log = True
sensor.add_alert(screen)
cancontinue = True
# This code will instantiate the Tropo Alert Class - Check to see if it is enabled
if (cfg.get("tropo","enabled") == "True"):
LoggerManager.logger.info("Tropo Alert Enabled for output...")
tropo = TropoAlert(cfg)
if (cfg.get("tropo","logging") == "True"):
tropo.log = True
sensor.add_alert(tropo)
cancontinue = True
# This code will instantiate the Tropo Alert Class - Check to see if it is enabled
if (cfg.get("spark","enabled") == "True"):
LoggerManager.logger.info("Spark Alert Enabled for output...")
spark = SparkRoomAlert(cfg)
if (cfg.get("spark","logging") == "True"):
spark.log = True
sensor.add_alert(spark)
cancontinue = True
if not cancontinue:
LoggerManager.logger.error("ERROR: No alerts are enabled. Please enable at least one alert.")
exit(-1)
# Let's loop forever
while True:
# Let's get data from the sensor
sensor.read()
currentdate = time.strftime("%b %d %Y, %H:%M:%S ", time.gmtime())
"""
Let's compare the data that was retrieved with the
value to determine the appropriate action
"""
if sensor.compare(sensor.comparedata,ops):
sensor.send_alerts(currentdate + success_string + " (Sensor Hits/Sensor Reads) ("+
str(sensor.sensorcount)+"/"+ str(sensor.totalcount)+")")
else:
sensor.send_alerts(currentdate + failure_string)
# Sleep for an appropriate time
time.sleep(delay_time)