A battery replacement cabinet protocol bidirectional conversion method and system

By deploying an independent gateway program instance and a four-thread model for the battery swapping cabinet, the problem of the battery swapping cabinet being unable to connect to the cloud IoT platform was solved, resulting in improved stability, reduced costs, and enhanced data processing capabilities.

CN123145332APending Publication Date: 2026-11-20上海耦荷科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611625360.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-20
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing battery swapping cabinets use a proprietary TCP protocol, which prevents them from being directly connected to cloud-based IoT platforms. This forces equipment manufacturers to redevelop their firmware, resulting in high costs and long development cycles. Furthermore, cloud platforms need to be compatible with each manufacturer's proprietary TCP protocol, leading to high development and maintenance costs.

Method used

Each battery swapping cabinet is deployed with an independent gateway program instance, including a four-thread model and four buffer queues. Protocol conversion is achieved through TCP receive thread, TCP-MQTT protocol conversion thread, MQTT communication thread and MQTT-TCP protocol conversion thread. Data conversion is performed using predefined bidirectional mapping rules to avoid the impact of single point of failure and network congestion.

Benefits of technology

It achieves device-level isolation, improves system stability, reduces equipment modification costs and R&D adaptation cycle, increases data throughput and concurrent processing capabilities, prevents channel blockage, and enables seamless access to the cloud IoT platform.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123145332A_ABST
    Figure CN123145332A_ABST
Patent Text Reader

Abstract

The application provides a battery replacement cabinet machine protocol bidirectional conversion method and system, an independent gateway instance is arranged for each battery replacement cabinet machine, each instance comprises a four-thread model and four cache queues, and each queue asynchronously transmits data; a TCP receiving thread receives TCP private JSON data reported by the cabinet machine and caches the data to a TCP receiving queue; a TCP-MQTT conversion thread converts the data into an MQTT object model format based on a bidirectional mapping rule and caches the data to an MQTT sending queue; an MQTT communication thread reports to the cloud and receives a cloud issued instruction and caches the instruction to an MQTT receiving queue; and an MQTT-TCP conversion thread converts the issued instruction into a TCP private JSON format and caches the instruction to a TCP sending queue and then issues the instruction to the cabinet machine, so that bidirectional asynchronous conversion of the TCP private protocol and the MQTT standard protocol is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of Internet of Things (IoT) communication technology, and more specifically, to a method and system for bidirectional protocol conversion of a battery swapping cabinet. Background Technology

[0002] With the popularization of new energy travel services, smart battery swapping stations are widely deployed in cities. As IoT terminal devices, these stations need to interact with cloud management platforms in real time. Currently, the battery swapping station industry faces a common technical challenge: On the device side, many battery swapping cabinet manufacturers use proprietary TCP protocols for device communication. The cabinets establish TCP connections with servers via mobile networks and exchange data using manufacturer-defined JSON formats. This approach is widely deployed on the device side, resulting in a large number of existing devices. Mainstream cloud-based IoT platforms all use the MQTT protocol as their standard access protocol. A protocol gap exists between the device-side proprietary TCP protocol and the cloud platform's standard MQTT protocol, preventing existing battery swapping cabinets from directly connecting to cloud-based IoT platforms. Device manufacturers need to redevelop their firmware to support the MQTT protocol, which is costly and time-consuming. Cloud platforms need to adapt to each manufacturer's proprietary TCP protocol, resulting in high development and maintenance costs. Summary of the Invention

[0003] In view of this, the purpose of this application is to provide a method and system for bidirectional protocol conversion of battery swapping cabinets, which can realize protocol conversion between battery swapping cabinets and the cloud, and improve system stability.

[0004] This application provides a method for bidirectional protocol conversion of a battery swapping cabinet, the method comprising: Deploy an independent gateway program instance for each battery swapping cabinet; the gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; the four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue, with asynchronous data transfer between the queues. The TCP receiving thread receives the reported JSON data in TCP private protocol format reported by the battery swapping cabinet and caches it in the TCP receiving queue; The TCP-MQTT protocol conversion thread reads the cached reported JSON data, and converts the reported JSON data into reported MQTT data in the cloud MQTT object model format based on the message type of the reported JSON data and the predefined bidirectional mapping rules between the TCPJSON format and MQTT object model fields, and caches it in the MQTT sending queue. The MQTT communication thread reads the cached reported MQTT data and reports the reported MQTT data to the cloud; and receives the MQTT command sent by the cloud and caches it in the MQTT receive queue. The MQTT-TCP protocol conversion thread reads the cached MQTT command, and converts the MQTT command into a JSON command in the TCP private protocol format of the battery swapping cabinet based on the message type of the MQTT command and the bidirectional mapping rule, and caches it in the TCP sending queue. The cached JSON command is sent to the battery swapping cabinet.

[0005] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet further includes: The battery swapping cabinet sends a device login message to the gateway program instance via a TCP connection; the device login message includes a device identity identifier. The gateway program instance verifies the device identity identifier in the device login message; After successful verification, the gateway program instance starts the MQTT communication thread and initiates an MQTT connection to the cloud; When the gateway program instance detects that the TCP connection of the battery swapping cabinet is disconnected, the gateway program instance stops the MQTT communication thread and disconnects the MQTT connection with the cloud.

[0006] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet, wherein deploying an independent gateway program instance for each battery swapping cabinet includes: Each gateway program instance is configured with an independent configuration file, independent memory space, and independent network port, so as to deploy an independent gateway program instance for each battery swapping cabinet. Multiple instances of the gateway program can be deployed centrally on the same server and listen on different ports, or distributed across multiple servers, or a hybrid deployment combining centralized and distributed approaches.

[0007] In some embodiments, in the bidirectional protocol conversion method for the battery swapping cabinet, the TCP receiving thread receives the reported JSON data in TCP private protocol format reported by the battery swapping cabinet and caches it in the TCP receiving queue, including: The TCP receiving thread receives JSON data in TCP private protocol format reported by the battery swapping cabinet. The reported JSON data will be attempted to be parsed directly into a JSON object. If the parsing fails, the reported JSON data will be appended to the end of the dedicated receive buffer. The accumulated data in the receiving buffer is parsed into JSON again. If the parsing is successful, the receiving buffer is checked for any remaining unparsed data. If it does not exist, then the integrity check passes, the receive buffer is cleared, and the parsed JSON object is stored in the TCP receive queue. If parsing fails, a timeout period is started, and the receive buffer is automatically cleared after the timeout.

[0008] In some embodiments, the message types in the bidirectional conversion method of the battery swapping cabinet protocol include device login, login response, attribute reporting, attribute reporting response, alarm reporting, alarm reporting response, remote control command, and remote control response. The TCP-MQTT protocol conversion thread and the MQTT-TCP protocol conversion thread execute the conversion logic corresponding to the message type in the bidirectional mapping rule, respectively, according to the message type.

[0009] In some embodiments, in the bidirectional protocol conversion method for the battery swapping cabinet, the TCP-MQTT protocol conversion thread executes TCP-MQTT direction mapping rules according to the message type, and the TCP-MQTT direction mapping rules include: For device login messages, extract the device ID and transaction number from the reported JSON data, map the device ID to the device identifier of the cloud IoT platform, and construct an MQTT login message containing the device identifier and transaction number; For attribute reporting messages, extract cabinet attribute data and battery attribute data from the reported JSON data, map the device private attribute ID to the cloud object model attribute field according to the pre-configured attribute mapping table, and construct an MQTT attribute reporting message; For alarm reporting messages, extract the alarm data from the reported JSON data, map the device private alarm code to the cloud alarm field according to the pre-configured alarm mapping table, and construct the MQTT alarm reporting message; For remote control response messages, extract the message type, device identifier, transaction number, and control response result from the reported JSON data, map the device identifier to the cloud device identifier, and construct an MQTT control response message.

[0010] In some embodiments, in the bidirectional protocol conversion method for the battery swapping cabinet, the MQTT-TCP protocol conversion thread executes MQTT-TCP direction mapping rules according to the message type, and the MQTT-TCP direction mapping rules include: For the login response message, extract the response result and transaction number from the MQTT login response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct a login response message in TCPJSON format; For attribute reporting response messages, extract the response result and transaction number from the MQTT attribute reporting response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct an attribute reporting response message in TCPJSON format. For alarm reporting response messages, extract the response result and transaction number from the MQTT alarm reporting response sent from the cloud, and construct an alarm reporting response message in TCPJSON format; For remote control command messages, extract the list of control parameters from the MQTT remote control commands sent from the cloud, map the cloud control parameters to the control fields of the battery swapping cabinet according to the pre-configured control parameter mapping table, and construct remote control command messages in TCPJSON format.

[0011] In some embodiments, the bidirectional conversion method for the battery swapping cabinet protocol includes mapping the device's private alarm code to a cloud alarm field according to a pre-configured alarm mapping table, including: Map the BMS fault codes at the motor replacement cabinet to the BMS fault fields in the cloud. The single-warehouse level alarm code at the motor replacement cabinet is mapped to a single-warehouse alarm field or single-warehouse fault field in the cloud according to the preset coding range to which the coding value of the single-warehouse level alarm code belongs. The cabinet-level alarm code at the motor replacement cabinet end is mapped to the cabinet-level alarm field or cabinet-level fault field in the cloud according to the preset coding range to which the coding value of the cabinet-level alarm code belongs.

