2121import java .io .IOException ;
2222import java .util .UUID ;
2323import javax .swing .JDialog ;
24- import org .apache .plc4x .app .api .DeviceDBRecord ;
25- import org .apache .plc4x .app .api .DriverDBRecord ;
24+ import org .apache .plc4x .app .api .MasterDB ;
2625import org .apache .plc4x .app .api .Plc4xDialog ;
27- import org .apache .plc4x .app .api .Plc4xDialogParametersEnum ;
26+ import org .apache .plc4x .app .api .Plc4xPropertyEnum ;
2827import org .openide .cookies .InstanceCookie ;
29- import org .openide .nodes .AbstractNode ;
3028import org .openide .nodes .BeanNode ;
3129import org .openide .nodes .Node ;
3230import org .openide .util .Exceptions ;
31+ import org .openide .util .Lookup ;
3332import org .openide .util .lookup .ServiceProvider ;
33+ import org .apache .plc4x .app .api .DeviceRecord ;
34+ import org .apache .plc4x .app .api .DriverRecord ;
3435
35- @ ServiceProvider (service =JDialog .class , path ="Plc4xDriver/modbus-ascii" )
36+ @ ServiceProvider (service =Plc4xDialog .class , path ="Plc4xDriver/modbus-ascii" )
3637public class Plc4xModbusAsciiDialog extends JDialog implements Plc4xDialog {
3738
39+ private final String DRIVER_CODE = "modbus-ascii" ;
40+ private final MasterDB db = Lookup .getDefault ().lookup (MasterDB .class );
41+
3842 public BeanNode node ;
39- private DriverDBRecord driverrecord = null ;
40- private DeviceDBRecord devicerecord = null ;
43+ private DriverRecord driverrecord = null ;
44+ private DeviceRecord devicerecord = null ;
4145 private UUID uuid ;
4246
43- private Plc4xDialogParametersEnum parameters ;
47+ private Plc4xPropertyEnum parameters ;
4448
4549 public Plc4xModbusAsciiDialog () {
4650 super (new javax .swing .JFrame (), true );
@@ -362,23 +366,28 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
362366 }// </editor-fold>//GEN-END:initComponents
363367
364368 private void brOkActionPerformed (java .awt .event .ActionEvent evt ) {//GEN-FIRST:event_brOkActionPerformed
365- // TODO add your handling code here:
366- /*
367- final InstanceCookie cookie = node.getLookup().lookup(InstanceCookie.class);
368- final DeviceDBRecord dbr = (DeviceDBRecord) cookie.instanceCreate();
369- node.setValue(parameters.SERIAL_PORT.name(), (String) cbSerialPort.getSelectedItem());
370- node.setValue(parameters.BAUD_RATE.name(), (String) cbBaudRate.getSelectedItem());
371- node.setValue(parameters.DATA_BITS.name(), (String) cbBaudRate.getSelectedItem());
372- node.setValue(parameters.PARITY.name(), (String) cbParity.getSelectedItem());
373- node.setValue(parameters.STOP_BITS.name(), (String) cbStopBits.getSelectedItem());
374- node.setValue(parameters.TIMEOUT.name(), (String) cbTimeOut.getSelectedItem());
375- */
376- if ((driverrecord != null ) && (devicerecord != null )) {
377-
378- devicerecord .setDeviceName (tfDeviceName .getText ());
379- devicerecord .setUUID (UUID .fromString (tfUUID .getText ()));
380- driverrecord .getMapDevices ().put (devicerecord .getUUID (), devicerecord );
381- }
369+
370+ driverrecord = db .getDriverByCode (DRIVER_CODE );
371+ devicerecord = db .createDeviceDBRecord ();
372+
373+ final DeviceRecord dbdevice = driverrecord .getDevice (tfDeviceName .getText ().trim ());
374+
375+ if ((driverrecord != null ) && (devicerecord != null ) && (dbdevice == null )) {
376+
377+ devicerecord .setDeviceName (tfDeviceName .getText ().trim ());
378+ devicerecord .setDeviceName (tfDeviceName .getText ().trim ());
379+ devicerecord .setUUID (UUID .fromString (tfUUID .getText ()));
380+
381+ devicerecord .getProperties ().put (parameters .SERIAL_PORT .name (), (String ) cbSerialPort .getSelectedItem ());
382+ devicerecord .getProperties ().put (parameters .BAUD_RATE .name (), (String ) cbBaudRate .getSelectedItem ());
383+ devicerecord .getProperties ().put (parameters .DATA_BITS .name (), (String ) cbBaudRate .getSelectedItem ());
384+ devicerecord .getProperties ().put (parameters .PARITY .name (), (String ) cbParity .getSelectedItem ());
385+ devicerecord .getProperties ().put (parameters .STOP_BITS .name (), (String ) cbStopBits .getSelectedItem ());
386+ devicerecord .getProperties ().put (parameters .TIMEOUT .name (), (String ) tfTimeOut .getText ());
387+
388+ driverrecord .addDevice (devicerecord );
389+
390+ }
382391
383392 this .setVisible (false );
384393 }//GEN-LAST:event_brOkActionPerformed
@@ -487,7 +496,7 @@ public void setNode(Node node) {
487496 final InstanceCookie cookie = node .getLookup ().lookup (InstanceCookie .class );
488497 driverrecord = null ;
489498 try {
490- driverrecord = (DriverDBRecord ) cookie .instanceCreate ();
499+ driverrecord = (DriverRecord ) cookie .instanceCreate ();
491500 initfields ();
492501 } catch (IOException | ClassNotFoundException ex ) {
493502 Exceptions .printStackTrace (ex );
@@ -496,7 +505,9 @@ public void setNode(Node node) {
496505
497506 private void initfields () {
498507 if (driverrecord != null ) {
499- devicerecord = (DeviceDBRecord ) node .getValue ("DEVICE" );
508+
509+ devicerecord = (DeviceRecord ) node .getValue ("DEVICE" );
510+
500511 tfDeviceProtocol .setText (driverrecord .getProtocolCode ());
501512
502513 tfDeviceName .setText ("" );
@@ -509,7 +520,7 @@ private void initfields() {
509520 //Default no S88 node selected
510521 tfS88UUID .setText ("" );
511522
512-
523+ //Default to 3 seconds delay
513524 tfTimeOut .setText ("3" );
514525
515526 }
0 commit comments