DELL
2024-01-10 554f43497fca806ef44491c7833e1b7ad7c7b285
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package org.openmuc.openiec61850.app;
 
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.JCheckBox;
import javax.swing.JTextField;
 
import org.openmuc.openiec61850.BdaFloat32;
import org.openmuc.openiec61850.BdaBoolean;
import org.openmuc.openiec61850.BdaTriggerConditions;
import org.openmuc.openiec61850.Brcb;
import org.openmuc.openiec61850.ClientAssociation;
import org.openmuc.openiec61850.ClientEventListener;
import org.openmuc.openiec61850.ClientSap;
import org.openmuc.openiec61850.DataSet;
import org.openmuc.openiec61850.Fc;
import org.openmuc.openiec61850.FcModelNode;
import org.openmuc.openiec61850.ModelNode;
import org.openmuc.openiec61850.Report;
import org.openmuc.openiec61850.SclParseException;
import org.openmuc.openiec61850.ServerModel;
import org.openmuc.openiec61850.ServerSap;
import org.openmuc.openiec61850.ServiceError;
import org.openmuc.openiec61850.Urcb;
import org.openmuc.openiec61850.clientgui.BasicDataBind;
import org.openmuc.openiec61850.internal.cli.Action;
import org.openmuc.openiec61850.internal.cli.ActionException;
import org.openmuc.openiec61850.internal.cli.ActionListener;
import org.openmuc.openiec61850.internal.cli.ActionProcessor;
import org.openmuc.openiec61850.internal.cli.CliParameter;
import org.openmuc.openiec61850.internal.cli.CliParameterBuilder;
import org.openmuc.openiec61850.internal.cli.CliParseException;
import org.openmuc.openiec61850.internal.cli.CliParser;
import org.openmuc.openiec61850.internal.cli.FatalActionException;
import org.openmuc.openiec61850.internal.cli.IntCliParameter;
import org.openmuc.openiec61850.internal.cli.StringCliParameter;
import org.openmuc.openiec61850.clientgui.databind.BooleanDataBind;
import org.openmuc.openiec61850.clientgui.databind.Float32DataBind;
 
/**
 *
 * @author Stefan Feuerhahn
 *
 */
public class ConsoleClient {
    private static final String PRINT_MODEL_KEY = "m";
    private static final String PRINT_MODEL_KEY_DESCRIPTION = "print model";
    private static final String GET_DATA_VALUES_KEY = "g";
    private static final String GET_DATA_VALUES_KEY_DESCRIPTION = "send GetDataValues request";
    private static final String READ_ALL_DATA_KEY = "ga";
    private static final String READ_ALL_DATA_KEY_DESCRIPTION = "update all data in the model";
    private static final String CREATE_DATA_SET_KEY = "cds";
    private static final String CREATE_DATA_SET_KEY_DESCRIPTION = "create data set";
    private static final String DELETE_DATA_SET_KEY = "dds";
    private static final String DELETE_DATA_SET_KEY_DESCRIPTION = "delete data set";
    private static final String REPORTING_KEY = "r";
    private static final String REPORTING_KEY_DESCRIPTION = "configure reporting";
 
    private static final StringCliParameter hostParam = new CliParameterBuilder("-h")
            .setDescription("The IP/domain address of the server you want to access.")
            .setMandatory()
            .buildStringParameter("host");
    private static final IntCliParameter portParam = new CliParameterBuilder("-p")
            .setDescription("The port to connect to.")
            .buildIntParameter("port", 102);
    private static final StringCliParameter modelFileParam = new CliParameterBuilder("-m").setDescription(
            "The file name of the SCL file to read the model from. If this parameter is omitted the model will be read from the server device after connection.")
            .buildStringParameter("model-file");
 
    private static volatile ClientAssociation association;
    private static ServerModel serverModel;
    private static final ActionProcessor actionProcessor = new ActionProcessor(new ActionExecutor());
 
    private static class EventListener implements ClientEventListener {
 
        @Override
        public void newReport(Report report) {
            System.out.println("\n----------------");
            System.out.println("Received report: ");
            System.err.println(report);
            System.out.println("------------------");
        }
 
        @Override
        public void associationClosed(IOException e) {
            System.out.print("Received connection closed signal. Reason: ");
            if (!e.getMessage().isEmpty()) {
                System.out.println(e.getMessage());
            }
            else {
                System.out.println("unknown");
            }
            actionProcessor.close();
        }
    }
 
    private static class ActionExecutor implements ActionListener {
 