[0012] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet further includes: When the MQTT command issued by the cloud is a battery swapping control command, the MQTT-TCP protocol conversion thread extracts the cloud transaction number and user identifier from the battery swapping control command and stores them in a pre-configured request-response matching table. During the battery swapping process, the battery swapping cabinet reports multiple alarm messages via TCP protocol. The alarm messages include user identifier and alarm description fields. The TCP-MQTT protocol conversion thread matches the corresponding cloud transaction number in the request-response matching table according to the user identifier in the alarm message, replaces the original device transaction number in the alarm message with the cloud transaction number, and reports it to the cloud. At the same time, the original device transaction number is backed up to the request-response matching table. When the cloud returns a response instruction based on the reported alarm message, the MQTT-TCP protocol conversion thread searches for the backup of the original transaction number of the device in the request-response matching table based on the cloud transaction number in the response, replaces it, and sends it to the battery swapping cabinet so that the device can recognize and execute the cloud response. If the TCP-MQTT protocol conversion thread determines that battery swapping is in progress based on the alarm description field, it will continue to wait for subsequent alarm messages. If the battery swap is determined to be complete based on the alarm description field, the battery swap process is marked as complete, and the corresponding record is cleared from the request-response matching table. If the alarm description field indicates that the battery swapping process has been abnormally completed, then the battery swapping process is marked as abnormally ended, and the corresponding record is removed from the request-response matching table.

[0013] In some embodiments, a bidirectional protocol conversion system for battery swapping cabinets is also provided, the system comprising: multiple independently deployed gateway program instances, each gateway program instance corresponding to a battery swapping cabinet; Each of the gateway program instances includes a four-thread model and four buffer queues, wherein the four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; The four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue. Each queue is used for asynchronous data transfer between threads. The TCP receiving thread is used to receive JSON data in TCP private protocol format reported by the battery swapping cabinet and cache it in the TCP receiving queue. The TCP-MQTT protocol conversion thread is used to read the reported JSON data cached in the TCP receive queue, and according to the message type of the reported JSON data, based on the predefined bidirectional mapping rules between the TCPJSON format and the MQTT object model fields, convert the reported JSON data into reported MQTT data in the cloud MQTT object model format, and cache it in the MQTT send queue. The MQTT communication thread is used to read the reported MQTT data cached in the MQTT sending queue and report it to the cloud; and to receive the MQTT sending instructions issued by the cloud and cache them in the MQTT receiving queue. The MQTT-TCP protocol conversion thread is used to read the MQTT instructions cached in the MQTT receive queue, and based on the message type of the MQTT instructions and the bidirectional mapping rules, convert the MQTT instructions into JSON instructions in the TCP private protocol format of the battery swapping cabinet, and cache them in the TCP send queue. The gateway program instance is also used to send the JSON command cached in the TCP sending queue to the battery swapping cabinet.

[0014] This application provides a method and system for bidirectional protocol conversion for battery swapping cabinets. An independent gateway program instance is deployed for each battery swapping cabinet, achieving device-level isolation and effectively avoiding the impact of single-point failures and network congestion on overall business, significantly improving system stability. The gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receiving thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread. The four buffer queues include a TCP receiving queue, an MQTT sending queue, an MQTT receiving queue, and a TCP sending queue. Asynchronous data transmission occurs between these queues, decoupling protocol conversion from data transmission and reception, greatly improving data throughput and concurrent processing capabilities, and preventing channel blockage caused by network jitter or cloud response delays. The TCP receiving thread receives JSON data reported by the battery swapping cabinet in a TCP private protocol format and caches it in the TCP receiving queue. The TCP-MQTT protocol conversion thread reads the cached JSON data and, based on the message class of the JSON data... Based on a predefined bidirectional mapping rule between TCP / JSON format and MQTT object model fields, the system converts the reported JSON data into cloud-based MQTT object model format reported MQTT data and caches it in the MQTT sending queue. The MQTT communication thread reads the cached reported MQTT data and reports it to the cloud. It also receives MQTT commands from the cloud and caches them in the MQTT receiving queue. The MQTT-TCP protocol conversion thread reads the cached MQTT commands and, based on the message type and the bidirectional mapping rule, converts them into JSON commands in the battery swapping cabinet's TCP private protocol format and caches them in the TCP sending queue. The cached JSON commands are then sent to the battery swapping cabinet. The protocol conversion rule is a bidirectional mapping between fields, enabling flexible configuration and automatic conversion of different protocol message formats. This allows seamless access to the cloud IoT platform without modifying the battery swapping cabinet's original TCP private protocol, significantly reducing equipment modification costs and R&D adaptation cycles. Attached Figure Description

[0015] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 A flowchart of the bidirectional protocol conversion method for the battery swapping cabinet described in an embodiment of this application is shown; Figure 2 This paper illustrates a system architecture diagram of the bidirectional protocol conversion of the battery swapping cabinet described in an embodiment of this application. Figure 3 A schematic diagram of the deployment architecture of the system described in the embodiments of this application is shown; Figure 4 A schematic diagram of fault isolation as described in an embodiment of this application is shown; Figure 5 A schematic diagram of the data flow of the four-thread model described in an embodiment of this application is shown; Figure 6 A flowchart illustrating the TCP→MQTT protocol conversion process described in an embodiment of this application is shown. Figure 7 A flowchart of the MQTT→TCP protocol conversion steps described in an embodiment of this application is shown; Figure 8 A flowchart illustrating the asynchronous request-response matching process described in an embodiment of this application is shown. Figure 9 A flowchart of the health monitoring method according to an embodiment of this application is shown. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the accompanying drawings in this application are for illustrative and descriptive purposes only and are not intended to limit the scope of protection of this application. Furthermore, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate operations implemented according to some embodiments of this application. It should be understood that the operations in the flowcharts may not be implemented in sequence, and steps without logical contextual relationships may be reversed or implemented simultaneously. In addition, those skilled in the art, guided by the content of this application, may add one or more other operations to the flowcharts, or remove one or more operations from the flowcharts.

[0018] Furthermore, the described embodiments are merely some, not all, of the embodiments of this application. The components of the embodiments of this application described and illustrated herein can typically be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.

[0019] It should be noted that the term "comprising" will be used in the embodiments of this application to indicate the presence of the features declared thereafter, but does not exclude the addition of other features.

[0020] On the device side, many battery swapping cabinet manufacturers use proprietary TCP protocols for device communication. The cabinets establish TCP connections with servers via mobile networks and exchange data using manufacturer-defined JSON formats. This approach is widely deployed on the device side, resulting in a large number of existing devices. Mainstream cloud-based IoT platforms all use the MQTT protocol as their standard access protocol. A protocol gap exists between the device-side proprietary TCP protocol and the cloud platform's standard MQTT protocol, preventing existing battery swapping cabinets from directly connecting to cloud-based IoT platforms. Device manufacturers need to redevelop their firmware to support the MQTT protocol, which is costly and time-consuming. Cloud platforms need to adapt to each manufacturer's proprietary TCP protocol, resulting in high development and maintenance costs.

[0021] Based on this, this application provides a method and system for bidirectional protocol conversion of battery swapping cabinets. An independent gateway program instance is deployed for each battery swapping cabinet, achieving device-level isolation and effectively avoiding the impact of single-point failures and network congestion on overall business, significantly improving system stability. The gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receiving thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread. The four buffer queues include a TCP receiving queue, an MQTT sending queue, an MQTT receiving queue, and a TCP sending queue. Asynchronous data transmission occurs between these queues, decoupling protocol conversion from data transmission and reception, greatly improving data throughput and concurrent processing capabilities, and preventing channel blockage caused by network jitter or cloud response delays. The TCP receiving thread receives JSON data in TCP private protocol format reported by the battery swapping cabinet and caches it in the TCP receiving queue. The TCP-MQTT protocol conversion thread reads the cached JSON data and, based on the message of the JSON data... The system uses a bidirectional mapping rule between predefined TCP / JSON format and MQTT object model fields to convert the reported JSON data into cloud-based MQTT object model format reported MQTT data, which is then cached in the MQTT sending queue. The MQTT communication thread reads the cached reported MQTT data and reports it to the cloud. It also receives MQTT commands from the cloud and caches them in the MQTT receiving queue. The MQTT-TCP protocol conversion thread reads the cached MQTT commands and, based on the message type and the bidirectional mapping rule, converts them into JSON commands in the battery swapping cabinet's TCP private protocol format, which are then cached in the TCP sending queue. The cached JSON commands are then sent to the battery swapping cabinet. The protocol conversion rule is a bidirectional mapping between fields, enabling flexible configuration and automatic conversion of different protocol message formats. This allows seamless access to the cloud IoT platform without modifying the battery swapping cabinet's original TCP private protocol, significantly reducing equipment modification costs and R&D adaptation cycles.

