def __init__(self): self.logger = self.logSetup() alerts = Alerts(self.logger) alerts.alertInfo('Raspberry Pi: Running HappyFish.py') self.mqtt_email = os.environ["MQTT_EMAIL"] self.mqtt_password = os.environ["MQTT_PASSWORD"] self.logger.info('='*50) self.logger.info('Running main script') self.settings = Settings(self.logger, False) self.electronics = Electronics(self.logger, self.settings) self.logger.info('Updating the pwm modules for the first time') self.result = self.electronics.updateModule() self.reconnect_count = 0 self.reconnect_delay = 60 if self.result == True: self.logger.info('PWM modules updated. Electronics working as intended') else: self.logger.critical('Failed to light up the lab room. Check pwm modules') self.logger.critical('Terminating script. Please check hardware') alerts = Alerts(self.logger) alerts.alertCritical('Raspberry Pi: PWM Module cannot be opened') exit() self.connection = Connection(self.logger, self.settings, self.mqtt_email, self.mqtt_password) self.connection.start(self) self.reconnecting = False
def on_disconnect(self, client, userdata, flags, rc=0): self.logger.critical('Connection disconnected, return code: ' + str(rc)) self.happyfish.reconnect_delay = self.happyfish.reconnect_delay * 2 self.connection_closed = True self.time_ended = time() alerts = Alerts(self.logger) alerts.alertCritical(f'RPi disconnected from the MQTT server. RC {rc}')
def on_connect(self, client, userdata, flags, rc): self.is_connecting = False if rc == 0: self.established_connection = True self.logger.info('Connected with MQTT broker, result code: ' + str(rc)) self.happyfish.reconnect_delay = 60 alerts = Alerts(self.logger) alerts.alertInfo('Raspberry Pi connected to MQTT successfully') else: self.established_connection = False self.failed_connection = True self.logger.critical('Bad connection, result code: ' + str(rc)) self.happyfish.reconnect_delay = self.happyfish.reconnect_delay * 2 alerts = Alerts(self.logger) alerts.alertCritical( f'Raspberry Pi could NOT connect to MQTT. Bad Connection. RC {rc}' )
def start(self): try: self.logger.info('Running main loop') while True: sleep(0.5) self.electronics.updateModule() if not self.reconnecting and self.connection.connection_closed and self.reconnect_count < 15: self.logger.critical(f'Connection appears to be closed... Ending connection and will reconnect after {self.reconnect_delay/60} min(s)') alerts = Alerts(self.logger) alerts.alertCritical(f'Connection appears to be closed. Reconnecting again in {self.reconnect_delay/60} min(s). Reconnect count is {self.reconnect_count}') self.connection.end() self.reconnecting = True self.timer = Timer(self.reconnect_delay, self.reconnect, args=None, kwargs=None) self.timer.start() except KeyboardInterrupt: self.logger.critical('Script manually terminated') self.connection.end() self.settings.printConfig() self.logger.info('Script ended. Shutting down the lights') self.settings.turnAllOff() self.result = self.electronics.updateModule() if self.result == True: self.logger.info('Successfully turned off all the lights') else: self.logger.critical('Failed to turn off the lights. Unable to communicate with pwm module') alerts = Alerts(self.logger) alerts.alertCritical('HappyFish script got terminated... Unknown reason') self.ended = True