示例#1
0
 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()
示例#2
0
 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()
示例#3
0
    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()
示例#4
0
class HeatService:

    bt = None
    DEFAULT_TIME = 10.0

    def temp_actor(self, pid_output):
        """
        switches the lan powerstrip on pid_output, if greater 0 switch PLUG_1 ON else switch OFF
        :param pid_output: PID calculated output value
        """

        status = PowerStrip().fetch_status()
        if pid_output > 0.0 and status.get(
                PowerStrip.PLUG_1) == PowerStrip.OFF:
            actor = HeatService.DEFAULT_TIME * (pid_output / BrewConfig.MAX)
            print("powerstrip on ", pid_output, "for", actor, "sec.")
            if self.alive():
                pass
            else:
                self.start(actor)
        if pid_output <= 0.0 and status.get(
                PowerStrip.PLUG_1) == PowerStrip.ON:
            if self.alive():
                self.stop()
                print("stopped timer")
            else:
                self.switch(PowerStrip.OFF)

    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 stop(self):
        self.bt.cancel()
        self.switch(PowerStrip.OFF)

    def switch(self, state=0):
        status = PowerStrip().switch(PowerStrip.PLUG_1, state)
        return status

    def alive(self):
        if self.bt:
            return self.bt.alive()
        return False

    def remaining(self):
        if self.bt:
            return self.bt.remaining()
        return False
示例#5
0
class HeatService:

    bt = None
    DEFAULT_TIME = 10.0

    def temp_actor(self, pid_output):
        """
        switches the lan powerstrip on pid_output, if greater 0 switch PLUG_1 ON else switch OFF
        :param pid_output: PID calculated output value
        """


        status = PowerStrip().fetch_status()
        if pid_output > 0.0 and status.get(PowerStrip.PLUG_1) == PowerStrip.OFF:
            actor = HeatService.DEFAULT_TIME * (pid_output / BrewConfig.MAX)
            print("powerstrip on ", pid_output, "for", actor, "sec.")
            if self.alive():
                pass
            else:
                self.start(actor)
        if pid_output <= 0.0 and status.get(PowerStrip.PLUG_1) == PowerStrip.ON:
            if self.alive():
                self.stop()
                print("stopped timer")
            else:
                self.switch(PowerStrip.OFF)

    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 stop(self):
        self.bt.cancel()
        self.switch(PowerStrip.OFF)

    def switch(self, state=0):
        status = PowerStrip().switch(PowerStrip.PLUG_1, state)
        return status

    def alive(self):
        if self.bt:
            return self.bt.alive()
        return False

    def remaining(self):
        if self.bt:
            return self.bt.remaining()
        return False
示例#6
0
    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()
示例#7
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()
示例#8
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()