jjq_support_1.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. import redis
  3. import asyncio
  4. from asyncua import Client
  5. from datetime import datetime
  6. opc_server_url = "opc.tcp://192.168.10.133:49320" # OPC UA服务器的URL
  7. redis_ip = '127.0.0.1'
  8. def get_point_arr():
  9. for i in range(137):
  10. three_digit_numbers = [f"OPC.电液控.支架集.{num:03d}.立柱压力.前柱压力" for num in range(1, 138)]
  11. return three_digit_numbers
  12. # async def connect_and_read_opcua_data():
  13. #
  14. # async with Client(url=opc_server_url) as client:
  15. # # 连接到服务器
  16. # await client.connect()
  17. #
  18. # # 读取节点的值
  19. # node = await client.nodes.root.get_child(get_point_arr()) # 替换成你要读取的节点路径
  20. # value = await node.read_value()
  21. #
  22. # print("Node Value:", value)
  23. async def get_opc_data():
  24. ns = 2
  25. opc_point_arr = get_point_arr()
  26. # redis
  27. pool = redis.ConnectionPool(host=redis_ip, password='')
  28. redis_conn = redis.Redis(connection_pool=pool)
  29. async with Client(url=opc_server_url) as client:
  30. i = 0
  31. result_arr = {}
  32. print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
  33. for i in range(len(opc_point_arr)):
  34. try:
  35. node = f"ns={ns};s={opc_point_arr[i]}"
  36. tag = client.get_node(node)
  37. value = await tag.read_value()
  38. # redis值
  39. redis_conn.set(str(opc_point_arr[i]), str(value))
  40. arr_key = node.split("=")
  41. result_arr[arr_key[2]] = value
  42. finally:
  43. continue
  44. return result_arr
  45. if __name__ == '__main__':
  46. print(get_opc_data())