def getBrightnessPercentage(self): raw_stage = self.getStageInfo() current_stage = raw_stage[0] seconds = raw_stage[1] if current_stage != self.stage: self.logger.info('Scheduled stage changed from \'' + self.stage + '\' to \'' + current_stage + '\'') alerts = Alerts(self.logger) alerts.alertInfo('Scheduled stage changed from \'' + self.stage + '\' to \'' + current_stage + '\'') self.stage = current_stage if self.stage == Stages.pre_sun_rise or self.stage == Stages.post_sun_set: return 0.0 if self.stage == Stages.sun_rise: return float(seconds - self.sunrise_start) / float( self.duration_seconds) if self.stage == Stages.sun_set: return 1 - (float(seconds - self.sunset_start) / float(self.duration_seconds)) return 1.0
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_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}' )