package com.northglass.service.message;
|
|
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.CountMachineTaskState;
|
import com.northglass.entity.ChamferMachine;
|
import com.northglass.entity.ChamferMachineTask;
|
import com.northglass.entity.MachineStatu;
|
import com.northglass.entity.ShelfStatu;
|
import com.northglass.repository.ChamferMachineTaskDao;
|
import com.northglass.repository.GaoliweiMachineTaskDao;
|
import com.northglass.repository.MachineStatuDao;
|
import com.northglass.repository.ShelfStatuDao;
|
import com.northglass.service.device.DeviceService;
|
import com.northglass.util.HexUtil;
|
|
@Component
|
@Transactional
|
public class ChamferMessageProcessor extends AbstractMessageProcessor {
|
// 倒角机
|
private static final Logger LOGGER = LoggerFactory.getLogger(ChamferMessageProcessor.class);
|
// 请求
|
private static final int TASK_ASK_START = 18;
|
private static final int TASK_ASK_END = 21;
|
// 请求ID
|
private static final int TASK_ASK_TD_START = 22;
|
private static final int TASK_ASK_ID_END = 25;
|
|
// 破损请求
|
private static final int DAMAGE_START = 46;
|
private static final int DAMAGE_END = 49;
|
// 破损请求Id
|
private static final int DAMAGE_ID_START = 50;
|
private static final int DAMAGE_ID_END = 53;
|
|
// 任务汇报字
|
private static final int TASK_FINISH_PLC_START = 38;
|
private static final int TASK_FINISH_PLC_END = 41;
|
|
// 任务汇报ID
|
private static final int TASK_FINISH_PLC_ID_START = 42;
|
private static final int TASK_FINISH_PLC_ID_END = 45;
|
|
// 任务发送字
|
private static final int TASK_MES_START = 58;
|
private static final int TASK_MES_END = 61;
|
|
// // 任务确认字
|
private static final int TASK_SURE_START = 98;
|
private static final int TASK_SURE_END = 101;
|
|
// 任务破损确认字
|
private static final int DAMAGE_SURE_START = 106;
|
private static final int DAMAGE_SURE_END = 109;
|
|
// 联机状态
|
private static final int AUTO_START = 158;
|
private static final int AUTO_END = 161;
|
|
// 任务报警字
|
private static final int TASK_WARN_START = 138;
|
private static final int TASK_WARN_END = 141;
|
|
@Autowired
|
private ShelfStatuDao shelfStatuDao;
|
|
@Autowired
|
private ChamferMachineTaskDao chamferMachineTaskDao;
|
|
public String generateReturnMessage(String sendMessageHex, ChamferMachine chamferMachine) {
|
// 请求信号
|
LOGGER.debug("收到倒角机信息:" + sendMessageHex);
|
if (sendMessageHex.length() == 24) {
|
String string = sendMessageHex.substring(8, 16);
|
if (string.equals("00060110")) {
|
// LOGGER.debug("倒角机消息发送成功!");
|
return "";
|
} else {
|
// LOGGER.debug("倒角机消息发送失败!");
|
return "";
|
}
|
} else if (sendMessageHex.length() == 198) {
|
String Texts[]=ResultTexts(sendMessageHex);
|
|
String taskAsk = Texts[0]; // 任务请求字
|
String taskIdAsk = Texts[1]; // 任务请求ID
|
String taskFinish = Texts[5]; // 任务完成字
|
String taskFinishId = Texts[6]; // 任务完成ID
|
String damageAsk = Texts[7]; // 破损汇报字
|
String damageIdAsk = Texts[8]; // 破损汇报ID
|
|
String taskMes = Texts[10]; // 任务发送字
|
String taskMesID = Texts[11]; // 任务发送字ID
|
String glassLong = Texts[12]; // 长
|
String glassWidth = Texts[13]; // 宽
|
String glassthinss = Texts[14]; // 厚
|
String chamfersize = Texts[15]; // 倒角大小
|
String mospeeh = Texts[16]; // 进片输送台和2#磨边机配速速度
|
String finishSure = Texts[20]; // 任务完成确认
|
String damageSure = Texts[22]; // 任务破损确认字
|
String damageSureID = Texts[22]; // 任务破损确认字ID
|
String taskWarning = Texts[30]; // 任务报警字
|
|
String auto = Texts[35];// 联机信息
|
String message = sendMessageHex.substring(TASK_ASK_START);// 信息
|
|
ShelfStatu statu = shelfStatuDao.findByMachine("chamfer" + chamferMachine.getId());
|
ShelfStatu Gaoliweistatus = shelfStatuDao.findByMachine("gaoliwei"+chamferMachine.getId());
|
String Gaoliweispeeh=Gaoliweistatus.getMessage().substring(12,16);
|
if (statu != null) {
|
statu.setMessage(message);
|
statu.setAutostate(auto);
|
statu.setSetTime(new Date());
|
shelfStatuDao.save(statu);
|
}
|
String PLCString="PLC: \n"+
|
"||请求信号:" + taskAsk+
|
"||请求ID:" + taskIdAsk+
|
"||任务汇报字:" + taskFinish +
|
"||任务汇报字ID:" + taskFinishId +
|
"||破损请求:" + damageAsk+
|
"||破损请求ID::" + damageIdAsk;
|
LOGGER.debug(PLCString);
|
|
String MesString="Mes: \n"+
|
"||任务发送字:" + taskMes+
|
"||任务发送字ID:" + taskMesID+
|
"||任务完成确认:" + finishSure+
|
"||任务破损确认字:" + damageSure+
|
"||长:" + glassLong+
|
"||宽:" + glassWidth+
|
"||任务报警字:" + taskWarning+
|
"||进片输送台和2#磨边机配速速度:" + mospeeh;
|
LOGGER.debug(MesString);
|
//破损
|
if(damageAsk.equals("0001")){
|
if(damageSure.equals("0000")){
|
ChamferMachineTask task=chamferMachineTaskDao.SelectTaskByINfoId(chamferMachine.getId(), damageIdAsk);
|
task.setState("破损");
|
chamferMachineTaskDao.save(task);
|
return message("0001","1b6e");
|
}
|
}else if(damageAsk.equals("0000")){
|
if(damageSure.equals("0001")){
|
return message("0000","1b6e");
|
}
|
}
|
//完成任务
|
if(taskFinish.equals("0001")){
|
if(finishSure.equals("0000")){
|
//PLC已完成,MES进行数据完成 ,完成确认字至1
|
ChamferMachineTask task =chamferMachineTaskDao.SelectTaskByINfoId(chamferMachine.getId(),Integer.parseInt(taskFinishId,16)+"");
|
if(task!=null){
|
task.setState("已完成");
|
task.setCompleteTime(new Date());
|
chamferMachineTaskDao.save(task);
|
return message("0001", "1b6c");
|
}
|
LOGGER.debug("没有此任务!");
|
}
|
}else{
|
if(finishSure.equals("0001")){
|
//PLC清空,MES未清空:完成确认字至0
|
return message("0000", "1b6c");
|
}
|
}
|
if (taskAsk.equals("0001")) {
|
if(taskMes.equals("0000")){
|
//请求数据
|
List<ChamferMachineTask> tasks =chamferMachineTaskDao.findWaitingTasksByChamferMachine(chamferMachine.getId());
|
if (tasks.size()>0) {
|
ChamferMachineTask task = tasks.get(0);
|
if(task!=null){
|
//发送数据:任务发送字+infoid+长+宽+厚+倒角大小+磨边机速度
|
String date = "0001"+HexUtil.intTo2ByteHex(Integer.valueOf(task.getInfoId()))+
|
HexUtil.intTo2ByteHex(new Double(task.getLength()).intValue())+
|
HexUtil.intTo2ByteHex(new Double(task.getWidth()).intValue())+
|
HexUtil.intTo2ByteHex(new Double(task.getGlass().getThickness()).intValue())+
|
"0000"+
|
Gaoliweispeeh;
|
task.setState("正在工作");
|
DeviceService.WriteFile("D:", "倒角记录", new Date()+"\rPLC接到数据:"+message);
|
return message(date, "1b62");
|
}
|
|
}
|
}
|
/*if(!taskMesID.equals("0000")){
|
return message("0001", "1b62");
|
}else{
|
//请求数据
|
List<ChamferMachineTask> tasks =chamferMachineTaskDao.findWaitingTasksByChamferMachine(chamferMachine.getId());
|
if (tasks.size()>0) {
|
ChamferMachineTask task = tasks.get(0);
|
//发送数据:任务发送字+infoid+长+宽+厚+倒角大小+磨边机速度
|
String date = HexUtil.intTo2ByteHex(Integer.valueOf(task.getInfoId()))+
|
HexUtil.intTo2ByteHex(new Double(task.getLength()).intValue())+
|
HexUtil.intTo2ByteHex(new Double(task.getWidth()).intValue())+
|
HexUtil.intTo2ByteHex(new Double(task.getGlass().getThickness()).intValue())+
|
"0000"+
|
Gaoliweispeeh;
|
task.setState("正在工作");
|
return message(date, "1b63");
|
}
|
}*/
|
} else {
|
if (taskMes.equals("0001")) {
|
//return message("0000", "1b6c");
|
return message("0000"+"0000"+"0000"+"0000"+"0000"+"0000", "1b62");
|
}
|
}
|
|
return message(Gaoliweispeeh,"1b68");
|
}
|
|
return "";
|
}
|
//得到所有的45个字
|
private String[] ResultTexts(String Str) {
|
String []InfoTex =new String[45];
|
Str=Str.substring(18);
|
for(int i=0;i<45;i++){
|
InfoTex[i]=Str.substring(0,4);
|
Str=Str.substring(4);
|
}
|
return InfoTex;
|
}
|
private String message(String senddate, String address) {
|
String Herd = "0110" + address;
|
int length = senddate.length() / 4;
|
String dates = Herd + HexUtil.intTo2ByteHex(length) + HexUtil.intTo1ByteHex(length * 2) + senddate;
|
int lengths = dates.length() / 2;
|
String date = "00000000" + HexUtil.intTo2ByteHex(lengths) + dates;
|
// LOGGER.debug("发送信息内容:" + date);
|
return date;
|
}
|
}
|