Skip to content

Commit

Permalink
feat: msg support custom
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqiufeng committed Sep 13, 2024
1 parent c2b4c04 commit c38f31d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(MessageAutoConfiguration.class)
@Import({MessageScanRegister.class, MessageAutoConfiguration.class})
public @interface EnableMessaging {
String[] msgPaths() default {};
}
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p> TODO
*
Expand All @@ -26,7 +18,7 @@
@Slf4j
@Configuration
@EnableConfigurationProperties(MessageProperties.class)
public class MessageAutoConfiguration implements ImportBeanDefinitionRegistrar {
public class MessageAutoConfiguration {

private final MessageProperties messageProperties;

Expand Down Expand Up @@ -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<String> pkgPaths = new HashSet<>();
if (attributes != null) {
String[] paths = attributes.getStringArray("msgPaths");
for (String path : paths) {
pkgPaths.add(path);
}
}
MessageRegister.init(pkgPaths);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> pkgPaths = new HashSet<>();
if (attributes != null) {
String[] paths = attributes.getStringArray("msgPaths");
pkgPaths.addAll(Arrays.asList(paths));
}
MessageRegister.init(pkgPaths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
connector.ak=stehdkw4navw5mwagh4d
connector.sk=fdc94f366dad4acc926a730551080665
connector.ak=
connector.sk=

0 comments on commit c38f31d

Please sign in to comment.