java110 пре 5 година
родитељ
комит
15c09f0576

+ 2 - 1
service-job/src/main/java/com/java110/job/adapt/hcIot/GetToken.java

@@ -42,7 +42,8 @@ public class GetToken {
         }
         HttpHeaders headers = new HttpHeaders();
         HttpEntity httpEntity = new HttpEntity(headers);
-        ResponseEntity<String> tokenRes = restTemplate.exchange(IotConstant.GET_TOKEN_URL, HttpMethod.GET, httpEntity, String.class);
+        String url = IotConstant.getUrl(IotConstant.GET_TOKEN_URL.replace("APP_ID", IotConstant.getAppId()).replace("APP_SECRET", IotConstant.getAppSecret()));
+        ResponseEntity<String> tokenRes = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
 
         if (tokenRes.getStatusCode() != HttpStatus.OK) {
             throw new IllegalArgumentException("获取token失败" + tokenRes.getBody());

+ 51 - 12
service-job/src/main/java/com/java110/job/adapt/hcIot/IotConstant.java

@@ -15,6 +15,9 @@
  */
 package com.java110.job.adapt.hcIot;
 
+import com.java110.utils.cache.MappingCache;
+import com.java110.utils.util.StringUtil;
+
 /**
  * HC 物联网常量类
  * 接口协议地址: https://gitee.com/java110/MicroCommunityThings/blob/master/back/docs/api.md
@@ -23,30 +26,35 @@ package com.java110.job.adapt.hcIot;
  */
 public class IotConstant {
 
-    public static final String IOT_URL = "https://things.homecommunity.cn:9999/";
+    public static final String IOT_DOMAIN = "IOT"; // 物联网域
+    public static final String IOT_URL = "IOT_URL"; // 物联网域
+    public static final String IOT_APP_ID = "APP_ID"; // 物联网域
+    public static final String IOT_APP_SECRET = "APP_SECRET"; // 物联网域
+
+    private static final String DEFAULT_IOT_URL = "https://things.homecommunity.cn/";
 
-    public static final String APP_ID = "e86a6a373c354927bea5fd21a0bec617";
-    public static final String APP_SECRET = "ead9a2f67f96e2b8ed2fe38cc9709463";
+    private static final String DEFAULT_APP_ID = "e86a6a373c354927bea5fd21a0bec617";
+    private static final String DEFAULT_APP_SECRET = "ead9a2f67f96e2b8ed2fe38cc9709463";
 
-    public static final String GET_TOKEN_URL = IOT_URL + " /extApi/auth/getAccessToken?appId=" + APP_ID + "&appSecret=" + APP_SECRET;
+    public static final String GET_TOKEN_URL = " /extApi/auth/getAccessToken?appId=APP_ID&appSecret=APP_SECRET";
 
     //添加小区
-    public static final String ADD_COMMUNITY_URL = IOT_URL + "/extApi/community/addCommunity";
+    public static final String ADD_COMMUNITY_URL = "/extApi/community/addCommunity";
     //修改小区
-    public static final String UPDATE_COMMUNITY_URL = IOT_URL + "/extApi/community/updateCommunity";
+    public static final String UPDATE_COMMUNITY_URL = "/extApi/community/updateCommunity";
     //删除小区
-    public static final String DELETE_COMMUNITY_URL = IOT_URL + "/extApi/community/deleteCommunity";
+    public static final String DELETE_COMMUNITY_URL = "/extApi/community/deleteCommunity";
 
     //添加设备
-    public static final String ADD_MACHINE_URL = IOT_URL + "/extApi/machine/addMachine";
+    public static final String ADD_MACHINE_URL = "/extApi/machine/addMachine";
     //修改设备
-    public static final String UPDATE_MACHINE_URL = IOT_URL + "/extApi/machine/updateMachine";
+    public static final String UPDATE_MACHINE_URL = "/extApi/machine/updateMachine";
     //删除设备
-    public static final String DELETE_MACHINE_URL = IOT_URL + "/extApi/machine/deleteMachine";
+    public static final String DELETE_MACHINE_URL = "/extApi/machine/deleteMachine";
     //开门接口
-    public static final String OPEN_DOOR = IOT_URL + "/extApi/machine/openDoor";
+    public static final String OPEN_DOOR = "/extApi/machine/openDoor";
     //重启接口
-    public static final String RESTART_MACHINE = IOT_URL + "/extApi/machine/restartMachine";
+    public static final String RESTART_MACHINE = "/extApi/machine/restartMachine";
 
     public static final String HC_TOKEN = "HC_ACCESS_TOKEN";
 
@@ -57,4 +65,35 @@ public class IotConstant {
     public static final String ADD_OWNER = "/extApi/user/addUser";
     public static final String EDIT_OWNER = "/extApi/user/updateUser";
     public static final String DELETE_OWNER = "/extApi/user/deleteUser";
+
+
+    public static String getUrl(String param) {
+        String url = MappingCache.getValue(IOT_DOMAIN, IotConstant.IOT_URL);
+
+        if (StringUtil.isEmpty(url)) {
+            return DEFAULT_IOT_URL + param;
+        }
+
+        return url + param;
+    }
+
+    public static String getAppId() {
+        String appId = MappingCache.getValue(IOT_DOMAIN, IotConstant.IOT_APP_ID);
+
+        if (StringUtil.isEmpty(appId)) {
+            return DEFAULT_APP_ID;
+        }
+
+        return appId;
+    }
+
+    public static String getAppSecret() {
+        String appSecret = MappingCache.getValue(IOT_DOMAIN, IotConstant.IOT_APP_SECRET);
+
+        if (StringUtil.isEmpty(appSecret)) {
+            return DEFAULT_APP_SECRET;
+        }
+
+        return appSecret;
+    }
 }

+ 1 - 1
service-job/src/main/java/com/java110/job/adapt/hcIot/OpenDoorAdapt.java

@@ -44,7 +44,7 @@ public class OpenDoorAdapt extends DatabusAdaptImpl {
         JSONObject postParameters = new JSONObject();
         postParameters.put("machineCode", paramIn.getString("machineCode"));
         HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(postParameters, getHeaders(outRestTemplate));
-        ResponseEntity<String> responseEntity = outRestTemplate.exchange(IotConstant.OPEN_DOOR, HttpMethod.POST, httpEntity, String.class);
+        ResponseEntity<String> responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.OPEN_DOOR), HttpMethod.POST, httpEntity, String.class);
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
             return new ResultVo(ResultVo.CODE_ERROR, responseEntity.getBody());
         }

