Skip to content

Commit

Permalink
Verifying the execution sequence for a multi-instance system.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceos01 committed May 6, 2023
1 parent 000455b commit facb33c
Show file tree
Hide file tree
Showing 19 changed files with 3,834 additions and 385 deletions.
5 changes: 5 additions & 0 deletions plc4j/integrations/apache-hop/plc4x-hop-actions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,10 @@
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-util-lookup</artifactId>
<version>RELEASE170</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@HopMetadata(
key = "Xplc4xaction",
name = "PLC4x Action",
description = "A shared PLC4x connection to a PLC",
description = "A shared PLC4x connection to a PLC_Borrar",
image = "plc4x_toddy.svg",
documentationUrl = "/metadata-types/neo4j/neo4j-connection.html")
public class ActionSampleMetaData extends HopMetadataBase implements IHopMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@


import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hop.core.Const;
import org.apache.hop.core.exception.HopException;
import org.apache.plc4x.hop.metadata.Plc4xConnection;
import org.apache.plc4x.hop.metadata.util.Plc4xLookup;
import org.apache.plc4x.hop.metadata.util.Plc4xWrapperConnection;
import org.apache.plc4x.java.DefaultPlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.openide.util.Lookup;
import org.w3c.dom.Node;


/*
* The purpose of this "Action" is firstly to verify the connection to
* the PLC and secondly to create a connection that will be shared by
* the Hop environment.
*/
@Action(
id = "CHECK_PLC4X_CONNECTIONS",
name = "i18n::Plc4xActionConnections.Name",
description = "i18n::Plc4xActionConnections.Description",
image = "plc4x_toddy.svg",
image = "plc4x_toddy_play.svg",
categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions",
keywords = "i18n::Plc4xActionConnections.keyword",
documentationUrl = "/workflow/actions/plc4x.html")
Expand All @@ -56,7 +60,13 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct

private Plc4xConnection[] connections;
private boolean connected = false;
private Plc4xWrapperConnection connwrapper = null;
private PlcConnection plcconn = null;
private ActionBase actionbase = null;

private Plc4xLookup lookup = Plc4xLookup.getDefault();
private Lookup.Template template = null;
private Lookup.Result<Plc4xWrapperConnection> lkresult = null;

