严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
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
package com.northglass.service.measuremachine;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import com.northglass.constants.FunctionNumber.MeasureMachineFunctionNumber;
import com.northglass.constants.StateConstants.ConnectState;
import com.northglass.constants.StateConstants.IdentifyMachineState;
import com.northglass.constants.StateConstants.PreprocessingGlassState;
import com.northglass.entity.Glass;
import com.northglass.entity.MeasureMachine;
import com.northglass.entity.TestOut;
import com.northglass.entity.TougheningGlass;
import com.northglass.listener.MeasureMachineClientListener;
import com.northglass.repository.ArrangeMachineRankDao;
import com.northglass.repository.ArrangeMachineTaskDao;
import com.northglass.repository.GlassDao;
import com.northglass.repository.MeasureMachineDao;
import com.northglass.repository.TestOutDao;
import com.northglass.repository.TougheningGlassDao;
import com.northglass.service.message.MeasureMachineMessageProcessor;
import com.northglass.util.CRCUtil;
import com.northglass.util.HexUtil;
 
@Component
@Transactional
public class MeasureMachineService {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(MeasureMachineService.class);
 
    @Autowired
    private MeasureMachineDao measureMachineDao;
 
    @Autowired
    private TougheningGlassDao tougheningGlassDao;
 
    @Autowired
    private GlassDao glassDao;
 
    @Autowired
    private TestOutDao testOutDao;
 
    @Autowired
    private MeasureMachineMessageProcessor processor;
 
    @Autowired
    private ArrangeMachineRankDao arrangeMachineRankDao;
 
    @Autowired
    private ArrangeMachineTaskDao arrangeMachineTaskDao;
 
    private static final String AGAIN_GLASS_DATA = "011000170001020002";
 
    private static final String ALLOW_GLASS_DATA = "0110001600010200016566";
 
    protected static final String END = "3c454f463e";
 
    protected static final String HEAD = "3c5354413e";
 
    /**
     * 加密方式00,表示不加密
     */
    protected static final String NO_ENCRYPT = "00";
    /**
     * 备用,共8个字节
     */
    protected static final String BACKUP = "0000000000000000";
 
    public MeasureMachine getById(Long id) {
        return measureMachineDao.findOne(id);
    }
 
    public List<MeasureMachine> getAll() {
        return measureMachineDao.findAll();
    }
 
    public void setConnectState(MeasureMachine measureMachine, String connectState) {
        measureMachine.setConnectState(connectState);
        measureMachineDao.save(measureMachine);
    }
 
    public void setState(MeasureMachine measureMachine, String state) {
        measureMachine.setState(state);
        measureMachineDao.save(measureMachine);
    }
 
    public String processMessage(String messageHex, MeasureMachine measureMachine) {
        return processor.generateReturnMessage(messageHex, measureMachine);
    }
 
    public TougheningGlass saveTougheningGlass(TougheningGlass tougheningGlass) {
        return tougheningGlassDao.save(tougheningGlass);
    }
 
    public Glass saveGlass(Glass glass) {
        return glassDao.save(glass);
    }
 
    public MeasureMachine saveMeasureMachine(MeasureMachine measureMachine) {
        return measureMachineDao.save(measureMachine);
    }
 
    /**
     * 根据玻璃尺寸查找匹配玻璃信息, - 若查到匹配玻璃,则返回该玻璃,设置处理序号,设置状态为待打标(已识别后状态即为待打标) -
     * 若未查到匹配玻璃,则返回null
     * 
     * @param length
     * @param width
     * @return
     */
    public Glass findMeasuredGlass(double length, double width) {
        List<Glass> glassList = glassDao.findToMeasureGlass();
 
        for (Glass glass : glassList) {
            // 当前玻璃状态为正在识别,比较长和宽
            // 若长和宽误差都在±5以内,则识别通过,否则就识别不通过
            if ((length >= (glass.getLength() - 3) && length <= (glass.getLength() + 3)
                    && width >= (glass.getWidth() - 3) && width <= (glass.getWidth() + 3))
                    && glass.getPieces()>glass.getCompletePieces()
                    || (length >= (glass.getWidth() - 3) && length <= (glass.getWidth() + 3)
                            && width >= (glass.getLength() - 3) && width <= (glass.getLength() + 3))
                    && glass.getPieces()>glass.getCompletePieces()) {
                return glass;
            }
        }
 
        return null;
    }
 
