StartHandler.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.gzlh.weighbridge.server;
  2. import cn.hutool.core.thread.ThreadUtil;
  3. import cn.hutool.extra.spring.SpringUtil;
  4. import cn.hutool.json.JSONUtil;
  5. import com.gzlh.config.ApplicationConfig;
  6. import com.gzlh.led.factory.LedFactory;
  7. import com.gzlh.led.properties.LedPropertiesConfig;
  8. import com.gzlh.task.UploadTask;
  9. import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig;
  10. import lombok.extern.slf4j.Slf4j;
  11. import java.util.Collections;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. @Slf4j
  15. public class StartHandler implements Runnable{
  16. private final ApplicationConfig applicationConfig=SpringUtil.getBean(ApplicationConfig.class);
  17. private final WeighbridgePropertiesConfig weighbridgePropertiesConfig= SpringUtil.getBean(WeighbridgePropertiesConfig.class);
  18. private final LedFactory ledFactory=SpringUtil.getBean(LedFactory.class);
  19. private final LedPropertiesConfig ledPropertiesConfig=SpringUtil.getBean(LedPropertiesConfig.class);
  20. @Override
  21. public void run() {
  22. long time=4000;
  23. log.info("start-------");
  24. while (time>=0){
  25. time=time-1000;
  26. ThreadUtil.sleep(1000);
  27. }
  28. log.info("stop-------");
  29. new Thread(CheckManager::setStop).start();
  30. List<Integer> list = CheckManager.getWeightList();
  31. log.info("list:{}", JSONUtil.toJsonStr(list));
  32. //读20个地磅数值触发上传
  33. int averagingWeight = handlerWeightList(list);
  34. log.info("weight:{}", averagingWeight);
  35. if (averagingWeight < weighbridgePropertiesConfig.getMinKg()) {
  36. return;
  37. }
  38. ledFactory.handler(ledPropertiesConfig.getBrand()).sendMsg("重量" + averagingWeight + "kg");
  39. String serverUrl= applicationConfig.getServerUrl();
  40. String channelName=applicationConfig.getChannelName();
  41. ThreadUtil.execute(new UploadTask(serverUrl, channelName, averagingWeight));
  42. }
  43. private int handlerWeightList(List<Integer> list) {
  44. if (list.isEmpty()) {
  45. return 0;
  46. }
  47. list=list.stream().sorted().collect(Collectors.toList());
  48. int size = list.size();
  49. int index = (size * 3) / 4;
  50. int weight= list.stream().collect(Collectors.averagingInt(i->i)).intValue();
  51. log.info("averaging weight:{}",weight);
  52. return list.get(index);
  53. }
  54. }