瀏覽代碼

数据中心端增加数据同步功能。

zhuoning 3 年之前
父節點
當前提交
a5e3a4a7e4

+ 2 - 1
huimv-farm-admin/src/main/java/com/huimv/admin/device/controller/DeviceController.java

@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.io.UnsupportedEncodingException;
 import java.text.ParseException;
 
 /**
@@ -34,7 +35,7 @@ public class DeviceController {
     @RequestMapping(value = "/newDevice",method = RequestMethod.GET)
     public Result saveDevice(@RequestParam(value = "deviceName",required = true) String deviceName, @RequestParam(value = "deviceCode",required = true) String deviceCode, @RequestParam(value = "deviceType",required = true) String deviceType,
                              @RequestParam(value = "factory",required = true) String factory, @RequestParam(value = "worker",required = true) String worker, @RequestParam(value = "mainParams",required = true) String mainParams,
-                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId){
+                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId) throws UnsupportedEncodingException {
         //
         return iDevice.newDevice(deviceName,deviceCode,deviceType,factory,worker,mainParams,state,record,farmId);
     }

+ 3 - 1
huimv-farm-admin/src/main/java/com/huimv/admin/device/service/IDevice.java

@@ -2,9 +2,11 @@ package com.huimv.admin.device.service;
 
 import com.huimv.common.utils.Result;
 
+import java.io.UnsupportedEncodingException;
+
 public interface IDevice {
     //
-    Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId);
+    Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) throws UnsupportedEncodingException;
 
     //
     Result editDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId, Integer id);

+ 35 - 2
huimv-farm-admin/src/main/java/com/huimv/admin/device/service/impl/DeviceImpl.java

@@ -1,14 +1,19 @@
 package com.huimv.admin.device.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.huimv.admin.device.dao.entity.FarmDeviceEntity;
 import com.huimv.admin.device.dao.repo.FarmDeviceRepo;
 import com.huimv.admin.device.service.IDevice;
 import com.huimv.admin.device.utils.DateUtil;
+import com.huimv.admin.device.utils.HttpTemplete;
 import com.huimv.admin.device.utils.MathUtil;
+import com.huimv.admin.device.utils.TextUtil;
 import com.huimv.common.utils.Result;
 import com.huimv.common.utils.ResultCode;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.domain.Specification;
@@ -16,6 +21,7 @@ import org.springframework.stereotype.Service;
 
 import javax.persistence.criteria.Order;
 import javax.persistence.criteria.Predicate;
+import java.io.UnsupportedEncodingException;
 import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Date;
@@ -31,6 +37,7 @@ import java.util.Optional;
  * @Create : 2020-12-25
  **/
 @Service