+ 1 - 1
service-job/src/main/java/com/java110/job/adapt/hcIot/RestartMachineAdapt.java

@@ -44,7 +44,7 @@ public class RestartMachineAdapt extends DatabusAdaptImpl {
         JSONObject postParameters = new JSONObject();
         postParameters.put("machineCode", paramIn.getString("machineCode"));
         HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(postParameters, getHeaders(outRestTemplate));
-        ResponseEntity<String> responseEntity = outRestTemplate.exchange(IotConstant.RESTART_MACHINE, HttpMethod.POST, httpEntity, String.class);
+        ResponseEntity<String> responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.RESTART_MACHINE), HttpMethod.POST, httpEntity, String.class);
         if (responseEntity.getStatusCode() != HttpStatus.OK) {
             return new ResultVo(ResultVo.CODE_ERROR, responseEntity.getBody());
         }

+ 9 - 9
service-job/src/main/java/com/java110/job/adapt/hcIot/asyn/impl/IotSendAsynImpl.java

@@ -96,7 +96,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.ADD_COMMUNITY_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.ADD_COMMUNITY_URL), HttpMethod.POST, httpEntity, String.class);
             logger.debug("调用HC IOT信息:" + responseEntity);
             JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
 
@@ -130,7 +130,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.UPDATE_COMMUNITY_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.UPDATE_COMMUNITY_URL), HttpMethod.POST, httpEntity, String.class);
             logger.debug("调用HC IOT信息:" + responseEntity);
             JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
             if (paramOut.getInteger("code") != ResultVo.CODE_OK) {
@@ -166,7 +166,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.DELETE_COMMUNITY_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.DELETE_COMMUNITY_URL), HttpMethod.POST, httpEntity, String.class);
             logger.debug("调用HC IOT信息:" + responseEntity);
             JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
             if (paramOut.getInteger("code") != ResultVo.CODE_OK) {
@@ -207,7 +207,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.ADD_MACHINE_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.ADD_MACHINE_URL), HttpMethod.POST, httpEntity, String.class);
 
             logger.debug("调用HC IOT信息:" + responseEntity);
 
@@ -265,7 +265,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.UPDATE_MACHINE_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.UPDATE_MACHINE_URL), HttpMethod.POST, httpEntity, String.class);
 
             logger.debug("调用HC IOT信息:" + responseEntity);
 
@@ -310,7 +310,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.DELETE_MACHINE_URL, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.DELETE_MACHINE_URL), HttpMethod.POST, httpEntity, String.class);
             logger.debug("调用HC IOT信息:" + responseEntity);
             if (responseEntity.getStatusCode() != HttpStatus.OK) {
                 machineTranslateDto.setState(MachineTranslateDto.STATE_ERROR);
@@ -354,7 +354,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.ADD_OWNER, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.ADD_OWNER), HttpMethod.POST, httpEntity, String.class);
 
             logger.debug("调用HC IOT信息:" + responseEntity);
             if (responseEntity.getStatusCode() != HttpStatus.OK) {
@@ -399,7 +399,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.EDIT_OWNER, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.EDIT_OWNER), HttpMethod.POST, httpEntity, String.class);
             logger.debug("调用HC IOT信息:" + responseEntity);
             if (responseEntity.getStatusCode() != HttpStatus.OK) {
                 machineTranslateDto.setState(MachineTranslateDto.STATE_ERROR);
@@ -444,7 +444,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         ResponseEntity<String> responseEntity = null;
         try {
             HttpEntity httpEntity = new HttpEntity(postParameters, getHeaders());
-            responseEntity = outRestTemplate.exchange(IotConstant.DELETE_OWNER, HttpMethod.POST, httpEntity, String.class);
+            responseEntity = outRestTemplate.exchange(IotConstant.getUrl(IotConstant.DELETE_OWNER), HttpMethod.POST, httpEntity, String.class);
 
             logger.debug("调用HC IOT信息:" + responseEntity);
             if (responseEntity.getStatusCode() != HttpStatus.OK) {