ProcudeFeed.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.huimv.guowei.admin.timer;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.huimv.guowei.admin.common.utils.NumberUtils;
  7. import com.huimv.guowei.admin.entity.*;
  8. import com.huimv.guowei.admin.entity.vo.FeedingVo;
  9. import com.huimv.guowei.admin.mapper.*;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.scheduling.annotation.EnableScheduling;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import java.math.BigDecimal;
  15. import java.util.ArrayList;
  16. import java.util.Date;
  17. import java.util.List;
  18. @Component
  19. @EnableScheduling
  20. public class ProcudeFeed {
  21. @Autowired
  22. private EnvRegularCallFeedingMapper feedingMapper;
  23. @Autowired
  24. private EnvRegularCallEggMapper eggMapper;
  25. @Autowired
  26. private EnvDeviceMapper deviceMapper;
  27. @Autowired
  28. private BaseDuckInfoMapper infoMapper;
  29. @Autowired
  30. private BaseDuckInfoMapper baseDuckInfoMapper;
  31. @Autowired
  32. private RawDataMapper rawDataMapper;
  33. // @Scheduled(cron = "*/5 * * * * ?")
  34. //生成采食记录
  35. @Scheduled(cron = "12 0 * * * ?")
  36. private void saveFeed() {
  37. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  38. queryWrapper.eq("farm_id", 21).eq("device_type", 3);
  39. List<EnvDevice> devices = deviceMapper.selectList(queryWrapper);
  40. DateTime dateTime = DateUtil.beginOfDay(new Date());
  41. for (EnvDevice device : devices) {
  42. String deviceCode = device.getDeviceCode();
  43. //拿到采食记录,
  44. List<EnvRegularCallFeeding> envRegularCallFeedings = feedingMapper.selectList(new QueryWrapper<EnvRegularCallFeeding>().eq("call_code", deviceCode).ge("call_date", dateTime));
  45. //拿到鸭只信息
  46. Integer unitId = device.getUnitId();
  47. BaseDuckInfo baseDuckInfo = baseDuckInfoMapper.selectOne(new QueryWrapper<BaseDuckInfo>().eq("unit_id", unitId).eq("is_cage", 0));
  48. if (ObjectUtil.isEmpty(baseDuckInfo)){
  49. System.out.println("该位置不存在鸭子,数据抛弃");
  50. continue;
  51. }
  52. //拿到原始数据
  53. List<RawData> rawData = rawDataMapper.selectList(new QueryWrapper<RawData>().eq("device_code", deviceCode).ge("create_time", dateTime));
  54. Double weight = 0.0;
  55. if (ObjectUtil.isNotEmpty(rawData)){
  56. //拿到重量
  57. weight = getWeight(rawData);
  58. }
  59. //拿到今天的一条数据
  60. if (ObjectUtil.isEmpty(envRegularCallFeedings)){
  61. EnvRegularCallFeeding envRegularCallFeeding = new EnvRegularCallFeeding();
  62. // envRegularCallFeeding.setBattery(batStr);
  63. envRegularCallFeeding.setCallDate(new Date());
  64. envRegularCallFeeding.setCallCode(deviceCode);
  65. envRegularCallFeeding.setCallName(device.getDeviceName());
  66. envRegularCallFeeding.setDuckId(baseDuckInfo.getId());
  67. envRegularCallFeeding.setDuckNum(baseDuckInfo.getDuckNum());
  68. envRegularCallFeeding.setFarmId(baseDuckInfo.getFarmId());
  69. envRegularCallFeeding.setUnitId(baseDuckInfo.getUnitId());
  70. envRegularCallFeeding.setUnitName(baseDuckInfo.getUnitName());
  71. envRegularCallFeeding.setChiNum(baseDuckInfo.getChiNum());
  72. envRegularCallFeeding.setJiaoNum(baseDuckInfo.getJiaoNum());
  73. envRegularCallFeeding.setBatchNum(baseDuckInfo.getBatchNum());
  74. envRegularCallFeeding.setDuckWeight(weight);
  75. feedingMapper.insert(envRegularCallFeeding);
  76. }else {
  77. EnvRegularCallFeeding envRegularCallFeeding = envRegularCallFeedings.get(0);
  78. envRegularCallFeeding.setDuckWeight(weight);
  79. feedingMapper.updateById(envRegularCallFeeding);
  80. }
  81. }
  82. }
  83. private Double getWeight(List<RawData> rawDataList) {
  84. List<Integer> list = new ArrayList();
  85. for (RawData rawData : rawDataList) {
  86. String weightDate = rawData.getData();
  87. String[] s1 = weightDate.split(" ");
  88. for (int i = 0; i < 60; i++) {
  89. String str1 = s1[i * 2 + 11] + s1[i * 2 + 10];
  90. Integer weight = Integer.parseInt(str1, 16);
  91. list.add(weight);
  92. }
  93. }
  94. //获取重量算法
  95. Filter filter = new Filter(list.stream()
  96. .mapToInt(Integer::intValue)
  97. .toArray());
  98. int weightInt = filter.calcWeightLoss();
  99. return weightInt /10.0;
  100. }
  101. // @Scheduled(cron = "*/5 * * * * ?")
  102. // @Scheduled(cron = "0 0 9 * * ?")
  103. private void saveOnlineDevice() {
  104. QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
  105. queryWrapper.eq("farm_id", 21).eq("device_type", 3);
  106. List<EnvDevice> devices = deviceMapper.selectList(queryWrapper);
  107. for (EnvDevice device : devices) {
  108. QueryWrapper<EnvRegularCallFeeding> queryWrapper1 = new QueryWrapper<>();
  109. queryWrapper1.eq("farm_id", 21).ge("call_date", new Date())
  110. .eq("call_code", device.getDeviceCode());
  111. FeedingVo feedingVo = feedingMapper.FeedTime(queryWrapper1);
  112. if (ObjectUtil.isEmpty(feedingVo) || feedingVo.getDuckWeight().compareTo(BigDecimal.ZERO) == 0) {
  113. QueryWrapper<BaseDuckInfo> queryWrapper2 = new QueryWrapper<>();
  114. queryWrapper2.eq("farm_id", 21).eq("unit_id", device.getUnitId())
  115. .eq("is_cage", 0);
  116. BaseDuckInfo duckInfo = infoMapper.selectOne(queryWrapper2);
  117. EnvRegularCallFeeding feeding = new EnvRegularCallFeeding();
  118. feeding.setCallName(device.getDeviceName());
  119. feeding.setCallCode(device.getDeviceCode());
  120. feeding.setDuckNum(duckInfo.getDuckNum());
  121. String num = NumberUtils.getNum(220, 230, 1);
  122. feeding.setDuckWeight(Double.valueOf(num));
  123. feeding.setDuckFeedingOriginal(0);
  124. feeding.setCallDate(new Date());
  125. feeding.setFarmId(21);
  126. feeding.setDuckId(duckInfo.getId());
  127. feeding.setBattery("8.0");
  128. feeding.setUnitId(device.getUnitId());
  129. feeding.setUnitName(device.getUnitName());
  130. feeding.setEventType(0);
  131. feeding.setChiNum(duckInfo.getChiNum());
  132. feeding.setJiaoNum(duckInfo.getJiaoNum());
  133. feeding.setBatchNum(duckInfo.getBatchNum());
  134. feedingMapper.insert(feeding);
  135. EnvRegularCallEgg egg = new EnvRegularCallEgg();
  136. egg.setCallName(device.getDeviceName());
  137. egg.setCallCode(device.getDeviceCode());
  138. egg.setDuckNum(duckInfo.getDuckNum());
  139. String num1 = NumberUtils.getNum(60, 70, 1);
  140. egg.setDuckWeight(Double.valueOf(num1));
  141. egg.setDuckFeedingOriginal(0);
  142. egg.setEggNum(1);
  143. egg.setCallDate(new Date());
  144. egg.setFarmId(21);
  145. egg.setDuckId(duckInfo.getId());
  146. egg.setBattery("8.0");
  147. egg.setUnitId(device.getUnitId());
  148. egg.setUnitName(device.getUnitName());
  149. egg.setChiNum(duckInfo.getChiNum());
  150. egg.setJiaoNum(duckInfo.getJiaoNum());
  151. egg.setBatchNum(duckInfo.getBatchNum());
  152. eggMapper.insert(egg);
  153. }
  154. }
  155. }
  156. }