Browse Source

iot-production模块和测试用例

home 2 years ago
parent
commit
ebe9976c71

+ 19 - 0
hx-service-api/iot-production-api/pom.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hx-service-api</artifactId>
+        <groupId>com.fjhx</groupId>
+        <version>3.2.0</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>iot-production-api</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+</project>

+ 1 - 0
hx-service-api/pom.xml

@@ -25,6 +25,7 @@
         <module>iot-management-api</module>
         <module>storage-api</module>
         <module>service-file-api</module>
+        <module>iot-production-api</module>
     </modules>
 
     <dependencyManagement>

+ 2 - 1
hx-service/iot-management/src/main/java/com/fjhx/working/service/impl/WorkingRouteServiceImpl.java

@@ -2,6 +2,7 @@ package com.fjhx.working.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -22,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -179,5 +181,4 @@ public class WorkingRouteServiceImpl extends ServiceImpl<WorkingRouteMapper, Wor
         productExService.updateBatchById(productExList);
     }
 
-
 }

+ 47 - 0
hx-service/iot-production/pom.xml

@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hx-service</artifactId>
+        <groupId>com.fjhx</groupId>
+        <version>3.2.0</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>iot-production</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>com.fjhx</groupId>
+            <artifactId>service-file-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.huaweicloud.sdk</groupId>
+            <artifactId>huaweicloud-sdk-core</artifactId>
+            <version>3.0.92</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.huaweicloud.sdk</groupId>
+            <artifactId>huaweicloud-sdk-iotda</artifactId>
+            <version>3.0.92</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.huaweicloud.sdk</groupId>
+            <artifactId>huaweicloud-sdk-all</artifactId>
+            <version>3.0.92</version>
+        </dependency>
+
+
+    </dependencies>
+
+</project>

+ 40 - 0
hx-service/iot-production/src/main/java/com/fjhx/IoTDA/IoTDAConfig.java

@@ -0,0 +1,40 @@
+package com.fjhx.IoTDA;
+
+import com.huaweicloud.sdk.core.auth.ICredential;
+import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
+import com.huaweicloud.sdk.iotda.v5.auth.IoTDACredentials;
+import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.context.annotation.Bean;
+
+@Configurable
+public class IoTDAConfig {
+
+    // REGION_ID:如果是上海一,请填写"cn-east-3";如果是北京四,请填写"cn-north-4";如果是华南广州,请填写"cn-south-4"
+    private static final String REGION_ID = "cn-north-4";
+    // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
+    private static final String ENDPOINT = "a1625d5cc8.iotda.cn-north-4.myhuaweicloud.com";
+
+    private static final String ak = "VPOCS8WGONHWOBOZNVVJ";
+    private static final String sk = "4mvyTAn75I0hvyAm8rKxuXi6TSyGDG5yblW43ci1";
+    private static final String projectId = "0f130b202d00f4e52fc0c01deb21f36a";
+
+    @Bean
+    public IoTDAClient client() {
+        // 创建认证
+        ICredential auth = new IoTDACredentials()
+                .withAk(ak)
+                .withSk(sk)
+                .withProjectId(projectId);
+
+        // 创建IoTDAClient实例并初始化
+        return IoTDAClient.newBuilder()
+                .withCredential(auth)
+                // 基础版:请选择IoTDARegion中的Region对象
+                .withRegion(IoTDARegion.CN_NORTH_4)
+                // 标准版/企业版:需自行创建Region对象
+                // .withRegion(new Region(REGION_ID, ENDPOINT))
+                .build();
+    }
+
+}

+ 175 - 0
hx-service/iot-production/src/main/java/com/fjhx/IoTDA/IoTDAUtil.java

