浏览代码

牧场端增加设备信息同步功能:新增设备数据

zhuoning 3 年之前
父节点
当前提交
7faf832340

+ 17 - 0
huimv-farm-device/huimv-farm-business/pom.xml

@@ -27,5 +27,22 @@
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
+        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.5</version>
+        </dependency>
+        <!-- huimv common -->
+<!--        <dependency>-->
+<!--            <groupId>com.huimv</groupId>-->
+<!--            <artifactId>huimv-common</artifactId>-->
+<!--            <version>0.0.1-SNAPSHOT</version>-->
+<!--        </dependency>-->
+        <dependency>
+            <groupId>com.huimv</groupId>
+            <artifactId>huimv-common</artifactId>
+            <version>0.0.1</version>
+        </dependency>
     </dependencies>
 </project>

+ 0 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/mobile/controller/readme


+ 0 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/mobile/service/readme


+ 81 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/server/controller/DeviceController.java

@@ -0,0 +1,81 @@
+package com.huimv.business.server.controller;
+
+import com.huimv.business.server.service.IDevice;
+import com.huimv.business.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.web.bind.annotation.*;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@CrossOrigin
+@RestController
+@Slf4j
+@RequestMapping(value = "/farm/local/device")
+public class DeviceController {
+    @Autowired
+    private IDevice iDevice;
+    /**
+     * @Method      : syncFarmDeviceInfo
+     * @Description : 数据中心操作同步牧场设备数据
+     * @Params      : [data]
+     * @Return      : com.huimv.common.utils.Result
+     *
+     * @Author      : ZhuoNing
+     * @Date        : 2021/12/15
+     * @Time        : 16:54
+     */
+    @RequestMapping(value = "/syncAddFarmDevice",method = RequestMethod.POST)
+    public Result syncFarmDeviceInfo(@RequestBody String data) throws UnsupportedEncodingException {
+        log.info("客户端接收到数据>>"+data);
+        //
+        return iDevice.syncAddFarmDevice(data);
+    }
+
+    /**
+     * @Method      : syncEditFarmDevice
+     * @Description : 同步编辑牧场设备数据
+     * @Params      : [data]
+     * @Return      : com.huimv.common.utils.Result
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/12/15       
+     * @Time        : 19:22
+     */
+    @RequestMapping(value = "/syncEditFarmDevice",method = RequestMethod.POST)
+    public Result syncEditFarmDevice(@RequestBody String data){
+        System.out.println("客户端接收到数据data>>"+data);
+        //
+        return iDevice.syncEditFarmDevice(data);
+    }
+
+    /**
+     * @Method      : syncRemoveFarmDevice
+     * @Description : 同步删除牧场设备数据
+     * @Params      : [data]
+     * @Return      : com.huimv.common.utils.Result
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/12/15       
+     * @Time        : 19:22
+     */
+    @RequestMapping(value = "/syncRemoveFarmDevice",method = RequestMethod.POST)
+    public Result syncRemoveFarmDevice(@RequestBody String data){
+        System.out.println("客户端接收到数据data>>"+data);
+        //
+        return iDevice.syncRemoveFarmDevice(data);
+    }
+
+
+
+}

+ 200 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/server/dao/entity/FarmDeviceEntity.java

