jjq_support_1.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import asyncio
  2. import redis
  3. from asyncua import Client
  4. from datetime import datetime
  5. opc_server_url = "opc.tcp://192.168.10.133:49320"
  6. redis_ip = '127.0.0.1'
  7. def get_point_arr():
  8. for i in range(137):
  9. three_digit_numbers = [f"OPC.电液控.支架集.{num:03d}.立柱压力.前柱压力" for num in range(1, 138)]
  10. return three_digit_numbers
  11. def get_opc_data():
  12. ns = 2
  13. opc_point_arr = get_point_arr()
  14. # redis
  15. pool = redis.ConnectionPool(host=redis_ip, password='')
  16. redis_conn = redis.Redis(connection_pool=pool)
  17. async with Client(url=opc_server_url) as client:
  18. i = 0
  19. result_arr = {}
  20. print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
  21. for i in range(len(opc_point_arr)):
  22. try:
  23. node = f"ns={ns};s={opc_point_arr[i]}"
  24. tag = client.get_node(node)
  25. value = await tag.read_value()
  26. # redis值
  27. redis_conn.set(str(opc_point_arr[i]), str(value))
  28. arr_key = node.split("=")
  29. result_arr[arr_key[2]] = value
  30. finally:
  31. continue
  32. return result_arr
  33. if __name__ == '__main__':
  34. print(get_opc_data())