[0022] Please refer to Figure 1 , Figure 1 A flowchart of the bidirectional protocol conversion method for the battery swapping cabinet according to an embodiment of this application is shown. The method includes the following steps S101-S105: S101. Deploy an independent gateway program instance for each battery swapping cabinet; the gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; the four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue, with asynchronous data transfer between the queues. S102. The TCP receiving thread receives the reported JSON data in TCP private protocol format reported by the battery swapping cabinet and caches it in the TCP receiving queue. S103. The TCP-MQTT protocol conversion thread reads the cached reported JSON data, and converts the reported JSON data into reported MQTT data in the cloud MQTT object model format according to the message type of the reported JSON data and based on the predefined bidirectional mapping rules between the TCPJSON format and MQTT object model fields, and caches it in the MQTT sending queue. S104. The MQTT communication thread reads the cached reported MQTT data and reports the reported MQTT data to the cloud; and receives the MQTT command sent by the cloud and caches it in the MQTT receive queue. S105. The MQTT-TCP protocol conversion thread reads the cached MQTT command to be sent, and converts the MQTT command to a JSON command in the TCP private protocol format of the battery swapping cabinet based on the message type of the MQTT command and the bidirectional mapping rule, and caches it in the TCP sending queue. The cached JSON command is sent to the battery swapping cabinet.

[0023] In step S101, an independent gateway program instance is deployed for each battery swapping cabinet. The gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread. The four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue. Asynchronous data transmission is performed between the queues.

[0024] The gateway instance refers to an independently running gateway program process, which contains complete TCP access, MQTT communication, protocol conversion and other functional threads; each gateway instance has an independent configuration file, independent memory space, independent network port, and is completely isolated from other gateway instances.

[0025] Here, the thread can also be called a module. That is to say, the gateway program instance includes a TCP receiving module, a TCP-MQTT protocol conversion module, an MQTT communication module, and an MQTT-TCP protocol conversion module.

[0026] The TCP receiving module is used to monitor the TCP connection of the battery swapping cabinet, receive JSON format data reported by the device, and send control commands to the device. The MQTT communication module is used to establish an MQTT connection with the cloud-based IoT platform to enable device data reporting and cloud command reception.

[0027] The TCP-MQTT protocol conversion module, also known as the TCP→MQTT protocol conversion module, is used to convert the TCP private protocol of the battery swapping cabinet into the MQTT object model format.

[0028] The MQTT-TCP protocol conversion module, also known as the MQTT→TCP protocol conversion module, is used to convert cloud-based MQTT commands into TCP JSON format for the battery swapping cabinet.

[0029] The four cache queues belong to the data buffer module and are four independent thread-safe JSON queues used for asynchronous data transfer between modules.

[0030] In some embodiments, the gateway program instance further includes a configuration management module for loading and managing the dedicated configuration information of the battery swapping cabinet, and a health monitoring module implemented by the main thread, which periodically checks the running status flags of the TCP server thread, MQTT communication thread, TCP→MQTT protocol conversion thread, and MQTT→TCP protocol conversion thread, and records alarm logs when an anomaly is detected.

[0031] The bidirectional conversion method for the battery swapping cabinet protocol is applied to a bidirectional conversion system for the battery swapping cabinet protocol. The system includes: multiple independently deployed gateway program instances, each gateway program instance corresponding to one battery swapping cabinet. Each of the gateway program instances includes a four-thread model and four buffer queues, wherein the four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; The four buffer queues include a TCP receive queue, an MQTT send queue, and a TCP send queue. Each queue is used for asynchronous data transfer between threads; please refer to... Figure 2 , Figure 2 The system architecture diagram of the bidirectional protocol conversion of the battery swapping cabinet described in this application embodiment is shown.

[0032] Please refer to Figure 3 , Figure 3This diagram illustrates the deployment architecture of the system described in an embodiment of this application; specifically, Figure 3 The document illustrates both centralized and distributed deployment scenarios. In some embodiments, deploying an independent gateway program instance for each battery swapping cabinet includes: Each gateway program instance is configured with an independent configuration file, independent memory space, and independent network port, so as to deploy an independent gateway program instance for each battery swapping cabinet. Multiple instances of the gateway program can be deployed centrally on the same server and listen on different ports, or distributed across multiple servers, or a hybrid deployment combining centralized and distributed approaches.

[0033] The gateway program instance can also be referred to simply as a gateway instance.

[0034] The system adopts a one-machine-one-instance deployment architecture, as follows: there is a one-to-one mapping relationship between the device (i.e., the battery swapping cabinet) and the gateway instance. Each battery swapping cabinet is bound to an independent IP address and port number. Each port number corresponds to an independent gateway program instance. Each gateway instance has an independent configuration file containing exclusive device authentication information.

[0035] The centralized deployment refers to the deployment of multiple gateway instances on the same server, with each instance listening on a different port.

[0036] The distributed deployment refers to the deployment of multiple gateway instances on multiple servers to achieve geographical-level disaster recovery.

[0037] The hybrid deployment refers to a combination of centralized deployment of some gateway instances and distributed deployment of others, which can be flexibly combined according to business needs.

[0038] All gateway instances are connected to the same cloud-based IoT platform.

[0039] Each gateway instance uses independent device authentication information. The cloud platform manages all battery swapping cabinets uniformly, eliminating the need for a gateway deployment architecture.

[0040] Please refer to Figure 4 , Figure 4 The diagram illustrates the fault isolation described in the embodiment of this application. Since the gateway instances are completely isolated, with no shared state, no shared memory, and no shared ports, the failure of a single gateway instance only affects the corresponding battery swapping cabinet, and the failure of a single server only affects the gateway instance deployed on that server. The scope of the fault impact is controllable and will not cause cascading faults.

[0041] The four-thread model allows each thread to focus on a single function, reducing coupling between modules and facilitating maintenance and expansion. Asynchronous communication between threads is achieved through a data buffer queue, avoiding blocking and waiting, and improving system throughput. The queue capacity is configurable; when a certain stage of processing is slow, the queue acts as a buffer to prevent data loss and achieve flow control. The processing logic of each thread can be independently adjusted according to business needs without modifying other modules, demonstrating strong scalability.

[0042] Please refer to Figure 5 , Figure 5 A schematic diagram of the data flow of the four-thread model described in an embodiment of this application is shown; as follows: Figure 5 As shown, the bidirectional data flow between the battery swapping cabinet and the cloud is as follows: Uplink (device → cloud) is as follows: The TCP private protocol data generated by the battery swapping cabinet is received by the TCP receiving thread and stored in the TCP receiving queue; after being processed by the TCP → MQTT conversion (i.e., the TCP-MQTT protocol conversion thread), it is converted into the MQTT object model format and stored in the MQTT sending queue; finally, it is reported to the cloud IoT platform by the MQTT communication thread.

[0043] The downlink (cloud → device) process is as follows: MQTT commands sent from the cloud are received by the MQTT communication thread and stored in the MQTT receive queue; after being processed by the MQTT→TCP conversion (i.e., the MQTT-TCP protocol conversion thread), they are converted into the TCP private protocol format and stored in the TCP send queue; finally, they are sent to the battery swapping cabinet by the TCP send thread.

[0044] The entire process is decoupled asynchronously through queues, enabling parallel processing of protocol conversion and data transmission and reception.

[0045] The TCP access steps of the standalone gateway program instance described in this application are as follows: S201, Create a TCP server socket and bind it to the configured port; S202, Set the socket to non-blocking mode and enable address reuse; S203, Use a multiplexed I / O mechanism to listen for connection events and set a timeout; S204, Accept TCP connection requests from the battery swapping cabinet and record client information (IP, port, socket descriptor); S205, Create an independent receiving thread for each client connection; S206, Use a multiplexed I / O mechanism in the TCP receiving thread. Listen for data readability events and set timeouts; the S207 and TCP packet assembly strategies adopt a buffer-based streaming JSON packet assembly mechanism; specifically including: Initial parsing: After receiving data from the device, attempt to parse the received data directly into a JSON object; Buffer accumulation: If the initial parsing fails, it indicates that the data is incomplete (TCP packet splicing / reassembly), and the received data is appended to the end of the client's dedicated receive buffer; Cumulative parsing: Perform JSON parsing on the accumulated data in the receive buffer; Integrity verification: After successful parsing, perform a JSON integrity check to confirm that all data has been parsed completely and there are no remnants. Unparsed content; Parsing failure recovery: If parsing still fails cumulatively, set a parsing failure flag and start a timeout; if parsing is still unsuccessful within a specified time, automatically clear the receive buffer to prevent the continuous accumulation of abnormal data; Success processing: After successful parsing and integrity verification, clear the receive buffer and store the JSON object in the TCP receive queue; S208, Client timeout detection: If there is no activity after the configured timeout period, close the connection; S209, Linkage mechanism between TCP connection and MQTT connection: Device login triggers MQTT connection: After the battery swapping cabinet connects via TCP, it must send a device login message ( After the device completes registration (including device identification), the gateway verifies the device's identity before starting the MQTT communication thread and connecting to the cloud IoT platform, signifying the device's official online status. Device offline triggers MQTT disconnection: When the TCP connection of the battery swapping cabinet is disconnected (due to power failure, network abnormality, active disconnection, etc.), the gateway stops the MQTT communication thread and disconnects from the cloud IoT platform, signifying the device's official offline status. Connection status synchronization: The TCP connection status and the MQTT connection status are kept synchronized. If the TCP connection is not established or has been disconnected, the MQTT connection will not be started or will automatically disconnect, ensuring consistency between the device's online and offline status.