@@ -0,0 +1,175 @@
+package com.fjhx.IoTDA;
+
+import cn.hutool.extra.spring.SpringUtil;
+import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
+import com.huaweicloud.sdk.iotda.v5.model.*;
+import lombok.Data;
+import org.springblade.core.log.exception.ServiceException;
+
+import java.util.Collections;
+
+public class IoTDAUtil {
+
+    private static final IoTDAClient client = SpringUtil.getBean(IoTDAClient.class);
+
+    /**
+     * 创建资源空间
+     *
+     * @param appName 资源空间名称
+     * @return 资源空间id
+     */
+    public static String addApplication(String appName) {
+        AddApplicationRequest addApplicationRequest = new AddApplicationRequest();
+        AddApplication addApplication = new AddApplication();
+        addApplication.setAppName(appName);
+        addApplicationRequest.setBody(addApplication);
+        AddApplicationResponse addApplicationResponse = client.addApplication(addApplicationRequest);
+        return addApplicationResponse.getAppId();
+    }
+
+    /**
+     * 删除资源空间
+     *
+     * @param appId 资源空间Id
+     */
+    public static void deleteApplication(String appId) {
+        try {
+            DeleteApplicationRequest deleteApplicationRequest = new DeleteApplicationRequest();
+            deleteApplicationRequest.setAppId(appId);
+            client.deleteApplication(deleteApplicationRequest);
+        } catch (Exception e) {
+            throw new ServiceException("资源空间删除失败");
+        }
+    }
+
+    /**
+     * 创建产品
+     *
+     * @param appId            资源空间Id
+     * @param name             产品名称
+     * @param deviceType       设备类型
+     * @param manufacturerName 厂商名称
+     * @return 产品id
+     */
+    public static String createProduct(String appId, String name, String deviceType, String manufacturerName) {
+        CreateProductRequest createProductRequest = new CreateProductRequest();
+        AddProduct addProduct = new AddProduct();
+        // 资源空间
+        addProduct.setAppId(appId);
+        // 产品名称
+        addProduct.setName(name);
+        addProduct.setDeviceType(deviceType);
+        addProduct.setManufacturerName(manufacturerName);
+
+        addProduct.setProtocolType("MQTT");
+        addProduct.setDataFormat("json");
+
+        // 添加产品属性
+        ServiceCapability serviceCapability = new ServiceCapability();
+        serviceCapability.setServiceId("Data");
+        serviceCapability.setServiceType("data");
+        // 添加命名空间
+        ServiceProperty serviceProperty = new ServiceProperty();
+        serviceProperty.setPropertyName("DeviceData");
+        serviceProperty.setDataType("string");
+        serviceProperty.setRequired(false);
+        serviceProperty.setMaxLength(2000000);
+        serviceProperty.setStep(0.0D);
+        serviceProperty.setMethod("RW");
+        serviceCapability.setProperties(Collections.singletonList(serviceProperty));
+        addProduct.setServiceCapabilities(Collections.singletonList(serviceCapability));
+
+        createProductRequest.setBody(addProduct);
+        CreateProductResponse product = client.createProduct(createProductRequest);
+        return product.getProductId();
+    }
+
+    /**
+     * 修改产品
+     *
+     * @param productId        产品id
+     * @param appId            资源空间Id
+     * @param name             产品名称
+     * @param deviceType       设备类型
+     * @param manufacturerName 厂商名称
+     */
+    public static void updateProduct(String productId, String appId, String name, String deviceType, String manufacturerName) {
+        UpdateProductRequest updateProductRequest = new UpdateProductRequest();
+        updateProductRequest.setProductId(productId);
+        UpdateProduct updateProduct = new UpdateProduct();
+        updateProduct.setAppId(appId);
+        updateProduct.setName(name);
+        updateProduct.setDeviceType(deviceType);
+        updateProduct.setManufacturerName(manufacturerName);
+        updateProductRequest.setBody(updateProduct);
+        client.updateProduct(updateProductRequest);
+    }
+
+    /**
+     * 删除产品
+     *
+     * @param appId     资源空间Id
+     * @param productId 产品id
+     */
+    public static void deleteProduct(String appId, String productId) {
+        try {
+            DeleteProductRequest deleteProductRequest = new DeleteProductRequest();
+            deleteProductRequest.setAppId("fa74090e9e534511b65545a456f4bf2f");
+            deleteProductRequest.setProductId("62d793e26b9813541d52492b");
+            client.deleteProduct(deleteProductRequest);
+        } catch (Exception e) {
+            throw new ServiceException("产品删除失败");
+        }
+    }
+
+    /**
+     * 添加设备
+     *
+     * @param nodeId     设备标识
+     * @param deviceName 设备名称
+     * @param appId      资源空间Id
+     * @param productId  产品id
+     * @return 设备secret
+     */
+    public static DeviceKey addDevice(String nodeId, String deviceName, String appId, String productId) {
+        AddDeviceRequest addDeviceRequest = new AddDeviceRequest();
+        AddDevice addDevice = new AddDevice();
+        addDevice.setNodeId(nodeId);
+        addDevice.setDeviceName(deviceName);
+        addDevice.setAppId(appId);
+        addDevice.setProductId(productId);
+
+        addDeviceRequest.setBody(addDevice);
+        AddDeviceResponse addDeviceResponse = client.addDevice(addDeviceRequest);
+
+        DeviceKey deviceKey = new DeviceKey();
+        deviceKey.setDeviceId(addDeviceResponse.getDeviceId());
+        deviceKey.setSecret(addDeviceResponse.getAuthInfo().getSecret());
+
+        return deviceKey;
+    }
+
+    @Data
+    public static class DeviceKey {
+        // 设备id
+        private String deviceId;
+        // 设备密匙
+        private String secret;
+    }
+
+    /**
+     * 删除设备
+     *
+     * @param deviceId 设备Id
+     */
+    public static void deleteDevice(String deviceId) {
+        try {
+            DeleteDeviceRequest deleteDeviceRequest = new DeleteDeviceRequest();
+            deleteDeviceRequest.setDeviceId(deviceId);
+            client.deleteDevice(deleteDeviceRequest);
+        } catch (Exception e) {
+            throw new ServiceException("产品删除失败");
+        }
+    }
+
+}

