Sfoglia il codice sorgente

取数方式调整

psto 2 anni fa
parent
commit
c1006b8ec6
3 ha cambiato i file con 50 aggiunte e 3 eliminazioni
  1. 9 1
      api_server.py
  2. 27 1
      get_opc_data.py
  3. 14 1
      main.py

+ 9 - 1
api_server.py

@@ -1,7 +1,8 @@
 import asyncio
 import get_config
+import json
 
-from get_opc_data import get_opc_data, point_data_to_arr
+from get_opc_data import get_opc_data, point_data_to_arr, get_redis_conn
 
 from flask import Flask, request, render_template
 
@@ -37,7 +38,14 @@ def get():
     if opc_data is None or isinstance(opc_data, str):
         return api_get_error("数据读取错误!请检查点位或服务配置!")
     else:
+        # 接口数据处理
         json_data = point_data_to_arr(opc_json_data, opc_data)
+        # 获取redis
+        redis_conn = get_redis_conn()
+        # 接口数据存入redis
+        redis_key = f"{sys_key}_{sys_name}"
+        json_str = json.dumps(json_data)
+        redis_conn.set(str(redis_key), json_str)
         # 数据处理
         return json_data
 

+ 27 - 1
get_opc_data.py

@@ -1,8 +1,11 @@
 import redis
 import logging
+import asyncio
+import json
+
 from asyncua import Client
 from datetime import datetime
-from get_config import get_conf
+from get_config import get_conf, get_opc_point_arr_conf, get_opc_sys_conf, get_opc_point_json_conf
 
 
 # 连接redis服务
@@ -92,3 +95,26 @@ def point_data_to_arr(point_data, data_arr=[]):
 
     return point_dict
 
+
+def json_data_to_redis(sys_key, sys_name):
+    # 3、开启服务
+    # 3.1 获取opc点位arr
+    opc_point_arr = get_opc_point_arr_conf(sys_key, sys_name)
+    # 3.2 根据点位获取opc数据
+    opc_sys_conf = get_opc_sys_conf(sys_key, sys_name)
+    opc_data = asyncio.run(get_opc_data(opc_sys_conf.opc_server_url, opc_point_arr))
+    # 3.3 获取json格式
+    opc_json_data = get_opc_point_json_conf(sys_key, sys_name)
+    if opc_data is None or isinstance(opc_data, str):
+        msg = "数据读取错误!请检查点位或服务配置!"
+        tips = '{"code":-1, "msg":"' + msg + '"}'
+        return msg
+    else:
+        # 将数据结合到json格式
+        json_data = point_data_to_arr(opc_json_data, opc_data)
+        # 获取redis
+        redis_conn = get_redis_conn()
+        # 接口数据存入redis
+        redis_key = f"{sys_key}_{sys_name}"
+        json_str = json.dumps(json_data)
+        redis_conn.set(str(redis_key), json_str)

+ 14 - 1
main.py

@@ -1,10 +1,23 @@
 import api_server
+import time
 
+from get_opc_data import json_data_to_redis
 
+
+# api接口,不稳定
 def api_run():
     # 开启接口服务
     api_server.run_api_server()
 
 
+def data_to_cache():
+    while True:
+        sys_key = 'jinjiaqu'
+        sys_name = 'support'
+        json_data_to_redis(sys_key, sys_name)
+        time.sleep(10)
+
+
 if __name__ == '__main__':
-    api_run()
+    # api_run()
+    data_to_cache()