[0046] In step S207, the TCP packet assembly strategy adopts a buffer-based streaming JSON packet assembly mechanism. That is, the TCP receiving thread receives the JSON data reported by the battery swapping station in TCP private protocol format and caches it in the TCP receiving queue, including: In other words, the TCP receiving thread receives JSON data in TCP private protocol format reported by the battery swapping cabinet. The reported JSON data will be attempted to be parsed directly into a JSON object. If the parsing fails, the reported JSON data will be appended to the end of the dedicated receive buffer. The accumulated data in the receiving buffer is parsed into JSON again. If the parsing is successful, the receiving buffer is checked for any remaining unparsed data. If it does not exist, then the integrity check passes, the receive buffer is cleared, and the parsed JSON object is stored in the TCP receive queue. If parsing fails, a timeout period is started, and the receive buffer is automatically cleared after the timeout.

[0047] In battery swapping stations, the racks are often located in underground garages or remote areas where 4G signals are unstable, and TCP communication often results in two situations: packet sticking and incomplete packet transmission.

[0048] Data merging (data clustering): When the terminal continuously reports multiple data entries, network fluctuations may cause several JSON data entries to be sent together. Direct parsing will cause errors in the code. Therefore, the data is automatically "temporarily stored" in a buffer, similar to piecing together data. Figure 1 By piecing together a complete JSON string before putting it into the queue, packet loss due to data concatenation is avoided, ensuring that every battery swap record and battery voltage data can be completely uploaded to the cloud without being missed due to network lag.

[0049] Partial packet (data only received): If the signal is extremely poor, the terminal will only send half of the data before disconnecting, and the rest will never arrive. Therefore, a "timeout" mechanism is involved, which acts as an automatic "garbage cleanup mechanism"—it will not allow this "crippled" data to occupy memory indefinitely, but will automatically clear the buffer after the timeout. This prevents the memory from being overwhelmed by invalid data, ensuring that the gateway program can run stably for a long time and will not cause the entire gateway program to freeze due to poor signal from a single terminal.

[0050] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet further includes: The battery swapping cabinet sends a device login message to the gateway program instance via a TCP connection; the device login message includes a device identity identifier. The gateway program instance verifies the device identity identifier in the device login message; After successful verification, the gateway program instance starts the MQTT communication thread and initiates an MQTT connection to the cloud; When the gateway program instance detects that the TCP connection of the battery swapping cabinet is disconnected, the gateway program instance stops the MQTT communication thread and disconnects the MQTT connection with the cloud.

[0051] The device login verification described in this application involves sending a "device identity identifier" as the first step after the cabinet connects to the gateway. Only after the gateway verifies the identifier will it initiate an MQTT connection to the cloud, thereby preventing unauthorized counterfeit devices or malicious attackers from accessing the cloud. Even if someone plugs an unfamiliar device into the network, it cannot connect to the gateway instance because it lacks a valid identity identifier, thus avoiding the consumption of cloud connection resources and ensuring the security of the cloud platform.

[0052] TCP connection termination triggers MQTT termination (automatic resource reclamation): If the battery swapping station suddenly loses power or network connection, the TCP connection will be terminated. This rule requires the gateway to immediately and synchronously disconnect the MQTT connection with the cloud. This practice has significant practical implications: it prevents "zombie connections" from consuming cloud licenses. Cloud connections often have commercial license limits (e.g., only 10,000 devices can connect simultaneously). If connections are not terminated promptly, the cloud will assume the station is still online, resulting in licenses being held by dead devices, preventing new devices from connecting. This mechanism ensures that licenses are released promptly to truly online stations, significantly reducing cloud connection overruns caused by device outages.

[0053] Steps S103-S105 are protocol conversion steps between TCP and MQTT.

[0054] Based on the message type of the reported JSON data, and using the predefined bidirectional mapping rules between the TCPJSON format and the MQTT object model fields, the conversion between issuing MQTT commands and the battery swapping cabinet's TCP private protocol is realized.

[0055] In some embodiments, the message types include device login, login response, attribute reporting, attribute reporting response, alarm reporting, alarm reporting response, remote control command, and remote control response; The TCP-MQTT protocol conversion thread and the MQTT-TCP protocol conversion thread execute the conversion logic corresponding to the message type in the bidirectional mapping rule, respectively, according to the message type.

[0056] Please refer to Figure 6 , Figure 6 A flowchart illustrating the TCP→MQTT protocol conversion process described in an embodiment of this application is shown.

[0057] In this embodiment of the application, in step S103, the TCP-MQTT protocol conversion thread reads the cached reported JSON data, and converts the reported JSON data into reported MQTT data in the cloud MQTT object model format based on the message type of the reported JSON data and the predefined bidirectional mapping rules between the TCPJSON format and MQTT object model fields, including the following steps S301-S304: Step S301: The TCP→MQTT conversion thread retrieves data from the TCP receive queue and sets a timeout period. If no data is received after the timeout, the thread continues to loop and check the running status flags, waiting for the next round of data to arrive. Step S302: Parse the message type field in the JSON to identify the message type; Step S303: Execute different conversion logic based on the message type. Specifically, For device login messages, the process includes: extracting the device ID and transaction number; mapping the device ID to the device identifier of the cloud IoT platform; constructing an MQTT login message and storing it in the MQTT sending queue. For attribute reporting messages, the process includes: extracting cabinet attribute data and battery attribute data; performing attribute field mapping conversion to map the device private attribute ID to the cloud object model field; constructing an MQTT attribute reporting message and storing it in the MQTT sending queue. For alarm reporting messages, the process includes: extracting alarm data, performing alarm field mapping conversion, and mapping device private alarm IDs to cloud alarm fields; supporting hierarchical alarm coding (BMS level, single warehouse level, and whole cabinet level): BMS level alarms map device-side BMS fault codes to cloud-side BMS fault fields; single warehouse level alarms map device-side single warehouse alarm codes and single warehouse fault codes to cloud-side single warehouse alarm fields and single warehouse fault fields; whole cabinet level alarms map device-side whole cabinet alarm codes to cloud-side whole cabinet alarm fields and whole cabinet fault fields; finally, constructing MQTT alarm reporting messages and storing them in the MQTT sending queue.

[0058] For remote control response messages, the following are extracted: message type, device identifier (mapped to cloud device identifier), transaction number, and control response result; if the response result indicates execution failure, the previously stored cloud control command is found in the request-response matching table according to the transaction number and deleted, indicating that the control command failed to execute and there is no need to continue waiting for a response; an MQTT control response message is constructed and stored in the MQTT sending queue.

[0059] Step S304: After the conversion is complete, release the original JSON object. In other words, the TCP-MQTT protocol conversion thread executes the TCP-MQTT direction mapping rules according to the message type, and the TCP-MQTT direction mapping rules include: For device login messages, extract the device ID and transaction number from the reported JSON data, map the device ID to the device identifier of the cloud IoT platform, and construct an MQTT login message containing the device identifier and transaction number; For attribute reporting messages, extract cabinet attribute data and battery attribute data from the reported JSON data, map the device private attribute ID to the cloud object model attribute field according to the pre-configured attribute mapping table, and construct an MQTT attribute reporting message; For alarm reporting messages, extract the alarm data from the reported JSON data, map the device private alarm code to the cloud alarm field according to the pre-configured alarm mapping table, and construct the MQTT alarm reporting message; For remote control response messages, extract the message type, device identifier, transaction number, and control response result from the reported JSON data, map the device identifier to the cloud device identifier, and construct an MQTT control response message.

[0060] In some embodiments, mapping device private alarm codes to cloud alarm fields according to a pre-configured alarm mapping table includes: Map the BMS fault codes at the motor replacement cabinet to the BMS fault fields in the cloud. The single-warehouse level alarm code at the motor replacement cabinet is mapped to a single-warehouse alarm field or single-warehouse fault field in the cloud according to the preset coding range to which the coding value of the single-warehouse level alarm code belongs. The cabinet-level alarm code at the motor replacement cabinet end is mapped to the cabinet-level alarm field or cabinet-level fault field in the cloud according to the preset coding range to which the coding value of the cabinet-level alarm code belongs.

[0061] Here, the messages reported by the battery swapping cabinet are mainly of three types, among which: device login messages (highest priority), which the gateway must process as soon as it receives them - verifying identity and establishing cloud channel to ensure that subsequent battery swapping operations can proceed normally.

[0062] Attribute reporting messages (normal heartbeats), such as temperature, voltage, and number of idle warehouses, can be reported in batches or smoothly, even if the message volume is large but the real-time requirement is not high.

[0063] Alarm reports, such as those about battery overheating or door malfunctions, need to reach the cloud and be pushed to maintenance personnel in the shortest possible time.

[0064] The protocol conversion described in this application involves mapping logic for hierarchical alarm codes (BMS level, single warehouse level, and whole cabinet level). The faults of the battery swapping cabinet have clear levels. If there is no hierarchical mapping, the cloud will only receive a string of numerical codes (such as "Error_503"), and the maintenance personnel will have to look up the meaning in the manual, which will not be able to handle it in a timely manner.

