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 1572f88  Fix s7 model. New Model command. working on.
1572f88 is described below

commit 1572f889e31905e59e5abb7e8483939c603cde25
Author: César García <cesar.gar...@ceos.com.ve>
AuthorDate: Thu Jun 26 12:21:54 2025 -0400

    Fix s7 model. New Model command. working on.
---
 .../plc4x/merlot/api/command/PlcModelCommand.java  | 122 +++++++++++
 .../plc4x/merlot/api/impl/PlcDeviceImpl.java       |   2 +-
 .../merlot/api/impl/PlcGeneralFunctionImpl.java    |  20 +-
 .../apache/plc4x/merlot/api/impl/PlcGroupImpl.java |   1 +
 .../apache/plc4x/merlot/db/impl/DBPersistImpl.java |   1 +
 .../org.apache.plc4x.merlot.drv.s7/nbactions.xml   |   2 +-
 .../plc4x/merlot/drv/s7/impl/S7PlcModelImpl.java   | 225 +++++++++++++--------
 7 files changed, 277 insertions(+), 96 deletions(-)

diff --git 
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcModelCommand.java
 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcModelCommand.java
new file mode 100644
index 0000000..a022ddd
--- /dev/null
+++ 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/command/PlcModelCommand.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.plc4x.merlot.api.command;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import java.util.List;
+import java.util.Optional;
+import org.apache.karaf.shell.api.action.Action;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.support.table.ShellTable;
+import org.apache.plc4x.merlot.api.PlcGeneralFunction;
+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.PlcModel;
+import org.apache.plc4x.merlot.api.core.PlcItemClientService;
+
+
+@Command(scope = "plc4x", name = "model", description = "Command to display 
information about a device's model.")
+@Service
+public class PlcModelCommand  implements Action {
+  
+    @Reference
+    BundleContext bc;  
+    
+    @Reference
+    PlcGeneralFunction gf;      
+    
+    @Reference
+    PlcItemClientService clients;
+    
+    @Reference
+    volatile List<PlcGroup> groups;
+    
+    @Option(name = "-d", aliases = "--dump", description = "Group uid.", 
required = true, multiValued = false)
+    Boolean d = false; 
+    
+    @Argument(index = 0, name = "name", description = "PlcModel dedvice name", 
required = true, multiValued = false)
+    String device_name = null;   
+    
+    @Argument(index = 1, name = "name", description = "PlcModel area name", 
required = false, multiValued = false)
+    String area_name = null;   
+
+    @Argument(index = 2, name = "name", description = "PlcModel area index", 
required = false, multiValued = false)
+    Integer index = -1;       
+   
+
+    @Override
+    public Object execute() throws Exception {
+        Optional<PlcModel> optPlcModel = gf.getPlcModel(null, device_name);
+        if (optPlcModel.isPresent()) {
+            final PlcModel plcModel = optPlcModel.get();     
+            if ((null != device_name) && (null == area_name) && (index == -1)) 
{
+                PrintMemoryAreas(plcModel);
+            } else if ((null != device_name) && (null != area_name) && (index 
== -1)) {
+                PrintMemoryIndex(plcModel, "DB");                
+            } else if ((null != device_name) && (null != area_name) && (index 
!= -1)) {
+                PrintMemoryByteBuf(plcModel, "DB", 100);                
+            } 
+        } else {
+            System.out.println("PlcModel not present.");
+        }
+        return null;
+    }
+
+
+    private void PrintMemoryAreas(PlcModel plcModel){
+        ShellTable table = new ShellTable();
+        table.column("Area");
+        table.column("Amount");           
+        plcModel.listMemoryAreas().stream().
+                forEach(s -> { 
+                    table.addRow().addContent(s, 
plcModel.getMemoryAreaSegmentCount(s).toString());
+                });
+        
+    }
+
+    private void PrintMemoryIndex(PlcModel plcModel, String strMemoryArea){
+        ShellTable table = new ShellTable();
+        table.column("Area");
+        table.column("Amount");          
+        plcModel.getMemoryAreaSegmentIds(strMemoryArea).stream().
+                forEach(i -> {
+                    System.out.println(i);
+                    });
+        
+    }  
+    
+    private void PrintMemoryByteBuf(PlcModel plcModel, String strMemoryArea, 
Integer index){
+        Optional<PlcItem> optPlcItem = 
plcModel.getMemoryAreaPlcItem(strMemoryArea, index);
+        if (optPlcItem.isPresent()) {
+            final PlcItem plcItem = optPlcItem.get();
+            final ByteBuf byteBuf = plcItem.getItemByteBuf();
+            System.out.println("----");
+            System.out.println(ByteBufUtil.prettyHexDump(byteBuf));
+        } else {
+            System.out.println("Memory area not present.");            
+        }
+        
+    }    
+    
+    
+}
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 cbc6b91..0b2117a 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
@@ -444,7 +444,7 @@ public class PlcDeviceImpl implements PlcDevice {
 
     @Override
     public void putGroup(PlcGroup group) {
-        if ((!enable) && (!deviceGroups.containsKey(group.getGroupUid()))) {
+        if ((!deviceGroups.containsKey(group.getGroupUid()))) {
                 group.setGroupDeviceUid(UUID.fromString((String) 
deviceProperties.get(PlcDevice.SERVICE_UID)));
                 group.setPlcConnection(refPlcConnection);
                 group.setReadRingBuffer(readRingBuffer);
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 ef24de7..59751dc 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
@@ -496,7 +496,7 @@ public class PlcGeneralFunctionImpl implements 
PlcGeneralFunction  {
     public Optional<PlcGroup> createGroup(String GroupUuid, String DeviceUuid, 
             String GroupName, String GroupDescription, 
             String GroupScanTime, String GroupEnable) {
-        
+        boolean enable = GroupEnable.equalsIgnoreCase("true");
         try {
             final PlcDevice plcDevice = 
getPlcDevice(UUID.fromString(DeviceUuid));
 
@@ -518,12 +518,11 @@ public class PlcGeneralFunctionImpl implements 
PlcGeneralFunction  {
                 PlcGroup plcGroup = new PlcGroupImpl.PlcGroupBuilder(bc, 
GroupName, UUID.fromString(GroupUuid)).
                                             
setGroupPeriod(Long.parseLong(GroupScanTime)).
                                             
setGroupDeviceUid(plcDevice.getUid()).
-                                            
setGroupDescription(GroupDescription).                        
+                                            
setGroupDescription(GroupDescription).
+                                            setGroupEnable(enable).
                                             build(); 
-                if (GroupEnable.equals("true")){
-                    plcGroup.enable();
-                } else plcGroup.disable();
-                    plcDevice.putGroup(plcGroup);
+                //The Device register the group like OSGi service 
+                plcDevice.putGroup(plcGroup);               
                 return Optional.of(plcGroup);
             } else {
                 LOGGER.info("Device don´t exists");                
@@ -709,9 +708,16 @@ public class PlcGeneralFunctionImpl implements 
PlcGeneralFunction  {
     @Override
     public Optional<PlcModel> getPlcModel(String deviceCategory, String 
deviceName) {
         try {
-            String filter = FILTER_DEVICE_MODEL.
+            String filter = null;
+            
+            if (null != deviceCategory) {
+                filter = FILTER_DEVICE_MODEL.
                     replace("*", deviceCategory).
                     replace("?", deviceName);
+            } else {
+                filter = FILTER_DEVICE_MODEL.
+                    replace("?", deviceName);                
+            }
             ServiceReference[] refs = bc.getServiceReferences((String) null, 
filter);  
             if (null != refs) {
                 final PlcModel plcModel = (PlcModel) bc.getService(refs[0]);
diff --git 
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGroupImpl.java
 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGroupImpl.java
index 2378189..4037251 100644
--- 
a/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGroupImpl.java
+++ 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.das.api/src/main/java/org/apache/plc4x/merlot/api/impl/PlcGroupImpl.java
@@ -96,6 +96,7 @@ public class PlcGroupImpl implements PlcGroup, Job {
         groupProperties.put(PlcGroup.GROUP_CONCURRENT, false);        
         groupProperties.put(PlcGroup.GROUP_IMMEDIATE, true);
         groupProperties.put(PlcGroup.GROUP_PERIOD, builder.groupPeriod);
+        this.enable = builder.group_enable;
         
     }
         
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 addd086..454ad9d 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
@@ -258,6 +258,7 @@ public class DBPersistImpl implements EventHandler {
                         if (optPlcModel.isPresent()) {
                             optPlcModel.get().createScanGroup(pvRecord);
                             optPlcModel.get().createMemoryArea(pvRecord);
+                            master.addRecord(pvRecord);
                         } else {
                             LOGGER.info("PlcModel {} is not present.", 
strFields[0]);
                         }                                                      
                  
diff --git a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/nbactions.xml 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/nbactions.xml
index adfb485..a9b81c7 100644
--- a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/nbactions.xml
+++ b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/nbactions.xml
@@ -9,7 +9,7 @@
                 <goal>install</goal>
             </goals>
             <properties>
-                <skipTests></skipTests>
+                <skipTests>true</skipTests>
             </properties>
         </action>
     </actions>
diff --git 
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/src/main/java/org/apache/plc4x/merlot/drv/s7/impl/S7PlcModelImpl.java
 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/src/main/java/org/apache/plc4x/merlot/drv/s7/impl/S7PlcModelImpl.java
index 1ee5566..aebec6b 100644
--- 
a/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/src/main/java/org/apache/plc4x/merlot/drv/s7/impl/S7PlcModelImpl.java
+++ 
b/plc4j/tools/merlot/org.apache.plc4x.merlot.drv.s7/src/main/java/org/apache/plc4x/merlot/drv/s7/impl/S7PlcModelImpl.java
@@ -1,4 +1,4 @@
-/*
+    /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
@@ -17,6 +17,7 @@
 package org.apache.plc4x.merlot.drv.s7.impl;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -66,7 +67,7 @@ public class S7PlcModelImpl implements PlcModel {
     private Map<UUID, PlcGroup> scanGroups = new HashMap<UUID, PlcGroup>();
     
     //Individual Items for a memory area.
-    private Map<PlcItem, List<Pair<PlcGroup,PlcItem>>> scanItems = new 
HashMap<PlcItem, List<Pair<PlcGroup,PlcItem>>>();    
+    private Map<PlcItem, List<PlcGroup>> scanItems = new HashMap<PlcItem, 
List<PlcGroup>>();    
     
     public S7PlcModelImpl(BundleContext bc, PlcGeneralFunction gf) {
         this.bc = bc;
@@ -108,16 +109,17 @@ public class S7PlcModelImpl implements PlcModel {
 
         String[] strTemp = pvId.split(":", 2);
         String strTag = strTemp[1];
-        
+        System.out.println("Paso: 01");
         //TODO: Split the Device name.
         S7Tag s7tag = S7Tag.of(strTag);
         
         if (null == memoryAreas.get(s7tag.getMemoryArea().getShortName())) {
             Map<Integer, PlcItem> inputBytes = new HashMap<Integer, PlcItem>();
             memoryAreas.put(s7tag.getMemoryArea().getShortName(), inputBytes);
-            logger.info("Created memmory area with PlcItem: " + "s7" + 
s7tag.getMemoryArea().getShortName() + "["+s7tag.getBlockNumber() +"]");
+            logger.info("Created memmory area with PlcItem: " + "s7 " + 
s7tag.getMemoryArea().getShortName() + "["+s7tag.getBlockNumber() +"]");
         }
-
+        
+        System.out.println("Paso: 02");
         final Map<Integer, PlcItem> memoryBytes = 
memoryAreas.get(s7tag.getMemoryArea().getShortName());          
         
         if (null == memoryBytes.get(s7tag.getBlockNumber())) {
@@ -125,19 +127,28 @@ public class S7PlcModelImpl implements PlcModel {
                 setItemDescription("Flag markes from PLC in byte order.").
                 setItemId("").
                 setItemEnable(true).
-                build();
+                build();                   
             memoryBytes.put(s7tag.getBlockNumber(), plcItem);
         }
         
+        System.out.println("Paso: 03");
         final PlcItem internalPlcItem = 
memoryBytes.get(s7tag.getBlockNumber());        
         final ByteBuf byteBuf = internalPlcItem.getItemByteBuf();
         int bufferSize = 
(dbrecord.getInnerBuffer().isPresent())?dbrecord.getInnerBuffer().get().capacity():1;
         int minSize =   s7tag.getByteOffset() + bufferSize;
 
+        System.out.println("Paso: 04");
+        
         if (byteBuf.capacity() < minSize) {
             byteBuf.capacity(minSize);
             logger.info("The buffer capacity was expanded to {}.", minSize);
-        }                   
+        } 
+        byteBuf.writerIndex(byteBuf.capacity());
+        System.out.println(ByteBufUtil.prettyHexDump(byteBuf));
+        
+                System.out.println("Paso: 05");
+        doUpdateByteBuf(dbrecord);
+                System.out.println("Paso: 06");
     }
 
     @Override
@@ -158,6 +169,7 @@ public class S7PlcModelImpl implements PlcModel {
         }
         
         if (null != plcDevice) {
+            
             Optional<Entry<UUID, PlcGroup>> optEntry = scanGroups.
                     entrySet().
                     stream().
@@ -168,19 +180,20 @@ public class S7PlcModelImpl implements PlcModel {
                 Optional<PlcGroup> optPlcGroup = 
gf.createGroup(uuid.toString(), 
                         plcDevice.getUid().toString(), 
                         Long.toString(System.currentTimeMillis()), 
-                        "S7 Model group " + scanGroups.size(), 
+                        uuid.toString(), 
                         pvScanTime, 
                         "true");
                 if (optPlcGroup.isPresent()) {
                     scanGroups.put(uuid, optPlcGroup.get());
-                    System.out.println("Grupo creado!!!");
+                    logger.info("Group {} was created with uuid: {} 
.",optPlcGroup.get().getGroupName(), uuid.toString());
                 } else {
                     logger.info("Scan group was not created for device {} and 
time {}", plcDevice.getDeviceName(), pvScanTime);
                 }
             }
+
+            
         } else {
-            logger.info("Scan group was not created for DBRecord {}.");
-            System.out.println("No encontro dispositvo!!!");            
+            logger.info("Scan group was not created for DBRecord {}.", 
dbRecord.toString());        
         }
     }
         
@@ -224,10 +237,23 @@ public class S7PlcModelImpl implements PlcModel {
     }
     
    
-    /*
-    *  
-    */
+    /**
+     * Updates the ByteBuf of a PlcItem based on data from a DBRecord.
+     * <p>
+     * This method retrieves data from a DBRecord, extracts relevant 
+     * information such as PV ID, scan time, and memory area,
+     * and updates the corresponding PlcItem's ByteBuf. 
+     * It handles cases where a PlcItem already exists for the given data 
range,
+     * or when a new PlcItem needs to be created and associated with a 
+     * scan group.  
+     * The method also considers scenarios where ByteBuf updates might 
+     * overlap existing items and attempts to find the closest matching 
+     * item to avoid redundancy.
+     * Error handling includes logging when a scan group is not found.
+     * @param dbRecord The DBRecord containing the data to update.
+     */
     private void doUpdateByteBuf(DBRecord dbRecord){
+        // document this method
         PlcItem tempPlcItem = null;
         final DBRecord dbrecord = (DBRecord) dbRecord;
         final PVStructure pvStructure = dbrecord.getPVStructure();        
@@ -242,7 +268,7 @@ public class S7PlcModelImpl implements PlcModel {
         
         String[] strTemp = pvId.split(":", 2);
         String strTag = strTemp[1];
-        
+        logger.info("Paso 01");
         //TODO: Split the Device name.
         S7Tag s7tag = S7Tag.of(strTag); 
         
@@ -251,7 +277,7 @@ public class S7PlcModelImpl implements PlcModel {
         final ByteBuf byteBuf = internalPlcItem.getItemByteBuf();  
         
         dbRecord.atach(internalPlcItem);
-        
+        logger.info("Paso 02");        
         //1. Chequea si el direccionamiento esta dentro de uno de los items
         //   si: 1.1 Verifica si esta dentro de todo el segmento.
         //       1.2 si sobre sale del segmento, llega a una distancia mínima
@@ -266,86 +292,111 @@ public class S7PlcModelImpl implements PlcModel {
         
         //Take the list of items associated with a memory area
         var plcItems = scanItems.get(internalPlcItem);
+        if (null == plcItems) { //(03)
+            scanItems.put(internalPlcItem, new ArrayList<PlcGroup>());         
    
+        }    
         
+        logger.info("Paso 03");  
         //Create the first PlcItem in this memory area
-        if (null == plcItems) { //(03)
-            
-            scanItems.put(internalPlcItem, new 
ArrayList<Pair<PlcGroup,PlcItem>>());            
-            //Create a new PlcItem associated with the memory area
-            PlcItem scanPlcItem = new 
PlcItemImpl.PlcItemBuilder(UUID.randomUUID().toString()).
-                setItemDescription("Flag markes from PLC in byte order.").
-                setItemId("").
-                setItemEnable(false).
-                build(); 
-            
-            //Assigns the request tag in Bytes.
-            
-            S7Tag s7PlcTag = null;
-            s7PlcTag = new S7Tag(TransportSize.USINT,
-                                s7tag.getMemoryArea(),
-                                s7tag.getBlockNumber(),
-                                s7tag.getByteOffset(),
-                                (byte) 0,
-                                
dbRecord.getInnerBuffer().get().writableBytes());   
-         
-            scanPlcItem.setItemPlcTag(s7PlcTag);
-            
-            scanPlcItem.setItemByteBuf(
-                byteBuf.slice(s7tag.getByteOffset(), 
s7tag.getNumberOfElements())
-            );   
-            
-            Optional<Entry<UUID, PlcGroup>> optGroup = scanGroups.
-                    entrySet().
-                    stream().
-                    filter(g -> g.getValue().getPeriod() == scan_time).
-                    findFirst();      
+//        if (null == plcItems) { //(03)
+        logger.info("Paso 04");  
 
-            if (optGroup.isPresent()) {                
-                scanPlcItem.addItemListener(dbrecord);
-                scanPlcItem.setEnable(true); 
-                scanItems.get(internalPlcItem).add(new 
MutablePair<>(optGroup.get().getValue(), scanPlcItem));                
-                optGroup.get().getValue().putItem(scanPlcItem);                
                
-            } else {
-                logger.info("Scan group no present {}.", scan_time);
-            }
-        } else {
-                
-            Optional<PlcItem> optPlcItem = scanItems.keySet().
-                    stream().
-                    filter(i -> {
-                        final S7Tag itemTag = (S7Tag) i.getItemPlcTag();
-                        int x1 = itemTag.getByteOffset();
-                        int x2 =  itemTag.getByteOffset() + 
s7tag.getByteOffset();
+        //Create a new PlcItem associated with the memory area
+        PlcItem scanPlcItem = new 
PlcItemImpl.PlcItemBuilder(UUID.randomUUID().toString()).
+            setItemDescription("Flag markes from PLC in byte order.").
+            setItemId("").
+            setItemEnable(false).
+            build(); 
+        logger.info("Paso 05");  
+        //Assigns the request tag in Bytes.
 
-                        return ((s7tag.getByteOffset() >= x1) && 
(s7tag.getByteOffset() + s7tag.getNumberOfElements() <= x2));
-                    }).
-                    findFirst();
+        S7Tag s7PlcTag = S7Tag.of(strTag);
+        
+//        s7PlcTag = new S7Tag(TransportSize.USINT,
+//                            s7tag.getMemoryArea(),
+//                            s7tag.getBlockNumber(),
+//                            s7tag.getByteOffset(),
+//                            (byte) 0,
+//                            
dbRecord.getInnerBuffer().get().writableBytes());  
 
-            if (!optPlcItem.isPresent()){
-                List<PlcItem> rangeItems = scanItems.keySet().
-                        stream().
-                        filter(i -> {
-                            final S7Tag itemTag = (S7Tag) i.getItemPlcTag();
-                            int x1 = itemTag.getByteOffset();
-                            return (s7tag.getByteOffset() >= x1);
-                        }).
-                        toList();
+        logger.info("Paso 06");  
+        scanPlcItem.setItemPlcTag(s7PlcTag);
+        logger.info("Paso 06.01");
+        scanPlcItem.setItemId(strTag);
+        logger.info("Paso 07");         
 
-                int distance = Integer.MAX_VALUE;
+        scanPlcItem.setItemByteBuf(
+            byteBuf.slice(s7tag.getByteOffset(), s7tag.getNumberOfElements())
+        );   
+        logger.info("Paso 08"); 
+        Optional<Entry<UUID, PlcGroup>> optPlcGroup = scanGroups.
+                entrySet().
+                stream().
+                filter(g -> g.getValue().getPeriod() == scan_time).
+                findFirst();
 
-                while(rangeItems.listIterator().hasNext()) {
-                    final PlcItem i = rangeItems.listIterator().next();
-                    final S7Tag itemTag = (S7Tag) i.getItemPlcTag();           
     
-                    if (Math.abs(s7tag.getByteOffset() - 
itemTag.getByteOffset()) < distance) {
-                        distance = Math.abs(s7tag.getByteOffset() - 
itemTag.getByteOffset());
-                        tempPlcItem = i;
-                    }                
-                }
+        logger.info("Paso 09");  
 
-            } else {
-                tempPlcItem = optPlcItem.get();
-            }
+        if (optPlcGroup.isPresent()) {      
+        logger.info("Paso 09.01");                  
+            scanPlcItem.addItemListener(dbrecord);
+            scanPlcItem.setEnable(true); 
+            scanItems.get(internalPlcItem).add(optPlcGroup.get().getValue());  
              
+            optPlcGroup.get().getValue().putItem(scanPlcItem);
+            logger.info("Paso 09.02");                 
+        } else {
+            logger.info("Scan group no present {}.", scan_time);
         }
+
+        logger.info("Paso 10");  
+            
+//        } 
+//        else {
+//            logger.info("Paso 09");  
+//            
+//            Optional<PlcItem> optPlcItem = plcItems.
+//                    stream(). 
+//                    flatMap(plcGroup -> plcGroup.getItems().stream()).
+//                    filter(i -> {                       
+//                        final S7Tag itemTag = (S7Tag) i.getItemPlcTag();
+//                        logger.info("s7tag: " + itemTag.toString());
+//                        int x1 = itemTag.getByteOffset();
+//                        int x2 =  itemTag.getByteOffset() + 
s7tag.getByteOffset();
+//
+//                        return ((s7tag.getByteOffset() >= x1) && 
(s7tag.getByteOffset() + s7tag.getNumberOfElements() <= x2));
+//                    }).
+//                    findFirst();
+//            
+//            logger.info("Paso 10");  
+//            
+//            if (!optPlcItem.isPresent()){
+//                logger.info("Paso 11");  
+//                List<PlcItem> rangeItems = plcItems.
+//                    stream(). 
+//                    flatMap(plcGroup -> plcGroup.getItems().stream()).       
                 
+//                    filter(i -> {
+//                        final S7Tag itemTag = (S7Tag) i.getItemPlcTag();
+//                        int x1 = itemTag.getByteOffset();
+//                        return (s7tag.getByteOffset() >= x1);
+//                    }).
+//                    toList();
+//                
+//                logger.info("Paso 12");  
+//                int distance = Integer.MAX_VALUE;
+//
+//                while(rangeItems.listIterator().hasNext()) {
+//                    final PlcItem i = rangeItems.listIterator().next();
+//                    final S7Tag itemTag = (S7Tag) i.getItemPlcTag();         
       
+//                    if (Math.abs(s7tag.getByteOffset() - 
itemTag.getByteOffset()) < distance) {
+//                        distance = Math.abs(s7tag.getByteOffset() - 
itemTag.getByteOffset());
+//                        tempPlcItem = i;
+//                    }                
+//                }
+//                logger.info("Paso 13");  
+//            } else {
+//                tempPlcItem = optPlcItem.get();
+//            }
+//        }
         
       
         

Reply via email to