123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package com.huimv.guowei.admin.timer;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.ObjectUtil;
- 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.*;
- 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 java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- @Component
- @EnableScheduling
- public class ProcudeFeed {
- @Autowired
- private EnvRegularCallFeedingMapper feedingMapper;
- @Autowired
- private EnvRegularCallEggMapper eggMapper;
- @Autowired
- private EnvDeviceMapper deviceMapper;
- @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 * * ?")
- private void saveOnlineDevice() {
- QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("farm_id", 21).eq("device_type", 3);
- List<EnvDevice> devices = deviceMapper.selectList(queryWrapper);
- for (EnvDevice device : devices) {
- QueryWrapper<EnvRegularCallFeeding> queryWrapper1 = new QueryWrapper<>();
- queryWrapper1.eq("farm_id", 21).ge("call_date", new Date())
- .eq("call_code", device.getDeviceCode());
- FeedingVo feedingVo = feedingMapper.FeedTime(queryWrapper1);
- if (ObjectUtil.isEmpty(feedingVo) || feedingVo.getDuckWeight().compareTo(BigDecimal.ZERO) == 0) {
- QueryWrapper<BaseDuckInfo> queryWrapper2 = new QueryWrapper<>();
- queryWrapper2.eq("farm_id", 21).eq("unit_id", device.getUnitId())
- .eq("is_cage", 0);
- BaseDuckInfo duckInfo = infoMapper.selectOne(queryWrapper2);
- EnvRegularCallFeeding feeding = new EnvRegularCallFeeding();
- feeding.setCallName(device.getDeviceName());
- feeding.setCallCode(device.getDeviceCode());
- feeding.setDuckNum(duckInfo.getDuckNum());
- String num = NumberUtils.getNum(220, 230, 1);
- feeding.setDuckWeight(Double.valueOf(num));
- feeding.setDuckFeedingOriginal(0);
- feeding.setCallDate(new Date());
- feeding.setFarmId(21);
- feeding.setDuckId(duckInfo.getId());
- feeding.setBattery("8.0");
- feeding.setUnitId(device.getUnitId());
- feeding.setUnitName(device.getUnitName());
- feeding.setEventType(0);
- feeding.setChiNum(duckInfo.getChiNum());
- feeding.setJiaoNum(duckInfo.getJiaoNum());
- feeding.setBatchNum(duckInfo.getBatchNum());
- feedingMapper.insert(feeding);
- EnvRegularCallEgg egg = new EnvRegularCallEgg();
- egg.setCallName(device.getDeviceName());
- egg.setCallCode(device.getDeviceCode());
- egg.setDuckNum(duckInfo.getDuckNum());
- String num1 = NumberUtils.getNum(60, 70, 1);
- egg.setDuckWeight(Double.valueOf(num1));
- egg.setDuckFeedingOriginal(0);
- egg.setEggNum(1);
- egg.setCallDate(new Date());
- egg.setFarmId(21);
- egg.setDuckId(duckInfo.getId());
- egg.setBattery("8.0");
- egg.setUnitId(device.getUnitId());
- egg.setUnitName(device.getUnitName());
- egg.setChiNum(duckInfo.getChiNum());
- egg.setJiaoNum(duckInfo.getJiaoNum());
- egg.setBatchNum(duckInfo.getBatchNum());
- eggMapper.insert(egg);
- }
- }
- }
- }
|