Changeset - 195dbcc38137
[Not reviewed]
refactor
0 2 0
matthewreed - 8 years ago 2017-09-22 16:13:40

Working on alerts - loading alerts from config and alert objects
2 files changed with 127 insertions and 0 deletions:
0 comments (0 inline, 0 general)
alerts.py
Show inline comments
 
@@ -9,12 +9,40 @@ class AlertManager:
 
        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
 
@@ -23,6 +51,36 @@ class AlertManager:
 
        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)
 
hydrobot_example.conf
Show inline comments
 
@@ -21,6 +21,75 @@ off_time: [None, None, None, None, 30]
 
module: RelayDriveOut
 
output: Output2
 
trigger: interval
 
#weeks, days, hours, minutes, seconds
 
on_duration: [0, 0, 0, 0, 20]
 
off_duration: [0, 0, 0, 0, 10]
 

	
 
[alert1]
 
name: "Pump"
 
type: output_feedback
 
output_module: RelayDrive1
 
output: output_1
 
input_module: RelayDrive1
 
input: input_1
 
on_threshold: 50
 
off_threshold: 1
 
deadband: 5
 

	
 
[alert2]
 
name: "Light"
 
type: output_feedback
 
output_module: RelayDrive1
 
output: output_1
 
input_module: AirSense1
 
input: ambient_light
 
on_threshold: 5000
 
off_threshold: 500
 
deadband: 10
 

	
 
[alert3]
 
name: "Lamp Dim"
 
type: output_feedback
 
output_module: RelayDrive1
 
output: output_1
 
input_module: AirSense1
 
input: ambient_light
 
on_threshold: 10000
 
off_threshold: None
 
deadband: 10
 

	
 
[alert4]
 
name: "Air Temperature"
 
type: measurement
 
input_module: AirSense1
 
input: air_temp
 
high_threshold: 35
 
low_threshold: 15
 
deadband: 10
 

	
 
[alert5]
 
name: "Air Humidity"
 
type: measurement
 
input_module: AirSense1
 
input: air_humid
 
high_threshold: 80
 
low_threshold: 30
 
deadband: 10
 

	
 
[alert6]
 
name: "Water Level"
 
type: measurement
 
input_module: WaterSense1
 
input: water_level
 
high_threshold: None
 
low_threshold: 50
 
deadband: 10
 

	
 
[alert7]
 
name: "Water Temperature"
 
type: measurement
 
input_module: WaterSense1
 
input: water_temp
 
high_threshold: 30
 
low_threshold: 20
 
deadband: 10
 
\ No newline at end of file
0 comments (0 inline, 0 general)