+@Slf4j
 public class DeviceImpl implements IDevice {
     @Autowired
     private FarmDeviceRepo deviceRepo;
@@ -38,7 +45,18 @@ public class DeviceImpl implements IDevice {
     private DateUtil dateUtil;
     @Autowired
     private MathUtil mathUtil;
-
+    @Autowired
+    private HttpTemplete httpTemplete;
+    @Autowired
+    private TextUtil textUtil;
+    @Value("${farm.device.sync}")
+    private boolean dataSync;
+    @Value("${farm.device.addService}")
+    private String deviceAddService;
+    @Value("${farm.device.editService}")
+    private String deviceEditService;
+    @Value("${farm.device.removeService}")
+    private String deviceRemoveService;
     /**
      * @Method : newDevice
      * @Description : 添加设备
@@ -49,7 +67,7 @@ public class DeviceImpl implements IDevice {
      * @Time : 22:03
      */
     @Override
-    public Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) {
+    public Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) throws UnsupportedEncodingException {
         FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
         deviceEntity.setDeviceCode(deviceCode);
         deviceEntity.setDeviceName(deviceName);
@@ -62,6 +80,21 @@ public class DeviceImpl implements IDevice {
         deviceEntity.setState(state);
         deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
         FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
+        log.info("数据中心添加设备信息>>"+outFarmDeviceEntity);
+
+        //同步牧场端设备数据
+        if(dataSync){
+            log.info("同步牧场端设备数据。");
+            //从数据库查询出牧场id
+            String farmIp = "http://192.168.1.49:8091";
+            String serviceUrl = farmIp + deviceAddService;
+            System.out.println("serviceUrl>>"+serviceUrl);
+            String data = textUtil.encode(JSON.toJSONString(outFarmDeviceEntity));
+            System.out.println("密文>>"+data);
+
+            //提交请求
+            httpTemplete.doPostSimple(serviceUrl,data);
+        }
         return new Result(ResultCode.SUCCESS, outFarmDeviceEntity);
     }
 

+ 518 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/utils/HttpTemplete.java

@@ -0,0 +1,518 @@
+package com.huimv.admin.device.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.*;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class HttpTemplete {
+    private String testHttpIp = "http://localhost:9105";
+
+//    public static void main(String[] args) throws Exception {
+//        //
+//        HttpTemplete client = new HttpTemplete();
+////        client.testGetRequest1();
+//        client.testGetRequest2();
+//    }
+    private Integer connectTimeout = null;
+    private Integer requestTimeout = null;
+    private Integer socketTimeout = null;
+
+    static {
+    }
+
+    //发送简单数据
+    public void doPostSimple(String serviceUrl,String data){
+        System.out.println("推送数据地址:"+serviceUrl);
+        //
+        Map<String,String> paramsMap = new HashMap<String,String>();
+        paramsMap.put("data", data);
+        //
+        Map<String,Integer> timeoutMap = new HashMap<String,Integer>();
+        timeoutMap.put("connectTimeout", 5000);
+        timeoutMap.put("requestTimeout", 5000);
+        timeoutMap.put("socketTimeout", 5000);
+        try{
+            // 用Post方法推送接口数据
+            doPost(serviceUrl,paramsMap,timeoutMap);
+        } catch (IOException e) {
+            System.out.println("###错误信息:"+e.getMessage());
+            System.out.println("###错误信息:"+e.getLocalizedMessage());
+            e.printStackTrace();
+        }
+    }
+
+
+    /**
+     * @Method      : doPost
+     * @Description : post方式推送接口
+     * @Params      : [url, paramsMap, timeoutMap]
+     * @Return      : com.alibaba.fastjson.JSONObject
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/11/19       
+     * @Time        : 14:55
+     */
+    public JSONObject doPost(String url, Map<String, String> paramsMap, Map<String, Integer> timeoutMap) throws IOException {
+        // 创建默认的httpClient实例.
+        CloseableHttpClient httpClient = getHttpClientConnection();
+        //
+        int connectTimeout = timeoutMap.get("connectTimeout");
+        int requestTimeout = timeoutMap.get("requestTimeout");
+        int socketTimeout = timeoutMap.get("socketTimeout");
+        // 创建httpPost
+        HttpPost httpPost = new HttpPost(url);
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setSocketTimeout(socketTimeout) //服务器响应超时时间
+                .setConnectTimeout(connectTimeout) //连接服务器超时时间
+                .setConnectionRequestTimeout(requestTimeout)
+                .build();
+        httpPost.setConfig(requestConfig);
+        // 创建参数队列
+        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
+        Set<Map.Entry<String, String>> entrySet = paramsMap.entrySet();
+        for (Map.Entry<String, String> e : entrySet) {
+            String name = e.getKey();
+            String value = e.getValue();
+            NameValuePair pair = new BasicNameValuePair(name, value);
+            paramsList.add(pair);
+        }
+        UrlEncodedFormEntity content = new UrlEncodedFormEntity(paramsList, "UTF-8");
+        //处理乱码
+//        StringEntity content = new StringEntity(paramsList.toString(), Charset.forName("UTF-8"));// 第二个参数,设置后才会对,内容进行编码
+//        content.setContentType("application/soap+xml; charset=UTF-8");
+//        content.setContentEncoding("UTF-8");
+        httpPost.setEntity(content);
+//        HttpUriRequest httpPost = RequestBuilder.post().setUri(url).setEntity(content).setConfig(requestConfig);
+        CloseableHttpResponse response = httpClient.execute(httpPost);
+        // 获取响应实体
+        HttpEntity entity = response.getEntity();
+        try {
+            if (response.getStatusLine().getStatusCode() == 200) {
+                // 打印响应内容
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", true);
+                resultJo.put("content", text);
+                return resultJo;
+            }else{
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", false);
+                resultJo.put("content", text);
+                return resultJo;
+            }
+        } finally {
+            //释放资源
+            if (response != null) {
+                response.close();
+            }
+            if (httpClient != null) {
+                httpClient.close();
+            }
+        }
+    }
+
+    /**
+     * @Method      : setTimeout
+     * @Description : 
+     * @Params      : [connectTimeout, requestTimeout, socketTimeout]
+     * @Return      : void
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/11/23       
+     * @Time        : 9:36
+     */
+    public Map<String, Integer> setTimeout(Integer connectTimeout, Integer requestTimeout, Integer socketTimeout){
+        //
+        if(connectTimeout == null){
+            this.connectTimeout = 3000;
+        }else{
+            this.connectTimeout = connectTimeout;
+        }
+        //
+        if(requestTimeout == null){
+            this.requestTimeout = 3000;
+        }else{
+            this.requestTimeout = requestTimeout;
+        }
+        //
+        if(socketTimeout == null){
+            this.socketTimeout = 3000;
+        }else{
+            this.socketTimeout = socketTimeout;
+        }
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", this.connectTimeout);
+        timeoutMap.put("requestTimeout", this.requestTimeout);
+        timeoutMap.put("socketTimeout", this.socketTimeout);
+        return timeoutMap;
+    }
+
+    /**
+     * @Method      : setTimeout
+     * @Description : 
+     * @Params      : [connectTimeout, requestTimeout, socketTimeout]
+     * @Return      : java.util.Map<java.lang.String,java.lang.Integer>
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/11/23       
+     * @Time        : 9:41
+     */
+    public Map<String, Integer> setTimeout(String connectTimeout, String requestTimeout, String socketTimeout){
+        //
+        if(connectTimeout == null){
+            this.connectTimeout = 3000;
+        }else{
+            this.connectTimeout = Integer.parseInt(connectTimeout);
+        }
+        //
+        if(requestTimeout == null){
+            this.requestTimeout = 3000;
+        }else{
+            this.requestTimeout = Integer.parseInt(requestTimeout);
+        }
+        //
+        if(socketTimeout == null){
+            this.socketTimeout = 3000;
+        }else{
+            this.socketTimeout = Integer.parseInt(socketTimeout);
+        }
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", this.connectTimeout);
+        timeoutMap.put("requestTimeout", this.requestTimeout);
+        timeoutMap.put("socketTimeout", this.socketTimeout);
+        return timeoutMap;
+    }
+
+    /**
+     * @Method      : doGet
+     * @Description : 
+     * @Params      : [url, paramsMap]
+     * @Return      : com.alibaba.fastjson.JSONObject
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/11/23       
+     * @Time        : 9:34
+     */
+    public JSONObject doGet(String url, Map<String, String> paramsMap) throws IOException {
+        //
+        if(this.connectTimeout == null){
+            this.connectTimeout = 3000;
+        }
+        //
+        if(this.requestTimeout == null)
+        {
+            this.requestTimeout = 3000;
+        }
+        //
+        if(this.socketTimeout == null){
+            this.socketTimeout = 3000;
+        }
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", this.connectTimeout);
+        timeoutMap.put("requestTimeout", this.requestTimeout);
+        timeoutMap.put("socketTimeout", this.socketTimeout);
+        //
+        CloseableHttpClient httpClient = getHttpClientConnection();
+        //执行//获取请求内容
+        CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, paramsMap, timeoutMap));
+        try {
+            // 获取响应实体
+            HttpEntity entity = response.getEntity();
+            // 打印响应状态
+            if (response.getStatusLine().getStatusCode() == 200) {
+                // 打印响应内容
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", true);
+                resultJo.put("content", text);
+                return resultJo;
+            } else {
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", false);
+                resultJo.put("content", text);
+                return resultJo;
+            }
+        } finally {
+            //释放资源
+            if (response != null) {
+                response.close();
+            }
+            if (httpClient != null) {
+                httpClient.close();
+            }
+        }
+    }
+
+    private CloseableHttpClient httpClient = null;
+    private CloseableHttpResponse response = null;
+
+    public JSONObject doGetBatch(String url, Map<String, String> paramsMap) throws IOException {
+        //
+        if(this.connectTimeout == null){
+            this.connectTimeout = 3000;
+        }
+        //
+        if(this.requestTimeout == null)
+        {
+            this.requestTimeout = 3000;
+        }
+        //
+        if(this.socketTimeout == null){
+            this.socketTimeout = 3000;
+        }
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", this.connectTimeout);
+        timeoutMap.put("requestTimeout", this.requestTimeout);
+        timeoutMap.put("socketTimeout", this.socketTimeout);
+        //
+        httpClient = getHttpClientConnection();
+        //执行//获取请求内容
+        response = httpClient.execute(getHttpRequest(url, paramsMap, timeoutMap));
+        // 获取响应实体
+        HttpEntity entity = response.getEntity();
+        // 打印响应状态
+        if (response.getStatusLine().getStatusCode() == 200) {
+                // 打印响应内容
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", true);
+                resultJo.put("content", text);
+                return resultJo;
+        } else {
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", false);
+                resultJo.put("content", text);
+                return resultJo;
+        }
+    }
+
+    //关闭http连接
+    public void closeHttpConn() throws IOException {
+        System.out.println("开始释放http连接资源");
+        //释放资源
+        if (response != null) {
+            response.close();
+        }
+        if (httpClient != null) {
+            httpClient.close();
+        }
+    }
+
+    /**
+     * @Method : doGet
+     * @Description :get方式推送接口
+     * @Params : [url, paramsMap, timeoutMap]
+     * @Return : com.alibaba.fastjson.JSONObject
+     * @Author : ZhuoNing
+     * @Date : 2021/11/18
+     * @Time : 17:39
+     */
+    public JSONObject doGet(String url, Map<String, String> paramsMap, Map<String, Integer> timeoutMap) throws IOException {
+        if (timeoutMap == null) {
+            timeoutMap.put("connectTimeout", 5000);
+            timeoutMap.put("requestTimeout", 5000);
+            timeoutMap.put("socketTimeout", 5000);
+        }
+        //
+        CloseableHttpClient httpClient = getHttpClientConnection();
+        //执行//获取请求内容
+        CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, paramsMap, timeoutMap));
+        try {
+            // 获取响应实体
+            HttpEntity entity = response.getEntity();
+            // 打印响应状态
+            if (response.getStatusLine().getStatusCode() == 200) {
+                // 打印响应内容
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", true);
+                resultJo.put("content", text);
+                return resultJo;
+            } else {
+                String text = EntityUtils.toString(entity, "utf-8");
+                JSONObject resultJo = new JSONObject();
+                resultJo.put("status", false);
+                resultJo.put("content", text);
+                return resultJo;
+            }
+        } finally {
+            //释放资源
+            if (response != null) {
+                response.close();
+            }
+            if (httpClient != null) {
+                httpClient.close();
+            }
+        }
+    }
+
+    private void testGetRequest1() throws IOException {
+        Map<String, Integer> timeoutMap = new HashMap<String, Integer>();
+        timeoutMap.put("connectTimeout", 5000);
+        timeoutMap.put("requestTimeout", 5000);
+        timeoutMap.put("socketTimeout", 5000);
+        String url = testHttpIp + "/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
+        // test error
+        url = testHttpIp + "/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c&userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c";
+        CloseableHttpClient httpClient = getHttpClientConnection();
+        CloseableHttpResponse response = httpClient.execute(getHttpRequest(url, timeoutMap));
+        // 打印响应状态
+        System.out.println("响应状态1 =" + response.getStatusLine());
+        try {
+            if (response.getStatusLine().getStatusCode() == 200) {
+                // 获取响应实体
+                HttpEntity entity = response.getEntity();
+                // 打印响应内容
+                String text = EntityUtils.toString(entity, "utf-8");
+                System.out.println("响应内容=" + text);
+
+                JSONObject resultJo = JSONObject.parseObject(text);
+                System.out.println("accessToken=" + resultJo.getString("accessToken"));
+            } else {
+                System.out.println("请求失败");
+            }
+        } finally {
+            //释放资源
+            if (response != null) {
+                response.close();
+            }
+            if (httpClient != null) {
+                httpClient.close();
+            }
+        }
+    }
+
+    //
+    private CloseableHttpClient getHttpClientConnection() {
+        //创建默认实例
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+        //
+//        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+        return httpclient;
+    }
+    //
+    private HttpUriRequest getHttpRequest(String url, Map<String, String> map, Map<String, Integer> timeoutMap) {
+        int connectTimeout = timeoutMap.get("connectTimeout");
+        int requestTimeout = timeoutMap.get("requestTimeout");
+        int socketTimeout = timeoutMap.get("socketTimeout");
+//        System.out.println("core connectTimeout="+connectTimeout);
+//        System.out.println("core requestTimeout="+requestTimeout);
+//        System.out.println("core socketTimeout="+socketTimeout);
+
+        List<NameValuePair> params = new ArrayList<NameValuePair>();
+        Set<Map.Entry<String, String>> entrySet = map.entrySet();
+        for (Map.Entry<String, String> e : entrySet) {
+            String name = e.getKey();
+            String value = e.getValue();
+            NameValuePair pair = new BasicNameValuePair(name, value);
+            params.add(pair);
+        }
+        //
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(requestTimeout)
+                .setSocketTimeout(socketTimeout).build();
+        HttpUriRequest httpGet = RequestBuilder.get().setUri(url)
+                .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
+                .setConfig(requestConfig).build();
+        return httpGet;
+    }
+    //
+    private HttpGet getHttpRequest(String url, Map<String, Integer> timeoutMap) {
+        int connectTimeout = timeoutMap.get("connectTimeout");
+        int requestTimeout = timeoutMap.get("requestTimeout");
+        int socketTimeout = timeoutMap.get("socketTimeout");
+
+        HttpGet httpGet = new HttpGet(url);
+        //设置超时时间
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(requestTimeout)
+                .setSocketTimeout(socketTimeout).build();
+        httpGet.setConfig(requestConfig);
+        return httpGet;
+    }
+
+    //test
+    private void get() {
+//        CloseableHttpClient httpclient = HttpClients.createDefault();
+//        try {
+//            // 创建httpget.
+//            HttpGet httpget = new HttpGet(httpIp + "/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c");
+//            System.out.println("executing request " + httpget.getURI());
+//            // 执行get请求.
+//            CloseableHttpResponse response = httpclient.execute(httpget);
+//            try {
+//                // 获取响应实体
+//                HttpEntity entity = response.getEntity();
+//                System.out.println("--------------------------------------");
+//                // 打印响应状态
+//                System.out.println(response.getStatusLine());
+//                if (entity != null) {
+//                    // 打印响应内容长度
+//                    System.out.println("Response content length: " + entity.getContentLength());
+//                    // 打印响应内容
+//                    System.out.println("Response content: " + EntityUtils.toString(entity));
+//                }
+//                System.out.println("------------------------------------");
+//            } finally {
+//                response.close();
+//            }
+//        } catch (ClientProtocolException e) {
+//            e.printStackTrace();
+//        } catch (ParseException e) {
+//            e.printStackTrace();
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        } finally {
+//            // 关闭连接,释放资源
+//            try {
+//                httpclient.close();
+//            } catch (IOException e) {
+//                e.printStackTrace();
+//            }
+//        }
+    }
+
+
+    private void getRequest() throws IOException {
+//        //1.打开浏览器
+//        CloseableHttpClient httpClient = HttpClients.createDefault();
+//        //2.声明get请求
+//        HttpGet httpGet = new HttpGet(httpIp + "/token/getToken?userId=20210501&timestamp=45546546454&random=1156&sign=7fa431325504e01e9fa87ed0e274c40c");
+//        //3.发送请求
+//        CloseableHttpResponse response = httpClient.execute(httpGet);
+//        System.out.println("StatusCode=" + response.getStatusLine().getStatusCode());
+//        //4.判断状态码
+//        if (response.getStatusLine().getStatusCode() == 200) {
+//            HttpEntity entity = response.getEntity();
+//            //使用工具类EntityUtils,从响应中取出实体表示的内容并转换成字符串
+//            String string = EntityUtils.toString(entity, "utf-8");
+//            System.out.println(string);
+//        }
+//        //5.关闭资源
+//        response.close();
+//        httpClient.close();
+    }
+}

