test_plc_update.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. PLC指令测试工具
  5. 用于测试PLC参数更新请求的实际发送
  6. """
  7. import requests
  8. import json
  9. import hashlib
  10. import time
  11. def load_config(config_file='config.json'):
  12. """加载配置文件"""
  13. with open(config_file, 'r', encoding='utf-8') as f:
  14. return json.load(f)
  15. def generate_md5_signature(record_data, secret, timestamp):
  16. """生成MD5签名"""
  17. cal_str = f"{record_data}{secret}{timestamp}"
  18. cal_md5 = hashlib.md5(cal_str.encode('utf-8')).hexdigest()
  19. return cal_md5.upper()
  20. def send_plc_update_test(device_name, item, old_value, new_value, command_type):
  21. """
  22. 发送PLC参数更新测试
  23. 参数:
  24. device_name: 设备名称
  25. item: 参数项名称
  26. old_value: 当前值
  27. new_value: 目标值
  28. command_type: 命令类型
  29. 返回:
  30. 是否发送成功
  31. """
  32. config = load_config()
  33. PLC_URL = config['api']['base_url'] + config['api']['plc_endpoint']
  34. PROJECT_ID = config['scada']['project_id']
  35. SCADA_SECRET = config['scada']['secret']
  36. timestamp = int(time.time())
  37. record_data = json.dumps({
  38. "project_id": PROJECT_ID,
  39. "item": item,
  40. "old_value": old_value,
  41. "new_value": new_value,
  42. "command_type": command_type
  43. }, separators=(',', ':'))
  44. signature = generate_md5_signature(record_data, SCADA_SECRET, timestamp)
  45. url = f"{PLC_URL}?sign={signature}&timestamp={timestamp}"
  46. payload = [{
  47. "project_id": PROJECT_ID,
  48. "item": item,
  49. "old_value": old_value,
  50. "new_value": new_value,
  51. "command_type": command_type
  52. }]
  53. print(f"PLC测试")
  54. print(f"设备 {device_name}")
  55. print(f"参数 {item}")
  56. print(f"旧值 {old_value}")
  57. print(f"新值 {new_value}")
  58. print(f"类型 {command_type}")
  59. print(f"时间戳 {timestamp}")
  60. print(f"URL {url}")
  61. print(f"请求体 {json.dumps(payload, indent=2, ensure_ascii=False)}")
  62. print(f"签名数据 {record_data}")
  63. print(f"签名 {signature}")
  64. print("-" * 50)
  65. try:
  66. headers = {"Content-Type": "application/json"}
  67. response = requests.post(url, headers=headers, json=payload, timeout=15)
  68. print(f"响应状态码 {response.status_code}")
  69. print(f"响应头 {dict(response.headers)}")
  70. try:
  71. response_json = response.json()
  72. print(f"响应JSON {json.dumps(response_json, indent=2, ensure_ascii=False)}")
  73. except:
  74. print(f"响应文本 {response.text}")
  75. response.raise_for_status()
  76. print("请求发送成功")
  77. return True
  78. except requests.exceptions.RequestException as e:
  79. print(f"请求失败 {e}")
  80. return False
  81. except Exception as e:
  82. print(f"未知错误 {e}")
  83. return False
  84. if __name__ == "__main__":
  85. # 测试配置
  86. device_name = "UF2"
  87. item = "C.M.UF2_DB@time_production"
  88. old_value = "3800"
  89. new_value = "3801"
  90. command_type = 1
  91. print("开始PLC指令测试")
  92. success = send_plc_update_test(device_name, item, old_value, new_value, command_type)
  93. if success:
  94. print("\n测试完成 请检查PLC系统")
  95. else:
  96. print("\n测试失败 请检查网络和配置")