From 9b5849988d14263a5e63856db885b60cb9a476ca Mon Sep 17 00:00:00 2001 From: Karl Moos Date: Sun, 18 Oct 2020 08:50:16 -0500 Subject: [PATCH 1/2] Add PTZ function --- api/ptz.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 api/ptz.py diff --git a/api/ptz.py b/api/ptz.py new file mode 100644 index 0000000..42b9a29 --- /dev/null +++ b/api/ptz.py @@ -0,0 +1,120 @@ +class PtzAPIMixin: + """ + API for PTZ functions. + """ + def _send_operation(self, operation, speed, index=None): + if index is None: + data = [{"cmd": "PtzCtrl", "action": 0, "param": {"channel": 0, "op": operation, "speed": speed}}] + else: + data = [{"cmd": "PtzCtrl", "action": 0, "param": { + "channel": 0, "op": operation, "speed": speed, "id": index}}] + return self._execute_command('PtzCtrl', data) + + def _send_noparm_operation(self, operation): + data = [{"cmd": "PtzCtrl", "action": 0, "param": {"channel": 0, "op": operation}}] + return self._execute_command('PtzCtrl', data) + + def _send_set_preset(self, operation, enable, preset=1, name='pos1'): + data = [{"cmd": "SetPtzPreset", "action": 0, "param": { + "channel": 0, "enable": enable, "id": preset, "name": name}}] + return self._execute_command('PtzCtrl', data) + + def go_to_preset(self, speed=60, index=1): + """ + Move the camera to a preset location + :return: response json + """ + return self._send_operation('ToPos', speed=speed, index=index) + + def add_preset(self, preset=1, name='pos1'): + """ + Adds the current camera position to the specified preset. + :return: response json + """ + return self._send_set_preset('PtzPreset', enable=1, preset=preset, name=name) + + def remove_preset(self, preset=1, name='pos1'): + """ + Removes the specified preset + :return: response json + """ + return self._send_set_preset('PtzPreset', enable=0, preset=preset, name=name) + + def move_right(self, speed=32): + """ + Move the camera to the right + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('Right', speed=speed) + + def move_right_up(self, speed=32): + """ + Move the camera to the right and up + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('RightUp', speed=speed) + + def move_right_down(self, speed=32): + """ + Move the camera to the right and down + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('RightDown', speed=speed) + + def move_left(self, speed=32): + """ + Move the camera to the left + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('Left', speed=speed) + + def move_left_up(self, speed=32): + """ + Move the camera to the left and up + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('LeftUp', speed=speed) + + def move_left_down(self, speed=32): + """ + Move the camera to the left and down + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('LeftDown', speed=speed) + + def move_up(self, speed=32): + """ + Move the camera up. + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('Up', speed=speed) + + def move_down(self, speed=32): + """ + Move the camera down. + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('Down', speed=speed) + + def stop_ptz(self): + """ + Stops the cameras current action. + :return: response json + """ + return self._send_noparm_operation('Stop') + + def auto_movement(self, speed=32): + """ + Move the camera in a clockwise rotation. + The camera moves self.stop_ptz() is called. + :return: response json + """ + return self._send_operation('Auto', speed=speed) From aa14b601f5d6e7fa281af1211cae278b886a2df5 Mon Sep 17 00:00:00 2001 From: Karl Moos Date: Sun, 18 Oct 2020 11:17:05 -0500 Subject: [PATCH 2/2] Add requirement file, docs, updates to complete PTZ --- README.md | 2 ++ api/APIHandler.py | 20 ++++++++++++++++++-- api/ptz.py | 18 +++++++++--------- requirements.txt | 4 ++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index 0d142a2..fe338d6 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ This project intends to stick with [PEP8](https://www.python.org/dev/peps/pep-00 GET: - [X] Login +- [X] Logout - [ ] Display -> OSD - [ ] Recording -> Encode (Clear and Fluent Stream) - [ ] Recording -> Advance (Scheduling) @@ -73,6 +74,7 @@ SET: - [ ] User -> Add User - [ ] User -> Manage User - [ ] Device -> HDD/SD Card +- [x] PTZ - [x] Zoom - [x] Focus - [ ] Image (Brightness, Contrass, Saturation, Hue, Sharp, Mirror, Rotate) diff --git a/api/APIHandler.py b/api/APIHandler.py index eb59ec6..7fa04b7 100644 --- a/api/APIHandler.py +++ b/api/APIHandler.py @@ -5,6 +5,7 @@ from .display import DisplayAPIMixin from .network import NetworkAPIMixin from .system import SystemAPIMixin from .user import UserAPIMixin +from .ptz import PtzAPIMixin from resthandle import Request @@ -14,7 +15,8 @@ class APIHandler(SystemAPIMixin, DeviceAPIMixin, DisplayAPIMixin, RecordingAPIMixin, - ZoomAPIMixin): + ZoomAPIMixin, + PtzAPIMixin): """ The APIHandler class is the backend part of the API, the actual API calls are implemented in Mixins. @@ -68,7 +70,21 @@ class APIHandler(SystemAPIMixin, except Exception as e: print("Error Login\n", e) raise - + + def logout(self) -> bool: + """ + Logout of the camera + :return: bool + """ + try: + data = [{"cmd": "Logout", "action": 0}] + ret = self._execute_command('Logout', data) + print(ret) + return True + except Exception as e: + print("Error Logout\n", e) + return False + def _execute_command(self, command, data, multi=False): """ Send a POST request to the IP camera with given data. diff --git a/api/ptz.py b/api/ptz.py index 42b9a29..463624e 100644 --- a/api/ptz.py +++ b/api/ptz.py @@ -40,7 +40,7 @@ class PtzAPIMixin: """ return self._send_set_preset('PtzPreset', enable=0, preset=preset, name=name) - def move_right(self, speed=32): + def move_right(self, speed=25): """ Move the camera to the right The camera moves self.stop_ptz() is called. @@ -48,7 +48,7 @@ class PtzAPIMixin: """ return self._send_operation('Right', speed=speed) - def move_right_up(self, speed=32): + def move_right_up(self, speed=25): """ Move the camera to the right and up The camera moves self.stop_ptz() is called. @@ -56,7 +56,7 @@ class PtzAPIMixin: """ return self._send_operation('RightUp', speed=speed) - def move_right_down(self, speed=32): + def move_right_down(self, speed=25): """ Move the camera to the right and down The camera moves self.stop_ptz() is called. @@ -64,7 +64,7 @@ class PtzAPIMixin: """ return self._send_operation('RightDown', speed=speed) - def move_left(self, speed=32): + def move_left(self, speed=25): """ Move the camera to the left The camera moves self.stop_ptz() is called. @@ -72,7 +72,7 @@ class PtzAPIMixin: """ return self._send_operation('Left', speed=speed) - def move_left_up(self, speed=32): + def move_left_up(self, speed=25): """ Move the camera to the left and up The camera moves self.stop_ptz() is called. @@ -80,7 +80,7 @@ class PtzAPIMixin: """ return self._send_operation('LeftUp', speed=speed) - def move_left_down(self, speed=32): + def move_left_down(self, speed=25): """ Move the camera to the left and down The camera moves self.stop_ptz() is called. @@ -88,7 +88,7 @@ class PtzAPIMixin: """ return self._send_operation('LeftDown', speed=speed) - def move_up(self, speed=32): + def move_up(self, speed=25): """ Move the camera up. The camera moves self.stop_ptz() is called. @@ -96,7 +96,7 @@ class PtzAPIMixin: """ return self._send_operation('Up', speed=speed) - def move_down(self, speed=32): + def move_down(self, speed=25): """ Move the camera down. The camera moves self.stop_ptz() is called. @@ -111,7 +111,7 @@ class PtzAPIMixin: """ return self._send_noparm_operation('Stop') - def auto_movement(self, speed=32): + def auto_movement(self, speed=25): """ Move the camera in a clockwise rotation. The camera moves self.stop_ptz() is called. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..30b468d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +requests +opencv-python +numpy +socks \ No newline at end of file