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