package com.ld.igds.protocol.bhzn.verb.server;
|
|
|
import com.ld.igds.util.BytesUtil;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.io.api.IoSession;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 工具类
|
*
|
* @author vince
|
*/
|
public class BhznVerbServerUtils {
|
|
|
/**
|
* 设备状态返回的结果 KEY= serId + "_STATUS_" + deviceId value = 结果状态
|
*/
|
private static Map<String, String> contextStatusMap = new HashMap<>();
|
|
|
/**
|
* 设备解析后的装法存放
|
*
|
* @param serId
|
* @param passcode 设备通道ID
|
* @param status
|
*/
|
public static void add2StatusMap(String companyId, String serId, String passcode, String status) {
|
contextStatusMap.put(ContextUtil.buildDeviceStatusKey(companyId, serId, passcode), status);
|
}
|
public static void add2StatusMap(String companyId, String serId, int passcode, String status) {
|
contextStatusMap.put(ContextUtil.buildDeviceStatusKey(companyId, serId, passcode), status);
|
}
|
public static Map<String, String> getStatusMap() {
|
return contextStatusMap;
|
}
|
/**
|
* 记录每个连接的最后一次信息时间 key = 连接的KYE,data =当前时间戳
|
*/
|
public static Map<String, Long> contextMapHeart = new HashMap<>();
|
|
public static Map<String, IoSession> contextIoSession = new HashMap<>();
|
|
|
public static int HEART_BEAT_TIME = 30;//心跳间隔时间
|
|
|
public static String MSG_START = "3C42485A4E3E";//<BHZN>
|
public static String MSG_START1 = "<BHZN>";//<BHZN>
|
public static String MSG_START2 = "AA";
|
public static String MSG_END = "<END>";//<END>
|
public static String MSG_END_16 = "3C454E443E";//<END>
|
public static final String CHARSET = "UTF-8";
|
|
|
/**
|
* 针对无线粮情主机的默认ID配置
|
*/
|
public static String DEFAULT_MAC_ID = "53681";
|
|
public static String FUNCTION_ID_00 = "00";
|
public static String FUNCTION_ID_F1 = "F1";
|
public static String FUNCTION_ID_F2 = "F2";
|
public static String FUNCTION_ID_83 = "83";
|
public static String FUNCTION_ID_93 = "93";
|
public static String FUNCTION_ID_92 = "92";
|
|
|
/**
|
* 生成TCP连接的KEY
|
*
|
* @param ip
|
* @param port
|
* @return
|
*/
|
public static String getServerKey(String ip, Integer port) {
|
return ip + ":" + port;
|
}
|
|
|
/**
|
* 添加最新心跳时间戳
|
*
|
* @param session
|
*/
|
public static void addHeartBeat(IoSession session) {
|
contextMapHeart.put(getServerKey(session.getAddress(), session.getPort()), System.currentTimeMillis());
|
}
|
|
public static Long getHearBeat(IoSession session) {
|
return contextMapHeart.get(getServerKey(session.getAddress(), session.getPort()));
|
}
|
|
|
/**
|
* 计算校验
|
*
|
* @param content
|
* @return
|
*/
|
public static String getCheck(String content) {
|
int start = BhznVerbServerUtils.MSG_START.length() + BhznVerbServerUtils.MSG_START2.length();
|
content = content.substring(start);
|
int sum = 0;
|
String hex;
|
for (int i = 0; i < content.length() / 2; i++) {
|
hex = content.substring(i * 2, i * 2 + 2);
|
sum += BytesUtil.hexToInt(hex);
|
}
|
String hexSum = BytesUtil.intToHexStr(sum);
|
int check = BytesUtil.hexToInt(hexSum.substring(hexSum.length() - 2));
|
|
return BytesUtil.intToHexStr(256 - check).substring(2);
|
}
|
|
// public static void addSession(IoSession session) {
|
// contextIoSession.put(DEFAULT_MAC_ID, session);
|
// }
|
//
|
// public static IoSession getSession() {
|
// return contextIoSession.get(DEFAULT_MAC_ID);
|
// }
|
}
|