def start(self, duration): if duration == HeatService.DEFAULT_TIME: self.bt = BrewTimer(duration, self.alive) else: self.bt = BrewTimer(duration, self.switch) self.switch(PowerStrip.ON) self.bt.start()
def run(self): self.state_params = self.simplestate.start() self.pid.set_setpoint(self.state_params["temp"]) try: old_calculation_time = None while True: msg_type, msg = self.receiver.receive() print("msg_type", msg_type) print("msg", msg) if msg_type == TYPE_TEMP: temp_current, sensor_id = self.convert_temp(msg) # computes timedelta for pid calculation_time = int(round(time.time() * 1000)) if old_calculation_time: self.pid.set_sample_time(calculation_time - old_calculation_time) old_calculation_time = calculation_time # calculates PID output value output = self.pid.compute(temp_current) # switches powerstrip based on output value self.heatservice.temp_actor(output) logging.warning({ "temp_actual": temp_current, "change": output, "state": self.state_params, "sensor": sensor_id }) timer_passed_checked = 0.0 if self.brew_timer is not None: timer_passed_checked = self.brew_timer.passed() log.log(temp_current, self.state_params["temp"], output, sensor_id, self.simplestate.state, self.brew_id, int(self.brew_timer.passed())) else: log.log(temp_current, self.state_params["temp"], output, sensor_id, self.simplestate.state, self.brew_id) print("temp_current", temp_current, "outout", output, "state_temp", self.state_params["temp"], "timer_passed", timer_passed_checked) if self.state_params["auto"] == True and self.state_params[ "temp"] - TEMP_TOLERANCE <= temp_current: if self.brew_timer is None: print("Start BrewTimer for ", self.simplestate.state, "and", self.state_params["time"], "seconds") self.brew_timer = BrewTimer( self.state_params["time"], self.next_state) self.brew_timer.start() elif msg_type == TYPE_CONTROL: if self.brew_timer: self.brew_timer.cancel() self.next_state() finally: self.receiver.cleanup()