[0065] In other words, the alarm data before mapping is the alarm code, and the alarm data after mapping is the cloud alarm field, including BMS-level fault field, single warehouse-level alarm field, single warehouse-level fault field, whole container-level alarm field, and whole container-level fault field, which are used by the cloud for alarm classification, push strategy matching, and maintenance work order generation.

[0066] For example, in a BMS (Battery Management System) alarm, if the temperature of a cell inside a battery is too high, the code reported by the device is BMS_OV_01; the effect after mapping is that the cloud directly displays "BMS fault: Battery ID=001, cell temperature too high".

[0067] In the alarm codes at the single-compartment and whole-compartment levels of the battery swapping cabinet, based on the preset coding range, they are mapped to the single-compartment alarm field or single-compartment fault field in the cloud, which means they are classified in the conversion thread. The original codes provided by the battery swapping cabinet manufacturers are often flat and messy (e.g., "Err_101"); the manufacturer may only define what kind of problem it is, but in actual operation and maintenance, different codes represent different "consequences". For the same type of equipment, "compartment door not closed" is just inconvenience (alarm), but "charging short circuit" is a safety hazard (fault). Therefore, alarms or faults are distinguished based on the preset code value range. For example, if the code value falls in the range of 1-100, it is mapped to "single compartment alarm". After receiving it in the cloud, it only records and pushes an APP notification, and the operation and maintenance personnel can deal with it in a unified manner during the day. If the code value falls in the range of 101-200, it is mapped to "single compartment fault". The cloud immediately triggers a telephone voice alarm and generates an emergency work order to ensure that serious problems that really affect safety or cause downtime are not drowned out by a large number of ordinary reminders.

[0068] In this embodiment, the classification logic is completed incidentally in the conversion thread of the gateway (edge). When the cloud receives data, it directly obtains the "classified" standard fields. The cloud no longer needs to configure a complex "encoding mapping table" or perform arithmetic judgments based on the encoding values. It can directly distribute data based on the fields, saving cloud computing power and significantly reducing latency when parsing data. Moreover, the code value tables of different manufacturers' devices are different. The method described in this embodiment only requires modifying the threshold of the preset encoding range in the configuration file of the gateway instance for the generator cabinet. There is no need to modify the cloud code or upgrade the cabinet firmware, which can flexibly adapt to the severity level classification of different manufacturers and models of devices.

[0069] In step S104, the MQTT communication thread reads the cached reported MQTT data and reports the reported MQTT data to the cloud; and receives the MQTT command sent by the cloud and caches it in the MQTT receive queue.

[0070] In this embodiment of the application, the MQTT communication steps include the following S401-S408: Step S401: Initialize IoT platform SDK dependencies; Step S402: Create a device instance and configure authentication information such as product key, device name, and device key; Step S403: Configure MQTT connection parameters, including server address and port; Step S404: Set the message callback function, status callback function, and result callback function; Step S405: Establish an MQTT connection and subscribe to the downlink topic; Step S406: Retrieve data from the MQTT send queue and construct an MQTT message; Step S407: Publish to the upstream topic; Step S408: After receiving the downlink message from the cloud, parse the JSON format and store it in the MQTT receive queue.

[0071] In step S105, the MQTT-TCP protocol conversion thread reads the cached MQTT command and, based on the message type of the MQTT command and the bidirectional mapping rules, converts the MQTT command into a JSON command in the TCP private protocol format of the battery swapping cabinet, and caches it in the TCP sending queue.

[0072] In some embodiments, the MQTT-TCP protocol conversion thread executes MQTT-TCP direction mapping rules according to the message type, the MQTT-TCP direction mapping rules including: For the login response message, extract the response result and transaction number from the MQTT login response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct a login response message in TCPJSON format; For attribute reporting response messages, extract the response result and transaction number from the MQTT attribute reporting response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct an attribute reporting response message in TCPJSON format. For alarm reporting response messages, extract the response result and transaction number from the MQTT alarm reporting response sent from the cloud, and construct an alarm reporting response message in TCPJSON format; For remote control command messages, extract the list of control parameters from the MQTT remote control commands sent from the cloud, map the cloud control parameters to the control fields of the battery swapping cabinet according to the pre-configured control parameter mapping table, and construct remote control command messages in TCPJSON format.

[0073] Please refer to Figure 7 , Figure 7 A flowchart illustrating the MQTT→TCP protocol conversion steps described in an embodiment of this application is shown.

[0074] In this embodiment of the application, the MQTT→TCP protocol conversion process includes the following steps S501-S504: Step S501: The MQTT→TCP conversion thread retrieves data from the MQTT receive queue and sets a timeout period. If no data is received after the timeout, the thread continues to loop and check the running status flags, waiting for the next round of data to arrive. Step S502: Parse the message type field in the JSON and identify the message type. Step S503: Execute different conversion logic based on message type: For the login response message, extract the response result and transaction number; map the cloud device identifier back to the battery swapping cabinet device ID; construct a TCP login response message and store it in the TCP sending queue.

[0075] For the attribute reporting response message, extract the response result and transaction number; map the cloud device identifier back to the battery swapping cabinet device ID; construct the TCP attribute reporting response message and store it in the TCP sending queue.

[0076] For alarm reporting response messages, extract the response result and transaction number; search for the backup transaction number in the request matching table, and replace it if it exists; construct a TCP alarm reporting response message and store it in the TCP sending queue.

[0077] For remote control command messages, the control parameter list is extracted, and control parameter mapping conversion is performed to map the cloud control parameters to the battery swapping cabinet control fields. Different processing is performed according to the control command type: if it is a control command that requires waiting for a device response, the control message is stored in the request-response matching table for matching when receiving a device response later, and then a TCP remote control command message is constructed and stored in the TCP sending queue; for other types of commands, a TCP remote control command message is directly constructed and stored in the TCP sending queue.

[0078] In step S105, the cached JSON command is sent to the battery swapping cabinet.

[0079] In some embodiments, the TCP sending function can be implemented by the MQTT-TCP protocol conversion thread or the existing TCP receiving thread.

[0080] In some embodiments, a separate TCP sending thread can be configured.

[0081] Sending the cached JSON command to the battery swapping cabinet includes the following steps S501-S504: Step S501: Retrieve data from the TCP send queue; Step S502: Serialize the JSON object into a string; Step S503: Traverse all active client connections and send data; Step S504: If sending fails, record the error log and close the corresponding client connection.

[0082] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet further includes: When the MQTT command issued by the cloud is a battery swapping control command, the MQTT-TCP protocol conversion thread extracts the cloud transaction number and user identifier from the battery swapping control command and stores them in a pre-configured request-response matching table. During the battery swapping process, the battery swapping cabinet reports multiple alarm messages via TCP protocol. The alarm messages include user identifier and alarm description fields. The TCP-MQTT protocol conversion thread matches the corresponding cloud transaction number in the request-response matching table according to the user identifier in the alarm message, replaces the original device transaction number in the alarm message with the cloud transaction number, and reports it to the cloud. At the same time, the original device transaction number is backed up to the request-response matching table. When the cloud returns a response instruction based on the reported alarm message, the MQTT-TCP protocol conversion thread searches for the backup of the original transaction number of the device in the request-response matching table based on the cloud transaction number in the response, replaces it, and sends it to the battery swapping cabinet so that the device can recognize and execute the cloud response. If the TCP-MQTT protocol conversion thread determines that battery swapping is in progress based on the alarm description field, it will continue to wait for subsequent alarm messages. If the battery swap is determined to be complete based on the alarm description field, the battery swap process is marked as complete, and the corresponding record is cleared from the request-response matching table. If the alarm description field indicates that the battery swapping process has been abnormally completed, then the battery swapping process is marked as abnormally ended, and the corresponding record is removed from the request-response matching table.

[0083] Here, user identifiers are used to establish cross-protocol associations between multiple alarms and the original battery swapping control commands throughout the entire battery swapping process, linking fragmented alarms into a complete business chain, enabling the cloud to track the entire battery swapping process. Through a bidirectional replacement and backup mechanism between cloud transaction numbers and the original device transaction numbers, seamless bridging of cross-protocol transaction numbers is achieved, ensuring that cloud responses are accurately delivered to the target device.

[0084] In battery swapping cabinet equipment communication, there is a time difference between requests and responses. This time difference can be caused by factors such as equipment processing delays, network transmission delays, and equipment offline reconnection. To ensure the correct association between requests and responses, this application's embodiment employs an asynchronous request-response matching mechanism based on transaction numbers. Please refer to... Figure 8 , Figure 8 A flowchart illustrating the asynchronous request-response matching process described in an embodiment of this application is shown.

[0085] In some embodiments, the asynchronous request-response matching mechanism includes the following steps: Step S601: Create a JSON table to store the request messages to be responded to; Step S602: The JSON table contains the following fields: JSON object, storage timestamp, and entry ID; Step S603: When sending a request, store the request JSON in a table and assign a unique ID; Step S604: Upon receiving a response, look up the corresponding request in the table based on the transaction number; Step S605: After a successful match, delete the corresponding entry in the table; Step S606: Supports two matching methods: by user ID and by transaction number.

