package com.northglass.service.edgemachine;
|
|
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.CutMachineState;
|
import com.northglass.entity.EdgeMachine;
|
import com.northglass.entity.GaoliweiMachine;
|
import com.northglass.listener.EdgeMachineClientListener;
|
import com.northglass.repository.EdgeMachineDao;
|
import com.northglass.service.cutmachine.CutMachineService;
|
import com.northglass.service.message.EdgeMessageProcessor;
|
|
@Component
|
@Transactional
|
public class EdgeMachineService {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CutMachineService.class);
|
@Autowired
|
private EdgeMachineDao edgeMachineDao;
|
|
@Autowired
|
private EdgeMessageProcessor processor;
|
|
/***
|
* @author northglass
|
* 2019年6月3日
|
* 功能:线程连接方法
|
* ***/
|
public void connect() {
|
List<EdgeMachine> edgeMachines = edgeMachineDao.findAll();
|
|
// 若监听线程为空,则新建监听线程
|
for (EdgeMachine edgeMachine : edgeMachines) {
|
if (edgeMachine.getServerConnection().getThread() == null) {
|
LOGGER.debug("> 创建连接:"+edgeMachine.getIpAddress()+":"+edgeMachine.getPort());
|
Thread thread = new Thread(new EdgeMachineClientListener(edgeMachine,this));
|
thread.start();
|
setConnectState(edgeMachine, ConnectState.CONNECTING);
|
edgeMachine.getServerConnection().setThread(thread);
|
}
|
}
|
}
|
|
public void setConnectState(EdgeMachine edgeMachine,String string) {
|
edgeMachine.setConnectState(string);
|
edgeMachine.setModifyTime(new Date());
|
edgeMachineDao.save(edgeMachine);
|
}
|
|
//交互数据
|
public String processMessage(String message, EdgeMachine edgeMachine) {
|
return processor.generateReturnMessage(message, edgeMachine);
|
}
|
|
/***
|
* @author northglass
|
* 2019年6月3日
|
* 功能:起始归零
|
* ***/
|
public void resetState() {
|
List<EdgeMachine> edgeMachineList = edgeMachineDao.findAll();
|
|
for (EdgeMachine edgeMachine : edgeMachineList) {
|
edgeMachine.setConnectState(ConnectState.NO_CONNECT);
|
edgeMachine.setState(CutMachineState.STOPPED);
|
}
|
|
edgeMachineDao.save(edgeMachineList);
|
}
|
|
}
|