• 玩转亚马逊 AWS IoT(3): SpringBoot 2.7 集成 AWS IoT 服务


    1 AWSIoT 开发者文档集合

    Amazon IoT Core 开发人员指南(设备端)

    Amazon IoT Device SDK(设备端)

    AWS IoT Core API 参考(服务端)

    AWS SDK for Java 2.0(服务端)

    服务端主要是定义 IoT相关的权限、物品类型、物品等功能;设备端主要是收发MQTT消息功能

    准备工作:

    玩转亚马逊 AWS IoT(1): IoT 业务梳理

    玩转亚马逊 AWS IoT(2): IoT 控制台使用与开发操作文档

    2 核心 Maven 依赖

    ./demo-aws-iot/pom.xml
    
    • 1
            
            <dependency>
                <groupId>software.amazon.awssdkgroupId>
                <artifactId>iotartifactId>
                <version>${aws-iot-server.version}version>
            dependency>
    
            
            <dependency>
                <groupId>com.amazonawsgroupId>
                <artifactId>aws-iot-device-sdk-javaartifactId>
                <version>${aws-iot-sdk-device.version}version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    其中依赖版本信息为:

            <aws-iot-server.version>2.17.230aws-iot-server.version>
            <aws-iot-sdk-device.version>1.3.10aws-iot-sdk-device.version>
    
    • 1
    • 2

    3 IoT 配置信息

    配置类

    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/common/config/AwsIotAccountConfig.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.common.config;
    
    import lombok.Getter;
    import lombok.ToString;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @Description: 亚马逊 iot 服务账号配置类
     * @Author: junqiang.lu
     * @Date: 2022/7/25
     */
    @Getter
    @ToString
    @Configuration
    public class AwsIotAccountConfig {
    
        /**
         * 终端节点
         */
        @Value(value = "${aws.iot.clientEndpoint}")
        private String clientEndpoint;
    
        /**
         * 接入公钥
         */
        @Value(value = "${aws.iot.accessKeyId}")
        private String accessKeyId;
    
        /**
         * 接入私钥
         */
        @Value(value = "${aws.iot.secretAccessKey}")
        private String secretAccessKey;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    application.yml 配置

    ./demo-aws-iot/src/main/resources/application.yml
    
    • 1
    ## aws iot config
    aws:
      iot:
        clientEndpoint: xxxx.iot.cn-northwest-1.amazonaws.com.cn
        accessKeyId: xxxxx
        secretAccessKey: xxxxxxxx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4 核心代码

    4.1 服务端

    权限策略、证书、物品等操作工具类

    工具类包含的 IoT操作方法: 创建物品类型、创建物品、创建证书、绑定物品与证书、创建策略、绑定证书与策略、更新策略

    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/server/util/AwsIotServerClientComponent.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.server.util;
    
    import com.ljq.demo.springboot.aws.iot.common.config.AwsIotAccountConfig;
    import com.ljq.demo.springboot.aws.iot.server.vo.CertificateVo;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    import org.springframework.util.CollectionUtils;
    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.iot.IotClient;
    import software.amazon.awssdk.services.iot.model.*;
    import software.amazon.awssdk.utils.StringInputStream;
    
    import java.util.Objects;
    
    /**
     * @Description: AWS iot 服务端工具类
     * @Author: junqiang.lu
     * @Date: 2022/7/21
     */
    @Slf4j
    @Component
    public class AwsIotServerClientComponent {
    
        private AwsIotServerClientComponent(){
        }
    
        @Autowired
        private AwsIotAccountConfig iotAccountConfig;
    
        private IotClient iotClient;
    
        /**
         * 初始化 iot 客户端
         *
         * @return
         */
        @Bean(value = "iotServerClient")
        public IotClient iotClient() {
            StringBuilder cfgBuilder = new StringBuilder("[default]\n");
            cfgBuilder.append(ProfileProperty.AWS_ACCESS_KEY_ID).append(" = ").append(iotAccountConfig.getAccessKeyId())
                    .append("\n");
            cfgBuilder.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(iotClient)) {
                synchronized (AwsIotServerClientComponent.class) {
                    if (Objects.isNull(iotClient)) {
                        iotClient = IotClient.builder()
                                .credentialsProvider(awsCredentialsProviderChain)
                                .region(Region.CN_NORTHWEST_1)
                                .build();
                    }
                }
            }
            return iotClient;
        }
    
        /**
         * 创建物品类型
         *
         * @param thingType 物品类型
         */
        public boolean createThingType(String thingType) {
            Tag tag = Tag.builder().key("type").value(thingType).build();
            CreateThingTypeRequest request = CreateThingTypeRequest.builder()
                    .thingTypeName(thingType)
                    .tags(tag).build();
            CreateThingTypeResponse response = iotClient.createThingType(request);
            return response.sdkHttpResponse().isSuccessful();
        }
    
        /**
         * 创建物品
         *
         * @param thingName 物品名称
         * @param thingType 物品类型
         * @return
         */
        public boolean createThing(String thingName, String thingType) {
            CreateThingRequest request = CreateThingRequest.builder()
                    .thingName(thingName)
                    .thingTypeName(thingType).build();
            CreateThingResponse response = iotClient.createThing(request);
            return response.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;
        }
    
        /**
         * 绑定物品与证书
         *
         * @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 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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201

    4.2 设备端

    4.2.1 MQTT 消息推送类
    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/device/pubsub/IotPublisher.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.device.pubsub;
    
    import com.amazonaws.services.iot.client.AWSIotMessage;
    import com.amazonaws.services.iot.client.AWSIotQos;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * @Description: AWS iot 消息推送类
     * @Author: junqiang.lu
     * @Date: 2022/7/21
     */
    @Slf4j
    public class IotPublisher extends AWSIotMessage {
    
        public IotPublisher(String topic, AWSIotQos qos, byte[] payload) {
            super(topic, qos, payload);
        }
    
        @Override
        public void onSuccess() {
            log.info("[MQTT消息推送-Success]topic: {},data: {}",topic, getStringPayload());
            // 业务处理
        }
    
        @Override
        public void onFailure() {
            log.info("[MQTT消息推送-Error]topic: {},data: {}",topic, getStringPayload());
            // 业务处理
        }
    
        @Override
        public void onTimeout() {
            log.info("[MQTT消息推送-time out]topic: {},data: {}",topic, getStringPayload());
            // 业务处理
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    4.2.2 MQTT 消息监听类
    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/device/pubsub/IotDemoListener.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.device.pubsub;
    
    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: AWS iot 主题监听类
     * @Author: junqiang.lu
     * @Date: 2022/7/21
     */
    @Slf4j
    @Component
    public class IotDemoListener extends AWSIotTopic {
    
        private static final String TOPIC = "hello/demo/xxx";
    
        public IotDemoListener() {
            super(TOPIC, AWSIotQos.QOS1);
        }
    
        @Override
        public void onMessage(AWSIotMessage message) {
            String content = message.getStringPayload();
            log.info("message content: {}", content);
            // 业务处理
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/device/pubsub/IotConnectEventListener.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.device.pubsub;
    
    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: AWS iot 设备上下线事件监听类
     * @Author: junqiang.lu
     * @Date: 2022/7/21
     */
    @Slf4j
    @Component
    public class IotConnectEventListener extends AWSIotTopic {
    
        private static final String TOPIC = "hello/demo/connect/event";
    
        public IotConnectEventListener() {
            super(TOPIC, AWSIotQos.QOS1);
        }
    
        @Override
        public void onMessage(AWSIotMessage message) {
            String content = message.getStringPayload();
            log.info("设备上下线通知: {}", content);
            // 业务处理
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    4.2.3 设备端操作工具类
    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/device/util/AwsIotDeviceClientComponent.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.device.util;
    
    import com.amazonaws.regions.Regions;
    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 com.ljq.demo.springboot.aws.iot.common.config.AwsIotAccountConfig;
    import com.ljq.demo.springboot.aws.iot.device.pubsub.IotConnectEventListener;
    import com.ljq.demo.springboot.aws.iot.device.pubsub.IotDemoListener;
    import com.ljq.demo.springboot.aws.iot.device.pubsub.IotPublisher;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    /**
     * @Description: AWS iot 设备端 MQTT 操作工具类
     * @Author: junqiang.lu
     * @Date: 2022/7/21
     */
    @Slf4j
    @Component
    public class AwsIotDeviceClientComponent implements ApplicationRunner {
    
        public AwsIotDeviceClientComponent(){
        }
    
        public static final String CLIENT_ID = "demo-server-202207";
    
        @Autowired
        private AwsIotAccountConfig iotAccountConfig;
    
        private AWSIotMqttClient mqttClient;
    
        @Autowired
        private IotDemoListener iotDemoListener;
        @Autowired
        private IotConnectEventListener iotConnectEventListener;
    
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            mqttClient.connect();
            log.info("aws mqtt server 客户端连接成功");
            // 主题订阅
            mqttClient.subscribe(iotDemoListener, true);
            mqttClient.subscribe(iotConnectEventListener, 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(), CLIENT_ID, credentialsProvider,
                    Regions.CN_NORTHWEST_1.getName());
            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));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    5 测试

    接口示例:

    创建策略

    POST
    http://127.0.0.1:9000/api/iot/server/policy/create
    
    • 1
    • 2

    请求参数:

    {
        "policyName": "testPolicy",
        "policyContent": "{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Action\": \"iot:Connect\", \"Resource\": \"arn:aws-cn:iot:cn-northwest-1:xxxxx:client/${iot:Connection.Thing.ThingName}\" }, { \"Effect\": \"Allow\", \"Action\": \"iot:Publish\", \"Resource\": \"arn:aws-cn:iot:cn-northwest-1:xxxxx:topic/hello/demo/xxx\" }, { \"Effect\": \"Allow\", \"Action\": \"iot:Subscribe\", \"Resource\": \"arn:aws-cn:iot:cn-northwest-1:xxxxx:topicfilter/hello/demo/client/${iot:Connection.Thing.ThingName}\" }, { \"Effect\": \"Allow\", \"Action\": \"iot:Receive\", \"Resource\": \"*\" } ] }"
    }
    
    • 1
    • 2
    • 3
    • 4

    其他接口参考源码:

    服务端:

    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/server/controller/IotServerDemoController.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.server.controller;
    
    import com.ljq.demo.springboot.aws.iot.server.param.*;
    import com.ljq.demo.springboot.aws.iot.server.util.AwsIotServerClientComponent;
    import com.ljq.demo.springboot.aws.iot.server.vo.CertificateVo;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    /**
     * @Description: iot 服务端示例控制层
     * @Author: junqiang.lu
     * @Date: 2022/7/25
     */
    @Slf4j
    @RestController
    @RequestMapping(value = "/api/iot/server")
    public class IotServerDemoController {
    
        @Autowired
        private AwsIotServerClientComponent iotClientComponent;
    
        /**
         * 创建策略
         *
         * @param createPolicyParam
         * @return
         */
        @PostMapping(value = "/policy/create", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> createPolicy(@RequestBody IotServerCreatePolicyParam createPolicyParam) {
            boolean flag = iotClientComponent.createPolicy(createPolicyParam.getPolicyName(),
                    createPolicyParam.getPolicyContent());
            return ResponseEntity.ok(flag);
        }
    
        /**
         * 更新策略
         *
         * @param updatePolicyParam
         * @return
         */
        @PutMapping(value = "/policy/update", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> updatePolicy(@RequestBody IotServerUpdatePolicyParam updatePolicyParam) {
            boolean flag = iotClientComponent.updatePolicy(updatePolicyParam.getPolicyName(),
                    updatePolicyParam.getPolicyContent());
            return ResponseEntity.ok(flag);
        }
    
        /**
         * 创建物品类型
         *
         * @param createThingTypeParam
         * @return
         */
        @PostMapping(value = "/thingType/create", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> createThingType(@RequestBody IotServerCreateThingTypeParam createThingTypeParam) {
            boolean flag = iotClientComponent.createThingType(createThingTypeParam.getThingTypeName());
            return ResponseEntity.ok(flag);
        }
    
        /**
         * 创建物品
         *
         * @param createThingParam
         * @return
         */
        @PostMapping(value = "/thing/create", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> createThing(@RequestBody IotServerCreateThingParam createThingParam) {
            boolean flag = iotClientComponent.createThing(createThingParam.getThingName(),
                    createThingParam.getThingTypeName());
            return ResponseEntity.ok(flag);
        }
    
        /**
         * 创建证书
         *
         * @param createCertParam
         * @return
         */
        @PostMapping(value = "/cert/create", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> createCert(@RequestBody IotServerCreateCertParam createCertParam) {
            CertificateVo cert = iotClientComponent.createCert();
            return ResponseEntity.ok(cert);
        }
    
        /**
         * 绑定物品与证书
         *
         * @param bindThingAndCertParam
         * @return
         */
        @PostMapping(value = "/thing/cert/bind", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> bindThingAndCert(@RequestBody IotServerBindThingAndCertParam bindThingAndCertParam) {
            boolean flag = iotClientComponent.bindThingAndCert(bindThingAndCertParam.getCertArn(),
                    bindThingAndCertParam.getThingName());
            return ResponseEntity.ok(flag);
        }
    
        /**
         * 绑定证书与策略
         *
         * @param bindCertAndPolicyParam
         * @return
         */
        @PostMapping(value = "/cert/policy/bind", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> bindCertAndPolicy(@RequestBody IotServerBindCertAndPolicyParam bindCertAndPolicyParam) {
            boolean flag = iotClientComponent.bindCertAndPolicy(bindCertAndPolicyParam.getCertArn(),
                    bindCertAndPolicyParam.getPolicyName());
            return ResponseEntity.ok(flag);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    设备端:

    ./demo-aws-iot/src/main/java/com/ljq/demo/springboot/aws/iot/device/controller/IotDeviceDemoController.java
    
    • 1
    package com.ljq.demo.springboot.aws.iot.device.controller;
    
    import com.amazonaws.services.iot.client.AWSIotException;
    import com.ljq.demo.springboot.aws.iot.device.param.IotDevicePublishParam;
    import com.ljq.demo.springboot.aws.iot.device.util.AwsIotDeviceClientComponent;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.nio.charset.StandardCharsets;
    
    /**
     * @Description: iot 设备示例控制层
     * @Author: junqiang.lu
     * @Date: 2022/7/22
     */
    @RestController
    @RequestMapping(value = "/api/iot/device")
    public class IotDeviceDemoController {
    
        @Autowired
        private AwsIotDeviceClientComponent deviceClientComponent;
    
        @PostMapping(value = "/publish", produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Object> publish(@RequestBody IotDevicePublishParam publishParam) throws AWSIotException {
            String topic = "hello/demo/client/" + publishParam.getClientId();
            deviceClientComponent.pushMessage(topic, publishParam.getMessage().getBytes(StandardCharsets.UTF_8));
            return ResponseEntity.ok(System.currentTimeMillis());
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    6 推荐参考文档

    Amazon IoT Core 开发人员指南(设备端)

    Amazon IoT Device SDK(设备端)

    AWS IoT Core API 参考(服务端)

    AWS SDK for Java 2.0(服务端)

    Amazon IoT 规则操作

    IoT Events

    Amazon IoT SQL 参考

    MQTT 主题-主题筛选条件

    生命周期事件

    Amazon IoT Core 策略操作

    Amazon IoT Core 策略示例

    基本任务策略示例

    7 Github 源码

    Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo

    个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.
    404Code

  • 相关阅读:
    Python Packaging & Distribution
    Google Earth Engine ——利用公开的河流数据计算河流的有效宽度
    MySQL备份与恢复
    如何用Python和NLTK有效地总结文本
    Seata入门开发
    kubeadm 部署的 k8s 增加 ip 并重新生成证书
    Linux系统编程系列之进程间通信-IPC对象
    springboot-admin整合及使用
    python--Matplotlib数据可视化基础
    SaaSBase:什么是易客管家SCRM?
  • 原文地址:https://blog.csdn.net/Mrqiang9001/article/details/126073487