瀏覽代碼

更新密码

523096025 1 年之前
父節點
當前提交
3c0ab6adf5

+ 0 - 1
huimv-admin/src/main/java/com/huimv/guowei/admin/service/impl/RawDataServiceImpl.java

@@ -232,7 +232,6 @@ public class RawDataServiceImpl extends ServiceImpl<RawDataMapper, RawData> impl
 
         String startDate = paramsMap.get("startDate");
         String endDate = paramsMap.get("endDate");
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         if (StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)) {
             startDate = DateUtil.beginOfDay(new Date()).toString();
             endDate = DateUtil.endOfDay(new Date()).toString();

+ 158 - 0
huimv-admin/src/main/java/com/huimv/guowei/admin/timer/Filter.java

@@ -0,0 +1,158 @@
+package com.huimv.guowei.admin.timer;
+
+public class Filter {
+    /*
+     * 重量单位:0.1g
+     * Threshold: 当重量波动超过该值则认为是噪声;
+     * SteadyCount: 发现噪声后,线性检测下一组稳定重量,当连续SteadyCount个稳定值后,才认为稳定;
+     * Accept:稳定评判标准,相临差值不超过Accept则为稳定。
+     * */
+    private final int Threshold, SteadyCount, Accept;
+    private final int[] Data;
+
+    public Filter(int[] data) {
+        Data = data;
+        Threshold = 10000;
+        SteadyCount = 20;
+        Accept = 200;
+        run();
+    }
+
+    public Filter(int[] data, int threshold, int count, int accept) {
+        Data = data;
+        Threshold = threshold;
+        SteadyCount = count;
+        Accept = accept;
+        run();
+    }
+    
+    public int[] getData() {
+        return Data;
+    }
+
+    public int calcWeightLoss() {
+        int pos1 = 0, weight = 0, pos2;
+        while (true) {
+            pos2 = nextSharp(pos1);
+            if (pos2 == -1) {
+                pos2 = Data.length - 1;
+                System.out.printf("data[%d]=%d, data[%d]=%d, gap=%d\n", pos1, Data[pos1], pos2, Data[pos2], Data[pos1] - Data[pos2]);
+                weight += Data[pos1] - Data[pos2];
+                return weight;
+            }
+            System.out.printf("data[%d]=%d, data[%d]=%d, gap=%d\n", pos1, Data[pos1], pos2, Data[pos2], Data[pos1] - Data[pos2]);
+            weight += Data[pos1] - Data[pos2];
+            pos1 = pos2 + 1;
+        }
+    }
+
+    private void run() {
+        int pos1 = 0, pos2;
+        while (true) {
+            pos2 = nextSharp(pos1);
+            if (pos2 == -1) {
+                update(pos1, Data.length - 1);
+                break;
+            } else {
+                update(pos1, pos2);
+                pos1 = pos2 + 1;
+            }
+        }
+    }
+
+    public void show(int start, int count) {
+        if (Data == null) {
+            System.out.println("[]");
+            return;
+        }
+        System.out.print('[');
+        while (count-- > 0) {
+            System.out.print(Data[start++]);
+            System.out.print(", ");
+        }
+        System.out.println("\b\b]");
+    }
+
+    public void show(int[] data, int start, int count) {
+        System.out.print('[');
+        while (count-- > 0) {
+            System.out.print(data[start++]);
+            System.out.print(", ");
+        }
+        System.out.println("\b\b]");
+    }
+
+    private int nextSharp(int start) {
+        if (Data == null) return -1;
+        while (start < Data.length - 1) {
+            if (Data[start + 1] - Data[start] > Threshold) {
+                return start;
+            }
+            start++;
+        }
+        return -1;
+    }
+
+    private void update(int start, int end) {
+        if (Data[end] > Data[start]) {
+            System.out.printf("fake: data[%d]=%d < data[%d]=%d\n", start, Data[start], end, Data[end]);
+            int tmp = Data[start];
+            while (start != end) Data[start++] = tmp;
+        } else {
+            float gap = (float)(Data[start] - Data[end]) / (end - start);
+            while (start != end) {
+                float liner = (int)((end - start) * gap + Data[end]);
+                if (Math.abs(Data[start] - liner) > 2000) Data[start] = (int)(liner);
+                else if (Math.abs(Data[start] - liner) > 1000) Data[start] = (int)(Data[start] * 0.5 + liner * 0.5);
+                else Data[start] = (int)(Data[start] * 0.8 + liner * 0.2);
+                start++;
+            }
+        }
+    }
+
+    /*
+     * return:
+     *   -1: no steady found
+     * */
+    private int nextSteady(int start) {
+        if (Data == null) return -1;
+        for (; start + SteadyCount <= Data.length; ++start) {
+            if (isSteady(start, start + SteadyCount - 1)) return start;
+        }
+        return -1;
+    }
+    
+    private boolean isSteady(int s, int e) {
+        if (Data == null) return false;
+        for (; s < e; ++s) {
+            if (Math.abs(Data[s] - Data[s + 1]) >= Accept) return false;
+        }
+        return true;
+    }
+    
+    /*
+    * smoothly update noise of Data[up, si)
+    * up: unstable position
+    * si: stable start index
+    * */
+    private void smoothUpdate(int up, int si) {
+        int gap = (Data[si] - Data[si + SteadyCount - 1]) / SteadyCount;
+        while (up != si) {
+            Data[up] = (si - up) * gap + Data[si];
+            up++;
+        }
+    }
+
+    /*
+    * return:
+    *   -1: no noise found
+    * other: noise in higher pos
+    * */
+    private int nextNoise(int start) {
+        while (start < Data.length - 1) {
+            if (Math.abs(Data[start] - Data[start + 1]) > Threshold) return start + 1;
+            start++;
+        }
+        return -1;
+    }
+}

+ 80 - 10
huimv-admin/src/main/java/com/huimv/guowei/admin/timer/ProcudeFeed.java

@@ -7,22 +7,14 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.huimv.guowei.admin.common.utils.NumberUtils;
 import com.huimv.guowei.admin.entity.*;
 import com.huimv.guowei.admin.entity.vo.FeedingVo;
-import com.huimv.guowei.admin.mapper.BaseDuckInfoMapper;
-import com.huimv.guowei.admin.mapper.EnvDeviceMapper;
-import com.huimv.guowei.admin.mapper.EnvRegularCallEggMapper;
-import com.huimv.guowei.admin.mapper.EnvRegularCallFeedingMapper;
-import com.huimv.guowei.admin.service.IBaseDuckInfoService;
-import com.huimv.guowei.admin.service.IEnvDeviceOnlineService;
-import com.huimv.guowei.admin.service.IEnvDeviceService;
-import com.huimv.guowei.admin.service.IRawDataService;
+import com.huimv.guowei.admin.mapper.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import javax.annotation.Resource;
 import java.math.BigDecimal;
-import java.math.RoundingMode;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
@@ -39,6 +31,84 @@ public class ProcudeFeed {
     @Autowired
     private BaseDuckInfoMapper infoMapper;
 
+    @Autowired
+    private BaseDuckInfoMapper baseDuckInfoMapper;
+    @Autowired
+    private RawDataMapper rawDataMapper;
+
+
+    //    @Scheduled(cron = "*/5 * * * * ?")
+    //生成采食记录
+    @Scheduled(cron = "12 0 * * * ?")
+    private void saveFeed() {
+
+        QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("farm_id", 21).eq("device_type", 3);
+        List<EnvDevice> devices = deviceMapper.selectList(queryWrapper);
+        DateTime dateTime = DateUtil.beginOfDay(new Date());
+        for (EnvDevice device : devices) {
+            String deviceCode = device.getDeviceCode();
+            //拿到采食记录,
+            List<EnvRegularCallFeeding> envRegularCallFeedings = feedingMapper.selectList(new QueryWrapper<EnvRegularCallFeeding>().eq("call_code", deviceCode).ge("call_date", dateTime));
+            //拿到鸭只信息
+            Integer unitId = device.getUnitId();
+            BaseDuckInfo baseDuckInfo = baseDuckInfoMapper.selectOne(new QueryWrapper<BaseDuckInfo>().eq("unit_id", unitId).eq("is_cage", 0));
+            if (ObjectUtil.isEmpty(baseDuckInfo)){
+                System.out.println("该位置不存在鸭子,数据抛弃");
+                continue;
+            }
+            //拿到原始数据
+            List<RawData> rawData = rawDataMapper.selectList(new QueryWrapper<RawData>().eq("device_code", deviceCode).ge("create_time", dateTime));
+            Double weight = 0.0;
+            if (ObjectUtil.isNotEmpty(rawData)){
+                //拿到重量
+                 weight = getWeight(rawData);
+            }
+            //拿到今天的一条数据
+            if (ObjectUtil.isEmpty(envRegularCallFeedings)){
+                EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding();
+//                envRegularCallFeeding.setBattery(batStr);
+                envRegularCallFeeding.setCallDate(new Date());
+                envRegularCallFeeding.setCallCode(deviceCode);
+                envRegularCallFeeding.setCallName(device.getDeviceName());
+                envRegularCallFeeding.setDuckId(baseDuckInfo.getId());
+                envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum());
+                envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId());
+                envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId());
+                envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName());
+                envRegularCallFeeding.setChiNum(baseDuckInfo.getChiNum());
+                envRegularCallFeeding.setJiaoNum(baseDuckInfo.getJiaoNum());
+                envRegularCallFeeding.setBatchNum(baseDuckInfo.getBatchNum());
+                envRegularCallFeeding.setDuckWeight(weight);
+                feedingMapper.insert(envRegularCallFeeding);
+            }else {
+                EnvRegularCallFeeding envRegularCallFeeding = envRegularCallFeedings.get(0);
+                envRegularCallFeeding.setDuckWeight(weight);
+                feedingMapper.updateById(envRegularCallFeeding);
+            }
+
+        }
+    }
+    private Double getWeight(List<RawData> rawDataList) {
+
+        List<Integer> list = new ArrayList();
+        for (RawData rawData : rawDataList) {
+            String weightDate = rawData.getData();
+            String[] s1 = weightDate.split(" ");
+            for (int i = 0; i < 60; i++) {
+                String str1 = s1[i * 2 + 11] + s1[i * 2 + 10];
+                Integer weight =  Integer.parseInt(str1, 16);
+                list.add(weight);
+            }
+        }
+        //获取重量算法
+        Filter filter = new Filter(list.stream()
+                .mapToInt(Integer::intValue)
+                .toArray());
+        int weightInt = filter.calcWeightLoss();
+        return  weightInt /10.0;
+
+    }
 
     //    @Scheduled(cron = "*/5 * * * * ?")
 //    @Scheduled(cron = "0 0 9 * * ?")

