From 403d8e9d0a0a7e9eb99fb334b0c1101719b3ee50 Mon Sep 17 00:00:00 2001 From: Pergola Fabio Date: Tue, 10 Oct 2023 10:03:51 +0200 Subject: [PATCH] try SDK method if ISAPI fails with err 23 for callsignal --- hikvision-doorbell/src/doorbell.py | 14 +++++++----- hikvision-doorbell/src/mqtt_input.py | 33 +++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/hikvision-doorbell/src/doorbell.py b/hikvision-doorbell/src/doorbell.py index 8d52495..0bbb61b 100644 --- a/hikvision-doorbell/src/doorbell.py +++ b/hikvision-doorbell/src/doorbell.py @@ -144,20 +144,22 @@ def unlock_door(self, lock_id: int): logger.info(" Door {} unlocked by SDK", lock_id + 1) - def answer_call(self): + def callsignal(self, cmd_type: int): """ Answer the specified door using the NET_DVR_VIDEO_CALL_PARAM. + command type: 0- Request call, 1- cancel call, 2- answer the call, 3- refuse the call, 4- called timeout, 5- end the call, 6- the device is busy, 7- the device is busy. """ gw = NET_DVR_VIDEO_CALL_PARAM() gw.dwSize = sizeof(NET_DVR_VIDEO_CALL_PARAM) - gw.dwCmdType = 0 - gw.wUnitNumber = 1 + gw.dwCmdType = cmd_type + #gw.wUnitNumber = 1 gw.byRes = (c_byte * 115)() result = self._sdk.NET_DVR_SetDVRConfig(self.user_id, 16036, 1, byref(gw),255) if not result: - logger.debug("NET_DVR_SetDVRConfig failed with code {}", self._sdk.NET_DVR_GetLastError()) - logger.info(" Door answered by SDK") - + raise SDKError(self._sdk, "Error while calling NET_DVR_VIDEO_CALL_PARAM") + logger.info("Callsignal {} sended with SDK", cmd_type) + + def reboot_device(self): # We know that the SDK gives error when rebooting since it cannot contact the device, raising error code 10 try: diff --git a/hikvision-doorbell/src/mqtt_input.py b/hikvision-doorbell/src/mqtt_input.py index 065f902..a89a31c 100644 --- a/hikvision-doorbell/src/mqtt_input.py +++ b/hikvision-doorbell/src/mqtt_input.py @@ -307,7 +307,15 @@ def _reject_call_callback(self, client, doorbell: Doorbell, message: MQTTMessage try: doorbell._call_isapi("PUT", url, json.dumps(requestBody)) except SDKError as err: - logger.error("Error while rejecting call: {}", err) + # If error code is 23 on some indoor stations, ISAPI failed, fallback to SDK method + logger.error("Error while answering call with ISAPI: {}", err) + error_code = err.args[1] + if error_code == 23: + try: + logger.debug("Answering call failed with ISAPI method, with error {} fallback to SDK method", error_code) + doorbell.callsignal(3) + except SDKError as err: + logger.error("Error while answering call with SDK: {}", err) def _hangup_call_callback(self, client, doorbell: Doorbell, message: MQTTMessage): logger.info("Received hangup command for doorbell: {}", doorbell._config.name) @@ -322,7 +330,15 @@ def _hangup_call_callback(self, client, doorbell: Doorbell, message: MQTTMessage try: doorbell._call_isapi("PUT", url, json.dumps(requestBody)) except SDKError as err: - logger.error("Error while hanging up call: {}", err) + # If error code is 23 on some indoor stations, ISAPI failed, fallback to SDK method + logger.error("Error while answering call with ISAPI: {}", err) + error_code = err.args[1] + if error_code == 23: + try: + logger.debug("Answering call failed with ISAPI method, with error {} fallback to SDK method", error_code) + doorbell.callsignal(5) + except SDKError as err: + logger.error("Error while answering call with SDK: {}", err) def _answer_call_callback(self, client, doorbell: Doorbell, message: MQTTMessage): logger.info("Received answer command for doorbell: {}", doorbell._config.name) @@ -336,11 +352,16 @@ def _answer_call_callback(self, client, doorbell: Doorbell, message: MQTTMessage # Avoid crashing inside the callback, otherwise we lose the MQTT client try: doorbell._call_isapi("PUT", url, json.dumps(requestBody)) - ## Tried to answer the call with SDK instead of ISAPI, but always receive error 23 here?? - ## result = self._sdk.NET_DVR_SetDVRConfig(self.user_id, 16036, 1, byref(gw),255) - # doorbell.answer_call() except SDKError as err: - logger.error("Error while answering call: {}", err) + # If error code is 23 on some indoor stations, ISAPI failed, fallback to SDK method + logger.error("Error while answering call with ISAPI: {}", err) + error_code = err.args[1] + if error_code == 23: + try: + logger.debug("Answering call failed with ISAPI method, with error {} fallback to SDK method", error_code) + doorbell.callsignal(2) + except SDKError as err: + logger.error("Error while answering call with SDK: {}", err) def _caller_info_callback(self, client, doorbell: Doorbell, message: MQTTMessage): logger.info("Trying to get caller info command for doorbell: {}", doorbell._config.name)