@@ -0,0 +1,200 @@
+package com.huimv.business.server.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "farm_device")
+public class FarmDeviceEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "device_name")
+    private String deviceName;
+
+    @Column(name = "device_code")
+    private String deviceCode;
+
+    @Column(name = "device_type")
+    private String deviceType;
+
+    @Column(name = "factory")
+    private String factory;
+
+    @Column(name = "worker")
+    private String worker;
+
+    @Column(name = "main_params")
+    private String mainParams;
+
+    @Column(name = "state")
+    private Integer state;
+
+    @Column(name = "record")
+    private String record;
+
+    @Column(name = "last_time")
+    private Timestamp lastTime;
+
+    @Column(name = "up_time")
+    private Timestamp upTime;
+
+    @Column(name = "down_time")
+    private Timestamp downTime;
+
+    @Column(name = "lockin_state")
+    private Integer lockinState;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    @Column(name = "data_id")
+    private Integer dataId;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setDeviceName(String deviceName) {
+        this.deviceName = deviceName;
+    }
+
+    public String getDeviceName() {
+        return deviceName;
+    }
+
+    public void setDeviceCode(String deviceCode) {
+        this.deviceCode = deviceCode;
+    }
+
+    public String getDeviceCode() {
+        return deviceCode;
+    }
+
+    public void setDeviceType(String deviceType) {
+        this.deviceType = deviceType;
+    }
+
+    public String getDeviceType() {
+        return deviceType;
+    }
+
+    public void setFactory(String factory) {
+        this.factory = factory;
+    }
+
+    public String getFactory() {
+        return factory;
+    }
+
+    public void setWorker(String worker) {
+        this.worker = worker;
+    }
+
+    public String getWorker() {
+        return worker;
+    }
+
+    public void setMainParams(String mainParams) {
+        this.mainParams = mainParams;
+    }
+
+    public String getMainParams() {
+        return mainParams;
+    }
+
+    public void setState(Integer state) {
+        this.state = state;
+    }
+
+    public Integer getState() {
+        return state;
+    }
+
+    public void setRecord(String record) {
+        this.record = record;
+    }
+
+    public String getRecord() {
+        return record;
+    }
+
+    public void setLastTime(Timestamp lastTime) {
+        this.lastTime = lastTime;
+    }
+
+    public Timestamp getLastTime() {
+        return lastTime;
+    }
+
+    public void setUpTime(Timestamp upTime) {
+        this.upTime = upTime;
+    }
+
+    public Timestamp getUpTime() {
+        return upTime;
+    }
+
+    public void setDownTime(Timestamp downTime) {
+        this.downTime = downTime;
+    }
+
+    public Timestamp getDownTime() {
+        return downTime;
+    }
+
+    public void setLockinState(Integer lockinState) {
+        this.lockinState = lockinState;
+    }
+
+    public Integer getLockinState() {
+        return lockinState;
+    }
+
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    public void setDataId(Integer dataId) {
+        this.dataId = dataId;
+    }
+
+    public Integer getDataId() {
+        return dataId;
+    }
+
+    @Override
+    public String toString() {
+        return "FarmDeviceEntity{" +
+                "id=" + id + '\'' +
+                "deviceName=" + deviceName + '\'' +
+                "deviceCode=" + deviceCode + '\'' +
+                "deviceType=" + deviceType + '\'' +
+                "factory=" + factory + '\'' +
+                "worker=" + worker + '\'' +
+                "mainParams=" + mainParams + '\'' +
+                "state=" + state + '\'' +
+                "record=" + record + '\'' +
+                "lastTime=" + lastTime + '\'' +
+                "upTime=" + upTime + '\'' +
+                "downTime=" + downTime + '\'' +
+                "lockinState=" + lockinState + '\'' +
+                "farmId=" + farmId + '\'' +
+                "dataId=" + dataId + '\'' +
+                '}';
+    }
+}

+ 14 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/server/dao/repo/FarmDeviceRepo.java

@@ -0,0 +1,14 @@
+package com.huimv.business.server.dao.repo;
+
+import com.huimv.business.server.dao.entity.FarmDeviceEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface FarmDeviceRepo extends JpaRepository<FarmDeviceEntity, Integer>, JpaSpecificationExecutor<FarmDeviceEntity> {
+    //
+    @Query(nativeQuery = true,value = "SELECT id FROM farm_device WHERE data_id=?1")
+    List<Object[]> getDevice(Integer dataId);
+}

+ 16 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/server/service/IDevice.java

@@ -0,0 +1,16 @@
+package com.huimv.business.server.service;
+
+import com.huimv.common.utils.Result;
+
+import java.io.UnsupportedEncodingException;
+
+public interface IDevice {
+    //同步添加牧场设备数据
+    Result syncAddFarmDevice(String deviceData) throws UnsupportedEncodingException;
+
+    //同步编辑牧场设备数据
+    Result syncEditFarmDevice(String deviceData);
+
+    //同步删除牧场设备数据
+    Result syncRemoveFarmDevice(String data);
+}

+ 110 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/server/service/impl/DeviceImpl.java

