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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.ld.igds.inout.manager;
 
import com.ld.igds.camera.ApiCameraManager;
import com.ld.igds.camera.CameraUtil;
import com.ld.igds.camera.data.ApiCameraData;
import com.ld.igds.camera.data.ApiCameraResp;
import com.ld.igds.camera.data.ApiSnapReq;
import com.ld.igds.file.CoreFileService;
import com.ld.igds.inout.InoutConstant;
import com.ld.igds.inout.dto.InoutApiData;
import com.ld.igds.inout.dto.InoutCameraDto;
import com.ld.igds.inout.dto.InoutData;
import com.ld.igds.inout.dto.InoutSnapImgDto;
import com.ld.igds.io.InoutApiService;
import com.ld.igds.io.constant.ProtocolEnum;
import com.ld.igds.m.service.InoutCommonService;
import com.ld.igds.models.InoutConf;
import com.ld.igds.models.SnapSer;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.view.service.SnapSerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
/**
 * @Desc: 出入库相关设备操作,业务逻辑包括摄像头操作,地磅,扦样机等所有设备
 * @author: Andy
 * @update-time: 2022/11/24
 */
@Slf4j
@Component
public class InoutDeviceManager {
 
    @Resource
    private SnapSerService snapSerService;
    @Resource
    private InoutCommonService inoutCommonService;
    @Resource
    private CoreFileService fileService;
    @Resource
    private ApiInoutManager apiInoutManager;
    @Resource
    private ApiCameraManager apiCameraManager;
 
 
    public InoutApiData getPlateNum(InoutApiData param) {
 
        //如果使用的是地磅插件获取,则需要调整设备为地磅插件信息
        if (ProtocolEnum.TCP_FZZY_WEIGHT.getCode().equals(param.getProtocol())) {
 
            InoutConf conf = this.getInoutConf(param.getCompanyId(), param.getDeptId(), param.getSort(), InoutConstant.CONF_TYPE_70);
            if (null == conf) {
                param.setErrorMsg("没有获取到当前协议插件配置配置信息,获取失败");
                return param;
            }
 
            InoutApiData pluginWeight = new InoutApiData();
            pluginWeight = param;
            pluginWeight.setIp(conf.getIp());
            pluginWeight.setPort(conf.getPort());
            pluginWeight.setProtocol(conf.getProtocol());
 
            return apiInoutManager.getApiService(param.getProtocol()).getPlateNum(pluginWeight);
 
        }
 
 
        return apiInoutManager.getApiService(param.getProtocol()).getPlateNum(param);
    }
 
    public InoutApiData getIcCardNum(InoutApiData param) {
 
        return apiInoutManager.getApiService(param.getProtocol()).getIcCardNum(param);
    }
 
    /**
     * 获取称重插件配置信息
     *
     * @param companyId
     * @param deptId
     * @param sort
     * @param type
     * @return
     */
    public InoutConf getInoutConf(String companyId, String deptId, String sort, String type) {
 
        // 获取出入库配置信息
        List<InoutConf> listInoutConf = inoutCommonService.getCacheInoutConf(companyId, deptId);
 
        if (null == listInoutConf) return null;
 
        InoutConf result = null;
 
        for (InoutConf conf : listInoutConf) {
            if (sort.equals(conf.getSort()) && type.equals(conf.getType())) {
                result = conf;
                break;
            }
        }
 
        if (null == result) return null;
 
        return result;
    }
 
 
    /**
     * 初始化车牌识别
     *
     * @param conf
     */
    public void initLpr(InoutConf conf) {
 
        if (null == conf) return;
 
        InoutApiData lprData = new InoutApiData(conf);
 
        apiInoutManager.getApiService(conf.getProtocol()).initLpr(lprData);
    }
 
 
    public String reConnectLpr(InoutApiData param) {
        return apiInoutManager.getApiService(param.getProtocol()).reConnectLpr(param);
    }
 
 
    /**
     * @param param
     * @return
     */
    public String noticeWeightDone(InoutApiData param) {
        //获取称重主控器配置
        InoutConf conf = getWeightGateway(param.getCompanyId(), param.getDeptId(), param.getSort());
 
        if (null == conf) {
            return "称重主控未配置,支持失败";
        }
 
        InoutApiData apiData = new InoutApiData(conf);
        apiData.setPlateNum(param.getPlateNum());
        apiData.setInoutProgress(param.getInoutProgress());
        apiData.setTime(new Date());
 
        InoutApiService inoutApiService = apiInoutManager.getApiService(conf.getProtocol());
 
        return inoutApiService.noticeWeightDone(apiData);
    }
 
