Kaynağa Gözat

Merge branch 'usky-zyj' of uskycloud/usky-modules into master

James 6 gün önce
ebeveyn
işleme
69b488a768

+ 10 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/DmpDeviceInfoController.java

@@ -137,6 +137,16 @@ public class DmpDeviceInfoController {
         dmpDeviceInfoService.export(dmpDeviceInfoRequest, response);
     }
 
+    /**
+     * 网关配置导出
+     * @param gatewayDeviceInfoRequestVO
+     * @return
+     */
+    @PostMapping("/gatewayExport")
+    public void gatewayExport(@RequestBody GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO) {
+        dmpDeviceInfoService.gatewayExport(gatewayDeviceInfoRequestVO);
+    }
+
     /**
      * 数据中心-设备实时心跳数据接口
      *

+ 6 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/DmpDeviceInfoService.java

@@ -68,6 +68,12 @@ public interface DmpDeviceInfoService extends CrudService<DmpDeviceInfo> {
      */
     void export(DmpDeviceInfoRequest dmpDeviceInfoRequest, HttpServletResponse response);
 
+    /**
+     * 网关设备信息导出
+     * @param gatewayDeviceInfoRequestVO
+     */
+    void gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO);
+
     CommonPage<Object> deviceCurrentDataList(String deviceName,String installAddress,String productCode,Integer pageNum,Integer pageSize);
 
     CommonPage<DataTVResponseVO> dataTVList(DataTVRequestVO requestVO);

+ 60 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -7,6 +7,8 @@ import cn.afterturn.easypoi.excel.entity.ExportParams;
 import cn.afterturn.easypoi.excel.entity.ImportParams;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -54,7 +56,10 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
 import java.text.DecimalFormat;
 import java.time.Instant;
 import java.time.LocalDateTime;
@@ -1155,6 +1160,61 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         }
     }
 
+    @Override
+    public void gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO){
+        try {
+            // 1. 查询数据库中的设备信息
+            LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
+            queryWrapper.eq(DmpDeviceInfo::getCategoryType,3)
+                    .eq(DmpDeviceInfo::getDeleteFlag, 0)
+                    .eq(StringUtils.isNotBlank(gatewayDeviceInfoRequestVO.getGatewayUuid()), DmpDeviceInfo::getGatewayUuid, gatewayDeviceInfoRequestVO.getGatewayUuid())
+                    .eq(StringUtils.isNotBlank(gatewayDeviceInfoRequestVO.getProductCode()), DmpDeviceInfo::getProductCode, gatewayDeviceInfoRequestVO.getProductCode())
+                    .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
+                    .orderByDesc(DmpDeviceInfo::getId);
+            List<DmpDeviceInfo> gatewayDeviceList = this.list(queryWrapper);
+
+            // 2. 定义服务器本地保存路径(建议配置在application.yml中,这里简化写死)
+            // 注意:要确保该目录存在,否则会报错;Windows路径用\\,Linux/Mac用/
+            String localPath = gatewayDeviceInfoRequestVO.getFilePath()+ "/" + gatewayDeviceInfoRequestVO.getProductCode() + ".cfg";
+
+            // 3. 调用exportToLocalFile导出到本地
+            File file = FileUtil.file(localPath);
+            exportDeviceToTxt(gatewayDeviceList, FileUtil.getOutputStream(file));
+        } catch (Exception e) {
+            throw new RuntimeException("导出网关设备配置信息失败", e);
+        }
+    }
+
+    /**
+     * 导出设备信息到TXT文件
+     * @param deviceList 设备列表
+     * @param outputStream 输出流(用于下载)
+     */
+    public static void exportDeviceToTxt(List<DmpDeviceInfo> deviceList, OutputStream outputStream) {
+        // 1. 构建TXT内容(自定义格式,按行写入)
+        StringBuilder content = new StringBuilder();
+        // 写入表头
+        content.append("{");
+        // 写入数据行
+        for (DmpDeviceInfo device : deviceList) {
+            content.append("\n\t\"").append(device.getImsiCode()).append("\":")
+                    .append("{").append("\"").append("deviceid").append("\"").append(":").append("\"").append(device.getDeviceId()).append("\"").append(",")
+                    .append("\"").append("deviceUuid").append("\"").append(":").append("\"").append(device.getDeviceUuid()).append("\"").append(",")
+                    .append("\"").append("productCode").append("\"").append(":").append("\"").append(device.getProductCode()).append("\"")
+                    .append("}").append(",");
+        }
+        content.delete(content.length()-1, content.length());
+        content.append("\n}");
+
+        // 2. 将内容写入输出流(UTF-8编码避免乱码)
+        try {
+            outputStream.write(content.toString().getBytes(StandardCharsets.UTF_8));
+            outputStream.flush();
+        } catch (Exception e) {
+            throw new RuntimeException("导出TXT文件失败", e);
+        }
+    }
+
     public void exportExcel(Workbook workbook, HttpServletResponse response) {
         // 定义一个流,用作流式输出
         ServletOutputStream outputStream = null;

+ 22 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/GatewayDeviceInfoRequestVO.java

@@ -0,0 +1,22 @@
+package com.usky.iot.service.vo;
+
+import lombok.Data;
+
+@Data
+public class GatewayDeviceInfoRequestVO {
+
+    /**
+     * 产品编码
+     */
+    private String productCode;
+
+    /**
+     * 网关设备Uuid
+     */
+    private String gatewayUuid;
+
+    /**
+     * 文件路径
+     */
+    private String filePath;
+}