protected static final String[] unitTimeDesc =
new String[] {
Expand All @@ -79,7 +89,7 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
private long timeStart;
private long now;

public Plc4xCheckConnections( String name) {
public Plc4xCheckConnections(String name) {
super(name, "");
//connections = null;
waitfors = null;
Expand Down Expand Up @@ -170,8 +180,6 @@ private static int getWaitTimeByCode(String tt) {
return 0;
}



/**
*
* Save values to XML
Expand Down Expand Up @@ -247,63 +255,84 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
}
}

/**
* Execute this action and return the result.
* In this case it means, just set the result boolean in the Result
* class.
* Check all conections metadata from the dialog.
* @param prevResult The result of the previous execution
* @return The Result of the execution.
*/
@Override
public Result execute( Result prevResult, int nr ) {
Result result = prevResult;
result.setNrErrors(0);
connected = true;
for (Plc4xConnection connmeta:connections) {
try {
plcconn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(01)
if (!plcconn.isConnected()) {
logBasic("Cant connect to: " + connmeta.getUrl());
connected = false;
plcconn = null;
//break;
/**
* Execute this action and return the result.
* In this case it means, just set the result boolean in the Result class.
*
* Check all conections metadata from the dialog (really only one).
* @param prevResult The result of the previous execution
* @return The Result of the execution.
*/
@Override
public Result execute( Result prevResult, int nr ) {

Result result = prevResult;
result.setNrErrors(0);
connected = true;

actionbase = null;

for (Plc4xConnection connmeta:connections) {

if (null == connwrapper) { //(01)
template = new Lookup.Template<>(Plc4xWrapperConnection.class, connmeta.getName(), null);
lkresult = lookup.lookup(template);
if (!lkresult.allItems().isEmpty()) {
connwrapper = (Plc4xWrapperConnection) lkresult.allInstances().toArray()[0]; //(02)
if (connwrapper != null) connwrapper.retain(); //(03)
}
};

if (null == connwrapper) { //(04)
try {
PlcConnection conn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(05)
if (conn.isConnected()) {
connwrapper = new Plc4xWrapperConnection(conn, connmeta.getName());
lookup.add(connwrapper); //(06)
} else {
connected = false;
plcconn = null;
}
} catch (Exception ex) {
connected = false;
plcconn = null;
}

} else {
if (!connwrapper.getConnection().isConnected()) { //(07)
connected = false;
plcconn = null;
}
}

if (null == connwrapper) { //(08)
try {

PlcConnection conn = new DefaultPlcDriverManager().getConnection(connmeta.getUrl()); //(09)

if (conn.isConnected()) {
conn.close(); //(10)
plcconn = null;
} else {
connected = false;
plcconn = null;
}
} catch (Exception ex){
connected = false;
plcconn = null;
}

}
plcconn.close();
plcconn = null;
} catch (Exception ex) {
Logger.getLogger(Plc4xCheckConnections.class.getName()).log(Level.SEVERE, null, ex);
connected = false;
plcconn = null;
//break;
} finally {

}

}

result.setResult(connected);
return result;
}

/**
*
* Add checks to report warnings
*
* @param remarks
* @param workflowMeta
* @param variables
* @param metadataProvider
*/
@Override
public void check( List<ICheckResult> remarks, WorkflowMeta workflowMeta, IVariables variables,
IHopMetadataProvider metadataProvider ) {
}

@Override
public boolean resetErrorsBeforeExecution() {
return false;
}
@Override
public boolean resetErrorsBeforeExecution() {
return false;
}

@Override
public boolean isEvaluation() {
Expand All @@ -313,9 +342,6 @@ public boolean isEvaluation() {
@Override
public boolean isUnconditional() {
return false;
}



}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,45 @@
import java.util.List;
import org.apache.hop.core.Const;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.pipeline.PipelineMeta;
import org.apache.hop.pipeline.engine.IPipelineEngine;
import org.apache.plc4x.hop.metadata.Plc4xConnection;
import org.apache.plc4x.hop.metadata.util.Plc4xLookup;
import org.apache.plc4x.hop.metadata.util.Plc4xWrapperConnection;
import org.openide.util.Lookup;
import org.w3c.dom.Node;


/*
* The purpose of this "Action" is to release the driver resource
* within a "workflow", in case the number of accounts reaches zero,
* the wrapper will close the connection.
* The work pattern must be within the "workflow":
*
* Start -> Create connection -> Run pipeline -> Close connection -> Finish.
*
* It should always be taken into account that the connections to
* the PLCs are limited resources.
*/
@Action(
id = "CHECK_PLC4X_DISCONNECTIONS",
name = "i18n::Plc4xActionDisConnections.Name",
description = "i18n::Plc4xActionDisConnections.Description",
image = "plc4x_toddy.svg",
image = "plc4x_toddy_stop.svg",
categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions",
keywords = "i18n::Plc4xActionDisConnections.keyword",
documentationUrl = "/workflow/actions/checkdbconnection.html")
public class Plc4xCheckDisConnections extends ActionBase implements Cloneable, IAction {
private static final Class<?> PKG = Plc4xCheckDisConnections.class; // Needed by Translator

private static final Class<?> PKG = Plc4xCheckDisConnections.class; // Needed by Translator


private Plc4xConnection[] connections;
private Plc4xConnection[] connections;
private Plc4xWrapperConnection connwrapper = null;
private ActionBase actionbase = null;

private Plc4xLookup lookup = Plc4xLookup.getDefault();
private Lookup.Template template = null;
private Lookup.Result<Plc4xWrapperConnection> lkresult = null;

protected static final String[] unitTimeDesc =
new String[] {
Expand Down Expand Up @@ -240,32 +262,31 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
}
}

/**
* Execute this action and return the result. In this case it means, just set the result boolean in the Result
* class.
*
* @param result The result of the previous execution
* @return The Result of the execution.
*/
@Override
public Result execute( Result result, int nr ) {
result.setResult(true);
System.out.println("NR: " + nr);
/**
* Execute this action and return the result. In this case it means,
* just set the result boolean in the Result class.
*
* @param result The result of the previous execution
* @return The Result of the execution.
*/
@Override
public Result execute( Result result, int nr ) {
result.setResult(true);

for (Plc4xConnection connmeta:connections) {
template = new Lookup.Template<>(Plc4xWrapperConnection.class, connmeta.getName(), null);
lkresult = lookup.lookup(template);
if (!lkresult.allItems().isEmpty()) {
connwrapper = (Plc4xWrapperConnection) lkresult.allInstances().toArray()[0];
if (connwrapper != null) {
connwrapper.release();
if (connwrapper.refCnt() <= 0)
lookup.remove(connwrapper);
}
}
};

return result;
}

/**
*
* Add checks to report warnings
*
* @param remarks
* @param workflowMeta
* @param variables
* @param metadataProvider
*/
@Override
public void check( List<ICheckResult> remarks, WorkflowMeta workflowMeta, IVariables variables,
IHopMetadataProvider metadataProvider ) {
}
}
Loading

0 comments on commit facb33c

Please sign in to comment.