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
/*
 * Copyright 2011-17 Fraunhofer ISE, energy & meteo Systems GmbH and other contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.openmuc.openiec61850;
 
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
import org.openmuc.openiec61850.internal.mms.asn1.AlternateAccess;
import org.openmuc.openiec61850.internal.mms.asn1.AlternateAccessSelection;
import org.openmuc.openiec61850.internal.mms.asn1.AlternateAccessSelection.SelectAccess;
import org.openmuc.openiec61850.internal.mms.asn1.AlternateAccessSelection.SelectAccess.Component;
import org.openmuc.openiec61850.internal.mms.asn1.BasicIdentifier;
import org.openmuc.openiec61850.internal.mms.asn1.Identifier;
import org.openmuc.openiec61850.internal.mms.asn1.ObjectName;
import org.openmuc.openiec61850.internal.mms.asn1.Unsigned32;
import org.openmuc.openiec61850.internal.mms.asn1.VariableDefs;
import org.openmuc.openiec61850.internal.mms.asn1.VariableSpecification;
 
public abstract class FcModelNode extends ModelNode {
 
    private VariableDefs.SEQUENCE variableDef = null;
    Fc fc;
    private ServerAssociation selected = null;
    private TimerTask task = null;
 
    public Fc getFc() {
        return fc;
    }
 
    boolean select(ServerAssociation association, Timer timer) {
        if (selected != null) {
            if (selected != association) {
                return false;
            }
        }
        else {
            selected = association;
            association.selects.add(this);
        }
 
        ModelNode sboTimeoutNode = association.serverModel.findModelNode(objectReference, Fc.CF).getChild("sboTimeout");
 
        if (sboTimeoutNode == null) {
            return true;
        }
 
        long sboTimeout = ((BdaInt32U) sboTimeoutNode).getValue();
 
        if (sboTimeout == 0) {
            return true;
        }
 
        class SelectResetTask extends TimerTask {
            ServerAssociation association;
 
            SelectResetTask(ServerAssociation association) {
                this.association = association;
            }
 
            @Override
            public void run() {
                synchronized (association.serverModel) {
                    if (task == this) {
                        task = null;
                        deselectAndRemove(association);
                    }
                }
            }
        }
 
        if (task != null) {
            task.cancel();
        }
 
        task = new SelectResetTask(association);
        timer.schedule(task, sboTimeout);
 
        return true;
 
    }
 
    void deselectAndRemove(ServerAssociation association) {
        selected = null;
        if (task != null) {
            task.cancel();
            task = null;
        }
        association.selects.remove(this);
    }
 
    void deselect() {
        selected = null;
        if (task != null) {
            task.cancel();
            task = null;
        }
    }
 
    boolean isSelected() {
        if (selected == null) {
            return false;
        }
        return true;
    }
 
    boolean isSelectedBy(ServerAssociation association) {
        if (selected == association) {
            return true;
        }
        return false;
    }
 
    VariableDefs.SEQUENCE getMmsVariableDef() {
 
        if (variableDef != null) {
            return variableDef;
        }
 
        AlternateAccess alternateAccess = null;
 
        StringBuilder preArrayIndexItemId = new StringBuilder(objectReference.get(1));
        preArrayIndexItemId.append("$");
        preArrayIndexItemId.append(fc);
 
        int arrayIndexPosition = objectReference.getArrayIndexPosition();
        if (arrayIndexPosition != -1) {
 
            for (int i = 2; i < arrayIndexPosition; i++) {
                preArrayIndexItemId.append("$");
                preArrayIndexItemId.append(objectReference.get(i));
            }
 
            alternateAccess = new AlternateAccess();
            List<AlternateAccess.CHOICE> subSeqOfAlternateAccess = alternateAccess.getCHOICE();
            Unsigned32 indexBerInteger = new Unsigned32(Integer.parseInt(objectReference.get(arrayIndexPosition)));
 
            if (arrayIndexPosition < (objectReference.size() - 1)) {
                // this reference points to a sub-node of an array element
 
                StringBuilder postArrayIndexItemId = new StringBuilder(objectReference.get(arrayIndexPosition + 1));
 
                for (int i = (arrayIndexPosition + 2); i < objectReference.size(); i++) {
                    postArrayIndexItemId.append("$");
                    postArrayIndexItemId.append(objectReference.get(i));
                }
 
                BasicIdentifier subIndexReference = new BasicIdentifier(postArrayIndexItemId.toString().getBytes());
 
                AlternateAccessSelection.SelectAccess subIndexReferenceSelectAccess = new AlternateAccessSelection.SelectAccess();
                Component component = new Component();
                component.setBasic(subIndexReference);
                subIndexReferenceSelectAccess.setComponent(component);
 
                AlternateAccessSelection subIndexReferenceAlternateAccessSelection = new AlternateAccessSelection();
                subIndexReferenceAlternateAccessSelection.setSelectAccess(subIndexReferenceSelectAccess);
 
                AlternateAccess.CHOICE subIndexReferenceAlternateAccessSubChoice = new AlternateAccess.CHOICE();
                subIndexReferenceAlternateAccessSubChoice.setUnnamed(subIndexReferenceAlternateAccessSelection);
 
                AlternateAccess subIndexReferenceAlternateAccess = new AlternateAccess();
 
                List<AlternateAccess.CHOICE> subIndexReferenceAlternateAccessSubSeqOf = subIndexReferenceAlternateAccess
                        .getCHOICE();
                subIndexReferenceAlternateAccessSubSeqOf.add(subIndexReferenceAlternateAccessSubChoice);
 
                // set array index:
 
                AlternateAccessSelection.SelectAlternateAccess.AccessSelection indexAccessSelectionChoice = new AlternateAccessSelection.SelectAlternateAccess.AccessSelection();
                indexAccessSelectionChoice.setIndex(indexBerInteger);
 
                AlternateAccessSelection.SelectAlternateAccess indexAndLowerReferenceSelectAlternateAccess = new AlternateAccessSelection.SelectAlternateAccess();
                indexAndLowerReferenceSelectAlternateAccess.setAccessSelection(indexAccessSelectionChoice);
                indexAndLowerReferenceSelectAlternateAccess.setAlternateAccess(subIndexReferenceAlternateAccess);
 
                AlternateAccessSelection indexAndLowerReferenceAlternateAccessSelection = new AlternateAccessSelection();
                indexAndLowerReferenceAlternateAccessSelection
                        .setSelectAlternateAccess(indexAndLowerReferenceSelectAlternateAccess);
 
                AlternateAccess.CHOICE indexAndLowerReferenceAlternateAccessChoice = new AlternateAccess.CHOICE();
                indexAndLowerReferenceAlternateAccessChoice.setUnnamed(indexAndLowerReferenceAlternateAccessSelection);
 
                subSeqOfAlternateAccess.add(indexAndLowerReferenceAlternateAccessChoice);
 
            }
            else {
                SelectAccess selectAccess = new SelectAccess();
                selectAccess.setIndex(indexBerInteger);
 
                AlternateAccessSelection alternateAccessSelection = new AlternateAccessSelection();
                alternateAccessSelection.setSelectAccess(selectAccess);
 
                AlternateAccess.CHOICE alternateAccessChoice = new AlternateAccess.CHOICE();
                alternateAccessChoice.setUnnamed(alternateAccessSelection);
 
                subSeqOfAlternateAccess.add(alternateAccessChoice);
            }
 
        }
        else {
 
            for (int i = 2; i < objectReference.size(); i++) {
                preArrayIndexItemId.append("$");
                preArrayIndexItemId.append(objectReference.get(i));
            }
        }
 
        ObjectName.DomainSpecific domainSpecificObjectName = new ObjectName.DomainSpecific();
        domainSpecificObjectName.setDomainID(new Identifier(objectReference.get(0).getBytes()));
        domainSpecificObjectName.setItemID(new Identifier(preArrayIndexItemId.toString().getBytes()));
 
        ObjectName objectName = new ObjectName();
        objectName.setDomainSpecific(domainSpecificObjectName);
 
        VariableSpecification varSpec = new VariableSpecification();
        varSpec.setName(objectName);
 
        variableDef = new VariableDefs.SEQUENCE();
        variableDef.setAlternateAccess(alternateAccess);
        variableDef.setVariableSpecification(varSpec);
 
        return variableDef;
    }
 
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getReference().toString()).append(" [").append(fc).append("]");
        for (ModelNode childNode : children.values()) {
            sb.append("\n");
            sb.append(childNode.toString());
        }
        return sb.toString();
    }
 
}