def write_config_exit(config_path): if not AlarmConfig.exists(config_path): logging.info('Writing configuration to %s and exiting.\n', config_path) AlarmConfig.create(config_path) else: logging.info( 'Configuration at %s already exists, skipping write\n', config_path) sys.exit(0)
def __init__(self): self.pin = int(AlarmConfig.get('RpiArmDisarm', 'gpio_pin')) if not self.valid_setup(): return self.alarm = AlarmStatus() self._initialize_rpi_gpio()
def create_params(events): timestamp, message = create_message(events) data = { 'token': AlarmConfig.get('PushoverNotification', 'token'), 'user': AlarmConfig.get('PushoverNotification', 'user'), 'timestamp': timestamp, 'title': 'Alarm Notification', 'message': message, } device = AlarmConfig.get('PushoverNotification', 'device') if device: data['device'] = device priority = AlarmConfig.get('PushoverNotification', 'priority') if priority: data['priority'] = priority return data
def create_or_check_required_config(path): if not AlarmConfig.exists(path): logging.info('Configuration missing, writing to %s.\n\n', path) AlarmConfig.create(path) AlarmConfig.load(path) missing_config = AlarmConfig.validate(AlarmConfig.get()) if missing_config: logging.error( 'The following required configuration is missing from %s\n\n', path) logging.error('\n'.join(missing_config)) logging.error('\n\nExiting\n\n') sys.exit(-1)
def alarm_main_loop(): phone_number = AlarmConfig.get('Main', 'phone_number') alarm_status = AlarmStatus() alarm_system = AlarmSystem() with open(tigerjet.hidraw_path(), 'rb') as alarmhid: with json_ipc.ServerSock() as sockfd: logging.info("Ready, listening for alarms") while True: read = [] read, _, _ = select([alarmhid, sockfd], [], []) if alarmhid in read: process_alarm_event(alarmhid, phone_number, alarm_status) if sockfd in read: process_sock_request(sockfd, alarm_system) return 0
def notify(events): if not events: return if not AlarmConfig.get('PushoverNotification'): return logging.info("Sending pushover notification...") data = create_params(events) pushover_uri = 'https://api.pushover.net/1/messages.json' response = requests.post(pushover_uri, data=data) if response.status_code == 200: logging.info("Sending complete") else: err_list = response.json().get('errors') status_code = response.status_code logging.error('Error sending pushover notification HTTP %s: %s', status_code, ', '.join(err_list))
def notify(events): if not events: return if not AlarmConfig.get('EmailNotification'): return logging.info("Sending email...") username = AlarmConfig.get('EmailNotification', 'username') password = AlarmConfig.get('EmailNotification', 'password') to_addr = AlarmConfig.get('EmailNotification', 'notification_email') subject = AlarmConfig.get('EmailNotification', 'notification_subject') tls = AlarmConfig.get('EmailNotification', 'tls') server = AlarmConfig.get('EmailNotification', 'server_address') server_port = AlarmConfig.get('EmailNotification', 'port') msg = MIMEMultipart('alternative') msg['From'] = username msg['To'] = to_addr msg['Subject'] = subject body = create_message(events) msg.attach(MIMEText(body, 'plain')) msg.attach(MIMEText(body, 'html')) try: s = smtplib.SMTP(server, server_port) s.ehlo() if tls.lower() in ("yes", "true", "t", "1"): s.starttls() s.ehlo() s.login(username, password) s.sendmail(username, [to_addr], msg.as_string()) s.quit() logging.info("Email send complete") except smtplib.SMTPException as exc: logging.error("Error sending email: %s", str(exc))
def get_zone_name(sensor_code): zone_name = AlarmConfig.get('ZoneMapping', sensor_code) if not zone_name: zone_name = 'Zone %s' % sensor_code return zone_name