diff --git a/water-admin/src/main/resources/application.yml b/water-admin/src/main/resources/application.yml index e81bb6e..13fb8b2 100644 --- a/water-admin/src/main/resources/application.yml +++ b/water-admin/src/main/resources/application.yml @@ -47,7 +47,7 @@ mqtt: command-ack: enabled: true max-retry-count: 3 - retry-interval-ms: 30000 + retry-interval-ms: 10000 scan-interval-ms: 5000 pending-ttl-seconds: 86400 ack-ttl-seconds: 86400 diff --git a/water-common/water-common-mqtt/src/main/java/org/dromara/mqtt/config/properties/MqttProperties.java b/water-common/water-common-mqtt/src/main/java/org/dromara/mqtt/config/properties/MqttProperties.java index 01b656d..664c15e 100644 --- a/water-common/water-common-mqtt/src/main/java/org/dromara/mqtt/config/properties/MqttProperties.java +++ b/water-common/water-common-mqtt/src/main/java/org/dromara/mqtt/config/properties/MqttProperties.java @@ -55,7 +55,7 @@ public class MqttProperties { public static class CommandAck { private boolean enabled = true; private int maxRetryCount = 3; - private long retryIntervalMs = 5000; + private long retryIntervalMs = 10000; private long scanIntervalMs = 5000; private long pendingTtlSeconds = 86400; private long ackTtlSeconds = 86400; diff --git a/water-common/water-common-mqtt/src/test/java/org/dromara/mqtt/DeviceMqttCommandPublisherTest.java b/water-common/water-common-mqtt/src/test/java/org/dromara/mqtt/DeviceMqttCommandPublisherTest.java index e2799ce..0d73fd6 100644 --- a/water-common/water-common-mqtt/src/test/java/org/dromara/mqtt/DeviceMqttCommandPublisherTest.java +++ b/water-common/water-common-mqtt/src/test/java/org/dromara/mqtt/DeviceMqttCommandPublisherTest.java @@ -1,15 +1,39 @@ package org.dromara.mqtt; +import cn.hutool.extra.spring.SpringUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.dromara.app.domain.mqtt.DeviceCommand; import org.dromara.mqtt.config.properties.MqttProperties; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.springframework.context.support.GenericApplicationContext; + +import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.springframework.test.util.ReflectionTestUtils.invokeMethod; @Tag("dev") class DeviceMqttCommandPublisherTest { + private static GenericApplicationContext applicationContext; + + @BeforeAll + static void initializeJsonUtils() { + applicationContext = new GenericApplicationContext(); + applicationContext.registerBean(ObjectMapper.class, (Supplier) ObjectMapper::new); + applicationContext.refresh(); + new SpringUtil().setApplicationContext(applicationContext); + } + + @AfterAll + static void closeApplicationContext() { + applicationContext.close(); + } + @Test void buildCommandTopicDefaultsToDeviceNo() { MqttProperties properties = new MqttProperties(); @@ -23,4 +47,20 @@ class DeviceMqttCommandPublisherTest { assertThat(topic).isEqualTo("/d01/subscriber/cmd"); } + + @Test + void sendSchedulesFirstRetryTenSecondsAfterInitialPublish() { + DeviceMqttCommandPublisher publisher = new DeviceMqttCommandPublisher( + mock(MqttClientManager.class), + mock(MqttCommandAckService.class), + new MqttProperties() + ); + DeviceCommand command = new DeviceCommand(); + command.setDeviceNo("D01"); + command.setCommandType("switch"); + + publisher.send(command); + + assertThat(command.getNextRetryAt() - command.getLastSentAt()).isEqualTo(10000); + } }