现场调度修改
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package com.ruoyi.schedule.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CheckWeight {
|
||||
|
||||
@@ -15,15 +19,16 @@ public class CheckWeight {
|
||||
Socket socket = new Socket(ip, por);
|
||||
InputStream is = socket.getInputStream();
|
||||
) {
|
||||
Thread.sleep(100);
|
||||
byte[] bytes = new byte[10];
|
||||
Thread.sleep(1000);
|
||||
byte[] bytes = new byte[100];
|
||||
int read = is.read(bytes);
|
||||
if (read!=-1){
|
||||
String weight = byteArrayToCharArray(bytes, "UTF-8");
|
||||
weight = weight.substring(1,8);
|
||||
weight = weight.replace("=","");
|
||||
String[] weightList = byteArrayToCharArray(bytes, "UTF-8").split("\r\n");
|
||||
System.out.println(Arrays.toString(weightList));
|
||||
String weight = getMostFrequentByHutool(weightList);
|
||||
weight = weight.replace("=","").replace("\r","").replace("\n","");
|
||||
|
||||
if (weight.length() == 7 && !weight.equals("0000000")) {
|
||||
if (!weight.equals("0000000")) {
|
||||
zhi = weight;
|
||||
}
|
||||
}
|
||||
@@ -34,6 +39,41 @@ public class CheckWeight {
|
||||
return zhi;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用 Hutool 找出数组中出现次数最多的字符串
|
||||
* @param strArray 目标字符串数组
|
||||
* @return 出现次数最多的字符串(空数组返回 null,多最大值返回第一个)
|
||||
*/
|
||||
/**
|
||||
* 兼容所有 Hutool 版本:找出数组中出现次数最多的字符串
|
||||
* @param strArray 目标字符串数组
|
||||
* @return 出现次数最多的字符串(空数组返回 null,多最大值返回第一个)
|
||||
*/
|
||||
public static String getMostFrequentByHutool(String[] strArray) {
|
||||
// 1. 空值校验 + 数组转 List
|
||||
if (strArray == null || strArray.length == 0) {
|
||||
return null;
|
||||
}
|
||||
List<String> strList = CollUtil.newArrayList(strArray);
|
||||
|
||||
// 2. 用 Hutool 统计频次(所有版本都支持)
|
||||
Map<String, Integer> countMap = CollUtil.countMap(strList);
|
||||
|
||||
// 3. 手动遍历找最大值(替代不存在的 getMaxKey 方法)
|
||||
String mostFrequent = null;
|
||||
int maxCount = 0;
|
||||
// 遍历 Map 条目,Hutool 5.x 及以上都支持 entrySet 遍历
|
||||
for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
|
||||
if (entry.getValue() > maxCount) {
|
||||
maxCount = entry.getValue();
|
||||
mostFrequent = entry.getKey();
|
||||
}
|
||||
}
|
||||
|
||||
return mostFrequent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将byte数组按指定编码转为char数组
|
||||
* @param byteArr 输入的byte数组
|
||||
@@ -51,7 +91,10 @@ public class CheckWeight {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CheckWeight checkWeight = new CheckWeight();
|
||||
System.out.println(checkWeight.sendWeight("172.19.50.75", 4001));
|
||||
while (true){
|
||||
System.out.println(checkWeight.sendWeight("172.19.50.80", 4001));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user