|
@@ -1,7 +1,9 @@
|
|
-import asyncio
|
|
|
|
|
|
+import os
|
|
|
|
+import re
|
|
import json
|
|
import json
|
|
import redis
|
|
import redis
|
|
import logging
|
|
import logging
|
|
|
|
+import asyncio
|
|
|
|
|
|
from flask import Flask, request, render_template
|
|
from flask import Flask, request, render_template
|
|
from asyncua import Client
|
|
from asyncua import Client
|
|
@@ -44,9 +46,10 @@ def get():
|
|
base_conf = get_conf()
|
|
base_conf = get_conf()
|
|
# 获取opc服务地址
|
|
# 获取opc服务地址
|
|
sys_url = base_conf["server_url"]
|
|
sys_url = base_conf["server_url"]
|
|
|
|
+
|
|
# 获取配置文件名
|
|
# 获取配置文件名
|
|
- sys_file_path = base_conf['sys_conf'][sys_key]['path']
|
|
|
|
- sys_file_name = base_conf['sys_conf'][sys_key]['file_name']
|
|
|
|
|
|
+ sys_file_path = base_conf['sys_conf'][sys_key][sys_name]['path']
|
|
|
|
+ sys_file_name = base_conf['sys_conf'][sys_key][sys_name]['file_name']
|
|
# 获取点位数组
|
|
# 获取点位数组
|
|
point_conf = get_conf(sys_file_name, sys_file_path)
|
|
point_conf = get_conf(sys_file_name, sys_file_path)
|
|
# 取到配置
|
|
# 取到配置
|
|
@@ -60,6 +63,7 @@ def get():
|
|
|
|
|
|
# 根据服务地址获取Opc数据
|
|
# 根据服务地址获取Opc数据
|
|
data = asyncio.run(get_opc_data(sys_url, point_arr, 2))
|
|
data = asyncio.run(get_opc_data(sys_url, point_arr, 2))
|
|
|
|
+ # 显示请求地址IP
|
|
get_request_ip()
|
|
get_request_ip()
|
|
|
|
|
|
for group in point_base_dic:
|
|
for group in point_base_dic:
|
|
@@ -86,33 +90,38 @@ 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
|
|
url = sys_url
|
|
url = sys_url
|
|
- async with Client(url=url) as client:
|
|
|
|
- handler = SubHandler()
|
|
|
|
- r = handler.r
|
|
|
|
- i = 0
|
|
|
|
- result_arr = {}
|
|
|
|
- print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
|
|
- while i < len(point_arr):
|
|
|
|
- node = f"ns={ns};s={point_arr[i]}"
|
|
|
|
- tag = client.get_node(node)
|
|
|
|
- sub = await client.create_subscription(500, handler)
|
|
|
|
- value = None
|
|
|
|
- try:
|
|
|
|
- value = await tag.read_value()
|
|
|
|
- #redis值
|
|
|
|
- r.set(str(node), str(value))
|
|
|
|
- #获取点位key
|
|
|
|
- arr_key = node.split("=")
|
|
|
|
- result_arr[arr_key[2]] = value
|
|
|
|
- # print(f"tag1 is: {tag} with value {value} ")
|
|
|
|
- i += 1
|
|
|
|
- if i == len(point_arr):
|
|
|
|
- print("Get Data Success")
|
|
|
|
- finally:
|
|
|
|
- i += 1
|
|
|
|
- continue
|
|
|
|
- print(datetime.now().strftime('End_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
|
|
- return result_arr
|
|
|
|
|
|
+ try:
|
|
|
|
+ async with Client(url=url) as client:
|
|
|
|
+ handler = SubHandler()
|
|
|
|
+ r = handler.r
|
|
|
|
+ i = 0
|
|
|
|
+ result_arr = {}
|
|
|
|
+ print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
|
|
+ while i < len(point_arr):
|
|
|
|
+ node = f"ns={ns};s={point_arr[i]}"
|
|
|
|
+ tag = client.get_node(node)
|
|
|
|
+ sub = await client.create_subscription(500, handler)
|
|
|
|
+ value = None
|
|
|
|
+ try:
|
|
|
|
+ value = await tag.read_value()
|
|
|
|
+ # redis值
|
|
|
|
+ r.set(str(node), str(value))
|
|
|
|
+ # 获取点位key
|
|
|
|
+ arr_key = node.split("=")
|
|
|
|
+ result_arr[arr_key[2]] = value
|
|
|
|
+ # print(f"tag1 is: {tag} with value {value} ")
|
|
|
|
+ i += 1
|
|
|
|
+ if i == len(point_arr):
|
|
|
|
+ print("Get Data Success")
|
|
|
|
+ finally:
|
|
|
|
+ i += 1
|
|
|
|
+ continue
|
|
|
|
+ print(datetime.now().strftime('End_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
|
|
+ return result_arr
|
|
|
|
+ finally:
|
|
|
|
+ # 网络连通性测试
|
|
|
|
+ sys_ip = sys_url[10: sys_url.index(":", 10)]
|
|
|
|
+ os.system(f'ping {sys_ip} -n 1')
|
|
|
|
|
|
|
|
|
|
def get_request_ip():
|
|
def get_request_ip():
|
|
@@ -120,5 +129,9 @@ def get_request_ip():
|
|
logging.debug(ip)
|
|
logging.debug(ip)
|
|
|
|
|
|
|
|
|
|
|
|
+def api_get_error(msg):
|
|
|
|
+ return '{"code":-1, "msg":"' + msg + '"}'
|
|
|
|
+
|
|
|
|
+
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
app.run()
|
|
app.run()
|