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;
|
}
|
|
|
|
/****
|
* 确认出库
|
* 删除之前的数据,重新生成,保证对应的顺序正确
|
*
|
* **/
|
}
|