import smtplib
from email.mime.text import MIMEText
import configparser
import logging
class AlertManager:
def __init__(self, config):
self.logger = logging.getLogger('hydrobot')
self.from_addr = config.get("alerts", "from_address")
self.to_addr = config.get("alerts", "to_address")
self.mail_server = config.get("alerts", "mail_server")
self.username = config.get("alerts", "username")
self.password = config.get("alerts", "password")
self.alerts = []
load_alerts(config)
def load_alerts(self, config):
for section in self.config.sections():
if "alert" in section:
type = self.config.get(section, "type")
if type == "output_feedback":
name = self.config.get(section, "name")
output_module = self.config.get(section, "output_module")
output = self.config.get(section, "output")
input_module = self.config.get(section, "input_module")
input = self.config.get(section, "input")
on_threshold = self.config.get(section, "on_threshold")
off_threshold = self.config.get(section, "off_threshold")
deadband = self.config.get(section, "deadband")
self.alerts.append(OutputFeedbackAlert(name, output_module, output, input_module, input, on_threshold, off_threshold, deadband))
elif type == "measurement":
name = self.config.get(section, "name")
input_module = self.config.get(section, "input_module")
input = self.config.get(section, "input")
high_threshold = self.config.get(section, "high_threshold")
low_threshold = self.config.get(section, "low_threshold")
deadband = self.config.get(section, "deadband")
self.alerts.append(MeasurementAlert(name, input_module, input, high_threshold, low_threshold, deadband))
def send_alert(self, message):
msg = MIMEText(message)
msg['Subject'] = "HydroBot Alert"
msg['From'] = self.from_addr
msg['To'] = self.to_addr
server = smtplib.SMTP(self.mail_server)
server.ehlo()
server.starttls()
server.login(self.username, self.password)
server.send_message(msg)
server.quit()
def evaluate_alerts(self):
for alert in self.alerts:
alert.evalutate()
class Alert(metaclass=ABCMeta):
def __init__(self, name):
self.name = name
@abstractmethod
def evalutate(self):
pass
class OutputFeedbackAlert(Alert):
def __init__(self, name, output_module, output, input_module, input, on_threshold, off_threshold, deadband):
super(Alert, self).__init__(name)
def evalutate(self):
print(self.name)
class MeasurementAlert(Alert):
def __init__(self, name, input_module, input, high_threshold, low_threshold, deadband):
super(Alert, self).__init__(name)
def evalutate(self):
print(self.name)