+ 20 - 0
hx-service/iot-production/src/main/java/com/fjhx/IotProductionApplication.java

@@ -0,0 +1,20 @@
+package com.fjhx;
+
+import org.springblade.core.cloud.feign.EnableBladeFeign;
+import org.springblade.core.launch.BladeApplication;
+import org.springframework.cloud.client.SpringCloudApplication;
+
+/**
+ * 针筒物联网管理模块启动器
+ */
+@EnableBladeFeign
+@SpringCloudApplication
+public class IotProductionApplication {
+
+    private static final String APP_NAME = "iot-production";
+
+    public static void main(String[] args) {
+        BladeApplication.run(APP_NAME, IotProductionApplication.class, args);
+    }
+
+}

+ 53 - 0
hx-service/iot-production/src/main/java/com/fjhx/test/ListDevicesSolution.java

@@ -0,0 +1,53 @@
+package com.fjhx.test;
+
+
+import com.fjhx.IoTDA.IoTDAConfig;
+import com.fjhx.IoTDA.IoTDAUtil;
+import com.huaweicloud.sdk.core.exception.ConnectionException;
+import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
+import com.huaweicloud.sdk.core.exception.ServiceResponseException;
+import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
+import com.huaweicloud.sdk.iotda.v5.model.*;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+import java.util.Collections;
+
+public class ListDevicesSolution {
+
+    private static final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(IoTDAConfig.class);
+    private static final IoTDAClient client = applicationContext.getBean(IoTDAClient.class);
+
+    public static void main(String[] args) {
+
+        try {
+
+            ShowApplicationsRequest showApplicationsRequest = new ShowApplicationsRequest();
+            System.out.println(client.showApplications(showApplicationsRequest));
+            System.out.println("========================================================");
+
+            ListProductsRequest listProductsRequest = new ListProductsRequest();
+            listProductsRequest.setAppId("fa74090e9e534511b65545a456f4bf2f");
+            System.out.println(client.listProducts(listProductsRequest));
+            System.out.println("========================================================");
+
+
+            ListDevicesRequest listDevicesRequest = new ListDevicesRequest();
+            listDevicesRequest.setProductId("62d4ff516b9813541d51f893");
+            ListDevicesResponse listDevicesResponse = client.listDevices(listDevicesRequest);
+            System.out.println(listDevicesResponse);
+            System.out.println("========================================================");
+
+
+
+
+
+        } catch (ConnectionException | RequestTimeoutException e) {
+            e.printStackTrace();
+        } catch (ServiceResponseException e) {
+            e.printStackTrace();
+            System.out.println(e.getHttpStatusCode());
+            System.out.println(e.getErrorCode());
+            System.out.println(e.getErrorMsg());
+        }
+    }
+}

+ 53 - 0
hx-service/iot-production/src/main/java/com/fjhx/test/ZipTest.java