        @Override
        public void actionCalled(String actionKey) throws ActionException, FatalActionException {
            try {
                switch (actionKey) {
                case PRINT_MODEL_KEY:
                    System.out.println(serverModel);
                    break;
                case READ_ALL_DATA_KEY:
                    System.out.print("Reading all data...");
                    try {
                        association.getAllDataValues();
                    } catch (ServiceError e) {
                        System.err.println("Service error: " + e.getMessage());
                    }
                    System.out.println("done");
                    break;
                case GET_DATA_VALUES_KEY: {
 
                    if (serverModel == null) {
                        System.out.println("You have to retrieve the model before reading data.");
                        return;
                    }
 
                    FcModelNode fcModelNode = askForFcModelNode();
                    
                    System.out.println("Sending SetDataValues request...");
 
                    try {
                        BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fcModelNode);
                        JTextField tf_t = (JTextField)data.getValueField();
                        tf_t.setText("12.0");
                        data.write();
                        association.setDataValues(fcModelNode);
                    } catch (ServiceError e) {
                        System.out.println("Service error: " + e.getMessage());
                        return;
                    } catch (IOException e) {
                        System.out.println("Fatal error: " + e.getMessage());
                        return;
                    }
                    System.out.println("Successfully write data.");
 
                    System.out.println("Sending GetDataValues request...");
                    try {
                        association.getDataValues(fcModelNode);
                    } catch (ServiceError e) {
                        System.out.println("Service error: " + e.getMessage());
                        return;
                    } catch (IOException e) {
                        System.out.println("Fatal error: " + e.getMessage());
                        return;
                    }
 
                    System.out.println("Successfully read data.");
                    BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fcModelNode);
                    JTextField tf_t = (JTextField)data.getValueField();
                    System.out.println("The read data is:" + tf_t.getText());
                    //System.out.println(fcModelNode);
                    
                    break;
                }
                case CREATE_DATA_SET_KEY: {
 
                    System.out.println("Enter the reference of the data set to create (e.g. myld/MYLN0.dataset1): ");
                    String reference = actionProcessor.getReader().readLine();
 
                    System.out.println("How many entries shall the data set have: ");
                    String numberOfEntriesString = actionProcessor.getReader().readLine();
                    int numDataSetEntries = Integer.parseInt(numberOfEntriesString);
 
                    List<FcModelNode> dataSetMembers = new ArrayList<>();
                    for (int i = 0; i < numDataSetEntries; i++) {
                        dataSetMembers.add(askForFcModelNode());
                    }
 
                    DataSet dataSet = new DataSet(reference, dataSetMembers);
                    System.out.print("Creating data set..");
                    association.createDataSet(dataSet);
                    System.out.println("done");
 
                    break;
                }
                case DELETE_DATA_SET_KEY: {
 
                    System.out.println("Enter the reference of the data set to delete (e.g. myld/MYLN0.dataset1): ");
                    String reference = actionProcessor.getReader().readLine();
 
                    DataSet dataSet = serverModel.getDataSet(reference);
                    if (dataSet == null) {
                        throw new ActionException("Unable to find data set with the given reference.");
                    }
                    System.out.print("Deleting data set..");
                    association.deleteDataSet(dataSet);
                    System.out.println("done");
 
                    break;
                }
                case REPORTING_KEY: {
 
                    System.out.println("Enter the URCB reference: ");
                    String reference = actionProcessor.getReader().readLine();
                    Urcb urcb = serverModel.getUrcb(reference);
                    if (urcb == null) {
                        Brcb brcb = serverModel.getBrcb(reference);
                        if (brcb != null) {
                            throw new ActionException(
                                    "Though buffered reporting is supported by the library it is not yet supported by the console application.");
                        }
                        throw new ActionException("Unable to find RCB with the given reference.");
                    }
 
                    while (true) {
                        association.getRcbValues(urcb);
                        System.out.println();
                        System.out.println(urcb);
                        System.out.println();
                        System.out.println("What do you want to configure?");
                        System.out.println("1 - reserve");
                        System.out.println("2 - cancel reservation");
                        System.out.println("3 - enable");
                        System.out.println("4 - disable");
                        System.out.println("5 - set data set");
                        System.out.println("6 - set trigger options");
                        System.out.println("7 - set integrity period");
                        System.out.println("8 - send general interrogation");
                        System.out.println("0 - quit");
                        try {
                            int rcbAction = Integer.parseInt(actionProcessor.getReader().readLine());
                            switch (rcbAction) {
                            case 0:
                                return;
                            case 1:
                                System.out.print("Reserving RCB..");
                                association.reserveUrcb(urcb);
                                System.out.println("done");
                                break;
                            case 2:
                                System.out.print("Canceling RCB reservation..");
                                association.cancelUrcbReservation(urcb);
                                System.out.println("done");
                                break;
                            case 3:
                                System.out.print("Enabling reporting..");
                                association.enableReporting(urcb);
                                System.out.println("done");
                                break;
                            case 4:
                                System.out.print("Disabling reporting..");
                                association.disableReporting(urcb);
                                System.out.println("done");
                                break;
                            case 5: {
                                System.out.print("Set data set reference:");
                                String dataSetReference = actionProcessor.getReader().readLine();
                                urcb.getDatSet().setValue(dataSetReference);
                                List<ServiceError> serviceErrors = association.setRcbValues(urcb, false, true, false,
                                        false, false, false, false, false);
                                if (serviceErrors.get(0) != null) {
                                    throw serviceErrors.get(0);
                                }
                                System.out.println("done");
                                break;
                            }
                            case 6: {
                                System.out.print(
                                        "Set the trigger options (data change, data update, quality change, interity, GI):");
                                String triggerOptionsString = actionProcessor.getReader().readLine();
                                String[] triggerOptionsStrings = triggerOptionsString.split(",");
                                BdaTriggerConditions triggerOptions = urcb.getTrgOps();
                                triggerOptions.setDataChange(Boolean.parseBoolean(triggerOptionsStrings[0]));
                                triggerOptions.setDataUpdate(Boolean.parseBoolean(triggerOptionsStrings[1]));
                                triggerOptions.setQualityChange(Boolean.parseBoolean(triggerOptionsStrings[2]));
                                triggerOptions.setIntegrity(Boolean.parseBoolean(triggerOptionsStrings[3]));
                                triggerOptions.setGeneralInterrogation(Boolean.parseBoolean(triggerOptionsStrings[4]));
                                List<ServiceError> serviceErrors = association.setRcbValues(urcb, false, false, false,
                                        false, true, false, false, false);
                                if (serviceErrors.get(0) != null) {
                                    throw serviceErrors.get(0);
                                }
                                System.out.println("done");
                                break;
                            }
                            case 7: {
                                System.out.print("Specify integrity period in ms:");
                                String integrityPeriodString = actionProcessor.getReader().readLine();
                                urcb.getIntgPd().setValue(Long.parseLong(integrityPeriodString));
                                List<ServiceError> serviceErrors = association.setRcbValues(urcb, false, false, false,
                                        false, false, true, false, false);
                                if (serviceErrors.get(0) != null) {
                                    throw serviceErrors.get(0);
                                }
                                System.out.println("done");
                                break;
                            }
                            case 8:
                                System.out.print("Sending GI..");
                                association.startGi(urcb);
                                System.out.println("done");
                                break;
                            default:
                                System.err.println("Unknown option.");
                                break;
                            }
                        } catch (ServiceError e) {
                            System.err.println("Service error: " + e.getMessage());
                        } catch (NumberFormatException e) {
                            System.err.println("Cannot parse number: " + e.getMessage());
                        }
 
                    }
                }
                default:
                    break;
                }
            } catch (Exception e) {
                throw new ActionException(e);
            }
        }
 
        private FcModelNode askForFcModelNode() throws IOException, ActionException {
            System.out.println("Enter reference (e.g. myld/MYLN0.do.da.bda): ");
            String reference = actionProcessor.getReader().readLine();
            System.out.println("Enter functional constraint of referenced node: ");
            String fcString = actionProcessor.getReader().readLine();
 
            Fc fc = Fc.fromString(fcString);
            if (fc == null) {
                throw new ActionException("Unknown functional constraint.");
            }
 
            ModelNode modelNode = serverModel.findModelNode(reference, Fc.fromString(fcString));
            if (modelNode == null) {
                throw new ActionException(
                        "A model node with the given reference and functional constraint could not be found.");
            }
 
            if (!(modelNode instanceof FcModelNode)) {
                throw new ActionException("The given model node is not a functionally constraint model node.");
            }
 
            FcModelNode fcModelNode = (FcModelNode) modelNode;
            return fcModelNode;
        }
 
        @Override
        public void quit() {
            System.out.println("** Closing connection.");
            association.close();
            return;
        }
    }
    
    public static void writeNodeData(String reference, String fc_str) {
        FcModelNode fcModelNode = (FcModelNode)serverModel.findModelNode(reference, Fc.fromString(fc_str));
        if(null == fcModelNode) {
            System.err.println("error, no modenode was found.....");
            return;
        }
        
        System.out.println("Sending SetDataValues request...");
        try {
            /*
            BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fcModelNode);
            JTextField tf_t = (JTextField)data.getValueField();
            tf_t.setText("12.0");
            */
            BasicDataBind<?> data = new BooleanDataBind((BdaBoolean)fcModelNode.getChild("ctlVal"));
            JCheckBox ckb_t = (JCheckBox)data.getValueField();
            ckb_t.setSelected(false);
            data.write();
            association.setDataValues(fcModelNode);
        } catch (ServiceError e) {
            System.out.println("Service error: " + e.getMessage());
            return;
        } catch (IOException e) {
            System.out.println("Fatal error: " + e.getMessage());
            return;
        }
        System.out.println("Successfully write data.");
    }
    
    public static void readNodeData(String reference, String fc_str) {
        FcModelNode fcModelNode = (FcModelNode)serverModel.findModelNode(reference, Fc.fromString(fc_str));
        if(null == fcModelNode) {
            System.err.println("error, no modenode was found.....");
            return;
        }
        
        System.out.println("Sending GetDataValues request...");
        try {
            association.getDataValues(fcModelNode);
        } catch (ServiceError e) {
            System.out.println("Service error: " + e.getMessage());
            return;
        } catch (IOException e) {
            System.out.println("Fatal error: " + e.getMessage());
            return;
        }
 
        System.out.println("Successfully read data.");
        /*
        BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fcModelNode);
        JTextField tf_t = (JTextField)data.getValueField();
        System.out.println("The modelnode is:" + fcModelNode.getReference() + ", the value is: " + tf_t.getText());
        */
        BasicDataBind<?> data = new BooleanDataBind((BdaBoolean)fcModelNode.getChild("ctlVal"));
        JCheckBox ckb_t = (JCheckBox)data.getValueField();
        System.out.println("The modelnode is:" + fcModelNode.getChild("ctlVal").getReference() + ", the value is: " + ckb_t.isSelected());
    }
 
    public static void main(String[] args) {
 
        List<CliParameter> cliParameters = new ArrayList<>();
        cliParameters.add(hostParam);
        cliParameters.add(portParam);
        cliParameters.add(modelFileParam);
 
        CliParser cliParser = new CliParser("openiec61850-console-client",
                "A client application to access IEC 61850 MMS servers.");
        cliParser.addParameters(cliParameters);
 
        try {
            cliParser.parseArguments(args);
        } catch (CliParseException e1) {
            System.err.println("Error parsing command line parameters: " + e1.getMessage());
            System.out.println(cliParser.getUsageString());
            System.exit(1);
        }
 
        InetAddress address;
        try {
            address = InetAddress.getByName(hostParam.getValue());
        } catch (UnknownHostException e) {
            System.out.println("Unknown host: " + hostParam.getValue());
            return;
        }
 
        ClientSap clientSap = new ClientSap();
 
        try {
            association = clientSap.associate(address, portParam.getValue(), null, new EventListener());
        } catch (IOException e) {
            System.out.println("Unable to connect to remote host.");
            return;
        }
 
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                association.close();
            }
        });
 
        System.out.println("successfully connected");
 
        /*if (modelFileParam.isSelected())*/ {
            System.out.println("reading model from file...");
 
            try {
                List<ServerSap> serverSaps = ServerSap.getSapsFromSclFile("zjdy_bts_1_2G.icd");
                if (serverSaps == null || serverSaps.size() == 0) {
                    throw new SclParseException("No AccessPoint found in SCL file.");
                }
                //serverModel = serverSaps.get(0).serverModel;
                //serverModel = association.getModelFromSclFile("zjdy_bts_1_2G.icd");
                //serverModel = association.getModelFromSclFile(modelFileParam.getValue());
            } catch (SclParseException e1) {
                System.out.println("Error parsing SCL file: " + e1.getMessage());
                return;
            }
 
            System.out.println("successfully read model");
 
        }/*
        else {
            System.out.println("retrieving model...");
 
            try {
                serverModel = association.retrieveModel();
            } catch (ServiceError e) {
                System.out.println("Service error: " + e.getMessage());
                return;
            } catch (IOException e) {
                System.out.println("Fatal error: " + e.getMessage());
                return;
            }
 
            System.out.println("successfully read model");
        }*/
 
        actionProcessor.addAction(new Action(PRINT_MODEL_KEY, PRINT_MODEL_KEY_DESCRIPTION));
        actionProcessor.addAction(new Action(GET_DATA_VALUES_KEY, GET_DATA_VALUES_KEY_DESCRIPTION));
        actionProcessor.addAction(new Action(READ_ALL_DATA_KEY, READ_ALL_DATA_KEY_DESCRIPTION));
        actionProcessor.addAction(new Action(CREATE_DATA_SET_KEY, CREATE_DATA_SET_KEY_DESCRIPTION));
        actionProcessor.addAction(new Action(DELETE_DATA_SET_KEY, DELETE_DATA_SET_KEY_DESCRIPTION));
        actionProcessor.addAction(new Action(REPORTING_KEY, REPORTING_KEY_DESCRIPTION));
 
        actionProcessor.start();
 
        /*
        writeNodeData("ZJDYBTSE/ncdGGIO1.Para6.setMag.f", "SP");
        readNodeData("ZJDYBTSE/ncdGGIO1.Para6.setMag.f", "SP");
        
        writeNodeData("ZJDYBTSE/ncdGGIO1.SPCSO1.Oper", "CO");
        readNodeData("ZJDYBTSE/ncdGGIO1.SPCSO1.Oper", "CO");*/
    }
}