|
@@ -1,13 +1,19 @@
|
|
package org.jeecg.modules.iotedgeCollectData.service;
|
|
package org.jeecg.modules.iotedgeCollectData.service;
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
import org.jeecg.modules.iotedgeConfig.service.IIotedgeConfigService;
|
|
import org.jeecg.modules.iotedgeConfig.service.IIotedgeConfigService;
|
|
import org.jeecg.modules.iotedgeConfig.util.ConfigInfo;
|
|
import org.jeecg.modules.iotedgeConfig.util.ConfigInfo;
|
|
|
|
+import org.jeecg.modules.util.MyResponseErrorHandler;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.*;
|
|
import org.springframework.http.*;
|
|
|
|
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
+import javax.management.Query;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -174,4 +180,145 @@ public class RestClientService {
|
|
return response;
|
|
return response;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: 调用接口 在新增装置系统时 在iotedge中新增 名称为 装置_系统 的组织
|
|
|
|
+ * date: 2024/7/29
|
|
|
|
+ */
|
|
|
|
+ public JSONObject addGroup(String groupName,String elToken){
|
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/groups";
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_ADDGROUP);
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("groupName",groupName);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
|
+ restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, httpEntity, JSONObject.class);
|
|
|
|
+ return response.getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: 调用接口 在删除装置系统时 在iotedge中删除对应的组织
|
|
|
|
+ * date: 2024/7/30
|
|
|
|
+ */
|
|
|
|
+ public JSONObject deleteGroup(String groupId,String elToken){
|
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/groups"+"/"+groupId;
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_DELETEGROUP)+"/"+groupId;
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization", "Bearer " + elToken);
|
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
|
+ restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.DELETE,httpEntity, JSONObject.class);
|
|
|
|
+ return response.getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: sso登录
|
|
|
|
+ * date: 2024/8/1
|
|
|
|
+ */
|
|
|
|
+ private String getSSOToken(){
|
|
|
|
+ //String url = "http://127.0.0.1:8188/v4.0/auth/native";
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_LOGIN_URL);
|
|
|
|
+ String username = configService.getConfigValue(ConfigInfo.SSO_SYS_USERNAME);
|
|
|
|
+ String password = configService.getConfigValue(ConfigInfo.SSO_SYS_PASSWORD);
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("username",username);
|
|
|
|
+ jsonObject.put("password",password);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ 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);
|
|
|
|
+ String accessToken = (String) response.getBody().get("accessToken");
|
|
|
|
+ return accessToken;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: 调用 SSO 的接口去获取 clientId 、 clientSecret
|
|
|
|
+ * date: 2024/8/1
|
|
|
|
+ */
|
|
|
|
+ public JSONObject getClientInfo(){
|
|
|
|
+ String token = getSSOToken();
|
|
|
|
+ //String url = "http://127.0.0.1:8188/v4.0/clients?page=1&maxResults=10&appName=iothub";
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_GetClientInfo);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization", "Bearer " + token);
|
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
|
+ restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, JSONObject.class);
|
|
|
|
+ return response.getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: 调用 SSO 的接口去获取 accessToken
|
|
|
|
+ * date: 2024/8/1
|
|
|
|
+ */
|
|
|
|
+ public String getClientToken(){
|
|
|
|
+ JSONObject clientInfo = getClientInfo();
|
|
|
|
+ ArrayList<Map<String,Object>> resources = (ArrayList<Map<String,Object>>) clientInfo.get("resources");
|
|
|
|
+ String clientId = (String) resources.get(0).get("clientId");
|
|
|
|
+ String clientSecret = (String) resources.get(0).get("clientSecret");
|
|
|
|
+ //String url = "http://127.0.0.1:8188/v4.0/oauth/token?grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret+"&duration=eternal";
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_GetClientToken)+"?grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret+"&duration=eternal";
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ //headers.setContentType(type);
|
|
|
|
+ //headers.add("Authorization", "Bearer " + token);
|
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
|
|
|
|
+ restTemplate.setErrorHandler(new MyResponseErrorHandler());
|
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST,httpEntity, JSONObject.class);
|
|
|
|
+ String accessToken = (String) response.getBody().get("accessToken");
|
|
|
|
+ System.out.println("-----------"+accessToken.length());
|
|
|
|
+ configService.updateClientToken(accessToken);
|
|
|
|
+ return accessToken;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * author: dzc
|
|
|
|
+ * version: 1.0
|
|
|
|
+ * des: 调用 IotEdge 的接口 在修改装置系统时 去修改IoTEdge中对应的组织名称
|
|
|
|
+ * date: 2024/8/1
|
|
|
|
+ */
|
|
|
|
+ public JSONObject updateGroup(String groupName,String groupId){
|
|
|
|
+ String clientToken = configService.getConfigValue(ConfigInfo.CLIENT_TOKEN);
|
|
|
|
+ if (ObjectUtil.isNull(clientToken) || "".equals(clientToken)){
|
|
|
|
+ clientToken = getClientToken();
|
|
|
|
+ }
|
|
|
|
+ //String url = "http://127.0.0.1:8082/v1/org"+"/"+groupId;
|
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.IOTEDGE_UPDATEGROUP)+"/"+groupId;
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.put("orgName",groupName);
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
+ headers.add("Authorization", "Bearer " + clientToken);
|
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<String>(jsonObject.toString(), headers);
|
|
|
|
+
|
|
|
|
+ HttpClient httpClient = HttpClients.custom().build();
|
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
|
|
|
|
+
|
|
|
|
+ RestTemplate restTemplate1 = new RestTemplate(factory);
|
|
|
|
+ restTemplate1.setErrorHandler(new MyResponseErrorHandler());
|
|
|
|
+ ResponseEntity<JSONObject> response = restTemplate1.exchange(url,HttpMethod.PATCH,httpEntity, JSONObject.class);
|
|
|
|
+ return response.getBody();
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|