Skip to content

Commit f0b3545

Browse files
committed
move to rest controller, use isDisconnected() waiter, add configurable PV_READ_TIMEOUT
1 parent 9bbc647 commit f0b3545

File tree

5 files changed

+39
-41
lines changed

5 files changed

+39
-41
lines changed

src/main/java/org/phoebus/pvws/EpicsConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class EpicsConfiguration {
6161
@Value("${PV_WRITE_SUPPORT:true}")
6262
private String pvWriteSupport;
6363

64+
@Value("${PV_READ_TIMEOUT:5000}")
65+
private String pvReadTimeout;
66+
6467
@PostConstruct
6568
public void init() {
6669
logger.log(Level.INFO, "===========================================");
@@ -89,6 +92,7 @@ else if (pvDefaultType != null && !pvDefaultType.isEmpty()) {
8992
System.setProperty("PV_THROTTLE_MS", pvThrottleMs);
9093
System.setProperty("PV_ARRAY_THROTTLE_MS", pvArrayThrottleMs);
9194
System.setProperty("PV_WRITE_SUPPORT", pvWriteSupport);
95+
System.setProperty("PV_READ_TIMEOUT", pvReadTimeout);
9296

9397
// Configure JCA/CAJ to use environment vars, not java properties or preferences
9498
System.setProperty("jca.use_env", "true");
@@ -98,6 +102,7 @@ else if (pvDefaultType != null && !pvDefaultType.isEmpty()) {
98102
logger.log(Level.INFO, "EPICS_CA_MAX_ARRAY_BYTES=" + System.getenv("EPICS_CA_MAX_ARRAY_BYTES"));
99103
logger.log(Level.INFO, "EPICS_PVA_ADDR_LIST=" + epicsPvaAddrList);
100104
logger.log(Level.INFO, "EPICS_PVA_AUTO_ADDR_LIST=" + epicsPvaAutoAddrList);
105+
logger.log(Level.INFO, "PV_READ_TIMEOUT=" + pvReadTimeout);
101106

102107
logger.log(Level.INFO, "===========================================");
103108

src/main/java/org/phoebus/pvws/EpicsWebSocketServerApplication.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,20 @@
1818

1919
package org.phoebus.pvws;
2020

21-
import org.epics.vtype.VType;
2221
import org.phoebus.pv.PV;
2322
import org.phoebus.pv.PVPool;
2423
import org.phoebus.pv.RefCountMap;
25-
import org.phoebus.pvws.ws.Vtype2Json;
2624
import org.phoebus.pvws.ws.WebSocket;
27-
import org.phoebus.pvws.ws.WebSocketPV;
2825
import org.springframework.boot.SpringApplication;
2926
import org.springframework.boot.autoconfigure.SpringBootApplication;
3027
import org.springframework.boot.builder.SpringApplicationBuilder;
3128
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
3229
import org.springframework.context.ApplicationContext;
33-
import org.springframework.http.MediaType;
34-
import org.springframework.web.bind.annotation.GetMapping;
35-
import org.springframework.web.bind.annotation.RequestParam;
36-
import org.springframework.web.bind.annotation.RestController;
3730

3831

3932
import java.util.List;
4033

4134
@SpringBootApplication
42-
@RestController
4335
public class EpicsWebSocketServerApplication extends SpringBootServletInitializer {
4436

4537
public static void main(String[] args) {
@@ -62,26 +54,4 @@ public static void main(String[] args) {
6254
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
6355
return application.sources(EpicsWebSocketServerApplication.class);
6456
}
65-
66-
@GetMapping(value= "/pvget", produces = MediaType.APPLICATION_JSON_VALUE)
67-
public String pvget(@RequestParam String name) {
68-
final WebSocketPV pv = new WebSocketPV(name, null);
69-
String ret;
70-
try {
71-
int maxAttempts = 100;
72-
int retryDelay = 50;
73-
74-
VType lastValue = null;
75-
for (int i = 0; i< maxAttempts; i++) {
76-
Thread.sleep(retryDelay);
77-
lastValue = pv.get();
78-
if (lastValue != null) break;
79-
}
80-
ret = Vtype2Json.toJson(name, lastValue, null, true, true);
81-
} catch (final Exception ex) {
82-
ret = String.format("Unable to get PV value for %s - exception %s", name, ex);
83-
}
84-
pv.dispose();
85-
return ret;
86-
}
8757
}

src/main/java/org/phoebus/pvws/controllers/PvwsRestController.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@
2222
import org.epics.util.array.ListInteger;
2323
import org.epics.vtype.Array;
2424
import org.epics.vtype.VType;
25+
import org.phoebus.core.vtypes.VTypeHelper;
2526
import org.phoebus.pv.PV;
2627
import org.phoebus.pv.PVPool;
2728
import org.phoebus.pv.RefCountMap;
2829
import org.phoebus.pvws.model.*;
30+
import org.phoebus.pvws.ws.Vtype2Json;
2931
import org.phoebus.pvws.ws.WebSocket;
3032
import org.phoebus.pvws.ws.WebSocketPV;
3133
import org.phoebus.util.time.TimestampFormats;
3234
import org.springframework.beans.factory.annotation.Autowired;
35+
import org.springframework.http.MediaType;
3336
import org.springframework.web.bind.annotation.*;
3437

3538
import java.time.Instant;
3639
import java.util.ArrayList;
3740
import java.util.List;
41+
import java.util.concurrent.CountDownLatch;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.atomic.AtomicReference;
3844
import java.util.logging.Level;
3945
import java.util.logging.Logger;
4046

@@ -146,4 +152,30 @@ public InfoData info(@RequestParam(name = "env", defaultValue = "false") boolean
146152
}
147153
return infoData;
148154
}
155+
156+
@GetMapping(value= "/pvget", produces = MediaType.APPLICATION_JSON_VALUE)
157+
public String pvget(@RequestParam String name) {
158+
CountDownLatch countDownLatch = new CountDownLatch(1);
159+
AtomicReference<VType> value = new AtomicReference<>(null);
160+
try {
161+
int pvReadTimeout = Integer.parseInt(System.getProperty("PV_READ_TIMEOUT"));
162+
PV pv = PVPool.getPV(name);
163+
pv.onValueEvent().subscribe(vtype -> {
164+
if (!VTypeHelper.isDisconnected(vtype)) {
165+
value.set(pv.read());
166+
}
167+
countDownLatch.countDown();
168+
});
169+
countDownLatch.await(pvReadTimeout, TimeUnit.MILLISECONDS);
170+
PVPool.releasePV(pv);
171+
if(value.get() == null){
172+
logger.info("PV " + name + " never connected.");
173+
return null;
174+
}
175+
return Vtype2Json.toJson(name, value.get(), null, true, true);
176+
} catch (Exception e) {
177+
logger.warning("Exception when reading PV " + name + " Exception: " + e);
178+
return null;
179+
}
180+
}
149181
}

src/main/java/org/phoebus/pvws/ws/WebSocketPV.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,6 @@ public void write(Object new_value) throws Exception
169169
throw new Exception("PV_WRITE_SUPPORT is disabled");
170170
}
171171

172-
/**
173-
* Get current value by calling read() on the PV
174-
* @return the current value
175-
* @throws Exception on error
176-
*/
177-
public VType get() throws Exception
178-
{
179-
pv = PVPool.getPV(name);
180-
return pv.read();
181-
}
182-
183172
/** Close PV */
184173
public void dispose()
185174
{

src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ EPICS_PVA_ADDR_LIST=
1414
EPICS_PVA_AUTO_ADDR_LIST=YES
1515
EPICS_PVA_BROADCAST_PORT=5076
1616
EPICS_PVA_NAME_SERVERS=
17+
18+
PV_READ_TIMEOUT=5000

0 commit comments

Comments
 (0)