    /**
     * 出入库抓拍
     *
     * @param param
     * @return
     */
    public InoutSnapImgDto snapImg(InoutSnapImgDto param) {
        try {
            InoutConf conf;
 
            //登记时候抓拍
            if (InoutConstant.PROGRESS_REGISTER.equals(param.getProgress())) {
                this.noticeGatewayAddWhitePlate(param);
            }
 
            //点击称重完成
            if (InoutConstant.PROGRESS_WEIGHT_TAG.equals(param.getProgress())
                    || InoutConstant.PROGRESS_WEIGHT_FULL.equals(param.getProgress()) ||
                    InoutConstant.PROGRESS_WEIGHT_EMPTY.equals(param.getProgress())) {
 
                this.noticeGatewayWeightInfo(param);
            }
 
            SnapSer ser = snapSerService.getCacheByDeptId(param.getDeptId());
 
            if (StringUtils.isNotEmpty(param.getId1())) {
                conf = inoutCommonService.getCacheInoutConf(param.getCompanyId(), param.getDeptId(), param.getId1());
                param.setFileName1(this.snapImgByConf(conf, param, ser));
                Thread.sleep(600);
            }
 
            if (StringUtils.isNotEmpty(param.getId2())) {
                conf = inoutCommonService.getCacheInoutConf(param.getCompanyId(), param.getDeptId(), param.getId2());
                param.setFileName2(this.snapImgByConf(conf, param, ser));
                Thread.sleep(600);
            }
 
            if (StringUtils.isNotEmpty(param.getId3())) {
                conf = inoutCommonService.getCacheInoutConf(param.getCompanyId(), param.getDeptId(), param.getId3());
                param.setFileName3(this.snapImgByConf(conf, param, ser));
            }
        } catch (Exception e) {
            log.error("---------------------出入库抓拍出现异常--------------------{}", e);
        }
 
        return param;
    }
 
    /**
     * 通知主控器-通知称重信息和称重完成信息
     *
     * @param snapImgDto
     */
    private void noticeGatewayWeightInfo(InoutSnapImgDto snapImgDto) {
 
        log.debug("-------------用户确认称重----通知称重网关----{}", snapImgDto.getWeight());
        InoutConf conf = this.getWeightGateway(snapImgDto.getCompanyId(), snapImgDto.getDeptId(), snapImgDto.getSort());
 
        if (null == conf) {
            log.info("-------------用户确认称重----通知称重网关---未配置网关----");
            return;
        }
 
        InoutApiData apiData = new InoutApiData(conf);
        apiData.setTime(new Date());
        apiData.setRepeat(2);
        apiData.setWeight(snapImgDto.getWeight());
        apiData.setPlateNum(snapImgDto.getPlateNum());
        //称重信息暂时取消播放
//        apiData.setMsg("当前称重:" + apiData.getWeight() + "千克");
        apiData.setMsg("称重已完成");
 
//        apiInoutManager.getApiService(apiData.getProtocol()).noticeWeightDone(apiData);
        apiInoutManager.getApiService(apiData.getProtocol()).noticeRadio(apiData);
    }
 
    /**
     * 新增白名单给住控制器,需要注意的是,登记时候不确认使用哪个地磅系统,如果系统配置多个主控器,需要同步全部推送
     *
     * @param imgDto
     */
    private void noticeGatewayAddWhitePlate(InoutSnapImgDto imgDto) {
 
        List<InoutConf> list = this.getWeightGateway(imgDto.getCompanyId(), imgDto.getDeptId());
 
        if (null == list || list.isEmpty()) {
            log.info("-------------称重网关增加白名单----没有获取到网关配置-----取消执行---");
            return;
        }
 
        log.debug("-------------登记完成,增加车牌白名单-----------{}", imgDto.getPlateNum());
 
        InoutApiService inoutApiService = apiInoutManager.getApiService(list.get(0).getProtocol());
 
        InoutApiData apiData;
        for (InoutConf conf : list) {
            apiData = new InoutApiData(conf);
            apiData.setPlateNum(imgDto.getPlateNum());
            inoutApiService.addWhitePlate(apiData);
        }
    }
 
    public void noticeProgressComplete(InoutData data) {
        log.debug("-----------------通知称重主控--------流程结束删除白名单");
        List<InoutConf> list = this.getWeightGateway(data.getCompanyId(), data.getDeptId());
 
        if (null == list || list.isEmpty()) {
            return;
        }
 
        InoutApiService inoutApiService = apiInoutManager.getApiService(list.get(0).getProtocol());
 
        InoutApiData apiData;
        for (InoutConf conf : list) {
            apiData = new InoutApiData(conf);
            apiData.setPlateNum(data.getPlateNum());
            inoutApiService.delWhitePlate(apiData);
        }
    }
 
