YYC
2023-09-07 7243e0b4bd9a267a633d2e481e17646509b6868d
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.ld.igds.protocol.zldz.analysis;
 
import com.ld.igds.common.CoreSerService;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.grain.dto.GrainSerCableData;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.GrainSerConfRequest;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.protocol.zldz.analysis.message.ReMessage;
import com.ld.igds.protocol.zldz.command.BaseRemoteImpl;
import com.ld.igds.protocol.zldz.command.builder.CommandBuilder8829;
import com.ld.igds.protocol.zldz.command.message.Message;
import com.ld.igds.protocol.zldz.task.CommandReSendService;
import com.ld.igds.protocol.zldz.util.ServerUtils;
import com.ld.igds.util.BytesUtil;
import com.ld.igds.util.RedisUtil;
import com.ld.igds.view.manager.TempManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author vince
 */
@Slf4j
@Component(AnalysisConf.BEAN_ID)
public class AnalysisConf extends BaseRemoteImpl {
 
    public static final String BEAN_ID = "zldz.analysisConf";
 
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
 
    @Autowired
    private CoreSerService serService;
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TempManager thManager;
 
    @Autowired
    private CommandReSendService commandReSendService;
 
    /**
     * 解析分机配置信息
     *
     * @param message
     * @param ser
     */
    public void analysis8816(ReMessage message, DeviceSer ser) {
 
        log.debug("分机---->>>平台:消息类型=8816----{}-{}.{}", ser.getCompanyId(),
                ser.getIp(), ser.getPort());
 
        // 清除缓存
        commandReSendService.cleanKey(message.getCompanyId(), message.getIp(),
                message.getPort(), ServerUtils.MSG_TYPE_8816);
 
        // 清楚初始化登陆的解析
        commandReSendService.cleanKey(ServerUtils.DEFAULT_COMPANY,
                message.getIp(), message.getPort(), ServerUtils.MSG_TYPE_8816);
 
        ser.setStatus(Constant.YN_Y);
 
        String content = message.getBody().getContent();
 
        // 读写标志
        int start = 0, len = 1;
        String temp = content.substring(start * 2, start * 2 + len * 2);
        int readTag = BytesUtil.hexToInt(temp);
 
        // 设备ID,全局唯一
        start = 1;
        len = 2;
        temp = content.substring(start * 2, start * 2 + len * 2);
 
        // 网络ID
        start = 3;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
 
        // 信道
        start = 4;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
 
        // 速率
        start = 5;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
 
        // 层数
        start = 6;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setCableZ(BytesUtil.hexToInt(temp));
 
        // 行数
        start = 7;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setCableY(BytesUtil.hexToInt(temp));
 
        // 列数
        start = 8;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setCableX(BytesUtil.hexToInt(temp));
 
        // 电缆制式
        start = 9;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setCableFormat("0" + BytesUtil.hexToInt(temp));
 
        // 电缆类型
        start = 10;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setCableType("0" + BytesUtil.hexToInt(temp));
 
        // 供电模式
        start = 11;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        ser.setPowerModel("0" + BytesUtil.hexToInt(temp));
 
        String msg = ser.getName() + " 远程获取配置成功,请刷新数据查看!";
 
        // 组织编码
        start = 12;
        temp = content.substring(start * 2);
        if (readTag == 1) {// 表示写的成功返回
            ser.setOrgId(ser.getCompanyId());
            msg = ser.getName() + " 远程写入成功,请刷新数据查看!";
        } else {
            ser.setOrgId(BytesUtil.hexToInt(BytesUtil.tran_LH(temp)) + "");
        }
 
        if (null == ser.getSn()) {
            ser.setSn(ser.getId());
        }
        ser.setIp(message.getIp());
        ser.setPort(message.getPort());
 
        // 开始列默认=1
        if (ser.getCableStart() == 0) {
            ser.setCableStart(1);
        }
 
        log.info("分机---->>>平台:分机配置解析完成----{}-{}-{}-{},更新到服服务器",
                ser.getCompanyId(), ser.getIp(), ser.getPort(), ser.getName());
        serService.updateSerById(ser);
 
        // 更新会话信息
        super.updateSession(ser.getIp(), ser.getPort(), ser.getCompanyId());
 
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS, msg);
 
