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
package com.mes.job;
 
import cn.smallbun.screw.core.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mes.common.S7object;
import com.mes.common.config.Const;
import com.mes.damage.entity.Damage;
import com.mes.damage.service.DamageService;
import com.mes.device.PlcParameterObject;
import com.mes.sysdict.entity.SysDictData;
import com.mes.sysdict.service.SysDictDataService;
import com.mes.temperingglass.entity.TemperingGlassInfo;
import com.mes.temperingglass.service.TemperingGlassInfoService;
import com.mes.temperingrecord.entity.TemperingRecord;
import com.mes.temperingrecord.service.TemperingRecordService;
import com.mes.tools.S7control;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Author : zhoush
 * @Date: 2024/7/17 12:47
 * @Description:
 */
@Component
@Slf4j
public class TemperingTask {
 
    @Autowired
    private TemperingRecordService temperingRecordService;
 
    @Autowired
    private TemperingGlassInfoService temporaryGlassInfoService;
 
    @Autowired
    private DamageService damageService;
 
    @Autowired
    private SysDictDataService sysDictDataService;
 
    private static final String ALONE_STATE = "0";
 
    @Value("${mes.width}")
    private Integer temperingWidth;
    @Value("${mes.height}")
    private Integer temperingHeight;
 
    @Scheduled(fixedDelay = 1000)
    public void temperingGlassBefore() {
        Date startDate = new Date();
        log.info("本次任务开始执行时间:{}", startDate);
        PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject;
        S7control plcControl = S7object.getinstance().plccontrol;
        String state = plcParameterObject.getPlcParameter("state").getValue();
//        当前连线状态为
        if (ALONE_STATE.equals(state)) {
            log.info("当前钢化炉连线模式为:{},(0:手动;1:连线),不执行该任务", state);
            return;
        }
        //将确认字置为0
        plcControl.writeWord(plcParameterObject.getPlcParameter("confirmationWord").getAddress(), 0);
        //可能有几炉玻璃同时在钢,需要获取钢化小片表中的数据信息状态为2(旋转台玻璃已送出未钢化,正在钢化)
        List<TemperingGlassInfo> temperingGlassInfoList = temporaryGlassInfoService.list(
                new LambdaQueryWrapper<TemperingGlassInfo>()
                        .inSql(TemperingGlassInfo::getTemperingLayoutId, "select distinct tempering_layout_id from tempering_glass_info where state=1")
                        .lt(TemperingGlassInfo::getState, Const.TEMPERING_START)
        );
        if (CollectionUtils.isEmpty(temperingGlassInfoList)) {
            log.info("当前系统没有需要钢化的玻璃信息");
            return;
        }
        TemperingGlassInfo minGlassInfo = temperingGlassInfoList.stream().min(Comparator.comparingLong(TemperingGlassInfo::getState)).get();
        if (minGlassInfo.getState() < Const.TEMPERING_DROP) {
            log.info("当前炉玻璃未到齐,稍后在执行");
            return;
        }
        //拿到工程id及炉号
        TemperingGlassInfo maxGlassInfo = temperingGlassInfoList.stream().max(Comparator.comparingLong(TemperingGlassInfo::getId)).get();
        //将正在钢化的炉号过滤掉,仅获取待钢化的玻璃:判断待钢的玻璃是否已发送对接数据
        List<TemperingRecord> temperingRecordList = temperingRecordService.list(new LambdaQueryWrapper<TemperingRecord>().eq(TemperingRecord::getEngineerId, maxGlassInfo.getEngineerId())
                .eq(TemperingRecord::getTemperingLayoutId, maxGlassInfo.getTemperingLayoutId()));
        if (CollectionUtils.isNotEmpty(temperingRecordList)) {
            log.info("没有待钢化的任务,结束");
            return;
        }
        //获取待钢化的所有玻璃信息
        List<TemperingGlassInfo> temperingGlassList = temperingGlassInfoList.stream()
                .filter(e -> e.getEngineerId().equals(maxGlassInfo.getEngineerId())
                        && e.getTemperingLayoutId().equals(maxGlassInfo.getTemperingLayoutId()))
                .sorted(Comparator.comparing(TemperingGlassInfo::getTemperingFeedSequence)).collect(Collectors.toList());
        //按照膜系获取玻璃材质
        SysDictData glassTypeSysDictData = sysDictDataService.getOne(new QueryWrapper<SysDictData>()
                .eq(StringUtils.isNotBlank(temperingGlassList.get(0).getFilmsid()), "dict_label", temperingGlassList.get(0).getFilmsid()));
 
        double sumArea = temperingGlassList.stream().mapToDouble(e -> e.getWidth() * e.getHeight()).sum();
        double maxArea = temperingGlassList.stream().mapToDouble(e -> e.getWidth() * e.getHeight()).max().orElse(0.0);
        double minArea = temperingGlassList.stream().mapToDouble(e -> e.getWidth() * e.getHeight()).min().orElse(0.0);
        double areaDifference = (10000 * (maxArea - minArea) / maxArea);
        double loadingRate = (int) (10000 * sumArea / (temperingHeight * temperingWidth));
        TemperingRecord temperingRecord = new TemperingRecord();
        temperingRecord.setEngineerId(maxGlassInfo.getEngineerId());
        temperingRecord.setTemperingLayoutId(maxGlassInfo.getTemperingLayoutId());
        temperingRecord.setGlassType(Integer.parseInt(glassTypeSysDictData.getDictTypeValue()));
        temperingRecord.setTemperingType(1);//全钢
        temperingRecord.setThickness(temperingGlassList.get(0).getThickness());
        temperingRecord.setAreaDifference(areaDifference);
        temperingRecord.setLoadingRate(loadingRate);
        temperingRecord.setState(Const.GLASS_STATE_NEW);
        //如果出现同一炉重读被处理,主键直接异常处理,不会发给plc钢化信息
        temperingRecordService.save(temperingRecord);
//      向plc发送钢化信息
        plcControl.writeWord(plcParameterObject.getPlcParameter("temperingLayoutId").getAddress(), maxGlassInfo.getTemperingLayoutId());
        plcControl.writeWord(plcParameterObject.getPlcParameter("filmsid").getAddress(), Integer.parseInt(glassTypeSysDictData.getDictTypeValue()));
        plcControl.writeWord(plcParameterObject.getPlcParameter("temperingType").getAddress(), 1);
        plcControl.writeWord(plcParameterObject.getPlcParameter("thickness").getAddress(), (int) (temperingGlassList.get(0).getThickness() * 10));
        plcControl.writeWord(plcParameterObject.getPlcParameter("areaDifference").getAddress(), (int) areaDifference);
        plcControl.writeWord(plcParameterObject.getPlcParameter("loadingRate").getAddress(), (int) loadingRate);
        //完成任务将确认字置为1
        plcControl.writeWord(plcParameterObject.getPlcParameter("confirmationWord").getAddress(), 1);
        Date endDate = new Date();
        log.info("本次任务结束时间:{},共耗时:{}ms", endDate, endDate.getTime() - startDate.getTime());
    }
 
