告警硬接点装置代码调整和判断调整

main
liuguijing 6 months ago
parent 2d5491876b
commit dfbb28af3d

@ -2,8 +2,8 @@ package com.xydl.cac.controller;
import com.xydl.cac.model.Response;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.serialport.SerialPortServer;
import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.service.SerialPortService;
import com.xydl.cac.socket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -11,7 +11,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.WebSocketSession;
import javax.annotation.Resource;
import java.io.IOException;
@ -30,7 +29,7 @@ public class TestController extends BasicController {
WebSocketServer webSocketServer;
@Resource
SerialPortService serialPortService;
SerialPortServer serialPortService;
@GetMapping("compare61850")
@ApiOperation("比对61850的不同点")
@ -90,13 +89,9 @@ public class TestController extends BasicController {
@GetMapping("/discover")
public Response<String> discoverSerialPort() {
Boolean aBoolean = serialPortService.discoverSerialPort();
if (aBoolean) {
return Response.success("发现了");
} else {
serialPortService.discoverSerialPort();
return Response.fail("未找到");
}
}
@GetMapping("/open")
public Response<Boolean> openSerialPort() {
@ -104,14 +99,14 @@ public class TestController extends BasicController {
}
@GetMapping("/send")
public String sendData(boolean data) throws IOException {
public String sendData() throws IOException {
//发送数据注意,提前与接收设备沟通好协议,发送什么样类型的数据设备才可以进行响应,否则设备无响应
serialPortService.sendData(data);
serialPortService.sendData();
return "Data sent>>>";
}
@GetMapping("/close")
public String closeSerialPort(String portname) {
public String closeSerialPort() {
serialPortService.closeSerialPort();
return "Serial port closed";
}

@ -30,8 +30,7 @@ public class StaticVariable {
public static List<Zsb> zsb_Cache = null;
public static ConcurrentHashMap<Integer, WarnRule> rule_Cache = new ConcurrentHashMap<>();
public static SerialPort currentPort;
public static Date LastWarningTime;//最后一次硬接点告警时间
public static void wait(int seconds) throws InterruptedException {
for (int i = 0; i < seconds; i++) {

@ -1,4 +1,4 @@
package com.xydl.cac.service.impl;
package com.xydl.cac.serialport;
import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;

@ -0,0 +1,137 @@
package com.xydl.cac.serialport;
import com.fazecast.jSerialComm.SerialPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
@Service
@Slf4j
public class SerialPortServer {
@Value("${cac.warnport.name}")
public String warnportname;
@Value("${cac.warnport.intervaltime:60}")
public Integer intervaltime;
@Value("${cac.warnport.warntime:5}")
public Integer warntime;
public SerialPort currentPort;//当前可用串口
public long LastWarningTime;//最后一次硬接点告警时间
public boolean started = false;
public void discoverSerialPort() {
if (currentPort == null) {
List<SerialPort> serialPortList = new ArrayList<>();
SerialPort[] commPorts = SerialPort.getCommPorts();
if (commPorts != null && commPorts.length > 0) {
serialPortList = Arrays.asList(commPorts);
for (SerialPort port : serialPortList) {
log.info("获取串口: " + port.getSystemPortName());
Pattern pattern = Pattern.compile("ttyCH341.");
String systemPortName = port.getSystemPortName();
if (systemPortName != null) {
if (pattern.matcher(systemPortName).matches()) {
currentPort = port;
log.info("获取到硬接点告警串口: " + port.getSystemPortName());
}
}
}
} else {
log.error("未获取到硬接点告警串口!");
}
}
}
public void closeSerialPort() {
if (currentPort != null) {
SerialPort port = currentPort;
port.closePort();
} else {
log.error("断开串口失败,没有对应的硬接点告警串口!");
}
}
//发送数据到串口
@Async
public void sendData() {
if (!started) {
started = true;
if (openSerialPort()) {
byte[] openbuffer = new byte[]{00, (byte) 0xf1, (byte) 0xff};//发送串口告警
byte[] stopbuffer = new byte[]{00, (byte) 0x01, (byte) 0xff};//关闭串口告警
SerialPort port = currentPort;
if (port != null) {
long l = System.currentTimeMillis();
if (LastWarningTime == 0) {
LastWarningTime = l;
} else {
if (l - LastWarningTime <= (intervaltime * 1000)) {
started = false;
return;
}
}
LastWarningTime = l;
port.writeBytes(openbuffer, openbuffer.length);
try {
Thread.sleep(warntime * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
port.writeBytes(stopbuffer, stopbuffer.length);
started = false;
}
}
}
started = false;
}
}
public Boolean openSerialPort() {
discoverSerialPort();
if (currentPort != null) {
if (currentPort.isOpen()) {
log.info("硬接点告警串口已经打开!");
return true;
} else {
int baudRate = 9600; // 波特率
int parity = SerialPort.EVEN_PARITY; // 校验位
int dataBits = 8; // 数据位
int stopBits = SerialPort.ONE_STOP_BIT; // 停止位
try {
boolean setComResult = currentPort.setComPortParameters(baudRate, dataBits, stopBits, parity); // 设置参数
boolean setComTimes = currentPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时
if (setComResult && setComTimes) {
currentPort.addDataListener(new DataListener(currentPort.getSystemPortName()));
}
boolean b = currentPort.openPort();
if (b) {
log.info("打开硬接点告警串口成功!");
return true;
} else {
currentPort = null;
log.error("打开串口失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
currentPort = null;
log.error("打开串口失败" + e.getMessage());
return false;
}
}
} else {
log.error("打开串口失败,找不到硬接点串口!");
return false;
}
}
}

@ -1,16 +0,0 @@
package com.xydl.cac.service;
import com.fazecast.jSerialComm.SerialPort;
import java.io.IOException;
import java.util.List;
public interface SerialPortService {
Boolean discoverSerialPort();
void closeSerialPort();
Boolean openSerialPort();
void sendData(boolean data) throws IOException;
}

@ -1,137 +0,0 @@
package com.xydl.cac.service.impl;
import com.fazecast.jSerialComm.SerialPort;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.SerialPortService;
import com.xydl.cac.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service
@Slf4j
public class SerialPortServiceImpl implements SerialPortService {
@Value("${cac.warnport.name}")
public String warnportname;
@Value("${cac.warnport.intervaltime:60000}")
public Integer intervaltime;
@Value("${cac.warnport.warntime:5000}")
public Integer warntime;
@Override
public Boolean discoverSerialPort() {
List<SerialPort> serialPortList = new ArrayList<>();
SerialPort[] commPorts = SerialPort.getCommPorts();
if (commPorts != null && commPorts.length > 0) {
serialPortList = Arrays.asList(commPorts);
for (SerialPort port : serialPortList) {
log.info("获取串口: " + port.getSystemPortName());
if (port.getSystemPortName().equals(warnportname)) {
StaticVariable.currentPort = port;
log.info("获取到硬接点告警串口: " + port.getSystemPortName());
return true;
}
}
return false;
} else {
log.error("未获取到硬接点告警串口!");
return false;
}
}
@Override
public void closeSerialPort() {
if (StaticVariable.currentPort != null) {
SerialPort port = StaticVariable.currentPort;
port.closePort();
} else {
log.error("断开串口失败,没有对应的硬接点告警串口!");
}
}
//发送数据到串口
@Override
@Async
public void sendData(boolean data) {
if (openSerialPort()) {
byte[] openbuffer = new byte[]{00, (byte) 0xf1, (byte) 0xff};//发送串口告警
byte[] stopbuffer = new byte[]{00, (byte) 0x01, (byte) 0xff};//关闭串口告警
SerialPort port = StaticVariable.currentPort;
if (port != null) {
Date date = new Date();
if (StaticVariable.LastWarningTime == null) {
StaticVariable.LastWarningTime = new Date();
} else {
long lastWarningTimeTime = StaticVariable.LastWarningTime.getTime();
long time = date.getTime();
if (time - lastWarningTimeTime <= intervaltime) {
return;
}
}
StaticVariable.LastWarningTime = date;
port.writeBytes(openbuffer, openbuffer.length);
try {
Thread.sleep(warntime);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
port.writeBytes(stopbuffer, stopbuffer.length);
}
}
}
}
@Override
public Boolean openSerialPort() {
Boolean discover = false;
if (StaticVariable.currentPort == null) {
discover = discoverSerialPort();
} else {
discover = true;
}
if (discover) {
SerialPort port = StaticVariable.currentPort;
if (port.isOpen()) {
log.info("硬接点告警串口已经打开!");
return true;
} else {
int baudRate = 9600; // 波特率
int parity = SerialPort.EVEN_PARITY; // 校验位
int dataBits = 8; // 数据位
int stopBits = SerialPort.ONE_STOP_BIT; // 停止位
try {
boolean setComResult = port.setComPortParameters(baudRate, dataBits, stopBits, parity); // 设置参数
boolean setComTimes = port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 20000, 0); // 设置超时
if (setComResult && setComTimes) {
port.addDataListener(new DataListener(port.getSystemPortName()));
}
boolean b = port.openPort();
if (b) {
log.info("打开硬接点告警串口成功!");
return true;
} else {
log.error("打开串口失败!");
return false;
}
} catch (Exception e) {
e.printStackTrace();
log.error("打开串口失败" + e.getMessage());
return false;
}
}
} else {
log.error("打开串口失败,找不到硬接点串口!");
return false;
}
}
}

@ -51,5 +51,5 @@ cac:
rsakey: MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKNPuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gAkM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWowcSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99EcvDQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthhYhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3UP8iWi1Qw0Y=
warnport:
name: ttyCH341USB0
intervaltime: 60000
warntime: 5000
intervaltime: 60
warntime: 5

Loading…
Cancel
Save