Exemplo n.º 1
0
class BoilingControll(object):
    def __init__(self, process):
        self.hop_order = process.recipe.get_hop_order()
        process.actual_hop = Hop.objects.get(pk=self.hop_order.get("1"))
        process.save()
        self.process = process
        self.serial_comunication = SerialCalls()

    def handle_states(self):
        state = self.process.state
        if state == STATES.get("warm_must"):
            logger.info("[Boiling] Function defined: warm_must")
            self.warm_must()
        elif state == STATES.get("add_hops"):
            logger.info("[Boiling] Function defined: add_hops")
            self.add_hops()
        elif state == STATES.get("continue_boiling"):
            logger.info("[Boiling] Function defined: continue_boiling")
            self.continue_boiling()

    def warm_must(self):
        temperature = ThermalSensor.get_current_temperature()
        logger.info("[Boiling] Temperature: %.2f" % temperature)
        if temperature < self.process.recipe.boiling_temperature:
            logger.info("[Boiling] Temperature less than actual " "heat temperature")
            self.serial_comunication.turn_on_resistor()
        else:
            logger.info("[Boiling] Temperature reached!")
            self.process.state = STATES.get("add_hops")
            self.process.boiling_stop_time = timezone.now() + timedelta(minutes=self.process.recipe.boiling_duration)
            logger.info("[Boining] State changed! New state: add_hops")
        self.process.save()

    def add_hops(self):
        if self.process.change_hop():
            self.serial_comunication.add_hop(self.process.next_hop)
            logger.info("[Boiling] Hop has been added to the pot!")
            if self.check_next():
                hops = Hop.objects.get(pk=self.get_next_hop())
                self.process.next_hop += 1
                self.process.actual_hop = hops
                self.process.state = STATES.get("add_hops")
                logger.info("[Boiling] State changed! New state: add_hops")
            else:
                self.process.state = STATES.get("continue_boiling")
                logger.info("[Boiling] State changed! " "New state: continue_boiling")

        self.process.save()

    def continue_boiling(self):
        if self.check_boiling_time_reached():
            logger.info("[Boiling] Boiling stage completed!" "New state: turn_on_chiller")
            self.process.state = cooling.STATES.get("turn_on_chiller")
            self.serial_comunication.turn_off_resistor(2)
        self.process.save()

    def check_next(self):
        if self.process.next_hop <= len(self.hop_order):
            return True
        else:
            return False

    def get_next_hop(self):
        next_hop = self.process.next_hop
        return self.hop_order.get(str(next_hop))

    def check_boiling_time_reached(self):
        now = timezone.now()

        if now >= self.process.boiling_stop_time:
            return True
        else:
            return False
Exemplo n.º 2
0
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))