From 303664eda3fc3166b200b08fa4ab65af394f6056 Mon Sep 17 00:00:00 2001
From: caixiang <939387484@qq.com>
Date: Sat, 25 Sep 2021 14:25:08 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=97=A5=E5=BF=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HELP.md | 5 +
pom.xml | 27 +++
.../com/qgs/dc/opcua/selfunion/NodeIdKey.java | 12 +-
.../com/qgs/dc/opcua/selfunion/UAService.java | 4 +-
.../qgs/dc/s7/controller/S7Controller.java | 89 ++++++++++
.../java/com/qgs/dc/s7/service/S7Service.java | 43 +++++
src/main/resources/application.yml | 5 +
src/main/resources/logback.xml | 168 ++++++++++++------
8 files changed, 295 insertions(+), 58 deletions(-)
create mode 100644 src/main/java/com/qgs/dc/s7/controller/S7Controller.java
create mode 100644 src/main/java/com/qgs/dc/s7/service/S7Service.java
diff --git a/HELP.md b/HELP.md
index 1c23734..b64e026 100644
--- a/HELP.md
+++ b/HELP.md
@@ -8,6 +8,11 @@ For further reference, please consider the following sections:
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.5.4/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.5.4/maven-plugin/reference/html/#build-image)
+系统相关
+ 日志问题
+ ① 一般系统的 debug模式 只有在开发调试阶段才会开启,生产环境是不开的。开启==》application.yml 下的logging 注释打开 ;;关闭 ==》application.yml 下的logging代码打上注释。;;
+ ② work目录下日志一般是汇总的所有模块的日志(包括所有模块他们依赖第三方jar包 输出的日志) ,其他目录下的日志是 手动用代码输出的日志
+
MQ 相关
1.src/main/java/com/qgs/dc/mq/configuration 包下新建 配置类。主要的作用的是在 MQ上新建队列(如果队列信息不存在的话),
水平扩展其他设备的时候 只要:control+R 然后 00B=>00C 然后replace all
diff --git a/pom.xml b/pom.xml
index a091496..75a148e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -131,6 +131,33 @@
+
+
+ org.apache.plc4x
+ plc4j-api
+ 0.9.0
+
+
+ org.apache.plc4x
+ plc4j-connection-cache
+ 0.9.0
+
+
+
+ org.apache.plc4x
+ plc4j-connection-pool
+ 0.9.0
+
+
+
+ org.apache.plc4x
+ plc4j-driver-s7
+ 0.9.0
+ runtime
+
+
+
+
org.springframework.boot
spring-boot-starter
diff --git a/src/main/java/com/qgs/dc/opcua/selfunion/NodeIdKey.java b/src/main/java/com/qgs/dc/opcua/selfunion/NodeIdKey.java
index 3043ba4..e8c9825 100644
--- a/src/main/java/com/qgs/dc/opcua/selfunion/NodeIdKey.java
+++ b/src/main/java/com/qgs/dc/opcua/selfunion/NodeIdKey.java
@@ -7,7 +7,7 @@ public class NodeIdKey {
private String nodeName;
private Integer namespace;
private String identifier;
- private List child;
+ private List children;
/**
* type 类型 (就是Node)
@@ -43,7 +43,7 @@ public class NodeIdKey {
this.namespace = namespace;
this.identifier = identifier;
this.varType = varType;
- this.child = new ArrayList<>();
+ this.children = new ArrayList<>();
}
public Integer getNodeType() {
@@ -62,12 +62,12 @@ public class NodeIdKey {
this.varType = varType;
}
- public List getChild() {
- return child;
+ public List getChildren() {
+ return children;
}
- public void setChild(List child) {
- this.child = child;
+ public void setChildren(List children) {
+ this.children = children;
}
diff --git a/src/main/java/com/qgs/dc/opcua/selfunion/UAService.java b/src/main/java/com/qgs/dc/opcua/selfunion/UAService.java
index 3d19ff9..db87919 100644
--- a/src/main/java/com/qgs/dc/opcua/selfunion/UAService.java
+++ b/src/main/java/com/qgs/dc/opcua/selfunion/UAService.java
@@ -1338,7 +1338,7 @@ public class UAService {
NodeId rootNodeId = new NodeId(rootNameSpace,identifier);
String rootName = rootNameSpace+","+identifier;
NodeIdKey rootNodeIdKey = new NodeIdKey(rootName, NodeClass.Object.getValue(), rootNameSpace, identifier,"根目录");
- rootNodeIdKey.setChild(browseNodeA(rootNodeIdKey, opcUaClients.get(plcName), rootNodeId));
+ rootNodeIdKey.setChildren(browseNodeA(rootNodeIdKey, opcUaClients.get(plcName), rootNodeId));
return rootNodeIdKey;
}
@@ -1381,7 +1381,7 @@ public class UAService {
//NodeIdKey nodeIdKey = new NodeIdKey(browName,66,Integer.parseInt(String.valueOf(browseRoot.getNamespaceIndex())),browseRoot.getIdentifier().toString());
// 目录也是一个NodeId,,所以要递归的去遍历看看是否 是目录
- nodeIdKey1.setChild(browseNodeA(nodeIdKey1, client, node.getNodeId()));
+ nodeIdKey1.setChildren(browseNodeA(nodeIdKey1, client, node.getNodeId()));
}
} catch (UaException e) {
logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e);
diff --git a/src/main/java/com/qgs/dc/s7/controller/S7Controller.java b/src/main/java/com/qgs/dc/s7/controller/S7Controller.java
new file mode 100644
index 0000000..6b2aedb
--- /dev/null
+++ b/src/main/java/com/qgs/dc/s7/controller/S7Controller.java
@@ -0,0 +1,89 @@
+package com.qgs.dc.s7.controller;
+
+import com.qgs.dc.opcua.arg.*;
+import com.qgs.dc.opcua.controller.R;
+import com.qgs.dc.s7.service.S7Service;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcReadResponse;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.api.value.PlcValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.concurrent.CompletableFuture;
+
+@RestController
+@RequestMapping("/s7")
+public class S7Controller {
+ private static final Logger logger = LoggerFactory.getLogger(S7Controller.class);
+
+ @Autowired
+ S7Service s7Service;
+
+
+ @PostMapping("/addThisPlc")
+ public R addThisPlc() throws PlcConnectionException {
+ Integer plc1 = s7Service.addPlc("plc1", "s7://192.168.0.200");
+ return R.ok().put("res",plc1);
+ }
+ @PostMapping("/getValue")
+ public R getValue() throws PlcConnectionException {
+ try(PlcConnection conn = s7Service.getConnection("s7://192.168.0.200")) {
+ if(conn.isConnected()){
+ if(conn.getMetadata().canRead()){
+ try {
+ PlcReadRequest.Builder builder = conn.readRequestBuilder();
+ //builder.addItem("INT_1", "%DB10:4:INT");
+ //builder.addItem("Byte-array", "%DB10:312:BYTE[20]");
+ builder.addItem("STRING", "%DB10:10.0:STRING(20)");
+ PlcReadRequest readRequest = builder.build();
+ //PlcReadResponse response = readRequest.execute().get();
+ CompletableFuture extends PlcReadResponse> execute = readRequest.execute();
+
+ PlcReadResponse response = execute.get();
+
+ for (String fieldName : response.getFieldNames()) {
+ if(response.getResponseCode(fieldName) == PlcResponseCode.OK) {
+ int numValues = response.getNumberOfValues(fieldName);
+ PlcValue asPlcValue = response.getAsPlcValue();
+ if(numValues == 1) {
+ Object obj = response.getObject(fieldName);
+
+ logger.info("Value[" + fieldName + "]: " + response.getObject(fieldName));
+ }
+ else {
+ logger.info("Value[" + fieldName + "]:");
+ for(int i = 0; i < numValues; i++) {
+ logger.info(" - " + response.getObject(fieldName, i));
+ }
+ }
+ }
+ else {
+ logger.error("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
+ }
+ }
+
+ }catch (Exception e){
+ logger.error(e.getMessage());
+ }
+ }else {
+ System.out.println("断线后 不能 read");
+ }
+ }else {
+ System.out.println("conn 为 connected");
+ }
+
+ }catch (Exception e){
+ System.out.println(e.getMessage());
+ }
+
+
+ return R.ok().put("res",1);
+ }
+
+}
diff --git a/src/main/java/com/qgs/dc/s7/service/S7Service.java b/src/main/java/com/qgs/dc/s7/service/S7Service.java
new file mode 100644
index 0000000..7979690
--- /dev/null
+++ b/src/main/java/com/qgs/dc/s7/service/S7Service.java
@@ -0,0 +1,43 @@
+package com.qgs.dc.s7.service;
+
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
+import org.apache.plc4x.java.utils.connectionpool2.CachedDriverManager;
+import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+
+/**
+ * @Desc: ""
+ * @Author: caixiang
+ * @DATE: 2021/9/23 15:16
+ */
+
+@Component
+public class S7Service {
+ private final Logger logger = LoggerFactory.getLogger(getClass());
+
+ private HashMap plcConnections = new HashMap<>();
+ private PlcDriverManager driverManager;
+
+
+ public S7Service(){
+ driverManager = new PooledPlcDriverManager();
+
+ }
+
+ public Integer addPlc(String plcName,String url) throws PlcConnectionException {
+
+
+ return 1;
+ }
+
+ public PlcConnection getConnection(String url) throws PlcConnectionException {
+ return driverManager.getConnection(url);
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index d8db2d9..07c7934 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -1,5 +1,10 @@
server:
port: 8009
+# 只有把 springboot 的debug 模式开启,slf4j 才会记录日志
+#logging:
+# level:
+# root: debug
+#
spring:
rabbitmq:
# 如果是rabbitmq+haproxy+keepalived集群 ,,那么192.168.0.176是haproxy代理的地址(严格来说是keepalived的vip)
diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml
index 71090d0..04939eb 100644
--- a/src/main/resources/logback.xml
+++ b/src/main/resources/logback.xml
@@ -12,7 +12,7 @@
-
+
@@ -24,15 +24,19 @@
-
-
- debug
-
+
${CONSOLE_LOG_PATTERN}
UTF-8
+
+
+
+
+ info
+
+
@@ -45,19 +49,6 @@
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
UTF-8
-
-
-
-
-
-
-
-
-
-
-
-
-
${logging.pathwork}/work-log-debug-%d{yyyy-MM-dd}.%i.log
@@ -67,8 +58,6 @@
200GB
-
-
debug
@@ -138,6 +127,32 @@
DENY
+
+
+ ${logging.s7}/debug/s7-log-debug.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+
+ ${logging.s7}/s7-log-debug-%d{yyyy-MM-dd}.%i.log
+
+ 100MB
+
+ 999
+ 200GB
+
+
+
+ debug
+ ACCEPT
+ DENY
+
+
+
@@ -148,16 +163,6 @@
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
UTF-8
-
-
-
-
-
-
-
-
-
-
${logging.pathwork}/work-log-info-%d{yyyy-MM-dd}.%i.log
@@ -242,6 +247,30 @@
DENY
+
+
+ ${logging.s7}/info/s7-log-info.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+ ${logging.s7}/s7-log-info-%d{yyyy-MM-dd}.%i.log
+
+ 100MB
+
+ 999
+ 200GB
+
+
+
+ info
+ ACCEPT
+ DENY
+
+
@@ -343,6 +372,30 @@
DENY
+
+
+ ${logging.s7}/warn/s7-log-warn.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+ ${logging.s7}/s7-log-warn-%d{yyyy-MM-dd}.%i.log
+
+ 100MB
+
+ 999
+ 200GB
+
+
+
+ warn
+ ACCEPT
+ DENY
+
+
@@ -388,16 +441,6 @@
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
UTF-8
-
-
-
-
-
-
-
-
-
-
${logging.pathopc}/opcua-log-error-%d{yyyy-MM-dd}.%i.log
@@ -422,15 +465,6 @@
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
UTF-8
-
-
-
-
-
-
-
-
-
@@ -448,6 +482,32 @@
DENY
+
+
+ ${logging.s7}/error/s7-log-error.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+
+ ${logging.s7}/s7-log-error-%d{yyyy-MM-dd}.%i.log
+
+ 100MB
+ 999
+ 200GB
+
+
+
+
+ ERROR
+ ACCEPT
+ DENY
+
+
+
-
+
@@ -491,6 +551,14 @@
+
+
+
+
+
+
+
+