1
2
3
4
5 package org.epics.pvmanager.sim;
6
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.concurrent.ScheduledExecutorService;
11 import java.util.concurrent.TimeUnit;
12 import org.epics.pvmanager.ChannelWriteCallback;
13 import org.epics.pvmanager.MultiplexedChannelHandler;
14 import org.epics.pvmanager.util.FunctionParser;
15 import org.epics.vtype.ValueFactory;
16
17
18
19
20
21 class DelayedConnectionChannelHandler extends MultiplexedChannelHandler<Object, Object> {
22
23 private final Object initialValue;
24 private final double delayInSeconds;
25 private final ScheduledExecutorService exec;
26
27 DelayedConnectionChannelHandler(String channelName, ScheduledExecutorService exec) {
28 super(channelName);
29 String errorMessage = "Incorrect syntax. Must match delayedConnectionChannel(delayInSeconds, value)";
30 List<Object> tokens = FunctionParser.parseFunctionAnyParameter(channelName);
31 if (tokens == null || tokens.size() <= 1) {
32 throw new IllegalArgumentException(errorMessage);
33 }
34 if (tokens.size() == 2) {
35 initialValue = "Initial value";
36 } else {
37 Object value = FunctionParser.asScalarOrList(tokens.subList(2, tokens.size()));
38 if (value == null) {
39 throw new IllegalArgumentException(errorMessage);
40 }
41 initialValue = ValueFactory.toVTypeChecked(value);
42 }
43 delayInSeconds = (Double) tokens.get(1);
44 this.exec = exec;
45 }
46
47 @Override
48 public void connect() {
49 exec.schedule(new Runnable() {
50
51 @Override
52 public void run() {
53 synchronized(DelayedConnectionChannelHandler.this) {
54 if (getUsageCounter() > 0) {
55 processConnection(new Object());
56 processMessage(initialValue);
57 }
58 }
59 }
60 }, (long) delayInSeconds * 1000, TimeUnit.MILLISECONDS);
61 }
62
63 @Override
64 public void disconnect() {
65 processConnection(null);
66 }
67
68 @Override
69 public Map<String, Object> getProperties() {
70 Map<String, Object> result = new HashMap<>();
71 result.put("delayInSeconds", delayInSeconds);
72 result.put("initialValue", initialValue);
73 return result;
74 }
75
76 @Override
77 public void write(Object newValue, ChannelWriteCallback callback) {
78 throw new UnsupportedOperationException("Can't write to simulation channel.");
79 }
80
81 }