CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
package com.ld.igds.protocol.snap;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.camera.data.ApiSnapReq;
import com.ld.igds.file.CoreFileService;
import com.ld.igds.file.dto.FileData;
import com.ld.igds.models.SnapSer;
import com.ld.igds.protocol.snap.server.Message;
import com.ld.igds.protocol.snap.server.SnapServerEngine;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.view.service.SnapSerService;
import com.ld.igds.warn.data.WarnData;
import com.ld.igds.constant.WarnLevel;
import com.ld.igds.constant.WarnStatus;
import com.ld.igds.constant.WarnType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
 
/**
 * 抓拍结果返回
 */
@Slf4j
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "snap.analysisService";
 
    @Autowired
    private SnapSerService snapSerService;
    @Autowired
    private CoreFileService fileService;
 
    /**
     * 解析插件返回的信息
     *
     * @param message
     */
    public void analysis(Message message) {
 
        log.info("抓拍插件--->平台--{}", message.toString());
 
        //添加心跳维持信息
        SnapUtils.addHeartBeat(message.getIp(), message.getPort());
 
        //新设备上线
        if (message.getFunctionId().equals(SnapUtils.FUNC_ID_0)) {
 
            SnapServerEngine.updateKey(message.getIp(), message.getPort(), message.getSn());
 
            snapSerService.connectAdd(message.getSn(), message.getIp(), message.getPort());
 
            log.info("++++抓拍插件登录认证完成++++-SN={}", message.getSn());
        }
 
        //抓拍结果返回
        if (message.getFunctionId().equals(SnapUtils.FUNC_ID_1)) {
 
            JSONObject jsonContent = JSONObject.parseObject(message.getContent());
 
            String imgData = (String) jsonContent.get("data");
 
            if ("error".equals(imgData) || StringUtils.isEmpty(imgData)) {
                SnapUtils.removeRequest(message.getOrderId().toString());
                log.error("抓拍插件--->平台--终端抓拍插件抓拍失败.");
                return;
            }
 
            this.analysisImg2Disk(message.getOrderId(), imgData);
        }
 
        //事件通知
        if (message.getFunctionId().equals(SnapUtils.FUNC_ID_2)) {
 
 
            JSONObject jsonContent = JSONObject.parseObject(message.getContent());
 
            String action = (String) jsonContent.get("action");
 
            //断电
            if (SnapUtils.ACTION_CODE_10.equals(action)) {
                SnapSer snapSer = snapSerService.getCacheBySn(message.getSn());
 
                WarnData warnData = new WarnData();
                warnData.setCompanyId(snapSer.getCompanyId());
                warnData.setDepotId(snapSer.getDeptId());
                warnData.setId(ContextUtil.getUUID());
                warnData.setLevel(WarnLevel.LEVEL_01.getCode());
                warnData.setName("库区断电警告");
                warnData.setStatus(WarnStatus.STATUS_10.getCode());
                warnData.setTime(new Date());
                warnData.setType(WarnType.TYPE_01.getCode());
                warnData.setInfo("系统收到库区断电警告通知,已经启动断电抓拍逻辑,请联系库区核对断电原因,时间:" + DateFormatUtils.format(warnData.getTime(), "yyyy-MM-dd HH:mm"));
 
 
                //TODO 逻辑待调整
                // coreWarnService.addWarn(warnData);
            }
 
            //上电
            if (SnapUtils.ACTION_CODE_11.equals(action)) {
                SnapSer snapSer = snapSerService.getCacheBySn(message.getSn());
 
                WarnData warnData = new WarnData();
                warnData.setCompanyId(snapSer.getCompanyId());
                warnData.setDepotId(snapSer.getDeptId());
                warnData.setId(ContextUtil.getUUID());
                warnData.setType(WarnType.TYPE_03.getCode());
                warnData.setName("库区上电通知");
                warnData.setStatus(WarnStatus.STATUS_40.getCode());
                warnData.setTime(new Date());
                warnData.setLevel(WarnLevel.LEVEL_01.getCode());
                warnData.setInfo("系统收到库区上电通知,时间:" + DateFormatUtils.format(warnData.getTime(), "yyyy-MM-dd HH:mm"));
 
                //TODO 逻辑待调整
                //coreWarnService.addWarn(warnData);
            }
 
 
            //主动心跳
            if (SnapUtils.ACTION_CODE_20.equals(action)) {
                log.debug("------------------抓拍插件心跳维持----------------");
            }
        }
    }
 
 
    private void analysisImg2Disk(Integer orderId, String imgData) {
        ApiSnapReq apiSnapReq = SnapUtils.getRequest(orderId.toString());
        if (null == apiSnapReq) {
            log.error("----平台解析抓拍插件返回信息,没有获取到原来请求,解析失败.");
            return;
        }
 
        log.debug("----抓拍返回----{}", apiSnapReq);
 
 
        //如果没有外键信息,表示只保持图片即可,如果有则需要添加附件列表。
        if (null == apiSnapReq.getBizId()) {
            fileService.baseImg2Disk(apiSnapReq.getFilePath(), apiSnapReq.getFileName(), imgData);
        } else {
            if (null == apiSnapReq.getFilePath()) {
                apiSnapReq.setFilePath(fileService.getCommonFilePath(new Date()));
            }
            if (null == apiSnapReq.getFileName()) {
                apiSnapReq.setFileName(ContextUtil.getCurTimeMillis() + ".jpg");
            }
 
            FileData fileData = new FileData();
            fileData.setFileId(ContextUtil.getUUID());
            fileData.setCompanyId(ContextUtil.getDefaultCompanyId());
            fileData.setBizId(apiSnapReq.getBizId());
            fileData.setFileName(apiSnapReq.getFileName());
            fileData.setBizTag(apiSnapReq.getCameraName());
            fileData.setCreateTime(new Date());
            fileService.addRecord(fileData);
 
            //持久保存
            fileService.baseImg2Disk(apiSnapReq.getFilePath(), apiSnapReq.getFileName(), imgData);
 
        }
 
        log.info("平台解析--抓拍插件照片成功--{}", apiSnapReq.getFilePath() + apiSnapReq.getFileName());
    }
 
 
}