czt
2024-07-13 bd01ba57ed8526a0ee94a50e1f52034251bb54f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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));
    }
 
}