CZT
2023-09-14 db72a27d10fdc4de0926fb8c7cc411ec0d4ea51b
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
package com.ld.igds.protocol.quantity.shuhan;
 
import com.ld.igds.camera.CameraUtil;
import com.ld.igds.camera.data.ApiSnapReq;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.io.request.QuantityRequest;
import com.ld.igds.models.QuantityConf;
import com.ld.igds.models.SnapSer;
import com.ld.igds.protocol.quantity.shuhan.command.BaseRemoteImpl;
import com.ld.igds.protocol.quantity.shuhan.command.RemoteQuantityServiceImpl;
import com.ld.igds.protocol.snap.SnapPluginService;
import com.ld.igds.quantity.service.CoreQuantityService;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
 
import java.util.Date;
import java.util.concurrent.Callable;
 
/**
 *
 * @author andy.jia
 */
@Slf4j
public class QueryStatusTask extends BaseRemoteImpl implements Callable<String> {
 
 
    private RedisUtil redisUtil;
 
    private CoreQuantityService coreQuantityService;
 
    private String redisKey;
 
    private String queryMsg;
 
    private RemoteQuantityServiceImpl remoteQuantityService;
 
    private SnapPluginService snapService;
 
    public QueryStatusTask() {
        super();
    }
 
    public QueryStatusTask(RedisUtil redisUtil, RemoteQuantityServiceImpl remoteQuantityService, CoreQuantityService coreQuantityService, SnapPluginService snapService, String redisKey, String queryMsg) {
        super();
        this.redisUtil = redisUtil;
        this.coreQuantityService = coreQuantityService;
        this.remoteQuantityService = remoteQuantityService;
        this.snapService = snapService;
        this.redisKey = redisKey;
        this.queryMsg = queryMsg;
    }
 
    @Override
    public String call() throws Exception {
        //记录抓拍次数
        int num = 1;
        //记录抓拍时间间隔
        Date lastTime = new Date();
        Date curTime = null;
 
        boolean flag = true;
        QuantityConf conf = null;
        while (flag){
            Thread.sleep(1000 * 60 * 2 );
            //发起状态查询命令
            try{
                QuantityRequest res = (QuantityRequest) redisUtil.get(redisKey);
                if(res != null ){
                    conf = coreQuantityService.getCacheQuantityConf(res.getCompanyId(),res.getDepotId());
                    if(conf == null )return "子流程结束,没有获取到分机。";
                    log.info("发送数量检测查询命令:" + res.toString());
                    remoteQuantityService.send(conf.getIp(),conf.getPort(),queryMsg.getBytes(ShuHanUtils.CHARSET));
 
                    //调用抓拍
                    if(num <= 3){
                        curTime = new Date();
                        if(DateUtil.difMin(lastTime, curTime) >= 4){
                            lastTime = curTime;
                            num ++;
                            this.snapImg(conf, res.getBatchId());
                        }
                    }
                }else{
                    flag = false;
                    log.info("检测流程结束,子线程关闭!");
                }
            }catch (Exception e){
                log.error("子线程查询进度出现异常。");
                log.error(e.getMessage(),e);
            }
 
        }
        return "子任务执行完成,数量结果应该已经正确解析。 ";
    }
 
    /**
     * 调用抓拍
     * @param conf
     * @return
     */
    private String snapImg(QuantityConf conf, String batchId) {
        if(null == conf){
            log.error("---配置参数为空,不抓拍---");
            return null;
        }
        if(StringUtils.isEmpty(conf.getDeptId()) || StringUtils.isEmpty(conf.getDepotId()) || StringUtils.isEmpty(conf.getMediaAddr())){
            log.error("---配置参数为空,不抓拍---,{}", conf);
            return null;
        }
 
        if(StringUtils.isEmpty(batchId)){
            log.error("---未获取到批次号,不抓拍---");
            return null;
        }
 
        String mediaAddr = CameraUtil.updateMediaAddr(conf.getMediaAddr(), conf.getLoginId(),
                conf.getPwd(), conf.getYtIp(), conf.getYtPort(), 1);
 
        ApiSnapReq req = new ApiSnapReq();
        req.setCompanyId(conf.getCompanyId());
        req.setDeptId(conf.getDeptId());
        req.setMediaAddr(mediaAddr);
        req.setBizId(batchId);
        req.setCameraName(BizType.QUANTITY.getCode());
        snapService.snapImg(req);
        return null;
    }
}