    public void resetState() {
        List<MeasureMachine> machineList = measureMachineDao.findAll();
 
        for (MeasureMachine machine : machineList) {
            machine.setConnectState(ConnectState.NO_CONNECT);
            machine.setState(IdentifyMachineState.STOPPED);
        }
 
        measureMachineDao.save(machineList);
    }
 
    public void connect() {
        LOGGER.debug("> Start connect");
 
        List<MeasureMachine> machineList = measureMachineDao.findAll();
 
        // 若监听线程为空,则新建监听线程
        for (MeasureMachine machine : machineList) {
            if (machine.getServerConnection().getThread() == null) {
                LOGGER.debug("创建新的立式测量【" + machine.getNumber() + "】监听线程!");
                Thread thread = new Thread(new MeasureMachineClientListener(machine,this));
                thread.start();
 
                setConnectState(machine, ConnectState.CONNECTING);
                machine.getServerConnection().setThread(thread);
            }
        }
 
        LOGGER.debug("> End connect");
    }
 
    public void clearData() {
//        processor.setPreviousData("");
    }
 
    public void getendtime(String name) {
        List<TestOut> testOuts = testOutDao.findByName(name);
        if (testOuts.size() > 0) {
            TestOut testOut = testOuts.get(testOuts.size() - 1);
            if (testOut.getEnd_time() != null && !testOut.getEnd_time().equals("")) {
                TestOut test = new TestOut(name);
                test.setStart_time(new Date());
                testOutDao.save(test);
            } else {
                testOut.setEnd_time(new Date());
                testOutDao.save(testOut);
            }
        } else {
            TestOut testOut = new TestOut(name + "start");
            testOut.setStart_time(new Date());
            testOutDao.save(testOut);
        }
    }
 
    public void getright(String name) {
        TestOut testOut = new TestOut(name);
        testOut.setStart_time(new Date());
        testOutDao.save(testOut);
    }
 
 
    public void failure(Long preprocessingGlassId) {
        TougheningGlass glass = tougheningGlassDao.findOne(preprocessingGlassId);
        glass.setState(PreprocessingGlassState.MATCH_FAILED);
        tougheningGlassDao.save(glass);
    }
 
    // 重测
    public String getAgainMessage() {
        String head = generateHead(AGAIN_GLASS_DATA, MeasureMachineFunctionNumber.ALLOW_PASS);
        LOGGER.debug("重测: " + head + CRCUtil.sourceCRC(AGAIN_GLASS_DATA) + END);
        return head + AGAIN_GLASS_DATA + END;
    }
 
    // 允许送片
    public String getallowMessage() {
        String head = generateHead(ALLOW_GLASS_DATA, MeasureMachineFunctionNumber.ALLOW_PASS);
        LOGGER.debug("放过: " + head + ALLOW_GLASS_DATA + END);
        return head + ALLOW_GLASS_DATA + END;
    }
 
    /**
     * 构造消息头部
     * 
     * 通信头部 = 开始标志 + 信息长度 + 订单编号 + 功能号 + 加密方式 + 发送时刻 + 备用
     * 
     * @param data
     * @param functionNumber
     * @return
     */
    public static String generateHead(String data, String functionNumber) {
        // 信息长度
        int length = (data + END).length() / 2;
 
        // 订单编号待定
        String orderNumber = "000000000000000000000000";
 
        String head = HEAD + HexUtil.intTo2ByteHex(length) + orderNumber + functionNumber + NO_ENCRYPT
                + HexUtil.timeToHex(new Date()) + BACKUP;
 
        return head;
    }
 
 
 
/****
 * 确认出库
 * 删除之前的数据,重新生成,保证对应的顺序正确
 * 
 * **/
}