From 7e52af155b16bdb9197ffc2fbce63beefa6eb9bb Mon Sep 17 00:00:00 2001 From: huangfeng Date: Mon, 30 Sep 2024 13:24:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0IecClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xydl/cac/iec/IecClient.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/main/java/com/xydl/cac/iec/IecClient.java diff --git a/src/main/java/com/xydl/cac/iec/IecClient.java b/src/main/java/com/xydl/cac/iec/IecClient.java new file mode 100644 index 0000000..e728032 --- /dev/null +++ b/src/main/java/com/xydl/cac/iec/IecClient.java @@ -0,0 +1,87 @@ +package com.xydl.cac.iec; + +import com.beanit.iec61850bean.*; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; + +@Slf4j +public class IecClient implements ClientEventListener { + + ClientSap clientSap = new ClientSap(); + ClientAssociation clientAssociation = null; + ServerModel serverModel; + + public void connect(String host, int port, String apTitle, String xml) throws Exception { + InputStream in = IOUtils.toInputStream(xml, StandardCharsets.UTF_8); + this.connect(host, port, apTitle, in); + } + + public void connect(String host, int port, String apTitle, InputStream in) throws Exception { + int[] title = new int[]{1, 3, 9999, 33}; + if (StringUtils.isNotBlank(apTitle)) { + String[] strs = apTitle.replaceAll(" ", ",").split(","); + if (strs.length > 3) { + title[0] = Integer.parseInt(strs[0]); + title[1] = Integer.parseInt(strs[1]); + title[2] = Integer.parseInt(strs[2]); + title[3] = Integer.parseInt(strs[3]); + } + } + clientSap.setTSelRemote(new byte[]{0, 1}); + clientSap.setTSelLocal(new byte[]{0, 0}); + clientSap.setApTitleCalled(title); + + clientAssociation = clientSap.associate(InetAddress.getByName(host), port, null, this); + serverModel = SclParser.parse(in).get(0); + clientAssociation.setServerModel(serverModel); + } + + public void disconnect() { + try { + clientAssociation.disconnect(); + } catch (Exception ignore) { + } + } + + public String getValue(String paramindex, String fc) throws Exception { + FcModelNode node = (FcModelNode) serverModel.findModelNode(paramindex, Fc.valueOf(fc)); + clientAssociation.getDataValues(node); + if (node instanceof BasicDataAttribute) { + String value = ((BasicDataAttribute) node).getValueString(); + return value; + } + return null; + } + + @Override + public void newReport(Report report) { + + } + + @Override + public void associationClosed(IOException e) { + + } + + public static void main(String[] args) { + IecClient iecClient = new IecClient(); + try { + iecClient.connect("192.168.1.17", 102, "1 3 9999 33", new FileInputStream("C:/资料/om.SCD")); + String str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.mag.f", "MX"); + System.out.println(str); + str = iecClient.getValue("OMDLMONT/SPDC1.MaxDsch.t", "MX"); + System.out.println(str); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + iecClient.disconnect(); + } + } +}