完善 。S7 健康检查

This commit is contained in:
caixiang 2022-08-09 09:32:19 +08:00
부모 00a7aaaa37
커밋 3b1c667915
9개의 변경된 파일96개의 추가작업 그리고 92개의 파일을 삭제

파일 보기

@ -136,7 +136,9 @@ public class Main {
// |> limit(n: 10, offset: 20)
//22-57 22.3
//23-20 78
//24
// QueryDataParam queryDataParam = new QueryDataParam();
// queryDataParam.setBucket("mytest");
// queryDataParam.setRange(new Range(getDate().toInstant(),new Date().toInstant()));

파일 보기

@ -61,6 +61,8 @@ public class InfluxDemoController {
return point;
}
@PostMapping("/insert")
public void insert() throws InterruptedException {
Event event = new Event();

파일 보기

@ -82,7 +82,7 @@ public class S7DemoController {
@PostMapping("/readTest2")
public R getTest2ForS7() throws UnsupportedEncodingException, ParseException {
public R getTest2ForS7() throws Exception {
// List<Character> characters = (List<Character>)read(PlcVarActual.CharArrays,S7Client.S7_1500);
//
// List<Boolean> booleans = (List<Boolean>)read(PlcVarActual.BooleanArrays,S7Client.S7_1500);
@ -99,7 +99,7 @@ public class S7DemoController {
}
@PostMapping("/testForString")
public R testForStrings() throws UnsupportedEncodingException, ParseException {
public R testForStrings() throws Exception {
//测试结果 l => 66ms
long l = System.currentTimeMillis();
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays);
@ -128,7 +128,7 @@ public class S7DemoController {
}
@PostMapping("/testForString1200")
public R testForString1200() throws UnsupportedEncodingException, ParseException {
public R testForString1200() throws Exception {
//测试结果 l => 66ms
long l = System.currentTimeMillis();
String[] subs = (String[])read(S7Client.S7_1200,PlcVarActual.SubIdArrays1200); //65ms
@ -151,7 +151,7 @@ public class S7DemoController {
return R.ok().put("l",(l1-l)).put("c",(c2-c1));
}
@PostMapping("/testForString1500")
public R testForString1500() throws UnsupportedEncodingException, ParseException {
public R testForString1500() throws Exception {
//测试结果 l => 66ms
long l = System.currentTimeMillis();
String[] subs = (String[])read(S7Client.S7_1500,PlcVarActual.SubIdArrays); //25ms
@ -178,7 +178,7 @@ public class S7DemoController {
@PostMapping("/testFor1200")
public R testFor1200() throws UnsupportedEncodingException, ParseException {
public R testFor1200() throws Exception {
//Object subs = read(PlcVarActual.INT1200, S7Client.S7_1200);
Object read = read(S7Client.S7_1200, PlcVarActual.SubIdArrays1200);
@ -208,7 +208,7 @@ public class S7DemoController {
return null;
}
}
private void write(S7Client s7Client,PlcVarActual var,Object newValue) {
private void write(S7Client s7Client,PlcVarActual var,Object newValue) throws Exception {
if(var.getType().equals(PlcVar.STRING_Array)){
String[] s = (String[])newValue;
String[] ss = (String[])newValue;

파일 보기

@ -13,7 +13,7 @@ import java.util.List;
* @DATE: 2021/12/10 10:17
*/
public class MainForRead {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
//前言
//DB3.1.1 中间那个1是byte区后面那个1 是bit
//缺陷 不支持 DB3.1.1

파일 보기

@ -17,7 +17,7 @@ import java.util.List;
* @DATE: 2021/12/10 10:17
*/
public class MainForReadDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
public static void main(String[] args) throws Exception {
// //1200 部分
// S7Connector connector1200 = S7Client.S7_1200.getConnector();

파일 보기

@ -18,7 +18,7 @@ import java.util.List;
* @DATE: 2021/12/10 10:17
*/
public class MainForWrite {
public static void main(String[] args) throws IOException, ParseException {
public static void main(String[] args) throws Exception {
//Create connection
S7Connector connector =
S7ConnectorFactory

파일 보기

@ -23,36 +23,36 @@ import java.io.Closeable;
public interface S7Connector extends Closeable {
/**
* Reads an area
*
* desc : 只要未抛出异常都是 操作成功的
* @param area
* @param areaNumber
* @param bytes
* @param offset
* @return
*/
public byte[] read(DaveArea area, int areaNumber, int bytes, int offset);
public byte[] read(DaveArea area, int areaNumber, int bytes, int offset) throws Exception;
/**
* Reads an area 读需要bit 位置的 变量其实就是 read bool变量的时候调用这个方法
*
* desc : 只要未抛出异常都是 操作成功的
* @param area
* @param areaNumber
* @param bytes
* @param offset
* @return
*/
public byte[] read(DaveArea area, int areaNumber, int bytes, int offset, int bitOffset, TransportSize transportSize);
public byte[] read(DaveArea area, int areaNumber, int bytes, int offset, int bitOffset, TransportSize transportSize) throws Exception;
/**
* Writes an area
*
* desc : 只要未抛出异常都是 操作成功的
* @param area
* @param areaNumber
* @param offset
* @param buffer
*/
public void write(DaveArea area, int areaNumber, int offset, byte[] buffer);
public void write(DaveArea area, int areaNumber, int offset, byte[] buffer) throws Exception;
//如果 bitOffset 没有 那么就填0
public void write(DaveArea area, int areaNumber, int byteOffset, int bitOffset, byte[] buffer, PlcVar var);
public void write(DaveArea area, int areaNumber, int byteOffset, int bitOffset, byte[] buffer, PlcVar var) throws Exception;
}

파일 보기

@ -16,6 +16,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* @Desc: ""
@ -114,7 +116,7 @@ public enum S7Client {
*
*
* */
public Object read(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset, Integer length, Integer strSizes, PlcVar type) throws UnsupportedEncodingException, ParseException {
public Object read(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset, Integer length, Integer strSizes, PlcVar type) throws Exception {
S7Connector connector = getConnector();
//String 类型比较特殊 String[] 也是同理Sring数组里面的子项 也是有两个字节的 readBytes
if(type.equals(PlcVar.STRING)){
@ -212,7 +214,7 @@ public enum S7Client {
*
*
* */
public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue){
public void write(DaveArea area, Integer areaNumber, Integer byteOffset, Integer bitOffset,Integer strSize, PlcVar type, Object newValue) throws Exception {
S7Connector connector = getConnector();
//String 类型比较特殊 String[] 也是同理Sring数组里面的子项 也是有两个字节的 readBytes
@ -281,89 +283,86 @@ public enum S7Client {
}
private void check_ping(){
ping_fail_check.execute(new Runnable() {
@Override
public void run() {
while (true){
if(connections.size()!=coreSize){
int c = coreSize-connections.size();
for(int z=0;z<c;z++){
S7Connector connect = connect(host, rack, slot);
if(connect!=null){
logger.info("host:"+host +" ;;(check_ping()) 检测到有断线 ==》 现在断线恢复成功");
connections.add(connect);
}else {
logger.info("host:"+host +" ;;(check_ping()) 检测到有断线 ==》 现在断线恢复失败");
}
while (true){
try {
ScheduledFuture<?> schedule = ping_fail_check.schedule(new Runnable() {
@Override
public void run() {
if (connections.size() != coreSize) {
int c = coreSize - connections.size();
for (int z = 0; z < c; z++) {
S7Connector connect = connect(host, rack, slot);
if (connect != null) {
logger.info("host:" + host + " ;;(check_ping()) 检测到有断线 ==》 现在断线恢复成功");
connections.add(connect);
} else {
logger.info("host:" + host + " ;;(check_ping()) 检测到有断线 ==》 现在断线恢复失败");
}
}
}
}
try {
Thread.sleep(10000);
}catch (Exception e){
logger.info("host:"+host +" ;;(check_ping()) Thread.sleep 异常,异常原因:"+e.getMessage());
}
}
}, 10000, TimeUnit.SECONDS);
Object o = schedule.get();
}catch (Exception e){
logger.info("( out catched ) host:"+host +" ;;(check_ping) "+"check connect.size 出现异常errMessage is : "+e.getMessage()+"现在进入下一次轮询");
}
});
}
}
//todo 当网络断开之后下面这个ping() 循环不生效 可能是read = connector.read( 出来是null但还是继续通过了也可能是 当连接全部断开后 getConnector(); 应该是取不到数据的也就会报null这个明天再看看
private void ping(){
executor.execute(new Runnable() {
@Override
public void run() {
while (true){
for(int i=0;i<coreSize;i++){
S7Connector connector = getConnector();
try {
//只要没有报异常 都是通讯正常的
byte[] read = connector.read(
heartBeat.getArea(),
heartBeat.getAreaNumber(),
heartBeat.getType().getTransportSize().getSizeInBytes(),
heartBeat.getByteOffset(),
heartBeat.getBitOffset(),
heartBeat.getType().getTransportSize());
System.out.println("host:"+host +" ;; "+connector.hashCode()+" : ping");
Thread.sleep(100);
}catch (Exception e){
logger.info("host:"+host +" ;;(ping) "+connector.hashCode()+" : connection error"+"errMessage is : "+e.getMessage());
//先把 socket close掉
while (true){
try {
ScheduledFuture<?> schedule = executor.schedule(new Runnable() {
@Override
public void run() {
for (int i = 0; i < coreSize; i++) {
S7Connector connector = getConnector();
try {
connector.close();
connections.remove(connector);
//如果是网络波动照成的socket断开 等个1S 再重连试试
//只要没有报异常 都是通讯正常的
byte[] read = connector.read(
heartBeat.getArea(),
heartBeat.getAreaNumber(),
heartBeat.getType().getTransportSize().getSizeInBytes(),
heartBeat.getByteOffset(),
heartBeat.getBitOffset(),
heartBeat.getType().getTransportSize());
System.out.println("host:" + host + " ;; " + connector.hashCode() + " : ping");
Thread.sleep(100);
}catch (Exception ee){
logger.info("host:"+host +" ;;(ping) "+"connector.close() 出现异常errMessage is : "+ee.getMessage());
} catch (Exception e) {
logger.info("host:" + host + " ;;(ping) " + connector.hashCode() + " : connection error" + "errMessage is : " + e.getMessage());
//先把 socket close掉
try {
connector.close();
connections.remove(connector);
//如果是网络波动照成的socket断开 等个1S 再重连试试
Thread.sleep(100);
} catch (Exception ee) {
logger.info("host:" + host + " ;;(ping) " + "connector.close() 出现异常errMessage is : " + ee.getMessage());
}
}
}
//todo 把之前的连接close 然后新增一个连接到connections
if(connections.size()!=coreSize){
int c = coreSize-connections.size();
for(int z=0;z<c;z++){
S7Connector connect = connect(host, rack, slot);
if(connect!=null){
logger.info("host:"+host +" ;;(ping) "+"ping时候出现异常尝试重连 重连成功!!");
connections.add(connect);
}else {
logger.info("host:"+host +" ;;(ping) "+"ping时候出现异常尝试重连 重连时候还是 出现异常。。");
//todo 把之前的连接close 然后新增一个连接到connections
if (connections.size() != coreSize) {
int c = coreSize - connections.size();
for (int z = 0; z < c; z++) {
S7Connector connect = connect(host, rack, slot);
if (connect != null) {
logger.info("host:" + host + " ;;(ping) " + "ping时候出现异常尝试重连 重连成功!!");
connections.add(connect);
} else {
logger.info("host:" + host + " ;;(ping) " + "ping时候出现异常尝试重连 重连时候还是 出现异常。。");
}
}
}
}
}
try {
Thread.sleep(30000);
}catch (Exception e){
e.printStackTrace();
logger.info("host:"+host +" ;;(ping) "+e.getMessage());
}
}
}, 30000, TimeUnit.SECONDS);
Object o = schedule.get();
}catch (Exception e){
logger.info("( out catched ) host:"+host +" ;;(ping) "+"ping的时候 整体 出现异常errMessage is : "+e.getMessage()+"现在进入下一次轮询");
}
});
}
}
}

파일 보기

@ -52,10 +52,11 @@ public abstract class S7BaseConnection implements S7Connector {
* @param libnodaveResult
* the libnodave result
*/
public static void checkResult(final int libnodaveResult) {
public static void checkResult(final int libnodaveResult) throws Exception {
if (libnodaveResult != Nodave.RESULT_OK) {
final String msg = Nodave.strerror(libnodaveResult);
throw new IllegalArgumentException("Result: " + msg);
// throw new IllegalArgumentException("Result: " + msg);
throw new Exception(msg);
}
}
@ -86,7 +87,7 @@ public abstract class S7BaseConnection implements S7Connector {
/** {@inheritDoc} */
@Override
public synchronized byte[] read(final DaveArea area, final int areaNumber, final int bytes, final int offset) {
public synchronized byte[] read(final DaveArea area, final int areaNumber, final int bytes, final int offset) throws Exception {
if (bytes > MAX_SIZE) {
final byte[] ret = new byte[bytes];
//注意这里 嵌套了 递归让无限递归去解决 MAX_SIZE的问题
@ -107,7 +108,7 @@ public abstract class S7BaseConnection implements S7Connector {
}
}
@Override
public synchronized byte[] read(final DaveArea area, final int areaNumber, final int bytes, final int offset, int bitOffset, TransportSize transportSize) {
public synchronized byte[] read(final DaveArea area, final int areaNumber, final int bytes, final int offset, int bitOffset, TransportSize transportSize) throws Exception {
if(bitOffset==0){
return read(area,areaNumber,bytes,offset);
}
@ -134,7 +135,7 @@ public abstract class S7BaseConnection implements S7Connector {
/** {@inheritDoc} */
@Override
public synchronized void write(final DaveArea area, final int areaNumber, final int offset, final byte[] buffer) {
public synchronized void write(final DaveArea area, final int areaNumber, final int offset, final byte[] buffer) throws Exception {
if (buffer.length > MAX_SIZE) {
// Split buffer
final byte[] subBuffer = new byte[MAX_SIZE];
@ -154,7 +155,7 @@ public abstract class S7BaseConnection implements S7Connector {
}
@Override
public synchronized void write(final DaveArea area, final int areaNumber, final int byteOffset, final int bitOffset, final byte[] buffer, PlcVar var) {
public synchronized void write(final DaveArea area, final int areaNumber, final int byteOffset, final int bitOffset, final byte[] buffer, PlcVar var) throws Exception {
if(bitOffset==0){
write(area,areaNumber,byteOffset,buffer);
return;