浏览代码

调用IoTEdge-Notification接口,发送邮件

丁治程 1 年之前
父节点
当前提交
c61ee37320

+ 9 - 16
jeecg-module-interlock/src/main/java/org/jeecg/modules/iotedgeCollectData/controller/IotedgeCollectDataController.java

@@ -2,18 +2,10 @@ package org.jeecg.modules.iotedgeCollectData.controller;
 
 import java.util.Arrays;
 import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
-import com.alibaba.fastjson.JSONObject;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.system.query.QueryGenerator;
-import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.iotedgeCollectData.entity.IoTEdgeDevices;
 import org.jeecg.modules.iotedgeCollectData.entity.IotedgeCollectData;
 import org.jeecg.modules.iotedgeCollectData.service.IIotedgeCollectDataService;
@@ -24,18 +16,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import lombok.extern.slf4j.Slf4j;
 
 import org.jeecg.modules.iotedgeCollectData.service.RestClientService;
-import org.jeecgframework.poi.excel.ExcelImportUtil;
-import org.jeecgframework.poi.excel.def.NormalExcelConstants;
-import org.jeecgframework.poi.excel.entity.ExportParams;
-import org.jeecgframework.poi.excel.entity.ImportParams;
-import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.jeecg.common.system.base.controller.JeecgController;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.multipart.MultipartFile;
-import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
-import com.alibaba.fastjson.JSON;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.jeecg.common.aspect.annotation.AutoLog;
@@ -54,6 +39,8 @@ public class IotedgeCollectDataController extends JeecgController<IotedgeCollect
 	@Autowired
 	private IIotedgeCollectDataService iotedgeCollectDataService;
 
+	@Autowired
+	private RestClientService restClientService;
 	
 	/**
 	 * 分页列表查询
@@ -194,4 +181,10 @@ public class IotedgeCollectDataController extends JeecgController<IotedgeCollect
 		 return  Result.OK(iotedgeCollectDataService.getDevicesInfoById());
 	 }
 
+	 @AutoLog(value = "IoTEdge-发送邮件测试")
+	 @ApiOperation(value="IoTEdge-发送邮件测试", notes="IoTEdge-发送邮件测试")
+	 @PostMapping(value = "/send")
+	 public ResponseEntity<String> send(@RequestParam(name = "fileName") String fileName,@RequestParam(name = "filePath") String filePath) throws Exception {
+		 return  restClientService.sendEmali(fileName,filePath);
+	 }
 }

+ 4 - 1
jeecg-module-interlock/src/main/java/org/jeecg/modules/iotedgeCollectData/entity/IotedgeTag.java

@@ -1,6 +1,7 @@
 package org.jeecg.modules.iotedgeCollectData.entity;
 
 import lombok.Data;
+import org.jeecgframework.poi.excel.annotation.Excel;
 
 /**
  * @author dzc
@@ -13,12 +14,14 @@ import lombok.Data;
 public class IotedgeTag {
 
     /** 点位名称 */
+    @Excel(name = "指标年份", width = 15)
     private String name;
 
     /** 点位值 */
+    @Excel(name = "指标金额(万元)", width = 15)
     private String value;
 
-
+    @Excel(name = "指标部门", width = 15)
     private String info;
 
 }

+ 131 - 5
jeecg-module-interlock/src/main/java/org/jeecg/modules/iotedgeCollectData/service/RestClientService.java

@@ -6,10 +6,14 @@ import org.jeecg.modules.iotedgeConfig.util.ConfigInfo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.*;
 import org.springframework.stereotype.Service;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
