package com.gzlh.weighbridge.server; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.extra.spring.SpringUtil; import cn.hutool.json.JSONUtil; import com.gzlh.config.ApplicationConfig; import com.gzlh.led.factory.LedFactory; import com.gzlh.led.properties.LedPropertiesConfig; import com.gzlh.task.UploadTask; import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig; import lombok.extern.slf4j.Slf4j; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @Slf4j public class StartHandler implements Runnable{ private final ApplicationConfig applicationConfig=SpringUtil.getBean(ApplicationConfig.class); private final WeighbridgePropertiesConfig weighbridgePropertiesConfig= SpringUtil.getBean(WeighbridgePropertiesConfig.class); private final LedFactory ledFactory=SpringUtil.getBean(LedFactory.class); private final LedPropertiesConfig ledPropertiesConfig=SpringUtil.getBean(LedPropertiesConfig.class); @Override public void run() { long time=4000; log.info("start-------"); while (time>=0){ time=time-1000; ThreadUtil.sleep(1000); } log.info("stop-------"); new Thread(CheckManager::setStop).start(); List list = CheckManager.getWeightList(); log.info("list:{}", JSONUtil.toJsonStr(list)); //读20个地磅数值触发上传 int averagingWeight = handlerWeightList(list); log.info("weight:{}", averagingWeight); if (averagingWeight < weighbridgePropertiesConfig.getMinKg()) { return; } ledFactory.handler(ledPropertiesConfig.getBrand()).sendMsg("重量" + averagingWeight + "kg"); String serverUrl= applicationConfig.getServerUrl(); String channelName=applicationConfig.getChannelName(); ThreadUtil.execute(new UploadTask(serverUrl, channelName, averagingWeight)); } private int handlerWeightList(List list) { if (list.isEmpty()) { return 0; } list=list.stream().sorted().collect(Collectors.toList()); int size = list.size(); int index = (size * 3) / 4; int weight= list.stream().collect(Collectors.averagingInt(i->i)).intValue(); log.info("averaging weight:{}",weight); return list.get(index); } }