def handle_component_input_changes(self, msg): print('handle component input changes') logging.info('handling component input changes') ci_id = int(msg) ci = self.db_session.query(ComponentInput).filter_by(_id=ci_id).one() if ci.component.component_type == 'device': self.reload_devices() if ci.component.component_type == 'regulator': if ci.component_id in self.regulators: reg = self.regulators[ci.component_id] old_order = reg.order self.db_session.refresh(reg) if old_order != reg.order: self.max_regulation_order = 0 for reg_id in self.regulators: order = self.regulators[reg_id].order if order > self.max_regulation_order: self.max_regulation_order = order else: reg = self.db_session.query(Regulator).filter_by( _id=ci.component_id).one() real_reg = regulator_factory(reg.algorithm_name) real_reg.initialize(reg) inputs = {} for inp in reg.inputs: inputs[inp] = get_redis_number(self.redis_conn, reg.inputs[inp].redis_key) if real_reg.is_executable(inputs): self.regulators[reg.id] = reg self.real_regulators[reg.id] = real_reg
def handle_component_input_changes(self, msg): print('handle component input changes') logging.info('handling component input changes') ci_id = int(msg) ci = self.db_session.query(ComponentInput).filter_by(_id=ci_id).one() if ci.component.component_type == 'device': self.reload_devices() if ci.component.component_type == 'regulator': if ci.component_id in self.regulators: reg = self.regulators[ci.component_id] old_order = reg.order self.db_session.refresh(reg) if old_order != reg.order: self.max_regulation_order = 0 for reg_id in self.regulators: order = self.regulators[reg_id].order if order > self.max_regulation_order: self.max_regulation_order = order else: reg = self.db_session.query(Regulator).filter_by(_id=ci.component_id).one() real_reg = regulator_factory(reg.algorithm_name) real_reg.initialize(reg) inputs = {} for inp in reg.inputs: inputs[inp] = get_redis_number(self.redis_conn, reg.inputs[inp].redis_key) if real_reg.is_executable(inputs): self.regulators[reg.id] = reg self.real_regulators[reg.id] = real_reg
def update_value(self, db_session, redis_conn, now, timeout): value = None if self.sensor is not None: value = get_redis_number(redis_conn, self.sensor.redis_key) self._outputs['value'].update_value(redis_conn, value, timeout) self.old_value_logs = ParameterValueLog.log(db_session, self, now, value, self.old_value_logs)
def update_value(self, db_session, redis_conn, now, timeout): if self.actuator is not None: value = get_redis_number(redis_conn, self._inputs['value'].redis_key) if value is None: # set actuator default value = self.actuator.default_value self.old_value_logs = DeviceValueLog.log(db_session, self, now, value, self.old_value_logs) redis_conn.setex(self.actuator.redis_key, value, timeout)
def handle_regulator_changes(self, msg): print('handle regulator changes') logging.info('handling regulator changes') change_type, r_id_str = msg.split(' ') r_id = int(r_id_str) if change_type == 'added': r = self.db_session.query(Regulator).filter_by(_id=r_id).one() rr = regulator_factory(r.algorithm_name) rr.initialize(r) inputs = {} for inp in r.inputs: inputs[inp] = get_redis_number(self.redis_conn, r.inputs[inp].redis_key) if rr.is_executable(inputs): self.regulators[r.id] = r self.real_regulators[r.id] = rr logging.info('added ' + str(r.name)) print('added ' + str(r.name)) if change_type == 'changed': if r_id in self.regulators.keys(): r = self.regulators[r_id] self.db_session.refresh(r) else: r = self.db_session.query(Regulator).filter_by(_id=r_id).one() rr = regulator_factory(r.algorithm_name) rr.initialize(r) inputs = {} for inp in r.inputs: inputs[inp] = get_redis_number(self.redis_conn, r.inputs[inp].redis_key) if rr.is_executable(inputs) or r.order > 0: logging.info('using: ' + str(r.name)) print('using: ' + r.name) self.regulators[r.id] = r self.real_regulators[r.id] = rr if r.order > self.max_regulation_order: self.max_regulation_order = r.order elif change_type == 'removed': if r_id in self.regulators.keys(): logging.info('removing regulator: ' + self.regulators[r_id].name) print('removing regulator: ' + self.regulators[r_id].name) self.devices.pop(r_id, None)
def handle_regulators(self, now): for order in range(self.max_regulation_order + 1): for reg_key in self.regulators: regulator = self.regulators[reg_key] real_regulator = self.real_regulators[reg_key] if regulator.order == order: inputs = {'now': now} for inp in regulator.inputs: inputs[inp] = get_redis_number(self.redis_conn, regulator.inputs[inp].redis_key) outputs = real_regulator.execute(inputs) for outp in regulator.outputs: self.redis_conn.setex(regulator.outputs[outp].redis_key, str(outputs[outp]), 2 * self.loop_time)
def handle_regulators(self, now): for order in range(self.max_regulation_order + 1): for reg_key in self.regulators: regulator = self.regulators[reg_key] real_regulator = self.real_regulators[reg_key] if regulator.order == order: inputs = {'now': now} for inp in regulator.inputs: inputs[inp] = get_redis_number( self.redis_conn, regulator.inputs[inp].redis_key) outputs = real_regulator.execute(inputs) for outp in regulator.outputs: self.redis_conn.setex( regulator.outputs[outp].redis_key, str(outputs[outp]), 2 * self.loop_time)
def get_actuator_value_from_redis(self, actuator): value = get_redis_number(self.redis_conn, actuator.redis_key) if value is None: # no setpoint, use default self.redis_conn.setex(actuator.redis_key, actuator.default_value, 2 * self.loop_time) return actuator.default_value elif actuator.device_type.unit == "%": if value > 100: return 100 elif value < 0: return 0 elif actuator.device_type.unit == "0/1": if value == 0: return 0 return 1 return value
def get_actuator_value_from_redis(self, actuator): value = get_redis_number(self.redis_conn, actuator.redis_key) if value is None: # no setpoint, use default self.redis_conn.setex(actuator.redis_key, actuator.default_value, 2 * self.loop_time) return actuator.default_value elif actuator.device_type.unit == '%': if value > 100: return 100 elif value < 0: return 0 elif actuator.device_type.unit == '0/1': if value == 0: return 0 return 1 return value
def check_watchdog(self): if get_redis_number(self.redis_conn, self.worker.watchdog_key) == 1: return True return False