        //分机上线进行一次温湿度采集
        thManager.checkThBySer(ser.getCompanyId(), ser.getId());
    }
 
    /**
     * 电缆初始化成功
     *
     * @param reMessage
     * @param ser
     */
    public void analysis8822(ReMessage reMessage, DeviceSer ser) {
        log.debug("-----------analysis8822------------{}-{}.{}",
                ser.getCompanyId(), ser.getIp(), ser.getPort());
 
        // 清除缓存
        commandReSendService.cleanKey(reMessage.getCompanyId(),
                reMessage.getIp(), reMessage.getPort(),
                ServerUtils.MSG_TYPE_8822);
 
        String msg = ser.getName() + " 初始化电缆完成!!";
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS, msg);
    }
 
    /**
     * 电缆修改返回,
     *
     * @param reMessage
     * @param ser
     */
    public void analysis8826(ReMessage reMessage, DeviceSer ser) {
        log.debug("-----------analysis8826------------{}-{}.{}",
                ser.getCompanyId(), ser.getIp(), ser.getPort());
 
        // 清除缓存
        commandReSendService.cleanKey(reMessage.getCompanyId(),
                reMessage.getIp(), reMessage.getPort(),
                ServerUtils.MSG_TYPE_8826);
 
        String msg = ser.getName() + " 电缆修改操作完成";
 
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS, msg);
    }
 
    /**
     * 通道电缆获取返回,分机返回收到命令信息,实际返回值在8829中说明
     *
     * @param reMessage
     * @param ser
     */
    public void analysis8825(ReMessage reMessage, DeviceSer ser) {
 
        log.debug("-----------analysis8825------------{}-{}.{}",
                ser.getCompanyId(), ser.getIp(), ser.getPort());
 
        // 清除缓存
        commandReSendService.cleanKey(reMessage.getCompanyId(),
                reMessage.getIp(), reMessage.getPort(),
                ServerUtils.MSG_TYPE_8825);
 
        String msg = ser.getName() + " 读取通道电缆开始返回";
 
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS, msg);
 
    }
 
    /**
     * 终端发送通道电缆数据给后台,分机主动推送
     *
     * @param reMessage
     * @param ser
     */
    @SuppressWarnings("unchecked")
    public void analysis1129(ReMessage reMessage, DeviceSer ser) {
 
        // 开始解析
        String content = reMessage.getBody().getContent();
 
        // 通道号
        int start = 0, len = 1;
        String temp = content.substring(start * 2, start * 2 + len * 2);
        int passCode = BytesUtil.hexToInt(temp);
 
        // 点序号,按照返回顺序填写从1开始
        start = 1;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        int iotNum = BytesUtil.hexToInt(temp);
 
        // 总包数
        start = 2;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        int sumPacket = BytesUtil.hexToInt(temp);
 
        // 当前包
        start = 3;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        int curPacket = BytesUtil.hexToInt(temp);
 
        // -----------后端通知终端收到信息----------------------//
        GrainSerConfRequest request = new GrainSerConfRequest();
        request.setSerId(ser.getId());
        request.setBizType(BizType.SYS.getCode());
        request.setIp(ser.getIp());
        request.setPort(ser.getPort());
        request.setCompanyId(ser.getCompanyId());
        request.setValue1(curPacket);
 
        Message message = CommandBuilder8829.getInstance()
                .buildMessage(request);
        log.info("平台------>>>分机:" + request.getSerName() + "后台收到电缆数据应答="
                + message.toString());
        // 发送命令
        send(request.getIp(), request.getPort(), message.getByteMsg());
 
        // -----------后端通知终端收到信息 EDN----------------------//
 
        log.info("分机------>>>平台:返回电缆信息,分机={},当前包:{},信息={}", ser.getName(),
                curPacket, content);
 
        // 详细信息,每长度5为一组数据
        start = 4;
        temp = content.substring(start * 2);
 
        String redisKey = RedisConst.buildKey(ser.getCompanyId(),
                RedisConst.KEY_TEMP_PREFIX, "CABLE_DATA_" + ser.getId() + "_"
                        + passCode);
 
        List<GrainSerCableData> dataList = null;
        if (curPacket != 1) {
            dataList = (List<GrainSerCableData>) redisUtil.get(redisKey);
        }
        if (null == dataList)
            dataList = new ArrayList<>();
 
        GrainSerCableData data;
 
        String str1 = temp.substring(0, 160);// 温度点 40个
        String str2 = temp.substring(160, 400);// 1个根号,1个点号,1个配置,功能40组
        String temp1, temp2;
        for (int i = 0; i < 40; i++) {
            data = new GrainSerCableData();
            data.setPassCode(passCode);
            data.setIotNum(i + 1);
            data.setReceiveDate(new Date());
            //data.setPointIndex(dataList.size() + i + 1);
 
            // 温度
            temp1 = str1.substring(i * 4, i * 4 + 4);
            data.setTemp(BytesUtil.hexToInt(BytesUtil.tran_LH(temp1)) / 10.0);
 
            //根号
            temp2 = str2.substring(i * 6, i * 6 + 6);
            data.setRootIndex(BytesUtil.hexToInt(temp2.substring(0, 2)));
 
            //点号
            data.setPointIndex(BytesUtil.hexToInt(temp2.substring(2, 4)));
 
            //配置
            data.setConf(temp2.substring(4, 6));
 
            dataList.add(data);
        }
 
        // 存放緩存
        redisUtil.set(redisKey, dataList, 5 * 60);
 
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS, "系统收到电缆配置信息,请手动刷新,分机="
                        + ser.getName() + ",通道=" + passCode + ",总包="
                        + sumPacket + ",当前包=" + curPacket);
    }
 
    /**
     * 电缆汇总应答
     *
     * @param reMessage
     * @param ser
     */
    public void analysis8823(ReMessage reMessage, DeviceSer ser) {
        // 开始解析
        String content = reMessage.getBody().getContent();
 
        // 动态拼接对象
        List<GrainSerCableData> dataList = new ArrayList<GrainSerCableData>();
        String tempStr;
        GrainSerCableData cableData;
        for (int i = 0; i < content.length() / 4; i++) {
            tempStr = content.substring(i * 4, i * 4 + 4);
            cableData = new GrainSerCableData();
            cableData.setPassCode(i + 1);
            cableData.setReceiveDate(new Date());
            cableData.setIotNum(BytesUtil.hexToInt(tempStr.substring(0, 2)));
            cableData.setIotErrNum(BytesUtil.hexToInt(tempStr.substring(2, 4)));
 
            dataList.add(cableData);
        }
 
        String redisKey = RedisConst.buildKey(ser.getCompanyId(),
                RedisConst.KEY_TEMP_PREFIX, "CABLE_DATA_" + ser.getId());
 
        // 存放緩存
        redisUtil.set(redisKey, dataList, 5 * 60);
 
        notifyWebInvoker.notifyWeb(ser.getCompanyId(),
                OrderRespEnum.MSG_SUCCESS, BizType.SYS,
                "系统收到电缆汇总信息,请手动更新查看,返回信息:分机=" + ser.getName());
    }
}