31
31
32
32
33
33
import java .util .List ;
34
- import java .util .logging .Level ;
35
- import java .util .logging .Logger ;
36
34
import org .apache .hop .core .Const ;
37
35
import org .apache .hop .core .exception .HopException ;
38
36
import org .apache .plc4x .hop .metadata .Plc4xConnection ;
37
+ import org .apache .plc4x .hop .metadata .util .Plc4xLookup ;
38
+ import org .apache .plc4x .hop .metadata .util .Plc4xWrapperConnection ;
39
39
import org .apache .plc4x .java .DefaultPlcDriverManager ;
40
40
import org .apache .plc4x .java .api .PlcConnection ;
41
- import org .apache . plc4x . java . api . exceptions . PlcConnectionException ;
41
+ import org .openide . util . Lookup ;
42
42
import org .w3c .dom .Node ;
43
43
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
+ */
45
49
@ Action (
46
50
id = "CHECK_PLC4X_CONNECTIONS" ,
47
51
name = "i18n::Plc4xActionConnections.Name" ,
48
52
description = "i18n::Plc4xActionConnections.Description" ,
49
- image = "plc4x_toddy .svg" ,
53
+ image = "plc4x_toddy_play .svg" ,
50
54
categoryDescription = "i18n:org.apache.hop.workflow:ActionCategory.Category.Conditions" ,
51
55
keywords = "i18n::Plc4xActionConnections.keyword" ,
52
56
documentationUrl = "/workflow/actions/plc4x.html" )
@@ -56,7 +60,13 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
56
60
57
61
private Plc4xConnection [] connections ;
58
62
private boolean connected = false ;
63
+ private Plc4xWrapperConnection connwrapper = null ;
59
64
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 ;
60
70
61
71
protected static final String [] unitTimeDesc =
62
72
new String [] {
@@ -79,7 +89,7 @@ public class Plc4xCheckConnections extends ActionBase implements Cloneable, IAct
79
89
private long timeStart ;
80
90
private long now ;
81
91
82
- public Plc4xCheckConnections ( String name ) {
92
+ public Plc4xCheckConnections (String name ) {
83
93
super (name , "" );
84
94
//connections = null;
85
95
waitfors = null ;
@@ -170,8 +180,6 @@ private static int getWaitTimeByCode(String tt) {
170
180
return 0 ;
171
181
}
172
182
173
-
174
-
175
183
/**
176
184
*
177
185
* Save values to XML
@@ -247,63 +255,84 @@ public void loadXml( Node entrynode, IHopMetadataProvider metadataProvider, IVar
247
255
}
248
256
}
249
257
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
+
271
325
}
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
-
281
326
}
282
-
283
- }
284
327
285
328
result .setResult (connected );
286
329
return result ;
287
330
}
288
331
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
+ }
307
336
308
337
@ Override
309
338
public boolean isEvaluation () {
@@ -313,9 +342,6 @@ public boolean isEvaluation() {
313
342
@ Override
314
343
public boolean isUnconditional () {
315
344
return false ;
316
- }
317
-
318
-
319
-
345
+ }
320
346
321
347
}
0 commit comments