This commit is contained in:
Nick Mowen 2022-12-14 12:08:11 -07:00
parent 6552c10fb8
commit f6e2357263
3 changed files with 14 additions and 8 deletions

View File

@ -70,7 +70,7 @@ class Dispatcher:
camera_name = topic.split("/")[-3] camera_name = topic.split("/")[-3]
command = topic.split("/")[-2] command = topic.split("/")[-2]
self._camera_settings_handlers[command](camera_name, payload) self._camera_settings_handlers[command](camera_name, payload)
except Exception as e: except IndexError as e:
logger.error(f"Received invalid set command: {topic}") logger.error(f"Received invalid set command: {topic}")
return return
elif topic.endswith("ptz"): elif topic.endswith("ptz"):
@ -78,7 +78,7 @@ class Dispatcher:
# example /cam_name/ptz payload=MOVE_UP|MOVE_DOWN|STOP... # example /cam_name/ptz payload=MOVE_UP|MOVE_DOWN|STOP...
camera_name = topic.split("/")[-2] camera_name = topic.split("/")[-2]
self._on_ptz_command(camera_name, payload) self._on_ptz_command(camera_name, payload)
except Exception as e: except IndexError as e:
logger.error(f"Received invalid ptz command: {topic}") logger.error(f"Received invalid ptz command: {topic}")
return return
elif topic == "restart": elif topic == "restart":
@ -222,5 +222,8 @@ class Dispatcher:
try: try:
command = OnvifCommandEnum[payload.lower()] command = OnvifCommandEnum[payload.lower()]
self.onvif.handle_command(camera_name, command) self.onvif.handle_command(camera_name, command)
except Exception as e: logger.info(f"Setting ptz command to {command} for {camera_name}")
return except KeyError as k:
logger.error(f"Invalid PTZ command {payload}: {k.with_traceback()}")
#except Exception as e:
# logger.error(f"Error sending {payload} to {camera_name}: {e}")

View File

@ -169,7 +169,7 @@ class MqttClient(Communicator): # type: ignore[misc]
if self.config.cameras[name].onvif.host: if self.config.cameras[name].onvif.host:
self.client.message_callback_add( self.client.message_callback_add(
f"{self.mqtt_config.topic_prefix}/{name}/ptz/#", f"{self.mqtt_config.topic_prefix}/{name}/ptz",
self.on_mqtt_command, self.on_mqtt_command,
) )

View File

@ -38,6 +38,7 @@ class OnvifController:
} }
def _init_onvif(self, camera_name: str) -> None: def _init_onvif(self, camera_name: str) -> None:
logger.error(f"Init onvif...")
onvif: ONVIFCamera = self.cams[camera_name]["onvif"] onvif: ONVIFCamera = self.cams[camera_name]["onvif"]
media = onvif.create_media_service() media = onvif.create_media_service()
profile = media.GetProfiles()[0] profile = media.GetProfiles()[0]
@ -56,6 +57,7 @@ class OnvifController:
self.cams[camera_name]["move_request"] = move_request self.cams[camera_name]["move_request"] = move_request
def _stop(self, camera_name: str) -> None: def _stop(self, camera_name: str) -> None:
logger.error(f"Stop onvif")
onvif: ONVIFCamera = self.cams[camera_name]["onvif"] onvif: ONVIFCamera = self.cams[camera_name]["onvif"]
move_request = self.cams[camera_name]["move_request"] move_request = self.cams[camera_name]["move_request"]
onvif.get_service("ptz").Stop( onvif.get_service("ptz").Stop(
@ -68,6 +70,7 @@ class OnvifController:
self.cams[camera_name]["active"] = False self.cams[camera_name]["active"] = False
def _move(self, camera_name: str, command: OnvifCommandEnum) -> None: def _move(self, camera_name: str, command: OnvifCommandEnum) -> None:
logger.error(f"Move onvif {command}")
if self.cams[camera_name]["active"]: if self.cams[camera_name]["active"]:
logger.warning( logger.warning(
f"{camera_name} is already performing an action, stopping..." f"{camera_name} is already performing an action, stopping..."
@ -78,13 +81,13 @@ class OnvifController:
onvif: ONVIFCamera = self.cams[camera_name]["onvif"] onvif: ONVIFCamera = self.cams[camera_name]["onvif"]
move_request = self.cams[camera_name]["move_request"] move_request = self.cams[camera_name]["move_request"]
if command == OnvifCommandEnum.left: if command == OnvifCommandEnum.move_left:
move_request.Velocity.PanTilt.x = -0.5 move_request.Velocity.PanTilt.x = -0.5
move_request.Velocity.PanTilt.y = 0 move_request.Velocity.PanTilt.y = 0
elif command == OnvifCommandEnum.right: elif command == OnvifCommandEnum.move_right:
move_request.Velocity.PanTilt.x = 0.5 move_request.Velocity.PanTilt.x = 0.5
move_request.Velocity.PanTilt.y = 0 move_request.Velocity.PanTilt.y = 0
elif command == OnvifCommandEnum.up: elif command == OnvifCommandEnum.move_up:
move_request.Velocity.PanTilt.x = 0 move_request.Velocity.PanTilt.x = 0
move_request.Velocity.PanTilt.y = 1 move_request.Velocity.PanTilt.y = 1
else: else: