def init_driver(self): # 遍历驱动字典 for driver_info in self.m_driver_dict.values(): if not driver_info.driver: LOG.error('驱动%s未加载' % (driver_info.driver_name,)) continue # 1.设置驱动基础配置 if not self.__driver_set_basic_config(driver_info): continue # 2.设置驱动运行配置 if not self.__driver_set_run_config(driver_info): continue # 3.驱动初始化操作 ret = driver_info.driver.init() if check_api_result(ret): LOG.info('驱动 %s 初始化成功' % driver_info.driver_name) else: LOG.error('驱动 %s 初始化失败' % driver_info.driver_name) continue # 4.设置回调函数 if not self.__driver_set_call_back(driver_info): continue # 5.启动驱动 ret = driver_info.driver.run() if check_api_result(ret): LOG.info('启动 %s 成功' % driver_info.driver_name) else: LOG.error('启动 %s 失败' % driver_info.driver_name) continue
def load_cfg(self, cfg_name): try: self.__cfg_msg = load_config(cfg_name) except Exception as err: LOG.error('加载%s配置文件失败:%s' % (cfg_name, err)) return False LOG.info('加载%s配置成功' % cfg_name)
def __driver_set_run_config(self, driver_info): if driver_info.run_config: ret = driver_info.driver.set_run_config(driver_info.run_config, json.dumps(load_config(driver_info.run_config))) if not check_api_result(ret): LOG.error('驱动 %s 设置运行配置失败' % driver_info.driver_name) return False LOG.info('驱动 %s 设置运行配置成功' % driver_info.driver_name) return True
def get_config(self, cfg_name): file_name = CFG_PATH + cfg_name + '.json' try: with open(file_name, 'r', encoding='UTF-8') as f: cfg_msg = json.loads(f.read()) return True, cfg_msg except Exception as err: ret = '读取配置失败:%s' % (str(err), ) LOG.error(ret) return False, str(err)
def get_pump_cfg(self): pumps = [] if 'pump' not in self.__cfg_msg: return pumps pump_cfg = self.__cfg_msg['pump'] for cfg in pump_cfg: pump = PumpInfo(cfg['topic'], cfg['driver'], cfg['cycle']) pumps.append(pump) for pump in pumps: if pump.topic not in self.get_topic_cfg(): LOG.error('发布泵配置错误: %s: %s' % (pump.driver, pump.topic)) if pump.driver not in self.get_driver_cfg(): LOG.error('发布泵配置错误: %s: %s' % (pump.driver, pump.topic)) return pumps
def set_config(self, cfg_name, cfg_msg): file_name = CFG_PATH + cfg_name + '.json' if type(cfg_msg) == dict or type(cfg_msg) == list: cfg_msg = json.dumps(cfg_msg, ensure_ascii=False, indent=2) try: with open(file_name, 'w', encoding='utf-8') as f: f.write(cfg_msg) ret = '保存配置:%s:%s' % (file_name, cfg_msg) LOG.info(ret) m_core.set_run_config(file_name, cfg_msg) return True, cfg_msg except Exception as err: ret = '保存配置失败:%s' % (str(err), ) LOG.error(ret) return False, str(err)
def __pump_set_config(self, driver_info, topic_info, cycle): if driver_info.direction == 'down': if driver_info.pub_type in ('seq_sensor', 'seq_event'): # 设置下行连续泵 self.__pump_set_topic_driver(self.m_pump_seq_down, topic_info, driver_info, cycle) elif driver_info.pub_type in ('dis_sensor', 'dis_event'): # 设置下行离散泵 self.__pump_set_topic_driver(self.m_pump_dis_down, topic_info, driver_info, cycle) else: LOG.error('驱动 %s 发布方式配置错误:%s' % (driver_info.driver_name, driver_info.pub_type)) return False elif driver_info.direction == 'up': if driver_info.pub_type in ('seq_sensor', 'seq_event'): # 设置上行连续泵 self.__pump_set_topic_driver(self.m_pump_seq_up, topic_info, driver_info, cycle) elif driver_info.pub_type in ('dis_sensor', 'dis_event'): # 设置上行离散泵 self.__pump_set_topic_driver(self.m_pump_dis_up, topic_info, driver_info, cycle) else: LOG.error('驱动 %s 发布方式配置错误:%s' % (driver_info.driver_name, driver_info.pub_type)) return False else: LOG.error('驱动 %s 数据流向配置错误:%s ' % (driver_info.driver_name, driver_info.direction)) return False return True
def __driver_set_call_back(self, driver_info): if driver_info.direction == 'down': if driver_info.sub_type == 'seq_sensor': # 设置下行连续回调 ret = driver_info.driver.set_fun(self.__write_data_seq_sensor_down) elif driver_info.sub_type == 'seq_event': # 设置下行连续回调 ret = driver_info.driver.set_fun(self.__write_data_seq_event_down) elif driver_info.sub_type in ('dis_sensor', 'dis_event'): # 设置下行离散回调 ret = driver_info.driver.set_fun(self.__write_data_dis_down) else: if driver_info.sub_type is not None: LOG.error('驱动%s采集方式配置错误:%s' % (driver_info.driver_name, driver_info.sub_type)) return False elif driver_info.direction == 'up': if driver_info.sub_type == 'seq_sensor': # 设置上行连续回调 ret = driver_info.driver.set_fun(self.__write_data_seq_sensor_up) elif driver_info.sub_type == 'seq_event': # 设置上行连续回调 ret = driver_info.driver.set_fun(self.__write_data_seq_event_up) elif driver_info.sub_type in ('dis_sensor', 'dis_event'): # 设置上行离散回调 ret = driver_info.driver.set_fun(self.__write_data_dis_up) else: if driver_info.sub_type is not None: LOG.error('驱动%s采集方式配置错误:%s' % (driver_info.driver_name, driver_info.sub_type)) return False else: LOG.error('驱动%s数据流向配置错误:%s' % (driver_info.driver_name, driver_info.direction)) return False if not check_api_result(ret): LOG.error('设置 %s 回调失败' % driver_info.driver_name) return False LOG.info('设置 %s 回调成功' % driver_info.driver_name) return True
def load_driver(self): self.m_driver_dict = self.m_cfg.get_driver_cfg() for driver_info in self.m_driver_dict.values(): # 加载驱动对象 if driver_info.lib_type == 'python': # python版本的驱动 driver = load_python(driver_info.lib_name) elif driver_info.lib_type == 'csharp': # C#版本的驱动 driver = load_csharp(driver_info.driver_name) else: # 其它语言版本的驱动暂不支持(待扩充) driver = None LOG.error('不支持的库类型:%s' % (driver_info.lib_type,)) if driver: driver_info.driver = driver LOG.info('加载驱动成功:%s' % (driver_info.driver_name,)) else: LOG.error('加载驱动失败:%s' % (driver_info.driver_name,))
def load_pump(self): pumbs_cfg = self.m_cfg.get_pump_cfg() # 遍历发布泵配置 for pump_info in pumbs_cfg: # 主题配置信息 if pump_info.topic not in self.m_topic_dict: LOG.error('%s 主题未配置' % (pump_info.topic,)) continue topic_info = self.m_topic_dict[pump_info.topic] # 驱动配置信息 if pump_info.driver not in self.m_driver_dict: LOG.error('%s 驱动未配置' % (pump_info.driver,)) continue driver_info = self.m_driver_dict[pump_info.driver] # 关联发布泵:pump,driver,topic,cycle if self.__pump_set_config(driver_info, topic_info, pump_info.cycle): LOG.info('关联发布泵成功:%s:%s:%d' % (driver_info.driver_name, topic_info.topic, pump_info.cycle)) else: continue
def get_driver_cfg(self): drivers_dict = {} if 'driver' not in self.__cfg_msg: return drivers_dict driver_cfg = self.__cfg_msg['driver'] for cfg in driver_cfg: driver = DriverInfo(cfg['driver_name'], cfg['lib_name'], cfg['lib_type'], cfg['basic_config'], cfg['run_config'], cfg['direction'], cfg['sub_type'], cfg['pub_type']) drivers_dict[driver.driver_name] = driver # 关键配置校验 for driver in drivers_dict.values(): if driver.direction not in Data_dir_Dict: LOG.error('驱动配置错误: %s: %s' % (driver.driver_name, driver.direction)) if driver.sub_type and driver.sub_type not in Data_Mode_Dict: LOG.error('驱动配置错误: %s: %s' % (driver.driver_name, driver.sub_type)) if driver.pub_type and driver.pub_type not in Data_Mode_Dict: LOG.error('驱动配置错误: %s: %s' % (driver.driver_name, driver.pub_type)) return drivers_dict