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
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 org.openmuc.openiec61850.BdaFloat32;
import org.openmuc.openiec61850.BdaBoolean;
import org.openmuc.openiec61850.ClientAssociation;
import org.openmuc.openiec61850.ClientSap;
import org.openmuc.openiec61850.DataSet;
import org.openmuc.openiec61850.Fc;
import org.openmuc.openiec61850.FcModelNode;
import org.openmuc.openiec61850.ServerModel;
import org.openmuc.openiec61850.ServiceError;
import org.openmuc.openiec61850.clientgui.BasicDataBind;
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.IntCliParameter;
import org.openmuc.openiec61850.internal.cli.StringCliParameter;
import org.openmuc.openiec61850.clientgui.databind.BooleanDataBind;
 
 
/**
 *
 * @author Stefan Feuerhahn
 *
 */
public class ConsoleClient_1 {
 
    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;
    
    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();
        
        while(true) {
            
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            try {
                association = clientSap.associate(address, portParam.getValue(), null, null);
            } catch (IOException e) {
                System.out.println("Unable to connect to remote host.");
                continue;
            }
 
            System.out.println("successfully connected");
            System.out.println("retrieving model...");
 
            try {
                serverModel = association.retrieveModel();
            } catch (ServiceError e) {
                System.out.println("Service error: " + e.getMessage());
                continue;
            } catch (IOException e) {
                System.out.println("Fatal error: " + e.getMessage());
                continue;
            }
 
            System.out.println("successfully read model");
 
            /*
            writeNodeData("ZJDYBTSE/ncdGGIO1.Para6.setMag.f", "SP");
            readNodeData("ZJDYBTSE/ncdGGIO1.Para6.setMag.f", "SP");
            */
            //DataSet ds_pm = serverModel.getDataSet("ZJDYBTSE/LLN0.dsParameter");
            //int size_cnt = ds_pm.getMembers().size();
            //m_Association.getDataSetValues(ds_pm);
            /*
            for(int n=0; n<size_cnt; n++) {
                FcModelNode fc_mode = (FcModelNode) ds_pm.getMembers().get(n).getChild("setMag").getChild("f");
                BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fc_mode);
                JTextField tf_t = (JTextField)data.getValueField();
                tf_t.setText(String.valueOf(n+1));
                data.write();
            }
            try {
                association.setDataSetValues(ds_pm);
            } catch (ServiceError e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("done");
            */
            writeNodeData("ZJDYBTSE/ncdGGIO1.SPCSO1.Oper", "CO");
            readNodeData("ZJDYBTSE/ncdGGIO1.SPCSO1.Oper", "CO");
            
            while(true) {
                DataSet ds = serverModel.getDataSet("ZJDYBTSE/LLN0.dsMeasure");
                int size_cnt1 = ds.getMembers().size();
                try {
                    association.getDataSetValues(ds);
                    
                     String str_out = "";
                     for(int n=0; n<size_cnt1; n++) {
                         FcModelNode fc_mode = (FcModelNode) ds.getMembers().get(n).getChild("mag").getChild("f");
                         //BasicDataBind<?> data = new Float32DataBind((BdaFloat32)fc_mode);
                         //((BdaFloat32)fc_mode).getFloat();
                         //JTextField tf_t = (JTextField)data.getValueField();
                         str_out += String.format("#%02d: %1.3f ", n+1, ((BdaFloat32)fc_mode).getFloat());
                         //System.out.println(fc_mode.getReference() + ", the value is: " + tf_t.getText());
                         //System.out.println(fc_mode.getChild("setMag").getChild("f").getReference());
                     }
                     System.out.println(str_out);
                    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    break;
                } finally {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }
}