feat: 新功能开发,监测历史记录。微信和物联网平台,模块搭建。
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.intc.iot.config;
|
||||
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 阿里云生活物联网平台(飞燕平台)配置
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "aliyun.living-iot", name = "access-key-id")
|
||||
public class AliyunIotConfiguration {
|
||||
|
||||
private final AliyunIotProperties iotProperties;
|
||||
|
||||
/**
|
||||
* 创建阿里云飞燕平台客户端
|
||||
*/
|
||||
@Bean
|
||||
public IAcsClient livingIotClient() {
|
||||
DefaultProfile profile = DefaultProfile.getProfile(
|
||||
iotProperties.getRegionId(),
|
||||
iotProperties.getAccessKeyId(),
|
||||
iotProperties.getAccessKeySecret()
|
||||
);
|
||||
|
||||
IAcsClient client = new DefaultAcsClient(profile);
|
||||
log.info("阿里云生活物联网平台客户端初始化成功,RegionId: {}", iotProperties.getRegionId());
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.intc.iot.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 阿里云生活物联网平台(飞燕平台)配置属性
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "aliyun.living-iot")
|
||||
public class AliyunIotProperties {
|
||||
|
||||
/**
|
||||
* 阿里云AccessKey ID
|
||||
*/
|
||||
private String accessKeyId;
|
||||
|
||||
/**
|
||||
* 阿里云AccessKey Secret
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
|
||||
/**
|
||||
* 地域节点(如:cn-shanghai)
|
||||
*/
|
||||
private String regionId;
|
||||
|
||||
/**
|
||||
* 飞燕平台项目ID(Project ID)
|
||||
*/
|
||||
private String projectId;
|
||||
|
||||
/**
|
||||
* App Key
|
||||
*/
|
||||
private String appKey;
|
||||
|
||||
/**
|
||||
* App Secret
|
||||
*/
|
||||
private String appSecret;
|
||||
|
||||
/**
|
||||
* 品类Key
|
||||
*/
|
||||
private String categoryKey;
|
||||
|
||||
/**
|
||||
* MQTT 配置
|
||||
*/
|
||||
private MqttConfig mqtt = new MqttConfig();
|
||||
|
||||
@Data
|
||||
public static class MqttConfig {
|
||||
/**
|
||||
* MQTT Broker 地址
|
||||
*/
|
||||
private String brokerUrl;
|
||||
|
||||
/**
|
||||
* 客户端ID
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 连接超时时间(秒)
|
||||
*/
|
||||
private Integer connectionTimeout = 30;
|
||||
|
||||
/**
|
||||
* 保活时间(秒)
|
||||
*/
|
||||
private Integer keepAliveInterval = 60;
|
||||
|
||||
/**
|
||||
* 自动重连
|
||||
*/
|
||||
private Boolean autoReconnect = true;
|
||||
|
||||
/**
|
||||
* 清除会话
|
||||
*/
|
||||
private Boolean cleanSession = true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.intc.iot.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* MQTT 客户端配置
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(prefix = "aliyun.living-iot.mqtt", name = "broker-url")
|
||||
public class MqttConfiguration {
|
||||
|
||||
private final AliyunIotProperties iotProperties;
|
||||
|
||||
/**
|
||||
* 创建 MQTT 客户端
|
||||
*/
|
||||
@Bean
|
||||
public MqttClient mqttClient() throws Exception {
|
||||
AliyunIotProperties.MqttConfig mqtt = iotProperties.getMqtt();
|
||||
|
||||
MemoryPersistence persistence = new MemoryPersistence();
|
||||
MqttClient client = new MqttClient(mqtt.getBrokerUrl(), mqtt.getClientId(), persistence);
|
||||
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setUserName(mqtt.getUsername());
|
||||
options.setPassword(mqtt.getPassword().toCharArray());
|
||||
options.setConnectionTimeout(mqtt.getConnectionTimeout());
|
||||
options.setKeepAliveInterval(mqtt.getKeepAliveInterval());
|
||||
options.setAutomaticReconnect(mqtt.getAutoReconnect());
|
||||
options.setCleanSession(mqtt.getCleanSession());
|
||||
|
||||
client.connect(options);
|
||||
log.info("MQTT 客户端连接成功,Broker: {}", mqtt.getBrokerUrl());
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user