package com.example.springboot.service;
|
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.List;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import com.example.springboot.component.PLCAutoMes;
|
import com.example.springboot.component.S7control;
|
import com.example.springboot.entity.GlassInfo;
|
import com.example.springboot.entity.StorageCage;
|
import com.example.springboot.entity.StorageTask;
|
import com.example.springboot.entity.device.PlcParameterObject;
|
import com.example.springboot.mapper.AlbaniaMapper;
|
import com.google.common.primitives.Bytes;
|
|
@Service
|
public class SpianServiceNew {
|
@Autowired
|
private AlbaniaMapper albaniaMapper;
|
@Autowired
|
private StorageCageService storageCageService;
|
// 读取DB105区文件
|
private PlcParameterObject plcmes = PLCAutoMes.PlcMesObject;
|
|
public Short selectAll(GlassInfo glassInfo) {
|
if (glassInfo == null) {
|
// 有进片请求但是测量台没有玻璃
|
return 400;
|
}
|
|
System.out.println(glassInfo + "初始");
|
// 定义初始字段
|
StorageCage cage;
|
String glassid = glassInfo.getGlassid();
|
Double width = glassInfo.getWidth();
|
Double height = glassInfo.getHeight();
|
String flowcard = glassInfo.getFlowcard();
|
Integer glasstype = glassInfo.getGlasstype();
|
Double thickness = glassInfo.getThickness();
|
GlassInfo glassfilms = albaniaMapper.SelectGlassinfo(glasstype);
|
// 判断是否有同类型的
|
cage = albaniaMapper.SelectCage(glasstype, width, 300);
|
// 如果有同类型时直接增加
|
if (cage != null) {
|
// 发送plc任务
|
Mestast(glassid, 1001, cage.getId(), 1, "MESID1", 1);
|
System.out.println("任务发送玻璃id:" + glassid + "起始位置:" + 1001 + "格子号:" + cage.getId() + "启动:" + 1);
|
|
} else {
|
// 判断玻璃是否进入大片笼
|
if (glassInfo.getThickness() > 14) {
|
cage = albaniaMapper.SelectNewCell(3, 5);
|
} else {
|
cage = albaniaMapper.SelectNewCell(0, 4);
|
|
// System.out.println(cage + "理片笼判断");
|
}
|
// 当小片笼子不够,则符合条件的可以进入大片笼
|
if (cage == null && glassInfo.getWidth() >= 1500 && glassInfo.getThickness() > 4) {
|
cage = albaniaMapper.SelectNewCell(3, 5);
|
}
|
// 当返回的格子号为空时,返回400笼子已满
|
if (cage != null) {
|
albaniaMapper.AddCage(cage.getId(), glassid, width, height, 1, glasstype, 0, thickness,
|
glassfilms.getFilms(), glassfilms.getFlowcard(), glassfilms.getMateid(), glassfilms.getTier());
|
Mestast(glassid, 1001, cage.getId(), 1, "MESID1", 1);
|
System.out.println("任务发送玻璃id:" + glassid + "起始位置:" + 1001 + "格子号:" + cage.getId() + "启动:" + 1);
|
albaniaMapper.UpdateStorage(width, height, cage.getId());
|
} else {
|
return (400);
|
}
|
}
|
// 增加任务记录
|
albaniaMapper.Inserttask(0, 0, 1001, cage.getId(), glassid, glasstype, flowcard, glassfilms.getMateid(),
|
glassfilms.getTier());
|
albaniaMapper.UpdateQueueState(glassid);
|
return (200);
|
|
}
|
|
// 补充出片
|
public Short mateOut() {
|
double carwidth = -300;
|
|
List<StorageTask> glassmate = albaniaMapper.SelectOutTask();
|
String type = glassmate.get(0).getTaskType();
|
if (glassmate.size() == 0) {
|
return (400);
|
} else {
|
for (int i = 0; i < glassmate.size(); i++) {
|
GlassInfo glass = albaniaMapper.SelectGlassInfo(glassmate.get(i).getGlasstype());
|
carwidth = carwidth + glass.getWidth() + 300;
|
if (carwidth >= 4300 && type != glassmate.get(i).getTaskType()) {
|
S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 出片任务启动
|
MestoPlc();
|
return (200);
|
} else {
|
// 增加一条任务记录
|
albaniaMapper.Inserttask(1, 0, Integer.parseInt(glassmate.get(i).getShelfRack()), 2001,
|
glassmate.get(i).getGlassId(), glassmate.get(i).getGlasstype(),
|
glassmate.get(i).getFlowcard(), glassmate.get(i).getMateid(), glassmate.get(i).geTier());
|
// 完成出片队列的任务
|
albaniaMapper.UpdateOutTask(glassmate.get(i).getId());
|
// 发送plc任务
|
Mestast(glassmate.get(i).getGlassId(),
|
Integer.parseInt(glassmate.get(i).getShelfRack()), 2001, 0, "MESID" + (i + 1), i + 1);
|
System.out.println("发送出片队列配片任务:" + glassmate.get(i).getShelfRack());
|
}
|
}
|
// 本次配片完成发送启动命令
|
// S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),
|
// (short) 1);// 出片任务启动
|
while (true) {
|
String currentValue = plcmes.getPlcParameter("MESToPLC").getValue();
|
// 检查 MESToPLC 的值是否为 0
|
if (currentValue.equals("1")) {
|
System.out.println("发送启动字现在为1,停止循环写入");
|
break; // 退出循环
|
}
|
// 如果不为 1,继续写入
|
S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 任务发送字
|
System.out.println("继续写入 MESToPLC,当前值:" + currentValue);
|
try {
|
Thread.sleep(100); // 例如每秒检查一次
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
MestoPlc();
|
|
}
|
return (200);
|
}
|
|
// 出片
|
public Short selectout(int line) {
|
GlassInfo glassmate = new GlassInfo();
|
GlassInfo method = new GlassInfo();
|
StorageCage glass = new StorageCage();
|
int glassend = 0;
|
double carwidth = -300;
|
double width = 0;
|
|
method = albaniaMapper.SelectMethod(line);
|
if (method == null) {
|
return (400);
|
}
|
// 如果不是直通模式只出有完整配片的流程卡
|
if (method.getMethod() == 0) {
|
// glassmate= albaniaMapper.SelectOutGlass(1);
|
// 判断有数量的配片id
|
List<GlassInfo> peipianid = albaniaMapper.SelectOutGlass3(method.getFlowcard());
|
if (peipianid.size() == 0) {
|
System.out.println("peipianid:" + peipianid);
|
return (400);
|
} else {
|
for (int j = 0; j < peipianid.size(); j++) {
|
// 判断配片宽度是否够上大车
|
// GlassInfo glasswidth =
|
// albaniaMapper.SelectOutGlass6(peipianid.get(j).getFlowcard(),peipianid.get(j).getMateid());
|
// if (glasswidth.getWidth() <= 4300) {
|
// 是否有足够的库存数量
|
List<String> peipiancage = albaniaMapper.SelectOutGlass4(peipianid.get(j).getFlowcard(),
|
peipianid.get(j).getMateid());
|
// 是否有足够的可出片数量
|
List<String> peipiancage2 = albaniaMapper.SelectOutGlass8(peipianid.get(j).getFlowcard(),
|
peipianid.get(j).getMateid());
|
// System.out.println("peipiancage:"+peipiancage+peipianid.get(j).getMateid());
|
// System.out.println("peipiancage2:"+peipiancage2);
|
if (!peipiancage.contains("不足")) {
|
List<GlassInfo> chupian = albaniaMapper.SelectOutGlass7(peipianid.get(j).getFlowcard(),
|
peipianid.get(j).getMateid());
|
for (int k = 0; k < chupian.size(); k++) {
|
glass = albaniaMapper.SelectCageGlass(chupian.get(k).getGlasstype());
|
carwidth = carwidth + glass.getGlassWidth() + 300;
|
if (carwidth <= 4300) {
|
// 发送配片数据
|
Mestast(chupian.get(k).getGlassid(), glass.getId(), 2001, 0, "MESID" + (k + 1),
|
k + 1);
|
albaniaMapper.AddFinishNumber(chupian.get(k).getFlowcard(),
|
chupian.get(k).getMateid(), chupian.get(k).getTier());
|
storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
|
albaniaMapper.Inserttask(1, 0, glass.getId(), 2001, chupian.get(k).getGlassid(),
|
chupian.get(k).getGlasstype(), chupian.get(k).getFlowcard(),
|
chupian.get(k).getMateid(), chupian.get(k).getTier());
|
} else {
|
albaniaMapper.AddFinishNumber(chupian.get(k).getFlowcard(),
|
chupian.get(k).getMateid(), chupian.get(k).getTier());
|
storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
|
// 增加出片队列的任务
|
albaniaMapper.InsertOutTask(1, 0, glass.getId(), 2001, chupian.get(k).getGlassid(),
|
chupian.get(k).getGlasstype(), chupian.get(k).getFlowcard(),
|
chupian.get(k).getMateid(), chupian.get(k).getTier());
|
}
|
|
}
|
// 本次配片完成发送启动命令
|
// S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),(short)
|
// 1);// 出片任务启动
|
MestoPlc();
|
return (200);// 结束
|
}
|
// }
|
// else {
|
// continue;
|
// }
|
}
|
System.out.println("未有完整配片");
|
return (400);
|
}
|
|
} else {
|
for (int i = 1; i <= 7; i++) {
|
// 直通模式时按顺序直出
|
// glassmate = albaniaMapper.SelectOutGlass(1);
|
glassmate = albaniaMapper.SelectOutGlass2();
|
|
System.err.println("出片:" + glassmate);
|
if (glassmate == null && i > 1) {
|
// 本次配片完成发送启动命令
|
// 出片任务启动
|
MestoPlc();
|
System.out.println("出完任务:1,任务状态:0,起始:" + glass.getId() + ",目标" + "2001");
|
return (200);// 结束
|
}
|
if (glassmate == null) {
|
// 未领取任务
|
System.out.println("未领取任务:" + i);
|
return (400);
|
|
} else {
|
glass = albaniaMapper.SelectCageGlass(glassmate.getGlasstype());
|
System.out.println("出片glass:" + glass);
|
if (glass != null) {
|
// 发送任务之前判断车上的剩余宽度是否大于此玻璃
|
width = width + glassmate.getWidth() + 300;
|
// 大于车上空余宽度时提前发送出片启动
|
if (width > 4300) {
|
// 本次配片完成发送启动命令
|
MestoPlc();
|
return (200);// 结束
|
}
|
// 发送配片数据
|
Mestast(glass.getGlassId(), glass.getId(), 2001, 0, "MESID" + i, i);
|
albaniaMapper.AddFinishNumber(glassmate.getFlowcard(), glassmate.getMateid(),
|
glassmate.getTier());
|
storageCageService.UpdateStroageCageByCell(glass.getId(), -1);// 玻璃数量-1
|
albaniaMapper.Inserttask(1, 0, glass.getId(), 2001, glassmate.getGlassid(),
|
glassmate.getGlasstype(), glass.getFlowcard(), glassmate.getMateid(),
|
glassmate.getTier());
|
} else {
|
return (400);// 结束
|
}
|
|
}
|
}
|
}
|
return (200);// 结束
|
}
|
|
// 派发任务玻璃id
|
public void outmesid(String glassid, String address) {
|
// 读取DB105区文件
|
PlcParameterObject plcmes = PLCAutoMes.PlcMesObject;
|
// System.out.println("outmesid:" + glassid);
|
List<Byte> glassidlist = new ArrayList();
|
char ds[] = glassid.toCharArray();
|
for (char iditem : ds) {
|
glassidlist.add((byte) iditem);
|
}
|
byte[] bytes = Bytes.toArray(glassidlist);
|
// writeString
|
System.out.println("outmesidbytes:" + bytes.length);
|
System.out.println(plcmes.getPlcParameter(address).getAddress());
|
System.out.println();
|
|
}
|
|
// 获取地址内的玻璃id转字符串
|
public StringBuilder queGlassid(String address, int count) {
|
StringBuilder writedstrIdOut = new StringBuilder();
|
byte[] writedglassidbytesOut = S7control.getinstance().ReadByte(address, count);
|
if (writedglassidbytesOut != null) {
|
// 获取玻璃id
|
for (byte iditem : writedglassidbytesOut) {
|
writedstrIdOut.append((char) iditem);
|
}
|
}
|
return writedstrIdOut;
|
}
|
|
// char数组转化成bit数组
|
public static byte[] toBytes(char[] chars) {
|
String s = new String(chars);
|
return s.getBytes(StandardCharsets.UTF_8);
|
}
|
|
// bit数组转化成char数组
|
public static char[] toChars(byte[] bytes) {
|
String s = new String(bytes, StandardCharsets.UTF_8);
|
return s.toCharArray();
|
}
|
|
// 判断集合里是否为1
|
public boolean listbool(String address) {
|
List<Short> list = S7control.getinstance().ReadWord(address, 1);// 返回为ture时为1
|
boolean listbool = list.contains((short) 1);
|
return listbool;
|
}
|
|
// 下发理片任务
|
public void Mestast(String glassid, int MESToPLCStart1, int MESToPLCTarget1, int MESToPLC, String address, int i) {
|
S7control.getinstance().writeString(plcmes.getPlcParameter(address).getAddress(), glassid);// 玻璃id
|
S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCStart" + i).getAddress(),
|
(short) MESToPLCStart1);// 起始位置
|
S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLCTarget" + i).getAddress(),
|
(short) MESToPLCTarget1);// 目标位置
|
if (MESToPLC != 0) {
|
// S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(),
|
// (short) MESToPLC);// mes理片任务类型
|
MestoPlc();
|
}
|
}
|
|
// 扫码增加队列任务
|
public boolean scan(String address) {
|
List<Short> list = S7control.getinstance().ReadWord(address, 1);// 返回为ture时为1
|
boolean listbool = list.contains((short) 1);
|
return listbool;
|
}
|
|
public void MestoPlc() {
|
while (true) {
|
String currentValue = plcmes.getPlcParameter("MESToPLC").getValue();
|
// 检查 MESToPLC 的值是否为 0
|
if (currentValue.equals("1")) {
|
System.out.println("发送启动字现在为1,停止循环写入");
|
break; // 退出循环
|
}
|
// 如果不为 1,继续写入
|
S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToPLC").getAddress(), (short) 1);// 任务发送字
|
System.out.println("继续写入 MESToPLC,当前值:" + currentValue);
|
try {
|
Thread.sleep(100); // 例如每秒检查一次
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
}
|