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
package com.ld.igds.view.manager;
 
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.common.CoreSerService;
import com.ld.igds.common.dto.THDto;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.io.RemoteCommonService;
import com.ld.igds.io.RemoteManager;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.request.CheckThRequest;
import com.ld.igds.io.response.BaseResponse;
import com.ld.igds.models.DepotConf;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.order.ExeOrderService;
import com.ld.igds.order.data.ExeRequest;
import com.ld.igds.th.CoreThService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 仓库温湿度管理
 *
 * @author: andy.jia
 * @description:
 * @version:
 * @data:2020年2月17日
 */
@Slf4j
@Component
public class TempManager {
 
 
    @Autowired
    private CoreCommonService commonService;
    @Autowired
    private CoreThService thService;
    @Autowired
    private CoreSerService serService;
    @Autowired
    private RemoteManager remoteManager;
    @Autowired
    private ExeOrderService exeOrderService;
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
 
    private RemoteCommonService remoteCommonService;
 
 
    /**
     * 系统定时检测
     */
    public void scheduledCheck(String companyId) {
        try {
            //获取所有的仓库配置信息
            List<DepotConf> list = commonService.getCacheDepotConf(companyId);
 
            //查看温湿度的配置,同时从当前列表中获取到对应的分机信息
            CheckThRequest request;
            DeviceSer deviceSer;
            //避免重复
            Map<String, String> tempMap = new HashMap<>();
 
            String key;
            for (DepotConf conf : list) {
                if (StringUtils.isEmpty(conf.getThSer())) continue;
 
                key = conf.getThSer() + "_" + conf.getThConf();
 
                if (null != tempMap.get(key)) {
                    continue;
                }
                tempMap.put(key, conf.getThSer());
 
                deviceSer = serService.getCacheSer(companyId, conf.getThSer());
 
                if (null == deviceSer) {
                    log.warn("根据仓库参数的温湿度分机配置未获取到相应的分机……");
                    continue;
                }
 
                if (deviceSer.getStatus().equals(Constant.YN_N)) {
                    log.warn("当前分机={},不在线,不执行温湿度度检测。", deviceSer.getName());
                    notifyWebInvoker.notifyWeb(deviceSer.getCompanyId(), OrderRespEnum.ORDER_ERROR, BizType.SYS, "分机=" + deviceSer.getName() + "不在线,不执行温湿度检测");
                    //每个设备间隔200毫秒
                    Thread.sleep(300);
                    continue;
                }
 
                request = new CheckThRequest();
                request.setCompanyId(companyId);
                request.setThConf(conf.getThConf());
                request.setDepotId(conf.getDepotId());
                request.setIp(deviceSer.getIp());
                request.setPort(deviceSer.getPort());
                request.setSerId(deviceSer.getId());
                request.setDepotName(conf.getDepotName());
                request.setSerName(deviceSer.getName());
 
                request.setBizType(BizType.SYS.getCode());
 
                // 避免重复发送命令和添加操作记录
                ExeRequest exeRequest = new ExeRequest(request);
                exeRequest.setAddLogTag(false);
                exeRequest.setThConf(conf.getThConf());
 
                exeOrderService.checkExecute(exeRequest);
 
                //开始调用命令执行
                remoteCommonService = remoteManager.getRemoteCommonService(deviceSer.getProtocol());
                if (null == remoteCommonService) {
                    log.error("当前分机={}协议没有实现类", deviceSer.getName());
                    continue;
                }
                BaseResponse resp = remoteCommonService.checkTh(request);
                if (OrderRespEnum.ORDER_SUCCESS.getCode().equals(resp.getCode())) {
                    exeOrderService.addThCache(exeRequest);
                } else {
                    notifyWebInvoker.notifyWeb(request.getCompanyId(), OrderRespEnum.ORDER_ERROR, BizType.SYS, "粮情分机=" + deviceSer.getName() + "发送温湿度命令信息失败:" + resp.getMsg());
                }
 
                //每个设备间隔200毫秒
                Thread.sleep(200);
            }
 
        } catch (Exception e) {
            log.error("执行出错:{}", e.getMessage());
        }
    }
 
    public THDto getCacheTH(String companyId, String tempSer, String thConf) {
        return thService.getCacheTH(companyId, tempSer, thConf);
    }
 
    /**
     * 分机上线时对分机进行一次温湿度采集
     * @param companyId 分机组织编码
     * @param serId     分机id
     */
    public void checkThBySer(String companyId, String serId) {
        try {
            //获取所有的仓库配置信息
            List<DepotConf> list = commonService.getCacheDepotConf(companyId);
 
            //查看温湿度的配置,同时从当前列表中获取到对应的分机信息
            CheckThRequest request;
            DeviceSer deviceSer;
            //避免重复
            Map<String, String> tempMap = new HashMap<>();
 
            String key;
            for (DepotConf conf : list) {
                if (StringUtils.isEmpty(conf.getThSer())) {
                    continue;
                }
                if (!serId.equals(conf.getThSer())) {
                    continue;
                }
 
                key = conf.getThSer() + "_" + conf.getThConf();
                if (null != tempMap.get(key)) {
                    continue;
                }
                tempMap.put(key, conf.getThSer());
 
                deviceSer = serService.getCacheSer(companyId, conf.getThSer());
 
                if (null == deviceSer) {
                    log.warn("根据仓库参数的温湿度分机配置未获取到相应的分机……");
                    continue;
                }
 
                if (deviceSer.getStatus().equals(Constant.YN_N)) {
                    log.warn("当前分机={},不在线,不执行温湿度度检测。", deviceSer.getName());
                    notifyWebInvoker.notifyWeb(deviceSer.getCompanyId(), OrderRespEnum.ORDER_ERROR, BizType.SYS, "分机=" + deviceSer.getName() + "不在线,不执行温湿度检测");
                    //每个设备间隔200毫秒
                    Thread.sleep(300);
                    continue;
                }
                request = new CheckThRequest();
                request.setCompanyId(companyId);
                request.setThConf(conf.getThConf());
                request.setDepotId(conf.getDepotId());
                request.setIp(deviceSer.getIp());
                request.setPort(deviceSer.getPort());
                request.setSerId(deviceSer.getId());
                request.setDepotName(conf.getDepotName());
                request.setSerName(deviceSer.getName());
 
                request.setBizType(BizType.SYS.getCode());
 
                // 避免重复发送命令和添加操作记录
                ExeRequest exeRequest = new ExeRequest(request);
                exeRequest.setAddLogTag(false);
                exeRequest.setThConf(conf.getThConf());
 
                exeOrderService.checkExecute(exeRequest);
 
                //开始调用命令执行
                remoteCommonService = remoteManager.getRemoteCommonService(deviceSer.getProtocol());
                if (null == remoteCommonService) {
                    log.error("当前分机={}协议没有实现类", deviceSer.getName());
                    continue;
                }
                BaseResponse resp = remoteCommonService.checkTh(request);
                if (OrderRespEnum.ORDER_SUCCESS.getCode().equals(resp.getCode())) {
                    exeOrderService.addThCache(exeRequest);
                } else {
                    notifyWebInvoker.notifyWeb(request.getCompanyId(), OrderRespEnum.ORDER_ERROR, BizType.SYS, "粮情分机=" + deviceSer.getName() + "发送温湿度命令信息失败:" + resp.getMsg());
                }
 
                //每个设备间隔200毫秒
                Thread.sleep(200);
            }
 
        } catch (Exception e) {
            log.error("执行出错:{}", e.getMessage());
        }
    }
}