-
-
Notifications
You must be signed in to change notification settings - Fork 8
feat(reolink): detect a motorised lens instead of assuming static cameras have none #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joseg20
wants to merge
3
commits into
pyronear:develop
Choose a base branch
from
joseg20:feat/motorised-lens-detection
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Copyright (C) 2022-2026, Pyronear. | ||
|
|
||
| # This program is licensed under the Apache License 2.0. | ||
| # See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
|
||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from pyro_camera_api.camera.adapters.reolink import ReolinkCamera | ||
|
|
||
| POST = "pyro_camera_api.camera.adapters.reolink.requests.post" | ||
|
|
||
|
|
||
| def _reply(status=200, code=0, zoom_pos=0): | ||
| """A GetZoomFocus response as the camera would send it.""" | ||
| resp = MagicMock() | ||
| resp.status_code = status | ||
| zoom = {} if zoom_pos is None else {"pos": zoom_pos} | ||
| resp.json.return_value = [{"code": code, "value": {"ZoomFocus": {"focus": {"pos": 336}, "zoom": zoom}}}] | ||
| return resp | ||
|
|
||
|
|
||
| def _camera(cam_type="static"): | ||
| return ReolinkCamera( | ||
| camera_id="cam", | ||
| ip_address="192.168.1.10", | ||
| username="user", | ||
| password="pwd", # noqa: S106 | ||
| cam_type=cam_type, | ||
| ) | ||
|
|
||
|
|
||
| def test_static_camera_with_a_varifocal_lens_can_zoom(): | ||
| """A bullet camera does not pan, which says nothing about its optics.""" | ||
| cam = _camera(cam_type="static") | ||
| with patch(POST, return_value=_reply(zoom_pos=0)): | ||
| assert cam.has_motorised_lens() is True | ||
|
|
||
|
|
||
| def test_fixed_lens_camera_reports_no_zoom_position(): | ||
| cam = _camera(cam_type="static") | ||
| with patch(POST, return_value=_reply(zoom_pos=None)): | ||
| assert cam.has_motorised_lens() is False | ||
|
|
||
|
|
||
| def test_a_camera_that_rejects_the_command_is_a_settled_answer(): | ||
| """A non-zero Reolink code is the camera answering that it does not serve | ||
| GetZoomFocus. Re-probing it would cost a request on every command forever.""" | ||
| cam = _camera(cam_type="static") | ||
| with patch(POST, return_value=_reply(code=-9)) as post: | ||
| assert cam.has_motorised_lens() is False | ||
| assert cam.has_motorised_lens() is False | ||
| assert post.call_count == 1 | ||
|
|
||
|
|
||
| def test_unreachable_camera_is_treated_as_fixed_lens(): | ||
| """Probing must not raise: a camera that cannot be asked keeps its commands | ||
| from being sent rather than taking the whole call down.""" | ||
| cam = _camera() | ||
| with patch(POST, side_effect=OSError("unreachable")): | ||
| assert cam.has_motorised_lens() is False | ||
|
|
||
|
|
||
| def test_an_unanswered_probe_is_not_cached(): | ||
| """A transport failure says nothing about the optics. Caching it would | ||
| strand a PTZ camera as fixed-lens for the rest of the process.""" | ||
| cam = _camera(cam_type="ptz") | ||
| with patch(POST, side_effect=OSError("unreachable")): | ||
| assert cam.has_motorised_lens() is False | ||
| with patch(POST, return_value=_reply(zoom_pos=0)): | ||
| assert cam.has_motorised_lens() is True | ||
|
|
||
|
|
||
| def test_an_http_error_is_not_cached_either(): | ||
| cam = _camera(cam_type="ptz") | ||
| with patch(POST, return_value=_reply(status=500)): | ||
| assert cam.has_motorised_lens() is False | ||
| with patch(POST, return_value=_reply(zoom_pos=4)): | ||
| assert cam.has_motorised_lens() is True | ||
|
|
||
|
|
||
| def test_capability_is_probed_once(): | ||
| """Zoom and focus commands are frequent; the lens cannot grow a motor.""" | ||
| cam = _camera() | ||
| with patch(POST, return_value=_reply(zoom_pos=4)) as post: | ||
| cam.has_motorised_lens() | ||
| cam.has_motorised_lens() | ||
| assert post.call_count == 1 | ||
|
|
||
|
|
||
| def test_zoom_command_is_sent_to_a_static_varifocal_camera(): | ||
| """The regression this change is about: the command used to be dropped for | ||
| every static camera, silently returning None with no request made.""" | ||
| cam = _camera(cam_type="static") | ||
| with ( | ||
| patch(POST, return_value=_reply(zoom_pos=0)) as post, | ||
| patch.object(ReolinkCamera, "_handle_response", return_value="ok"), | ||
| ): | ||
| assert cam.start_zoom_focus(32) == "ok" | ||
| # one for the probe, one for the command | ||
| assert post.call_count == 2 | ||
| payload = post.call_args.kwargs["json"][0] | ||
| assert payload["param"]["ZoomFocus"]["pos"] == 32 | ||
| assert payload["param"]["ZoomFocus"]["op"] == "ZoomPos" | ||
|
|
||
|
|
||
| def test_zoom_command_is_not_sent_to_a_fixed_lens_camera(): | ||
| cam = _camera(cam_type="static") | ||
| with patch(POST, return_value=_reply(zoom_pos=None)) as post: | ||
| assert cam.start_zoom_focus(32) is None | ||
| # only the probe | ||
| assert post.call_count == 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the static varifocal cameras this new branch is meant to support,
POST /focus/focus_finderstill rejects the request before calling this method:routes_focus.pylines 121-122 unconditionally return 400 whenevercam_type == "static". Consequently the changed capability check cannot enable focus optimization through the camera service; the route needs to use the lens capability rather than the mounting type as well.Useful? React with 👍 / 👎.