1818
1919package org .phoebus .pvws ;
2020
21+ import org .epics .vtype .VType ;
2122import org .phoebus .pv .PV ;
2223import org .phoebus .pv .PVPool ;
2324import org .phoebus .pv .RefCountMap ;
25+ import org .phoebus .pvws .ws .Vtype2Json ;
2426import org .phoebus .pvws .ws .WebSocket ;
27+ import org .phoebus .pvws .ws .WebSocketPV ;
2528import org .springframework .boot .SpringApplication ;
2629import org .springframework .boot .autoconfigure .SpringBootApplication ;
2730import org .springframework .boot .builder .SpringApplicationBuilder ;
2831import org .springframework .boot .web .servlet .support .SpringBootServletInitializer ;
2932import 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 ;
37+
3038
3139import java .util .List ;
3240
3341@ SpringBootApplication
42+ @ RestController
3443public class EpicsWebSocketServerApplication extends SpringBootServletInitializer {
3544
3645 public static void main (String [] args ) {
@@ -53,4 +62,32 @@ public static void main(String[] args) {
5362 protected SpringApplicationBuilder configure (SpringApplicationBuilder application ) {
5463 return application .sources (EpicsWebSocketServerApplication .class );
5564 }
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+
72+ // TODO make these configurable?
73+ int maxAttempts = 100 ;
74+ int retryDelay = 50 ;
75+
76+ VType lastValue = null ;
77+
78+ for (int i = 0 ; i < maxAttempts ; i ++) {
79+ Thread .sleep (retryDelay );
80+ lastValue = pv .get ();
81+ if (lastValue != null ) break ;
82+ System .out .println ("value is" + lastValue + "attempt no " + i );
83+ }
84+
85+ System .out .println ("value is" + lastValue );
86+ ret = Vtype2Json .toJson (name , lastValue , null , true , true );
87+ } catch (final Exception ex ) {
88+ ret = String .format ("Unable to get PV value for %s - exception %s" , name , ex );
89+ }
90+ pv .dispose ();
91+ return ret ;
92+ }
5693}
0 commit comments