-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTag.java
More file actions
executable file
·429 lines (391 loc) · 13.9 KB
/
Copy pathTag.java
File metadata and controls
executable file
·429 lines (391 loc) · 13.9 KB
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
package javaforce.controls;
import java.util.*;
import javaforce.*;
/** Monitors a PLC Tag.
*
* Auto-reconnects when disconnects.
*
* @author pquiring
*/
public class Tag {
/** Host (usually IP Address) */
public String host;
/** Type of host (S7, AB, MB, NI, MIC) See ControllerType */
public int type;
/** Tag name. */
public String tag;
/** Size of tag. See TagType */
public int size;
/** Color of tag (for reporting) */
public int color;
/** int min/max values (for reporting) */
public int min, max;
/** float min/max values (for reporting) */
public float fmin, fmax;
/** Speed to poll data (delay = ms delay between polls) (min = 25ms) */
public int delay;
/** User defined description */
public String desc;
private byte[] pending;
private Object pendingLock = new Object();
/** Get user data. */
public Object getData(String key) {
return user.get(key);
}
/** Set user data. */
public void setData(String key, Object value) {
user.put(key, value);
}
/** Set host,type,tag,size,delay(ms). */
public void setTag(String host, int type, String tag, int size, int delay) {
this.host = host;
this.type = type;
this.tag = tag;
this.size = size;
this.delay = delay;
}
public boolean isValid() {
if (size == TagType.unknown) return false;
if (type == ControllerType.UNKNOWN) return false;
if (host == null || host.length() == 0) return false;
if (host.equalsIgnoreCase("null")) return false;
return true;
}
private Controller c;
private String socks;
private Timer timer;
private Reader reader;
private TagListener listener;
private HashMap<String, Object> user = new HashMap<String, Object>();
private Tag parent;
private int childIdx;
private Object lock = new Object();
private ArrayList<Tag> children = new ArrayList<Tag>();
private byte[][] childData;
private ArrayList<Tag> queue = new ArrayList<Tag>();
private boolean multiRead = true;
/** Returns true if data type is float32 or float64 */
public boolean isFloat() {
return size == TagType.float32 || size == TagType.float64;
}
/** Returns true is controller is Big Endian byte order. */
public boolean isBE() {
if (parent != null) {
return parent.c.isBE();
}
return c.isBE();
}
/** Returns true is controller is Little Endian byte order. */
public boolean isLE() {
if (parent != null) {
return parent.c.isLE();
}
return c.isLE();
}
/** Enables reading multiple tags in one request (currently only S7 supported) */
public void setMultiRead(boolean state) {
if (type != ControllerType.S7) return;
if (true) return; //TODO!!!
multiRead = state;
}
/** Adds a child tag and returns index. */
public int addChild(Tag child) {
children.add(child);
return children.size() - 1;
}
private void queue(Tag tag) {
synchronized(queue) {
queue.add(tag);
}
}
/** Returns # of bytes tag uses. */
public int getSize() {
switch (size) {
case TagType.bit: return 1;
case TagType.int8: return 1;
case TagType.int16: return 2;
case TagType.int32: return 4;
case TagType.int64: return 8;
case TagType.uint8: return 1;
case TagType.uint16: return 2;
case TagType.uint32: return 4;
case TagType.uint64: return 8;
case TagType.float32: return 4;
case TagType.float64: return 8;
}
return 0;
}
public String getURL() {
switch (type) {
case ControllerType.JF: return "JF:" + host;
case ControllerType.S7: return "S7:" + host;
case ControllerType.AB: return "AB:" + host;
case ControllerType.MB: return "MB:" + host;
case ControllerType.MIC: return "MIC:" + host;
}
JFLog.log("Tag:Error:type unknown:" + type);
return null;
}
public Controller getController() {
if (parent != null) {
return parent.c;
} else {
return c;
}
}
public void setListener(TagListener listener) {
this.listener = listener;
}
public String toString() {
if (!isValid()) return "not set";
if (type == ControllerType.MIC) {
return "MIC:" + host;
}
if (desc != null && desc.length() > 0) {
return tag + " - " + desc;
}
return tag;
}
public String getmin() {
if (isFloat()) {
return Float.toString(fmin);
} else {
return Integer.toString(min);
}
}
public String getmax() {
if (isFloat()) {
return Float.toString(fmax);
} else {
return Integer.toString(max);
}
}
private boolean startTimer() {
if (parent == null) {
childData = null;
c = new Controller();
if (socks != null) {
c.setSOCKS(socks);
}
} else {
c = null;
}
timer = new Timer();
reader = new Reader();
reader.tag = this;
if (delay < 25) delay = 25;
timer.scheduleAtFixedRate(reader, delay, delay);
return true;
}
/** Start reading tag at interval (delay). */
public boolean start() {
parent = null;
return startTimer();
}
/** Start reading tag at interval (delay) using another Tags connection. */
public boolean start(Tag parent) {
this.parent = parent;
if (parent != null) {
if (parent.type != type) return false;
childIdx = parent.addChild(this);
}
return startTimer();
}
/** Stop monitoring tag value. */
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
if (reader != null) {
reader = null;
}
disconnect();
}
public void setSOCKS(String socksHost) {
socks = socksHost;
}
public boolean connect() {
if (parent != null) return false; //wait for parent to connect
if (c.connect(getURL())) return true;
return false;
}
public void disconnect() {
if (parent != null) {
parent = null;
return;
}
if (c != null) {
c.disconnect();
c = null;
}
children.clear();
}
private String value = "0";
/** Returns current value (only valid if start() has been called). */
public String getValue() {
return value;
}
/** Queues pending data to be written on next cycle. (only valid if start() has been called). */
public void setValue(String value) {
byte[] data = null;
if (isBE()) {
switch (size) {
case TagType.bit: data = new byte[] {(byte)(value.equals("0") ? 0 : 1)}; break;
case TagType.int8: data = new byte[] {Byte.valueOf(value)}; break;
case TagType.int16: data = new byte[2]; BE.setuint16(data, 0, Short.valueOf(value)); break;
case TagType.int32: data = new byte[4]; BE.setuint32(data, 0, Integer.valueOf(value)); break;
case TagType.int64: data = new byte[8]; BE.setuint64(data, 0, Long.valueOf(value)); break;
case TagType.uint8: data = new byte[] {Byte.valueOf(value)}; break;
case TagType.uint16: data = new byte[2]; BE.setuint16(data, 0, Short.valueOf(value)); break;
case TagType.uint32: data = new byte[4]; BE.setuint32(data, 0, Integer.valueOf(value)); break;
case TagType.uint64: data = new byte[8]; BE.setuint64(data, 0, Long.valueOf(value)); break;
case TagType.float32: data = new byte[4]; BE.setuint32(data, 0, Float.floatToIntBits(Float.valueOf(value))); break;
case TagType.float64: data = new byte[4]; BE.setuint64(data, 0, Double.doubleToLongBits(Double.valueOf(value))); break;
}
} else {
switch (size) {
case TagType.bit: data = new byte[] {(byte)(value.equals("0") ? 0 : 1)}; break;
case TagType.int8: data = new byte[] {Byte.valueOf(value)}; break;
case TagType.int16: data = new byte[2]; LE.setuint16(data, 0, Short.valueOf(value)); break;
case TagType.int32: data = new byte[4]; LE.setuint32(data, 0, Integer.valueOf(value)); break;
case TagType.int64: data = new byte[8]; LE.setuint64(data, 0, Long.valueOf(value)); break;
case TagType.uint8: data = new byte[] {Byte.valueOf(value)}; break;
case TagType.uint16: data = new byte[2]; LE.setuint16(data, 0, Short.valueOf(value)); break;
case TagType.uint32: data = new byte[4]; LE.setuint32(data, 0, Integer.valueOf(value)); break;
case TagType.uint64: data = new byte[8]; LE.setuint64(data, 0, Long.valueOf(value)); break;
case TagType.float32: data = new byte[4]; LE.setuint32(data, 0, Float.floatToIntBits(Float.valueOf(value))); break;
case TagType.float64: data = new byte[4]; LE.setuint64(data, 0, Double.doubleToLongBits(Double.valueOf(value))); break;
}
}
synchronized(pendingLock) {
pending = data;
}
}
/** Returns current value as int (only valid if start() has been called). */
public int intValue() {
return Integer.valueOf(value);
}
/** Returns current value as float (only valid if start() has been called). */
public float floatValue() {
return Float.valueOf(value);
}
/** Returns current value as double (float64) (only valid if start() has been called). */
public double doubleValue() {
return Double.valueOf(value);
}
/** Reads value directly. */
public byte[] read() {
if (parent != null) {
if (parent.c == null) return null;
if (multiRead) {
return parent.read(childIdx);
} else {
//queue read with parent to prevent some threads from starving
synchronized(lock) {
parent.queue(this);
try {lock.wait();} catch (Exception e) {}
return parent.c.read(tag);
}
}
} else {
if (multiRead && type == ControllerType.S7 && children.size() > 0) {
int cnt = children.size();
String[] tags = new String[cnt+1];
tags[cnt] = tag;
for(int a=0;a<cnt;a++) {
tags[a] = children.get(a).tag;
}
childData = c.read(tags);
if (childData == null) return null;
return childData[cnt];
} else {
//allow queued children to proceed
synchronized(queue) {
while (queue.size() > 0) {
Tag child = queue.remove(0);
synchronized(child.lock) {
child.lock.notify();
}
}
}
return c.read(tag);
}
}
}
/** Writes data to tag. */
public void write(byte[] data) {
if (parent != null) {
if (parent.c == null) return;
parent.c.write(tag, data);
} else {
c.write(tag, data);
}
}
private byte[] read(int idx) {
if (childData == null || idx >= childData.length) return null;
return childData[idx];
}
private static class Reader extends TimerTask {
public Tag tag;
public byte[] data;
public void run() {
try {
String lastValue = tag.value;
if (tag.parent == null) {
if (!tag.c.isConnected()) {
if (!tag.connect()) {
return;
}
}
}
data = tag.read();
if (data == null) {
//JFLog.log("Error:TagRead:data==null:host=" + tag.host + ":tag=" + tag.tag);
return;
}
if (tag.isBE()) {
switch (tag.size) {
case TagType.bit: tag.value = data[0] == 0 ? "0" : "1"; break;
case TagType.int8: tag.value = Byte.toString(data[0]); break;
case TagType.int16: tag.value = Short.toString((short)BE.getuint16(data, 0)); break;
case TagType.int32: tag.value = Integer.toString(BE.getuint32(data, 0)); break;
case TagType.int64: tag.value = Long.toString(BE.getuint64(data, 0)); break;
case TagType.uint8: tag.value = Integer.toUnsignedString(data[0] & 0xff); break;
case TagType.uint16: tag.value = Integer.toUnsignedString(BE.getuint16(data, 0) & 0xffff); break;
case TagType.uint32: tag.value = Integer.toUnsignedString(BE.getuint32(data, 0)); break;
case TagType.uint64: tag.value = Long.toUnsignedString(BE.getuint64(data, 0)); break;
case TagType.float32: tag.value = Float.toString(Float.intBitsToFloat(BE.getuint32(data, 0))); break;
case TagType.float64: tag.value = Double.toString(Double.longBitsToDouble(BE.getuint64(data, 0))); break;
}
} else {
switch (tag.size) {
case TagType.bit: tag.value = data[0] == 0 ? "0" : "1"; break;
case TagType.int8: tag.value = Byte.toString(data[0]); break;
case TagType.int16: tag.value = Short.toString((short)LE.getuint16(data, 0)); break;
case TagType.int32: tag.value = Integer.toString(LE.getuint32(data, 0)); break;
case TagType.int64: tag.value = Long.toString(LE.getuint64(data, 0)); break;
case TagType.uint8: tag.value = Integer.toUnsignedString(data[0] & 0xff); break;
case TagType.uint16: tag.value = Integer.toUnsignedString(LE.getuint16(data, 0) & 0xffff); break;
case TagType.uint32: tag.value = Integer.toUnsignedString(LE.getuint32(data, 0)); break;
case TagType.uint64: tag.value = Long.toUnsignedString(LE.getuint64(data, 0)); break;
case TagType.float32: tag.value = Float.toString(Float.intBitsToFloat(LE.getuint32(data, 0))); break;
case TagType.float64: tag.value = Double.toString(Double.longBitsToDouble(LE.getuint64(data, 0))); break;
}
}
synchronized(tag.pendingLock) {
if (tag.pending != null) {
tag.write(tag.pending);
tag.pending = null;
}
}
if (tag.listener == null) return;
if (lastValue == null || !tag.value.equals(lastValue)) {
tag.listener.tagChanged(tag, tag.value);
}
} catch (Exception e) {
JFLog.log(e);
}
}
}
}