jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.ld.igds.protocol.quantity.shuhan.analysis;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.io.request.QuantityRequest;
import com.ld.igds.models.Depot;
import com.ld.igds.models.QuantityConf;
import com.ld.igds.protocol.quantity.shuhan.ShuHanUtils;
import com.ld.igds.protocol.quantity.shuhan.command.CommandBuilder;
import com.ld.igds.protocol.quantity.shuhan.command.Message;
import com.ld.igds.protocol.quantity.shuhan.command.MessageContent;
import com.ld.igds.protocol.quantity.shuhan.server.ShuhanServerEngine;
import com.ld.igds.quantity.dto.QuantityData;
import com.ld.igds.quantity.dto.QuantityProgressData;
import com.ld.igds.quantity.service.CoreQuantityService;
import com.ld.igds.util.RedisUtil;
import com.ld.io.api.IoSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.List;
 
/**
 * 协议解析入口
 *
 * @author jiazx
 */
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "shuhan.analysisService";
 
    @Autowired
    private CoreCommonService coreCommonService;
    @Autowired
    private CoreQuantityService coreQuantityService;
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
    @Autowired
    private RedisUtil redisUtil;
 
    /**
     * 协议解析
     *
     * @param ip
     * @param port
     * @param message
     */
    public void analysis(String ip, Integer port, Message message) {
 
        //添加心跳信息
        ShuHanUtils.addHeartBeat(ip, port);
        log.info("蜀汉数量检测命令={}", message);
        if (message == null)
            return;
        switch (message.getFunctionId()) {
            case "0":
                analysis0(ip, port, message);
                break;
            case "1":
                analysis1(ip, port, message);
                break;
            case "2":
                analysis2(ip, port, message);
                break;
            case "3":
                analysis3(ip, port, message);
                break;
            case "4":
                analysis4(ip, port, message);
                break;
            default:
                break;
        }
 
    }
 
    /**
     * 登录 初次连接
     *
     * @param ip
     * @param port
     * @param message
     */
    private void analysis0(String ip, Integer port, Message message) {
 
        QuantityConf quantityConf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
        if (quantityConf == null) {
            log.error("没有获取到对应数量检测配置,取消连接");
            ShuhanServerEngine.destroy(ip, port);
            return;
        }
 
        //更新所有的数量检测配置的通讯IP和端口
        List<QuantityConf> list = coreQuantityService.getCacheQuantityConf(quantityConf.getCompanyId());
        if(null != list && list.size() > 0){
            for (QuantityConf conf : list) {
                conf.setIp(ip);
                conf.setPort(port);
                conf.setStatus(Constant.YN_Y);
                coreQuantityService.updateQuantityConfBySn(conf);
            }
        }
 
        coreQuantityService.refreshQuantityConfCache();
 
        log.info("++++蜀汉插件登录认证完成++++-SN={}", message.getSn());
    }
 
    /**
     * 开始采集,无需解析
     *
     * @param ip
     * @param port
     * @param message
     */
    private void analysis1(String ip, Integer port, Message message) {
        // do Nothing
    }
 
    /**
     * 查询状态
     *
     * @param ip
     * @param port
     * @param message
     */
    private void analysis2(String ip, Integer port, Message message) {
 
        QuantityConf conf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
 
        if (conf == null) {
            log.error("没有获取到对应分机,取消解析:" + message.toString());
            return;
        }
        String ytIp = message.getSn();
 
        String key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
        QuantityRequest quantityRequest = (QuantityRequest) redisUtil.get(key);
        if (quantityRequest == null) {
            log.error("没有找到发送的命令,可能已经超时,取消解析:" + message.toString());
            return;
        }
        QuantityProgressData progressData = new QuantityProgressData();
        progressData.setCompanyId(conf.getCompanyId());
        progressData.setDepotId(conf.getDepotId());
        progressData.setDeptId(conf.getDeptId());
        MessageContent content = message.getContent();
        if (content == null) {
            log.error("没有找到关联配置,取消解析:" + message.toString());
            return;
        }
        Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(),
                conf.getDepotId());
        progressData.setProgress(Integer.parseInt(content.getProgress()));
        progressData.setState(content.getState());
 
        if ("finished".equals(progressData.getState())) {
            // 检测完成,发送发送计算命令。删除缓存
            key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
            redisUtil.set(key, null, 3);
            // redisUtil.del(ServerUtils.buildRedisKey(conf.getCompanyId(),ytIp));
            IoSession session = ShuhanServerEngine.getSession(ip, port);
            if (session != null) {
                session.invoke(CommandBuilder.getResultMsg(quantityRequest)
                        .getBytes());
            }
            progressData.setMsg(depot.getName() + "数量监测执行进度:"
                    + progressData.getProgress() + "%,开始计算结果!");
        }
        if ("failed".equals(progressData.getState())) {
            // 检测失败,删除缓存
            key = RedisConst.buildKey(conf.getCompanyId(), ytIp);
            redisUtil.set(key, null, 3);
            // redisUtil.del(ServerUtils.buildRedisKey(conf.getCompanyId(),ytIp));
            progressData.setMsg(depot.getName() + "数量检测执行失败,进度:"
                    + progressData.getProgress() + "%,开始检测时间:"
                    + content.getStart_time());
        }
 
        log.info(depot.getName() + "数量监测执行进度:" + progressData.getProgress()
                + "%,开始检测时间:" + content.getStart_time());
        progressData.setMsg(depot.getName() + "数量监测执行进度:"
                + progressData.getProgress() + "%,开始检测时间:"
                + content.getStart_time());
 
        notifyWebInvoker.notifyQuantityProgress(progressData);
    }
 
    /**
     * 返回结果
     *
     * @param ip
     * @param port
     * @param message
     */
    private void analysis3(String ip, Integer port, Message message) {
        QuantityConf conf = coreQuantityService.getCacheQuantityConfBySn(message.getSn());
        if (conf == null) {
            log.error("没有获取到对应分机,取消解析:" + message.toString());
            return;
        }
 
        MessageContent content = message.getContent();
        if (content == null) {
            log.error("没有找到关联配置,取消解析:" + message.toString());
            return;
        }
        Depot depot = coreCommonService.getCacheDepot(conf.getCompanyId(),
                conf.getDepotId());
        JSONObject msg = content.getMsg();
        if (msg == null) {
            log.error("没有数据,取消解析:" + message.toString());
            return;
        }
 
        String key = RedisConst.buildKey(conf.getCompanyId(), conf.getYtIp());
        QuantityRequest res = (QuantityRequest) redisUtil.get(key);
        if (res == null) {
            log.error("没有找到关联配置,取消解析:" + message.toString());
            return;
        }
 
        QuantityData quantityData = new QuantityData();
 
        quantityData.setBatchId(res.getBatchId());
        quantityData.setReceiveDate(new Date());
        quantityData.setBulk(msg.getDoubleValue("bulk"));
        quantityData.setWeight(msg.getDoubleValue("weight"));
        quantityData.setDepotId(depot.getId());
        quantityData.setDeptId(depot.getDeptId());
        quantityData.setCompanyId(depot.getCompanyId());
        quantityData.setPoints(JSON.toJSONString(msg.getJSONArray("heights")));
 
        coreQuantityService.saveData(quantityData);
 
        //清除缓存
        redisUtil.del(key);
    }
 
    /**
     * 心跳判断
     *
     * @param ip
     * @param port
     * @param message
     */
    private void analysis4(String ip, Integer port, Message message) {
        log.debug("------------------蜀汉插件心跳维持----------------");
    }
 
}