gateway_api.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import annotations
  2. from typing import Any
  3. from .auth import find_project_config
  4. from .http_client import request_json
  5. from .protocols import MODBUS_SPEC, S7_SPEC
  6. def _request_gateway(project_key: str, path: str | None, payload: dict[str, Any], protocol: str) -> dict[str, Any]:
  7. project = find_project_config(project_key)
  8. if not path:
  9. raise ValueError(f"{protocol} gateway path is not configured")
  10. response_payload = request_json(
  11. "POST",
  12. f"{project['base_url']}{path}",
  13. json_payload=payload,
  14. )
  15. if not isinstance(response_payload, dict):
  16. raise ValueError(f"gateway API returned invalid payload: {response_payload}")
  17. return response_payload
  18. def modbus_point_collect_test(
  19. project_key: str,
  20. *,
  21. ip: str,
  22. port: int,
  23. slave_id: int,
  24. points: list[dict[str, Any]],
  25. device_type: str = "ModbusTCP",
  26. word_byte_order: str = "ABCD",
  27. address_base: int = 0,
  28. ) -> dict[str, Any]:
  29. payload = {
  30. "device_type": device_type,
  31. "ip": ip,
  32. "port": port,
  33. "word_byte_order": word_byte_order,
  34. "address_base": address_base,
  35. "slave_id": slave_id,
  36. "points": points,
  37. }
  38. return _request_gateway(project_key, MODBUS_SPEC.point_test_path, payload, "modbus point test")
  39. def s7_raw_read(
  40. project_key: str,
  41. *,
  42. ip: str,
  43. rock: int,
  44. slot: int,
  45. read: dict[str, Any],
  46. device_type: str = "S7-1200",
  47. port: int = 102,
  48. tsap_conn_type: str | None = None,
  49. ) -> dict[str, Any]:
  50. payload = {
  51. "device_type": device_type,
  52. "ip": ip,
  53. "port": port,
  54. "rock": rock,
  55. "slot": slot,
  56. "tsap_conn_type": _resolve_s7_tsap_conn_type(device_type, tsap_conn_type),
  57. "read": read,
  58. }
  59. return _request_gateway(project_key, S7_SPEC.raw_read_path, payload, "s7 raw read")
  60. def s7_point_collect_test(
  61. project_key: str,
  62. *,
  63. ip: str,
  64. rock: int,
  65. slot: int,
  66. points: list[dict[str, Any]],
  67. device_type: str = "S7-1200",
  68. port: int = 102,
  69. tsap_conn_type: str | None = None,
  70. ) -> dict[str, Any]:
  71. payload = {
  72. "device_type": device_type,
  73. "ip": ip,
  74. "port": port,
  75. "rock": rock,
  76. "slot": slot,
  77. "tsap_conn_type": _resolve_s7_tsap_conn_type(device_type, tsap_conn_type),
  78. "points": points,
  79. }
  80. return _request_gateway(project_key, S7_SPEC.point_test_path, payload, "s7 point test")
  81. def s7_connect_scan(project_key: str, *, ip: str) -> dict[str, Any]:
  82. return _request_gateway(project_key, S7_SPEC.connect_scan_path, {"ip": ip}, "s7 connect scan")
  83. def _resolve_s7_tsap_conn_type(device_type: str, tsap_conn_type: str | None) -> str:
  84. normalized_tsap = str(tsap_conn_type or "").strip().upper()
  85. if normalized_tsap:
  86. return normalized_tsap
  87. normalized_device_type = str(device_type or "").strip().lower().replace(" ", "")
  88. if normalized_device_type in {"s7-smart200", "s7smart200", "smart200"}:
  89. return "OP"
  90. return "PG"