+ 2 - 209
input/src/main/java/com/huimv/input/server/EnvInputServerHandler.java

@@ -415,7 +415,7 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
         }
 
         //上料
-     else  if(currentTime.isAfter(liaoStartTime) && currentTime.isBefore(liaoEndTime)){
+     /*else  if(currentTime.isAfter(liaoStartTime) && currentTime.isBefore(liaoEndTime)){
             BigDecimal lastWeight2 = lastWeight;
             BigDecimal maxWeight = null;
             for (int i = 0; i < 60; i++) {
@@ -550,215 +550,8 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter   {
             rawData.setLastWeight(minWeight);
             rawDataMapper.updateById(rawData);
 
-        }
-
-
-
-
-
-//        try {
-//            BigDecimal bigDecimal3 = new BigDecimal(3);
-//            BigDecimal bigDecimala3 = new BigDecimal(-3);
-//            BigDecimal bigDecimal0 = new BigDecimal(0);
-//
-//            BigDecimal bigDecimal800 = new BigDecimal(-800);
-//            BigDecimal bigDecimal1600 = new BigDecimal(1600);
-//            BigDecimal bigDecimal2 = new BigDecimal(2);
-//            Boolean flag = true;
-            //循环获取体重
-      /*  for (int i = 0; i < 60; i++) {
-            String str1 = s[i * 2 + 11] + s[i * 2 + 10];
-            BigDecimal weight = new BigDecimal(Integer.parseInt(str1,16));
-            if (flag){
-                //当时时间
-//                Date thenTime = new Date(l - ((60-i)*10000));
-                Date thenTime = new Date(l - ((60-i)*10000));
-                //
-                String str2 = "";
-                BigDecimal weight2 ;
-                if(i<59){
-                    str2 =  s[(i+1) * 2 + 11] + s[(i+1)  * 2 + 10];
-                    weight2 =  new BigDecimal(Integer.parseInt(str2,16));
-                    BigDecimal subtract2 = weight2.subtract(lastWeight);
-                    if (subtract2.compareTo(bigDecimal800) < 0){
-                        System.out.println("连续两次减重之和小于-80,舍弃");
-                        flag = false;
-                        lastWeight  = weight;
-                        continue;
-                    }
-                }
-
-                BigDecimal subtract = weight.subtract(lastWeight);
-
-
-                if (subtract.compareTo(bigDecimal3)<0&& subtract.compareTo(bigDecimal0)>0){
-                    System.out.println("波动小于0.3g,舍弃");
-                    continue;
-                }
-                if (subtract.compareTo(bigDecimala3)>0 && subtract.compareTo(bigDecimal0)<0){
-                    System.out.println("波动小于0.3g,舍弃");
-                    continue;
-                }
-
-                int compare = subtract.compareTo(new BigDecimal(1500));
-                System.out.println(subtract +"------>"+weight+"----------->"+lastWeight);
-                EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding();
-                envRegularCallFeeding.setBattery(batStr);
-                envRegularCallFeeding.setCallDate(thenTime);
-                envRegularCallFeeding.setCallCode(decId+"");
-                envRegularCallFeeding.setCallName(envDevice.getDeviceName());
-                envRegularCallFeeding.setDuckId(baseDuckInfo.getId());
-                envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum());
-                envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId());
-                envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId());
-                envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName());
-                if (weight.compareTo(bigDecimal0 )==0){
-                    envRegularCallFeeding.setDuckFeedingOriginal(bigDecimal0);
-                }else {
-                    envRegularCallFeeding.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                }
-                if (subtract.compareTo(bigDecimal0 )==0){
-                    envRegularCallFeeding.setDuckWeight(bigDecimal0);
-                }else {
-                    envRegularCallFeeding.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                }
-
-                //加料
-                if (compare>0){
-
-                    EnvRegularCallFeedingCopy envRegularCallFeedingCopy = new EnvRegularCallFeedingCopy();
-                    envRegularCallFeedingCopy.setBattery(new BigDecimal(Integer.parseInt(bat, 16)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP).toString());
-                    envRegularCallFeedingCopy.setCallDate(thenTime);
-                    envRegularCallFeedingCopy.setCallCode(decId+"");
-                    envRegularCallFeedingCopy.setCallName(envDevice.getDeviceName());
-                    envRegularCallFeedingCopy.setDuckId(baseDuckInfo.getId());
-                    envRegularCallFeedingCopy.setDuckNum(baseDuckInfo.getDuckNum());
-                    envRegularCallFeedingCopy.setFarmId(baseDuckInfo.getFarmId());
-                    envRegularCallFeedingCopy.setUnitId(baseDuckInfo.getUnitId());
-                    envRegularCallFeedingCopy.setUnitName(baseDuckInfo.getUnitName());
-                    if (weight.compareTo(bigDecimal0)==0){
-                        envRegularCallFeedingCopy.setDuckFeedingOriginal(bigDecimal0);
-                    }else {
-                        envRegularCallFeedingCopy.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-                    if (subtract.compareTo(bigDecimal0 )==0){
-                        envRegularCallFeedingCopy.setDuckWeight(bigDecimal0);
-                    }else {
-                        envRegularCallFeedingCopy.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-
-                    envRegularCallFeedingCopy.setEventType(1);
-                    envRegularCallFeedingCopyMapper.insert(envRegularCallFeedingCopy);
-                    lastWeight =weight;
-                    continue;
-                }
-                int compare50 = subtract.compareTo(new BigDecimal(800));
-                //下蛋
-                if (compare50>0){
-                    EnvRegularCallEgg envRegularCallEgg = new EnvRegularCallEgg();
-                    envRegularCallEgg.setBattery(bat);
-                    envRegularCallEgg.setCallDate(thenTime);
-                    envRegularCallEgg.setCallCode(decId+"");
-                    envRegularCallEgg.setCallName(envDevice.getDeviceName());
-                    envRegularCallEgg.setDuckId(baseDuckInfo.getId());
-                    envRegularCallEgg.setDuckNum(baseDuckInfo.getDuckNum());
-                    envRegularCallEgg.setFarmId(baseDuckInfo.getFarmId());
-                    envRegularCallEgg.setUnitId(baseDuckInfo.getUnitId());
-                    envRegularCallEgg.setUnitName(baseDuckInfo.getUnitName());
-                    envRegularCallEgg.setEggNum(1);
-                    if (weight.compareTo(bigDecimal0)==0){
-                        envRegularCallEgg.setDuckFeedingOriginal(bigDecimal0);
-                    }else {
-                        envRegularCallEgg.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-                    if (subtract.compareTo(bigDecimal0 )==0){
-                        envRegularCallEgg.setDuckWeight(bigDecimal0);
-                    }else {
-                        envRegularCallEgg.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-                    envRegularCallEggMapper.insert(envRegularCallEgg);
-                    lastWeight =weight;
-                    continue;
-                }
+        }*/
 
-                int compare1600 = subtract.compareTo(bigDecimal1600);
-                //两枚下蛋
-                if (compare1600>0){
-                    EnvRegularCallEgg envRegularCallEgg = new EnvRegularCallEgg();
-                    envRegularCallEgg.setBattery(bat);
-                    envRegularCallEgg.setCallDate(thenTime);
-                    envRegularCallEgg.setCallCode(decId+"");
-                    envRegularCallEgg.setCallName(envDevice.getDeviceName());
-                    envRegularCallEgg.setDuckId(baseDuckInfo.getId());
-                    envRegularCallEgg.setDuckNum(baseDuckInfo.getDuckNum());
-                    envRegularCallEgg.setFarmId(baseDuckInfo.getFarmId());
-                    envRegularCallEgg.setUnitId(baseDuckInfo.getUnitId());
-                    envRegularCallEgg.setUnitName(baseDuckInfo.getUnitName());
-                    envRegularCallEgg.setEggNum(2);
-                    if (weight.compareTo(bigDecimal0)==0){
-                        envRegularCallEgg.setDuckFeedingOriginal(bigDecimal0);
-                    }else {
-                        envRegularCallEgg.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-                    if (subtract.compareTo(bigDecimal0 )==0){
-                        envRegularCallEgg.setDuckWeight(bigDecimal0);
-                    }else {
-                        envRegularCallEgg.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                    }
-                    envRegularCallEggMapper.insert(envRegularCallEgg);
-                    lastWeight =weight;
-                    continue;
-                }
-
-                int compare0 = subtract.compareTo(bigDecimal0);
-                //小于零大于负八十
-                int compare80 = subtract.compareTo(bigDecimal800);
-                //吃料
-                if (compare0<0 && compare80 >=0){
-                    if (subtract.compareTo(bigDecimal0 )==0){
-                        envRegularCallFeeding.setDuckWeight(bigDecimal0);
-                    }else {
-                        envRegularCallFeeding.setDuckWeight(subtract.subtract(subtract.multiply(bigDecimal2)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP));
-                    }
-                    envRegularCallFeeding.setEventType(0);
-                    envRegularCallFeedingMapper.insert(envRegularCallFeeding);
-                    lastWeight =weight;
-                    continue;
-                }
-
-                //其它
-                EnvRegularCallFeedingCopy envRegularCallFeedingCopy = new EnvRegularCallFeedingCopy();
-                envRegularCallFeedingCopy.setBattery(new BigDecimal(Integer.parseInt(bat, 16)).divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP).toString());
-                envRegularCallFeedingCopy.setCallDate(thenTime);
-                envRegularCallFeedingCopy.setCallCode(decId+"");
-                envRegularCallFeedingCopy.setCallName(envDevice.getDeviceName());
-                envRegularCallFeedingCopy.setDuckId(baseDuckInfo.getId());
-                envRegularCallFeedingCopy.setDuckNum(baseDuckInfo.getDuckNum());
-                envRegularCallFeedingCopy.setFarmId(baseDuckInfo.getFarmId());
-                envRegularCallFeedingCopy.setUnitId(baseDuckInfo.getUnitId());
-                envRegularCallFeedingCopy.setUnitName(baseDuckInfo.getUnitName());
-                if (weight.compareTo(bigDecimal0 )==0){
-                    envRegularCallFeedingCopy.setDuckFeedingOriginal(bigDecimal0);
-                }else {
-                    envRegularCallFeedingCopy.setDuckFeedingOriginal(weight.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                }
-                if (subtract.compareTo(bigDecimal0 )==0){
-                    envRegularCallFeedingCopy.setDuckWeight(bigDecimal0);
-                }else {
-                    envRegularCallFeedingCopy.setDuckWeight(subtract.divide(bigDecimal10, 1, BigDecimal.ROUND_HALF_UP) );
-                }
-
-                envRegularCallFeedingCopy.setEventType(2);
-                envRegularCallFeedingCopyMapper.insert(envRegularCallFeedingCopy);
-                continue;
-            }
-            lastWeight  = weight;
-            flag = true;
-        }
-        }catch (Exception e){
-            System.out.println(e);
-        }
-*/
     }