You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cg...@apache.org on 2024/10/02 21:11:18 UTC

(plc4x-extras) branch feature/merlot updated: Fix general function.

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 86d0644  Fix general function.
86d0644 is described below

commit 86d0644b9b4ed60a227d5e1deac984ae07664054
Author: Cesar Garcia <ce...@ceos.com.ve>
AuthorDate: Wed Oct 2 17:11:04 2024 -0400

    Fix general function.
---
 .../plc4x/merlot/api/command/DemoCommand002.java   |   2 +-
 .../merlot/api/impl/PlcGeneralFunctionImpl.java    |  10 +-
 .../plc4x/merlot/api/impl/PlcSecureBootImpl.java   | 109 ++++++++++++++++-----
 .../resources/OSGI-INF/blueprint/api-service.xml   |   1 +
 plc4j/tools/merlot/pom.xml                         |   2 +-
 5 files changed, 94 insertions(+), 30 deletions(-)

diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand002.java b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand002.java
index 0c18caa..0ed911e 100644
--- a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand002.java
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/DemoCommand002.java
@@ -71,7 +71,7 @@ public class DemoCommand002  implements Action  {
                     
                         item = new PlcItemImpl.PlcItemBuilder("ITEM_00" + i).
                                     setItemDescription("THE ITEM 00" + i).
-                                    setItemId("4x00001:UINT[100]").
+                                    setItemId("RANDOM/dummy:UINT[100]").
                                     setItemEnable(true).
                                     build(); 
                         
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 c9a8f5e..03e689a 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
@@ -461,6 +461,7 @@ public class PlcGeneralFunctionImpl implements PlcGeneralFunction  {
         
         try {
             String filter = FILTER_DEVICE_UID.replace("*", group_uid.toString());
+            
             ServiceReference ref = bc.getServiceReference(filter);
             if (null != ref) {
                 final PlcDevice device = (PlcDevice) bc.getService(ref);
@@ -515,14 +516,19 @@ public class PlcGeneralFunctionImpl implements PlcGeneralFunction  {
         Map<UUID, String> plcgroups = new HashMap<>();
         
         try {
-            String filter = FILTER_DEVICE_GROUP.replace("*", device_uid.toString());
-            ServiceReference[] refs = bc.getServiceReferences((String) null, filter);
+            //String filter = FILTER_DEVICE_GROUP.replace("*", device_uid.toString());
+            String filter = "(DEVICE_UID = " + device_uid.toString() + ")";
+            ServiceReference[] refs = bc.getServiceReferences(PlcGroup.class.getName(), (String) null);
+            System.out.println("X Filtro: " + filter);
+            System.out.println("X Numero de referencias ");
             if (null != refs) {
                 for (ServiceReference ref:refs){
                     final PlcGroup group = (PlcGroup) bc.getService(ref);
                     plcgroups.put(group.getGroupUid(),group.getGroupName());
                 }
+                System.out.println("X Salido por aqui #1");
             }
+            System.out.println("X Salido por aqui #2");
         } catch (InvalidSyntaxException ex) {
             LOGGER.info(ex.getMessage());
         } finally {
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 83261fd..1e3dcf5 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
@@ -27,9 +27,14 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.sql.DataSource;
 import org.apache.plc4x.java.api.PlcDriver;
+import org.apache.plc4x.merlot.api.PlcDevice;
 import org.apache.plc4x.merlot.api.PlcGeneralFunction;
+import org.apache.plc4x.merlot.api.PlcGroup;
+import org.apache.plc4x.merlot.api.PlcItem;
 import org.apache.plc4x.merlot.api.PlcSecureBoot;
 import org.apache.plc4x.merlot.scheduler.api.Job;
 import org.apache.plc4x.merlot.scheduler.api.JobContext;
@@ -49,7 +54,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
         
     private static final String SQL_CREATE_TABLE_DEVICES = 
             "CREATE TABLE IF NOT EXISTS Devices("
-            + "DeviceUuId TEXT,"
+            + "DeviceUuId TEXT NOT NULL PRIMARY KEY,"
             + "DriverName TEXT,"            
             + "DeviceName TEXT,"
             + "DeviceId TEXT,"
@@ -59,7 +64,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     
     private static final String SQL_CREATE_TABLE_GROUPS = 
             "CREATE TABLE IF NOT EXISTS Groups("
-            + "GroupUuid TEXT,"
+            + "GroupUuid TEXT NOT NULL PRIMARY KEY,"
             + "DeviceUuid TEXT,"            
             + "GroupName TEXT,"
             + "GroupDescripcion TEXT,"
@@ -68,7 +73,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     
     private static final String SQL_CREATE_TABLE_ITEMS = 
             "CREATE TABLE IF NOT EXISTS Items("
-            + "ItemUuid TEXT,"
+            + "ItemUuid TEXT NOT NULL PRIMARY KEY,"
             + "DeviceUuid TEXT,"
             + "GroupUuid TEXT,"
             + "ItemName TEXT,"
@@ -85,7 +90,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     private static final String SQL_SELECT_ITEMS = 
             "SELECT * FROM  WHERE GroupId = ?";     
     
-    private static final String SQL_INSERT_DEVICES = 
+    private static final String SQL_INSERT_DEVICE = 
             "INSERT INTO Devices(DeviceUuId, DriverName, DeviceName, DeviceId, ShortName, Description, Md5)"
             + "VALUES(?, ?, ?, ?, ?, ?, ?) "
             + "ON CONFLICT(DeviceUuId) "
@@ -99,10 +104,10 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
             + "Md5 = excluded.Md5;";
               
     
-    private static final String SQL_INSERT_GROUPS = 
-            "INSERT INTO Devices(GroupUuid, DeviceUuid, GroupName, GroupDescripcion, GroupScantime, Md5)"
+    private static final String SQL_INSERT_GROUP = 
+            "INSERT INTO Groups(GroupUuid, DeviceUuid, GroupName, GroupDescripcion, GroupScantime, Md5)"
             + "VALUES(?, ?, ?, ?, ?, ?) "
-            + "ON CONFLICT(DeviceUuId) "
+            + "ON CONFLICT(GroupUuid) "
             + "DO "
             + "UPDATE SET "
             + "GroupUuid  =     excluded.GroupUuid, "
@@ -112,10 +117,10 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
             + "GroupScantime    = excluded.GroupScantime, "
             + "Md5 = excluded.Md5;";
     
-    private static final String SQL_INSERT_ITEMS = 
-            "INSERT INTO Devices(ItemUuid, DeviceUuid, GroupUuid, ItemName, ItemDescripcion, ItemTag, Md5)"
+    private static final String SQL_INSERT_ITEM = 
+            "INSERT INTO Items(ItemUuid, DeviceUuid, GroupUuid, ItemName, ItemDescripcion, ItemTag, Md5)"
             + "VALUES(?, ?, ?, ?, ?, ?, ?) "
-            + "ON CONFLICT(DeviceUuId) "
+            + "ON CONFLICT(ItemUuid) "
             + "DO "
             + "UPDATE SET "
             + "ItemUuid = excluded.ItemUuid, "
@@ -201,7 +206,6 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
         init();
     }  
     
-   
     @Override
     public void execute(JobContext context) {
         if (null != dbConnection) {
@@ -231,19 +235,33 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
     public void store(String plcDriver) {
         var plcDevices = plcGeneralFunction.getPlcDevices(plcDriver);
         plcDevices.forEach((duid, dname) ->{
-            var plcDevice = plcGeneralFunction.getPlcDevice(duid);
-            //Store Device
-            var plcGroups = plcGeneralFunction.getPlcDeviceGroups(duid);
-            plcGroups.forEach((guid, gname) -> {
-                var plcGroup = plcGeneralFunction.getPlcGroup(guid);
-                //Store Group
-                var plcItems = plcGeneralFunction.getPlcGroupItems(guid);
-                plcItems.forEach((iuid, iname) -> {
-                    var plcItem = plcGeneralFunction.getPlcItem(iuid);
-                    //Store item
-                    
+            try {
+                var plcDevice = plcGeneralFunction.getPlcDevice(duid);
+                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 {
+                        insertGroup(plcGroup);
+                    } catch (SQLException ex) {
+                        LOGGER.info(ex.getMessage());
+                    }
+                    var plcItems = plcGeneralFunction.getPlcGroupItems(guid);
+                    plcItems.forEach((iuid, iname) -> {
+                        var plcItem = plcGeneralFunction.getPlcItem(iuid);
+                        try {
+                            //Store item
+                            insertItem(duid.toString(), guid.toString(), plcItem.get());
+                        } catch (SQLException ex) {
+                            LOGGER.info(ex.getMessage());
+                        }
+                    });
                 });
-            });
+            } catch (SQLException ex) {
+                LOGGER.info(ex.getMessage());
+            }
         
         });
     }
@@ -258,9 +276,7 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
             eventAdmin.sendEvent(eventPersist);            
         }        
     }
-    
-    
-    
+        
     private void createTables() throws SQLException{
         Statement statement;
         statement = dbConnection.createStatement();
@@ -270,4 +286,45 @@ public class PlcSecureBootImpl implements PlcSecureBoot, Job {
         statement.execute(SQL_CREATE_TABLE_ITEMS);                 
     }
     
+    private void insertDevice(String driverName, PlcDevice plcDevice) throws SQLException{
+        if (null != dbConnection) {
+            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(6, plcDevice.getDeviceDescription());    
+            query.setString(7,"");
+            query.executeUpdate();
+        }
+    }
+    
+    private void insertGroup(PlcGroup plcGroup) throws SQLException{
+        if (null != dbConnection) {
+            var query = dbConnection.prepareStatement(SQL_INSERT_GROUP);
+            query.setString(1, plcGroup.getGroupUid().toString());
+            query.setString(2, plcGroup.getGroupDeviceUid().toString());             
+            query.setString(3, plcGroup.getGroupName());   
+            query.setString(4, plcGroup.getGroupDescription()); 
+            query.setString(5, Long.toOctalString(plcGroup.getPeriod()));  
+            query.setString(6, "");    
+            query.executeUpdate();
+        }
+    }  
+    
+    private void insertItem(String uuidDevice, String uuidGroup, PlcItem plcItem) throws SQLException{
+        if (null != dbConnection) {
+            var query = dbConnection.prepareStatement(SQL_INSERT_ITEM);
+            query.setString(1, plcItem.getItemUid().toString());
+            query.setString(2, uuidDevice);             
+            query.setString(3, uuidGroup);   
+            query.setString(4, plcItem.getItemName()); 
+            query.setString(5, plcItem.getItemDescription());  
+            query.setString(5, plcItem.getItemId());             
+            query.setString(6, "");    
+            query.executeUpdate();
+        }
+    }      
+    
 }
diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/resources/OSGI-INF/blueprint/api-service.xml b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/resources/OSGI-INF/blueprint/api-service.xml
index 9cb84a8..5ca2c66 100644
--- a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/resources/OSGI-INF/blueprint/api-service.xml
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/resources/OSGI-INF/blueprint/api-service.xml
@@ -127,6 +127,7 @@
             init-method = "init"
             destroy-method = "destroy"            
             activation="eager">
+            <argument ref="blueprintBundleContext" />        
             <argument ref="beanPlcGeneralFunction" />        
     </bean>    
            
diff --git a/plc4j/tools/merlot/pom.xml b/plc4j/tools/merlot/pom.xml
index edac304..afa10de 100644
--- a/plc4j/tools/merlot/pom.xml
+++ b/plc4j/tools/merlot/pom.xml
@@ -51,7 +51,7 @@ under the License.
     <quartz.version>2.3.2</quartz.version>
     <slf4j.version>2.0.3</slf4j.version>
     <spifly.version>1.3.6</spifly.version>
-    <jackson.version>2.17.2</jackson.version>
+    <jackson.version>2.18.0</jackson.version>
     <jakarta.xml.bind.version>2.3.3</jakarta.xml.bind.version>
     <parser-ng.version>0.1.8</parser-ng.version>
     <vavr.version>0.10.4</vavr.version>