3131
3232
3333import java .util .List ;
34- import java .util .logging .Level ;
35- import java .util .logging .Logger ;
3634import org .apache .hop .core .Const ;
3735import org .apache .hop .core .exception .HopException ;
3836import org .apache .plc4x .hop .metadata .Plc4xConnection ;
37+ import org .apache .plc4x .hop .metadata .util .Plc4xLookup ;
38+ import org .apache .plc4x .hop .metadata .util .Plc4xWrapperConnection ;
3939import org .apache .plc4x .java .DefaultPlcDriverManager ;
4040import org .apache .plc4x .java .api .PlcConnection ;
41- import org .apache . plc4x . java . api . exceptions . PlcConnectionException ;
41+ import org .openide . util . Lookup ;
4242import org .w3c .dom .Node ;
4343
44-
44+ /*
45+ * The purpose of this "Action" is firstly to verify the connection to
46+ * the PLC and secondly to create a connection that will be shared by
47+ * the Hop environment.
48+ */
4549@ Action (
4650 id = "CHECK_PLC4X_CONNECTIONS" ,
4751 name = "i18n::Plc4xActionConnections.Name" ,
4852 description = "i18n::Plc4xActionConnections.Description" ,
49- image = "plc4x_toddy .svg" ,
53+ image = "plc4x_toddy_play .svg" ,
5054 categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions" ,
5155 keywords = "i18n::Plc4xActionConnections.keyword" ,
5256 documentationUrl = "/workflow/actions/plc4x.html" )
@@ -56,7 +60,13 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
5660
5761 private Plc4xConnection [] connections ;
5862 private boolean connected = false ;
63+ private Plc4xWrapperConnection connwrapper = null ;
5964 private PlcConnection plcconn = null ;
65+ private ActionBase actionbase = null ;
66+
67+ private Plc4xLookup lookup = Plc4xLookup .getDefault ();
68+ private Lookup .Template template = null ;
69+ private Lookup .Result <Plc4xWrapperConnection > lkresult = null ;
6070
6171 protected static final String [] unitTimeDesc =
6272 new String [] {
@@ -79,7 +89,7 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
7989 private long timeStart ;
8090 private long now ;
8191
82- public Plc4xCheckConnections ( String name ) {
92+ public Plc4xCheckConnections (String name ) {
8393 super (name , "" );
8494 //connections = null;
8595 waitfors = null ;
@@ -170,8 +180,6 @@ private static int getWaitTimeByCode(String tt) {
170180 return 0 ;
171181 }
172182
173-
174-
175183 /**
176184 *
177185 * Save values to XML
@@ -247,63 +255,84 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
247255 }
248256 }
249257
250- /**
251- * Execute this action and return the result.
252- * In this case it means, just set the result boolean in the Result
253- * class.
254- * Check all conections metadata from the dialog.
255- * @param prevResult The result of the previous execution
256- * @return The Result of the execution.
257- */
258- @ Override
259- public Result execute ( Result prevResult , int nr ) {
260- Result result = prevResult ;
261- result .setNrErrors (0 );
262- connected = true ;
263- for (Plc4xConnection connmeta :connections ) {
264- try {
265- plcconn = new DefaultPlcDriverManager ().getConnection (connmeta .getUrl ()); //(01)
266- if (!plcconn .isConnected ()) {
267- logBasic ("Cant connect to: " + connmeta .getUrl ());
268- connected = false ;
269- plcconn = null ;
270- //break;
258+ /**
259+ * Execute this action and return the result.
260+ * In this case it means, just set the result boolean in the Result class.
261+ *
262+ * Check all conections metadata from the dialog (really only one).
263+ * @param prevResult The result of the previous execution
264+ * @return The Result of the execution.
265+ */
266+ @ Override
267+ public Result execute ( Result prevResult , int nr ) {
268+
269+ Result result = prevResult ;
270+ result .setNrErrors (0 );
271+ connected = true ;
272+
273+ actionbase = null ;
274+
275+ for (Plc4xConnection connmeta :connections ) {
276+
277+ if (null == connwrapper ) { //(01)
278+ template = new Lookup .Template <>(Plc4xWrapperConnection .class , connmeta .getName (), null );
279+ lkresult = lookup .lookup (template );
280+ if (!lkresult .allItems ().isEmpty ()) {
281+ connwrapper = (Plc4xWrapperConnection ) lkresult .allInstances ().toArray ()[0 ]; //(02)
282+ if (connwrapper != null ) connwrapper .retain (); //(03)
283+ }
284+ };
285+
286+ if (null == connwrapper ) { //(04)
287+ try {
288+ PlcConnection conn = new DefaultPlcDriverManager ().getConnection (connmeta .getUrl ()); //(05)
289+ if (conn .isConnected ()) {
290+ connwrapper = new Plc4xWrapperConnection (conn , connmeta .getName ());
291+ lookup .add (connwrapper ); //(06)
292+ } else {
293+ connected = false ;
294+ plcconn = null ;
295+ }
296+ } catch (Exception ex ) {
297+ connected = false ;
298+ plcconn = null ;
299+ }
300+
301+ } else {
302+ if (!connwrapper .getConnection ().isConnected ()) { //(07)
303+ connected = false ;
304+ plcconn = null ;
305+ }
306+ }
307+
308+ if (null == connwrapper ) { //(08)
309+ try {
310+
311+ PlcConnection conn = new DefaultPlcDriverManager ().getConnection (connmeta .getUrl ()); //(09)
312+
313+ if (conn .isConnected ()) {
314+ conn .close (); //(10)
315+ plcconn = null ;
316+ } else {
317+ connected = false ;
318+ plcconn = null ;
319+ }
320+ } catch (Exception ex ){
321+ connected = false ;
322+ plcconn = null ;
323+ }
324+
271325 }
272- plcconn .close ();
273- plcconn = null ;
274- } catch (Exception ex ) {
275- Logger .getLogger (Plc4xCheckConnections .class .getName ()).log (Level .SEVERE , null , ex );
276- connected = false ;
277- plcconn = null ;
278- //break;
279- } finally {
280-
281326 }
282-
283- }
284327
285328 result .setResult (connected );
286329 return result ;
287330 }
288331
289- /**
290- *
291- * Add checks to report warnings
292- *
293- * @param remarks
294- * @param workflowMeta
295- * @param variables
296- * @param metadataProvider
297- */
298- @ Override
299- public void check ( List <ICheckResult > remarks , WorkflowMeta workflowMeta , IVariables variables ,
300- IHopMetadataProvider metadataProvider ) {
301- }
302-
303- @ Override
304- public boolean resetErrorsBeforeExecution () {
305- return false ;
306- }
332+ @ Override
333+ public boolean resetErrorsBeforeExecution () {
334+ return false ;
335+ }
307336
308337 @ Override
309338 public boolean isEvaluation () {
@@ -313,9 +342,6 @@ public boolean isEvaluation() {
313342 @ Override
314343 public boolean isUnconditional () {
315344 return false ;
316- }
317-
318-
319-
345+ }
320346
321347}
0 commit comments