ソースを参照

对数据中心端添加、编辑和删除功能的bug进行了修复。

zhuoning 3 年 前
コミット
0779222323

+ 26 - 14
huimv-farm-admin/src/main/java/com/huimv/admin/device/dao/entity/FarmDeviceEntity.java

@@ -54,6 +54,9 @@ public class FarmDeviceEntity implements Serializable {
     @Column(name = "farm_id")
     @Column(name = "farm_id")
     private Integer farmId;
     private Integer farmId;
 
 
+    @Column(name = "data_id")
+    private Long dataId;
+
     public void setId(Integer id) {
     public void setId(Integer id) {
         this.id = id;
         this.id = id;
     }
     }
@@ -166,23 +169,32 @@ public class FarmDeviceEntity implements Serializable {
         return farmId;
         return farmId;
     }
     }
 
 
+    public Long getDataId() {
+        return dataId;
+    }
+
+    public void setDataId(Long dataId) {
+        this.dataId = dataId;
+    }
+
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "FarmDeviceEntity{" +
         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 + '\'' +
+                "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 - 8
huimv-farm-admin/src/main/java/com/huimv/admin/device/service/impl/DeviceImpl.java

@@ -5,10 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.huimv.admin.device.dao.entity.FarmDeviceEntity;
 import com.huimv.admin.device.dao.entity.FarmDeviceEntity;
 import com.huimv.admin.device.dao.repo.FarmDeviceRepo;
 import com.huimv.admin.device.dao.repo.FarmDeviceRepo;
 import com.huimv.admin.device.service.IDevice;
 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.admin.device.utils.*;
 import com.huimv.common.utils.Result;
 import com.huimv.common.utils.Result;
 import com.huimv.common.utils.ResultCode;
 import com.huimv.common.utils.ResultCode;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -69,6 +66,8 @@ public class DeviceImpl implements IDevice {
      */
      */
     @Override
     @Override
     public Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) throws UnsupportedEncodingException {
     public Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) throws UnsupportedEncodingException {
+        //
+        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
         FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
         FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
         deviceEntity.setDeviceCode(deviceCode);
         deviceEntity.setDeviceCode(deviceCode);
         deviceEntity.setDeviceName(deviceName);
         deviceEntity.setDeviceName(deviceName);
@@ -80,6 +79,9 @@ public class DeviceImpl implements IDevice {
         deviceEntity.setWorker(worker);
         deviceEntity.setWorker(worker);
         deviceEntity.setState(state);
         deviceEntity.setState(state);
         deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
         deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
+        deviceEntity.setDataId(idWorker.newId());
+        deviceEntity.setDownTime(new Timestamp(new Date().getTime()));
+        deviceEntity.setLockinState(2);
         FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
         FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
         log.info("数据中心添加设备信息>>"+outFarmDeviceEntity);
         log.info("数据中心添加设备信息>>"+outFarmDeviceEntity);
         //同步牧场端设备数据
         //同步牧场端设备数据
@@ -113,13 +115,13 @@ public class DeviceImpl implements IDevice {
      */
      */
     @Override
     @Override
     public Result editDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId, Integer id) throws UnsupportedEncodingException {
     public Result editDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId, Integer id) throws UnsupportedEncodingException {
-
+        //
         Optional<FarmDeviceEntity> optional = deviceRepo.findById(id);
         Optional<FarmDeviceEntity> optional = deviceRepo.findById(id);
         if (!optional.isPresent()) {
         if (!optional.isPresent()) {
             return new Result(10001, "该记录已不存在.", false);
             return new Result(10001, "该记录已不存在.", false);
         }
         }
-        FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
-        deviceEntity.setId(id);
+        FarmDeviceEntity deviceEntity = optional.get();
+//        deviceEntity.setId(id);
         deviceEntity.setDeviceCode(deviceCode);
         deviceEntity.setDeviceCode(deviceCode);
         deviceEntity.setDeviceName(deviceName);
         deviceEntity.setDeviceName(deviceName);
         deviceEntity.setDeviceType(deviceType);
         deviceEntity.setDeviceType(deviceType);
@@ -130,6 +132,8 @@ public class DeviceImpl implements IDevice {
         deviceEntity.setWorker(worker);
         deviceEntity.setWorker(worker);
         deviceEntity.setState(state);
         deviceEntity.setState(state);
         deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
         deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
+        deviceEntity.setDownTime(new Timestamp(new Date().getTime()));
+        deviceEntity.setLockinState(2);
         FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
         FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
         log.info("数据中心添加设备信息>>"+outFarmDeviceEntity);
         log.info("数据中心添加设备信息>>"+outFarmDeviceEntity);
         //同步牧场端设备数据
         //同步牧场端设备数据
@@ -172,6 +176,8 @@ public class DeviceImpl implements IDevice {
         }
         }
         //
         //
         deviceRepo.deleteById(id);
         deviceRepo.deleteById(id);
+        FarmDeviceEntity deviceEntity = optional.get();
+
         //同步牧场端设备数据
         //同步牧场端设备数据
         if(dataSync){
         if(dataSync){
             log.info("同步牧场端设备数据。");
             log.info("同步牧场端设备数据。");
@@ -179,7 +185,7 @@ public class DeviceImpl implements IDevice {
             String farmIp = _getFarmIP(farmId);
             String farmIp = _getFarmIP(farmId);
             String serviceUrl = farmIp + deviceRemoveService;
             String serviceUrl = farmIp + deviceRemoveService;
             log.info("serviceUrl>>"+serviceUrl);
             log.info("serviceUrl>>"+serviceUrl);
-            String data = textUtil.encode(JSON.toJSONString(id));
+            String data = textUtil.encode(String.valueOf(deviceEntity.getDataId()));
             log.info("base64密文>>"+data);
             log.info("base64密文>>"+data);
             //提交请求
             //提交请求
             httpTemplete.doPostSimple(serviceUrl,data);
             httpTemplete.doPostSimple(serviceUrl,data);

+ 214 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/utils/SnowflakeIdWorker.java

@@ -0,0 +1,214 @@
+package com.huimv.admin.device.utils;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+public class SnowflakeIdWorker {
+    // ==============================Fields===========================================
+    /**
+     * 开始时间截 (2015-01-01)
+     */
+    private final long twepoch = 1420041600000L;
+    /**
+     * 机器id所占的位数
+     */
+    private final long workerIdBits = 5L;
+    /**
+     * 数据标识id所占的位数
+     */
+    private final long datacenterIdBits = 5L;
+    /**
+     * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
+     */
+    private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
+    /**
+     * 支持的最大数据标识id,结果是31
+     */
+    private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
+    /**
+     * 序列在id中占的位数
+     */
+    private final long sequenceBits = 12L;
+    /**
+     * 机器ID向左移12位
+     */
+    private final long workerIdShift = sequenceBits;
+    /**
+     * 数据标识id向左移17位(12+5)
+     */
+    private final long datacenterIdShift = sequenceBits + workerIdBits;
+    /**
+     * 时间截向左移22位(5+5+12)
+     */
+    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
+    /**
+     * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
+     */
+    private final long sequenceMask = -1L ^ (-1L << sequenceBits);
+    /**
+     * 工作机器ID(0~31)
+     */
+    private long workerId;
+    /**
+     * 数据中心ID(0~31)
+     */
+    private long datacenterId;
+    /**
+     * 毫秒内序列(0~4095)
+     */
+    private long sequence = 0L;
+    /**
+     * 上次生成ID的时间截
+     */
+    private long lastTimestamp = -1L;
+    //==============================Constructors=====================================
+
+    /**
+     * 构造函数
+     *
+     * @param workerId     工作ID (0~31)
+     * @param datacenterId 数据中心ID (0~31)
+     */
+    public SnowflakeIdWorker(long workerId, long datacenterId) {
+        if (workerId > maxWorkerId || workerId < 0) {
+            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
+        }
+        if (datacenterId > maxDatacenterId || datacenterId < 0) {
+            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
+        }
+        this.workerId = workerId;
+        this.datacenterId = datacenterId;
+    }
+
+    // ==============================Methods==========================================
+
+    /**
+     * 获得下一个ID (该方法是线程安全的)
+     *
+     * @return SnowflakeId
+     */
+    public synchronized long nextId() {
+        long timestamp = timeGen();
+
+        //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
+        if (timestamp < lastTimestamp) {
+            throw new RuntimeException(
+                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
+        }
+        //如果是同一时间生成的,则进行毫秒内序列
+        if (lastTimestamp == timestamp) {
+            sequence = (sequence + 1) & sequenceMask;
+            //毫秒内序列溢出
+            if (sequence == 0) {
+                //阻塞到下一个毫秒,获得新的时间戳
+                timestamp = tilNextMillis(lastTimestamp);
+            }
+        }
+        //时间戳改变,毫秒内序列重置
+        else {
+            sequence = 0L;
+        }
+
+        //上次生成ID的时间截
+        lastTimestamp = timestamp;
+
+        //移位并通过或运算拼到一起组成64位的ID
+        return ((timestamp - twepoch) << timestampLeftShift) //
+                | (datacenterId << datacenterIdShift) //
+                | (workerId << workerIdShift) //
+                | sequence;
+    }
+
+    /**
+     * @Method      : newId
+     * @Description : 
+     * @Params      : []
+     * @Return      : long
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/12/16       
+     * @Time        : 20:37
+     */
+    public synchronized long newId() {
+        long timestamp = timeGen();
+
+        //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
+        if (timestamp < lastTimestamp) {
+            throw new RuntimeException(
+                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
+        }
+        //如果是同一时间生成的,则进行毫秒内序列
+        if (lastTimestamp == timestamp) {
+            sequence = (sequence + 1) & sequenceMask;
+            //毫秒内序列溢出
+            if (sequence == 0) {
+                //阻塞到下一个毫秒,获得新的时间戳
+                timestamp = tilNextMillis(lastTimestamp);
+            }
+        }
+        //时间戳改变,毫秒内序列重置
+        else {
+            sequence = 0L;
+        }
+
+        //上次生成ID的时间截
+        lastTimestamp = timestamp;
+
+        //移位并通过或运算拼到一起组成64位的ID
+        return ((timestamp - twepoch) << timestampLeftShift) //
+                | (datacenterId << datacenterIdShift) //
+                | (workerId << workerIdShift) //
+                | sequence;
+    }
+
+    /**
+     * 阻塞到下一个毫秒,直到获得新的时间戳
+     *
+     * @param lastTimestamp 上次生成ID的时间截
+     * @return 当前时间戳
+     */
+    protected long tilNextMillis(long lastTimestamp) {
+        long timestamp = timeGen();
+        while (timestamp <= lastTimestamp) {
+            timestamp = timeGen();
+        }
+        return timestamp;
+    }
+
+    /**
+     * 返回以毫秒为单位的当前时间
+     *
+     * @return 当前时间(毫秒)
+     */
+    protected long timeGen() {
+        return System.currentTimeMillis();
+    }
+
+    //==============================Test=============================================
+
+    /**
+     * 测试
+     */
+    public static void main(String[] args) {
+        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
+        for (int i = 0; i < 5; i++) {
+            long id = idWorker.nextId();
+            System.out.println(Long.toBinaryString(id));
+            System.out.println(id);
+        }
+        System.out.println("");
+        SnowflakeIdWorker idWorker1 = new SnowflakeIdWorker(1, 0);
+        for (int i = 0; i < 5; i++) {
+            long id = idWorker1.nextId();
+            System.out.println(Long.toBinaryString(id));
+            System.out.println(id);
+        }
+        long abc = 921133285172576260L;
+        System.out.println("abc>>"+abc);
+    }
+}