@@ -0,0 +1,53 @@
+package com.fjhx.test;
+
+import cn.hutool.http.HttpUtil;
+import lombok.SneakyThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+
+public class ZipTest {
+
+    @SneakyThrows
+    public static void main(String[] args) {
+        byte[] bytes = HttpUtil.downloadBytes("http://36.134.91.96:10001/file/test.zip");
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        byteArrayOutputStream.write(bytes);
+
+
+        ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(bytes));
+        ZipEntry zipEntry;
+
+        ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("E:\\test.zip"));
+
+        while ((zipEntry = in.getNextEntry()) != null) {
+            String name = zipEntry.getName();
+
+            zipOutputStream.putNextEntry(new ZipEntry(name));
+
+            //读取该文件的字节内容
+            byte[] buff = new byte[1024];
+            int len;
+            while ((len = in.read(buff)) != -1) {
+                zipOutputStream.write(buff, 0, len);
+            }
+
+        }
+
+        String testCont = "测试内容";
+
+        zipOutputStream.putNextEntry(new ZipEntry("测试zip\\config"));
+        zipOutputStream.write(testCont.getBytes(StandardCharsets.UTF_8));
+
+        zipOutputStream.closeEntry();
+        zipOutputStream.close();
+
+
+    }
+
+}

+ 13 - 0
hx-service/iot-production/src/main/resources/application-dev.yml

@@ -0,0 +1,13 @@
+# 服务器端口
+server:
+  port: 8201
+logging:
+  level:
+    org.springframework.data.mongodb.core: DEBUG
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.iot_production.dev.url}
+    username: ${blade.datasource.iot_production.dev.username}
+    password: ${blade.datasource.iot_production.dev.password}

+ 12 - 0
hx-service/iot-production/src/main/resources/application-prod.yml

@@ -0,0 +1,12 @@
+# 服务器端口
+server:
+  port: 8201
+
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.iot_production.prod.url}
+    username: ${blade.datasource.iot_production.prod.username}
+    password: ${blade.datasource.iot_production.prod.password}
+

+ 11 - 0
hx-service/iot-production/src/main/resources/application-test.yml

@@ -0,0 +1,11 @@
+# 服务器端口
+server:
+  port: 8201
+
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.iot_production.test.url}
+    username: ${blade.datasource.iot_production.test.username}
+    password: ${blade.datasource.iot_production.test.password}

+ 1 - 0
hx-service/pom.xml

@@ -24,6 +24,7 @@
         <module>iot-management</module>
         <module>storage</module>
         <module>service-file</module>
+        <module>iot-production</module>
     </modules>
 
     <dependencyManagement>

+ 11 - 5
hx-service/storage/src/main/java/com/fjhx/attachment/controller/StockAttachmentController.java

@@ -25,37 +25,43 @@ public class StockAttachmentController {
     @Autowired
     private StockAttachmentService stockAttachmentService;
 
+    @GetMapping("/helloWord")
+    public R helloWord() {
+        return R.success("hello word");
+    }
+
     @PostMapping("/page")
-    public R page(@RequestBody Map<String, String> condition){
+    public R page(@RequestBody Map<String, String> condition) {
         Page<StockAttachment> result = stockAttachmentService.getPage(condition);
         return R.success(result);
     }
 
     @PostMapping("/add")
-    public R add(@RequestBody StockAttachmentVo stockAttachmentVo){
+    public R add(@RequestBody StockAttachmentVo stockAttachmentVo) {
         stockAttachmentService.add(stockAttachmentVo);
         return R.success();
     }
 
     @PostMapping("/edit")
-    public R edit(@RequestBody StockAttachmentVo stockAttachmentVo){
+    public R edit(@RequestBody StockAttachmentVo stockAttachmentVo) {
         stockAttachmentService.edit(stockAttachmentVo);
         return R.success();
     }
 
     @PostMapping("/delete")
-    public R delete(@RequestBody StockAttachmentVo stockAttachmentVo){
+    public R delete(@RequestBody StockAttachmentVo stockAttachmentVo) {
         stockAttachmentService.delete(stockAttachmentVo);
         return R.success();
     }
 
     /**
      * 根据业务ID查询文件列表
+     *
      * @param busi
      * @return
      */
     @GetMapping("/getListByBusi")
-    public R getListByBusi(@RequestParam("busi")String busi){
+    public R getListByBusi(@RequestParam("busi") String busi) {
         return R.success(stockAttachmentService.getListByBusi(busi));
     }
 }