|
@@ -0,0 +1,117 @@
|
|
|
|
|
+package com.ruoyi.common.utils;
|
|
|
|
|
+
|
|
|
|
|
+import com.ruoyi.common.utils.cache.CacheManager;
|
|
|
|
|
+import com.ruoyi.common.utils.cache.CacheManagerEntity;
|
|
|
|
|
+import lombok.Value;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+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.messages.PlcWriteRequest;
|
|
|
|
|
+import org.apache.plc4x.java.api.messages.PlcWriteResponse;
|
|
|
|
|
+import org.apache.plc4x.java.api.types.PlcResponseCode;
|
|
|
|
|
+
|
|
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class ModbusUtils {
|
|
|
|
|
+
|
|
|
|
|
+// private PlcConnection plcConnection;
|
|
|
|
|
+
|
|
|
|
|
+ //private static CacheManager cacheManager;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public String plcRead(String url,String uuid,String name,String valueStr) throws Exception{
|
|
|
|
|
+ String value = "";
|
|
|
|
|
+ //建立plc连接
|
|
|
|
|
+ PlcConnection plcConnection = (PlcConnection) CacheManager.getCacheDataByKey(uuid);
|
|
|
|
|
+ if(plcConnection == null){
|
|
|
|
|
+ this.createplcConnection(url,uuid);
|
|
|
|
|
+ plcConnection = (PlcConnection) CacheManager.getCacheDataByKey(uuid);
|
|
|
|
|
+ }
|
|
|
|
|
+// Object entity = cacheManager.getCacheDataByKey("test");
|
|
|
|
|
+ // 2.判断是否可以读取
|
|
|
|
|
+ boolean canRead = plcConnection.getMetadata().canRead();
|
|
|
|
|
+ if (!canRead) {
|
|
|
|
|
+ log.error("该连接不支持读取数据");
|
|
|
|
|
+ //return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 3.创建读取请求
|
|
|
|
|
+ PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
|
|
|
|
|
+ builder.addItem(name,valueStr);
|
|
|
|
|
+ PlcReadRequest readRequest = builder.build();
|
|
|
|
|
+ PlcReadResponse response = readRequest.execute().get(5000,TimeUnit.MILLISECONDS);
|
|
|
|
|
+ for (String fieldName : response.getFieldNames()) {
|
|
|
|
|
+ if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
|
|
|
|
|
+ int numValues = response.getNumberOfValues(fieldName);
|
|
|
|
|
+ if (numValues == 1) {
|
|
|
|
|
+ log.info("Value[" + fieldName + "]: " + response.getObject(fieldName));
|
|
|
|
|
+ value = value + response.getObject(fieldName);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("Value[" + fieldName + "]:");
|
|
|
|
|
+ for (int i = 0; i < numValues; i++) {
|
|
|
|
|
+ if(i != numValues-1){
|
|
|
|
|
+ value = value + response.getObject(fieldName, i)+",";
|
|
|
|
|
+ }else {
|
|
|
|
|
+ value = value + response.getObject(fieldName, i);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info(" - " + response.getObject(fieldName, i));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return value;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void plcWrite(String url,String uuid,String key,String writeStr,Object... value) throws Exception{
|
|
|
|
|
+ PlcConnection plcConnection = (PlcConnection)CacheManager.getCacheDataByKey(uuid);
|
|
|
|
|
+// PlcConnection plcConnection = (PlcConnection) CacheManager.
|
|
|
|
|
+ if(plcConnection == null){
|
|
|
|
|
+ this.createplcConnection(url,uuid);
|
|
|
|
|
+ plcConnection = (PlcConnection) CacheManager.getCacheDataByKey(uuid);
|
|
|
|
|
+ }
|
|
|
|
|
+ boolean canWrite = plcConnection.getMetadata().canWrite();
|
|
|
|
|
+ if (!canWrite) {
|
|
|
|
|
+ log.error("该连接不支持写入数据");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ PlcWriteRequest.Builder builder = plcConnection.writeRequestBuilder();
|
|
|
|
|
+ builder.addItem(key, writeStr, value);
|
|
|
|
|
+ PlcWriteRequest writeRequest = builder.build();
|
|
|
|
|
+ PlcWriteResponse response = writeRequest.execute().get();
|
|
|
|
|
+ for (String fieldName : response.getFieldNames()) {
|
|
|
|
|
+ PlcResponseCode responseCode = response.getResponseCode(fieldName);
|
|
|
|
|
+ if(responseCode == PlcResponseCode.OK) {
|
|
|
|
|
+ log.info("Value[" + fieldName + "]: updated");
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ log.error("Error[" + fieldName + "]: " + responseCode.name());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void createplcConnection(String url,String uuid) throws PlcConnectionException {
|
|
|
|
|
+ PlcConnection plcConnection = new PlcDriverManager().getConnection(url);
|
|
|
|
|
+// CaceUtils.put("tcp",uuid,"test");
|
|
|
|
|
+// CacheManagerEntity entity = new CacheManagerEntity();
|
|
|
|
|
+ System.out.println(uuid);
|
|
|
|
|
+ CacheManager.putCache(uuid, new CacheManagerEntity(plcConnection));
|
|
|
|
|
+// Map<String,CacheManagerEntity> map = CacheManager.getCacheAll();
|
|
|
|
|
+ System.out.println("连接缓存放入成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|