123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package com.gzlh.device.weighbridge.handler.impl;
- import cn.hutool.core.thread.ThreadUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import cn.hutool.json.JSONUtil;
- import cn.hutool.log.StaticLog;
- import com.gzlh.bus.ChannelCacheManager;
- import com.gzlh.bus.SysConfig;
- import com.gzlh.bus.EventDataManager;
- import com.gzlh.bus.EventBus;
- import com.gzlh.config.SystemObject;
- import com.gzlh.config.dto.SerialSetting;
- import com.gzlh.device.led.utils.LedOptions;
- import com.gzlh.device.weighbridge.event.WeighbridgeEvent;
- import com.gzlh.utils.DeviceCache;
- import lombok.extern.slf4j.Slf4j;
- import java.util.*;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- @Slf4j
- public class CommonWeighbridgeHandler {
- private final static List<Integer> WEIGHT_CACHE = new CopyOnWriteArrayList<>();
- public static boolean isStart = false;
- public static boolean hsCar = false;
- /**
- * 添加地磅数据到缓存
- *
- * @param weight 地磅数
- */
- public static void cacheWeight(int weight) {
- if (!isStart) {
- WEIGHT_CACHE.clear();
- return;
- }
- WEIGHT_CACHE.add(weight);
- }
- /**
- * 开始读取地磅数据
- */
- public static void startToRead() {
- //不启用
- if (!ChannelCacheManager.stateEnable(SysConfig.channelSetting.getChannelCode())) {
- return;
- }
- isStart = true;
- ThreadUtil.execute(new WatchThread());
- }
- /**
- * 监控地磅缓存是否读取到了数据
- * 监控2秒内的地磅数
- */
- static class WatchThread implements Runnable {
- private int time = 5000;
- @Override
- public void run() {
- int averaWeight = 0;
- SerialSetting serialSetting = SysConfig.serialSetting;
- while (time > 0) {
- if (!isStart) {
- return;
- }
- time = time - 1000;
- // 检查前后红外和雷达的触发情况
- // deviceStatus = new StringBuffer(DeviceCache.getPlcStatus()).reverse().toString();
- ThreadUtil.sleep(1000);
- }
- isStart = false;
- averaWeight = handlerWeight();
- log.info("----------weight---------:{}", averaWeight);
- EventDataManager.cacheData("weight", averaWeight);
- }
- private int handlerWeight() {
- List<Integer> list = WEIGHT_CACHE;
- log.info("w:{}", JSONUtil.toJsonStr(list));
- if (list.isEmpty()) {
- return 0;
- }
- int size = list.size();
- int index = (size * 3) / 4;
- return list.get(index);
- }
- public static int findClosestWithHighestFrequency(List<Integer> numbers) {
- if (numbers == null || numbers.size() == 0) {
- throw new IllegalArgumentException("数组不能为空");
- }
- // 计算平均值
- double sum = 0;
- for (int number : numbers) {
- sum += number;
- }
- double average = sum / numbers.size();
- // 使用Map存储每个数字的出现次数
- Map<Integer, Integer> frequencyMap = new HashMap<>();
- // 使用Map存储每个数字与平均值的误差
- Map<Integer, Double> differenceMap = new HashMap<>();
- // 计算每个数字的频率和误差
- for (int number : numbers) {
- frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1);
- differenceMap.put(number, Math.abs(number - average));
- }
- int result = numbers.get(0);
- double minDifference = Double.MAX_VALUE;
- int maxFrequency = 0;
- // 遍历所有不同的数字
- for (int number : frequencyMap.keySet()) {
- double currentDifference = differenceMap.get(number);
- int currentFrequency = frequencyMap.get(number);
- // 如果找到误差更小的数字
- if (currentDifference < minDifference) {
- minDifference = currentDifference;
- maxFrequency = currentFrequency;
- result = number;
- }
- // 如果误差相同,比较频率
- else if (currentDifference == minDifference && currentFrequency > maxFrequency) {
- maxFrequency = currentFrequency;
- result = number;
- }
- }
- return result;
- }
- }
- public static void main(String[] args) {
- String str = "51200,51180,51100,51160,51140,51160,51160";
- List<Integer> list = StrUtil.split(str, ",").stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
- System.out.println(list.stream().collect(Collectors.averagingLong(Integer::intValue)));
- System.out.println(WatchThread.findClosestWithHighestFrequency(list));
- }
- }
|