-
Notifications
You must be signed in to change notification settings - Fork 3
/
NodeData.cs
214 lines (202 loc) · 7.28 KB
/
NodeData.cs
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
/*
* Copyright (C) 2013 Pavel Charvat
*
* This file is part of IEDExplorer.
*
* IEDExplorer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* IEDExplorer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IEDExplorer. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IEDExplorer
{
class NodeData : NodeBase
{
private scsm_MMS_TypeEnum _dataType = scsm_MMS_TypeEnum.structure;
private Object _dataValue = null;
private Object _dataParam = null;
private Object _valueTag = null;
public event EventHandler ValueChanged;
public NodeData(string Name)
: base(Name)
{
}
public scsm_MMS_TypeEnum DataType
{
get
{
return _dataType;
}
set
{
_dataType = value;
}
}
public Object DataValue
{
get
{
lock (this)
return _dataValue;
}
set
{
bool fire = false;
lock (this)
if (_dataValue == null || !_dataValue.Equals(value))
{
_dataValue = value;
fire = true;
}
if (fire && ValueChanged != null)
{
ValueChanged(this, new EventArgs());
}
}
}
public Object DataParam
{
get
{
return _dataParam;
}
set
{
_dataParam = value;
}
}
public object ValueTag
{
get
{
return _valueTag;
}
set
{
_valueTag = value;
}
}
internal override NodeBase FindNodeByValue(scsm_MMS_TypeEnum dataType, object dataValue, ref NodeBase ContinueAfter)
{
if (dataValue == null)
return null;
NodeBase res = null;
if (_childNodes.Count > 0)
{
foreach (NodeBase b in _childNodes)
{
res = b.FindNodeByValue(dataType, dataValue, ref ContinueAfter);
if (res != null && ContinueAfter == null)
return res;
if (res != null && ContinueAfter != null && res == ContinueAfter)
ContinueAfter = null;
}
}
else
{
if (dataType == this.DataType &&
this.DataValue != null && dataValue.ToString() == DataValue.ToString())
{
return this;
}
}
return null;
}
public string StringValue
{
get
{
string val = "";
if (DataValue != null)
{
switch (DataType)
{
case scsm_MMS_TypeEnum.utc_time:
if (DataParam != null)
{
if (((byte)(DataParam) & 0x40) > 0) // TimQualTimeBaseErr
val = DataValue.ToString() + "." + ((DateTime)(DataValue)).Millisecond.ToString() + " TimQualTimeBaseErr";
else
val = DataValue.ToString() + "." + ((DateTime)(DataValue)).Millisecond.ToString();
}
break;
case scsm_MMS_TypeEnum.bit_string:
if (DataParam != null)
{
byte[] bbval = (byte[])DataValue;
int blen = bbval.Length;
int trail = (int)DataParam;
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < blen * 8 - trail; i++)
{
if (((bbval[(i / 8)] << (i % 8)) & 0x80) > 0)
sb.Append(1); //.Insert(0, 1);
else
sb.Append(0); //.Insert(0, 0);
}
val = sb.ToString();
}
break;
default:
val = DataValue.ToString();
break;
}
}
return val;
}
set
{
if (value != null && value != "")
{
switch (DataType)
{
case scsm_MMS_TypeEnum.utc_time:
// Not supported
break;
case scsm_MMS_TypeEnum.bit_string:
byte[] bbval = (byte[])DataValue;
int blen = bbval.Length;
int trail = (int)DataParam;
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < blen * 8 - trail; i++)
{
if (((bbval[(i / 8)] << (i % 8)) & 0x80) > 0)
sb.Append(1); //.Insert(0, 1);
else
sb.Append(0); //.Insert(0, 0);
}
//val = sb.ToString();
break;
case scsm_MMS_TypeEnum.boolean:
if (value.StartsWith("0") || value.StartsWith("f", StringComparison.CurrentCultureIgnoreCase))
DataValue = false;
if (value.StartsWith("1") || value.StartsWith("t", StringComparison.CurrentCultureIgnoreCase))
DataValue = true;
break;
case scsm_MMS_TypeEnum.visible_string:
DataValue = value;
break;
case scsm_MMS_TypeEnum.octet_string:
DataValue = Encoding.ASCII.GetBytes(value);
break;
default:
//val = DataValue.ToString();
break;
}
}
}
}
}
}