A method, apparatus, device, and medium for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL.
By caching control commands for offline devices on the cloud platform using a dynamic TTL mechanism and sending data in batches when the devices come online, the message loss problem of the MQTT protocol during device offline is solved, enabling timely message delivery and confirmation, and improving user experience and device reliability.
Patent Information
- Application Number
- CN202611020784.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-09
- Publication Date
- 2026-11-17
AI Technical Summary
The existing MQTT protocol cannot effectively process messages when the device is offline, resulting in the loss of offline messages.
The control commands of offline devices are cached on the cloud platform through a dynamic TTL mechanism, and the cached data is distributed in batches when the devices come online. Combined with the ACK confirmation mechanism of the MQTT protocol, message delivery and cleanup are ensured.
It achieves zero loss of offline device messages, ensuring that user control commands are delivered and executed in a timely manner after the device comes online, thereby improving user experience and device lifespan.
Smart Images

Figure CN123123436A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of Internet of Things (IoT) technology, and in particular to a method, apparatus, device, and medium for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL. Background Technology
[0002] In IoT systems, communication between devices and cloud platforms typically uses the MQTT (Message Queuing Telemetry Transport) protocol. MQTT is a lightweight messaging protocol based on a publish / subscribe model, suitable for communication between IoT devices in low-bandwidth and unstable network environments.
[0003] The MQTT protocol defines three Quality of Service (QoS) levels: QoS 0: deliver at most once, delivery is not guaranteed; QoS 1: deliver at least once, duplicates are possible; QoS 2: deliver exactly once, but has high performance overhead.
[0004] The existing MQTT QoS mechanism is only effective when the device is online; messages cannot be processed when the device is offline.
[0005] Therefore, there is an urgent need for a new method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL. Summary of the Invention
[0006] In view of the above problems, the present invention is proposed to provide a method, apparatus, device and medium for intelligent offline message caching and retransmission of Internet of Things devices based on dynamic TTL to overcome or at least partially solve the above problems.
[0007] Other features and advantages of the invention will become apparent from the following detailed description, or may be learned in part by practice of the invention.
[0008] According to a first aspect of the present invention, a method for intelligent offline message caching and retransmission of Internet of Things (IoT) devices based on dynamic TTL is provided, comprising the following steps: S1: Receive control commands sent by the user via the App through the cloud platform. The control commands include the target device ID, propertyKey, and attribute value. S2: In response to receiving the control command, query and confirm the current online status of the target device based on the MQTT connection status or application layer status record; S3: If the current online status of the target device is offline, obtain the device keepOnlineSecond pre-configured online determination time for the product type to which the target device belongs, and construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. S4: Cache the control command as cache data in the in-memory database Redis with attribute values as the granularity, and set the time-to-live (TTL) of the cache key Key to the device keep-online determination time keepOnlineSecond; if the cache key Key already exists, perform an overwrite operation, replace the original cache data Value with the attribute value of the latest received control command, and refresh the time-to-live (TTL) to the current device keep-online determination time keepOnlineSecond; S5: When the current online status of the target device changes to online status, the cloud platform receives the device online event and scans all cache keys in the Redis memory database according to the target device ID; for each cache key, it reads the attribute value corresponding to it in the cache data value and sends the control command to the target device through the MQTT protocol; S6: If an ACK confirmation message is received from the target device in response to the control command within a predetermined time, the cache key Key corresponding to the Redis in-memory database is immediately deleted; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key Key is deleted and an alarm notification is triggered after a preset number of retries is reached.
[0009] In some embodiments of the present invention, step S2, which involves querying and confirming the current online status of the target device based on MQTT connection status or application layer status records, specifically includes: The first connection state of the target device is confirmed by performing at least one of the following through the MQTT connection layer: TCP connection disconnection detection, MQTT Keepalive heartbeat timeout detection, or sensing MQTT will messages. The second connection status of the target device is determined by whether the difference between the last online time and the current time in the application layer status record is greater than the current device keepOnlineSecond online determination time. If the target device is determined to be offline in either the first connection state or the second connection state, then the current online state of the target device is confirmed to be offline.
[0010] In some embodiments of the present invention, step S4, the execution of the overlay operation includes: Using the cache key as an index, query whether the cache key exists in the in-memory database Redis; If the cache key Key exists, replace the original attribute value in the current cached data Value with the attribute value in the control instruction received this time, while keeping the data structure of the cached data Value unchanged; Refresh the time-to-live (TTL) corresponding to the cache key Key to determine the current device's online status (keepOnlineSecond).
[0011] In some embodiments of the present invention, in step S5, when scanning all cache keys in the Redis memory database according to the target device ID, the SCAN command is used to traverse the database, matching the key prefix corresponding to the target device ID as the matching pattern, and returning the matching cache keys in batches until the traversal is complete.
[0012] In some embodiments of the present invention, the execution retry mechanism in step S6 includes: Define the preset total number of retries and the time interval between each retries, wherein the time interval between two adjacent retries increases sequentially; After each control command is sent to the target device, a timer is started to keep track of the time. If no ACK confirmation message is received from the target device in response to the control command within the predetermined time, the control command is deemed to have failed to be sent and a retry is performed. If no ACK confirmation message is received from the target device in response to the control command after performing the preset total number of retries, the retry is abandoned, the cache key corresponding to the in-memory database Redis is immediately deleted, and an alarm notification is triggered.
[0013] In some embodiments of the present invention, if the deletion of the cache key Key corresponding to the memory database Redis fails in step S6, the deletion of the cache key Key corresponding to the memory database Redis will be automatically performed after the current device keepOnlineSecond obtained based on the overwrite operation expires.
[0014] In some embodiments of the present invention, in step S5, when the control command is sent to the target device via the MQTT protocol, the control command is published to a preset downlink command topic corresponding to the target device, and the ACK confirmation response message is obtained by listening to a preset uplink response topic corresponding to the target device.
[0015] According to a second aspect of the present invention, an intelligent offline message caching and retransmission device for Internet of Things (IoT) devices based on dynamic TTL is provided, the device comprising: The instruction receiving module is used to receive control instructions sent by users via the App through the cloud platform. The control instructions include the target device ID, property key, and attribute value. The status detection module is used to respond to the received control command by querying and confirming the current online status of the target device based on the MQTT connection status or application layer status record. The cache key construction module is used to obtain the device keepOnlineSecond, which is pre-configured for the product type to which the target device belongs, if the current online status of the target device is offline, and to construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. The cache management module is used to cache the control commands as cache data in the in-memory database Redis with attribute values as the granularity, and to set the time-to-live (TTL) of the cache key Key to the device keep-online determination time keepOnlineSecond; if the cache key Key already exists, an overwrite operation is performed to replace the original cache data Value with the attribute value of the latest received control command, and the TTL is refreshed to the current device keep-online determination time keepOnlineSecond; The instruction delivery module is used to receive the device online event when the current online status of the target device changes to online status, and scan all cache keys in the Redis memory database according to the target device ID; for each cache key, read the attribute value corresponding to it in the cache data value, and send the control instruction to the target device through the MQTT protocol; The cache cleanup module is used to immediately delete the cache key corresponding to the Redis in-memory database if an ACK confirmation message is received from the target device in response to the control command within a predetermined time; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key is deleted and an alarm notification is triggered after a preset number of retries is reached.
[0016] According to a third aspect of the present invention, a computer device is provided, including a processor and a memory, the memory storing computer program instructions executable by the processor, wherein when the processor executes the computer program instructions, it implements the instructions as described in any of the above methods.
[0017] According to a fourth aspect of the present invention, a computer-readable storage medium is provided, wherein computer program instructions are stored therein, the computer program instructions being loaded and executed by a processor to perform the operations performed by the method described in any of the preceding claims.
[0018] The technical solutions provided in the embodiments of the present invention have at least the following technical effects or advantages: This invention provides a method, apparatus, device, and medium for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL. This invention forms a complete closed loop through instruction reception, online status detection, dynamic management of cache, and ACK confirmation and cleanup after control instructions are sent. This closed loop ensures that every user control instruction during device offline can be tracked, delivered, and confirmed, thus guaranteeing zero loss of offline messages.
[0019] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 A flowchart illustrating a method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL, provided in an embodiment of the present invention; Figure 2 A schematic diagram illustrating the principle structure of an intelligent offline message caching and retransmission device for IoT devices based on dynamic TTL, provided in an embodiment of the present invention; Figure 3 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0022] Exemplary embodiments of this application will now be described in more detail with reference to the accompanying drawings.
[0023] The accompanying drawings illustrate various structural schematics according to embodiments of this application. These drawings are not to scale, and some details have been enlarged for clarity, and some details may have been omitted. The shapes of the various regions and layers shown in the drawings, as well as their relative sizes and positional relationships, are merely exemplary and may deviate from reality due to manufacturing tolerances or technical limitations. Furthermore, those skilled in the art can design regions / layers with different shapes, sizes, and relative positions as needed.
[0024] It should be noted that the terms "first," "second," etc., used in this application can be used to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from the second element. The terms "comprising" and "having," and any variations thereof, used in this application, are intended to cover non-exclusive inclusion. The term "multiple" used in this application refers to two or more. In the context of this application, similar or identical parts may be represented by the same or similar reference numerals.
[0025] To better understand the above technical solutions, the following will describe the above technical solutions in detail with reference to specific implementation methods. It should be understood that the embodiments of this application and the specific features in the embodiments are detailed descriptions of the technical solutions of the present invention, rather than limitations on the technical solutions of the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0026] Figure 1 This is a flowchart illustrating a method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL, as provided in an embodiment of the present invention. Figure 1 As shown, the intelligent offline message caching and retransmission method for IoT devices based on dynamic TTL includes the following steps: S1: Receive control commands sent by the user via the App through the cloud platform. The control commands include the target device ID, propertyKey, and attribute value. In this embodiment of the invention, a cloud platform (or a device controller within the cloud platform) receives control commands sent by a user via an App. The App (application software) is running control software installed on an IoT device and is used for control communication between the IoT device and the cloud platform. The control command includes a target device ID, a property identifier (propertyKey), and a property value, which are subsequently used to construct a hierarchical attribute-level cache key. The device ID is used to uniquely identify the device. The property identifier (propertyKey) is a unique identifier for the attribute. The attribute is a readable / writable / read-write data feature of the device, used to describe the device's state or parameters. Each attribute has a unique identifier (propertyKey), data type, read / write permissions, and current value (attribute value or attribute value, i.e., the numerical value of the attribute).
[0027] S2: In response to receiving the control command, query and confirm the current online status of the target device based on the MQTT connection status or application layer status record; In step S2, the process of confirming the current online status of the target device based on MQTT connection status or application layer status records specifically includes: confirming the first connection status of the target device by performing at least one of TCP connection disconnection detection, MQTT Keepalive heartbeat timeout detection, or sensing MQTT will messages at the MQTT connection layer; confirming the second connection status of the target device based on whether the difference between the last online time and the current time in the application layer status records is greater than the current device keep-online determination time keepOnlineSecond; and confirming that the target device is offline if either the first or second connection status is determined, then confirming that the current online status of the target device is offline.
[0028] In this embodiment of the invention, TCP connection disconnection detection is performed through the MQTT connection layer as follows: MQTT is based on the TCP protocol, and the TCP connection is interrupted when the device loses power or the network is disconnected; if the device sends a DISCONNECT message normally, the Broker server immediately detects the disconnection; if the device disconnects abnormally (power failure, signal loss), the Broker server needs to wait for the keepalive heartbeat to time out before making a determination.
[0029] In this embodiment of the invention, the MQTT Keepalive heartbeat timeout detection is performed through the MQTT connection layer as follows: when the device establishes an MQTT connection, the keepalive interval is negotiated (e.g., 60 seconds by default); the device needs to send any data packets (including PINGREQ heartbeats) within 1.5 times the keepalive heartbeat timeout (e.g., 90 seconds); if no data is received within the timeout period, the Broker server determines that the device is offline, closes the connection, and publishes a disconnection event.
[0030] In this embodiment of the invention, the execution of the MQTT will message awareness through the MQTT connection layer is as follows: when a device connects, it can register a will message (Last Will), specifying a topic and message content; when the broker server detects that the device has abnormally disconnected, it automatically publishes the will message to the specified topic; the will topic registered by the device is, for example, / device / {deviceId} / offline, and the message body includes the disconnection timestamp.
[0031] In this embodiment of the invention, if any of the following methods are used to detect that a device is offline during TCP connection disconnection detection, MQTT Keepalive heartbeat timeout detection, or MQTT will message detection at the MQTT connection layer, then the first connection state of the target device is confirmed to be offline.
[0032] The cloud platform maintains application-layer status records of devices in the in-memory database Redis, including the status field `status`, the last online time `lastOnlineTime` (precise timestamp), and the corresponding device online determination time `keepOnlineSecond` (precise timestamp). For example, after a device establishes an MQTT connection and passes authentication, the cloud platform writes the following to the in-memory database Redis: Key:device:status:{deviceId}, Value: JSON string `{"status":"online","lastOnlineTime":1713849600000,"keepOnlineSecond":300}`, TTL: set to the `keepOnlineSecond` value configured for this product (e.g., 300 seconds). The second connection status of the target device is determined based on whether the difference between the last online time in the application-layer status record and the current time is greater than the current device online determination time `keepOnlineSecond`. If the difference is greater than the current device online determination time `keepOnlineSecond`, the second connection status of the target device is confirmed to be offline.
[0033] If the target device is determined to be offline in either the first connection state or the second connection state, this embodiment of the invention confirms that the current online state of the target device is offline.
[0034] The keepOnlineSecond parameter represents the maximum allowed offline time a device can remain offline before being considered likely to reconnect. Different product types have different keepOnlineSecond parameters. For example, WiFi smart sockets have 180 seconds (usually recovering within 3 minutes after brief network fluctuations), Zigbee sensors have 3600 seconds (low-power devices have long sleep cycles, and recovery within 1 hour is normal), smart lights have 300 seconds (medium-frequency online), and cameras have 60 seconds (high real-time requirements, being offline for 1 minute is considered abnormal), etc. A reasonable keepOnlineSecond parameter can be configured according to actual application needs, and this embodiment of the invention does not limit this.
[0035] S3: If the current online status of the target device is offline, obtain the device keepOnlineSecond pre-configured online determination time for the product type to which the target device belongs, and construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. The hierarchical cache key is an attribute-level cache key with attribute granularity. The cache key contains a preset separator, such as a colon (:). The cache key is constructed according to the format: device key prefix: device ID: attribute identifier. The device key prefix is a fixed string (e.g., device:cache) used for namespace isolation from other business data (such as device status data) in the Redis in-memory database. That is, the cache key format is device:cache: deviceId: propertyKey, where device:cache is the key prefix, deviceId is the device ID (or device identifier) field, and propertyKey is the attribute identifier field. For example: the cache key for the power attribute of device_001 is device:cache:device_001:power; the cache key for the brightness attribute of device_001 is device:cache:device_001:brightness. The key prefix of the cache key can be used as an index when scanning all cache keys in the Redis in-memory database in subsequent step S5.
[0036] S4: Cache the control command as cache data in the in-memory database Redis with attribute values as the granularity, and set the time-to-live (TTL) of the cache key Key to the device keep-online determination time keepOnlineSecond; if the cache key Key already exists, perform an overwrite operation, replace the original cache data Value with the attribute value of the latest received control command, and refresh the time-to-live (TTL) to the current device keep-online determination time keepOnlineSecond; In step S4, this embodiment of the invention further includes a cache update method for the same attribute receiving multiple control commands during offline periods through an overwrite operation. The overwrite operation specifically includes: using the cache key Key as an index, querying whether the cache key Key exists in the in-memory database Redis; if the cache key Key exists, replacing the original attribute value in the current cached data Value with the attribute value in the currently received control command, while keeping the data structure of the cached data Value unchanged; refreshing the time-to-live (TTL) corresponding to the cache key Key to the current device keepOnlineSecond. For example: A user sends the command 'Set brightness to 30%'; the device is offline, cache key Key=device:cache:device_001:brightness, Value={'value':30}, TTL=300 seconds; the user sends the command 'Set brightness to 50%'; the cache key already exists, so an overwrite operation is performed: Value={'value':50}, TTL refreshed to 300 seconds; the user sends the command 'Set brightness to 80%'; the cache key already exists, so an overwrite operation is performed: Value={'value':80}, TTL refreshed to 300 seconds. This overwrite mechanism ensures that when the device comes online, it receives the user's last operation intent (80%), rather than multiple repeated sends (30%→50%→80%), and the cache space always occupies only 1 cache key, instead of 3 cache keys, reducing space usage. Furthermore, it ensures that each attribute matches only one cache key during the subsequent S5 scan, avoiding repeated sending of intermediate state commands.
[0037] S5: When the current online status of the target device changes to online status, the cloud platform receives the device online event and scans all cache keys in the Redis memory database according to the target device ID; for each cache key, it reads the attribute value corresponding to it in the cache data value and sends the control command to the target device through the MQTT protocol; In step S5, when scanning all cache keys in the Redis in-memory database according to the target device ID, the SCAN command is used for traversal. Matching is performed using the key prefix device:cache:{deviceId}:* corresponding to the target device ID, and the matched cache keys are returned in batches until the traversal is complete. This embodiment of the invention uses the SCAN command instead of the KEYS command to avoid blocking the Redis in-memory database and ensure performance in large-scale device scenarios. Specifically, the SCAN command uses a cursor iteration method to return matched cache keys in batches. Compared to the KEYS command returning all matched cache keys at once, the SCAN command does not block the Redis service for a long time, ensuring that in large-scale IoT scenarios, when multiple devices simultaneously trigger cache scanning, the Redis in-memory database can maintain normal read and write service capabilities. The list of cache keys returned by the SCAN command is directly used as the traversal object for issuing control commands via the MQTT protocol.
[0038] In step S5, when the control command is sent to the target device via the MQTT protocol, the control command is published to a preset downlink command topic (e.g., / device / {deviceId} / command) corresponding to the target device, and the ACK confirmation message is obtained by listening to a preset uplink response topic (e.g., device / {deviceId} / command / ack) corresponding to the target device. This invention employs a publish / subscribe model to decouple command issuance and acknowledgment. The cloud platform publishes control commands to the ` / device / {deviceId} / command` topic, and the device subscribes to this topic to receive commands. After execution, the device replies with an ACK acknowledgment message to the ` / device / {deviceId} / command / ack` topic, and the cloud platform subscribes to this topic to receive the acknowledgment. By distinguishing between the command issuance (preset downlink command) topic and the acknowledgment (preset uplink response) topic, confusion between command issuance and acknowledgment messages in the same channel is avoided, ensuring the accuracy of ACK response message timeout judgment. Thus, the cloud platform can clearly distinguish between two scenarios: the device has not received the command and the device has received the command but the ACK message has been incorrectly routed.
[0039] S6: If an ACK confirmation message is received from the target device in response to the control command within a predetermined time, the cache key Key corresponding to the Redis in-memory database is immediately deleted; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key Key is deleted and an alarm notification is triggered after a preset number of retries is reached.
[0040] In step S6, the retry mechanism includes: defining a preset total number of retries and a time interval for each retrieval, wherein the time interval between two adjacent retries increases sequentially (the time interval is, for example, 1 second, 2 seconds, and 4 seconds sequentially); starting a timer after each control command is sent to the target device; if no ACK confirmation message is received from the target device in response to the control command within the preset time, the control command is determined to have failed, and the next retry is performed; if no ACK confirmation message is received from the target device in response to the control command after performing the preset total number of retries, the retry is abandoned, the cache key corresponding to the Redis in-memory database is immediately deleted, and an alarm notification is triggered. Compared to fixed-interval retries, this invention, by defining an incrementally increasing interval between two adjacent retries, effectively avoids exacerbating the burden during network congestion and improves the delivery success rate in scenarios with instantaneous network fluctuations. Furthermore, by actively deleting the cached key after the total number of retries is exhausted, it ensures that even if a certain instruction cannot be delivered, the device will not repeatedly receive expired instructions when it comes back online, thus avoiding blocking the issuance of subsequent instructions.
[0041] In step S6, when the Redis DEL command fails to execute due to network jitter or other reasons, this embodiment of the invention also includes a cleanup step that combines passive expiration of cache TTL with active deletion. That is, if the deletion of the cache key corresponding to the in-memory database Redis fails, the cache key corresponding to the in-memory database Redis will be automatically deleted after the current device keepOnlineSecond, which is obtained based on the overwrite operation, expires. Thus, the active cleanup in the previous step ensures timeliness, and the passive expiration in this step ensures eventual consistency. When active deletion fails, the automatic expiration mechanism of the in-memory database Redis serves as the final guarantee to ensure that the cache will not remain permanently.
[0042] The intelligent offline message caching and retransmission method for IoT devices based on dynamic TTL described in this invention has the following advantages compared to existing technologies: 1. This invention forms a complete closed loop through instruction reception, online status detection, dynamic management of cache, and ACK confirmation and cleanup after the control instruction is issued. This closed loop ensures that every user control instruction during device offline can be tracked (cached), delivered (issued), and confirmed (executed), thus guaranteeing zero loss of offline messages.
[0043] 2. The cache TTL is bound to the keepOnlineSecond of the device type and does not use a fixed value. This dynamic TTL strategy makes the cache time adapt to the actual online characteristics of the device, which not only extends the cache life of effective devices, but also shortens the garbage data retention time of ineffective devices, thus achieving optimal allocation of cache resources.
[0044] 3. Construct fine-grained cache keys at the attribute level, and set up an overwrite mechanism to overwrite old instructions with new instructions of the same attribute, which greatly saves storage space. After the device comes online, it directly executes the user's last operation intention, avoiding device status replay jumps (such as lights flickering or motors moving back and forth), which greatly improves user experience and device lifespan. The combination of device ID and attribute identifier greatly improves the efficiency of subsequent batch scanning by device dimension.
[0045] 4. Based on a dual cleanup mechanism that combines active deletion with passive TTL expiration, the DEL command is executed immediately upon receiving the ACK, ensuring that delivered instructions are never sent repeatedly. This precisely solves the inherent problem in existing technologies where devices may receive cached messages repeatedly when they go online. Even if active deletion fails due to network jitter, Redis will still automatically reclaim the data after its TTL expires, preventing permanent garbage data from remaining.
[0046] 5. Dual state detection combining MQTT connection status and application layer timestamps accurately covers both direct connection and gateway proxy scenarios. For directly connected devices (WiFi / 4G), it relies on real-time disconnection events from the Broker (TCP connection disconnection detection, MQTT Keepalive heartbeat timeout detection, or detection of MQTT will messages) to achieve millisecond-level offline detection and timely triggering of caching. For gateway proxy devices (Zigbee / Bluetooth), since the gateway maintains a long MQTT connection but the sub-device may be in sleep mode, simple MQTT events cannot detect the offline status of the sub-device. This embodiment of the invention compares lastOnlineTime and keepOnlineSecond in Redis to accurately identify the disconnection status of the sub-device under the gateway, solving the message loss problem caused by false online misjudgment in IoT hybrid networking scenarios.
[0047] Based on the above embodiments, as a supplement to the above... Figure 1 The present invention provides an embodiment of a smart offline message caching and retransmission device for IoT devices based on dynamic TTL, which is similar to the implementation of the method shown. Figure 1 Corresponding to the method embodiments shown, this device can be specifically applied to various electronic devices, see reference. Figure 2 As shown, the IoT device intelligent offline message caching and retransmission device based on dynamic TTL includes: The instruction receiving module 100 is used to receive control instructions sent by users via App through the cloud platform. The control instructions include target device ID, property key, and attribute value. The status detection module 200 is used to respond to the received control command by querying and confirming the current online status of the target device based on the MQTT connection status or application layer status record; The cache key construction module 300 is used to obtain the device keep-online determination time keepOnlineSecond pre-configured for the product type to which the target device belongs if the current online status of the target device is offline, and to construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. The cache management module 400 is used to cache the control instructions as cache data in the in-memory database Redis with attribute values as the granularity, and to set the time-to-live (TTL) of the cache key Key to the device keep-online determination time keepOnlineSecond; if the cache key Key already exists, an overwrite operation is performed to replace the original cache data Value with the attribute value of the latest received control instruction, and the TTL is refreshed to the current device keep-online determination time keepOnlineSecond; The instruction issuing module 500 is used to receive the device online event when the current online status of the target device changes to online status, and scan all cache keys in the Redis memory database according to the target device ID; for each cache key, read the attribute value corresponding to it in the cache data value, and issue the control instruction to the target device through the MQTT protocol; The cache cleanup module 600 is used to immediately delete the cache key corresponding to the Redis memory database if an ACK confirmation message is received from the target device in response to the control command within a predetermined time; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key is deleted and an alarm notification is triggered after a preset number of retries is reached.
[0048] The modules in the aforementioned intelligent offline message caching and retransmission device for IoT devices based on dynamic TTL can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of a computer device in software form, so that the processor can call and execute the corresponding operations of each module.
[0049] The IoT device intelligent offline message caching and retransmission device based on dynamic TTL described in this embodiment can execute the IoT device intelligent offline message caching and retransmission method based on dynamic TTL provided in the above embodiment. The IoT device intelligent offline message caching and retransmission device based on dynamic TTL has the corresponding functional steps and beneficial effects of the IoT device intelligent offline message caching and retransmission method based on dynamic TTL described in the above embodiment. For details, please refer to the embodiment of the IoT device intelligent offline message caching and retransmission method based on dynamic TTL described above. The embodiments of this invention will not be repeated here.
[0050] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 3 As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface, such as a network interface card, is used for wired or wireless communication with external terminals. Wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. The display unit is used to create a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0051] Those skilled in the art will understand that Figure 3 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0052] In one exemplary embodiment, a chip is provided, including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the steps of the method in any of the above embodiments.
[0053] In one exemplary embodiment, a network interface card is provided, including a chip as described in any of the above embodiments and multiple interfaces, wherein the chip communicates externally through the interfaces.
[0054] In one embodiment, a computer device is also provided, including a processor, a chip in any of the above embodiments, or a network interface card in any of the above embodiments, wherein the chip or the network interface card is used to schedule packets to the processor or the chip or the network interface card itself for processing, and the processor is used to process the packets scheduled by the chip or the network interface card.
[0055] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above method embodiments.
[0056] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[0057] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory may include read-only memory (Read-Only Memory). Only memory (ROM), magnetic tape, floppy disk, flash memory, optical storage, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory may include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in this application may include at least one of relational databases and non-relational databases. Non-relational databases may include distributed databases based on blockchain, etc., and are not limited thereto. The processors involved in the various embodiments provided in this application may be general-purpose processors, central processing units, graphics processors, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited thereto.
[0058] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.
[0059] Similarly, it should be understood that, for the purpose of simplification and aiding understanding of one or more aspects of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof in the description of exemplary embodiments of the invention above. Other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the disclosure herein. This application is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein. The specification and embodiments are to be considered exemplary only, and it should be noted that the above embodiments are illustrative of the invention and not restrictive, and that alternative embodiments can be devised by those skilled in the art without departing from its scope.
Claims
1. A method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL, characterized in that, Includes the following steps: S1: Receive control commands sent by the user via the App through the cloud platform. The control commands include the target device ID, propertyKey, and attribute value. S2: In response to receiving the control command, query and confirm the current online status of the target device based on the MQTT connection status or application layer status record; S3: If the current online status of the target device is offline, obtain the device keepOnlineSecond pre-configured online determination time for the product type to which the target device belongs, and construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. S4: Cache the control instructions as cache data in the in-memory database Redis with attribute values as the granularity, and set the time-to-live (TTL) of the cache key Key to the keep-online determination time of the device keepOnlineSecond; If the cache key Key already exists, an overwrite operation is performed, replacing the original cache data Value with the attribute value of the latest received control instruction, and refreshing the Time-To-Live (TTL) to the current device keepOnlineSecond. S5: When the current online status of the target device changes to online status, the cloud platform receives the device online event and scans all cache keys in the Redis memory database according to the target device ID; for each cache key, it reads the attribute value corresponding to it in the cache data value and sends the control command to the target device through the MQTT protocol; S6: If an ACK confirmation message is received from the target device in response to the control command within a predetermined time, the cache key Key corresponding to the Redis in-memory database is immediately deleted; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key Key is deleted and an alarm notification is triggered after a preset number of retries is reached.
2. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that, In step S2, the process of querying and confirming the current online status of the target device based on MQTT connection status or application layer status records specifically includes: The first connection state of the target device is confirmed by performing at least one of the following through the MQTT connection layer: TCP connection disconnection detection, MQTT Keepalive heartbeat timeout detection, or sensing MQTT will messages. The second connection status of the target device is determined by whether the difference between the last online time and the current time in the application layer status record is greater than the current device keepOnlineSecond online determination time. If the target device is determined to be offline in either the first connection state or the second connection state, then the current online state of the target device is confirmed to be offline.
3. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that, In step S4, the overwrite operation includes: Using the cache key as an index, query whether the cache key exists in the in-memory database Redis; If the cache key Key exists, replace the original attribute value in the current cached data Value with the attribute value in the control instruction received this time, while keeping the data structure of the cached data Value unchanged; Refresh the time-to-live (TTL) corresponding to the cache key Key to determine the current device's online status (keepOnlineSecond).
4. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that: In step S5, when scanning all cache keys in the Redis memory database according to the target device ID, the SCAN command is used to traverse the database, matching the key prefix corresponding to the target device ID as the matching pattern, and returning the matching cache keys in batches until the traversal is complete.
5. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that, In step S6, the execution retry mechanism includes: Define the preset total number of retries and the time interval between each retries, wherein the time interval between two adjacent retries increases sequentially; After each control command is sent to the target device, a timer is started to keep track of the time. If no ACK confirmation message is received from the target device in response to the control command within the predetermined time, the control command is deemed to have failed to be sent and a retry is performed. If no ACK confirmation message is received from the target device in response to the control command after performing the preset total number of retries, the retry is abandoned, the cache key corresponding to the in-memory database Redis is immediately deleted, and an alarm notification is triggered.
6. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that: In step S6, if the deletion of the cache key Key corresponding to the in-memory database Redis fails, the deletion of the cache key Key corresponding to the in-memory database Redis will be automatically performed after the current device keepOnlineSecond, which is based on the overwrite operation, expires.
7. The method for intelligent offline message caching and retransmission of IoT devices based on dynamic TTL according to claim 1, characterized in that: In step S5, when the control command is sent to the target device via the MQTT protocol, the control command is published to a preset downlink command topic corresponding to the target device, and the ACK confirmation response message is obtained by listening to a preset uplink response topic corresponding to the target device.
8. A smart offline message caching and retransmission device for IoT devices based on dynamic TTL, applied to the method described in any one of claims 1-7, characterized in that, The device includes: The instruction receiving module is used to receive control instructions sent by users via the App through the cloud platform. The control instructions include the target device ID, property key, and attribute value. The status detection module is used to respond to the received control command by querying and confirming the current online status of the target device based on the MQTT connection status or application layer status record. The cache key construction module is used to obtain the device keepOnlineSecond, which is pre-configured for the product type to which the target device belongs, if the current online status of the target device is offline, and to construct a hierarchical cache key Key containing the device ID field and the attribute identifier field based on the target device ID and the attribute identifier. The cache management module is used to cache the control commands as cache data in the in-memory database Redis with attribute values as the granularity, and to set the time-to-live (TTL) of the cache key Key to the device keep-online determination time keepOnlineSecond; if the cache key Key already exists, an overwrite operation is performed to replace the original cache data Value with the attribute value of the latest received control command, and the TTL is refreshed to the current device keep-online determination time keepOnlineSecond; The instruction delivery module is used to receive the device online event when the current online status of the target device changes to online status, and scan all cache keys in the Redis memory database according to the target device ID; for each cache key, read the attribute value corresponding to it in the cache data value, and send the control instruction to the target device through the MQTT protocol; The cache cleanup module is used to immediately delete the cache key corresponding to the Redis in-memory database if an ACK confirmation message is received from the target device in response to the control command within a predetermined time; if an ACK confirmation message is not received in response to the control command within a predetermined time, a retry mechanism is executed, and the cache key is deleted and an alarm notification is triggered after a preset number of retries is reached.
9. A computer device comprising a processor and a memory, characterized in that, The memory stores computer program instructions that can be executed by the processor, and when the processor executes the computer program instructions, it implements the instructions of the method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions that are loaded and executed by a processor to perform the operations described in any one of claims 1-7.
