get_config.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import json
  2. class modelOpcConf:
  3. # OPC服务地址
  4. opc_server_url = None
  5. # 矿名id
  6. sys_key = None
  7. # 系统名
  8. sys_name = None
  9. # 接口配置文件名
  10. file_name = None
  11. # 接口配置路径
  12. path = None
  13. # 点位数组路径
  14. # point_arr_path = None
  15. class modelBaseConf:
  16. server_port = None
  17. def get_conf(file_name="config.json", file_path="./config"):
  18. path = f"{file_path}/{file_name}"
  19. try:
  20. with open(path, "r", encoding="utf-8") as f:
  21. content = json.load(f)
  22. f.close()
  23. return content
  24. except BaseException as e:
  25. print(e)
  26. print(f"异常,配置文件读取错误!path:{path}")
  27. def get_base_conf():
  28. model = modelBaseConf()
  29. base_conf = get_conf()
  30. model.server_port = base_conf['server_port']
  31. return model
  32. def get_opc_sys_conf(sys_key, sys_name):
  33. base_conf = get_conf()
  34. conf_dict = base_conf['sys_conf'][sys_key][sys_name]
  35. # 创建model
  36. model = modelOpcConf()
  37. # model赋值
  38. model.opc_server_url = conf_dict['opc_server_url']
  39. model.sys_key = conf_dict['sys_key']
  40. model.sys_name = conf_dict['sys_name']
  41. model.file_name = conf_dict['file_name']
  42. model.path = conf_dict['path']
  43. return model
  44. def get_opc_point_json_conf(sys_key, sys_name):
  45. config = get_opc_sys_conf(sys_key, sys_name)
  46. # 获取opc服务地址
  47. sys_file_path = config.path
  48. sys_file_name = config.file_name
  49. point_conf = get_conf(sys_file_name, sys_file_path)
  50. return point_conf
  51. def get_opc_point_arr_conf(sys_key, sys_name):
  52. config = get_opc_sys_conf(sys_key, sys_name)
  53. # 获取opc服务地址
  54. arr_file_path = f"{config.path}/array"
  55. arr_file_name = f"{config.sys_key}_arr.json"
  56. point_conf_arr = get_conf(arr_file_name, arr_file_path)
  57. return point_conf_arr
  58. if __name__ == "__main__":
  59. sys_key = 'jinjiaqu'
  60. sys_name = 'support'
  61. point_arr = get_opc_point_arr_conf(sys_key, sys_name)