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
package com.ld.igds.timer;
 
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.DeviceType;
import com.ld.igds.io.request.ExeDevice;
import com.ld.igds.lamp.LampManager;
import com.ld.igds.lamp.dto.LampParam;
import com.ld.igds.models.Device;
import com.ld.igds.util.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 照明定时控制
 * @author chen
 */
@Slf4j
@Service(Constant.JOB_BEAN_LAMP)
public class JobLampService implements Job {
 
    @Resource
    private LampManager lampManager;
    @Resource
    private CoreDeviceService coreDeviceService;
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("=======================开始执行照明定时控制=============================");
 
        // 获取JOB的ID
        String key = context.getTrigger().getKey().getName();
        String jobId = key.substring(7);
 
        List<Device> listDevice = coreDeviceService.getDeviceByTimId(null, jobId);
 
 
        if (null == listDevice || listDevice.isEmpty()) {
            log.info("照明定时控制执行中没有找到需要操作的设备信息,定时执行失败!");
            return;
        }
        ExeDevice exeDevice;
 
        // 对设备进行调整,按照分机进行分组以便调用执行
        Map<String, List<ExeDevice>> deviceMap = new HashMap<>();
        //存放分机与仓库编码关系
        Map<String, String> depotMap = new HashMap<>();
 
        for (Device d : listDevice) {
            if (DeviceType.TYPE_06.getCode().equals(d.getType())) {
                exeDevice = new ExeDevice();
                BeanUtils.copyProperties(d, exeDevice);
 
                if (null == deviceMap.get(exeDevice.getSerId())) {
                    deviceMap.put(exeDevice.getSerId(), new ArrayList<>());
                }
 
                deviceMap.get(exeDevice.getSerId()).add(exeDevice);
                depotMap.put(exeDevice.getSerId(), exeDevice.getDepotId());
            }
        }
 
        if (deviceMap.isEmpty()) {
            log.info("照明定时控制执行中没有找到需要操作的设备信息,定时执行失败!");
            return;
        }
 
        try {
            LampParam param ;
            for (String mapKey : deviceMap.keySet()) {
                param = new LampParam();
                param.setCompanyId(ContextUtil.getDefaultCompanyId());
                param.setDepotId(depotMap.get(mapKey));
                param.setSerId(mapKey);
                param.setDeviceList(deviceMap.get(mapKey));
                lampManager.controlDevice2(param);
                Thread.sleep(2000);
            }
        } catch (Exception e) {
            log.error("照明定时控制执行发生错误,执行失败!", e);
        }
    }
}