    @Scheduled(fixedDelay = 1000)
    public void temperingGlassAfter() {
        //暂不处理
    }
 
    @Scheduled(fixedDelay = 1000)
    public void dealDamageTask() {
        Date startDate = new Date();
        log.info("钢化破损玻璃清除任务开始执行时间:{}", startDate);
        List<TemperingGlassInfo> temperingGlassInfoList = temporaryGlassInfoService.list(new LambdaQueryWrapper<TemperingGlassInfo>().in(TemperingGlassInfo::getState, Const.GLASS_STATE_DAMAGE_TAKE));
        if (CollectionUtils.isNotEmpty(temperingGlassInfoList)) {
            //获取破损/拿走玻璃id
            List<String> glassList = temperingGlassInfoList.stream().map(TemperingGlassInfo::getGlassId).collect(Collectors.toList());
            //将任务表中的数据删除
            temporaryGlassInfoService.remove(new LambdaQueryWrapper<TemperingGlassInfo>().in(TemperingGlassInfo::getGlassId, glassList));
            List<Damage> damageList = temperingGlassInfoList.stream().map(e -> {
                Damage damage = new Damage();
                damage.setGlassId(e.getGlassId());
                damage.setLine(Const.TEMPERING_OUT_TARGET_POSITION);
                damage.setWorkingProcedure("钢化");
                damage.setRemark("钢化");
                damage.setStatus(1);
                damage.setType(e.getState());
                return damage;
            }).collect(Collectors.toList());
            damageService.batchInsertDamage(damageList);
        }
        Date endDate = new Date();
        log.info("本次任务结束时间:{},共耗时:{}ms", endDate, endDate.getTime() - startDate.getTime());
    }
}