From 926b077b932bd45f89d7cbafcff92e2304380c26 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 20:56:05 +0000 Subject: [PATCH] la_mojo cameras: track PTZ health and re-initialise a failed PTZ protocol PTZ health per camera (deviceNodeCamera): - PTZ_HEALTH_INFO: state (NONE / INIT / OK / FAILED), failure and recovery counters, consecutive failed initialisations, time of the last state change / failure, next retry and the last error text. getPtzHealth() returns a copy (mutex-protected, any thread). - getPtzStatus() - already sent to the remote server as CAMERA_STATUS_PTZ_OK - now reflects it: 1 for OK (or no PTZ protocol), 0 while PTZ is initialising or failed. Before it was 1 as soon as the state machine left CAM_WS_INIT, whatever the protocol did. Failure detection and re-initialisation: - While idle (CAM_WS_READY) the camera checks the protocol's validate() every loop and calls the new protocolPTZ::probe() every 30 s (ONVIF: GetStatus; a SOAP Fault still means "reachable", only a communication error marks the camera lost). Default probe() is the protocol's validate(), so serial protocols are unchanged. - On failure the PTZ protocol is recreated through CAM_WS_INIT: first attempt at once, then 5, 10, 20, 40, 60 s (capped) between attempts; recoveries are logged and counted. - A failed target-driven goAbsolute/goPreset no longer ends the camera thread (CAM_WS_END rebuilt the whole node): for ONVIF a command the camera rejected (connection OK) only drops that ROI, anything else re-initialises the PTZ protocol. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodeCamera.cpp | 181 +++++++++++++++++++++++- lib/App/common/deviceNodeCamera.h | 39 +++++ lib/data/protocol/protocolPTZ.h | 7 + lib/data/protocol/protocolPTZ_ONVIF.cpp | 25 ++++ lib/data/protocol/protocolPTZ_ONVIF.h | 1 + 5 files changed, 246 insertions(+), 7 deletions(-) diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index f1b31092..733c719f 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -46,6 +46,10 @@ deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) : m_pBEH = basicEventHeap::getInstance(); m_bSkeepPositioningWait = true; m_bNoMoveDoneReport = false; + memset(&m_sPtzHealth, 0, sizeof(m_sPtzHealth)); + m_sPtzHealth.state = PTZ_HEALTH_INIT; + m_sPtzHealth.since = time(NULL); + m_tPtzLastProbe = 0; m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; m_tTimeTSeconds = 0; @@ -79,6 +83,10 @@ deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) : m_pBEH = basicEventHeap::getInstance(); m_bSkeepPositioningWait = true; m_bNoMoveDoneReport = false; + memset(&m_sPtzHealth, 0, sizeof(m_sPtzHealth)); + m_sPtzHealth.state = PTZ_HEALTH_INIT; + m_sPtzHealth.since = time(NULL); + m_tPtzLastProbe = 0; m_bNoAbsPositionning = false; m_bCameraOnHomePosition = false; m_tTimeTSeconds = 0; @@ -1072,17 +1080,159 @@ bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, C3D_ORIENT_ONVIF O) return false; } +// PTZ control status for the health report: 1 = PTZ works (or the camera has no PTZ), 0 = PTZ failed / not ready uint8_t deviceNodeCamera::getPtzStatus() { if (m_sCamData.protocol == 0) { return 1; } - if (getWM() > CAM_WS_INIT) + m_mPtzHealth.lock(); + PTZ_HEALTH_STATE tState = m_sPtzHealth.state; + m_mPtzHealth.unlock(); + return (tState == PTZ_HEALTH_OK || tState == PTZ_HEALTH_NONE) ? 1 : 0; +} + +void deviceNodeCamera::getPtzHealth(PTZ_HEALTH_INFO * info) +{ + if (info == NULL) { - return 1; + return; + } + m_mPtzHealth.lock(); + memcpy(info, &m_sPtzHealth, sizeof(PTZ_HEALTH_INFO)); + m_mPtzHealth.unlock(); +} + +/* + State changes of the PTZ health. FAILED schedules the next re-initialisation: the first one right away + (the camera may just have rebooted), then CAM_PTZ_RETRY_MIN_S doubling up to CAM_PTZ_RETRY_MAX_S. +*/ +void deviceNodeCamera::ptzHealthSet(PTZ_HEALTH_STATE state, const char * reason) +{ + time_t tNow = time(NULL); + char tB[256]; + tB[0] = (char) 0; + m_mPtzHealth.lock(); + PTZ_HEALTH_STATE tOld = m_sPtzHealth.state; + if (state == PTZ_HEALTH_FAILED) + { + if (tOld != PTZ_HEALTH_FAILED) + { + m_sPtzHealth.failures++; + m_sPtzHealth.failed_inits = 0; + m_sPtzHealth.next_retry = tNow; + } + else + { + // another failed initialisation + m_sPtzHealth.failed_inits++; + uint32_t tDelay = CAM_PTZ_RETRY_MIN_S; + for (uint32_t i = 1; i < m_sPtzHealth.failed_inits && tDelay < CAM_PTZ_RETRY_MAX_S; i++) + { + tDelay *= 2; + } + if (tDelay > CAM_PTZ_RETRY_MAX_S) + { + tDelay = CAM_PTZ_RETRY_MAX_S; + } + m_sPtzHealth.next_retry = tNow + tDelay; + }; + m_sPtzHealth.last_failure = tNow; + snprintf(m_sPtzHealth.last_error, sizeof(m_sPtzHealth.last_error), "%s", (reason != NULL) ? reason : "PTZ failure"); + snprintf(tB, sizeof(tB), "PTZ FAILED (%s), failures %u, next re-init in %ld s", m_sPtzHealth.last_error, m_sPtzHealth.failures, + (long) (m_sPtzHealth.next_retry - tNow)); } - return 0; + else + { + if (state == PTZ_HEALTH_OK && tOld == PTZ_HEALTH_FAILED) + { + m_sPtzHealth.recoveries++; + snprintf(tB, sizeof(tB), "PTZ recovered after %u failed attempt(s), recoveries %u", m_sPtzHealth.failed_inits + 1, m_sPtzHealth.recoveries); + } + m_sPtzHealth.failed_inits = 0; + m_sPtzHealth.next_retry = 0; + }; + if (state != tOld) + { + m_sPtzHealth.since = tNow; + } + m_sPtzHealth.state = state; + m_mPtzHealth.unlock(); + if (tB[0]) + { + if (state == PTZ_HEALTH_FAILED) + { + LOG_ERROR(tB); + } + else + { + LOG(tB); + } + }; +} + +// CAM_WS_INIT may (re)create the protocol now? Only throttled while FAILED. +bool deviceNodeCamera::ptzReinitDue(void) +{ + m_mPtzHealth.lock(); + bool tDue = (m_sPtzHealth.state != PTZ_HEALTH_FAILED) || (time(NULL) >= m_sPtzHealth.next_retry); + m_mPtzHealth.unlock(); + return tDue; +} + +/* + A target-driven PTZ command failed. Before, the camera thread ended (CAM_WS_END -> the node was rebuilt). + Now: ONVIF tells apart a rejected command (SOAP Fault, camera reachable -> validate() still true: drop this + ROI and carry on) from a lost camera. Every other failure re-initialises the PTZ protocol. +*/ +void deviceNodeCamera::ptzCommandFailed(const char * command) +{ + char tB[160]; + utilizeROI(m_pCurrentROI); + if (m_sCamData.protocol == CAM_CTRL_PROTO_ONVIF && m_pProtocol != NULL && m_pProtocol->validate()) + { + snprintf(tB, sizeof(tB), "%s rejected by the camera (see the PONVIF log), PTZ connection OK", command); + LOG_ERROR(tB); + setWM(CAM_WS_READY); + return; + } + snprintf(tB, sizeof(tB), "%s failed: PTZ protocol lost", command); + ptzLost(tB); +} + +// The PTZ protocol was lost: mark FAILED and re-initialise it (CAM_WS_INIT deletes and recreates it). +void deviceNodeCamera::ptzLost(const char * reason) +{ + ptzHealthSet(PTZ_HEALTH_FAILED, reason); + setWM(CAM_WS_INIT); +} + +/* + Called every loop of the camera thread. Only while idle (CAM_WS_READY), so moves are not disturbed: + - the protocol's own status (validate) is checked every loop; + - every CAM_PTZ_PROBE_INTERVAL_S the camera is actively asked (protocolPTZ::probe; ONVIF: GetStatus). +*/ +void deviceNodeCamera::ptzCheck(uint8_t tState) +{ + if (tState != CAM_WS_READY || m_pProtocol == NULL || m_sCamData.protocol == 0) + { + return; + } + if (! m_pProtocol->validate()) + { + ptzLost("PTZ protocol reports a failure"); + return; + } + time_t tNow = time(NULL); + if (tNow - m_tPtzLastProbe >= CAM_PTZ_PROBE_INTERVAL_S) + { + m_tPtzLastProbe = tNow; + if (! m_pProtocol->probe()) + { + ptzLost("PTZ liveness check failed (camera not reachable)"); + } + }; } uint8_t deviceNodeCamera::getStreamStatus() @@ -1106,6 +1256,8 @@ void deviceNodeCamera::m_fThreadProc(void) if ((bNewSecondLoop = (cTimeSeconds != oTimeSeconds))) oTimeSeconds = cTimeSeconds; uint8_t tState = getWM(); + ptzCheck(tState); + tState = getWM(); //char tB[128]; #ifndef NO_CAMERA_FUSION_ENGINE @@ -1115,6 +1267,10 @@ void deviceNodeCamera::m_fThreadProc(void) { case CAM_WS_INIT: { + if (! ptzReinitDue()) + { + break; // PTZ failed: wait for the next re-initialisation attempt + } if (m_pProtocol != NULL) { switch (m_sCamData.protocol) @@ -1371,6 +1527,19 @@ void deviceNodeCamera::m_fThreadProc(void) setWM(CAM_WS_POWER_INIT); }; m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online); + if (m_sCamData.protocol == 0) + { + ptzHealthSet(PTZ_HEALTH_NONE); + } + else if (m_pProtocol != NULL) + { + m_tPtzLastProbe = time(NULL); + ptzHealthSet(PTZ_HEALTH_OK); + } + else + { + ptzHealthSet(PTZ_HEALTH_FAILED, "PTZ protocol initialisation failed"); + }; break; }; case CAM_WS_POWER_INIT: @@ -1673,8 +1842,7 @@ void deviceNodeCamera::m_fThreadProc(void) m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom; if (! m_pProtocol->goPreset(token)) { - LOG_ERROR("Cannot Issue goPreset Command!->Restart Camera?"); - setWM(CAM_WS_END); + ptzCommandFailed("goPreset"); } else { @@ -1739,8 +1907,7 @@ void deviceNodeCamera::m_fThreadProc(void) // Do Movement if (! m_pProtocol->goAbsolute(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection)) { - LOG_ERROR("Cannot Issue goAbsolute Command!->Restart Camera?"); - setWM(CAM_WS_END); + ptzCommandFailed("goAbsolute"); } else { diff --git a/lib/App/common/deviceNodeCamera.h b/lib/App/common/deviceNodeCamera.h index f5277735..d8f6e6de 100644 --- a/lib/App/common/deviceNodeCamera.h +++ b/lib/App/common/deviceNodeCamera.h @@ -103,6 +103,34 @@ typedef enum E_CAM_WORKING_STATE CAM_WS_END = 255 } CAM_WORK_STATE; +/* + PTZ control health of a camera (deviceNodeCamera::getPtzHealth), e.g. for the health status sent to the + remote server. getPtzStatus() > 0 <=> state is PTZ_HEALTH_OK or PTZ_HEALTH_NONE. +*/ +typedef enum E_PTZ_HEALTH_STATE +{ + PTZ_HEALTH_NONE = 0, // camera has no PTZ control protocol (static camera): nothing that can fail + PTZ_HEALTH_INIT = 1, // protocol not created yet (first initialisation) + PTZ_HEALTH_OK = 2, // protocol created and working + PTZ_HEALTH_FAILED = 3 // protocol lost or cannot be created; re-initialisation is retried (next_retry) +} PTZ_HEALTH_STATE; + +typedef struct S_PTZ_HEALTH_INFO +{ + PTZ_HEALTH_STATE state; + uint32_t failures; // PTZ failures detected since start (lost protocol / failed init) + uint32_t recoveries; // successful re-initialisations after a failure + uint32_t failed_inits; // consecutive failed initialisations (reset on success) + time_t since; // time of the last state change + time_t last_failure; // time of the last failure (0 = never) + time_t next_retry; // next re-initialisation attempt while FAILED (0 = none) + char last_error[128]; +} PTZ_HEALTH_INFO; + +#define CAM_PTZ_PROBE_INTERVAL_S 30 // idle liveness check of the PTZ protocol (protocolPTZ::probe) +#define CAM_PTZ_RETRY_MIN_S 5 // first re-initialisation delay after a failure +#define CAM_PTZ_RETRY_MAX_S 60 // delay doubles per failed attempt up to this + typedef struct S_CAM_TASK { bool isDone; @@ -169,6 +197,16 @@ class deviceNodeCamera : public deviceNode, public basicThread float m_fMaxVFov; protocolPTZ * m_pProtocol; + + // PTZ health (written by the camera thread, read by others through getPtzHealth / getPtzStatus) + PTZ_HEALTH_INFO m_sPtzHealth; + basicMutex m_mPtzHealth; + time_t m_tPtzLastProbe; + void ptzHealthSet(PTZ_HEALTH_STATE state, const char * reason = NULL); + bool ptzReinitDue(void); + void ptzCheck(uint8_t tState); + void ptzLost(const char * reason); + void ptzCommandFailed(const char * command); // basicTransport * m_pTransport; CAM_TASK_QUEUE m_qCAM_TASK; @@ -283,6 +321,7 @@ class deviceNodeCamera : public deviceNode, public basicThread } virtual uint8_t getPtzStatus(); + void getPtzHealth(PTZ_HEALTH_INFO * info); virtual uint8_t getStreamStatus(); virtual float getSystemLocationX(){ return m_sCamData.x; diff --git a/lib/data/protocol/protocolPTZ.h b/lib/data/protocol/protocolPTZ.h index 677df7f6..941652f9 100644 --- a/lib/data/protocol/protocolPTZ.h +++ b/lib/data/protocol/protocolPTZ.h @@ -492,6 +492,13 @@ class protocolPTZ : public protocolParser { return true; } + + // Active liveness check, called by the camera node while it is idle. Protocols that can ask the + // camera something (ONVIF) do it here and update the result of validate(). Default: nothing to check. + virtual bool probe(void) + { + return validate(); + } }; #endif // NO_PTZ_CAMERAS diff --git a/lib/data/protocol/protocolPTZ_ONVIF.cpp b/lib/data/protocol/protocolPTZ_ONVIF.cpp index 35f43cf8..047ec916 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.cpp +++ b/lib/data/protocol/protocolPTZ_ONVIF.cpp @@ -698,5 +698,30 @@ bool protocolPTZ_ONVIF::validate() return (m_bStatus > 0); } +/* + Liveness check while the camera is idle: GetStatus on the PTZ profile. A camera that answers (even with a + SOAP Fault, e.g. GetStatus not supported) is reachable; only a communication error marks it lost. +*/ +bool protocolPTZ_ONVIF::probe(void) +{ +#ifdef USE_PONVIF + if (m_pOnVif == NULL || m_pProfile == NULL) + { + m_bStatus = 0; + return false; + } + ptzStatus tVal; + if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal)) + { + commandResult(false); + } + else + { + m_bStatus = 1; + } +#endif + return validate(); +} + #undef LOG #undef LOG_ERROR diff --git a/lib/data/protocol/protocolPTZ_ONVIF.h b/lib/data/protocol/protocolPTZ_ONVIF.h index ff161723..43f43a6d 100644 --- a/lib/data/protocol/protocolPTZ_ONVIF.h +++ b/lib/data/protocol/protocolPTZ_ONVIF.h @@ -80,6 +80,7 @@ class protocolPTZ_ONVIF : public protocolPTZ bool setAuthMode(uint8_t Mode); virtual bool waitMoveIsDone(void); virtual bool validate(); + virtual bool probe(void); }; #endif -- 2.39.5