#! python3 # -*- encoding: utf-8 -*- ''' @File : myconfig.py @Time : 2023/03/01 15:28:20 @Author : Jim @ Yiwin @Version : 1.0 @Contact : jim@yi-win.com @Descrip : SysConfig ''' import yaml from enum import Enum from pathlib import Path VERSION = "3.6.0" LASTDATE = "2023-04-11" COMPANY = u"奕枫仪器" CURRENT_DIR = Path() DATA_DIR = "data" CAL_DIR = "calfile" OUTPUT_DIR = "output" YAML_FILE_NAME = "config.yml" RETRIEVE_CFG_FILE = "retrieve.yml" PURE_WATER_FNAME = "attenuation_pure_water.DAT" FILE_MARK = ['Spectrum', 'DATA'] SAVE_EXT_NAME = ".csv" INTERVAL = 1.0 SEPARATOR = ";" TOKEN = ";" NEWLINE = "\n" FLOAT_RESERVE_BIT = 8 class DeviceType(Enum) : VIPER = 1 OSCAR = 2 SC6 = 3 DEVICE = { "UIPath" : 150 , "UISN" : "A05D" } # 一次最多 256个字节,步长<128, 字节数<256,浮点数最多63个 # viper 2100 2462 352/2 = 171, 一次60个 # oscar 2100 2329 230/2 = 116 一次60个 REGISTER = { "slaveaddress" : 1, "functioncode" : 3, "SNAddress" : 10, # 测试使用2980 "SNLen" : 5, "WLBeginAddress" : 2102, # 2100+2 "DataBeginAddress" : 2614, # 2612+2 "count" : 3 #oscar 2 viper 3 } COMSETTING = { "port" : "COM1" , "baudrate" : 9600, 'bytesize' : 8, 'parity' : "N", 'stopbit' : 1 } PLOTSETTING = { "LineInterval" : 2 , "LineBegin" : 1 } LOGSETTING= { "LogInterval" : 1, "RefreshInterval" : 0 } ALGORITHM = { "A720" : 0, # 0: 720 11项平均值法, 1: 720 附近两个点插值 2: 720 为0 "PureWater" : 0, # 0 :包含纯水系数 1: 不包含纯水系数 "PureWater2" : 0 } RETRIEVE = { # "Enable" : 1, "BeginWL" : 360, "EndWL" : 720, "Interval" : 1 } FILEPATH = { "path" : '' } class MyConfig(object): """ 如上预定义数据 先写一个yaml 文件出来,修改后,读文件数据就可以 """ def __init__(self) -> None: self.device_sn = "" self.system_cfg = {} self.cfg_path = Path() self.yml_cfg_file = self.cfg_path.joinpath(YAML_FILE_NAME) self.retrieve_cfg_file = Path(RETRIEVE_CFG_FILE) self.getSystemCfg() # 默认的值 def getSystemCfg(self,)->None: ''' 获得默认的值 ''' self.system_cfg = {} self.system_cfg.update( {"device":DEVICE} ) self.system_cfg.update( {"register":REGISTER} ) self.system_cfg.update( {"plotsetting":PLOTSETTING} ) self.system_cfg.update( {"logsetting":LOGSETTING} ) self.system_cfg.update( {"comsetting":COMSETTING} ) self.system_cfg.update( {"algorithm":ALGORITHM} ) self.system_cfg.update( {"filepath":FILEPATH} ) pass def getDictByAttr(self, *args) -> dict: ret = {} if len(args) == 0: return ret if len(args) == 1: if not hasattr( self, args[0] ): return ret tmp = getattr( self, args[0] ) if isinstance( tmp, dict ): ret.update(tmp) return ret if len(args) == 2: if not hasattr(self, args[0]): return ret if not isinstance(getattr(self, args[0]), dict): return ret tmp: dict = getattr(self, args[0]) if not tmp.__contains__(args[1]): return ret tmp2 = tmp[args[1]] if isinstance(tmp2, dict): ret.update(tmp2) return ret if len(args) > 2: return ret pass # 设置字典对应的键值 def set_attr(self, d: dict, k, v) -> bool: if d.__contains__(k): d.update({k: v}) return True return False def write_yaml(self, d: dict): with open(self.yml_cfg_file, "w", encoding = "utf-8") as f: yaml.dump(d, f) def read_yaml(self ) -> dict: # self.write_yaml(self.system_cfg) with open(self.yml_cfg_file, "r", encoding="utf-8") as f: content = f.read() # conent 读出来是字符串 d = yaml.load(content, Loader=yaml.FullLoader) # 用load方法转字典 return d def write_rtv_yaml(self, d: dict): with open(self.retrieve_cfg_file, "w", encoding="utf-8") as f: yaml.dump(d, f) def read_rtv_yaml(self ) -> dict: with open(self.retrieve_cfg_file, "r", encoding="utf-8") as f: content = f.read() # conent 读出来是字符串 d = yaml.load(content, Loader=yaml.FullLoader) # 用load方法转字典 return d def set_retrieve(self) -> dict: retrieve = {} # retrieve.update({"enable": RETRIEVE["Enable"]}) retrieve.update({"beginWL": RETRIEVE["BeginWL"]}) retrieve.update({"endWL": RETRIEVE["EndWL"]}) retrieve.update({"interval": RETRIEVE["Interval"]}) self.write_rtv_yaml(retrieve) return retrieve pass if __name__ == "__main__": pass