|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|