This is an automated email from the ASF dual-hosted git repository.
cgarcia pushed a commit to branch feature/merlot
in repository https://gitbox.apache.org/repos/asf/plc4x-extras.git
The following commit(s) were added to refs/heads/feature/merlot by this push:
new b59ed60 Fixing write prpcedure.
b59ed60 is described below
commit b59ed60c779a259dd2b540b14b2fb86d10d6ffd2
Author: César García <[email protected]>
AuthorDate: Tue Oct 15 10:14:40 2024 -0400
Fixing write prpcedure.
---
.../org/apache/plc4x/merlot/api/PlcDevice.java | 14 +-
.../apache/plc4x/merlot/api/PlcDeviceFactory.java | 4 +-
.../org/apache/plc4x/merlot/api/PlcSecureBoot.java | 2 +-
.../plc4x/merlot/api/command/DemoCommand003.java | 21 ++-
.../merlot/api/command/PlcDeviceDelCommand.java | 2 +-
.../merlot/api/command/PlcDeviceListCommand.java | 4 +-
.../merlot/api/impl/PlcDeviceFactoryImpl.java | 4 +-
.../plc4x/merlot/api/impl/PlcDeviceImpl.java | 108 +++++++++-----
.../merlot/api/impl/PlcGeneralFunctionImpl.java | 86 ++++++++---
.../apache/plc4x/merlot/api/impl/PlcItemImpl.java | 28 ++--
.../plc4x/merlot/api/impl/PlcSecureBootImpl.java | 162 ++++++++++++---------
.../org/apache/plc4x/merlot/db/api/DBRecord.java | 6 +-
.../plc4x/merlot/db/core/DBBooleanFactory.java | 9 +-
.../apache/plc4x/merlot/db/core/DBByteFactory.java | 16 +-
.../plc4x/merlot/db/core/DBDoubleFactory.java | 32 ++--
.../plc4x/merlot/db/core/DBFloatFactory.java | 34 +++--
.../apache/plc4x/merlot/db/core/DBIntFactory.java | 32 ++--
.../apache/plc4x/merlot/db/core/DBLongFactory.java | 30 ++--
.../merlot/db/core/DBRecordsManagedService.java | 24 +--
.../plc4x/merlot/db/core/DBShortFactory.java | 26 ++--
.../plc4x/merlot/db/core/DBUByteFactory.java | 30 ++--
.../apache/plc4x/merlot/db/core/DBUIntFactory.java | 29 ++--
.../plc4x/merlot/db/core/DBULongFactory.java | 30 ++--
.../plc4x/merlot/db/core/DBUShortFactory.java | 31 ++--
.../apache/plc4x/merlot/db/impl/DBPersistImpl.java | 78 ++++++----
.../plc4x/merlot/db/impl/DBWriterHandlerImpl.java | 28 +++-
.../resources/OSGI-INF/blueprint/db-service.xml | 18 ++-
.../drv/mb/impl/ModbusPlcTagFunctionImpl.java | 61 +++++---
.../impl/SimulatedPlcTagFunctionImpl.java | 8 +-
plc4j/tools/merlot/pom.xml | 1 -
30 files changed, 610 insertions(+), 348 deletions(-)
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDevice.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDevice.java
index b0f6d1c..9edcf4e 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDevice.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDevice.java
@@ -36,6 +36,8 @@ public interface PlcDevice extends
org.osgi.service.device.Device,
ConnectionStateListener,
Job {
+ public static final String SERVICE_KEY = "dal.device.key";
+
/*
*
*/
@@ -66,6 +68,16 @@ public interface PlcDevice extends
org.osgi.service.device.Device,
*/
public Hashtable<String, ?> getProperties();
+ /*
+ *
+ */
+ public String getDeviceKey();
+
+ /*
+ *
+ */
+ public void setDeviceKey(String deviceKey);
+
/*
*
*/
@@ -74,7 +86,7 @@ public interface PlcDevice extends
org.osgi.service.device.Device,
/*
*
*/
- public void setDeviceName(String groupname);
+ public void setDeviceName(String deviceName);
/*
*
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDeviceFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDeviceFactory.java
index 9bf5242..7d3aa9f 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDeviceFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcDeviceFactory.java
@@ -22,8 +22,8 @@ package org.apache.plc4x.merlot.api;
public interface PlcDeviceFactory {
/*
- *
+ *
*/
- public PlcDevice create(String device, String url, String name, String
description);
+ public PlcDevice create(String deviceKey, String deviceUrl, String
deviceName, String deviceDescription);
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcSecureBoot.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcSecureBoot.java
index cc4c900..f4adb4d 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcSecureBoot.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/PlcSecureBoot.java
@@ -81,6 +81,6 @@ public interface PlcSecureBoot {
/*
* Restores all resources assigned to the designated PlcDriver.
*/
- public void restore(String plcDriver);
+ public boolean restore(String plcDriver);
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand003.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand003.java
index 1699916..69d247a 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand003.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand003.java
@@ -16,7 +16,6 @@
*/
package org.apache.plc4x.merlot.api.command;
-import java.util.Hashtable;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@@ -27,13 +26,10 @@ import
org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.plc4x.merlot.api.PlcDevice;
import org.apache.plc4x.merlot.api.PlcGeneralFunction;
-import org.apache.plc4x.merlot.scheduler.api.Job;
import org.osgi.framework.BundleContext;
import org.apache.plc4x.merlot.api.PlcGroup;
import org.apache.plc4x.merlot.api.PlcItem;
import org.apache.plc4x.merlot.api.core.PlcItemClientService;
-import org.apache.plc4x.merlot.api.impl.PlcGroupImpl;
-import org.apache.plc4x.merlot.api.impl.PlcItemImpl;
@Service
@@ -64,10 +60,10 @@ public class DemoCommand003 implements Action {
UUID devUuid = UUID.randomUUID();
Optional<PlcDevice> optPlcDevice =
plcGeneralFunction.createDevice(devUuid.toString(),
- "simulated",
- "DEMO003",
- "simulated://localhost",
- "Nombre corto",
+ "modbus-tcp",
+ "AS01",
+ "modbus-tcp://localhost:10502",
+ "+C1=AS01.",
"La descripcion",
"true");
if (optPlcDevice.isPresent()){
@@ -87,15 +83,18 @@ public class DemoCommand003 implements Action {
optPlcDevice.get().getUid().toString(),
"ITEM_" + i,
"Item description _" + i,
- "RANDOM/dummy:int[10]",
+ "4x00001:UINT[16]",
"true");
if (optPlcItem.isPresent()){
- System.out.println("Item create: " +
optPlcItem.get().getItemName());
+ optPlcItem.get().enable();
+
System.out.println(optPlcItem.get().getItemUid().toString()+ " : " +
optPlcItem.get().getItemName());
}
}
- }
+ }
+
+ optPlcDevice.get().enable();
}
return null;
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceDelCommand.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceDelCommand.java
index 1bf630c..54be80d 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceDelCommand.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceDelCommand.java
@@ -55,7 +55,7 @@ public class PlcDeviceDelCommand implements Action {
if (null != name) {
ctxdevices = devices.stream().
- filter(dev -> dev.getDeviceName().equals(name)).
+ filter(dev -> dev.getDeviceKey().equals(name)).
collect(toList());
} else if (null != uuid) {
ctxdevices = devices.stream().
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceListCommand.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceListCommand.java
index 39a3fb5..74871de 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceListCommand.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcDeviceListCommand.java
@@ -61,7 +61,7 @@ public class PlcDeviceListCommand implements Action {
if (null != devices) {
ShellTable table = new ShellTable();
table.column("Uid");
- table.column("Name");
+ table.column("Key");
table.column("Description");
table.column("Enable");
table.column("Url");
@@ -75,7 +75,7 @@ public class PlcDeviceListCommand implements Action {
i[0] += g.getItems().size();
});
table.addRow().addContent(dev.getUid(),
- dev.getDeviceName(),
+ dev.getDeviceKey(),
dev.getDeviceDescription(),
dev.isEnable(),
dev.getUrl(),
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceFactoryImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceFactoryImpl.java
index 224f7d2..31a3e37 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceFactoryImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceFactoryImpl.java
@@ -35,8 +35,8 @@ public class PlcDeviceFactoryImpl implements PlcDeviceFactory
{
}
@Override
- public PlcDevice create(String device, String url, String name, String
description) {
- return new PlcDeviceImpl.PlcDeviceBuilder(bc, url, device,
description).build();
+ public PlcDevice create(String deviceKey, String deviceUrl, String
deviceName, String deviceDescription) {
+ return new PlcDeviceImpl.PlcDeviceBuilder(bc, deviceUrl, deviceKey,
deviceName, deviceDescription).build();
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceImpl.java
index e870bcf..3e67ad6 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcDeviceImpl.java
@@ -21,6 +21,7 @@ import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SequenceBarrier;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
+import io.netty.buffer.ByteBufUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -127,7 +128,8 @@ public class PlcDeviceImpl implements PlcDevice {
//Basic device information
deviceProperties.put(PlcDevice.SERVICE_DRIVER,
builder.service_driver);
- deviceProperties.put(PlcDevice.SERVICE_NAME, builder.service_name);
+ deviceProperties.put(PlcDevice.SERVICE_KEY, builder.service_name);
+ deviceProperties.put(PlcDevice.SERVICE_NAME,
builder.service_short_desc);
deviceProperties.put(PlcDevice.SERVICE_DESCRIPTION,
builder.service_description);
//Adjusting monitoring times.
@@ -219,41 +221,57 @@ public class PlcDeviceImpl implements PlcDevice {
if (null != event.getPlcItem()){
if (null != plcConnection) {
if (refPlcConnection.get().isConnected()) {
- if (messageCounter[0] == 0) {
- readProcessor.pause();
- }
- messageCounter[0]++;
-
- if (null != plcTagFunction) {
- writeBuffer.add(plcTagFunction.getPlcTag(
- event.getPlcItem().getItemPlcTag(),
- event.getByteBuf(),
event.getOffset()));
- }
-
- /*
- * A maximum of DEFAULT_WRITE_BATCH_SIZE or
there
- * are no more messages in the ringbuffer,
- * the write will proceed.
- */
- if ((messageCounter[0] >
DEFAULT_WRITE_BATCH_SIZE) || (endofbatch)) {
- if (!writeBuffer.isEmpty()) {
- final Builder builder =
refPlcConnection.get().writeRequestBuilder();
-
- writeBuffer.forEach(i ->
builder.addTag(Long.toString(System.nanoTime()), i.left, i.right));
+ try {
+ if (messageCounter[0] == 0) {
+ readProcessor.pause();
+ }
+ messageCounter[0]++;
+
+ if (null != plcTagFunction) {
+
+ System.out.println("TAG: " +
event.getPlcItem().getItemPlcTag());
+ System.out.println("OFFSET: " +
event.getOffset());
+ System.out.println("BYTEBUF: \r\n" +
ByteBufUtil.prettyHexDump(event.getByteBuf()));
- final PlcWriteRequest writeRequest =
builder.build();
- writeBuffer.clear();
- //TODO: Max time of waiting
- PlcWriteResponse writeResponse =
- writeRequest.execute().get();
- //TODO: Change to debug
- writeResponse.getTagNames().forEach(
t->
- LOGGER.info("Write tag[{}] is
{}", t, writeResponse.getResponseCode(t))
- );
+
writeBuffer.add(plcTagFunction.getPlcTag(
+
event.getPlcItem().getItemPlcTag(),
+ event.getByteBuf(),
event.getOffset()));
+ }
+
+ /*
+ * A maximum of DEFAULT_WRITE_BATCH_SIZE or
there
+ * are no more messages in the ringbuffer,
+ * the write will proceed.
+ */
+ if ((messageCounter[0] >
DEFAULT_WRITE_BATCH_SIZE) || (endofbatch)) {
+ if (!writeBuffer.isEmpty()) {
+ final Builder builder =
refPlcConnection.get().writeRequestBuilder();
+
+ writeBuffer.forEach(i -> {
+ if (null != i)
+
builder.addTag(Long.toString(System.nanoTime()), i.left, i.right);
+ });
+
+ final PlcWriteRequest writeRequest
= builder.build();
+ writeBuffer.clear();
+ //TODO: Max time of waiting
+ PlcWriteResponse writeResponse =
+ writeRequest.execute().get();
+ //TODO: Change to debug
+
writeResponse.getTagNames().forEach( t->
+ LOGGER.info("Write tag[{}]
is {}", t, writeResponse.getResponseCode(t))
+ );
+ }
+
}
+
+ } catch (Exception ex) {
+ LOGGER.error(ex.getMessage());
+ } finally {
readProcessor.restart();
- messageCounter[0] = 0;
- }
+ messageCounter[0] = 0;
+ }
+
}
}
}
@@ -265,11 +283,11 @@ public class PlcDeviceImpl implements PlcDevice {
//TODO: Lanzar las tareas.
threadWriteProcessor = new Thread(writeProcessor);
- threadWriteProcessor.setName(this.getDeviceName() + WRITE_TASK_NAME);
+ threadWriteProcessor.setName(this.getDeviceKey() + "_"
+WRITE_TASK_NAME);
threadWriteProcessor.start();
threadReadProcessor = new Thread(readProcessor);
- threadReadProcessor.setName(this.getDeviceName() + READ_TASK_NAME);
+ threadReadProcessor.setName(this.getDeviceKey() + "_"
+READ_TASK_NAME);
threadReadProcessor.start();
}
@@ -349,14 +367,24 @@ public class PlcDeviceImpl implements PlcDevice {
return deviceProperties;
}
+ @Override
+ public String getDeviceKey() {
+ return (String) deviceProperties.get(PlcDevice.SERVICE_KEY);
+ }
+
+ @Override
+ public void setDeviceKey(String devicename) {
+ deviceProperties.put(PlcDevice.SERVICE_KEY, devicename);
+ }
+
@Override
public String getDeviceName() {
- return (String) deviceProperties.get(Device.SERVICE_NAME);
+ return (String) deviceProperties.get(PlcDevice.SERVICE_NAME);
}
@Override
- public void setDeviceName(String devicename) {
- deviceProperties.put(Device.SERVICE_NAME, devicename);
+ public void setDeviceName(String deviceName) {
+ deviceProperties.put(PlcDevice.SERVICE_NAME, deviceName);
}
@Override
@@ -541,6 +569,7 @@ public class PlcDeviceImpl implements PlcDevice {
public static class PlcDeviceBuilder {
private final BundleContext bc;
private final String service_name;
+ private final String service_short_desc;
private final String service_description;
private final String service_driver;
private UUID service_uid;
@@ -556,9 +585,10 @@ public class PlcDeviceImpl implements PlcDevice {
private String service_status_detail;
private String[] service_types;
- public PlcDeviceBuilder(BundleContext bc, String service_driver,
String service_name, String service_description) {
+ public PlcDeviceBuilder(BundleContext bc, String service_driver,
String service_name, String service_short_desc, String service_description) {
this.bc = bc;
this.service_name = service_name;
+ this.service_short_desc = service_short_desc;
this.service_description = service_description;
this.service_driver = service_driver;
String[] drv = service_driver.split(":");
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGeneralFunctionImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGeneralFunctionImpl.java
index 2fc3d2a..7a22cde 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGeneralFunctionImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGeneralFunctionImpl.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@@ -59,7 +60,7 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
"(" +
org.osgi.service.device.Constants.DEVICE_CATEGORY + "=*))";
private static String FILTER_DEVICE = "(&(" + Constants.OBJECTCLASS + "="
+ Device.class.getName() + ")" +
- "(" +
org.apache.plc4x.merlot.api.PlcDevice.SERVICE_NAME + "=*))";
+ "(" +
org.apache.plc4x.merlot.api.PlcDevice.SERVICE_KEY + "=*))";
private static String FILTER_FACTORY = "(&(" + Constants.OBJECTCLASS +
"=" + PlcDeviceFactory.class.getName() + ")" +
"(org.apache.plc4x.device.factory=*))";
@@ -408,6 +409,23 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
try {
Map<UUID, String> plcDevices = getPlcDevices(DriverName);
+
+ if (plcDevices.containsValue(DeviceName)) {
+ Optional<Entry<UUID, String>> optEntry = plcDevices.
+ entrySet().
+ stream().
+ filter(e -> e.getValue().equals(DeviceName)).
+ findFirst();
+ if (optEntry.isPresent()){
+ PlcDevice plcDevice =
getPlcDevice(optEntry.get().getKey());
+ final String driver = (String)
plcDevice.getServiceProperty(PlcDevice.SERVICE_DRIVER);
+ if (driver.equals(DriverName)) {
+ LOGGER.info("PLCGF Device [%s] alredy exist.",
plcDevice.getDeviceKey());
+ return Optional.of(plcDevice);
+ }
+ }
+ }
+
if (!plcDevices.values().contains(DeviceName)){
String factoryFilter = FILTER_FACTORY.replace("*",
DriverName);
ServiceReference[] references =
bc.getServiceReferences((String) null, factoryFilter);
@@ -416,11 +434,12 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
PlcDeviceFactory bdf = (PlcDeviceFactory)
bc.getService(reference);
if (null != bdf){
PlcDevice device = bdf.create(DeviceName, DeviceId,
DeviceShortName, DeviceDescription);
- if (null != device) {
+ if (null != device) {
+ device.setUid(UUID.fromString(DeviceUuid));
device.init();
- if (DeviceEnable.equals("true")) {
- device.enable();
- } else device.disable();
+// if (DeviceEnable.equals("true")) {
+// device.enable();
+// } else device.disable();
bc.registerService(new
String[]{org.apache.plc4x.merlot.api.PlcDevice.class.getName(),
org.apache.plc4x.merlot.scheduler.api.Job.class.getName()},
device,
device.getProperties());
@@ -438,11 +457,12 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
if ((null != references) && (references.length > 0)) {
ServiceReference reference = references[0];
PlcDeviceFactory bdf = (PlcDeviceFactory)
bc.getService(reference);
- PlcDevice device = bdf.create(DeviceName, DeviceId,
DeviceShortName, DeviceDescription);
+ PlcDevice device = bdf.create(DeviceName, DeviceId,
DeviceShortName, DeviceDescription);
+ device.setUid(UUID.fromString(DeviceUuid));
device.init();
- if (DeviceEnable.equals("true")) {
- device.enable();
- } else device.disable();
+// if (DeviceEnable.equals("true")) {
+// device.enable();
+// } else device.disable();
bc.registerService(new
String[]{org.apache.plc4x.merlot.api.PlcDevice.class.getName(),
org.apache.plc4x.merlot.scheduler.api.Job.class.getName()},
device,
device.getProperties());
@@ -452,7 +472,7 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
}
}
} else {
- LOGGER.info("Device already exists");
+ LOGGER.info("PLCGF Device [%s] already exists", DeviceName);
}
} catch (Exception ex){
@@ -467,16 +487,32 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
String GroupScanTime, String GroupEnable) {
try {
- final PlcDevice plcDevice =
getPlcDevice(UUID.fromString(DeviceUuid));
+ final PlcDevice plcDevice =
getPlcDevice(UUID.fromString(DeviceUuid));
+
+ Map<UUID, String> plcDeviceGroups =
getPlcDeviceGroups(plcDevice.getUid());
+ if (plcDeviceGroups.containsValue(GroupName)){
+ Optional<Entry<UUID, String>> optEntry = plcDeviceGroups.
+ entrySet().
+ stream().
+ filter(e -> e.getValue().equals(GroupName)).
+ findFirst();
+ if (optEntry.isPresent()) {
+ PlcGroup plcGroup = getPlcGroup(optEntry.get().getKey());
+ LOGGER.info("PlcGroup [%s] alredy exist.",
plcGroup.getGroupName());
+ return Optional.of(plcGroup);
+ }
+ }
+
if (null != plcDevice){
PlcGroup plcGroup = new PlcGroupImpl.PlcGroupBuilder(bc,
GroupName, UUID.fromString(GroupUuid)).
setGroupPeriod(Long.parseLong(GroupScanTime)).
setGroupDeviceUid(plcDevice.getUid()).
+
setGroupDescription(GroupDescription).
build();
if (GroupEnable.equals("true")){
plcGroup.enable();
} else plcGroup.disable();
- plcDevice.putGroup(plcGroup);
+ plcDevice.putGroup(plcGroup);
return Optional.of(plcGroup);
} else {
LOGGER.info("Device don´t exists");
@@ -496,6 +532,16 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
try {
final PlcGroup plcGroup = getPlcGroup(UUID.fromString(GroupUuid));
+
+ Map<UUID, String> plcGroupItems =
getPlcGroupItems(plcGroup.getGroupUid());
+ if (plcGroupItems.containsValue(ItemName)){
+ Optional<PlcItem> optPlcItem = getPlcItem(ItemName);
+ if (optPlcItem.isPresent()) {
+ LOGGER.info("PlcItem [%s] alredy exist.",
optPlcItem.get().getItemName());
+ return Optional.of(optPlcItem.get());
+ }
+ }
+
PlcItem plcItem = new PlcItemImpl.PlcItemBuilder(ItemName).
setItemDescription(ItemDescription).
setItemId(ItemTag).
@@ -526,7 +572,7 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
}
}
} catch (InvalidSyntaxException ex) {
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
} finally {
return drivers;
}
@@ -542,7 +588,7 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
if (null != refs) {
for (ServiceReference ref:refs){
final PlcDevice device = (PlcDevice) bc.getService(ref);
- plcdevices.put(device.getUid(),device.getDeviceName());
+ plcdevices.put(device.getUid(),device.getDeviceKey());
}
}
} catch (InvalidSyntaxException ex) {
@@ -590,18 +636,18 @@ public class PlcGeneralFunctionImpl implements
PlcGeneralFunction {
}
@Override
- public void setPlcDeviceName(UUID device_uid, String group_name) {
- final PlcDevice plcdevice = getPlcDevice(device_uid);
+ public void setPlcDeviceName(UUID deviceUuid, String deviceName) {
+ final PlcDevice plcdevice = getPlcDevice(deviceUuid);
if (null != plcdevice){
- plcdevice.setDeviceName(group_name);
+ plcdevice.setDeviceKey(deviceName);
}
}
@Override
- public void setPlcDeviceDesc(UUID device_uid, String device_desc) {
- final PlcDevice plcdevice = getPlcDevice(device_uid);
+ public void setPlcDeviceDesc(UUID deviceUuid, String deviceDesc) {
+ final PlcDevice plcdevice = getPlcDevice(deviceUuid);
if (null != plcdevice){
- plcdevice.setDeviceDescription(device_desc);
+ plcdevice.setDeviceDescription(deviceDesc);
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcItemImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcItemImpl.java
index 456e190..be3251d 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcItemImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcItemImpl.java
@@ -35,12 +35,15 @@ import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.values.PlcList;
import org.apache.plc4x.merlot.api.PlcItem;
import org.apache.plc4x.merlot.api.PlcItemListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/*
*
*/
public class PlcItemImpl implements PlcItem {
-
+ private static final Logger LOGGER =
LoggerFactory.getLogger(PlcItem.class);
+
private ReentrantLock lock = new ReentrantLock();
private String itemName;
private String itemDescription;
@@ -233,6 +236,8 @@ public class PlcItemImpl implements PlcItem {
itemInnerBuffer = (size == -1) ? new
byte[plcvalue.getRaw().length] :
new byte[size];
itemBuffer = Unpooled.wrappedBuffer(itemInnerBuffer);
+ //Update all clients
+ itemClients.forEach(c -> c.atach(this));
}
//Transfers data to a byte buffer
@@ -245,6 +250,7 @@ public class PlcItemImpl implements PlcItem {
} catch (Exception ex){
itemErrors++;
lastErrorDate = Date.from(Instant.now());
+ LOGGER.error(ex.getMessage());
}
//Update stat data
@@ -270,14 +276,14 @@ public class PlcItemImpl implements PlcItem {
@Override
public ByteBuf getItemByteBuf() {
- lock.lock();
- ByteBuf itembuffer;
- try {
- itembuffer = itemBuffer.duplicate();
- } finally {
- lock.unlock();
- }
- return itembuffer;
+// lock.lock();
+// ByteBuf itembuffer;
+// try {
+// itembuffer = itemBuffer.duplicate();
+// } finally {
+// lock.unlock();
+// }
+ return itemBuffer;
}
@Override
@@ -338,7 +344,7 @@ public class PlcItemImpl implements PlcItem {
@Override
public void itemWrite(final ByteBuf byteBuf, int offset) {
if (null == writeRingBuffer) {
- System.out.println("*** Rinbuffer es null ***");
+ LOGGER.info("*** Rinbuffer es null ***");
return;
}
long sequenceId = writeRingBuffer.next();
@@ -359,7 +365,7 @@ public class PlcItemImpl implements PlcItem {
append("Is enable: ").append(itemEnable).append("\r\n").
append("Access rigths: ").append(itemAccessrigths).append("\r\n").
append("Disable output:
").append(itemDisableOutput).append("\r\n").
- append("Number of clients: ").append(itemClients).append("\r\n").
+ append("Number of clients:
").append(itemClients.size()).append("\r\n").
append("Transmits: ").append(itemClients).append("\r\n").
append("Last transmits date:
").append(lastWriteDate).append("\r\n").
append("Receives: ").append(itemClients).append("\r\n").
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
index dc37fb3..cd0118c 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcSecureBootImpl.java
@@ -53,9 +53,9 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
"CREATE TABLE IF NOT EXISTS Devices("
+ "DeviceUuId TEXT NOT NULL PRIMARY KEY,"
+ "DriverName TEXT,"
+ + "DeviceKey TEXT,"
+ + "DeviceUrl TEXT,"
+ "DeviceName TEXT,"
- + "DeviceId TEXT,"
- + "DeviceShortName TEXT,"
+ "DeviceDescription TEXT,"
+ "DeviceEnable TEXT,"
+ "Md5 TEXT)";
@@ -65,7 +65,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
+ "GroupUuid TEXT NOT NULL PRIMARY KEY,"
+ "DeviceUuid TEXT,"
+ "GroupName TEXT,"
- + "GroupDescripcion TEXT,"
+ + "GroupDescription TEXT,"
+ "GroupScantime TEXT,"
+ "GroupEnable TEXT,"
+ "Md5 TEXT)";
@@ -76,36 +76,37 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
+ "DeviceUuid TEXT,"
+ "GroupUuid TEXT,"
+ "ItemName TEXT,"
- + "ItemDescripcion TEXT,"
+ + "ItemDescription TEXT,"
+ "ItemTag TEXT,"
+ "ItemEnable TEXT,"
+ "Md5 TEXT)";
private static final String SQL_SELECT_DEVICES =
- "SELECT * FROM Devices WHERE DriverName = ?";
+ "SELECT * FROM Devices";
private static final String SQL_SELECT_GROUPS =
- "SELECT * FROM Groups WHERE DeviceId = ?";
+ "SELECT * FROM Groups WHERE DeviceUuid = '?'";
private static final String SQL_SELECT_ITEMS =
- "SELECT * FROM WHERE GroupId = ?";
+ "SELECT * FROM Items WHERE GroupUuid = '?'";
private static final String SQL_INSERT_DEVICE =
- "INSERT INTO Devices(DeviceUuId, DriverName, DeviceName, DeviceId,
ShortName, Description, Enable, Md5)"
+ "INSERT INTO Devices(DeviceUuId, DriverName, DeviceKey, DeviceUrl,
DeviceName, DeviceDescription, DeviceEnable, Md5)"
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?) "
+ "ON CONFLICT(DeviceUuId) "
+ "DO "
+ "UPDATE SET "
+ "DriverName = excluded.DriverName, "
- + "DeviceName = excluded.DeviceName, "
- + "DeviceId = excluded.DeviceId, "
- + "ShortName = excluded.DeviceShortName, "
- + "Description= excluded.DeviceDescription, "
+ + "DeviceKey = excluded.DeviceKey, "
+ + "DeviceUrl = excluded.DeviceUrl, "
+ + "DeviceName = excluded.DeviceName, "
+ + "DeviceDescription= excluded.DeviceDescription, "
+ + "DeviceEnable= excluded.DeviceEnable, "
+ "Md5 = excluded.Md5;";
private static final String SQL_INSERT_GROUP =
- "INSERT INTO Groups(GroupUuid, DeviceUuid, GroupName,
GroupDescripcion, GroupScantime, Enable, Md5)"
+ "INSERT INTO Groups(GroupUuid, DeviceUuid, GroupName,
GroupDescription, GroupScantime, GroupEnable, Md5)"
+ "VALUES(?, ?, ?, ?, ?, ?, ?) "
+ "ON CONFLICT(GroupUuid) "
+ "DO "
@@ -113,12 +114,13 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
+ "GroupUuid = excluded.GroupUuid, "
+ "DeviceUuid = excluded.DeviceUuid, "
+ "GroupName = excluded.GroupName, "
- + "GroupDescripcion = excluded.GroupDescripcion, "
+ + "GroupDescription = excluded.GroupDescription, "
+ "GroupScantime = excluded.GroupScantime, "
+ + "GroupEnable = excluded.GroupEnable, "
+ "Md5 = excluded.Md5;";
private static final String SQL_INSERT_ITEM =
- "INSERT INTO Items(ItemUuid, DeviceUuid, GroupUuid, ItemName,
ItemDescripcion, ItemTag, Enable, Md5)"
+ "INSERT INTO Items(ItemUuid, DeviceUuid, GroupUuid, ItemName,
ItemDescription, ItemTag, ItemEnable, Md5)"
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?) "
+ "ON CONFLICT(ItemUuid) "
+ "DO "
@@ -127,8 +129,9 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
+ "DeviceUuid = excluded.DeviceUuid, "
+ "GroupUuid = excluded.GroupUuid, "
+ "ItemName = excluded.ItemName, "
- + "ItemDescripcion = excluded.ItemDescripcion, "
- + "ItemTag = excluded.ItemTag, "
+ + "ItemDescription = excluded.ItemDescription, "
+ + "ItemTag = excluded.ItemTag, "
+ + "ItemEnable = excluded.ItemEnable, "
+ "Md5 = excluded.Md5;";
private Map<String, PlcDriver> delayedBootPlcDivers = new
ConcurrentHashMap<>();
@@ -155,7 +158,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
dbConnection = ds.getConnection();
if (null != dbConnection) {
var databaseMetaData = dbConnection.getMetaData();
- LOGGER.info("Boot driver name is {}.",
databaseMetaData.getDriverName());
+ LOGGER.info("Boot driver name is [%s].",
databaseMetaData.getDriverName());
createTables();
//Catalog,Schema, Table pattern,types of tables
try(ResultSet resultSet = databaseMetaData.getTables(null,
null, null, new String[]{"TABLE"})){
@@ -168,7 +171,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
dbConnection.close();
}
} catch (SQLException ex) {
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
}
}
}
@@ -186,11 +189,11 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
@Override
public void bindPlcDriver(PlcDriver plcDriver) {
- LOGGER.info("Loading driver: {}",plcDriver.getProtocolCode());
+ LOGGER.info("Loading driver: [%s].",plcDriver.getProtocolCode());
if (null != dbConnection) {
-
+ restore(plcDriver.getProtocolCode());
} else {
- LOGGER.info("Delayed start of driver
{}.",plcDriver.getProtocolCode());
+ LOGGER.info("Delayed start of driver
[%s].",plcDriver.getProtocolCode());
delayedBootPlcDivers.put(plcDriver.getProtocolCode(), plcDriver);
}
}
@@ -208,11 +211,15 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
@Override
public void execute(JobContext context) {
+ boolean res = false;
if (null != dbConnection) {
if (!delayedBootPlcDivers.isEmpty()) {
- Set<String> keys = delayedBootPlcDivers.keySet();
- for (String key:keys) {
- delayedBootPlcDivers.remove(key);
+ if (null != dbConnection) {
+ Set<String> keys = delayedBootPlcDivers.keySet();
+ for (String key:keys) {
+ res = restore(key);
+ delayedBootPlcDivers.remove(key);
+ }
}
}
}
@@ -240,7 +247,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
insertDevice(plcDriver, plcDevice);
var plcGroups = plcGeneralFunction.getPlcDeviceGroups(duid);
- System.out.println("Tamano de grupos: " + plcGroups.size());
+
plcGroups.forEach((guid, gname) -> {
var plcGroup = plcGeneralFunction.getPlcGroup(guid);
try {
@@ -260,75 +267,96 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
});
});
} catch (SQLException ex) {
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
}
});
}
@Override
- public void restore(String plcDriver) {
+ public boolean restore(String plcDriver) {
+ boolean res = false;
if (null != dbConnection) {
try {
+
var stmt = dbConnection.createStatement();
//PlcDevice
var rsDevices = stmt.executeQuery(SQL_SELECT_DEVICES);
while (rsDevices.next()) {
+ String isDeviceEnable =
rsDevices.getString("DeviceEnable");
Optional<PlcDevice> optPlcDevice =
plcGeneralFunction.createDevice(
rsDevices.getString("DeviceUuid"),
rsDevices.getString("DriverName"),
+ rsDevices.getString("DeviceKey"),
+ rsDevices.getString("DeviceUrl"),
rsDevices.getString("DeviceName"),
- rsDevices.getString("DeviceId"),
-
rsDevices.getString("DeviceShortName"),
rsDevices.getString("DeviceDescription"),
rsDevices.getString("DeviceEnable"));
- if (optPlcDevice.isPresent())
- LOGGER.info("Created PlcDevice [?].",
optPlcDevice.get().getDeviceName());
-
- //PlcGroups
- var rsGroups = stmt.executeQuery(SQL_SELECT_GROUPS);
- while (rsGroups.next()) {
- Optional<PlcGroup> optPlcGroup =
plcGeneralFunction.createGroup(
- rsGroups.getString("GroupUuid"),
- rsGroups.getString("DeviceUuid"),
- rsGroups.getString("GroupName"),
-
rsGroups.getString("GroupDescription"),
-
rsGroups.getString("GroupScanTime"),
- rsGroups.getString("GroupEnable"));
- if (optPlcGroup.isPresent())
- LOGGER.info("Created PlcGroup [?].",
optPlcGroup.get().getGroupName());
-
- //PlcItems
- var rsItems = stmt.executeQuery(SQL_SELECT_ITEMS);
- while (rsItems.next()) {
- Optional<PlcItem> optPlcItem =
plcGeneralFunction.createItem(
- rsItems.getString("ItemUuid"),
- rsItems.getString("GroupUuid"),
- rsItems.getString("DeviceUuid"),
- rsItems.getString("ItemName"),
-
rsItems.getString("ItemDescription"),
- rsItems.getString("ItemTag"),
- rsItems.getString("ItemEnable"));
- if (optPlcItem.isPresent())
- LOGGER.info("Created PlcItem [?].",
optPlcItem.get().getItemName());
-
+
+ if (optPlcDevice.isPresent()) {
+ LOGGER.info("Created PlcDevice [{}].",
optPlcDevice.get().getDeviceKey());
+
+ //PlcGroups
+ String queryGroups = SQL_SELECT_GROUPS.replace("?",
optPlcDevice.get().getUid().toString());
+
+ var rsGroups = stmt.executeQuery(queryGroups);
+ while (rsGroups.next()) {
+ Optional<PlcGroup> optPlcGroup =
plcGeneralFunction.createGroup(
+
rsGroups.getString("GroupUuid"),
+
rsGroups.getString("DeviceUuid"),
+
rsGroups.getString("GroupName"),
+
rsGroups.getString("GroupDescription"),
+
rsGroups.getString("GroupScanTime"),
+
rsGroups.getString("GroupEnable"));
+
+ if (optPlcGroup.isPresent()) {
+ LOGGER.info("Created PlcGroup [{}].",
optPlcGroup.get().getGroupName());
+// String isGroupEnable =
rsGroups.getString("GroupEnable");
+// if (isGroupEnable.equals("true"))
optPlcGroup.get().enable();
+
+ //PlcItems
+ String queryItems =
SQL_SELECT_ITEMS.replace("?", optPlcGroup.get().getGroupUid().toString());
+
+ var rsItems = stmt.executeQuery(queryItems);
+ while (rsItems.next()) {
+ Optional<PlcItem> optPlcItem =
plcGeneralFunction.createItem(
+
rsItems.getString("ItemUuid"),
+
rsItems.getString("GroupUuid"),
+
rsItems.getString("DeviceUuid"),
+
rsItems.getString("ItemName"),
+
rsItems.getString("ItemDescription"),
+
rsItems.getString("ItemTag"),
+
rsItems.getString("ItemEnable"));
+
+ if (optPlcItem.isPresent())
+ LOGGER.info("Created PlcItem [{}].",
optPlcItem.get().getItemName());
+ }
+ }
}
}
+
+ if (isDeviceEnable.equals("true"))
optPlcDevice.get().enable();
+
}
-
ServiceReference ref =
ctx.getServiceReference(EventAdmin.class.getName());
- if (ref != null){
+ if (ref != null){
EventAdmin eventAdmin = (EventAdmin) ctx.getService(ref);
Event eventPersist = new Event(EVENT_RESTORE, (Map) null);
- eventAdmin.sendEvent(eventPersist);
+ eventAdmin.sendEvent(eventPersist);
+ LOGGER.info("Allow clients to restore their state.");
}
+
+ res = true;
+
} catch (Exception ex) {
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
}
} else {
LOGGER.info("Database don't created.");
}
+
+ return res;
}
private void createTables() throws SQLException{
@@ -345,9 +373,9 @@ public class PlcSecureBootImpl implements PlcSecureBoot,
Job {
var query = dbConnection.prepareStatement(SQL_INSERT_DEVICE);
query.setString(1, plcDevice.getUid().toString());
query.setString(2, driverName);
- query.setString(3, plcDevice.getDeviceName());
- query.setString(4, plcDevice.getUid().toString());
- query.setString(5, "");
+ query.setString(3, plcDevice.getDeviceKey());
+ query.setString(4, plcDevice.getUrl());
+ query.setString(5, plcDevice.getDeviceName());
query.setString(6, plcDevice.getDeviceDescription());
query.setString(7, Boolean.toString(plcDevice.isEnable()));
query.setString(8,"");
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/api/DBRecord.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/api/DBRecord.java
index 88c87e8..ddcadd8 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/api/DBRecord.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/api/DBRecord.java
@@ -52,17 +52,17 @@ public class DBRecord extends PVRecord {
}
public Optional<PlcItem> getPlcItem(){
- if (null == plcItem) Optional.empty();
+ if (null == plcItem) return Optional.empty();
return Optional.of(plcItem);
};
public Optional<ByteBuf> getInnerBuffer(){
- if (null == innerBuffer) Optional.empty();
+ if (null == innerBuffer) return Optional.empty();
return Optional.of(innerBuffer);
};
public Optional<ByteBuf> getWriteBuffer(){
- if (null == innerWriteBuffer) Optional.empty();
+ if (null == innerWriteBuffer) return Optional.empty();
return Optional.of(innerWriteBuffer);
};
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBBooleanFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBBooleanFactory.java
index b1701bb..b6f7da1 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBBooleanFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBBooleanFactory.java
@@ -88,12 +88,14 @@ public class DBBooleanFactory extends DBBaseFactory {
class DBBooleanRecord extends DBRecord implements PlcItemListener {
private PVBoolean value;
- private PVBoolean write_value;
+ private PVBoolean write_value;
+ private PVBoolean write_enable;
public DBBooleanRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getBooleanField("value");
- write_value = pvStructure.getBooleanField("write_value");
+ write_value = pvStructure.getBooleanField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -119,7 +121,8 @@ public class DBBooleanFactory extends DBBaseFactory {
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Byte.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Byte.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Byte.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBByteFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBByteFactory.java
index 488a9aa..eba8d96 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBByteFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBByteFactory.java
@@ -112,13 +112,13 @@ public class DBByteFactory extends DBBaseFactory {
*/
public void process()
{
- if (null != plcItem) {
+ if (null != plcItem) {
if (value.get() != write_value.get()) {
- if (write_enable.get()) {
- write_value.put(value.get());
- innerWriteBuffer.clear();
- innerWriteBuffer.writeByte(write_value.get());
- super.process();
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeByte(write_value.get());
+ super.process();
}
}
}
@@ -127,8 +127,8 @@ public class DBByteFactory extends DBBaseFactory {
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
- offset = this.getPVStructure().getIntField("offset").get() *
Byte.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Byte.BYTES);
+ offset = this.getPVStructure().getIntField("offset").get() *
Byte.BYTES;
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Byte.BYTES);
innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBDoubleFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBDoubleFactory.java
index 4e52b42..642ea54 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBDoubleFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBDoubleFactory.java
@@ -29,6 +29,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVDouble;
import org.epics.pvdata.pv.PVDoubleArray;
import org.epics.pvdata.pv.PVStructure;
@@ -51,6 +52,7 @@ public class DBDoubleFactory extends DBBaseFactory {
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvDouble)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -88,13 +90,14 @@ public class DBDoubleFactory extends DBBaseFactory {
class DBDoubleRecord extends DBRecord implements PlcItemListener {
private PVDouble value;
- private PVDouble write_value;
- private int offset = 0;
+ private PVDouble write_value;
+ private PVBoolean write_enable;
public DBDoubleRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getDoubleField("value");
- write_value = pvStructure.getDoubleField("write_value");
+ write_value = pvStructure.getDoubleField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -103,19 +106,24 @@ public class DBDoubleFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por Double");
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeDouble(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
- offset = this.getPVStructure().getIntField("offset").get() *
Double.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Double.BYTES);
+ offset = this.getPVStructure().getIntField("offset").get();
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Double.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -132,7 +140,7 @@ public class DBDoubleFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBFloatFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBFloatFactory.java
index bd7ef81..3dcb146 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBFloatFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBFloatFactory.java
@@ -51,7 +51,8 @@ public class DBFloatFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvFloat)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -89,13 +90,16 @@ public class DBFloatFactory extends DBBaseFactory {
class DBFloatRecord extends DBRecord implements PlcItemListener {
private PVFloat value;
- private PVFloat write_value;
- private int offset = 0;
+ private PVFloat write_value;
+ private PVBoolean write_enable;
public DBFloatRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getFloatField("value");
- write_value = pvStructure.getFloatField("write_value");
+ write_value = pvStructure.getFloatField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
+ offset = pvStructure.getIntField("offset").get();
+ System.out.println("EL OFFSET EN EL RECORD: " + offset);
}
/**
@@ -104,13 +108,13 @@ public class DBFloatFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por Float");
+ if (null != plcItem) {
if (value.get() != write_value.get()) {
- final PVBoolean isWriteEnbale =
this.getPVStructure().getBooleanField("write_enable");
- if (isWriteEnbale.get()) {
- write_value.put(value.get());
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeFloat(write_value.get());
+ super.process();
}
}
}
@@ -118,9 +122,11 @@ public class DBFloatFactory extends DBBaseFactory {
@Override
public void atach(PlcItem plcItem) {
- this.plcItem = plcItem;
- offset = this.getPVStructure().getIntField("offset").get() *
Float.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Float.BYTES);
+ this.plcItem = plcItem;
+ offset = this.getPVStructure().getIntField("offset").get();
+ System.out.println("Y EN ATACH EL OFFSET EN EL RECORD: " +
offset);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Float.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -137,7 +143,7 @@ public class DBFloatFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBIntFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBIntFactory.java
index 8587121..3baee7f 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBIntFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBIntFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVInt;
import org.epics.pvdata.pv.PVIntArray;
import org.epics.pvdata.pv.PVStructure;
@@ -49,7 +50,8 @@ public class DBIntFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvInt)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -87,13 +89,14 @@ public class DBIntFactory extends DBBaseFactory {
class DBIntRecord extends DBRecord implements PlcItemListener {
private PVInt value;
- private PVInt write_value;
- private int offset = 0;
+ private PVInt write_value;
+ private PVBoolean write_enable;
public DBIntRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getIntField("value");
- write_value = pvStructure.getIntField("write_value");
+ write_value = pvStructure.getIntField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -102,19 +105,24 @@ public class DBIntFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por Int");
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeInt(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Integer.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Integer.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset,
Integer.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -131,7 +139,7 @@ public class DBIntFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBLongFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBLongFactory.java
index d8f20ed..cf9f975 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBLongFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBLongFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVLong;
import org.epics.pvdata.pv.PVLongArray;
import org.epics.pvdata.pv.PVStructure;
@@ -49,7 +50,8 @@ public class DBLongFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvLong)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -88,12 +90,13 @@ public class DBLongFactory extends DBBaseFactory {
private PVLong value;
private PVLong write_value;
- private int offset = 0;
+ private PVBoolean write_enable;
public DBLongRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getLongField("value");
- write_value = pvStructure.getLongField("write_value");
+ write_value = pvStructure.getLongField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -102,19 +105,24 @@ public class DBLongFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
-
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeLong(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Long.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Long.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Long.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -131,7 +139,7 @@ public class DBLongFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBRecordsManagedService.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBRecordsManagedService.java
index 572dd9e..1e924fc 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBRecordsManagedService.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBRecordsManagedService.java
@@ -186,19 +186,23 @@ public class DBRecordsManagedService implements
ManagedServiceFactory, Job {
}
//Si todo OK, los agregoa a la base de datos
- LOGGER.info("Agrega la lista de Records...");
dbRecords.forEach(pvr -> {
PVStructure structure = pvr.getPVStructure();
PVBoolean pvScanEnable =
structure.getBooleanField("scan_enable");
pvScanEnable.put(false);
String id = structure.getStringField("id").get();
- LOGGER.info("Agrega la lista de Records... {}",id);
+
Optional<PlcItem> plcItem = generalFunction.getPlcItem(id);
if (plcItem.isPresent()) {
- plcItem.get().addItemListener((PlcItemListener) pvr);
- master.addRecord(pvr);
- LOGGER.info("Agreg un Record a la base de datos {}",
pvr.getRecordName());
- writerHandler.putDBRecord(pvr);
+ if (null == master.findRecord(pvr.getRecordName())) {
+ plcItem.get().addItemListener((PlcItemListener) pvr);
+ master.addRecord(pvr);
+ System.out.println("XXXXX> " + pvr.getOffset() );
+ writerHandler.putDBRecord(pvr);
+ LOGGER.info("Add DBRecord... [{}] linked to
[{}].",pvr.getRecordName(), id);
+ } else {
+ LOGGER.info("DBRMS DBRecord [{}] already exist.",
pvr.getRecordName());
+ }
}
});
@@ -224,7 +228,7 @@ public class DBRecordsManagedService implements
ManagedServiceFactory, Job {
try {
updated(pid,props);
} catch (ConfigurationException ex) {
- LOGGER.info("Problem updating [" + key +"] from waiting list."
);
+ LOGGER.error("Problem updating [" + key +"] from waiting
list." );
}
}
}
@@ -240,20 +244,20 @@ public class DBRecordsManagedService implements
ManagedServiceFactory, Job {
return null;
}
} catch (Exception ex) {
- LOGGER.info("getRecordFactory: " + ex.toString());
+ LOGGER.error("getRecordFactory: " + ex.toString());
}
return null;
}
private PlcDevice getDevice(String device){
try{
- String filterdriver = "(dal.device.name=" + device + ")";
+ String filterdriver = "(dal.device.key=" + device + ")";
ServiceReference[] refdrvs =
bundleContext.getAllServiceReferences(PlcDevice.class.getName(), filterdriver);
PlcDevice refDev = (PlcDevice)
bundleContext.getService(refdrvs[0]);
if (refDev == null) LOGGER.info("Device [" + device + "] don't
found");
return refDev;
} catch (Exception ex){
- LOGGER.info("getDriver: " + ex.toString());
+ LOGGER.error("getDevice: " + ex.toString());
}
return null;
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBShortFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBShortFactory.java
index dc2cedd..64c95bf 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBShortFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBShortFactory.java
@@ -54,7 +54,8 @@ public class DBShortFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvShort)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -93,13 +94,14 @@ public class DBShortFactory extends DBBaseFactory {
class DBShortRecord extends DBRecord implements PlcItemListener {
private PVShort value;
- private PVShort write_value;
- private int offset = 0;
+ private PVShort write_value;
+ private PVBoolean write_enable;
public DBShortRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = pvStructure.getShortField("value");
- write_value = pvStructure.getShortField("write_value");
+ write_value = pvStructure.getShortField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -108,12 +110,13 @@ public class DBShortFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
+ if (null != plcItem) {
if (value.get() != write_value.get()) {
- final PVBoolean isWriteEnbale =
this.getPVStructure().getBooleanField("write_enable");
- if (isWriteEnbale.get()) {
- write_value.put(value.get());
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeShort(write_value.get());
+ super.process();
}
}
}
@@ -123,7 +126,8 @@ public class DBShortFactory extends DBBaseFactory {
public void atach(final PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Short.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Short.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Short.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -141,7 +145,7 @@ public class DBShortFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUByteFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUByteFactory.java
index 3d815dd..b2e7ab3 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUByteFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUByteFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvdata.pv.PVUByte;
import org.epics.pvdata.pv.PVUByteArray;
@@ -49,7 +50,8 @@ public class DBUByteFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvUByte)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -88,13 +90,13 @@ public class DBUByteFactory extends DBBaseFactory {
private PVUByte value;
private PVUByte write_value;
-
- private int offset = 0;
+ private PVBoolean write_enable;
public DBUByteRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = (PVUByte) pvStructure.getByteField("value");
write_value = (PVUByte) pvStructure.getByteField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -103,19 +105,25 @@ public class DBUByteFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por UByte");
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
+ System.out.println("PROCESANDO BYTE: ");
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeByte(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Byte.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Byte.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Byte.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -133,7 +141,7 @@ public class DBUByteFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUIntFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUIntFactory.java
index 015b810..29ccebe 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUIntFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUIntFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvdata.pv.PVUInt;
import org.epics.pvdata.pv.PVUIntArray;
@@ -49,7 +50,8 @@ public class DBUIntFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvUInt)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -88,13 +90,13 @@ public class DBUIntFactory extends DBBaseFactory {
private PVUInt value;
private PVUInt write_value;
-
- private int offset = 0;
+ private PVBoolean write_enable;
public DBUIntRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = (PVUInt) pvStructure.getIntField("value");
- write_value = (PVUInt) pvStructure.getIntField("write_value");
+ write_value = (PVUInt) pvStructure.getIntField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -103,11 +105,15 @@ public class DBUIntFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por UInt");
- if (value.get() != write_value.get())
- write_value.put(value.get());
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeInt(write_value.get());
+ super.process();
+ }
+ }
}
}
@@ -115,7 +121,8 @@ public class DBUIntFactory extends DBBaseFactory {
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Integer.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Integer.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset,
Integer.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -132,7 +139,7 @@ public class DBUIntFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBULongFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBULongFactory.java
index 23d6566..79d841c 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBULongFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBULongFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvdata.pv.PVULong;
import org.epics.pvdata.pv.PVULongArray;
@@ -51,7 +52,8 @@ public class DBULongFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvULong)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -90,13 +92,13 @@ public class DBULongFactory extends DBBaseFactory {
private PVULong value;
private PVULong write_value;
-
- private int offset = 0;
+ private PVBoolean write_enable;
DBULongRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = (PVULong) pvStructure.getLongField("value");
- write_value = (PVULong) pvStructure.getLongField("write_value");
+ write_value = (PVULong) pvStructure.getLongField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -105,18 +107,24 @@ public class DBULongFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeLong(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Long.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Long.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Long.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -133,7 +141,7 @@ public class DBULongFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUShortFactory.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUShortFactory.java
index c0a147d..6257497 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUShortFactory.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/core/DBUShortFactory.java
@@ -28,6 +28,7 @@ import org.epics.nt.NTScalarArrayBuilder;
import org.epics.nt.NTScalarBuilder;
import org.epics.pvdata.factory.FieldFactory;
import org.epics.pvdata.pv.FieldCreate;
+import org.epics.pvdata.pv.PVBoolean;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvdata.pv.PVUShort;
import org.epics.pvdata.pv.PVUShortArray;
@@ -51,7 +52,8 @@ public class DBUShortFactory extends DBBaseFactory {
add("offset", fieldCreate.createScalar(ScalarType.pvInt)).
add("scan_time", fieldCreate.createScalar(ScalarType.pvString)).
add("scan_enable", fieldCreate.createScalar(ScalarType.pvBoolean)).
- add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_enable",
fieldCreate.createScalar(ScalarType.pvBoolean)).
+ add("write_value", fieldCreate.createScalar(ScalarType.pvUShort)).
addAlarm().
addTimeStamp().
addDisplay().
@@ -90,13 +92,13 @@ public class DBUShortFactory extends DBBaseFactory {
private PVUShort value;
private PVUShort write_value;
-
- private int offset = 0;
+ private PVBoolean write_enable;
private DBUShortRecord(String recordName,PVStructure pvStructure) {
super(recordName, pvStructure);
value = (PVUShort) pvStructure.getShortField("value");
- write_value = (PVUShort) pvStructure.getShortField("write_value");
+ write_value = (PVUShort) pvStructure.getShortField("write_value");
+ write_enable = pvStructure.getBooleanField("write_enable");
}
/**
@@ -105,19 +107,24 @@ public class DBUShortFactory extends DBBaseFactory {
*/
public void process()
{
- super.process();
- if (null != plcItem) {
- System.out.println("Paso por UShort");
- if (value.get() != write_value.get())
- write_value.put(value.get());
- }
+ if (null != plcItem) {
+ if (value.get() != write_value.get()) {
+ if (write_enable.get()) {
+ write_value.put(value.get());
+ innerWriteBuffer.clear();
+ innerWriteBuffer.writeShort(write_value.get());
+ super.process();
+ }
+ }
+ }
}
@Override
public void atach(final PlcItem plcItem) {
this.plcItem = plcItem;
offset = this.getPVStructure().getIntField("offset").get() *
Short.BYTES;
- innerBuffer = Unpooled.wrappedBuffer(plcItem.getInnerBuffer(),
offset, Short.BYTES);
+ innerBuffer = plcItem.getItemByteBuf().slice(offset, Short.BYTES);
+ innerWriteBuffer = Unpooled.copiedBuffer(innerBuffer);
}
@Override
@@ -134,7 +141,7 @@ public class DBUShortFactory extends DBBaseFactory {
@Override
public String getFieldsToMonitor() {
- return MONITOR_WRITE_FIELD;
+ return MONITOR_FIELDS;
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBPersistImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBPersistImpl.java
index 7c7503e..c80de10 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBPersistImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBPersistImpl.java
@@ -33,7 +33,11 @@ import org.apache.plc4x.merlot.api.PlcSecureBoot;
import org.apache.plc4x.merlot.db.api.DBRecord;
import org.apache.plc4x.merlot.db.api.DBRecordFactory;
import org.apache.plc4x.merlot.db.api.DBWriterHandler;
+import org.epics.pvdata.pv.PVBoolean;
+import org.epics.pvdata.pv.PVDouble;
+import org.epics.pvdata.pv.PVInt;
import org.epics.pvdata.pv.PVScalar;
+import org.epics.pvdata.pv.PVString;
import org.epics.pvdatabase.PVDatabase;
import org.epics.pvdatabase.PVRecord;
import org.osgi.framework.BundleContext;
@@ -70,13 +74,13 @@ public class DBPersistImpl implements EventHandler{
+ "Md5 TEXT)";
private static final String SQL_SELECT_PVRECORDS =
- "SELECT * FROM PvRecords WHERE PvName = ?";
+ "SELECT * FROM PvRecords";
private static final String SQL_INSERT_PVRECORDS =
- "INSERT INTO Devices(PvUuId, PvName, PvType, PvId, PvOffset,
PvDescriptor, PvScanTime, pvScanEnable,"
+ "INSERT INTO PvRecords(PvUuId, PvName, PvType, PvId, PvOffset,
PvDescriptor, PvScanTime, pvScanEnable,"
+ "PvWriteEnable, PvDisplayLimitLow, PvDisplayLimitHigh,
PvDisplayDescription, PvDisplayFormat,"
- + "PvDisplayUnits, PvControlLimitLow, PvControlLimitHigh,
PvControlMinStep, Mmd5)"
- + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
+ + "PvDisplayUnits, PvControlLimitLow, PvControlLimitHigh,
PvControlMinStep, Md5)"
+ + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
+ "ON CONFLICT(PvUuId) "
+ "DO "
+ "UPDATE SET "
@@ -85,7 +89,7 @@ public class DBPersistImpl implements EventHandler{
+ "PvId = excluded.PvId, "
+ "PvOffset = excluded.PvOffset, "
+ "PvDescriptor = excluded.PvDescriptor, "
- + "PvScanTime = excluded.PvScanRate, "
+ + "PvScanTime = excluded.PvScanTime, "
+ "PvScanEnable = excluded.PvScanEnable, "
+ "PvWriteEnable = excluded.PvWriteEnable, "
+ "PvDisplayLimitLow = excluded.PvDisplayLimitLow, "
@@ -161,13 +165,13 @@ public class DBPersistImpl implements EventHandler{
try {
store();
} catch (Exception ex){
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
}
} else if (event.getTopic().equals(PlcSecureBoot.EVENT_RESTORE)) {
try {
restore();
} catch (Exception ex){
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
}
}
}
@@ -187,13 +191,22 @@ public class DBPersistImpl implements EventHandler{
}
public void restore() throws SQLException, InvalidSyntaxException {
+ String filter = null;
if (null != dbConnection) {
+
var stmt = dbConnection.createStatement();
var rs = stmt.executeQuery(SQL_SELECT_PVRECORDS);
+
while (rs.next()) {
- ServiceReference[] refs =
bc.getServiceReferences(DBRecordFactory.class.getName(),
"(db.record.type="+rs.getString("pvType")+")");
+
+ filter = "(db.record.type="+rs.getString("pvType")+")";
+
+ ServiceReference[] refs =
bc.getServiceReferences(DBRecordFactory.class.getName(), filter);
+
if (null != refs) {
+
final DBRecordFactory recordFactory = (DBRecordFactory)
bc.getService(refs[0]);
+
PVRecord pvRecord =
recordFactory.create(rs.getString("PvName"));
pvRecord.getPVStructure().getStringField("id").put(rs.getString("PvId"));
pvRecord.getPVStructure().getStringField("descriptor").put(rs.getString("PvDescriptor"));
@@ -208,13 +221,20 @@ public class DBPersistImpl implements EventHandler{
pvRecord.getPVStructure().getDoubleField("control.limitLow").put(rs.getDouble("PvControlLimitLow"));
pvRecord.getPVStructure().getDoubleField("control.limitHigh").put(rs.getDouble("PvControlLimitHigh"));
pvRecord.getPVStructure().getDoubleField("control.minStep").put(rs.getDouble("PvControlMinStep"));
-
+
//Talk to PLC4X
+
Optional<PlcItem> plcItem =
plcGeneralFunction.getPlcItem(rs.getString("PvId"));
+
if (plcItem.isPresent()) {
- plcItem.get().addItemListener((PlcItemListener)
pvRecord);
- master.addRecord(pvRecord);
- writerHandler.putDBRecord((DBRecord) pvRecord);
+ if (null ==
master.findRecord(pvRecord.getRecordName())) {
+ plcItem.get().addItemListener((PlcItemListener)
pvRecord);
+ master.addRecord(pvRecord);
+ writerHandler.putDBRecord((DBRecord) pvRecord);
+ } else {
+ LOGGER.info("DBRecord [?] already exist.",
rs.getString("PvId"));
+ }
+
} else {
LOGGER.error("PlcItem [?] don't exist.",
rs.getString("PvId"));
}
@@ -227,25 +247,29 @@ public class DBPersistImpl implements EventHandler{
if (null != dbConnection) {
var query = dbConnection.prepareStatement(SQL_INSERT_PVRECORDS);
+ if (pvRecord.getRecordName().contains("_")) return;
+
PVScalar value = (PVScalar)
pvRecord.getPVStructure().getSubField("value");
query.setString(1, UUID.randomUUID().toString());
query.setString(2, pvRecord.getRecordName());
- query.setString(3, value.getScalar().getScalarType().name());
- query.setString(4,
pvRecord.getPVStructure().getSubField("id").toString());
- query.setString(5,
pvRecord.getPVStructure().getSubField("offset").toString());
- query.setString(6,
pvRecord.getPVStructure().getSubField("descriptor").toString());
- query.setString(7,
pvRecord.getPVStructure().getSubField("scan_time").toString());
- query.setString(8,
pvRecord.getPVStructure().getSubField("scan_enable").toString());
- query.setString(9,
pvRecord.getPVStructure().getSubField("write_enable").toString());
- query.setString(10,
pvRecord.getPVStructure().getSubField("display.limitLow").toString());
- query.setString(11,
pvRecord.getPVStructure().getSubField("display.limitHigh").toString());
- query.setString(12,
pvRecord.getPVStructure().getSubField("display.description").toString());
- query.setString(13,
pvRecord.getPVStructure().getSubField("display.format").toString());
- query.setString(14,
pvRecord.getPVStructure().getSubField("display.units").toString());
- query.setString(15,
pvRecord.getPVStructure().getSubField("control.limitLow").toString());
- query.setString(16,
pvRecord.getPVStructure().getSubField("control.limitHigh").toString());
- query.setString(17,
pvRecord.getPVStructure().getSubField("control.minStep").toString());
+ query.setString(3, value.getScalar().getScalarType().toString());
+
+ query.setString(4, ((PVString)
pvRecord.getPVStructure().getSubField("id")).get());
+ query.setString(5, Integer.toString(((PVInt)
pvRecord.getPVStructure().getSubField("offset")).get()));
+ query.setString(6, ((PVString)
pvRecord.getPVStructure().getSubField("descriptor")).get());
+ query.setString(7, ((PVString)
pvRecord.getPVStructure().getSubField("scan_time")).get());
+ query.setString(8, Boolean.toString(((PVBoolean)
pvRecord.getPVStructure().getSubField("scan_enable")).get()));
+ query.setString(9, Boolean.toString(((PVBoolean)
pvRecord.getPVStructure().getSubField("write_enable")).get()));
+ query.setString(10, Double.toString(((PVDouble)
pvRecord.getPVStructure().getSubField("display.limitLow")).get()));
+ query.setString(11, Double.toString(((PVDouble)
pvRecord.getPVStructure().getSubField("display.limitHigh")).get()));
+ query.setString(12, ((PVString)
pvRecord.getPVStructure().getSubField("display.description")).get());
+ query.setString(13, ((PVString)
pvRecord.getPVStructure().getSubField("display.format")).get());
+ query.setString(14, ((PVString)
pvRecord.getPVStructure().getSubField("display.units")).get());
+ query.setString(15, Double.toString(((PVDouble)
pvRecord.getPVStructure().getSubField("control.limitLow")).get()));
+ query.setString(16, Double.toString(((PVDouble)
pvRecord.getPVStructure().getSubField("control.limitHigh")).get()));
+ query.setString(17, Double.toString(((PVDouble)
pvRecord.getPVStructure().getSubField("control.minStep")).get()));
+ query.setString(18, "");
query.executeUpdate();
}
}
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBWriterHandlerImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBWriterHandlerImpl.java
index 60b8bd8..4bc4ce1 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBWriterHandlerImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/java/org/apache/plc4x/merlot/db/impl/DBWriterHandlerImpl.java
@@ -18,6 +18,7 @@
*/
package org.apache.plc4x.merlot.db.impl;
+import io.netty.buffer.ByteBufUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -64,7 +65,8 @@ public class DBWriterHandlerImpl implements DBWriterHandler {
structure = element.getPVStructure();
changedBitSet = element.getChangedBitSet();
overrunBitSet = element.getOverrunBitSet();
-
+ System.out.println("La estructura \r\n:" + structure.toString());
+
if ((recordMonitors.containsKey(monitor)) &&
structure.getBooleanField("write_enable").get()) {
if (changedBitSet.get(1) &&
@@ -72,14 +74,19 @@ public class DBWriterHandlerImpl implements DBWriterHandler
{
overrunBitSet.isEmpty()) {
final DBRecord dbRecord = recordMonitors.get(monitor);
- final Optional<PlcItem> refPlcItem =
dbRecord.getPlcItem();
-
-
refPlcItem.get().itemWrite(dbRecord.getWriteBuffer().get(),
dbRecord.getOffset());
+ final Optional<PlcItem> optPlcItem = dbRecord.getPlcItem();
+
LOGGER.info(ByteBufUtil.prettyHexDump(dbRecord.getWriteBuffer().get()));
+
+ if (optPlcItem.isPresent()) {
+ System.out.println("Aqui el Offset: " +
dbRecord.getOffset());
+
optPlcItem.get().itemWrite(dbRecord.getWriteBuffer().get(),
dbRecord.getOffset());
+ }
}
}
} catch (Exception ex) {
- LOGGER.info(ex.getMessage());
+ LOGGER.error(ex.getMessage());
+ ex.printStackTrace();
} finally {
monitor.release(element);
}
@@ -103,11 +110,16 @@ public class DBWriterHandlerImpl implements
DBWriterHandler {
@Override
public void putDBRecord(DBRecord dbRecord) {
- LOGGER.debug("Monitor with fields = {}",
dbRecord.getFieldsToMonitor());
+ LOGGER.info("Monitor with fields = {}",
dbRecord.getFieldsToMonitor());
+ System.out.println(">>>> el OFFSET aqui: " + dbRecord.getOffset());
PVStructure request =
createRequest.createRequest(dbRecord.getFieldsToMonitor());
Monitor monitor = MonitorFactory.create(dbRecord, this, request);
- recordMonitors.put(monitor, dbRecord);
- monitor.start();
+ if (null != monitor) {
+ recordMonitors.put(monitor, dbRecord);
+ monitor.start();
+ } else {
+ LOGGER.error("The monitor is 'null' for [{}]",
dbRecord.getRecordName());
+ }
}
@Override
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/resources/OSGI-INF/blueprint/db-service.xml
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/resources/OSGI-INF/blueprint/db-service.xml
index 22daaf2..d8c91c8 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/resources/OSGI-INF/blueprint/db-service.xml
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.db/src/main/resources/OSGI-INF/blueprint/db-service.xml
@@ -19,7 +19,7 @@
<reference
id="refPlcGeneralFunction"
- interface="org.apache.plc4x.merlot.api.PlcGeneralFunction"
availability="mandatory" timeout="120000"/>
+ interface="org.apache.plc4x.merlot.api.PlcGeneralFunction"
availability="mandatory" timeout="1200"/>
@@ -87,10 +87,20 @@
<bean id="DBPersistBean"
class="org.apache.plc4x.merlot.db.impl.DBPersistImpl"
+ init-method="init"
scope="singleton">
<argument ref="blueprintBundleContext"/>
- <argument ref="DBBean"/>
- </bean>
+ <argument ref="DBBean"/>
+ <argument ref="refPlcGeneralFunction"/>
+ <argument ref="DBWriterHandlerImpl"/>
+ </bean>
+
+ <reference id="servDataSourceFactory"
+ interface="org.osgi.service.jdbc.DataSourceFactory"
+ availability="optional"
+ filter="(osgi.jdbc.driver.name=sqlite)">
+ <reference-listener bind-method="bindDataSourceFactory"
ref="DBPersistBean"/>
+ </reference>
<reference-list id="refJaasRealmService"
interface="org.apache.karaf.jaas.config.JaasRealm"
@@ -245,7 +255,7 @@
<service ref="DBPersistBean" auto-export="interfaces">
<service-properties>
- <entry key="event.filter" value="org/apache/plc4x/merlot/*"/>
+ <entry key="event.topics" value="org/apache/plc4x/merlot/*"/>
</service-properties>
</service>
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.mb/src/main/java/org/apache/plc4x/merlot/drv/mb/impl/ModbusPlcTagFunctionImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.mb/src/main/java/org/apache/plc4x/merlot/drv/mb/impl/ModbusPlcTagFunctionImpl.java
index 76a7206..29fa6b1 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.mb/src/main/java/org/apache/plc4x/merlot/drv/mb/impl/ModbusPlcTagFunctionImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.mb/src/main/java/org/apache/plc4x/merlot/drv/mb/impl/ModbusPlcTagFunctionImpl.java
@@ -17,6 +17,8 @@
package org.apache.plc4x.merlot.drv.mb.impl;
import io.netty.buffer.ByteBuf;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.plc4x.java.api.model.PlcTag;
import static org.apache.plc4x.java.api.types.PlcValueType.BOOL;
@@ -27,6 +29,7 @@ import
org.apache.plc4x.java.modbus.base.tag.ModbusTagDiscreteInput;
import org.apache.plc4x.java.modbus.base.tag.ModbusTagExtendedRegister;
import org.apache.plc4x.java.modbus.base.tag.ModbusTagHoldingRegister;
import org.apache.plc4x.java.modbus.base.tag.ModbusTagInputRegister;
+import org.apache.plc4x.java.modbus.readwrite.ModbusDataType;
import org.apache.plc4x.merlot.api.PlcTagFunction;
import org.osgi.framework.BundleContext;
import org.osgi.service.dal.OperationMetadata;
@@ -38,7 +41,7 @@ import org.slf4j.LoggerFactory;
public class ModbusPlcTagFunctionImpl implements PlcTagFunction {
private static final Logger LOGGER =
LoggerFactory.getLogger(ModbusPlcTagFunctionImpl.class);
private BundleContext bc;
-
+ Map<String, String> config = new HashMap<>();
int byteOffset = 0;
int bitOffset = 0;
@@ -46,6 +49,16 @@ public class ModbusPlcTagFunctionImpl implements
PlcTagFunction {
this.bc = bc;
}
+ /*
+ * MODBUS is a protocol oriented to data stored in bits or words,
+ * therefore the handling of individual bytes is considered a special case.
+ * Byte reading is accomplished with the offset over the read buffer,
+ * but a byte write is rejected since the process would overwrite its
+ * partner byte.
+ * The user can read individual bytes, but must mask the write in a
+ * "short" for the write.
+ *
+ */
@Override
public ImmutablePair<PlcTag, Object[]> getPlcTag(PlcTag plcTag, ByteBuf
byteBuf, int offset) {
LOGGER.info("PlcTag class {} and type {} ", plcTag.getClass(),
plcTag.getPlcValueType());
@@ -54,7 +67,8 @@ public class ModbusPlcTagFunctionImpl implements
PlcTagFunction {
if (plcTag instanceof ModbusTag){
final ModbusTag mbTag = (ModbusTag) plcTag;
LOGGER.info("Processing ModbusTag: {}", mbTag.toString());
- Object[] objValues = new Object[byteBuf.capacity()];
+ Object[] objValues = new Object[byteBuf.capacity() / 2];
+ LOGGER.info("Borrar: pasa a verificar el tipo...");
switch (mbTag.getPlcValueType()) {
case BOOL:
byteOffset = mbTag.getAddress() + offset;
@@ -63,45 +77,56 @@ public class ModbusPlcTagFunctionImpl implements
PlcTagFunction {
byteOffset,
byteBuf.capacity(),
mbTag.getDataType(),
- null);
+ config);
} else if (mbTag instanceof ModbusTagDiscreteInput) {
mbPlcTag = new ModbusTagCoil(
byteOffset,
byteBuf.capacity(),
mbTag.getDataType(),
- null);
+ config);
}
byteBuf.resetReaderIndex();
for (int i=0; i < byteBuf.capacity(); i++) {
objValues[i] = byteBuf.readBoolean();
}
break;
- case BYTE:
- byteOffset = mbTag.getAddress() + offset * 2;
+ case UINT:
+ System.out.println("Paso por aqui 1");
+ if (byteBuf.capacity() == 1) {
+ LOGGER.info("In MODBUS writing 'byte' types is
rejected.");
+ return null;
+ }
+ System.out.println("Direccion del Tag: " +
mbTag.getAddress() );
+ System.out.println("Valor Offset: " + offset );
+ byteOffset = mbTag.getAddress() + offset / 2;
if (mbTag instanceof ModbusTagHoldingRegister) {
+ System.out.println("Paso por aqui 2");
mbPlcTag = new ModbusTagHoldingRegister(
byteOffset,
- byteBuf.capacity(),
- mbTag.getDataType(),
- null);
+ byteBuf.capacity() / 2,
+ ModbusDataType.INT,
+ config);
+ System.out.println("Paso por aqui 2.9: " +
mbPlcTag.toString());
} else if (mbTag instanceof ModbusTagInputRegister){
mbPlcTag = new ModbusTagInputRegister(
byteOffset,
- byteBuf.capacity(),
- mbTag.getDataType(),
- null);
+ byteBuf.capacity() / 2,
+ ModbusDataType.INT,
+ config);
} else if (mbTag instanceof
ModbusTagExtendedRegister) {
mbPlcTag = new ModbusTagExtendedRegister(
byteOffset,
- byteBuf.capacity(),
- mbTag.getDataType(),
- null);
+ byteBuf.capacity() / 2,
+ ModbusDataType.INT,
+ config);
}
+ System.out.println("Paso por aqui 3");
byteBuf.resetReaderIndex();
- for (int i=0; i < byteBuf.capacity(); i++){
- tempValue = (short) (byteBuf.readByte() & 0xFF);
+ for (int i=0; i < byteBuf.capacity() / 2; i++){
+ tempValue = (short) (byteBuf.readShort());
objValues[i] = tempValue;
- }
+ }
+ System.out.println("Paso por aqui 4");
break;
default:;
diff --git
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.simulated/src/main/java/org/apache/plc4x/merlot/drv/simulated/impl/SimulatedPlcTagFunctionImpl.java
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.simulated/src/main/java/org/apache/plc4x/merlot/drv/simulated/impl/SimulatedPlcTagFunctionImpl.java
index 940f684..325e085 100644
---
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.simulated/src/main/java/org/apache/plc4x/merlot/drv/simulated/impl/SimulatedPlcTagFunctionImpl.java
+++
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.simulated/src/main/java/org/apache/plc4x/merlot/drv/simulated/impl/SimulatedPlcTagFunctionImpl.java
@@ -64,10 +64,10 @@ public class SimulatedPlcTagFunctionImpl implements
PlcTagFunction {
objValues[i] = byteBuf.readBoolean();
}
break;
- case BYTE:
+ case SINT:
strTagBuilder.append("STDOUT/").
append("merlot").
- append(":BYTE[").
+ append(":SINT[").
append(byteBuf.capacity()).
append("]");
byteBuf.resetReaderIndex();
@@ -110,10 +110,10 @@ public class SimulatedPlcTagFunctionImpl implements
PlcTagFunction {
objValues[i] = byteBuf.readBoolean();
}
break;
- case BYTE:
+ case SINT:
strTagBuilder.append("STDOUT/").
append("merlot").
- append(":BYTE[").
+ append(":SINT[").
append(byteBuf.capacity()).
append("]");
simPlcTag = SimulatedTag.of(strTagBuilder.toString());
diff --git a/plc4j/tools/merlot/pom.xml b/plc4j/tools/merlot/pom.xml
index e95c4fd..5792f64 100644
--- a/plc4j/tools/merlot/pom.xml
+++ b/plc4j/tools/merlot/pom.xml
@@ -142,7 +142,6 @@ under the License.
<module>org.apache.plc4x.merlot.h2</module>
<module>org.apache.plc4x.merlot.drv.simulated</module>
<module>org.apache.plc4x.merlot.das.ref</module>
- <module>org.apache.plc4x.merlot.das.base</module>
<module>org.apache.plc4x.merlot.grpc</module>
<!-- <module>org.apache.plc4x.merlot.ui</module> -->
<module>org.apache.plc4x.merlot</module>