fix: mes对接

This commit is contained in:
2023-09-20 17:18:53 +08:00
parent 852a705ffb
commit d8d9c21be6
44 changed files with 1350 additions and 220 deletions

View File

@@ -4,6 +4,9 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Date;
/**
* 线程父类:抽象类
*/
@Slf4j
public abstract class AbstractAutoRunnable implements Runnable {
private ThreadStatusEnum status;

View File

@@ -4,15 +4,40 @@ import java.util.List;
import java.util.Map;
public interface AutoRunService {
/** 启动线程 */
void startThread(String arg0);
/**
* 停止线程
* @param arg0
*/
void stopThread(String arg0);
/**
* 获取线程
* @param arg0线程编码
* @param arg1线程ID
* @return
*/
ThreadDto findByCode(String arg0, String arg1);
/**
* 获取所有线程
* @return
*/
List<ThreadDto> findAll();
/**
* 根据条件获取线程
* @param whereJson
* @return
*/
List<ThreadDto> findByCondition(Map whereJson);
/**
* 通过线程编码获取线程
* @param code线程key
* @return
*/
Thread getThreadByCode(String code);
}

View File

@@ -15,11 +15,26 @@ import java.util.*;
@Service
@Slf4j
public class AutoRunServiceImpl implements AutoRunService, ApplicationAutoInitial {
/**
* 获取所有继承次抽象类->AbstractAutoRunnable的bean对象
*/
@Autowired(required = false)
private List<AbstractAutoRunnable> abstractAutoRunnableList;
/**
* 线程索引(类名)
*/
private List<String> autoRun_code_index = new ArrayList();
/**
* 存放线程
*/
private List<Thread> threads = new ArrayList();
/**
* 存放AbstractAutoRunnable的子类
*/
private List<AbstractAutoRunnable> thread_autoRun = new ArrayList();
/**
* 存储运行中线程下标(类名)
*/
private List<String> thread_code_index = new ArrayList();
@Override
@@ -166,6 +181,7 @@ public class AutoRunServiceImpl implements AutoRunService, ApplicationAutoInitia
private List<AbstractAutoRunnable> getAllAutoThread() {
// 如果此bean存在就直接返回
return ObjectUtil.isNotEmpty(abstractAutoRunnableList)
? this.abstractAutoRunnableList
: new LinkedList();
@@ -173,6 +189,7 @@ public class AutoRunServiceImpl implements AutoRunService, ApplicationAutoInitia
@Override
public void autoInitial() throws Exception {
// 获取自动线程
List<AbstractAutoRunnable> list = this.getAllAutoThread();
Iterator it = list.iterator();
@@ -185,11 +202,13 @@ public class AutoRunServiceImpl implements AutoRunService, ApplicationAutoInitia
a = "code 因为空而未加载";
log.warn(a);
t.setStopMessage(a);
// todo: 编码为空,此时应该不能插入
this.autoRun_code_index.add(t.getCode());
} else if (this.autoRun_code_index.contains(t.getCode())) {
a = String.format("code:%s 因为重复而未加载", new Object[]{t.getCode()});
log.warn(a);
t.setStopMessage(a);
// todo: 既然已经包含,再次添加会构成重复
this.autoRun_code_index.add(t.getCode());
} else {
t.setUsedStatus(ThreadUsedStatusEnum.used);

View File

@@ -1597,6 +1597,7 @@ public class DeviceServiceImpl implements DeviceService, ApplicationAutoInitial
@Override
public void reload() {
// 将货位信息存到内存中
this.storageCells = queryAllStorageCell();
}

View File

@@ -5,6 +5,9 @@ import org.nl.acs.opc.DeviceType;
import java.util.List;
/**
* 设备驱动定义
*/
public interface DeviceDriverDefination {
String getDriverCode();

View File

@@ -323,7 +323,7 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn
this.code_indexs = new ArrayList();
Collections.synchronizedList(this.code_indexs);
int i = 0;
// 将设备与opc对应的数据装入内存变量之中
for (int size = deviceManageDtos.size(); i < size; ++i) {
DeviceManageDto deviceManage = (DeviceManageDto) deviceManageDtos.get(i);
Device device = new Device();
@@ -347,7 +347,7 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn
@Override
public void removeDevice(String deviceCode) {
// 移除内存变量的设备实体
Iterator<Device> iterator = devices.iterator();
while (iterator.hasNext()) {
Device device = iterator.next();
@@ -355,6 +355,7 @@ public class DeviceAppServiceImpl implements DeviceAppService, ApplicationAutoIn
iterator.remove();
}
}
// 移除设备的内存key
Iterator<String> iterator2 = code_indexs.iterator();
while (iterator2.hasNext()) {
String str = iterator2.next();

View File

@@ -28,167 +28,169 @@ import java.util.Map;
@Service
public class OpcServerServiceImpl implements OpcServerService, ApplicationAutoInitial {
private static final Logger log = LoggerFactory.getLogger(OpcServerServiceImpl.class);
@Autowired OpcServerManageService opcServerManageService;
Map<String, OpcServerManageDto> opcServerManageDtos = new HashMap();
Map<String, Server> servers = Collections.synchronizedMap(new HashMap());
Map<String, Group> groups = Collections.synchronizedMap(new HashMap());
private static final Logger log = LoggerFactory.getLogger(OpcServerServiceImpl.class);
@Autowired
OpcServerManageService opcServerManageService;
Map<String, OpcServerManageDto> opcServerManageDtos = new HashMap();
Map<String, Server> servers = Collections.synchronizedMap(new HashMap());
Map<String, Group> groups = Collections.synchronizedMap(new HashMap());
public OpcServerServiceImpl() {}
public OpcServerServiceImpl() {
}
public void autoInitial() throws Exception {
this.reload();
if (OpcConfig.auto_start_opc) {
Thread t =
new Thread() {
public void run() {
Iterator var1 = OpcServerServiceImpl.this.opcServerManageDtos.values().iterator();
public void autoInitial() throws Exception {
this.reload();
if (OpcConfig.auto_start_opc) {
Thread t = new Thread(() -> {
Iterator var1 = OpcServerServiceImpl.this.opcServerManageDtos.values().iterator();
while (var1.hasNext()) {
OpcServerManageDto dto = (OpcServerManageDto) var1.next();
while (var1.hasNext()) {
OpcServerManageDto dto = (OpcServerManageDto) var1.next();
try {
OpcServerServiceImpl.this.getServer(dto.getOpc_code());
OpcServerServiceImpl.log.info("加载opc server {}", dto.getOpc_code());
} catch (Exception var4) {
OpcServerServiceImpl.log.warn("启动无法载入servers", var4);
try {
OpcServerServiceImpl.this.getServer(dto.getOpc_code());
OpcServerServiceImpl.log.info("加载opc server {}", dto.getOpc_code());
} catch (Exception var4) {
OpcServerServiceImpl.log.warn("启动无法载入servers", var4);
}
}
}
}
};
t.start();
}
}
public synchronized void reload() {
this.opcServerManageDtos = this.opcServerManageService.queryAllServerMap();
this.opcServerManageDtos = Collections.synchronizedMap(this.opcServerManageDtos);
}
public void cleanGroups(String opcCode) {
Group group = (Group)this.groups.get(opcCode);
if (group != null) {
Server server = group.getServer();
try {
group.remove();
} catch (JIException var5) {
var5.printStackTrace();
}
this.groups.remove(opcCode);
server.disconnect();
this.servers.remove(opcCode);
});
t.start();
}
}
}
public synchronized void reload() {
this.opcServerManageDtos = this.opcServerManageService.queryAllServerMap();
this.opcServerManageDtos = Collections.synchronizedMap(this.opcServerManageDtos);
}
public Group getServer(String code) throws Exception {
synchronized(this.buildLock(code)) {
Group group = null;
group = (Group)this.groups.get(code);
if (group != null) {
label68: {
Group var10000;
try {
if (!group.isActive()) {
break label68;
public void cleanGroups(String opcCode) {
Group group = (Group) this.groups.get(opcCode);
if (group != null) {
Server server = group.getServer();
try {
group.remove();
} catch (JIException var5) {
var5.printStackTrace();
}
var10000 = group;
} catch (JIException var14) {
log.error(code, var14);
break label68;
}
return var10000;
this.groups.remove(opcCode);
server.disconnect();
this.servers.remove(opcCode);
}
}
Server server = (Server)this.servers.get(code);
boolean needcreate = false;
String groupName = code;
if (server == null) {
needcreate = true;
} else {
try {
group = server.findGroup(groupName);
} catch (UnknownHostException | JIException | UnknownGroupException | NotConnectedException | IllegalArgumentException var13) {
log.error(code, var13);
needcreate = true;
}
}
}
if (needcreate) {
OpcServerManageDto dto = (OpcServerManageDto)this.opcServerManageDtos.get(code);
if (dto == null) {
throw new RuntimeException(code+"不存在");
}
public Group getServer(String code) throws Exception {
synchronized (this.buildLock(code)) {
Group group = null;
group = (Group) this.groups.get(code);
if (group != null) {
label68:
{
Group var10000;
try {
if (!group.isActive()) {
break label68;
}
var10000 = group;
} catch (JIException var14) {
log.error(code, var14);
break label68;
}
return var10000;
}
}
Server server = (Server) this.servers.get(code);
boolean needcreate = false;
String groupName = code;
if (server == null) {
needcreate = true;
} else {
try {
group = server.findGroup(groupName);
} catch (UnknownHostException | JIException | UnknownGroupException | NotConnectedException |
IllegalArgumentException var13) {
log.error(code, var13);
needcreate = true;
}
}
if (needcreate) {
OpcServerManageDto dto = (OpcServerManageDto) this.opcServerManageDtos.get(code);
if (dto == null) {
throw new RuntimeException(code + "不存在");
}
// if (server!=null){
// server.disconnect();
// server=null;
// }
if (server == null) {
server = OpcServerUtl.getServerWithOutException(dto.getOpc_host(), StrUtil.trim(dto.getCls_id()), dto.getUser(), dto.getPassword(), StrUtil.trim(dto.getDomain())); }
if (server == null) {
server = OpcServerUtl.getServerWithOutException(dto.getOpc_host(), StrUtil.trim(dto.getCls_id()), dto.getUser(), dto.getPassword(), StrUtil.trim(dto.getDomain()));
}
try {
group = server.addGroup(groupName);
} catch (Exception var12) {
this.clearServer(code);
ThreadUtl.sleep(5000L);
log.warn("获取opc出错重新获取", code, var12);
server = OpcServerUtl.getServerWithOutException(dto.getOpc_host(), StrUtil.trim(dto.getCls_id()), dto.getUser(), dto.getPassword(), StrUtil.trim(dto.getDomain()));
try {
group = server.addGroup(groupName);
} catch (Exception var11) {
throw new RuntimeException(var12);
}
try {
group = server.addGroup(groupName);
} catch (Exception var12) {
this.clearServer(code);
ThreadUtl.sleep(5000L);
log.warn("获取opc出错重新获取", code, var12);
server = OpcServerUtl.getServerWithOutException(dto.getOpc_host(), StrUtil.trim(dto.getCls_id()), dto.getUser(), dto.getPassword(), StrUtil.trim(dto.getDomain()));
try {
group = server.addGroup(groupName);
} catch (Exception var11) {
throw new RuntimeException(var12);
}
}
this.servers.put(code, server);
this.groups.put(code, group);
}
return group;
}
this.servers.put(code, server);
this.groups.put(code, group);
}
return group;
}
}
public void clearServer(String code) {
synchronized(this.buildLock(code)) {
try {
Server server = (Server)this.servers.get(code);
server.disconnect();
} catch (Exception var5) {
}
public void clearServer(String code) {
synchronized (this.buildLock(code)) {
try {
Server server = (Server) this.servers.get(code);
server.disconnect();
} catch (Exception var5) {
}
this.servers.remove(code);
this.groups.remove(code);
this.servers.remove(code);
this.groups.remove(code);
}
}
}
public void writeInteger(String code, ItemValue... values) {
try {
Group group = this.getServer(code);
OpcUtl.writeValue(group, values);
} catch (Exception var4) {
this.clearServer(code);
log.warn("写入出错opc server {} 重新加载", code, var4);
ThreadUtl.sleep(5000L);
public void writeInteger(String code, ItemValue... values) {
try {
Group group = this.getServer(code);
OpcUtl.writeValue(group, values);
} catch (Exception var4) {
this.clearServer(code);
log.warn("写入出错opc server {} 重新加载", code, var4);
ThreadUtl.sleep(5000L);
// throw var4;
}
}
}
private String buildLock(String key) {
if (StrUtil.isEmpty(key)) {
key = "";
private String buildLock(String key) {
if (StrUtil.isEmpty(key)) {
key = "";
}
StringBuilder builder = new StringBuilder();
builder.append("OpcServerService.");
builder.append(key);
String lock = builder.toString().intern();
return lock;
}
StringBuilder builder = new StringBuilder();
builder.append("OpcServerService.");
builder.append(key);
String lock = builder.toString().intern();
return lock;
}
}