严智鑫
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
package com.northglass.service.chamfermachine;
 
import java.util.Date;
import java.util.List;
 
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.StateConstants.ConnectState;
import com.northglass.constants.StateConstants.GaoliweiMachineState;
import com.northglass.entity.ChamferMachine;
import com.northglass.entity.CountMachine;
import com.northglass.entity.GaoliweiMachine;
import com.northglass.entity.TestOut;
import com.northglass.listener.ChamferMachineClientListener;
import com.northglass.listener.GaoliweiMachineClientListener;
import com.northglass.repository.ChamferMachineDao;
import com.northglass.repository.CountMachineDao;
import com.northglass.repository.GaoliweiMachineDao;
import com.northglass.repository.TestOutDao;
import com.northglass.service.message.ChamferMessageProcessor;
import com.northglass.service.message.GaoliweiMessageProcessor;
 
@Component
@Transactional
public class ChamferMachineService {
    @Autowired
    private ChamferMachineDao chamferMachineDao;
    
    @Autowired
    private TestOutDao testOutDao;
    
    @Autowired
    private ChamferMessageProcessor processor;
    
    @Autowired
    private CountMachineDao countMachineDao;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ChamferMachineService.class);
    
    public void connect() {
        LOGGER.debug("> Start connect");
 
        List<ChamferMachine> chamferMachineList = chamferMachineDao.findAll();
 
        // 若监听线程为空,则新建监听线程
        for (ChamferMachine chamferMachine : chamferMachineList) {
            if (chamferMachine.getServerConnection().getThread() == null) {
                LOGGER.debug("创建新的倒角机【" + chamferMachine.getNumber() + "】监听线程!");
                Thread thread = new Thread(new ChamferMachineClientListener(chamferMachine,this));
                thread.start();
 
                setConnectState(chamferMachine, ConnectState.CONNECTING);
                chamferMachine.getServerConnection().setThread(thread);
            }
        }
 
        LOGGER.debug("> End connect");
    }
    
    public void setConnectState(ChamferMachine chamferMachine, String connectState) {
        chamferMachine.setConnectState(connectState);
        chamferMachine.setModifyTime(new Date());
        chamferMachineDao.save(chamferMachine);
    }
    
    public void resetState() {
        List<ChamferMachine> chamferMachineList = chamferMachineDao.findAll();
 
        for (ChamferMachine chamferMachine : chamferMachineList) {
            chamferMachine.setConnectState(ConnectState.NO_CONNECT);
            chamferMachine.setState(GaoliweiMachineState.STOPPED);
        }
        chamferMachineDao.save(chamferMachineList);
    }
    
    public ChamferMachine getById(Long id) {
        return chamferMachineDao.findOne(id);
    }
    
    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 String generateReturnMessage(String sendMessageHex, ChamferMachine chamferMachine) {
        return processor.generateReturnMessage(sendMessageHex, chamferMachine);
    }
    
    public void getright(String name) {
        TestOut testOut = new TestOut(name);
        testOut.setStart_time(new Date());
        testOutDao.save(testOut);
    }
    
    public CountMachine getCountMachine(ChamferMachine chamferMachine) {
        return countMachineDao.findOne(chamferMachine.getId());
    }
}