/\*\*
* 删除物品
*
* @param thingName 物品名称
* @return
*/
public boolean deleteThing(String thingName) {
DeleteThingRequest request = DeleteThingRequest.builder()
.thingName(thingName).build();
DeleteThingResponse deleteThingResponse = iotClient.deleteThing(request);
return deleteThingResponse.sdkHttpResponse().isSuccessful();
}
/\*\*
* 创建证书
*
* @return
*/
public CertificateVo createCert() {
CreateKeysAndCertificateRequest request = CreateKeysAndCertificateRequest.builder().setAsActive(true).build();
CreateKeysAndCertificateResponse response = iotClient.createKeysAndCertificate(request);
if (response.sdkHttpResponse().isSuccessful()) {
CertificateVo certVo = new CertificateVo();
certVo.setCertificateArn(response.certificateArn());
certVo.setCertificateId(response.certificateId());
certVo.setCertificatePem(response.certificatePem());
certVo.setPublicKey(response.keyPair().publicKey());
certVo.setPrivateKey(response.keyPair().privateKey());
return certVo;
}
return null;
}
/**
* 删除证书
*
* @return
*/
public boolean deleteCert(String certificateId) {
DeleteCaCertificateRequest request = DeleteCaCertificateRequest.builder().certificateId(certificateId).build();
DeleteCaCertificateResponse deleteCaCertificateResponse = iotClient.deleteCACertificate(request);
return deleteCaCertificateResponse.sdkHttpResponse().isSuccessful();
}
/\*\*
* 绑定物品与证书
*
* @param certArn 证书资源唯一标识
* @param thingId 物品 ID
* @return
*/
public boolean bindThingAndCert(String certArn, String thingId) {
AttachThingPrincipalRequest request = AttachThingPrincipalRequest.builder()
.thingName(thingId)
.principal(certArn).build();
AttachThingPrincipalResponse response = iotClient.attachThingPrincipal(request);
return response.sdkHttpResponse().isSuccessful();
}
/\*\*
* 创建策略
*
* @param policyName 策略名称
* @param policyContent 策略内容(json 格式)
* @return
*/
public boolean createPolicy(String policyName, String policyContent) {
CreatePolicyRequest request = CreatePolicyRequest.builder()
.policyName(policyName)
.policyDocument(policyContent).build();
CreatePolicyResponse response = iotClient.createPolicy(request);
return response.sdkHttpResponse().isSuccessful();
}
/\*\*
* 创建策略
*
* @param policyName 策略名称
* @return
*/
public boolean deletePolicy(String policyName) {
DeletePolicyRequest request = DeletePolicyRequest.builder()
.policyName(policyName).build();
DeletePolicyResponse deletePolicyResponse = iotClient.deletePolicy(request);
return deletePolicyResponse.sdkHttpResponse().isSuccessful();
}
/\*\*
* 绑定证书与策略
*
* @param certArn
* @param policyName
* @return
*/
public boolean bindCertAndPolicy(String certArn, String policyName) {
AttachPolicyRequest request = AttachPolicyRequest.builder()
.policyName(policyName)
.target(certArn)
.build();
AttachPolicyResponse response = iotClient.attachPolicy(request);
return response.sdkHttpResponse().isSuccessful();
}
/\*\*
* 更新策略
*
* @param policyName
* @param policyContent
* @return
*/
public boolean updatePolicy(String policyName, String policyContent) {
// 查询策略的所有版本
ListPolicyVersionsRequest listPolicyVersionsRequest = ListPolicyVersionsRequest.builder()
.policyName(policyName).build();
ListPolicyVersionsResponse listPolicyVersionsResponse = iotClient.listPolicyVersions(listPolicyVersionsRequest);
if (!listPolicyVersionsResponse.sdkHttpResponse().isSuccessful()) {
log.warn(“删除策略失败,查询策略列表出错”);
return false;
}
if (CollectionUtils.isEmpty(listPolicyVersionsResponse.policyVersions())) {
log.warn(“删除策略失败,策略列表为空”);
return false;
}
// 删除非活跃版本
listPolicyVersionsResponse.policyVersions().forEach(version -> {
if (!version.isDefaultVersion()) {
DeletePolicyVersionRequest deletePolicyVersionRequest = DeletePolicyVersionRequest.builder()
.policyName(policyName)
.policyVersionId(version.versionId()).build();
iotClient.deletePolicyVersion(deletePolicyVersionRequest);
}
});
// 创建策略版本并设置为活跃状态
CreatePolicyVersionRequest request = CreatePolicyVersionRequest.builder()
.policyName(policyName)
.policyDocument(policyContent)
.setAsDefault(true).build();
CreatePolicyVersionResponse response = iotClient.createPolicyVersion(request);
return response.sdkHttpResponse().isSuccessful();
}
/\*\*
* 获取策略
*
* @param policyName 策略名称
* @return
*/
public GetPolicyResponse getPolicy(String policyName) {
GetPolicyRequest request = GetPolicyRequest.builder().policyName(policyName).build();
GetPolicyResponse getPolicyResponse = iotClient.getPolicy(request);
return getPolicyResponse;
}
}
import com.aiper.app.component.iot.listener.SwimmingCleanDataListener;
import com.aiper.app.component.iot.listener.SwimmingInfoListener;
import com.aiper.app.component.iot.listener.SwimmingStatusListener;
import com.amazonaws.services.iot.client.AWSIotException;
import com.amazonaws.services.iot.client.AWSIotMqttClient;
import com.amazonaws.services.iot.client.AWSIotQos;
import com.amazonaws.services.iot.client.auth.Credentials;
import com.amazonaws.services.iot.client.auth.StaticCredentialsProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @Description: AWS iot 设备端 MQTT 操作工具类
*/
@Slf4j
@Component
public class AwsIotDeviceClientComponent implements ApplicationRunner {
public AwsIotDeviceClientComponent(){
}
private AWSIotMqttClient mqttClient;
@Resource
private AwsIotAccountConfig iotAccountConfig;
@Resource
private Listener listener;
@Override
public void run(ApplicationArguments args) throws Exception {
mqttClient.connect();
log.info(“aws mqtt server 客户端连接成功”);
// 主题订阅
mqttClient.subscribe(listener, true);
}
@Bean(name = “awsIotMqttClient”)
public AWSIotMqttClient awsIotMqttClient() {
log.info(“创建 awsIotMqttClient bean”);
Credentials credentials = new Credentials(iotAccountConfig.getAccessKeyId(),
iotAccountConfig.getSecretAccessKey());
StaticCredentialsProvider credentialsProvider = new StaticCredentialsProvider(credentials);
mqttClient = new AWSIotMqttClient(iotAccountConfig.getClientEndpoint(),
“javaClient_”+ System.currentTimeMillis()
, credentialsProvider, iotAccountConfig.getRegions());
return mqttClient;
}
/**
* 消息推送
* @param topic
* @param data
* @throws AWSIotException
*/
public void pushMessage(String topic, byte[] data) throws AWSIotException {
log.info(“server topic={},>>> {}”, topic, new String(data));
mqttClient.publish(new IotPublisher(topic, AWSIotQos.QOS1, data));
}
}
import com.amazonaws.services.iot.client.AWSIotMessage;
import com.amazonaws.services.iot.client.AWSIotQos;
import com.amazonaws.services.iot.client.AWSIotTopic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @Description: 信息监听
*/
@Slf4j
@Component
public class Listener extends AWSIotTopic {
private final static String TOPIC = “topic/test”;
public SwimmingInfoListener() {
super(TOPIC, AWSIotQos.QOS1);
}
@Override
public void onMessage(AWSIotMessage message) {
//业务代码
}
## Cognito相关服务端代码
import com.aiper.app.component.iot.AwsIotAccountConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileProperty;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient;
import software.amazon.awssdk.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityRequest;
import software.amazon.awssdk.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityResponse;
import software.amazon.awssdk.utils.StringInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
@Component
public class AwsCognitoIdentityClientComponent {
public AwsCognitoIdentityClientComponent() {
}
@Autowired
private AwsIotAccountConfig iotAccountConfig;
@Autowired
private AwsCognitoIdentityPoolConfig cognitoIdentityPoolConfig;
private CognitoIdentityClient cognitoIdentityClient;
/\*\*
*
* 初始化 cognitoIdentity 客户端
* @return
*/
@Bean(value = “cognitoIdentityClient”)
public CognitoIdentityClient cognitoIdentityClient() {
StringBuilder cfgBuilder = new StringBuilder(“[default]\n”);
cfgBuilder.append(ProfileProperty.AWS_ACCESS_KEY_ID).append(" = “).append(iotAccountConfig.getAccessKeyId())
.append(”\n").append(ProfileProperty.AWS_SECRET_ACCESS_KEY).append(" = “).append(iotAccountConfig.getSecretAccessKey()).append(”\n");
ProfileFile profileFile = ProfileFile.builder()
.content(new StringInputStream(cfgBuilder.toString()))
.type(ProfileFile.Type.CONFIGURATION).build();
AwsCredentialsProviderChain awsCredentialsProviderChain = AwsCredentialsProviderChain.of(
ProfileCredentialsProvider.builder().profileFile(profileFile).build());
if (Objects.isNull(cognitoIdentityClient)) {
synchronized (AwsCognitoIdentityClientComponent.class) {
if (Objects.isNull(cognitoIdentityClient)) {
cognitoIdentityClient = CognitoIdentityClient.builder()
.credentialsProvider(awsCredentialsProviderChain)
.region(Region.of(iotAccountConfig.getRegions()))
.build();
}
}
}
return cognitoIdentityClient;
}
public GetOpenIdTokenForDeveloperIdentityResponse getOpenIdTokenForDeveloperIdentity(String serialNumber) {
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!
[外链图片转存中…(img-N6zTrB1o-1715540478474)]
[外链图片转存中…(img-2eiz2BJq-1715540478475)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新!!