[0086] Matching by transaction number is used for regular control command and response matching. Specifically, when a control command is issued from the cloud, the command and its transaction number are stored in the request matching table; when the device returns a response, the original command that matches the transaction number is found. User identifier matching is used for cross-protocol association of control commands and alarm messages in the battery swapping process. Specifically, when the cloud sends battery swapping control commands (including user identifiers) via MQTT, it stores the commands in the request matching table. During the battery swapping process, the device reports multiple alarm messages (including user identifier and alarm description fields) via TCP. The gateway matches the alarm messages with the original control commands based on the user identifier to determine the status of the battery swapping process. If the alarm description field indicates that the battery swapping process is in progress, it continues to wait for subsequent messages. If the alarm description field indicates that the battery swapping process is completed, it marks the battery swapping process as completed. If the alarm description field indicates that the battery swapping process is abnormally completed, it marks the battery swapping process as abnormally terminated. After a successful match, the original device transaction number in the alarm message is replaced with the transaction number of the cloud control command, and the message is reported to the cloud. At the same time, the original device transaction number is backed up in the request matching table. When the cloud responds to the alarm, the gateway looks up the backed-up original device transaction number from the request matching table, replaces it, and sends it to the device.

[0087] The battery swapping process status tracking and cleanup are as follows: When a battery swapping completion alarm is received, the corresponding control command record in the matching table is cleared; when a battery swapping abnormal completion alarm is received, the corresponding control command record in the matching table is cleared, and a battery swapping completion flag is set.

[0088] In some embodiments, the bidirectional protocol conversion method for the battery swapping cabinet also includes health monitoring of the gateway program instance; please refer to... Figure 9 , Figure 9 A flowchart of the health monitoring method according to an embodiment of this application is shown.

[0089] In some embodiments, the health monitoring steps include: Step S701: The main thread enters a loop and periodically performs health checks; Step S702: Check the running status flags of the following components: TCP server thread, MQTT communication thread, TCP→MQTT protocol conversion thread, and MQTT→TCP protocol conversion thread; Step S703: If any component's running status flag is found to be abnormal, record an alarm log and indicate that the corresponding component is not running; Step S704: After the health check is completed, hibernate for a specified time interval and proceed to the next round of checks; Step S705: Upon receiving an exit signal, set the exit flag and stop all working threads; Step S706: After all threads exit, release the resources (JSON table, queue, log module).

[0090] The data structure in the bidirectional conversion method of the battery swapping cabinet protocol described in this application embodiment is as follows.

[0091] The server configuration structure is as follows: The configuration information of the gateway instance includes: server listening IP and port, maximum number of connections, send and receive cache size, log level and log file path, client timeout, JSON queue length, battery swapping cabinet device ID, and cloud IoT platform authentication information (instance ID, product key, device name, device key).

[0092] The JSON queue structure includes the following components: head and tail pointers, queue size and capacity, mutex lock, non-empty condition variable, and non-full condition variable; supported operations include: enqueue: adding a JSON object to the tail of the queue, supporting blocking wait; dequeue: retrieving a JSON object from the head of the queue, supporting timeout wait; viewing the head element (without dequeuing); and getting the queue size.

[0093] The JSON table structure is used for matching asynchronous request responses and contains the following components: an array of entries: each entry contains a JSON object, a storage timestamp, and an entry ID; and an array capacity: the current number of entries, the next available ID, and a mutex lock.

[0094] The message type definitions are shown in Table 1 below.

[0095] Table 1:

[0096] Table 1 shows the definitions of eight message types.

[0097] When the gateway instance starts, it loads the configuration from the XML configuration file. The configuration content includes: TCP listening port, battery swapping cabinet device ID, cloud IoT platform authentication information, and log configuration.

[0098] For example, the gateway program instance provided in this application supports both centralized and distributed deployment architecture modes.

[0099] In centralized deployment mode, multiple gateway program instances are deployed on the same server, with each instance listening on a different TCP port and connecting to a different battery swapping cabinet. For example, three gateway instances can be deployed simultaneously on server A, located in the directories " / path / gateway_1 / , / path / gateway_2 / , and / path / gateway_3 / ", respectively, listening on ports P1, P2, and P3. Each instance has an independent configuration file, configured with different ports and device authentication information.

[0100] In distributed deployment mode, multiple gateway program instances are deployed on multiple servers to achieve geographically-level disaster recovery. For example, server A (IP address A) runs gateway instance 1 connected to battery swapping cabinet 1, server B (IP address B) runs gateway instance 2 connected to battery swapping cabinet 2, and server C (IP address C) runs gateway instance 3 connected to battery swapping cabinet 3. Each instance listens on the same port P1 but is bound to a different IP address. The two deployment methods can be flexibly selected based on actual business needs and data center resources.

[0101] The configuration and startup process for the gateway program instance is as follows: Each gateway program instance needs to prepare an independent configuration file for the battery swapping cabinet it connects to. This configuration file contains parameters such as the TCP listening port, battery swapping cabinet device ID, cloud IoT platform authentication information (instance ID, product key, device name, device key), and log configuration, thereby supporting the configuration of differentiated protocol mapping rules for battery swapping cabinets from different manufacturers and of different models.

[0102] After configuration, deploy the configuration file to the specified directory on the server. When starting the gateway program, pass the configuration file path as a startup parameter. After loading the configuration file, the gateway program initializes the TCP server, MQTT client, and protocol conversion module in sequence. Then, it creates four worker threads: a TCP receiving thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread, entering normal operation. Finally, the battery swapping cabinet connects to the port listened to by the gateway instance via TCP and begins data communication.

[0103] In the methods described in this application embodiment, the data flow example is as follows: In the attribute reporting process, the battery swapping cabinet sends attribute reporting JSON data to the gateway program instance via a TCP connection. This data includes information such as message type, device ID, transaction number, cabinet attribute list, and battery attribute list. The TCP receiving thread receives this data, parses it into a JSON object, and stores it in the TCP receiving queue. The TCP-MQTT protocol conversion thread retrieves the data from the TCP receiving queue, performs field mapping conversion, maps the device's private attribute ID to the standard attribute field defined by the cloud IoT platform's object model, constructs an MQTT-formatted attribute reporting message, and stores it in the MQTT sending queue. The MQTT communication thread reads the message from the MQTT sending queue, publishes it to the cloud IoT platform, and completes the attribute reporting.

[0104] In the remote control process, the cloud sends control commands via the MQTT protocol. These commands include information such as message type, device identifier, transaction number, and a list of control parameters. The MQTT communication thread receives the command and stores it in the MQTT receive queue. The MQTT-TCP protocol conversion thread retrieves the command from the MQTT receive queue, performs parameter mapping conversion, maps the cloud control parameters to private control fields recognizable by the battery swapping cabinet, constructs a JSON command in the TCP private protocol format, and stores it in the TCP send queue. The TCP send thread retrieves the data from the TCP send queue and sends it to the corresponding battery swapping cabinet. Upon receiving the command, the battery swapping cabinet executes the corresponding control operation and returns an execution response.

[0105] This application also provides a comprehensive exception handling mechanism to ensure the long-term stable operation of the gateway program instance in complex network environments. When the received JSON data has an incorrect format or fails to be parsed, the system records an error log and appends the data to the end of a dedicated receive buffer. The accumulated data in the buffer is parsed again. If parsing still fails, a timeout is started, and the receive buffer is automatically cleared after the timeout to prevent invalid data from blocking the queue. For client connections, if there is no activity after the configured timeout period, the system actively closes the connection to release resources. When the queue capacity reaches its limit, the producer thread blocks and waits until there is available space in the queue to prevent data overflow. When the MQTT connection is unexpectedly disconnected, the cloud IoT platform SDK automatically triggers a reconnection mechanism without manual intervention. Each component integrates a health monitoring module, which records alarm logs when an anomaly occurs and supports instance-level restart of abnormal instances to ensure the continuous availability of the overall service.

[0106] The bidirectional protocol conversion method for battery swapping cabinets described in this application deploys an independent gateway program instance for each battery swapping cabinet as a bidirectional conversion bridge between the TCP private protocol and the MQTT standard protocol. The battery swapping cabinet can connect to the cloud IoT platform without modifying the device firmware. Device manufacturers do not need to redevelop the MQTT protocol stack, and the cloud platform does not need to adapt to the private protocols of each manufacturer, which significantly reduces the development cost and adaptation cycle of devices to the cloud. The "one machine, one instance" deployment architecture ensures that instances are completely isolated and have no shared state. The failure of a single instance only affects the corresponding device, and the failure of a single server only affects the instances on that server, minimizing the scope of the fault impact and preventing cascading failures. At the same time, the independent log files facilitate quick problem location.

[0107] Furthermore, the method effectively solves the data incompleteness problem caused by TCP packet splicing / reassembly through a buffer-based streaming JSON packet assembly and integrity verification mechanism. In addition, the device can only establish an MQTT connection after TCP login verification, and the MQTT will automatically disconnect when TCP is disconnected. This linkage mechanism ensures accurate synchronization of the device's online and offline status and avoids invalid connections occupying cloud resources.

