|
|
@ -30,131 +30,127 @@ 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()); |
|
|
|
|
|
|
|
public OpcServerServiceImpl() { |
|
|
|
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 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(); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
t.start(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 加载opc服务,启动opc线程 |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
public void autoInitial() throws Exception { |
|
|
|
this.reload(); // 加载opc服务
|
|
|
|
if (OpcConfig.auto_start_opc) { |
|
|
|
Thread t = |
|
|
|
new Thread(() -> { |
|
|
|
Iterator var1 = OpcServerServiceImpl.this.opcServerManageDtos.values().iterator(); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
t.start(); |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized void reload() { |
|
|
|
this.opcServerManageDtos = this.opcServerManageService.queryAllServerMap(); |
|
|
|
this.opcServerManageDtos = Collections.synchronizedMap(this.opcServerManageDtos); |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized Group getServer(String code) { |
|
|
|
Group group = null; |
|
|
|
group = (Group) this.groups.get(code); |
|
|
|
if (group != null) { |
|
|
|
try { |
|
|
|
if (group.isActive()) { |
|
|
|
return group; |
|
|
|
} |
|
|
|
} catch (JIException var9) { |
|
|
|
var9.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized void reload() { |
|
|
|
this.opcServerManageDtos = this.opcServerManageService.queryAllServerMap(); |
|
|
|
this.opcServerManageDtos = Collections.synchronizedMap(this.opcServerManageDtos); |
|
|
|
Server server = (Server) this.servers.get(code); |
|
|
|
boolean needcreate = false; |
|
|
|
if (server == null) { |
|
|
|
needcreate = true; |
|
|
|
} else { |
|
|
|
try { |
|
|
|
group = server.findGroup(OpcConfig.opc_server_default_group); |
|
|
|
} catch (UnknownHostException |
|
|
|
| JIException |
|
|
|
| UnknownGroupException |
|
|
|
| NotConnectedException |
|
|
|
| IllegalArgumentException var8) { |
|
|
|
var8.printStackTrace(); |
|
|
|
needcreate = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized Group getServer(String code) { |
|
|
|
Group group = null; |
|
|
|
group = (Group) this.groups.get(code); |
|
|
|
if (group != null) { |
|
|
|
try { |
|
|
|
if (group.isActive()) { |
|
|
|
return group; |
|
|
|
} |
|
|
|
} catch (JIException var9) { |
|
|
|
var9.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (needcreate) { |
|
|
|
OpcServerManageDto dto = (OpcServerManageDto) this.opcServerManageDtos.get(code); |
|
|
|
if (dto == null) { |
|
|
|
throw new BadRequestException("code 不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
Server server = (Server) this.servers.get(code); |
|
|
|
boolean needcreate = false; |
|
|
|
if (server == null) { |
|
|
|
needcreate = true; |
|
|
|
} else { |
|
|
|
try { |
|
|
|
group = server.findGroup(OpcConfig.opc_server_default_group); |
|
|
|
} catch (UnknownHostException |
|
|
|
| JIException |
|
|
|
| UnknownGroupException |
|
|
|
| NotConnectedException |
|
|
|
| IllegalArgumentException var8) { |
|
|
|
var8.printStackTrace(); |
|
|
|
needcreate = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (needcreate) { |
|
|
|
OpcServerManageDto dto = (OpcServerManageDto) this.opcServerManageDtos.get(code); |
|
|
|
if (dto == null) { |
|
|
|
throw new BadRequestException("code 不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
server = OpcServerUtl.getServerWithOutException( |
|
|
|
dto.getOpc_host(), |
|
|
|
dto.getCls_id(), |
|
|
|
dto.getUser(), |
|
|
|
dto.getPassword(), |
|
|
|
dto.getDomain()); |
|
|
|
server = OpcServerUtl.getServerWithOutException( |
|
|
|
dto.getOpc_host(), |
|
|
|
dto.getCls_id(), |
|
|
|
dto.getUser(), |
|
|
|
dto.getPassword(), |
|
|
|
dto.getDomain()); |
|
|
|
|
|
|
|
// server = OpcServerUtl.getServerWithOutException(this.OpcServer.getOpc_host(), this.OpcServer.getCls_id(), this.OpcServer.getUser(), this.OpcServer.getPassword(), this.OpcServer.getDomain());
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
group = server.addGroup(OpcConfig.opc_server_default_group); |
|
|
|
} catch (Exception var7) { |
|
|
|
throw new BadRequestException(var7.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
this.servers.put(code, server); |
|
|
|
this.groups.put(code, group); |
|
|
|
} |
|
|
|
try { |
|
|
|
group = server.addGroup(OpcConfig.opc_server_default_group); |
|
|
|
} catch (Exception var7) { |
|
|
|
throw new BadRequestException(var7.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
return group; |
|
|
|
this.servers.put(code, server); |
|
|
|
this.groups.put(code, group); |
|
|
|
} |
|
|
|
|
|
|
|
public synchronized void clearServer(String code) { |
|
|
|
try { |
|
|
|
Server server = (Server) this.servers.get(code); |
|
|
|
if (server != null) { |
|
|
|
server.disconnect(); |
|
|
|
} |
|
|
|
return group; |
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("清理server异常,", e.getMessage()); |
|
|
|
} |
|
|
|
public synchronized void clearServer(String code) { |
|
|
|
try { |
|
|
|
Server server = (Server) this.servers.get(code); |
|
|
|
if (server!=null){ |
|
|
|
server.disconnect(); |
|
|
|
} |
|
|
|
|
|
|
|
this.servers.remove(code); |
|
|
|
this.groups.remove(code); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("清理server异常,",e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
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); |
|
|
|
throw var4; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|