    /**
     * 调用接口进行抓拍
     *
     * @param conf
     * @param param
     * @param snapSer
     * @return
     */
    private String snapImgByConf(InoutConf conf, InoutSnapImgDto param, SnapSer snapSer) {
        if (null == conf) return null;
 
        if (null == conf.getSnapType()) {
            log.error("-----------抓拍执行失败:没有配置抓拍方式-----{}", conf.getName());
            return null;
        }
 
        ApiSnapReq req = new ApiSnapReq();
        req.setCompanyId(conf.getCompanyId());
        req.setDeptId(conf.getDeptId());
        req.setSn(conf.getSn());
        req.setSnapType(conf.getSnapType());
        req.setPlayType(conf.getPlayType());
        req.setSerSn(null == snapSer ? null : snapSer.getSn());
        req.setBizId(param.getBizId());
        req.setCameraName(conf.getName());
 
        req.setMediaAddr(CameraUtil.updateMediaAddr(conf.getMediaAddr(), conf.getLoginId(),
                conf.getPwd(), conf.getIp(), conf.getPortCtrl(), conf.getChanNum()));
 
        String filePath = fileService.getInoutFilePath(new Date());
 
        req.setFilePath(filePath);
 
        //调整附件名称,避免出现中文
        String fileName = ContextUtil.getTimeId(100) + "_" + conf.getInoutProgress() + ".jpg";
        req.setFileName(fileName);
 
        //执行抓拍
        ApiCameraResp resp = apiCameraManager.getApiCameraService(conf.getPlayType()).snapImg(req);
 
        if (ApiCameraResp.CODE_ERROR.equals(resp.getCode())) {
            log.error("---------抓拍执行失败:{}-----{}", resp.getMsg(), conf.getName());
 
            return null;
        }
 
        return fileName;
    }
 
 
    /**
     * 出入库视频 -- 根据播放方式获取不同的播放地址
     *
     * @param param
     * @return
     */
    public InoutCameraDto inoutVideoPlay(InoutCameraDto param) {
 
        //没有配置播放方式
        if (StringUtils.isEmpty(param.getPlayType())) {
            param.setErrorMsg("没有配置播放方式,无法播放!");
            return param;
        }
 
        if (StringUtils.isEmpty(param.getCompanyId())) {
            param.setCompanyId(ContextUtil.getCompanyId());
        }
 
        //通过接口获取播放地址
        ApiCameraData apiCameraData = new ApiCameraData();
        apiCameraData.setCompanyId(param.getCompanyId());
        apiCameraData.setSn(param.getSn());
        apiCameraData.setCameraId(param.getId());
        apiCameraData.setPlayType(param.getPlayType());
        apiCameraData.setMediaAddr(CameraUtil.updateMediaAddr(param.getMediaAddr(),
                param.getLoginId(), param.getPwd(), param.getIp(), param.getPortCtrl(), param.getChanNum()));
 
        ApiCameraResp result = apiCameraManager.getApiCameraService(param.getPlayType()).getPlayAddr(apiCameraData);
 
        if (null == result) {
            return param;
        }
 
        if (ApiCameraResp.CODE_SUCCESS.equals(result.getCode())) {
            param.setPlayAddr(result.getPlayAddr());
        }
        if (ApiCameraResp.CODE_ERROR.equals(result.getCode())) {
            param.setErrorMsg(result.getMsg());
        }
        return param;
    }
 
    public String videoKeepLive(InoutCameraDto param) {
 
        ApiCameraData apiCameraData = new ApiCameraData();
        apiCameraData.setSn(param.getSn());
        apiCameraData.setCameraId(param.getId());
        apiCameraData.setPlayType(param.getPlayType());
 
        apiCameraManager.getApiCameraService(param.getPlayType()).keepAlive(apiCameraData);
 
        return null;
    }
 
 
    /**
     * 根据id获取出入库设备信息
     *
     * @param id
     * @return
     */
    public InoutConf getInoutConf(String id) {
        //没有配置播放方式
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return inoutCommonService.getCacheInoutConf(ContextUtil.getCompanyId(), ContextUtil.subDeptId(null), id);
    }
 
 
    /**
     * @Desc: 获取称重网关设备
     * @author: Andy
     * @update-time: 2022/11/26
     */
    public List<InoutConf> getWeightGateway(String companyId, String deptId) {
        List<InoutConf> list = inoutCommonService.getCacheInoutConf(companyId, deptId);
        if (null == list || list.isEmpty()) return null;
 
        List<InoutConf> result = new ArrayList<>();
 
        for (InoutConf conf : list) {
            if (InoutConstant.CONF_TYPE_70.equals(conf.getType())) {
                result.add(conf);
            }
        }
        return result;
    }
 
    /**
     * @Desc: 获取称重网关设备
     * @author: Andy
     * @update-time: 2022/11/26
     */
    public InoutConf getWeightGateway(String companyId, String deptId, String sort) {
        if (null == sort) sort = "1";
        List<InoutConf> list = inoutCommonService.getCacheInoutConf(companyId, deptId);
        if (null == list || list.isEmpty()) return null;
 
        for (InoutConf conf : list) {
            if (InoutConstant.CONF_TYPE_70.equals(conf.getType()) && sort.equals(conf.getSort())) {
                return conf;
            }
        }
        return null;
    }
 
 
}