+ 31 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/utils/TextUtil.java

@@ -0,0 +1,31 @@
+package com.huimv.admin.device.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Base64;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class TextUtil {
+
+    //base64编码
+    public String encode(String text) throws UnsupportedEncodingException {
+        final Base64.Encoder encoder = Base64.getEncoder();
+        final byte[] textByte = text.replaceAll(" ", "").getBytes("UTF-8");
+        return encoder.encodeToString(textByte);
+    }
+
+    //base64解码
+    public String decode(String encodedText) throws UnsupportedEncodingException {
+        final Base64.Decoder decoder = Base64.getDecoder();
+        return new String(decoder.decode(encodedText.toString().replace("\r\n", "")), "UTF-8");
+    }
+}

+ 5 - 3
huimv-farm-admin/src/main/resources/application-dev.yml

@@ -9,14 +9,16 @@ spring:
     username: root
     password: hm123456
     driver-class-name: com.mysql.cj.jdbc.Driver
-#    driver-class-name: com.mysql.jdbc.Driver
   jpa:
     hibernate:
       ddl-auto: update
     database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
-
+  redis:
+    host: 122.112.224.199
+    port: 6379
+    password: hm123456
 # Socket配置
-socket:
+#socket:
   # 监听端口 9001
 #  listen:
 #    #ip: 192.168.16.3

+ 6 - 0
huimv-farm-admin/src/main/resources/application.properties

@@ -18,3 +18,9 @@ local.farmID=1
 weight.batch_interval=4
 weight.batch.prefix=W
 weight.batchCode.spacemark=-
+
+#ÄÁ³¡É豸Êý¾ÝÊÇ·ñͬ²½(true/false)
+farm.device.sync=true
+farm.device.addService=/farm/local/device/syncAddFarmDevice
+farm.device.editService=/farm/local/device/syncEditFarmDevice
+farm.device.removeService=/farm/local/device/syncRemoveFarmDevice