|
@@ -45,7 +45,7 @@ def get():
|
|
# 参数判断
|
|
# 参数判断
|
|
if sys_key is None or len(sys_key) == 0 or sys_name is None or len(sys_name) == 0:
|
|
if sys_key is None or len(sys_key) == 0 or sys_name is None or len(sys_name) == 0:
|
|
return "参数错误"
|
|
return "参数错误"
|
|
- # 获取对应配置
|
|
|
|
|
|
+ # 获取程序配置
|
|
base_conf = get_conf()
|
|
base_conf = get_conf()
|
|
# 获取opc服务地址
|
|
# 获取opc服务地址
|
|
sys_url = base_conf['sys_conf'][sys_key][sys_name]['opc_server_url']
|
|
sys_url = base_conf['sys_conf'][sys_key][sys_name]['opc_server_url']
|
|
@@ -56,43 +56,77 @@ def get():
|
|
# 获取点位数组
|
|
# 获取点位数组
|
|
point_conf = get_conf(sys_file_name, sys_file_path)
|
|
point_conf = get_conf(sys_file_name, sys_file_path)
|
|
try:
|
|
try:
|
|
- # 取到配置
|
|
|
|
- point_base_dic = point_conf['sys_point']
|
|
|
|
- point_arr = []
|
|
|
|
- # 取点位数据
|
|
|
|
- # print(point_base_dic)
|
|
|
|
- for group in point_base_dic:
|
|
|
|
- for item in point_base_dic[group]:
|
|
|
|
- point_arr.append(item['key'])
|
|
|
|
|
|
+ # 取需要监听的点位key数组
|
|
|
|
+ point_arr = point_to_arr(point_conf)
|
|
|
|
+ # 根据服务地址获取Opc数据
|
|
|
|
+ data = asyncio.run(get_opc_data(sys_url, point_arr, 2))
|
|
|
|
+ # 根据获取值整理成json
|
|
|
|
+ dict_data = point_data_to_arr(point_conf, data)
|
|
|
|
+
|
|
|
|
+ # 显示请求地址IP
|
|
|
|
+ # print(f"接收到请求,来自{get_request_ip()} {sys_key} {sys_name}")
|
|
|
|
+
|
|
|
|
+ return dict_data
|
|
|
|
+
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- print ("点位获取错误")
|
|
|
|
raise Exception("点位获取错误!")
|
|
raise Exception("点位获取错误!")
|
|
-
|
|
|
|
- # 根据服务地址获取Opc数据
|
|
|
|
- data = asyncio.run(get_opc_data(sys_url, point_arr, 2))
|
|
|
|
- # 显示请求地址IP
|
|
|
|
- print(f"接收到请求,来自{get_request_ip()} {sys_key} {sys_name}")
|
|
|
|
-
|
|
|
|
- for group in point_base_dic:
|
|
|
|
- for item in point_base_dic[group]:
|
|
|
|
- try:
|
|
|
|
- key = item['key']
|
|
|
|
- point_val = data[key]
|
|
|
|
- item['val'] = point_val
|
|
|
|
- finally:
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# 将点位整理数据,用于获取点位值
|
|
|
|
+def point_to_arr(point_data):
|
|
|
|
+ if isinstance(point_data, dict):
|
|
|
|
+ point_arr = []
|
|
|
|
+ for key, val in point_data.items():
|
|
|
|
+ data = point_data[key]
|
|
|
|
+ if 'key' in point_data.keys():
|
|
|
|
+ return point_data['key']
|
|
|
|
+ if isinstance(data, str):
|
|
continue
|
|
continue
|
|
|
|
+ return point_to_arr(data)
|
|
|
|
+ elif isinstance(point_data, list):
|
|
|
|
+ point_arr = []
|
|
|
|
+ for i in range(len(point_data)):
|
|
|
|
+ data = point_to_arr(point_data[i])
|
|
|
|
+ point_arr.append(data)
|
|
|
|
+ return point_arr
|
|
|
|
+ else:
|
|
|
|
+ return
|
|
|
|
|
|
- return point_base_dic
|
|
|
|
|
|
|
|
|
|
+# 将点位值赋予json(递归,适配多级json)
|
|
|
|
+def point_data_to_arr(point_data, data_arr=[]):
|
|
|
|
+ # 未获取到值返回空数组
|
|
|
|
+ if len(data_arr) == 0:
|
|
|
|
+ return point_data
|
|
|
|
+
|
|
|
|
+ point_dict = {}
|
|
|
|
+ if isinstance(point_data, dict):
|
|
|
|
+ for key, val in point_data.items():
|
|
|
|
+ data = point_data[key]
|
|
|
|
+ if 'val' == key:
|
|
|
|
+ point_key = point_dict['key']
|
|
|
|
+ if point_key in data_arr:
|
|
|
|
+ point_dict['val'] = data_arr[point_key]
|
|
|
|
+ else:
|
|
|
|
+ point_dict['val'] = ""
|
|
|
|
+ elif isinstance(val, str):
|
|
|
|
+ point_dict[key] = point_data[key]
|
|
|
|
+ else:
|
|
|
|
+ test = point_data_to_arr(data, data_arr)
|
|
|
|
+ point_dict[key] = test
|
|
|
|
+ elif isinstance(point_data, list):
|
|
|
|
+ point_arr = []
|
|
|
|
+ for i in range(len(point_data)):
|
|
|
|
+ data = point_data[i]
|
|
|
|
+ data_dic = point_data_to_arr(data, data_arr)
|
|
|
|
+ point_arr.append(data_dic)
|
|
|
|
+ # point_arr[i] =
|
|
|
|
+ return point_arr
|
|
|
|
|
|
-def get_conf(file_name="config.json", file_path="./config"):
|
|
|
|
- path = f"{file_path}/{file_name}"
|
|
|
|
- with open(path, "r", encoding="utf-8") as f:
|
|
|
|
- content = json.load(f)
|
|
|
|
- f.close()
|
|
|
|
- return content
|
|
|
|
|
|
+ return point_dict
|
|
|
|
|
|
|
|
|
|
|
|
+# 获取点位值
|
|
async def get_opc_data(sys_url, point_arr, ns=2):
|
|
async def get_opc_data(sys_url, point_arr, ns=2):
|
|
if sys_url is None or point_arr is None:
|
|
if sys_url is None or point_arr is None:
|
|
return
|
|
return
|
|
@@ -131,10 +165,21 @@ async def get_opc_data(sys_url, point_arr, ns=2):
|
|
return result_arr
|
|
return result_arr
|
|
|
|
|
|
|
|
|
|
|
|
+# 获取文件配置
|
|
|
|
+def get_conf(file_name="config.json", file_path="./config"):
|
|
|
|
+ path = f"{file_path}/{file_name}"
|
|
|
|
+ with open(path, "r", encoding="utf-8") as f:
|
|
|
|
+ content = json.load(f)
|
|
|
|
+ f.close()
|
|
|
|
+ return content
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# 获取请求Ip
|
|
def get_request_ip():
|
|
def get_request_ip():
|
|
return request.remote_addr
|
|
return request.remote_addr
|
|
|
|
|
|
|
|
|
|
|
|
+# 返回错误信息
|
|
def api_get_error(msg):
|
|
def api_get_error(msg):
|
|
return '{"code":-1, "msg":"' + msg + '"}'
|
|
return '{"code":-1, "msg":"' + msg + '"}'
|
|
|
|
|