Skip to content

BountyHunter-ControlServer - Business Flow

Tài liệu này mô tả luồng dữ liệu giữa các máy gắp gấu (IoT Machines), ControlServer và hệ thống Backend.

1. Kiến trúc tổng thể (High-level)

graph TD
    subgraph "Mechanical Layer"
        Machine1[Claw Machine 1]
        Machine2[Claw Machine 2]
    end

    subgraph "IoT Bridge (ControlServer)"
        WawaServer[WawaServer:9999]
        ConnectedClient[ConnectedClient Thread]
        JmsListener[JMS Dynamic Listeners]
    end

    subgraph "Infrastructure"
        Redis[(Redis)]
        MQ[(ActiveMQ Artemis)]
    end

    subgraph "Backend"
        Receiver[Receiver-Service]
        Core[Application-Core]
    end

    Machine1 <-->|TCP Socket| WawaServer
    WawaServer --> ConnectedClient
    ConnectedClient -->|Update Status| Redis
    ConnectedClient -->|JMS Events| MQ
    MQ --> Receiver
    Receiver --> Core

    Core -->|Commands| MQ
    MQ --> JmsListener
    JmsListener --> ConnectedClient
    ConnectedClient -->|TCP Command| Machine1

2. Luồng dữ liệu chi tiết

2.1 Từ Machine đến Backend (Upstream)

  1. Kết nối: Máy gắp gấu kết nối tới ControlServer qua TCP port 9999.
  2. Nhận tin nhắn: ConnectedClient đọc dữ liệu byte từ socket.
  3. Dịch tin nhắn: ClientMessageHandler dịch các byte thô thành các DTO như MachineStatusReport, GameStarted, GameEnded.
  4. Xử lý & Đồng bộ:
    • Cập nhật trạng thái máy vào Redis (RedisMachineRepository) để Backend biết máy nào đang online/offline/busy.
    • Gửi sự kiện (SendGamePlay, SendHealthCheck) vào ActiveMQ qua các queue:
      • queue-control-server-gameplay
      • queue-control-server-health-check
  5. Tiếp nhận: receiver-service của Backend tiêu thụ các JMS message này để xử lý logic nghiệp vụ.

2.2 Từ Backend đến Machine (Downstream)

  1. Gửi lệnh: Backend gửi lệnh (ví dụ: START_GAME, RESTART_ANDROID) vào ActiveMQ.
  2. Lắng nghe: DynamicJmsListenerService trong ControlServer lắng nghe các queue dành riêng cho từng máy.
  3. Chuyển đổi: Lệnh từ Backend được TranslateMsgToWWJ chuyển đổi thành định dạng gói tin byte mà máy hiểu được.
  4. Thực thi: ConnectedClient gửi các byte này qua TCP socket tới máy gắp gấu.

3. Định dạng gói tin (Protocol)

Giao thức giao tiếp giữa ControlServer và máy gắp gấu là một custom binary protocol: - Header: 0xFE - Length: Byte thứ 7 quy định độ dài gói tin. - Payload: Các lệnh điều khiển (0x34 để query state, 0x3E để query game result, v.v.). - Checksum: Byte cuối cùng là sum % 100 của các byte trước đó.