Skip to content

Conversation

rerpha
Copy link
Contributor

@rerpha rerpha commented Sep 23, 2025

Closes #8

@rerpha rerpha changed the title 8 get endpoint Add GET endpoint for one-shot pv value reads Sep 23, 2025
@rerpha rerpha marked this pull request as ready for review September 24, 2025 01:49
Copy link
Collaborator

@georgweiss georgweiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Some remarks...

Retrying a read read operation is not the preferred pattern. Instead you could consider something like

 CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference<VType> value = new AtomicReference<>(null);
        try {
            PV pv = PVPool.getPV(name);
            pv.onValueEvent().subscribe(vtype -> {
                if (!VTypeHelper.isDisconnected(vtype)) {
                    value.set(pv.read());
                }
               countDownLatch.countDown();
            });
            countDownLatch.await(5000, TimeUnit.MILLISECONDS);
            PVPool.releasePV(pv);
            if(value.get() == null){
                return null;
            }
            return Vtype2Json.toJson(name, value.get(), null, true, true);
        } catch (Exception e) {
            // Do something sensible here to avoid HTTP 500 status
        }

The above connects to the PV and reads the value when connected. If the PV is offline - or does not exist - the code waits at most 5000 ms and then proceeds.
Also, returning a non-JSON formatted string forces the client to inspect the response. So returning null (=empty body) may be the preferred option.

@rerpha
Copy link
Contributor Author

rerpha commented Sep 24, 2025

Thanks for the PR.

Some remarks...

Retrying a read read operation is not the preferred pattern. Instead you could consider something like

 CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicReference<VType> value = new AtomicReference<>(null);
        try {
            PV pv = PVPool.getPV(name);
            pv.onValueEvent().subscribe(vtype -> {
                if (!VTypeHelper.isDisconnected(vtype)) {
                    value.set(pv.read());
                }
               countDownLatch.countDown();
            });
            countDownLatch.await(5000, TimeUnit.MILLISECONDS);
            PVPool.releasePV(pv);
            if(value.get() == null){
                return null;
            }
            return Vtype2Json.toJson(name, value.get(), null, true, true);
        } catch (Exception e) {
            // Do something sensible here to avoid HTTP 500 status
        }

The above connects to the PV and reads the value when connected. If the PV is offline - or does not exist - the code waits at most 5000 ms and then proceeds. Also, returning a non-JSON formatted string forces the client to inspect the response. So returning null (=empty body) may be the preferred option.

Thanks - will implement. I wasn't sure if retrying was the best approach, seemed messy!

@rerpha
Copy link
Contributor Author

rerpha commented Sep 24, 2025

ok - all done I think @georgweiss - let me know what you think

@georgweiss georgweiss merged commit 70edbb1 into ControlSystemStudio:master Sep 25, 2025
4 checks passed
@rerpha rerpha deleted the 8_get_endpoint branch September 25, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add GET endpoint for arbritrary one-shot value
2 participants