package com.northglass.service.Liedown; 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.CountMachine; import com.northglass.entity.GaoliweiMachine; import com.northglass.entity.LiedownMachine; import com.northglass.entity.TestOut; import com.northglass.listener.GaoliweiMachineClientListener; import com.northglass.listener.LiedownMachineClientListener; import com.northglass.repository.CountMachineDao; import com.northglass.repository.GaoliweiMachineDao; import com.northglass.repository.LiedownMachineDao; import com.northglass.repository.TestOutDao; import com.northglass.service.message.GaoliweiMessageProcessor; import com.northglass.service.message.LiedownMessageProcessor; @Component @Transactional public class LiedownMachineService { @Autowired private LiedownMachineDao liedownMachineDao; @Autowired private TestOutDao testOutDao; @Autowired private LiedownMessageProcessor processor; @Autowired private CountMachineDao countMachineDao; private static final Logger LOGGER = LoggerFactory.getLogger(LiedownMachineService.class); public void connect() { List liedownMachineList = liedownMachineDao.findAll(); // 若监听线程为空,则新建监听线程 for (LiedownMachine liedownMachine : liedownMachineList) { if (liedownMachine.getServerConnection().getThread() == null) { LOGGER.debug("> 创建连接:"+liedownMachine.getIpAddress()+":"+liedownMachine.getPort()); Thread thread = new Thread(new LiedownMachineClientListener(liedownMachine,this)); thread.start(); setConnectState(liedownMachine, ConnectState.CONNECTING); liedownMachine.getServerConnection().setThread(thread); } } } public void setConnectState(LiedownMachine liedownMachine, String connectState) { liedownMachine.setConnectState(connectState); liedownMachine.setModifyTime(new Date()); liedownMachineDao.save(liedownMachine); } public void resetState() { List liedownMachineList = liedownMachineDao.findAll(); for (LiedownMachine liedownMachine : liedownMachineList) { liedownMachine.setConnectState(ConnectState.NO_CONNECT); liedownMachine.setState(GaoliweiMachineState.STOPPED); } liedownMachineDao.save(liedownMachineList); } public LiedownMachine getById(Long id) { return liedownMachineDao.findOne(id); } public void getendtime(String name) { List 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, LiedownMachine liedownMachine) { return processor.generateReturnMessage(sendMessageHex, liedownMachine); } public void getright(String name) { TestOut testOut = new TestOut(name); testOut.setStart_time(new Date()); testOutDao.save(testOut); } public CountMachine getCountMachine(LiedownMachine liedownMachine) { return countMachineDao.findOne(liedownMachine.getId()); } }