Exemple #1
0
 def __init__(self):
     self.config = BrewConfig()
     self.powerstrip = PowerStrip(self.config.get("powerstrip")["url"])
     self.powerstrip.all_off()
     self.heatservice = HeatService()
     self.simplestate = SimpleState()
     self.brew_id = int(round(time.time() * 1000))
     print("Brew ID:", self.brew_id)
     self.init_pid()
     self.receiver = IPCReceiver(BrewConfig.BRAUBAR_QUEUE)
     print("opened message queue", self.receiver.name)
Exemple #2
0
 def __init__(self):
     self.config = BrewConfig()
     self.powerstrip = PowerStrip(self.config.get("powerstrip")["url"])
     self.powerstrip.all_off()
     self.heatservice = HeatService()
     self.simplestate = SimpleState()
     self.brew_id = int(round(time.time() * 1000))
     print("Brew ID:", self.brew_id)
     self.init_pid()
     self.receiver = IPCReceiver(BrewConfig.BRAUBAR_QUEUE)
     print("opened message queue", self.receiver.name)
Exemple #3
0
class BrewDaemon:
    pid = None
    temp_control = None
    read_temp_socket = None
    temp_event = None
    brew_timer = None
    brew_id = None
    chart_service = None
    config = None
    heatservice = None
    state_params = None
    receiver = None

    def __init__(self):
        self.config = BrewConfig()
        self.powerstrip = PowerStrip(self.config.get("powerstrip")["url"])
        self.powerstrip.all_off()
        self.heatservice = HeatService()
        self.simplestate = SimpleState()
        self.brew_id = int(round(time.time() * 1000))
        print("Brew ID:", self.brew_id)
        self.init_pid()
        self.receiver = IPCReceiver(BrewConfig.BRAUBAR_QUEUE)
        print("opened message queue", self.receiver.name)

    def init_pid(self):
        self.pid = Pid(BrewConfig.P, BrewConfig.I, BrewConfig.D)
        self.pid.set_setpoint(0.0)
        self.pid.set_sample_time(1000.0)
        self.pid.set_output_limits(BrewConfig.MIN, BrewConfig.MAX)

    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()

    def next_state(self):
        self.state_params = self.simplestate.next()
        if self.state_params["temp"] == 0.0:
            self.pid.set_mode(Pid.MANUAL)
        else:
            self.pid.set_mode(Pid.AUTOMATIC)
        self.pid.set_setpoint(self.state_params["temp"])
        self.brew_timer = None

    def convert_temp(self, temp_json):
        sensor_id = -1
        temp_response = json.loads(temp_json)
        try:
            sensor_id = temp_response["id"]
            temp = float(temp_response["temp"])
        except ValueError:
            temp = 0.0
            print("Could not get correct temperature value")
        return temp, sensor_id

    def start_flask(self, host=HOST_IP, brew_id=None):
        args = ["python3", FLASK_FILE, "--host", host]
        if brew_id:
            args.append("--id")
            args.append(str(brew_id))
        subprocess.Popen(args)

    def start_sensor_server(self, host=HOST_IP, port=None):
        args = ["python3", SENSOR_SERVER_FILE, host, str(port)]
        subprocess.Popen(args)

    def shutdown(self):
        self.powerstrip.all_off()
        self.powerstrip.logout()
Exemple #4
0
class BrewDaemon:
    pid = None
    temp_control = None
    read_temp_socket = None
    temp_event = None
    brew_timer = None
    brew_id = None
    chart_service = None
    config = None
    heatservice = None
    state_params = None
    receiver = None

    def __init__(self):
        self.config = BrewConfig()
        self.powerstrip = PowerStrip(self.config.get("powerstrip")["url"])
        self.powerstrip.all_off()
        self.heatservice = HeatService()
        self.simplestate = SimpleState()
        self.brew_id = int(round(time.time() * 1000))
        print("Brew ID:", self.brew_id)
        self.init_pid()
        self.receiver = IPCReceiver(BrewConfig.BRAUBAR_QUEUE)
        print("opened message queue", self.receiver.name)

    def init_pid(self):
        self.pid = Pid(BrewConfig.P, BrewConfig.I, BrewConfig.D)
        self.pid.set_setpoint(0.0)
        self.pid.set_sample_time(1000.0)
        self.pid.set_output_limits(BrewConfig.MIN, BrewConfig.MAX)

    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()

    def next_state(self):
        self.state_params = self.simplestate.next()
        if self.state_params["temp"] == 0.0:
            self.pid.set_mode(Pid.MANUAL)
        else:
            self.pid.set_mode(Pid.AUTOMATIC)
        self.pid.set_setpoint(self.state_params["temp"])
        self.brew_timer = None

    def convert_temp(self, temp_json):
        sensor_id = -1
        temp_response = json.loads(temp_json)
        try:
            sensor_id = temp_response["id"]
            temp = float(temp_response["temp"])
        except ValueError:
            temp = 0.0
            print("Could not get correct temperature value")
        return temp, sensor_id

    def start_flask(self, host=HOST_IP, brew_id=None):
        args = ["python3", FLASK_FILE, "--host", host]
        if brew_id:
            args.append("--id")
            args.append(str(brew_id))
        subprocess.Popen(args)

    def start_sensor_server(self, host=HOST_IP, port=None):
        args = ["python3", SENSOR_SERVER_FILE, host, str(port)]
        subprocess.Popen(args)

    def shutdown(self):
        self.powerstrip.all_off()
        self.powerstrip.logout()