+import java.io.File;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Map;
+
 /**
  * @author dzc
  * @date 2024/5/22 15:14
@@ -46,11 +50,23 @@ public class RestClientService {
         MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
         headers.setContentType(type);
         HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
-        ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
+        ResponseEntity<JSONObject> response = null;
+        try {
+            response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
+        } catch (Exception e) {
+            System.out.println("query error: " + e.getMessage());
+            response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
+        }
         String accessToken = (String) response.getBody().get("accessToken");
         return accessToken;
     }
 
+    /**
+    *   author: dzc
+    *   version: 1.0
+    *   des: 调用IoTEdge接口 获取所有设备信息
+    *   date: 2024/5/29
+    */
     public JSONObject getAllDevicesInfo(){
         //String url = "http://127.0.0.1:8082/v1/devices?deviceType=&projectID=all&productID=all";
         String url = configService.getConfigValue(ConfigInfo.GETALLDEVICESINFO);
@@ -60,11 +76,23 @@ public class RestClientService {
         headers.setContentType(type);
         headers.add("Authorization", "Bearer " + accessToken);
         HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
-        ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        ResponseEntity<JSONObject> response = null;
+        try {
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        } catch (Exception e) {
+            System.out.println("query error: " + e.getMessage());
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        }
         return response.getBody();
     }
 
 
+    /**
+    *   author: dzc
+    *   version: 1.0
+    *   des: 调用IoTEdge接口 获取指定设备下的点位信息(根据设备ID)
+    *   date: 2024/5/29
+    */
     public JSONObject getAllDevicesInfoById(String deviceId){
         //String url = "http://127.0.0.1:8082/v1/devices/"+deviceId+"/deviceshadow";
         String url = configService.getConfigValue(ConfigInfo.GETALLDEVICESINFOBYID)+"/"+deviceId+"/deviceshadow";
@@ -74,8 +102,106 @@ public class RestClientService {
         headers.setContentType(type);
         headers.add("Authorization", "Bearer " + accessToken);
         HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
-        ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        ResponseEntity<JSONObject> response = null;
+        try {
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        } catch (Exception e) {
+            System.out.println("query error: " + e.getMessage());
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        }
+        return response.getBody();
+    }
+
+
+    /**
+    *   author: dzc
+    *   version: 1.0
+    *   des: 调用IoTEdge-Notification接口 获取所有群组信息
+    *   date: 2024/5/29
+    */
+    public JSONObject getAllEmailGroupInfo(){
+        //String url = "http://127.0.0.1:3005/api/v1.5/Groups?count=1000&index=1&desc=false";
+        String url = configService.getConfigValue(ConfigInfo.GETGROUPINFO);
+        String accessToken = getAccessToken();
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Authorization", "Bearer " + accessToken);
+        HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
+        ResponseEntity<JSONObject> response = null;
+        try {
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        } catch (Exception e) {
+            System.out.println("query error: " + e.getMessage());
+            response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
+        }
         return response.getBody();
     }
 
+
+    /**
+    *   author: dzc
+    *   version: 1.0
+    *   des: 调用IoTEdge-Notification接口 发送邮件(带附件)   (需求:定时生成历史数据的Excel表格之后就发送一次邮件,手动生成暂时不管)
+    *   date: 2024/5/29
+    */
+    public ResponseEntity<String> sendEmali(String fileName,String filePath) throws Exception {
+        // 由于 IoTEdge 中 Notification 可以定义多个群组 所以把所有的群组取出 将ID放入一个集合中
+        ArrayList<String> groupIdList = new ArrayList<>();
+        JSONObject allEmailGroupInfo = getAllEmailGroupInfo();
+        ArrayList<Map<String,Object>> items = (ArrayList<Map<String, Object>>) allEmailGroupInfo.get("list");
+        for (Map<String, Object> item : items) {
+            groupIdList.add(item.get("groupId").toString());
+        }
+        ArrayList<JSONObject> list = new ArrayList<>();
+        ArrayList<JSONObject> requestList = new ArrayList<>();
+        //String url = "http://127.0.0.1:3005/api/v1.5/Groups/send";
+        String url = configService.getConfigValue(ConfigInfo.SENDEMAil);
+        String accessToken = getAccessToken();
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Authorization", "Bearer " + accessToken);
+        // 文本内容参数
+        JSONObject jsonObject1 = new JSONObject();
+        jsonObject1.put("params","接口测试"); // Notification中模板中的参数 也可以没有,根据具体情况决定
+        // 附件内容
+        JSONObject jsonObject2 = new JSONObject();
+        jsonObject2.put("filename",fileName);
+        //File file = new File("D:/桌面/.../"+fileName);
+        //File file = new File("D:\\桌面\\...\\"+fileName);
+        File file;
+        if (filePath.endsWith("/")) {
+            file = new File(filePath+fileName);
+        } else {
+            file = new File(filePath+"/"+fileName);
+        }
+        jsonObject2.put("content", Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath())));
+        list.add(jsonObject2);
+
+        // 向所有群组 中的成员 发送邮件
+        for (String groupid:groupIdList) {
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("groupId",groupid);
+            jsonObject.put("useTemplate",true);
+            jsonObject.put("variables",jsonObject1);
+            jsonObject.put("attachments",list);
+            requestList.add(jsonObject);
+        }
+        //JSONObject jsonObject = new JSONObject();
+        //jsonObject.put("groupId","O5SwKzpdpiZh");
+        //jsonObject.put("useTemplate",true);
+        //jsonObject.put("variables",jsonObject1);
+        //jsonObject.put("attachments",list);
+        HttpEntity<String> httpEntity = new HttpEntity<String>(requestList.toString(), headers);
+        ResponseEntity<String> response = null;
+        try {
+            response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, String.class);
+        } catch (Exception e) {
+            System.out.println("query error: " + e.getMessage());
+            response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, String.class);
+        }
+        return response;
+    }
+
 }

+ 6 - 0
jeecg-module-interlock/src/main/java/org/jeecg/modules/iotedgeConfig/util/ConfigInfo.java

@@ -23,4 +23,10 @@ public class ConfigInfo {
 
     /** 根据设备id获取点位信息 */
     public static String GETALLDEVICESINFOBYID = "IoTEdge.GetDevicesInfoById";
+
+    /** 发送邮件 */
+    public static String SENDEMAil = "IoTEdge.Notification.SendEmail";
+
+    /** 邮件定义的群组 */
+    public static String GETGROUPINFO = "IoTEdge.Notification.GetGroupInfo";
 }