package com.ld.igds.weather;
|
|
import com.ld.igds.models.WeatherInfo;
|
import com.ld.igds.util.BytesUtil;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.SpringUtil;
|
import com.ld.igds.utils.CRC16Util;
|
import com.ld.io.api.IoMsgConsumer;
|
import com.ld.io.api.IoSession;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
|
/**
|
* @Desc: 收到终端请求信息
|
* @author: Andy
|
* @update-time: 2023/4/23
|
*/
|
@Slf4j
|
public class MsgConsumer implements IoMsgConsumer {
|
|
private CoreWeatherService coreWeatherService;
|
|
@Override
|
public void consume(IoSession session, byte[] bytes) {
|
|
String strMsg = BytesUtil.bytesToString(bytes) + ServerUtils.MSG_END;
|
|
// 二次调整报文
|
if (!strMsg.startsWith(ServerUtils.MSG_START)) {
|
strMsg = strMsg.substring(strMsg.indexOf(ServerUtils.MSG_START));
|
}
|
|
String defaultCompanyId = ContextUtil.getDefaultCompanyId();
|
|
|
if (null == coreWeatherService) {
|
coreWeatherService = SpringUtil.getBean(CoreWeatherService.class);
|
}
|
|
WeatherInfo weatherInfo = coreWeatherService.getCacheWeather(defaultCompanyId);
|
|
|
String returnMsg = this.builderMsg(weatherInfo);
|
if(null == weatherInfo){
|
log.info("-------------返回气象信息----{}:{}={}", session.getAddress(), session.getPort(), returnMsg);
|
}else{
|
log.info("-------------返回气象信息---气象时间={}---{}:{}={}",weatherInfo.getUpdateTime(), session.getAddress(), session.getPort(), returnMsg);
|
}
|
|
|
|
WeatherServerEngine.push(session, BytesUtil.hexStrToBytes(returnMsg));
|
}
|
|
private String builderMsg(WeatherInfo weatherInfo) {
|
StringBuffer sb = new StringBuffer(ServerUtils.MSG_START);
|
|
//气象地址
|
int val = 1;
|
sb.append(BytesUtil.intToHexStr1(val));
|
|
//功能码
|
val = 2;
|
sb.append(BytesUtil.intToHexStr1(val));
|
|
|
if (null == weatherInfo) {
|
val = 0;
|
//长度信息 = 0
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//校验
|
val = calcCrc16(sb.toString());
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//结尾
|
sb.append(ServerUtils.MSG_END);
|
return sb.toString();
|
}
|
|
|
//长度 =10
|
val = 10;
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//温度
|
val = getInfo(weatherInfo, "WD");
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//湿度
|
val = getInfo(weatherInfo, "SD");
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//风向
|
val = getInfo(weatherInfo, "FX");
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//风力
|
val = getInfo(weatherInfo, "FL");
|
sb.append(BytesUtil.intToHexStr(val));
|
|
//天气
|
val = getInfo(weatherInfo, "TQ");
|
sb.append(BytesUtil.intToHexStr(val));
|
|
|
//校验
|
val = calcCrc16(sb.toString());
|
sb.append(BytesUtil.intToHexStr(val));
|
|
|
sb.append(ServerUtils.MSG_END);
|
return sb.toString();
|
}
|
|
private int getInfo(WeatherInfo weatherInfo, String tag) {
|
int val = -100;
|
String info = null;
|
try {
|
if ("WD".equals(tag)) {
|
info = weatherInfo.getTemp();
|
if (StringUtils.isNotEmpty(info)) {
|
info = info.replace("℃", "");
|
val = Integer.valueOf(info);
|
}
|
}
|
|
if ("SD".equals(tag)) {
|
info = weatherInfo.getHumidity();
|
if (StringUtils.isNotEmpty(info)) {
|
info = info.replace("%", "");
|
val = Integer.valueOf(info);
|
}
|
}
|
|
if ("FX".equals(tag)) {
|
val = 0;
|
info = weatherInfo.getWindDirection();
|
if (StringUtils.isNotEmpty(info)) {
|
if ("静风".equals(info)) return 0;
|
if ("北风".equals(info)) return 1;
|
if ("东北风".equals(info)) return 2;
|
if ("东风".equals(info)) return 3;
|
if ("东南风".equals(info)) return 4;
|
if ("南风".equals(info)) return 5;
|
if ("西南风".equals(info)) return 6;
|
if ("西风".equals(info)) return 7;
|
if ("西北风".equals(info)) return 8;
|
}
|
}
|
|
if ("FL".equals(tag)) {
|
val = 0;
|
info = weatherInfo.getWindSpeed();
|
if (StringUtils.isNotEmpty(info)) {
|
info = info.replace("级", "");
|
val = Integer.valueOf(info);
|
}
|
}
|
|
if ("TQ".equals(tag)) {
|
val = 0;
|
info = weatherInfo.getWeather();
|
if (StringUtils.isNotEmpty(info)) {
|
if (info.indexOf("阴") >= 0) return 1;
|
if (info.indexOf("雷雨") >= 0) return 7;
|
if (info.indexOf("雨") >= 0) return 2;
|
if (info.indexOf("多云") >= 0) return 3;
|
if (info.indexOf("冰雹") >= 0) return 4;
|
if (info.indexOf("雾") >= 0) return 5;
|
if (info.indexOf("沙") >= 0) return 6;
|
if (info.indexOf("雪") >= 0) return 8;
|
}
|
}
|
|
} catch (Exception e) {
|
log.error("----------气象信息转换异常-------{}-----{}", tag, e.getMessage());
|
return val;
|
}
|
return val;
|
}
|
|
|
public int calcCrc16(String hexStr) {
|
return CRC16Util.calcCrc16(BytesUtil.hexStrToBytes(hexStr));
|
}
|
|
}
|