@@ -0,0 +1,110 @@
+package com.huimv.business.server.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.business.server.dao.entity.FarmDeviceEntity;
+import com.huimv.business.server.dao.repo.FarmDeviceRepo;
+import com.huimv.business.server.service.IDevice;
+import com.huimv.business.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.stereotype.Service;
+
+import java.io.UnsupportedEncodingException;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.util.List;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+@Slf4j
+public class DeviceImpl implements IDevice {
+    @Autowired
+    private FarmDeviceRepo deviceRepo;
+    @Autowired
+    private TextUtil textUtil;
+
+    @Override
+    public Result syncAddFarmDevice(String deviceData) throws UnsupportedEncodingException {
+        deviceData = deviceData.replace("data=","");
+        deviceData = textUtil.decode(deviceData.replace("%3D","="));
+        JSONObject deviceDataJo = JSON.parseObject(deviceData);
+//        String encodedText = deviceDataJo.getString("data");
+//        System.out.println("encodedText>>"+encodedText);
+        FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
+        deviceEntity.setDataId(deviceDataJo.getInteger("id"));
+        deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
+        deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
+        deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
+        deviceEntity.setDownTime(new Timestamp(new  java.util.Date().getTime()));
+        deviceEntity.setFactory(deviceDataJo.getString("factory"));
+        deviceEntity.setFarmId(deviceDataJo.getInteger("farmId"));
+        deviceEntity.setLastTime(new Timestamp(new  java.util.Date().getTime()));
+        deviceEntity.setLockinState(2);
+        deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
+        deviceEntity.setRecord(deviceDataJo.getString("record"));
+        deviceEntity.setState(deviceDataJo.getInteger("state"));
+        deviceEntity.setWorker(deviceDataJo.getString("worker"));
+        FarmDeviceEntity addEntity = deviceRepo.saveAndFlush(deviceEntity);
+        log.info("同步牧场端设备数据>>"+addEntity);
+        return new Result(ResultCode.SUCCESS,addEntity);
+    }
+
+    @Override
+    public Result syncEditFarmDevice(String deviceData){
+        JSONObject deviceDataJo = JSON.parseObject(deviceData);
+        Integer dataId = deviceDataJo.getInteger("id");
+        //查询目标设备数据
+        List<Object[]> existList = deviceRepo.getDevice(dataId);
+        if(existList.size()>0) {
+            Object[] object = existList.get(0);
+            Integer id = (Integer) object[0];
+            //更新目标设备数据
+            FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
+            deviceEntity.setId(id);
+            deviceEntity.setDataId(deviceDataJo.getInteger("id"));
+            deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
+            deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
+            deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
+            deviceEntity.setDownTime(new Timestamp(new java.util.Date().getTime()));
+            deviceEntity.setFactory(deviceDataJo.getString("factory"));
+            deviceEntity.setFarmId(deviceDataJo.getInteger("farmId"));
+            deviceEntity.setLastTime(new Timestamp(new java.util.Date().getTime()));
+            deviceEntity.setLockinState(2);
+            deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
+            deviceEntity.setRecord(deviceDataJo.getString("record"));
+            deviceEntity.setState(deviceDataJo.getInteger("state"));
+            deviceEntity.setWorker(deviceDataJo.getString("worker"));
+            FarmDeviceEntity editEntity = deviceRepo.saveAndFlush(deviceEntity);
+            log.info("编辑后的设备数据>>" + editEntity);
+            return new Result(ResultCode.SUCCESS,editEntity);
+        }else{
+            return new Result(ResultCode.SUCCESS);
+        }
+    }
+
+    @Override
+    public Result syncRemoveFarmDevice(String deviceData){
+        JSONObject deviceDataJo = JSON.parseObject(deviceData);
+        Integer dataId = deviceDataJo.getInteger("dataId");
+        //查询目标设备数据
+        List<Object[]> existList = deviceRepo.getDevice(dataId);
+        if(existList.size()>0) {
+            Object[] object = existList.get(0);
+            Integer id = (Integer) object[0];
+            deviceRepo.deleteById(id);
+            return new Result(ResultCode.SUCCESS);
+        }else{
+            return new Result(10001,"暂无删除设备数据.",false);
+        }
+    }
+}

+ 516 - 0
huimv-farm-device/huimv-farm-business/src/main/java/com/huimv/business/utils/HttpTemplete.java

@@ -0,0 +1,516 @@
+package com.huimv.business.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());
+        }
+    }
+
+
+    /**
+     * @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-device/huimv-farm-business/src/main/java/com/huimv/business/utils/TextUtil.java

@@ -0,0 +1,31 @@
+package com.huimv.business.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");
+    }
+}

+ 31 - 0
huimv-farm-device/huimv-farm-business/src/test/java/com/huimv/business/utils/TextTest.java

@@ -0,0 +1,31 @@
+package com.huimv.business.utils;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@SpringBootTest
+public class TextTest {
+    @Autowired
+    private TextUtil textUtil;
+    @Test
+    public void test() throws UnsupportedEncodingException {
+        String text = "\n" +
+                "{\"deviceCode\":\"deviceCode\",\"deviceName\":\"deviceName\",\"deviceType\":\"deviceType\",\"factory\":\"factory\",\"farmId\":1,\"id\":22,\"lastTime\":1639575081406,\"mainParams\":\"mainParams\",\"record\":\"record\",\"state\":1,\"worker\":\"worker\"}";
+        System.out.println("明文:"+text);
+        String encodedText = textUtil.encode(text);
+        System.out.println("编码后:"+encodedText);
+        String decodedText = textUtil.decode(encodedText);
+        System.out.println("解码后:"+decodedText);
+    }
+}