diff --git a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/EnableMessaging.java b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/EnableMessaging.java index 772ac9f..238a4e5 100644 --- a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/EnableMessaging.java +++ b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/EnableMessaging.java @@ -9,7 +9,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -@Import(MessageAutoConfiguration.class) +@Import({MessageScanRegister.class, MessageAutoConfiguration.class}) public @interface EnableMessaging { String[] msgPaths() default {}; } diff --git a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageAutoConfiguration.java b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageAutoConfiguration.java index 732db3a..d1422d9 100644 --- a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageAutoConfiguration.java +++ b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageAutoConfiguration.java @@ -1,21 +1,13 @@ package com.tuya.connector.open.messaging.autoconfig; -import com.tuya.connector.open.messaging.MessageRegister; import com.tuya.connector.open.messaging.TuyaMessageDispatcher; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; -import org.springframework.core.annotation.AnnotationAttributes; -import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.StringUtils; -import java.util.HashSet; -import java.util.Set; - /** *

TODO * @@ -26,7 +18,7 @@ @Slf4j @Configuration @EnableConfigurationProperties(MessageProperties.class) -public class MessageAutoConfiguration implements ImportBeanDefinitionRegistrar { +public class MessageAutoConfiguration { private final MessageProperties messageProperties; @@ -61,17 +53,4 @@ public TuyaMessageDataSource tuyaMessageDataSource() { messageProperties.getSubNameSuffix() ); } - - @Override - public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { - AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableMessaging.class.getName())); - Set pkgPaths = new HashSet<>(); - if (attributes != null) { - String[] paths = attributes.getStringArray("msgPaths"); - for (String path : paths) { - pkgPaths.add(path); - } - } - MessageRegister.init(pkgPaths); - } } diff --git a/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageScanRegister.java b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageScanRegister.java new file mode 100644 index 0000000..ba65b5e --- /dev/null +++ b/tuya-messaging/src/main/java/com/tuya/connector/open/messaging/autoconfig/MessageScanRegister.java @@ -0,0 +1,24 @@ +package com.tuya.connector.open.messaging.autoconfig; + +import com.tuya.connector.open.messaging.MessageRegister; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.type.AnnotationMetadata; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class MessageScanRegister implements ImportBeanDefinitionRegistrar { + @Override + public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { + AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableMessaging.class.getName())); + Set pkgPaths = new HashSet<>(); + if (attributes != null) { + String[] paths = attributes.getStringArray("msgPaths"); + pkgPaths.addAll(Arrays.asList(paths)); + } + MessageRegister.init(pkgPaths); + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java index 3a891cd..ef67ff7 100644 --- a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/TuyaSpringBootStarterSampleApplication.java @@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @ConnectorScan(basePackages = "com.tuya.open.spring.boot.sample.ability.api") - @EnableMessaging +@EnableMessaging(msgPaths = {"com.tuya.open.spring.boot.sample.ability.messaging.msg"}) @SpringBootApplication public class TuyaSpringBootStarterSampleApplication { diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java index 5f31c13..5e19c6e 100644 --- a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/TuyaMessageListener.java @@ -2,6 +2,7 @@ import com.tuya.connector.open.messaging.event.NameUpdateMessage; import com.tuya.connector.open.messaging.event.StatusReportMessage; +import com.tuya.open.spring.boot.sample.ability.messaging.msg.DeviceNameUpdate; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @@ -26,4 +27,9 @@ public void nameUpdateMessage(NameUpdateMessage message) { log.info("NameUpdate event happened: {}", message); } + @EventListener + public void deviceNameUpdateMsg(DeviceNameUpdate message) { + log.info("deviceNameUpdateMsg event happened: {}", message); + } + } diff --git a/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java new file mode 100644 index 0000000..030de21 --- /dev/null +++ b/tuya-spring-boot-starter-sample/src/main/java/com/tuya/open/spring/boot/sample/ability/messaging/msg/DeviceNameUpdate.java @@ -0,0 +1,31 @@ +package com.tuya.open.spring.boot.sample.ability.messaging.msg; + +import com.alibaba.fastjson.JSONObject; +import com.tuya.connector.open.messaging.SourceMessage; +import com.tuya.connector.open.messaging.event.BaseTuyaMessage; +import lombok.Getter; + +@Getter +public class DeviceNameUpdate extends BaseTuyaMessage { + + private String devId; + private String produceId; + private String uid; + private String name; + private String uuid; + + @Override + public void defaultBuild(SourceMessage sourceMessage, JSONObject messageBody) { + super.defaultBuild(sourceMessage, messageBody); + this.devId = messageBody.getString("devId"); + this.produceId = messageBody.getString("produceId"); + this.uid = messageBody.getString("uid"); + this.name = messageBody.getString("name"); + this.uuid = messageBody.getString("uuid"); + } + + @Override + public String type() { + return "deviceNameUpdate"; + } +} diff --git a/tuya-spring-boot-starter-sample/src/main/resources/application.properties b/tuya-spring-boot-starter-sample/src/main/resources/application.properties index 89fdf26..ca71288 100644 --- a/tuya-spring-boot-starter-sample/src/main/resources/application.properties +++ b/tuya-spring-boot-starter-sample/src/main/resources/application.properties @@ -1,2 +1,2 @@ -connector.ak=stehdkw4navw5mwagh4d -connector.sk=fdc94f366dad4acc926a730551080665 \ No newline at end of file +connector.ak= +connector.sk= \ No newline at end of file