[0108] Furthermore, the method uses a request-response matching table to connect multiple alarms reported separately during the battery swapping process into a complete business link, using the user identifier as a clue, and combines the alarm description field to identify the battery swapping in progress, completed, or abnormally completed status in real time.

[0109] In a single step, the method simultaneously supports unified mapping of three levels of alarm structures: BMS level, single warehouse level, and whole container level. It converts the private alarm codes of different manufacturers into cloud standard fields, providing operation and maintenance personnel with accurate fault location information.

[0110] Based on the same inventive concept, this application also provides a bidirectional conversion device for the battery swapping cabinet protocol corresponding to the bidirectional conversion method for the battery swapping cabinet protocol. Since the principle of the device in this application is similar to the bidirectional conversion method for the battery swapping cabinet protocol described above, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be described again.

[0111] Please refer to Figure 2 , Figure 2 A schematic diagram of the bidirectional protocol conversion system for battery swapping cabinets according to an embodiment of this application is shown; the system includes: multiple independently deployed gateway program instances, each gateway program instance corresponding to one battery swapping cabinet; Each of the gateway program instances includes a four-thread model and four buffer queues, wherein the four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; The four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue. Each queue is used for asynchronous data transfer between threads. The TCP receiving thread is used to receive JSON data in TCP private protocol format reported by the battery swapping cabinet and cache it in the TCP receiving queue. The TCP-MQTT protocol conversion thread is used to read the reported JSON data cached in the TCP receive queue, and according to the message type of the reported JSON data, based on the predefined bidirectional mapping rules between the TCPJSON format and the MQTT object model fields, convert the reported JSON data into reported MQTT data in the cloud MQTT object model format, and cache it in the MQTT send queue. The MQTT communication thread is used to read the reported MQTT data cached in the MQTT sending queue and report it to the cloud; and to receive the MQTT sending instructions issued by the cloud and cache them in the MQTT receiving queue. The MQTT-TCP protocol conversion thread is used to read the MQTT instructions cached in the MQTT receive queue, and based on the message type of the MQTT instructions and the bidirectional mapping rules, convert the MQTT instructions into JSON instructions in the TCP private protocol format of the battery swapping cabinet, and cache them in the TCP send queue. The gateway program instance is also used to send the JSON command cached in the TCP sending queue to the battery swapping cabinet.

[0112] In some embodiments, the battery swapping cabinet protocol bidirectional conversion system is further used for: The gateway program instance is also used to: receive a device login message sent by the battery swapping cabinet via a TCP connection, wherein the device login message includes a device identity identifier; Verify the device identity identifier in the device login message; After successful verification, the MQTT communication thread is started and an MQTT connection is initiated to the cloud; When the TCP connection of the battery swapping cabinet is detected to be disconnected, the MQTT communication thread is stopped and the MQTT connection with the cloud is disconnected.

[0113] Each gateway instance is configured with an independent configuration file, independent memory space, and independent network port; multiple gateway instances support centralized deployment. In some embodiments, in the bidirectional conversion system of the battery swapping cabinet protocol, each gateway program instance is configured with an independent configuration file, an independent memory space, and an independent network port; multiple gateway program instances can be centrally deployed on the same server and listen to different ports, or distributedly deployed on multiple servers, or a hybrid deployment combining centralized and distributed methods. In some embodiments, the bidirectional protocol conversion system of the battery swapping cabinet... The TCP receiving thread, when receiving the JSON data reported by the battery swapping cabinet in the TCP private protocol format and caching it in the TCP receiving queue, is specifically used for: The TCP receiving thread receives JSON data in TCP private protocol format reported by the battery swapping cabinet. The reported JSON data will be attempted to be parsed directly into a JSON object. If the parsing fails, the reported JSON data will be appended to the end of the dedicated receive buffer. The accumulated data in the receiving buffer is parsed into JSON again. If the parsing is successful, the receiving buffer is checked for any remaining unparsed data. If it does not exist, then the integrity check passes, the receive buffer is cleared, and the parsed JSON object is stored in the TCP receive queue. If parsing fails, a timeout period is started, and the receive buffer is automatically cleared after the timeout.

[0114] In some embodiments, in the bidirectional protocol conversion system of the battery swapping cabinet, the message types include device login, login response, attribute reporting, attribute reporting response, alarm reporting, alarm reporting response, remote control command, and remote control response; The TCP-MQTT protocol conversion thread and the MQTT-TCP protocol conversion thread execute the conversion logic corresponding to the message type in the bidirectional mapping rule, respectively, according to the message type.

[0115] In some embodiments, in the bidirectional protocol conversion system of the battery swapping cabinet, the TCP-MQTT protocol conversion thread executes TCP-MQTT direction mapping rules according to the message type, and the TCP-MQTT direction mapping rules include: For device login messages, extract the device ID and transaction number from the reported JSON data, map the device ID to the device identifier of the cloud IoT platform, and construct an MQTT login message containing the device identifier and transaction number; For attribute reporting messages, extract cabinet attribute data and battery attribute data from the reported JSON data, map the device private attribute ID to the cloud object model attribute field according to the pre-configured attribute mapping table, and construct an MQTT attribute reporting message; For alarm reporting messages, extract the alarm data from the reported JSON data, map the device private alarm code to the cloud alarm field according to the pre-configured alarm mapping table, and construct the MQTT alarm reporting message; For remote control response messages, extract the message type, device identifier, transaction number, and control response result from the reported JSON data, map the device identifier to the cloud device identifier, and construct an MQTT control response message.

[0116] In some embodiments of the battery swapping cabinet protocol bidirectional conversion system, the MQTT-TCP protocol conversion thread executes MQTT-TCP direction mapping rules according to the message type, and the MQTT-TCP direction mapping rules include: For the login response message, extract the response result and transaction number from the MQTT login response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct a login response message in TCPJSON format; For attribute reporting response messages, extract the response result and transaction number from the MQTT attribute reporting response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct an attribute reporting response message in TCPJSON format. For alarm reporting response messages, extract the response result and transaction number from the MQTT alarm reporting response sent from the cloud, and construct an alarm reporting response message in TCPJSON format; For remote control command messages, extract the list of control parameters from the MQTT remote control commands sent from the cloud, map the cloud control parameters to the control fields of the battery swapping cabinet according to the pre-configured control parameter mapping table, and construct remote control command messages in TCPJSON format.

[0117] In some embodiments, in the bidirectional protocol conversion system of the battery swapping cabinet, the TCP-MQTT protocol conversion thread, when mapping the device's private alarm code to a cloud alarm field according to a pre-configured alarm mapping table, is specifically used for: Map the BMS fault codes at the motor replacement cabinet to the BMS fault fields in the cloud. The single-warehouse level alarm code at the motor replacement cabinet is mapped to a single-warehouse alarm field or single-warehouse fault field in the cloud according to the preset coding range to which the coding value of the single-warehouse level alarm code belongs. The cabinet-level alarm code at the motor replacement cabinet end is mapped to the cabinet-level alarm field or cabinet-level fault field in the cloud according to the preset coding range to which the coding value of the cabinet-level alarm code belongs.

[0118] In some embodiments, in the bidirectional protocol conversion system for the battery swapping cabinet, the TCP-MQTT protocol conversion thread is further configured to: when the MQTT instruction issued by the cloud is a battery swapping control instruction, the MQTT-TCP protocol conversion thread extracts the cloud transaction number and user identifier from the battery swapping control instruction and stores them in a pre-configured request-response matching table. During the battery swapping process, the battery swapping cabinet reports multiple alarm messages via TCP protocol. The alarm messages include user identifier and alarm description fields. The TCP-MQTT protocol conversion thread matches the corresponding cloud transaction number in the request-response matching table according to the user identifier in the alarm message, replaces the original device transaction number in the alarm message with the cloud transaction number, and reports it to the cloud. At the same time, the original device transaction number is backed up to the request-response matching table. When the cloud returns a response instruction based on the reported alarm message, the MQTT-TCP protocol conversion thread searches for the backup of the original transaction number of the device in the request-response matching table based on the cloud transaction number in the response, replaces it, and sends it to the battery swapping cabinet so that the device can recognize and execute the cloud response. If the TCP-MQTT protocol conversion thread determines that battery swapping is in progress based on the alarm description field, it will continue to wait for subsequent alarm messages. If the battery swap is determined to be complete based on the alarm description field, the battery swap process is marked as complete, and the corresponding record is cleared from the request-response matching table. If the alarm description field indicates that the battery swapping process has been abnormally completed, then the battery swapping process is marked as abnormally ended, and the corresponding record is removed from the request-response matching table.

[0119] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.

[0120] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0121] In addition, the functional units in the embodiments provided in this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.

[0122] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0123] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.

[0124] Finally, it should be noted that the above-described embodiments are merely specific implementations of this application, used to illustrate the technical solutions of this application, and not to limit them. The protection scope of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this application; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application. All should be covered within the protection scope of this application. Therefore, the protection scope of this application should be determined by the protection scope of the claims.

Claims

