class FilteringControll(object): def __init__(self, process): self.process = process self.serial_comunication = SerialCalls() def handle_states(self): state = self.process.state if state == STATES.get('open_pot_valve'): logger.info("[Filtering] Function defined: open_pot_valve") self.open_pot_valve() elif state == STATES.get('insert_water'): logger.info("[Filtering] Function defined: insert_water") self.insert_water() elif state == STATES.get('check_level'): logger.info("[Filtering] Function defined: check_level") self.check_level() elif state == STATES.get('stop_water'): logger.info("[Filtering] Function defined: stop_water") self.stop_water() def open_pot_valve(self): now = timezone.now() minutes = timedelta(minutes=1) if now > self.process.filtering_init + minutes: self.process.filtering_init = now logger.info("[Filtering] 20 minutes after filtering init") self.serial_comunication.activate_alarm() self.process.state = STATES.get('insert_water') self.process.save() logger.info("[Filtering] State changed! New state: insert_water") def insert_water(self): self.serial_comunication.insert_water() logger.info("[Filtering] Turn on the water") self.process.state = STATES.get('check_level') self.process.save() logger.info("[Filtering] State changed! New state: check_level") def check_level(self): logger.info("[Filtering] Checking level of pot2. . .") now = timezone.now() minutes = timedelta(minutes=10) if now > self.process.filtering_init + minutes: logger.info("[Filtering] Pot water level reached") self.process.state = STATES.get('stop_water') self.process.save() logger.info("[Filtering] State changed! New state: stop_water") else: logger.info("[Filtering] Pot water level not reached") def stop_water(self): self.serial_comunication.stop_water() logger.info("[Filtering] Closing valve") self.process.state = boiling.STATES.get('warm_must') self.process.save() self.serial_comunication.turn_on_resistor() logger.info("[Filtering] State changed! New state: warm_must " "(from boiling process)")
class FermentationControll(object): def __init__(self, process): self.process = process self.freezer_temperature = self.process.recipe.fermentation_temperature self.serial_comunication = SerialCalls() self.serial_comunication.activate_alarm() def handle_states(self): state = self.process.state if state == STATES.get('chill_must'): logger.info("[Fermentation] Function defined: chill_must") self.chill_must() elif state == STATES.get('maintain_temperature'): logger.info("[Fermentation] Function defined: " "maintain_temperature") self.maintain_temperature() def chill_must(self): self.serial_comunication.turn_on_freezer(self.freezer_temperature) logger.info('[Fermentation] Cooling must on the freezer') temperature = ThermalSensor.get_current_temperature() if temperature > self.freezer_temperature: logger.info("[Fermentation] Fermentation temperature not reached") self.process.state = STATES.get('chill_must') # Decrease temperature pass else: logger.info("[Fermentation] Temperature reached!") self.process.state = STATES.get('maintain_temperature') logger.info("[Fermentation] State changed! " "New state: maintain_temperature") self.process.save() def maintain_temperature(self): temperature = ThermalSensor.get_current_temperature() if self.freezer_temperature - 2 <= temperature \ <= self.freezer_temperature + 2: logger.info("[Fermentation] Temperature on range") self.process.state = STATES.get('process_end') logger.info("[Fermentation] State changed! " "New state: process_end") else: logger.info("[Fermentation] Temperature not on range") self.process.state = STATES.get('chill_must') pass self.process.save()
class BreweryControll(object): def __init__(self, process): self.heat_order = process.recipe.get_heat_order() process.actual_heat = Heat.objects.get(pk=self.heat_order.get('1')) process.save() self.process = process self.next_heat = '2' self.serial_comunication = SerialCalls() def handle_states(self): state = self.process.state if state == STATES.get('initial_boiling'): logger.info("[Brewery] Function defined: initial_boiling") self.initial_boiling() elif state == STATES.get('heating'): self.serial_comunication.turn_on_engine() self.heating() elif state == STATES.get('heat_controll'): logger.info("[Brewery] Function defined: heat_controll") self.heat_controll() def initial_boiling(self): boiling_temperature = self.process.recipe.initial_boiling_temperature self.serial_comunication.turn_on_resistor(boiling_temperature) temperature = ThermalSensor.get_current_temperature() logger.info("[Brewery] Temperature: %.2f" % temperature) if temperature < boiling_temperature: logger.info("[Brewery] Actual temperature is lower " "than %.2f" % boiling_temperature) self.serial_comunication.turn_on_resistor(boiling_temperature) pass else: logger.info("[Brewery] Actual temperature is greater " "than %.2f" % boiling_temperature) self.serial_comunication.turn_off_resistor(1) self.process.state = STATES.get('insert_malt') self.serial_comunication.activate_alarm() logger.info("[Brewery] State changed! New state: insert_malt") self.process.save() def heating(self): self.serial_comunication.turn_on_resistor( self.process.recipe.boiling_temperature) logger.info("[Brewery] Function defined: heating") temperature = ThermalSensor.get_current_temperature() logger.info("[Brewery] Temperature: %.2f" % temperature) if temperature < self.process.actual_heat.temperature: logger.info("[Brewery] Temperature less than actual " "heat temperature") self.serial_comunication.turn_on_resistor( self.process.actual_heat.temperature) else: logger.info("[Brewery] Temperature greater than " "actual_heat temperature") self.process.actual_heat_time = timezone.now() self.process.state = STATES.get('heat_controll') logger.info("[Brewery] State changed! New state: heat_controll") self.process.save() def heat_controll(self): if self.process.change_heat(): logger.info('[Brewery] Previous heating is done. Change heat!') if self.check_next(): heat = Heat.objects.get(pk=self.get_next_heat()) self.process.next_heat += 1 self.process.actual_heat = heat self.process.state = STATES.get('heating') logger.info("[Brewery] State changed! New state: heating") else: self.process.state = STATES.get('iodine_test') self.serial_comunication.activate_alarm() logger.info("[Brewery] State changed! New state: iodine_test") else: # Maintain temperature pass self.process.save() def check_next(self): if self.process.next_heat <= len(self.heat_order): return True else: return False def get_next_heat(self): next_heat = self.process.next_heat return self.heat_order.get(str(next_heat))