1. A method for bidirectional protocol conversion of a battery swapping cabinet, characterized in that: The method includes: Deploy an independent gateway program instance for each battery swapping cabinet; the gateway program instance includes a four-thread model and four buffer queues. The four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; the four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue, with asynchronous data transfer between the queues. The TCP receiving thread receives the reported JSON data in TCP private protocol format reported by the battery swapping cabinet and caches it in the TCP receiving queue; The TCP-MQTT protocol conversion thread reads the cached reported JSON data, and converts the reported JSON data into reported MQTT data in the cloud MQTT object model format based on the message type of the reported JSON data and the predefined bidirectional mapping rules between the TCPJSON format and MQTT object model fields, and caches it in the MQTT sending queue. The MQTT communication thread reads the cached reported MQTT data and reports the reported MQTT data to the cloud; and receives the MQTT command sent by the cloud and caches it in the MQTT receive queue. The MQTT-TCP protocol conversion thread reads the cached MQTT command, and converts the MQTT command into a JSON command in the TCP private protocol format of the battery swapping cabinet based on the message type of the MQTT command and the bidirectional mapping rule, and caches it in the TCP sending queue. The cached JSON command is sent to the battery swapping cabinet.

2. The bidirectional protocol conversion method for battery swapping cabinets according to claim 1, characterized in that: The method further includes: The battery swapping cabinet sends a device login message to the gateway program instance via a TCP connection; the device login message includes a device identity identifier. The gateway program instance verifies the device identity identifier in the device login message; After successful verification, the gateway program instance starts the MQTT communication thread and initiates an MQTT connection to the cloud; When the gateway program instance detects that the TCP connection of the battery swapping cabinet is disconnected, the gateway program instance stops the MQTT communication thread and disconnects the MQTT connection with the cloud.

3. The bidirectional protocol conversion method for battery swapping cabinets according to claim 1, characterized in that: The deployment of an independent gateway program instance for each battery swapping cabinet includes: Each gateway program instance is configured with an independent configuration file, independent memory space, and independent network port, so as to deploy an independent gateway program instance for each battery swapping cabinet. Multiple instances of the gateway program can be deployed centrally on the same server and listen on different ports, or distributed across multiple servers, or a hybrid deployment combining centralized and distributed approaches.

4. The bidirectional protocol conversion method for battery swapping cabinets according to claim 1, characterized in that: The TCP receiving thread receives the reported JSON data in TCP private protocol format reported by the battery swapping cabinet, and caches it in the TCP receiving queue, including: The TCP receiving thread receives JSON data in TCP private protocol format reported by the battery swapping cabinet. The reported JSON data will be attempted to be parsed directly into a JSON object. If the parsing fails, the reported JSON data will be appended to the end of the dedicated receive buffer. The accumulated data in the receiving buffer is parsed into JSON again. If the parsing is successful, the receiving buffer is checked for any remaining unparsed data. If it does not exist, then the integrity check passes, the receive buffer is cleared, and the parsed JSON object is stored in the TCP receive queue. If parsing fails, a timeout period is started, and the receive buffer is automatically cleared after the timeout.

5. The bidirectional protocol conversion method for battery swapping cabinets according to claim 1, characterized in that: The message types include device login, login response, attribute reporting, attribute reporting response, alarm reporting, alarm reporting response, remote control command, and remote control response; The TCP-MQTT protocol conversion thread and the MQTT-TCP protocol conversion thread execute the conversion logic corresponding to the message type in the bidirectional mapping rule, respectively, according to the message type.

6. The bidirectional protocol conversion method for battery swapping cabinets according to claim 5, characterized in that: The TCP-MQTT protocol conversion thread executes the TCP-MQTT direction mapping rules according to the message type. The TCP-MQTT direction mapping rules include: For device login messages, extract the device ID and transaction number from the reported JSON data, map the device ID to the device identifier of the cloud IoT platform, and construct an MQTT login message containing the device identifier and transaction number; For attribute reporting messages, extract cabinet attribute data and battery attribute data from the reported JSON data, map the device private attribute ID to the cloud object model attribute field according to the pre-configured attribute mapping table, and construct an MQTT attribute reporting message; For alarm reporting messages, extract the alarm data from the reported JSON data, map the device private alarm code to the cloud alarm field according to the pre-configured alarm mapping table, and construct the MQTT alarm reporting message; For remote control response messages, extract the message type, device identifier, transaction number, and control response result from the reported JSON data, map the device identifier to the cloud device identifier, and construct an MQTT control response message.

7. The bidirectional protocol conversion method for battery swapping cabinets according to claim 5, characterized in that: The MQTT-TCP protocol conversion thread executes the MQTT-TCP direction mapping rules according to the message type. The MQTT-TCP direction mapping rules include: For the login response message, extract the response result and transaction number from the MQTT login response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct a login response message in TCPJSON format; For attribute reporting response messages, extract the response result and transaction number from the MQTT attribute reporting response sent from the cloud, map the cloud device identifier to the battery swapping cabinet device ID, and construct an attribute reporting response message in TCPJSON format. For alarm reporting response messages, extract the response result and transaction number from the MQTT alarm reporting response sent from the cloud, and construct an alarm reporting response message in TCPJSON format; For remote control command messages, extract the list of control parameters from the MQTT remote control commands sent from the cloud, map the cloud control parameters to the control fields of the battery swapping cabinet according to the pre-configured control parameter mapping table, and construct remote control command messages in TCPJSON format.

8. The bidirectional protocol conversion method for battery swapping cabinets according to claim 6, characterized in that: The step of mapping the device's private alarm code to cloud alarm fields according to a pre-configured alarm mapping table includes: Map the BMS fault codes at the motor replacement cabinet to the BMS fault fields in the cloud. The single-warehouse level alarm code at the motor replacement cabinet is mapped to a single-warehouse alarm field or single-warehouse fault field in the cloud according to the preset coding range to which the coding value of the single-warehouse level alarm code belongs. The cabinet-level alarm code at the motor replacement cabinet end is mapped to the cabinet-level alarm field or cabinet-level fault field in the cloud according to the preset coding range to which the coding value of the cabinet-level alarm code belongs.

9. The bidirectional protocol conversion method for battery swapping cabinets according to claim 1 or 5, characterized in that: The method further includes: When the MQTT command issued by the cloud is a battery swapping control command, the MQTT-TCP protocol conversion thread extracts the cloud transaction number and user identifier from the battery swapping control command and stores them in a pre-configured request-response matching table. During the battery swapping process, the battery swapping cabinet reports multiple alarm messages via TCP protocol. The alarm messages include user identifier and alarm description fields. The TCP-MQTT protocol conversion thread matches the corresponding cloud transaction number in the request-response matching table according to the user identifier in the alarm message, replaces the original device transaction number in the alarm message with the cloud transaction number, and reports it to the cloud. At the same time, the original device transaction number is backed up to the request-response matching table. When the cloud returns a response instruction based on the reported alarm message, the MQTT-TCP protocol conversion thread searches for the backup of the original transaction number of the device in the request-response matching table based on the cloud transaction number in the response, replaces it, and sends it to the battery swapping cabinet so that the device can recognize and execute the cloud response. If the TCP-MQTT protocol conversion thread determines that battery swapping is in progress based on the alarm description field, it will continue to wait for subsequent alarm messages. If the battery swap is determined to be complete based on the alarm description field, the battery swap process is marked as complete, and the corresponding record is cleared from the request-response matching table. If the alarm description field indicates that the battery swapping process has been abnormally completed, then the battery swapping process is marked as abnormally ended, and the corresponding record is removed from the request-response matching table.

10. A bidirectional protocol conversion system for a battery swapping cabinet, characterized in that, The system includes: multiple independently deployed gateway program instances, each gateway program instance corresponding to a battery swapping cabinet; Each of the gateway program instances includes a four-thread model and four buffer queues, wherein the four-thread model includes a TCP receive thread, a TCP-MQTT protocol conversion thread, an MQTT communication thread, and an MQTT-TCP protocol conversion thread; The four buffer queues include a TCP receive queue, an MQTT send queue, an MQTT receive queue, and a TCP send queue. Each queue is used for asynchronous data transfer between threads. The TCP receiving thread is used to receive JSON data in TCP private protocol format reported by the battery swapping cabinet and cache it in the TCP receiving queue. The TCP-MQTT protocol conversion thread is used to read the reported JSON data cached in the TCP receive queue, and according to the message type of the reported JSON data, based on the predefined bidirectional mapping rules between the TCPJSON format and the MQTT object model fields, convert the reported JSON data into reported MQTT data in the cloud MQTT object model format, and cache it in the MQTT send queue. The MQTT communication thread is used to read the reported MQTT data cached in the MQTT sending queue and report it to the cloud; and to receive the MQTT sending instructions issued by the cloud and cache them in the MQTT receiving queue. The MQTT-TCP protocol conversion thread is used to read the MQTT instructions cached in the MQTT receive queue, and based on the message type of the MQTT instructions and the bidirectional mapping rules, convert the MQTT instructions into JSON instructions in the TCP private protocol format of the battery swapping cabinet, and cache them in the TCP send queue. The gateway program instance is also used to send the JSON command cached in the TCP sending queue to the battery swapping cabinet.