You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3176 lines
80 KiB

​
​
​
5 months ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
4 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: wait long enough for the device's reply over the Internet "NODSEN: Communication Wait State TO!" with a sensor behind the gateway: in SERIAL_PING_PONG mode the I/O thread waited only 20 x 5 ms (~100 ms, a serial-line value) for the reply to a request, and did not read the socket while no request was queued. A reply slower than that, which is normal over a mobile link, stayed in the socket. It was read with the next request and given to that request's parser, so the sensor ran one reply behind, retried, and after 16 failed attempts reported the timeout. deviceETH2UART I/O thread (SERIAL_PING_PONG) - Waits up to E2UART_REPLY_TIMEOUT_MS (800 ms, below the sensor's 1 s per attempt) for the reply to start, and E2UART_REPLY_GAP_MS (150 ms) between the pieces of a reply split over several TCP segments. It is done as soon as the parser has a complete packet and no more data is waiting (as before). - Reads the socket also between requests. Late data goes to the parser of the last request at once. - Gives data only to a parser that is still bound to the transport (a sensor that disconnected or was deleted is no longer called). Serial resource casts (deviceNode2D, deviceNodeCamera) - dynamic_cast<basicTransport*> instead of static_cast through hardwareResourceSerialProvider. The resource can be a deviceETH2UART, and the old cast was only right because of a matching base-class layout. A NULL transport isn't bound. Tested with a fake device answering after 50/300/700 ms, also split into two TCP pieces. The old code delivered nothing, or the previous request's answer. Now each request gets its own answer (ASan clean). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: wait long enough for the device's reply over the Internet "NODSEN: Communication Wait State TO!" with a sensor behind the gateway: in SERIAL_PING_PONG mode the I/O thread waited only 20 x 5 ms (~100 ms, a serial-line value) for the reply to a request, and did not read the socket while no request was queued. A reply slower than that, which is normal over a mobile link, stayed in the socket. It was read with the next request and given to that request's parser, so the sensor ran one reply behind, retried, and after 16 failed attempts reported the timeout. deviceETH2UART I/O thread (SERIAL_PING_PONG) - Waits up to E2UART_REPLY_TIMEOUT_MS (800 ms, below the sensor's 1 s per attempt) for the reply to start, and E2UART_REPLY_GAP_MS (150 ms) between the pieces of a reply split over several TCP segments. It is done as soon as the parser has a complete packet and no more data is waiting (as before). - Reads the socket also between requests. Late data goes to the parser of the last request at once. - Gives data only to a parser that is still bound to the transport (a sensor that disconnected or was deleted is no longer called). Serial resource casts (deviceNode2D, deviceNodeCamera) - dynamic_cast<basicTransport*> instead of static_cast through hardwareResourceSerialProvider. The resource can be a deviceETH2UART, and the old cast was only right because of a matching base-class layout. A NULL transport isn't bound. Tested with a fake device answering after 50/300/700 ms, also split into two TCP pieces. The old code delivered nothing, or the previous request's answer. Now each request gets its own answer (ASan clean). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
5 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: wait long enough for the device's reply over the Internet "NODSEN: Communication Wait State TO!" with a sensor behind the gateway: in SERIAL_PING_PONG mode the I/O thread waited only 20 x 5 ms (~100 ms, a serial-line value) for the reply to a request, and did not read the socket while no request was queued. A reply slower than that, which is normal over a mobile link, stayed in the socket. It was read with the next request and given to that request's parser, so the sensor ran one reply behind, retried, and after 16 failed attempts reported the timeout. deviceETH2UART I/O thread (SERIAL_PING_PONG) - Waits up to E2UART_REPLY_TIMEOUT_MS (800 ms, below the sensor's 1 s per attempt) for the reply to start, and E2UART_REPLY_GAP_MS (150 ms) between the pieces of a reply split over several TCP segments. It is done as soon as the parser has a complete packet and no more data is waiting (as before). - Reads the socket also between requests. Late data goes to the parser of the last request at once. - Gives data only to a parser that is still bound to the transport (a sensor that disconnected or was deleted is no longer called). Serial resource casts (deviceNode2D, deviceNodeCamera) - dynamic_cast<basicTransport*> instead of static_cast through hardwareResourceSerialProvider. The resource can be a deviceETH2UART, and the old cast was only right because of a matching base-class layout. A NULL transport isn't bound. Tested with a fake device answering after 50/300/700 ms, also split into two TCP pieces. The old code delivered nothing, or the previous request's answer. Now each request gets its own answer (ASan clean). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: wait long enough for the device's reply over the Internet "NODSEN: Communication Wait State TO!" with a sensor behind the gateway: in SERIAL_PING_PONG mode the I/O thread waited only 20 x 5 ms (~100 ms, a serial-line value) for the reply to a request, and did not read the socket while no request was queued. A reply slower than that, which is normal over a mobile link, stayed in the socket. It was read with the next request and given to that request's parser, so the sensor ran one reply behind, retried, and after 16 failed attempts reported the timeout. deviceETH2UART I/O thread (SERIAL_PING_PONG) - Waits up to E2UART_REPLY_TIMEOUT_MS (800 ms, below the sensor's 1 s per attempt) for the reply to start, and E2UART_REPLY_GAP_MS (150 ms) between the pieces of a reply split over several TCP segments. It is done as soon as the parser has a complete packet and no more data is waiting (as before). - Reads the socket also between requests. Late data goes to the parser of the last request at once. - Gives data only to a parser that is still bound to the transport (a sensor that disconnected or was deleted is no longer called). Serial resource casts (deviceNode2D, deviceNodeCamera) - dynamic_cast<basicTransport*> instead of static_cast through hardwareResourceSerialProvider. The resource can be a deviceETH2UART, and the old cast was only right because of a matching base-class layout. A NULL transport isn't bound. Tested with a fake device answering after 50/300/700 ms, also split into two TCP pieces. The old code delivered nothing, or the previous request's answer. Now each request gets its own answer (ASan clean). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
2 months ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
Camera: re-create only the PTZ protocol on ONVIF trouble, keep the node and its stream A pOnvif problem could delete and re-create the whole camera node, which dropped the video stream bound to it, so recordings were cut although the video was fine. deviceNodeCamera::validate() compared the whole camera record with memcmp; any difference rebuilt the node: - CAM_WS_INIT sets m_sCamData.online = false before re-creating the PTZ protocol and writes the DB only afterwards. A validate() during a slow ONVIF re-init saw "online" differ and deleted the camera mid-re-init. - The ONVIF discovery (updateIPCam) wrote online=1 and the whole XAddrs list into onvif_ctrl (the web UI and la_mojo store only the path), so the record changed on discovery rounds. - A failed read (SQLite busy, queries fail at once) returned NULL = "camera deleted". Now: - Field classes (camDataChange): - runtime and discovery fields (online, status, session, max_range, uuid, mac): ignored; - name and hardware: taken over; - PTZ access (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the camera thread re-creates only the PTZ protocol, keeping the node and the stream; - anything else (ip and credentials used by the stream, stream_id, type, geometry, ...): rebuild as before, logged with the reason. - A lasting PTZ problem rebuilds the camera only after CAM_REBUILD_AFTER_FAILED_INITS (10) failed protocol initialisations in a row (several minutes with the retry back-off). - Node / camera record not read: rebuild only after NODE_DATA_MISSES_TO_REBUILD (3) validations in a row, for all nodes (deviceNode::validate). - updateIPCam: onvif_ctrl = path of the first XAddr. Host and port come from the first XAddr only: ':' was searched in the whole list, so a second, IPv6 XAddr gave a broken IP. A result without an address doesn't wipe the IP. Tested (functions extracted unchanged): camDataChange classifies online / status / max_range / uuid / mac as ignored, rename as cosmetic, port / onvif_ctrl / auth / ptz token as PTZ re-init, and ip / password / stream / geometry as rebuild. The XAddrs parser was checked with a port, without a port, with an IPv6 second XAddr, with a query, and with no address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
5 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeCamera.h"
  2. #include "../../components/base/basicCameraInfo.h"
  3. #include "../../components/object/objectSystemAnchor.h"
  4. #include "../../data/protocol/protocolPTZ_ONVIF.h"
  5. #include "../../data/protocol/protocolPTZ_PELCO.h"
  6. #include "../../data/protocol/protocolPTZ_VISCA.h"
  7. #include "../../data/protocol/protocolPTZ_BRCP.h"
  8. //#include "../../hardware/uart/serialPort.h"
  9. //#include "../../hardware/uart/serialSOL.h"
  10. #include "../mojo/mojoKernel.h"
  11. #include "hardwareManager.h"
  12. #include "hardwareResourceSerialProvider.h"
  13. #include "kernelModuleEmbedVideo.h"
  14. #define LOG(X) deviceNode::m_pLog->Log("RCAM", this, X)
  15. //#define DEBUG_EXTRA
  16. #ifdef DEBUG
  17. #define LOG_ERROR(X) \
  18. { \
  19. char _tBfrErrMSG2w34[512]; \
  20. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  21. deviceNode::m_pLog->Log("RCAM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  22. }
  23. #else
  24. #define LOG_ERROR(X) deviceNode::m_pLog->Log("RCAM", this, _LOG_ERROR, X);
  25. #endif
  26. // all sizes in mm
  27. static const float AC_imager_diag[12] = {5.0f, 5.68f, 6.0f, 6.592f, 8.0f, 8.933f, 11.0f, 16.0f, 22.5f, 30.1f, 43.3f, 69.7f};
  28. static const float AC_imager_width[12] = {4.0f, 4.536f, 4.8f, 5.27f, 6.4f, 7.176f, 8.8f, 12.8f, 18.0f, 25.1f, 36.0f, 56.0f};
  29. static const float AC_imager_height[12] = {3.0f, 3.416f, 3.6f, 3.96f, 4.8f, 5.319f, 6.6f, 9.6f, 13.5f, 16.7f, 24.0f, 41.5f};
  30. #define CAM_FOCAL_LENGTH_CNT 49
  31. static const float cam_predef_focal_length[CAM_FOCAL_LENGTH_CNT] = {2.7f, 3.0f, 3.2f, 3.5f, 3.7f, 3.9f, 4.0f, 4.3f, 4.8f, 5.0f, 5.1f, 5.5f, 5.8f, 6.0f, 6.2f, 6.6f, 7.0f, 7.1f, 7.5f, 7.6f, 8.0f, 8.6f, 8.9f, 9.0f, 9.2f, 9.3f, 10.0f, 10.5f, 10.6f,
  32. 10.7f, 11.0f, 11.3f, 12.0f, 12.9f, 13.0f, 13.5f, 13.6f, 14.0f, 14.6f, 15.0f, 15.1f, 15.5f, 16.0f, 17.0f, 17.5f, 17.7f, 18.0f, 18.2f, 19.0f};
  33. deviceNodeCamera::deviceNodeCamera(uint32_t NodeID) :
  34. deviceNode(NodeID),
  35. basicThread()
  36. {
  37. // m_pLog = basicLogCollection::getInstance();
  38. m_pBEH = basicEventHeap::getInstance();
  39. m_bSkeepPositioningWait = true;
  40. m_bNoMoveDoneReport = false;
  41. memset(&m_sPtzHealth, 0, sizeof(m_sPtzHealth));
  42. m_sPtzHealth.state = PTZ_HEALTH_INIT;
  43. m_sPtzHealth.since = time(NULL);
  44. m_tPtzLastProbe = 0;
  45. m_bPendingCamData = false;
  46. m_bPendingProtoReinit = false;
  47. memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData));
  48. m_bNoAbsPositionning = false;
  49. m_bCameraOnHomePosition = false;
  50. m_bCameraAIControled = false;
  51. m_tTimeTSeconds = 0;
  52. m_pProtocol = NULL;
  53. // m_pTransport = NULL;
  54. m_pCurrentROI = NULL;
  55. m_nRealFrameWidth = 640;
  56. // m_bState = 0;
  57. // m_bBusy = true;
  58. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  59. m_nRelativeMoveCounter = 0;
  60. m_bRelativeMoveMatrix = 0;
  61. m_bCurrentPresetCol = 0;
  62. m_bCurrentPresetRow = 0;
  63. m_bShineOnTarget = false;
  64. m_bCameraIsStatic = false;
  65. m_bIRIsOn = false;
  66. m_bIRTO = 0;
  67. m_fSpeedRise = 0.03f;
  68. m_fZoomCorrection = 1.0f;
  69. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  70. // m_bSourceConnected = false;
  71. // m_pSource = NULL;
  72. }
  73. deviceNodeCamera::deviceNodeCamera(DBC_NODE_DATA * Data) :
  74. deviceNode(Data),
  75. basicThread()
  76. {
  77. // m_pLog = basicLogCollection::getInstance();
  78. m_pBEH = basicEventHeap::getInstance();
  79. m_bSkeepPositioningWait = true;
  80. m_bNoMoveDoneReport = false;
  81. memset(&m_sPtzHealth, 0, sizeof(m_sPtzHealth));
  82. m_sPtzHealth.state = PTZ_HEALTH_INIT;
  83. m_sPtzHealth.since = time(NULL);
  84. m_tPtzLastProbe = 0;
  85. m_bPendingCamData = false;
  86. m_bPendingProtoReinit = false;
  87. memset(&m_sPendingCamData, 0, sizeof(m_sPendingCamData));
  88. m_bNoAbsPositionning = false;
  89. m_bCameraOnHomePosition = false;
  90. m_tTimeTSeconds = 0;
  91. m_pProtocol = NULL;
  92. // m_pTransport = NULL;
  93. m_pCurrentROI = NULL;
  94. m_nRealFrameWidth = 640;
  95. // m_bState = 0;
  96. // m_bBusy = true;
  97. m_fMaxRange = m_fMinHFov = m_fMaxHFov = m_fMinVFov = m_fMaxVFov = .0f;
  98. m_nRelativeMoveCounter = 0;
  99. m_bRelativeMoveMatrix = 0;
  100. m_bCurrentPresetCol = 0;
  101. m_bCurrentPresetRow = 0;
  102. m_bShineOnTarget = false;
  103. m_bIRIsOn = false;
  104. m_bIRTO = 0;
  105. m_fSpeedRise = 0.03f;
  106. m_fZoomCorrection = 1.0f;
  107. m_fRM_Pan = m_fRM_Tilt = m_fRM_Zoom = .0f;
  108. // m_bSourceConnected = false;
  109. // m_pSource = NULL;
  110. }
  111. deviceNodeCamera::~deviceNodeCamera()
  112. {
  113. LOG("Destruct");
  114. // m_mQPROTECTOR.unlock();
  115. waitThreadEnd(true);
  116. LOG("Thread Del OK");
  117. /*if( m_pTransport!= NULL ) {
  118. if( m_sCamData.uart_id ) {
  119. DBC_UART_DATA *tData = NULL;
  120. tData = m_pDBC->getUartData( m_sCamData.uart_id );
  121. switch( tData->type ) {
  122. case UART_TYPE_HARDWARE:{
  123. delete((serialPort*)m_pTransport);
  124. break;
  125. };
  126. case UART_TYPE_SOL:{
  127. delete((serialSOL*)m_pTransport);
  128. break;
  129. };
  130. };
  131. };
  132. };
  133. LOG("Transport Del OK");
  134. */
  135. if (m_pProtocol != NULL)
  136. {
  137. switch (m_sCamData.protocol)
  138. {
  139. case CAM_CTRL_PROTO_VISCA: delete((protocolPTZ_VISCA *) m_pProtocol); break;
  140. case CAM_CTRL_PROTO_PELCO: delete((protocolPTZ_PELCO *) m_pProtocol); break;
  141. case CAM_CTRL_PROTO_ONVIF: delete((protocolPTZ_ONVIF *) m_pProtocol); break;
  142. case CAM_CTRL_PROTO_BRCP: delete((protocolPTZ_BRCP *) m_pProtocol); break;
  143. default : delete(m_pProtocol);
  144. };
  145. };
  146. LOG("Proto Del OK");
  147. // if( m_pPosition != NULL ) delete( m_pPosition );
  148. // LOG("Pos Del OK");
  149. // if( m_bSourceConnected ){
  150. // if( m_pSource != NULL ){
  151. // m_pSource->_close();
  152. // delete( m_pSource );
  153. // };
  154. // };
  155. // LOG("Stream Del OK");
  156. }
  157. void deviceNodeCamera::updateOpticalParameters(uint16_t frame_width, uint16_t frame_heigth)
  158. {
  159. #ifdef DEBUG
  160. char tB[128];
  161. LOG("Set Optical Parameters");
  162. sprintf(tB, "\tCapturing Frame: %dx%d ", frame_width, frame_heigth);
  163. LOG(tB);
  164. #endif
  165. if (frame_width > 0 && frame_heigth > 0)
  166. {
  167. m_nRealFrameWidth = frame_width;
  168. m_nRealFrameHeight = frame_heigth;
  169. float tFrameDiag = sqrtf(m_nRealFrameHeight * m_nRealFrameHeight + m_nRealFrameWidth * m_nRealFrameWidth);
  170. switch (m_sCamData.zoom_model)
  171. {
  172. case 0:
  173. {
  174. #ifdef DEBUG
  175. LOG("\tFixed Zoom");
  176. #endif
  177. m_fMaxFL = m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  178. m_fMinRange = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag);
  179. m_fMaxRange = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag);
  180. m_fR2FL = .0f;
  181. m_fZoomCorrection = .0f;
  182. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  183. break;
  184. };
  185. case 1:
  186. {
  187. #ifdef DEBUG
  188. LOG("\tAutomated Zoom");
  189. #endif
  190. m_fMinFL = cam_predef_focal_length[m_sCamData.lens_f];
  191. m_fMinRange = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMinFL, tFrameDiag);
  192. m_fMaxFL = cam_predef_focal_length[m_sCamData.lens_f] * m_sCamData.zoom;
  193. m_fMaxRange = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], m_fMaxFL, tFrameDiag);
  194. if (m_fMaxRange > m_fMinRange)
  195. {
  196. m_fR2FL = ((m_fMaxFL - m_fMinFL) / (m_fMaxRange - m_fMinRange));
  197. }
  198. else
  199. {
  200. m_fR2FL = .0f;
  201. };
  202. m_fZoomCorrection = 1.0f;
  203. m_pDBC->updateCamRange(m_sCamData.id, m_fMaxRange);
  204. break;
  205. };
  206. case 2:
  207. {
  208. #ifdef DEBUG
  209. LOG("\tManual Zoom");
  210. #endif
  211. //The Fixing minimal distance from 5 meters to a 0.5
  212. m_fMinRange = 0.5f; // Minimal 0.5 meters
  213. if (m_sCamData.max_range < m_fMinRange)
  214. {
  215. m_fMaxRange = m_fMinRange;
  216. }
  217. else
  218. {
  219. m_fMaxRange = m_sCamData.max_range;
  220. };
  221. m_fZoomCorrection = m_sCamData.zoom * 0.01f;
  222. m_fMinFL = calcCFL(AC_imager_width[m_sCamData.imager_type], m_fMinRange);//, tFrameDiag);
  223. m_fMaxFL = calcCFL(AC_imager_width[m_sCamData.imager_type], m_fMaxRange);//, tFrameDiag);
  224. break;
  225. };
  226. };
  227. m_fMinHFov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], m_fMaxFL));
  228. m_fMaxHFov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], m_fMinFL));
  229. m_fMinVFov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], m_fMaxFL));
  230. m_fMaxVFov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], m_fMinFL));
  231. #ifdef DEBUG
  232. sprintf(tB, "\tMinimal FL: %0.3fmm. Maximal FL: %0.3fmm. ", m_fMinFL, m_fMaxFL);
  233. LOG(tB);
  234. sprintf(tB, "\tMinimal Range: %0.2fm. Maximal Range: %0.2fm. ", m_fMinRange, m_fMaxRange);
  235. LOG(tB);
  236. sprintf(tB, "\tMinimal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMinHFov, m_fMinVFov);
  237. LOG(tB);
  238. sprintf(tB, "\tMaximal FOVs, Horizontal: %0.2f Vertical: %0.2f", m_fMaxHFov, m_fMaxVFov);
  239. LOG(tB);
  240. #endif
  241. };
  242. }
  243. bool deviceNodeCamera::create()
  244. {
  245. if (m_bThreadStatus == TH_WORKING)
  246. {
  247. return true;
  248. }
  249. setWM(CAM_WS_INIT);
  250. if (m_sNodeData.aux_id == 0)
  251. {
  252. return false;
  253. }
  254. DBC_CAM_DATA * tData = m_pDBC->getCamData(m_sNodeData.aux_id);
  255. if (tData != NULL)
  256. {
  257. memcpy(&m_sCamData, tData, sizeof(DBC_CAM_DATA));
  258. free(tData);
  259. }
  260. else
  261. {
  262. LOG_ERROR("Cannot Fetch CAM Data from Database!");
  263. return false;
  264. };
  265. if (! (m_sCamData.lens_f < CAM_FOCAL_LENGTH_CNT))
  266. {
  267. m_sCamData.lens_f = CAM_FOCAL_LENGTH_CNT - 1;
  268. }
  269. updateOpticalParameters(1280, 720); // HD Ready
  270. if (m_sCamData.ptz_speed_step > .0f)
  271. {
  272. m_fSpeedRise = m_sCamData.ptz_speed_step;
  273. };
  274. // m_pPosition = new basicPosition();// m_sCamData.lat, m_sCamData.lon, m_sCamData.alt, m_sCamData.roll, m_sCamData.pitch, m_sCamData.yaw );
  275. // if( m_pPosition == NULL ) {
  276. // LOG_ERROR("Position Constructor Failure!");
  277. // return false;
  278. // };
  279. // m_oPosition.setPositionXYZ( m_sCamData.x, m_sCamData.y, m_sCamData.z );
  280. // m_oPosition.setOrientationDeg( m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot );
  281. systemAnchor * SA = systemAnchor::getInstance();
  282. if( SA ){
  283. LOG("SysAnchor OK");
  284. if( SA->beLocal() ){
  285. LOG("SysAnchor beLocal!");
  286. m_oPosition.setPositionXYZ(m_sCamData.x, m_sCamData.y, m_sCamData.z, 0); // SA->getUTMZone() );
  287. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  288. } else {
  289. LOG("SysAnchor is Global Position System!");
  290. C3D_DECART LD = SA->getXYZ();
  291. m_oPosition.setPositionXYZ(LD.x + m_sCamData.x, LD.y + m_sCamData.y, LD.z + m_sCamData.z, SA->getUTMZone());
  292. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  293. };
  294. } else {
  295. LOG("No SysAnchor!");
  296. m_oPosition.setPositionXYZ(m_sCamData.x, m_sCamData.y, m_sCamData.z);
  297. m_oPosition.setOrientationDeg(m_sCamData.x_rot, m_sCamData.y_rot, m_sCamData.z_rot);
  298. };
  299. m_oPosition.updateMatrix();
  300. #ifdef DEBUG
  301. char tB[128];
  302. sprintf(tB, "Finalized Camera Location: Lat:%.3f Lon:%.3f Alt:%.3f", m_oPosition.getLat(), m_oPosition.getLon(), m_oPosition.getAlt());
  303. LOG(tB);
  304. sprintf(tB, "Finalized Camera Location: x:%.3f y:%.3f z:%.3f", m_oPosition.getX(), m_oPosition.getY(), m_oPosition.getZ());
  305. LOG(tB);
  306. sprintf(tB, "Orientation: x:%.3f y:%.3f z:%.3f", m_oPosition.getRotX_deg(), m_oPosition.getRotY_deg(), m_oPosition.getRotZ_deg());
  307. LOG(tB);
  308. #endif
  309. if (m_pProtocol != NULL)
  310. {
  311. LOG_ERROR("Cannot Start: Service Already Running!");
  312. return false;
  313. };
  314. #ifdef CAM_POST_INIT
  315. if ((m_sCamData.type == ARGUS_IP_PRESET_CAMERA) || (m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA))
  316. {
  317. m_bNoAbsPositionning = true;
  318. }
  319. if (m_sCamData.protocol == 0)
  320. {
  321. m_bCameraIsStatic = true;
  322. m_bSkeepPositioningWait = true;
  323. m_sCamData.online = true;
  324. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  325. return m_sCamData.online;
  326. };
  327. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  328. return init();
  329. #else //! CAM_POST_INIT
  330. switch (m_sCamData.protocol)
  331. {
  332. case CAM_CTRL_PROTO_VISCA:
  333. {
  334. #ifdef DEBUG
  335. LOG("VISCA");
  336. #endif
  337. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(m_sCamData.protocol_aux);
  338. if (m_pProtocol != NULL)
  339. {
  340. basicTransport * tTransport = NULL;
  341. hardwareManager * tHM = hardwareManager::getInstance();
  342. if (tHM)
  343. {
  344. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  345. if (tRES != NULL)
  346. {
  347. if (tRES->isValid())
  348. {
  349. tTransport = dynamic_cast<basicTransport *>(tRES);
  350. if (tTransport != NULL && m_pProtocol->setTransport(tTransport))
  351. {
  352. if (! m_pProtocol->create())
  353. {
  354. LOG_ERROR("Protocol <<< Create >>> Failure!");
  355. };
  356. }
  357. else
  358. {
  359. LOG_ERROR("Binding Port Failure!");
  360. };
  361. };
  362. #ifdef DEBUG
  363. }
  364. else
  365. {
  366. LOG("HWM Has No UART PORT!");
  367. #endif
  368. };
  369. #ifdef DEBUG
  370. }
  371. else
  372. {
  373. LOG_ERROR("Can not get Hardware Manager!");
  374. #endif
  375. };
  376. if (tTransport != NULL)
  377. {
  378. LOG("Bind Transport Port!");
  379. }
  380. else
  381. {
  382. LOG_ERROR("Transport Creation Failure!");
  383. delete((protocolPTZ_VISCA *) m_pProtocol);
  384. m_pProtocol = NULL;
  385. };
  386. #ifdef DEBUG
  387. }
  388. else
  389. {
  390. LOG_ERROR("Protocol Creation failure!");
  391. #endif
  392. };
  393. break;
  394. };
  395. case CAM_CTRL_PROTO_PELCO:
  396. {
  397. #ifdef DEBUG
  398. LOG("PELCO");
  399. #endif
  400. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(m_sCamData.protocol_aux);
  401. if (m_pProtocol != NULL)
  402. {
  403. basicTransport * tTransport = NULL;
  404. hardwareManager * tHM = hardwareManager::getInstance();
  405. if (tHM)
  406. {
  407. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  408. if (tRES != NULL)
  409. {
  410. if (tRES->isValid())
  411. {
  412. tTransport = dynamic_cast<basicTransport *>(tRES);
  413. if (tTransport != NULL && m_pProtocol->setTransport(tTransport))
  414. {
  415. if (! m_pProtocol->create())
  416. {
  417. LOG_ERROR("Protocol <<< Create >>> Failure!");
  418. };
  419. }
  420. else
  421. {
  422. LOG_ERROR("Binding Port Failure!");
  423. };
  424. };
  425. #ifdef DEBUG
  426. }
  427. else
  428. {
  429. LOG("HWM Has No UART PORT!");
  430. #endif
  431. };
  432. #ifdef DEBUG
  433. }
  434. else
  435. {
  436. LOG_ERROR("Can not get Hardware Manager!");
  437. #endif
  438. };
  439. if (tTransport != NULL)
  440. {
  441. LOG("Bind Transport Port!");
  442. }
  443. else
  444. {
  445. LOG_ERROR("Transport Creation Failure!");
  446. delete((protocolPTZ_PELCO *) m_pProtocol);
  447. m_pProtocol = NULL;
  448. };
  449. #ifdef DEBUG
  450. }
  451. else
  452. {
  453. LOG_ERROR("Protocol Creation failure!");
  454. #endif
  455. };
  456. break;
  457. };
  458. case CAM_CTRL_PROTO_ONVIF:
  459. {
  460. #ifdef DEBUG_EXTRA
  461. LOG("ONVIF");
  462. #endif
  463. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  464. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  465. {
  466. char tURI[256];
  467. if (m_sCamData.port)
  468. {
  469. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  470. }
  471. else
  472. {
  473. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  474. };
  475. LOG("PTZ BUILD URI");
  476. LOG(tURI);
  477. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  478. }
  479. else
  480. {
  481. LOG("USE FULL PTZ URI");
  482. LOG(m_sCamData.onvif_ctrl);
  483. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  484. };
  485. #ifdef DEBUG_EXTRA
  486. LOG("ONVIF-END");
  487. #endif
  488. if (m_pProtocol != NULL)
  489. {
  490. if (! m_pProtocol->create())
  491. {
  492. LOG_ERROR("Error Creating Proto!");
  493. delete((protocolPTZ_ONVIF *) m_pProtocol);
  494. m_pProtocol = NULL;
  495. };
  496. #ifdef DEBUG_EXTRA
  497. LOG("PROTO CREATE DONE");
  498. #endif
  499. };
  500. break;
  501. };
  502. case CAM_CTRL_PROTO_BRCP:{
  503. #ifdef DEBUG
  504. LOG("BRCP");
  505. #endif
  506. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  507. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  508. {
  509. char tURI[256];
  510. if (m_sCamData.port)
  511. {
  512. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  513. }
  514. else
  515. {
  516. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  517. };
  518. m_pProtocol = (protocolPTZ *) new protocolPTZ_BRCP(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  519. }
  520. else
  521. {
  522. m_pProtocol = (protocolPTZ *) new protocolPTZ_BRCP(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  523. };
  524. #ifdef DEBUG
  525. LOG("BRCP-END");
  526. #endif
  527. if (m_pProtocol != NULL)
  528. {
  529. if (! m_pProtocol->create())
  530. {
  531. LOG_ERROR("Error Creating Proto!");
  532. delete((protocolPTZ_BRCP *) m_pProtocol);
  533. m_pProtocol = NULL;
  534. };
  535. #ifdef DEBUG
  536. LOG("PROTO CREATE DONE");
  537. #endif
  538. };
  539. break;
  540. };
  541. default:
  542. {
  543. #ifdef DEBUG
  544. LOG("Camera Has no defined control protocol");
  545. #endif
  546. };
  547. };
  548. // Check - No ABS Movement!
  549. if ((m_sCamData.type == ARGUS_IP_PRESET_CAMERA) || (m_sCamData.type == ARGUS_ANALOG_PRESET_CAMERA))
  550. {
  551. m_bNoAbsPositionning = true;
  552. }
  553. if (m_pProtocol == NULL)
  554. {
  555. if (m_sCamData.protocol == 0)
  556. {
  557. m_bCameraIsStatic = true;
  558. m_bSkeepPositioningWait = true;
  559. m_sCamData.online = true;
  560. }
  561. else
  562. {
  563. LOG_ERROR("No Proto Created!");
  564. }
  565. }
  566. else
  567. {
  568. #ifdef DEBUG
  569. LOG("Proto is OK");
  570. #endif
  571. // Load Presets only if we have protocol
  572. load_presets();
  573. mojoKernel * tKRNL = mojoKernel::getInstance();
  574. m_bNoMoveDoneReport = false;
  575. if (tKRNL)
  576. {
  577. m_bSkeepPositioningWait = ((tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO);
  578. #ifdef DEBUG
  579. char ttTT[128];
  580. sprintf(ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode());
  581. LOG(ttTT);
  582. if (m_bSkeepPositioningWait)
  583. {
  584. LOG("Camera No Wait On Pos!");
  585. }
  586. else
  587. {
  588. LOG("Camera Will Wait On Pos!");
  589. };
  590. #endif
  591. }
  592. else
  593. {
  594. LOG_ERROR("No Kernel Instance!");
  595. };
  596. m_sCamData.online = init();
  597. };
  598. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  599. return m_sCamData.online;
  600. #endif //! CAM_POST_INIT
  601. }
  602. // void deviceNodeCamera::setBusy( bool Status ){
  603. // m_bBusy = Status;
  604. //};
  605. bool deviceNodeCamera::relativeMove(float PanSPD, float TiltSPD, float ZoomSPD)
  606. {
  607. if (m_sCamData.online)
  608. {
  609. uint32_t MoveMatrix = 0;
  610. if (PanSPD != .0f)
  611. {
  612. if (PanSPD > 0)
  613. {
  614. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2)
  615. {
  616. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  617. }
  618. else
  619. {
  620. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  621. };
  622. }
  623. else
  624. {
  625. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 2)
  626. {
  627. MoveMatrix |= PTZ_DRIVE_PAN_LEFT;
  628. }
  629. else
  630. {
  631. MoveMatrix |= PTZ_DRIVE_PAN_RIGHT;
  632. };
  633. };
  634. };
  635. if (TiltSPD != .0f)
  636. {
  637. if (TiltSPD > 0)
  638. {
  639. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1)
  640. {
  641. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  642. }
  643. else
  644. {
  645. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  646. };
  647. }
  648. else
  649. {
  650. if (m_sCamData.flip_mode == 0 || m_sCamData.flip_mode == 1)
  651. {
  652. MoveMatrix |= PTZ_DRIVE_TILT_DOWN;
  653. }
  654. else
  655. {
  656. MoveMatrix |= PTZ_DRIVE_TILT_UP;
  657. };
  658. };
  659. };
  660. if (ZoomSPD != .0f)
  661. {
  662. if (ZoomSPD > 0)
  663. {
  664. MoveMatrix |= PTZ_DRIVE_ZOOM_WIDE;
  665. }
  666. else
  667. {
  668. MoveMatrix |= PTZ_DRIVE_ZOOM_TELE;
  669. };
  670. };
  671. if (MoveMatrix != 0)
  672. {
  673. m_pProtocol->setPTZSpeed(fabs(PanSPD), fabs(TiltSPD), fabs(ZoomSPD));
  674. };
  675. return m_pProtocol->move(MoveMatrix);
  676. };
  677. return false;
  678. }
  679. bool deviceNodeCamera::relativeStop()
  680. {
  681. if (m_sCamData.online)
  682. {
  683. return m_pProtocol->move(0);
  684. };
  685. return false;
  686. }
  687. CAM_TASK * deviceNodeCamera::fetchCamTask(void)
  688. {
  689. CAM_TASK * rVal = NULL;
  690. m_mQPROTECTOR.lock();
  691. if (m_qCAM_TASK.size())
  692. {
  693. rVal = m_qCAM_TASK.front();
  694. m_qCAM_TASK.pop();
  695. };
  696. m_mQPROTECTOR.unlock();
  697. return rVal;
  698. }
  699. bool deviceNodeCamera::pushCamTask(CAM_TASK * ptr)
  700. {
  701. if (ptr == NULL)
  702. {
  703. return false;
  704. }
  705. m_mQPROTECTOR.lock();
  706. m_qCAM_TASK.push(ptr);
  707. m_mQPROTECTOR.unlock();
  708. return true;
  709. }
  710. #ifndef NO_CAMERA_FUSION_ENGINE
  711. bool deviceNodeCamera::fusion_compare(CAM_ROI * ptr, C3D_SPH_DEGREES tp)
  712. {
  713. if (ptr == NULL)
  714. {
  715. return false;
  716. }
  717. float tFI = fabsf(ptr->cam_position.fi - tp.fi);
  718. float tTETTA = fabsf(ptr->cam_position.tetta - tp.tetta);
  719. #ifdef DEBUG_ALL
  720. char tB[128];
  721. sprintf(tB, "FC: FOV:[%.5f:%.5f] dTG:[%.5f:%.5f] %.2f<%.2f>%.2f", ptr->h_fov, ptr->v_fov, tFI, tTETTA, ptr->min_range, tp.r, ptr->max_range);
  722. LOG(tB);
  723. #endif
  724. if (tFI > ptr->h_fov)
  725. {
  726. return false;
  727. }
  728. if (tTETTA > ptr->v_fov)
  729. {
  730. return false;
  731. }
  732. if (tp.r < ptr->min_range)
  733. {
  734. return false;
  735. }
  736. if (tp.r > ptr->max_range)
  737. {
  738. return false;
  739. }
  740. return true;
  741. }
  742. bool deviceNodeCamera::fusion_consumeData(uint32_t db_id, C3D_SPH_DEGREES tOR)
  743. {
  744. if (db_id == 0)
  745. {
  746. return false;
  747. }
  748. CAM_TRACK_RECORD * tTrackRecord = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  749. if (tTrackRecord == NULL)
  750. {
  751. return false;
  752. }
  753. #ifdef DEBUG_EXTRA
  754. LOG("Fusion: Consuming new Data!");
  755. #endif
  756. memset(tTrackRecord, 0, sizeof(CAM_TRACK_RECORD));
  757. tTrackRecord->target_direction.fi = tOR.fi;
  758. tTrackRecord->target_direction.r = tOR.r;
  759. tTrackRecord->target_direction.tetta = tOR.tetta;
  760. tTrackRecord->evt_db_ID = db_id;
  761. tTrackRecord->ts_ms = getMilliseconds();
  762. m_mROIsProtector.lock();
  763. vCAM_ROIS::iterator idx = m_vROIS.begin();
  764. for (; idx < m_vROIS.end(); idx++)
  765. {
  766. if (fusion_compare(*idx, tOR))
  767. {
  768. break;
  769. }
  770. }
  771. if (idx != m_vROIS.end())
  772. {
  773. // have Match!
  774. #ifdef DEBUG_EXTRA
  775. LOG("ROI MATCHED!");
  776. #endif
  777. (*idx)->targets.push_back(tTrackRecord);
  778. fusion_validateROI(tTrackRecord->ts_ms, *idx);
  779. }
  780. else
  781. {
  782. #ifdef DEBUG_EXTRA
  783. LOG("NO MATCH FOUND!");
  784. #endif
  785. // No mathced ROI -> create new one
  786. //CAM_ROI * tROI = (CAM_ROI *) malloc(sizeof(CAM_ROI));
  787. CAM_ROI * tROI = new CAM_ROI();
  788. if (tROI)
  789. {
  790. tROI->ts_ms = tTrackRecord->ts_ms;
  791. spherical2onvif(tOR, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  792. tROI->cam_position.fi = tOR.fi;
  793. tROI->cam_position.tetta = tOR.tetta;
  794. tROI->cam_position.r = tOR.r;
  795. float cFL = m_fMinFL + m_fR2FL * (tROI->cam_position.r - m_fMinRange);
  796. tROI->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  797. tROI->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  798. tROI->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to no to div in comparation function
  799. tROI->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // that will be called more ofthen
  800. tROI->targets.push_back(tTrackRecord);
  801. m_vROIS.push_back(tROI);
  802. };
  803. };
  804. m_mROIsProtector.unlock();
  805. return true;
  806. }
  807. bool deviceNodeCamera::fusion_validateROI(uint64_t cts, CAM_ROI * ptr)
  808. {
  809. if (ptr == NULL)
  810. {
  811. return false;
  812. }
  813. if ((cts - ptr->ts_ms) > LOSE_INTERES_TIME_MS)
  814. {
  815. return false;
  816. }
  817. #ifdef DEBUG_EXTRA
  818. char tB[128];
  819. sprintf(tB, "\tROI [%d]", ptr->targets.size());
  820. LOG(tB);
  821. #endif
  822. if (ptr->targets.size())
  823. {
  824. double tA = 0.0;
  825. double tB = 0.0;
  826. double tC = 0.0;
  827. uint32_t tCNT = 0;
  828. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end();)
  829. {
  830. if ((cts - (*idx)->ts_ms) > LOSE_INTERES_TIME_MS)
  831. {
  832. free(*idx);
  833. ptr->targets.erase(idx);
  834. }
  835. else
  836. {
  837. tA = tA + (*idx)->target_direction.fi;
  838. tB = tB + (*idx)->target_direction.tetta;
  839. tC = tC + (*idx)->target_direction.r;
  840. tCNT++;
  841. idx++;
  842. };
  843. };
  844. if (tCNT)
  845. {
  846. C3D_SPH_DEGREES L;// = {0};
  847. //memset( &L, 0, sizeof(C3D_SPH_DEGREES));
  848. L.fi = (float) (tA / (double) tCNT);
  849. L.tetta = (float) (tB / (double) tCNT);
  850. L.r = (float) (tC / (double) tCNT);
  851. spherical2onvif(L, ptr->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  852. ptr->ts_ms = cts;
  853. float cFL = m_fMinFL + m_fR2FL * (ptr->cam_position.r - m_fMinRange);
  854. ptr->max_range = calcMaxDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  855. ptr->min_range = calcMinDistance_mm(AC_imager_diag[m_sCamData.imager_type], cFL, m_nRealFrameWidth);
  856. ptr->h_fov = rad2deg(calcFOV_mm(AC_imager_width[m_sCamData.imager_type], cFL)) / 2.0f; // div to prevent division in comparation
  857. ptr->v_fov = rad2deg(calcFOV_mm(AC_imager_height[m_sCamData.imager_type], cFL)) / 2.0f; // function that will be called more ofthen
  858. return true;
  859. };
  860. };
  861. return false;
  862. }
  863. CAM_ROI * deviceNodeCamera::fusion_fetchROI(void)
  864. {
  865. CAM_ROI * tVAL = NULL;
  866. m_mROIsProtector.lock();
  867. if (m_vROIS.size())
  868. {
  869. vCAM_ROIS::iterator sRec;
  870. uint64_t cts = 0;
  871. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end(); idx++)
  872. {
  873. if (tVAL == NULL)
  874. {
  875. sRec = idx;
  876. tVAL = (*idx);
  877. cts = tVAL->ts_ms;
  878. }
  879. else
  880. {
  881. #ifdef CAM_FETCH_ROI_NEWEST
  882. if ((*idx)->ts_ms > cts)
  883. #else
  884. if ((*idx)->ts_ms < cts)
  885. #endif
  886. {
  887. sRec = idx;
  888. tVAL = (*idx);
  889. cts = tVAL->ts_ms;
  890. };
  891. };
  892. };
  893. m_vROIS.erase(sRec);
  894. };
  895. m_mROIsProtector.unlock();
  896. if (m_bNoAbsPositionning && tVAL)
  897. {
  898. // Skip targets older 3 seconds
  899. if (getMilliseconds() - tVAL->ts_ms > 3000)
  900. #ifdef DEBUG_EXTRA
  901. {
  902. LOG("Targetd too old! ---> Skiping");
  903. utilizeROI(tVAL);
  904. };
  905. #else
  906. utilizeROI(tVAL);
  907. #endif
  908. };
  909. return tVAL;
  910. }
  911. DBC_SNAPSHOT_DATA * deviceNodeCamera::fusion_translate_ROI(CAM_ROI *(&ptr))
  912. {
  913. DBC_SNAPSHOT_DATA * rVal = NULL;
  914. if (ptr == NULL)
  915. {
  916. return NULL;
  917. }
  918. if (ptr->targets.size())
  919. {
  920. //rVal = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  921. rVal = new DBC_SNAPSHOT_DATA();
  922. if (rVal)
  923. {
  924. //memset(rVal, 0, sizeof(DBC_SNAPSHOT_DATA));
  925. rVal->stream_id = m_sNodeData.aux_parameter;
  926. for (vCAM_TRACK_RECORDS::iterator idx = ptr->targets.begin(); idx < ptr->targets.end(); idx++)
  927. {
  928. rVal->assoc_event_ids.push_back((*idx)->evt_db_ID);
  929. }
  930. };
  931. ptr->targets.clear();
  932. }
  933. else
  934. {
  935. LOG_ERROR("No Events ID to ROI");
  936. };
  937. utilizeROI(ptr);
  938. // Not Setting MD5 and TS -> will set at capture time
  939. return rVal;
  940. }
  941. void deviceNodeCamera::fusion_validate(void)
  942. {
  943. if (m_bNoAbsPositionning)
  944. {
  945. return;
  946. }
  947. m_mROIsProtector.lock();
  948. if (m_vROIS.size())
  949. {
  950. #ifdef DEBUG_EXTRA
  951. char tB[128];
  952. sprintf(tB, "Fusion: Have [%d] Regions Of Interes", m_vROIS.size());
  953. LOG(tB);
  954. #endif
  955. uint64_t cts = getMilliseconds();
  956. for (vCAM_ROIS::iterator idx = m_vROIS.begin(); idx != m_vROIS.end();)
  957. {
  958. if (! fusion_validateROI(cts, *idx))
  959. {
  960. utilizeROI(*idx);
  961. m_vROIS.erase(idx);
  962. }
  963. else
  964. {
  965. idx++;
  966. }
  967. };
  968. };
  969. m_mROIsProtector.unlock();
  970. }
  971. #endif // !NO_CAMERA_FUSION_ENGINE
  972. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, float PanPOS, float TiltPOS, float ZoomPOS)
  973. {
  974. //if (m_sCamData.online)
  975. {
  976. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  977. if (tTask)
  978. {
  979. memset(tTask, 0, sizeof(CAM_TASK));
  980. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  981. if (tTask->CMD)
  982. {
  983. tTask->CMD->CMD = PTZ_ABSOLUTE;
  984. tTask->CMD->PAN = PanPOS;
  985. tTask->CMD->TILT = TiltPOS;
  986. tTask->CMD->ZOOM = ZoomPOS;
  987. tTask->movemet_needed = true;
  988. tTask->source_evt_id = TargetRefID;
  989. setWM(CAM_WS_BUSY);
  990. return pushCamTask(tTask);
  991. }
  992. else
  993. {
  994. LOG_ERROR("CMD malloc Error!");
  995. free(tTask);
  996. };
  997. }
  998. else
  999. {
  1000. LOG_ERROR("Task malloc error!");
  1001. }
  1002. };
  1003. return false;
  1004. }
  1005. bool deviceNodeCamera::absoluteMove(uint32_t TargetRefID, C3D_ORIENT_ONVIF O)
  1006. {
  1007. //if (m_sCamData.online)
  1008. {
  1009. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  1010. if (tTask)
  1011. {
  1012. memset(tTask, 0, sizeof(CAM_TASK));
  1013. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  1014. if (tTask->CMD)
  1015. {
  1016. tTask->CMD->CMD = PTZ_ABSOLUTE;
  1017. tTask->CMD->PAN = O.pan;
  1018. tTask->CMD->TILT = O.tilt;
  1019. tTask->CMD->ZOOM = O.zoom;
  1020. tTask->movemet_needed = true;
  1021. tTask->source_evt_id = TargetRefID;
  1022. setWM(CAM_WS_BUSY);
  1023. return pushCamTask(tTask);
  1024. }
  1025. else
  1026. {
  1027. LOG_ERROR("CMD malloc Error!");
  1028. free(tTask);
  1029. };
  1030. }
  1031. else
  1032. {
  1033. LOG_ERROR("Task malloc error!");
  1034. }
  1035. };
  1036. return false;
  1037. }
  1038. // PTZ control status for the health report: 1 = PTZ works (or the camera has no PTZ), 0 = PTZ failed / not ready
  1039. uint8_t deviceNodeCamera::getPtzStatus()
  1040. {
  1041. if (m_sCamData.protocol == 0)
  1042. {
  1043. return 1;
  1044. }
  1045. m_mPtzHealth.lock();
  1046. PTZ_HEALTH_STATE tState = m_sPtzHealth.state;
  1047. m_mPtzHealth.unlock();
  1048. return (tState == PTZ_HEALTH_OK || tState == PTZ_HEALTH_NONE) ? 1 : 0;
  1049. }
  1050. void deviceNodeCamera::getPtzHealth(PTZ_HEALTH_INFO * info)
  1051. {
  1052. if (info == NULL)
  1053. {
  1054. return;
  1055. }
  1056. m_mPtzHealth.lock();
  1057. memcpy(info, &m_sPtzHealth, sizeof(PTZ_HEALTH_INFO));
  1058. m_mPtzHealth.unlock();
  1059. }
  1060. /*
  1061. State changes of the PTZ health. FAILED schedules the next re-initialisation: the first one right away
  1062. (the camera may just have rebooted), then CAM_PTZ_RETRY_MIN_S doubling up to CAM_PTZ_RETRY_MAX_S.
  1063. */
  1064. void deviceNodeCamera::ptzHealthSet(PTZ_HEALTH_STATE state, const char * reason)
  1065. {
  1066. time_t tNow = time(NULL);
  1067. char tB[256];
  1068. tB[0] = (char) 0;
  1069. m_mPtzHealth.lock();
  1070. PTZ_HEALTH_STATE tOld = m_sPtzHealth.state;
  1071. if (state == PTZ_HEALTH_FAILED)
  1072. {
  1073. if (tOld != PTZ_HEALTH_FAILED)
  1074. {
  1075. m_sPtzHealth.failures++;
  1076. m_sPtzHealth.failed_inits = 0;
  1077. m_sPtzHealth.next_retry = tNow;
  1078. }
  1079. else
  1080. {
  1081. // another failed initialisation
  1082. m_sPtzHealth.failed_inits++;
  1083. uint32_t tDelay = CAM_PTZ_RETRY_MIN_S;
  1084. for (uint32_t i = 1; i < m_sPtzHealth.failed_inits && tDelay < CAM_PTZ_RETRY_MAX_S; i++)
  1085. {
  1086. tDelay *= 2;
  1087. }
  1088. if (tDelay > CAM_PTZ_RETRY_MAX_S)
  1089. {
  1090. tDelay = CAM_PTZ_RETRY_MAX_S;
  1091. }
  1092. m_sPtzHealth.next_retry = tNow + tDelay;
  1093. };
  1094. m_sPtzHealth.last_failure = tNow;
  1095. snprintf(m_sPtzHealth.last_error, sizeof(m_sPtzHealth.last_error), "%s", (reason != NULL) ? reason : "PTZ failure");
  1096. snprintf(tB, sizeof(tB), "PTZ FAILED (%s), failures %u, next re-init in %ld s", m_sPtzHealth.last_error, m_sPtzHealth.failures,
  1097. (long) (m_sPtzHealth.next_retry - tNow));
  1098. }
  1099. else
  1100. {
  1101. if (state == PTZ_HEALTH_OK && tOld == PTZ_HEALTH_FAILED)
  1102. {
  1103. m_sPtzHealth.recoveries++;
  1104. snprintf(tB, sizeof(tB), "PTZ recovered after %u failed attempt(s), recoveries %u", m_sPtzHealth.failed_inits + 1, m_sPtzHealth.recoveries);
  1105. }
  1106. m_sPtzHealth.failed_inits = 0;
  1107. m_sPtzHealth.next_retry = 0;
  1108. };
  1109. if (state != tOld)
  1110. {
  1111. m_sPtzHealth.since = tNow;
  1112. }
  1113. m_sPtzHealth.state = state;
  1114. m_mPtzHealth.unlock();
  1115. if (tB[0])
  1116. {
  1117. if (state == PTZ_HEALTH_FAILED)
  1118. {
  1119. LOG_ERROR(tB);
  1120. }
  1121. else
  1122. {
  1123. LOG(tB);
  1124. }
  1125. };
  1126. }
  1127. // CAM_WS_INIT may (re)create the protocol now? Only throttled while FAILED.
  1128. bool deviceNodeCamera::ptzReinitDue(void)
  1129. {
  1130. m_mPtzHealth.lock();
  1131. bool tDue = (m_sPtzHealth.state != PTZ_HEALTH_FAILED) || (time(NULL) >= m_sPtzHealth.next_retry);
  1132. m_mPtzHealth.unlock();
  1133. return tDue;
  1134. }
  1135. /*
  1136. A target-driven PTZ command failed. Before, the camera thread ended (CAM_WS_END -> the node was rebuilt).
  1137. Now: ONVIF tells apart a rejected command (SOAP Fault, camera reachable -> validate() still true: drop this
  1138. ROI and carry on) from a lost camera. Every other failure re-initialises the PTZ protocol.
  1139. */
  1140. void deviceNodeCamera::ptzCommandFailed(const char * command)
  1141. {
  1142. char tB[160];
  1143. utilizeROI(m_pCurrentROI);
  1144. if (m_sCamData.protocol == CAM_CTRL_PROTO_ONVIF && m_pProtocol != NULL && m_pProtocol->validate())
  1145. {
  1146. snprintf(tB, sizeof(tB), "%s rejected by the camera (see the PONVIF log), PTZ connection OK", command);
  1147. LOG_ERROR(tB);
  1148. setWM(CAM_WS_READY);
  1149. return;
  1150. }
  1151. snprintf(tB, sizeof(tB), "%s failed: PTZ protocol lost", command);
  1152. ptzLost(tB);
  1153. }
  1154. // The PTZ protocol was lost: mark FAILED and re-initialise it (CAM_WS_INIT deletes and recreates it).
  1155. void deviceNodeCamera::ptzLost(const char * reason)
  1156. {
  1157. ptzHealthSet(PTZ_HEALTH_FAILED, reason);
  1158. setWM(CAM_WS_INIT);
  1159. }
  1160. /*
  1161. Called every loop of the camera thread. Only while idle (CAM_WS_READY), so moves are not disturbed:
  1162. - the protocol's own status (validate) is checked every loop;
  1163. - every CAM_PTZ_PROBE_INTERVAL_S the camera is actively asked (protocolPTZ::probe; ONVIF: GetStatus).
  1164. */
  1165. void deviceNodeCamera::ptzCheck(uint8_t tState)
  1166. {
  1167. if (tState != CAM_WS_READY || m_pProtocol == NULL || m_sCamData.protocol == 0)
  1168. {
  1169. return;
  1170. }
  1171. if (! m_pProtocol->validate())
  1172. {
  1173. ptzLost("PTZ protocol reports a failure");
  1174. return;
  1175. }
  1176. time_t tNow = time(NULL);
  1177. if (tNow - m_tPtzLastProbe >= CAM_PTZ_PROBE_INTERVAL_S)
  1178. {
  1179. m_tPtzLastProbe = tNow;
  1180. if (! m_pProtocol->probe())
  1181. {
  1182. ptzLost("PTZ liveness check failed (camera not reachable)");
  1183. }
  1184. };
  1185. }
  1186. uint8_t deviceNodeCamera::getStreamStatus()
  1187. {
  1188. return 1;
  1189. }
  1190. void deviceNodeCamera::m_fThreadProc(void)
  1191. {
  1192. DBC_PUSHED_CMD * PUSHED = NULL;
  1193. m_bThreadStatus = TH_WORKING;
  1194. bool bNewSecondLoop = false;
  1195. uint32_t cTimeSeconds = 0;
  1196. uint32_t oTimeSeconds = 0;
  1197. pthread_setname_np( pthread_self(), "NodeCamera" );
  1198. while (m_bThreadStatus < TH_ENDED)
  1199. {
  1200. cTimeSeconds = (uint32_t) time(NULL);
  1201. if ((bNewSecondLoop = (cTimeSeconds != oTimeSeconds))) oTimeSeconds = cTimeSeconds;
  1202. applyPendingCamData(); // settings changed in the DB that need no rebuild (validate)
  1203. uint8_t tState = getWM();
  1204. ptzCheck(tState);
  1205. tState = getWM();
  1206. //char tB[128];
  1207. #ifndef NO_CAMERA_FUSION_ENGINE
  1208. fusion_validate();
  1209. #endif
  1210. switch (tState)
  1211. {
  1212. case CAM_WS_INIT:
  1213. {
  1214. if (! ptzReinitDue())
  1215. {
  1216. break; // PTZ failed: wait for the next re-initialisation attempt
  1217. }
  1218. if (m_pProtocol != NULL)
  1219. {
  1220. switch (m_sCamData.protocol)
  1221. {
  1222. case CAM_CTRL_PROTO_VISCA: delete((protocolPTZ_VISCA *) m_pProtocol); break;
  1223. case CAM_CTRL_PROTO_PELCO: delete((protocolPTZ_PELCO *) m_pProtocol); break;
  1224. case CAM_CTRL_PROTO_ONVIF: delete((protocolPTZ_ONVIF *) m_pProtocol); break;
  1225. case CAM_CTRL_PROTO_BRCP : delete((protocolPTZ_BRCP *) m_pProtocol); break;
  1226. default : delete(m_pProtocol);
  1227. };
  1228. m_pProtocol = NULL;
  1229. m_sCamData.online = false;
  1230. };
  1231. switch (m_sCamData.protocol)
  1232. {
  1233. case CAM_CTRL_PROTO_VISCA:
  1234. {
  1235. #ifdef DEBUG
  1236. LOG("VISCA");
  1237. #endif
  1238. m_pProtocol = (protocolPTZ *) new protocolPTZ_VISCA(m_sCamData.protocol_aux);
  1239. if (m_pProtocol != NULL)
  1240. {
  1241. basicTransport * tTransport = NULL;
  1242. hardwareManager * tHM = hardwareManager::getInstance();
  1243. if (tHM)
  1244. {
  1245. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1246. if (tRES != NULL)
  1247. {
  1248. if (tRES->isValid())
  1249. {
  1250. tTransport = dynamic_cast<basicTransport *>(tRES);
  1251. if (tTransport != NULL && m_pProtocol->setTransport(tTransport))
  1252. {
  1253. if (! m_pProtocol->create())
  1254. {
  1255. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1256. };
  1257. }
  1258. else
  1259. {
  1260. LOG_ERROR("Binding Port Failure!");
  1261. };
  1262. };
  1263. #ifdef DEBUG
  1264. }
  1265. else
  1266. {
  1267. LOG("HWM Has No UART PORT!");
  1268. #endif
  1269. };
  1270. #ifdef DEBUG
  1271. }
  1272. else
  1273. {
  1274. LOG_ERROR("Can not get Hardware Manager!");
  1275. #endif
  1276. };
  1277. if (tTransport != NULL)
  1278. {
  1279. LOG("Bind Transport Port!");
  1280. }
  1281. else
  1282. {
  1283. LOG_ERROR("Transport Creation Failure!");
  1284. delete((protocolPTZ_VISCA *) m_pProtocol);
  1285. m_pProtocol = NULL;
  1286. };
  1287. #ifdef DEBUG
  1288. }
  1289. else
  1290. {
  1291. LOG_ERROR("Protocol Creation failure!");
  1292. #endif
  1293. };
  1294. break;
  1295. };
  1296. case CAM_CTRL_PROTO_PELCO:
  1297. {
  1298. #ifdef DEBUG
  1299. LOG("PELCO");
  1300. #endif
  1301. m_pProtocol = (protocolPTZ *) new protocolPTZ_PELCO(m_sCamData.protocol_aux);
  1302. if (m_pProtocol != NULL)
  1303. {
  1304. basicTransport * tTransport = NULL;
  1305. hardwareManager * tHM = hardwareManager::getInstance();
  1306. if (tHM)
  1307. {
  1308. hardwareResource * tRES = tHM->requestResource(HARDWARE_PROVIDER_SERIAL, m_sCamData.uart_id);
  1309. if (tRES != NULL)
  1310. {
  1311. if (tRES->isValid())
  1312. {
  1313. tTransport = dynamic_cast<basicTransport *>(tRES);
  1314. if (tTransport != NULL && m_pProtocol->setTransport(tTransport))
  1315. {
  1316. if (! m_pProtocol->create())
  1317. {
  1318. LOG_ERROR("Protocol <<< Create >>> Failure!");
  1319. };
  1320. }
  1321. else
  1322. {
  1323. LOG_ERROR("Binding Port Failure!");
  1324. };
  1325. };
  1326. #ifdef DEBUG
  1327. }
  1328. else
  1329. {
  1330. LOG("HWM Has No UART PORT!");
  1331. #endif
  1332. };
  1333. #ifdef DEBUG
  1334. }
  1335. else
  1336. {
  1337. LOG_ERROR("Can not get Hardware Manager!");
  1338. #endif
  1339. };
  1340. if (tTransport != NULL)
  1341. {
  1342. LOG("Bind Transport Port!");
  1343. }
  1344. else
  1345. {
  1346. LOG_ERROR("Transport Creation Failure!");
  1347. delete((protocolPTZ_PELCO *) m_pProtocol);
  1348. m_pProtocol = NULL;
  1349. };
  1350. #ifdef DEBUG
  1351. }
  1352. else
  1353. {
  1354. LOG_ERROR("Protocol Creation failure!");
  1355. #endif
  1356. };
  1357. break;
  1358. };
  1359. case CAM_CTRL_PROTO_ONVIF:
  1360. {
  1361. #ifdef DEBUG_EXTRA
  1362. LOG("ONVIF");
  1363. #endif
  1364. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  1365. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL)
  1366. {
  1367. char tURI[512];
  1368. if (m_sCamData.port)
  1369. {
  1370. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  1371. }
  1372. else
  1373. {
  1374. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  1375. };
  1376. LOG("PTZ URI CREATE");
  1377. LOG( tURI );
  1378. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1379. }
  1380. else
  1381. {
  1382. LOG("PTZ USE");
  1383. LOG( m_sCamData.onvif_ctrl );
  1384. m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1385. };
  1386. #ifdef DEBUG_EXTRA
  1387. LOG("ONVIF-END");
  1388. #endif
  1389. if (m_pProtocol != NULL)
  1390. {
  1391. if (! m_pProtocol->create())
  1392. {
  1393. LOG_ERROR("Error Creating Proto!");
  1394. delete((protocolPTZ_ONVIF *) m_pProtocol);
  1395. m_pProtocol = NULL;
  1396. };
  1397. #ifdef DEBUG_EXTRA
  1398. LOG("PROTO CREATE DONE");
  1399. #endif
  1400. };
  1401. break;
  1402. };
  1403. case CAM_CTRL_PROTO_BRCP:{
  1404. #ifdef DEBUG
  1405. LOG("BRCP");
  1406. #endif
  1407. // m_pProtocol = (protocolPTZ *) new protocolPTZ_ONVIF( m_sCamData.ip, m_sCamData.port, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode );
  1408. if (strstr(m_sCamData.onvif_ctrl, "http") == NULL){
  1409. char tURI[256];
  1410. if (m_sCamData.port) {
  1411. sprintf(tURI, "http://%s:%d%s", m_sCamData.ip, m_sCamData.port, m_sCamData.onvif_ctrl);
  1412. } else {
  1413. sprintf(tURI, "http://%s%s", m_sCamData.ip, m_sCamData.onvif_ctrl);
  1414. };
  1415. m_pProtocol = (protocolPTZ *) new protocolPTZ_BRCP(tURI, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1416. } else {
  1417. m_pProtocol = (protocolPTZ *) new protocolPTZ_BRCP(m_sCamData.onvif_ctrl, m_sCamData.username, m_sCamData.password, m_sCamData.auth_mode);
  1418. };
  1419. #ifdef DEBUG
  1420. LOG("BRCP-END");
  1421. #endif
  1422. if (m_pProtocol != NULL) {
  1423. if (! m_pProtocol->create()) {
  1424. LOG_ERROR("Error Creating Proto!");
  1425. delete((protocolPTZ_BRCP *) m_pProtocol);
  1426. m_pProtocol = NULL;
  1427. };
  1428. #ifdef DEBUG
  1429. LOG("PROTO CREATE DONE");
  1430. #endif
  1431. };
  1432. break;
  1433. };
  1434. default:
  1435. {
  1436. #ifdef DEBUG
  1437. LOG("Camera Has no defined control protocol");
  1438. #endif
  1439. };
  1440. };
  1441. if (m_pProtocol != NULL)
  1442. {
  1443. // Load Presets only if we have protocol
  1444. load_presets();
  1445. mojoKernel * tKRNL = mojoKernel::getInstance();
  1446. m_bNoMoveDoneReport = false;
  1447. if (tKRNL)
  1448. {
  1449. m_bSkeepPositioningWait = ((tKRNL->getMediaMode() & EVE_OUTPUT_VIDEO) == EVE_OUTPUT_VIDEO);
  1450. #ifdef DEBUG_EXTRA
  1451. char ttTT[128];
  1452. sprintf(ttTT, "Kernel Media Mode:%d", tKRNL->getMediaMode());
  1453. LOG(ttTT);
  1454. if (m_bSkeepPositioningWait)
  1455. {
  1456. LOG("Camera No Wait On Pos!");
  1457. }
  1458. else
  1459. {
  1460. LOG("Camera Will Wait On Pos!");
  1461. };
  1462. #endif
  1463. }
  1464. else
  1465. {
  1466. LOG_ERROR("No Kernel Instance!");
  1467. };
  1468. m_sCamData.online = true;
  1469. setWM(CAM_WS_POWER_INIT);
  1470. };
  1471. m_pDBC->updateCamOnlineStatus(m_sCamData.id, m_sCamData.online);
  1472. if (m_sCamData.protocol == 0)
  1473. {
  1474. ptzHealthSet(PTZ_HEALTH_NONE);
  1475. }
  1476. else if (m_pProtocol != NULL)
  1477. {
  1478. m_tPtzLastProbe = time(NULL);
  1479. ptzHealthSet(PTZ_HEALTH_OK);
  1480. }
  1481. else
  1482. {
  1483. ptzHealthSet(PTZ_HEALTH_FAILED, "PTZ protocol initialisation failed");
  1484. };
  1485. break;
  1486. };
  1487. case CAM_WS_POWER_INIT:
  1488. {
  1489. #ifdef DEBUG
  1490. LOG("Init Camera - PowerMode");
  1491. #endif
  1492. // setBusy( true );
  1493. if (m_pProtocol->initCamera())
  1494. {
  1495. setWM(CAM_WS_WHITE_BALLANCE);
  1496. }
  1497. // setBusy( false );
  1498. break;
  1499. };
  1500. case CAM_WS_WHITE_BALLANCE:
  1501. {
  1502. #ifdef DEBUG
  1503. LOG("Set WB");
  1504. #endif
  1505. // setBusy( true );
  1506. if (m_pProtocol->setWhiteBalanceMode(WB_AUTO))
  1507. {
  1508. setWM(CAM_WS_BACKLIGHT);
  1509. }
  1510. // setBusy( false );
  1511. break;
  1512. };
  1513. case CAM_WS_BACKLIGHT:
  1514. {
  1515. #ifdef DEBUG
  1516. LOG("Set BLC");
  1517. #endif
  1518. // setBusy( true );
  1519. if (m_pProtocol->setBackLightMode(true))
  1520. {
  1521. setWM(CAM_WS_AUTO_FOCUS);
  1522. }
  1523. // setBusy( false );
  1524. break;
  1525. };
  1526. case CAM_WS_AUTO_FOCUS:
  1527. {
  1528. #ifdef DEBUG
  1529. LOG("Set AF");
  1530. #endif
  1531. // setBusy( true );
  1532. if (m_pProtocol->setAutoFocus(true))
  1533. {
  1534. setWM(CAM_WS_INIT_GO_HOME);
  1535. }
  1536. // setBusy( false );
  1537. break;
  1538. };
  1539. case CAM_WS_INIT_GO_HOME:
  1540. {
  1541. m_nHomingCounter = 0;
  1542. /* Skip For Restartin bug
  1543. if( !m_bNoAbsPositionning ) {
  1544. #ifdef DEBUG
  1545. LOG("INIT GO -1 0 -1");
  1546. #endif
  1547. if (m_pProtocol->goAbsolute(-1, 0, -1)) {
  1548. m_fCP_Pan = -1;
  1549. m_fCP_Tilt = 0;
  1550. m_fCP_Zoom = -1;
  1551. setWM( CAM_WS_READY );
  1552. };
  1553. } else {
  1554. #ifdef DEBUG
  1555. LOG("Go Home!");
  1556. #endif
  1557. CSTR TK = getPresetToken( ARGUS_PRESET_TYPE_HOME );
  1558. if( TK ) {
  1559. if ( !m_bCameraOnHomePosition ) {
  1560. if( !m_pProtocol->goPreset( TK )){
  1561. LOG_ERROR("Cannot Issue goPreset Command!");
  1562. };
  1563. m_bCameraOnHomePosition = true;
  1564. };
  1565. };
  1566. setWM( CAM_WS_READY );
  1567. };*/
  1568. LOG("INit GoHome");
  1569. setWM(CAM_WS_READY);
  1570. break;
  1571. };
  1572. case CAM_WS_AUTOMATED_MOVE:
  1573. {
  1574. m_bCameraOnHomePosition = false;
  1575. m_nRelativeMoveCounter++;
  1576. if ((m_bRelativeMoveMatrix & 0x03) != 0)
  1577. {
  1578. // Pan Move
  1579. if ((m_bRelativeMoveMatrix & 0x01) == 0)
  1580. {
  1581. m_fRM_Pan -= m_fSpeedRise;
  1582. }
  1583. else
  1584. {
  1585. m_fRM_Pan += m_fSpeedRise;
  1586. };
  1587. };
  1588. if ((m_bRelativeMoveMatrix & 0x0C) != 0)
  1589. {
  1590. // TILT Move
  1591. if ((m_bRelativeMoveMatrix & 0x04) == 0)
  1592. {
  1593. m_fRM_Tilt -= m_fSpeedRise;
  1594. }
  1595. else
  1596. {
  1597. m_fRM_Tilt += m_fSpeedRise;
  1598. }
  1599. };
  1600. if ((m_bRelativeMoveMatrix & 0x30) != 0)
  1601. {
  1602. // ZOOM Move
  1603. if ((m_bRelativeMoveMatrix & 0x10) == 0)
  1604. {
  1605. // Wide
  1606. m_fRM_Zoom -= m_fSpeedRise;
  1607. }
  1608. else
  1609. {
  1610. // Tele
  1611. m_fRM_Zoom += m_fSpeedRise;
  1612. };
  1613. };
  1614. if (m_fRM_Pan < -1.0f)
  1615. {
  1616. m_fRM_Pan = -1.0f;
  1617. }
  1618. if (m_fRM_Pan > 1.0f)
  1619. {
  1620. m_fRM_Pan = 1.0f;
  1621. }
  1622. if (m_fRM_Tilt < -1.0f)
  1623. {
  1624. m_fRM_Tilt = -1.0f;
  1625. }
  1626. if (m_fRM_Tilt > 1.0f)
  1627. {
  1628. m_fRM_Tilt = 1.0f;
  1629. }
  1630. if (m_fRM_Zoom < -1.0f)
  1631. {
  1632. m_fRM_Zoom = -1.0f;
  1633. }
  1634. if (m_fRM_Zoom > 1.0f)
  1635. {
  1636. m_fRM_Zoom = 1.0f;
  1637. }
  1638. m_fCP_Zoom += 0.5f * m_fRM_Zoom;
  1639. if (m_fCP_Zoom < -1.0f)
  1640. {
  1641. m_fCP_Zoom = -1.0f;
  1642. }
  1643. if (m_fCP_Zoom > 1.0f)
  1644. {
  1645. m_fCP_Zoom = 1.0f;
  1646. }
  1647. if (! relativeMove(m_fRM_Pan, m_fRM_Tilt, m_fRM_Zoom))
  1648. {
  1649. m_nRelativeMoveCounter = 0;
  1650. setWM(CAM_WS_READY);
  1651. break;
  1652. };
  1653. if (m_sCamData.ptz_axel)
  1654. { // Manual
  1655. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1656. {
  1657. m_nRelativeMoveCounter = 0;
  1658. setWM(CAM_WS_READY);
  1659. };
  1660. }
  1661. else
  1662. {
  1663. // Stepping Axel by Zoom
  1664. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1665. if (m_nRelativeMoveCounter > tVal)
  1666. {
  1667. m_nRelativeMoveCounter = 0;
  1668. setWM(CAM_WS_READY);
  1669. };
  1670. };
  1671. break;
  1672. };
  1673. case CAM_WS_STOP_MOVE:
  1674. {
  1675. m_nRelativeMoveCounter++;
  1676. m_fRM_Pan = .0f;
  1677. m_fRM_Pan = .0f;
  1678. m_fRM_Tilt = .0f;
  1679. m_fRM_Tilt = .0f;
  1680. m_fRM_Zoom = .0f;
  1681. m_fRM_Zoom = .0f;
  1682. if (relativeStop())
  1683. {
  1684. m_nHomingCounter = 0;
  1685. m_nRelativeMoveCounter = 0;
  1686. setWM(CAM_WS_READY);
  1687. break;
  1688. };
  1689. if (m_sCamData.ptz_axel)
  1690. {
  1691. if (m_nRelativeMoveCounter > m_sCamData.ptz_axel)
  1692. {
  1693. setWM(CAM_WS_READY);
  1694. }
  1695. }
  1696. else
  1697. {
  1698. uint8_t tVal = (uint8_t) (10.0f * (2.0f - (1.0f + m_fCP_Zoom)));
  1699. if (m_nRelativeMoveCounter > tVal)
  1700. {
  1701. setWM(CAM_WS_READY);
  1702. }
  1703. };
  1704. break;
  1705. };
  1706. case CAM_WS_GO_ABS_ROI:
  1707. case CAM_WS_GO_ABS_POSITION:
  1708. {
  1709. m_bCameraOnHomePosition = false;
  1710. setWM(CAM_WS_BUSY);
  1711. if (m_pCurrentROI != NULL)
  1712. {
  1713. // LOG("ROI Ok");
  1714. m_nHomingCounter = 0;
  1715. if (m_pBEH)
  1716. {
  1717. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_MOVE_TO, NULL, NULL, NULL);
  1718. if (tEVT)
  1719. {
  1720. m_pBEH->broadcast(tEVT);
  1721. }
  1722. };
  1723. if (m_bNoAbsPositionning)
  1724. {
  1725. // LOG("ABS to PRESET");
  1726. uint8_t COL, ROW;
  1727. translateROI2PRESET(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom, COL, ROW);
  1728. // char tB[64];
  1729. // sprintf( tB, "Get ABS Preset Col %d Row %d!", COL, ROW );
  1730. // LOG( tB );
  1731. // sprintf( tB, "Old Preset Col %d Row %d!", m_bCurrentPresetCol, m_bCurrentPresetRow );
  1732. // LOG( tB );
  1733. if ((m_bCurrentPresetCol == COL) && (m_bCurrentPresetRow == ROW))
  1734. {
  1735. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1736. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1737. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1738. #ifdef DEBUG_EXTRA
  1739. LOG("INSTANT BCAST CAM ONPOS");
  1740. #endif
  1741. // Send Capture event
  1742. #ifndef NO_CAMERA_FUSION_ENGINE
  1743. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1744. #else
  1745. DBC_SNAPSHOT_DATA * tData = NULL;
  1746. #endif
  1747. if (tData != NULL)
  1748. {
  1749. if (m_pBEH)
  1750. {
  1751. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1752. if (tEVT)
  1753. {
  1754. m_pBEH->broadcast(tEVT);
  1755. }
  1756. else
  1757. {
  1758. //free(tData);
  1759. delete(tData);
  1760. };
  1761. }
  1762. else
  1763. {
  1764. //free(tData);
  1765. delete(tData);
  1766. };
  1767. };
  1768. setWM(CAM_WS_READY);
  1769. }
  1770. else
  1771. {
  1772. CSTR token = getPresetToken(COL, ROW);
  1773. if (token)
  1774. {
  1775. // update position Info
  1776. m_bCurrentPresetCol = COL;
  1777. m_bCurrentPresetRow = ROW;
  1778. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1779. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1780. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1781. if (! m_pProtocol->goPreset(token))
  1782. {
  1783. ptzCommandFailed("goPreset");
  1784. }
  1785. else
  1786. {
  1787. // Waiting Movement finish
  1788. if (m_bSkeepPositioningWait && 0)
  1789. {
  1790. #ifdef DEBUG_EXTRA
  1791. LOG("INSTANT BCAST CAM ONPOS");
  1792. #endif
  1793. // Send Capture event
  1794. #ifndef NO_CAMERA_FUSION_ENGINE
  1795. DBC_SNAPSHOT_DATA * tData = fusion_translate_ROI(m_pCurrentROI);
  1796. #else
  1797. DBC_SNAPSHOT_DATA * tData = NULL;
  1798. #endif
  1799. if (tData != NULL)
  1800. {
  1801. if (m_pBEH)
  1802. {
  1803. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1804. if (tEVT)
  1805. {
  1806. m_pBEH->broadcast(tEVT);
  1807. }
  1808. else
  1809. {
  1810. //free(tData);
  1811. delete(tData);
  1812. };
  1813. }
  1814. else
  1815. {
  1816. //free(tData);
  1817. delete(tData);
  1818. };
  1819. };
  1820. setWM(CAM_WS_READY);
  1821. }
  1822. else
  1823. {
  1824. // utilizeROI( m_pCurrentROI );
  1825. setWM(CAM_WS_WAIT_ONPOS);
  1826. };
  1827. };
  1828. }
  1829. else
  1830. {
  1831. #ifdef DEBUG_EXTRA
  1832. LOG("Not Found!");
  1833. #endif
  1834. utilizeROI(m_pCurrentROI);
  1835. setWM(CAM_WS_READY);
  1836. };
  1837. };
  1838. }
  1839. else
  1840. {
  1841. // update position Info
  1842. m_fCP_Pan = m_pCurrentROI->cam_orientation.pan;
  1843. m_fCP_Tilt = m_pCurrentROI->cam_orientation.tilt;
  1844. m_fCP_Zoom = m_pCurrentROI->cam_orientation.zoom;
  1845. // Do Movement
  1846. if (! m_pProtocol->goAbsolute(m_pCurrentROI->cam_orientation.pan, m_pCurrentROI->cam_orientation.tilt, m_pCurrentROI->cam_orientation.zoom * m_fZoomCorrection))
  1847. {
  1848. ptzCommandFailed("goAbsolute");
  1849. }
  1850. else
  1851. {
  1852. // EVT_CAMERA_ON_POSITION is sent from CAM_WS_WAIT_ONPOS once the move is done
  1853. // (before, with video output it was sent right after the command).
  1854. setWM(CAM_WS_WAIT_ONPOS);
  1855. };
  1856. };
  1857. }
  1858. else
  1859. {
  1860. setWM(CAM_WS_READY);
  1861. }
  1862. break;
  1863. };
  1864. case CAM_WS_WAIT_ONPOS:
  1865. {
  1866. // LOG("WaitOnPos...");
  1867. DBC_SNAPSHOT_DATA * tData = NULL;
  1868. if (m_pCurrentROI != NULL)
  1869. {
  1870. // Send Capture event
  1871. #ifndef NO_CAMERA_FUSION_ENGINE
  1872. tData = fusion_translate_ROI(m_pCurrentROI);
  1873. #else
  1874. tData = NULL;
  1875. #endif
  1876. };
  1877. // To Accept any new ROI's ??
  1878. // setWM( CAM_WS_READY );
  1879. #ifdef DEBUG_EXTRA
  1880. LOG("Start Waiting Cam On Position!");
  1881. #endif
  1882. // Target-driven ABS / preset move: EVT_CAMERA_ON_POSITION only after the move is done.
  1883. // Protocols that cannot report the end of a move get a fixed wait.
  1884. if (! m_bNoMoveDoneReport)
  1885. {
  1886. if (! m_pProtocol->waitMoveIsDone())
  1887. {
  1888. LOG("Protocol cannot report the end of a move -> fixed wait");
  1889. m_bNoMoveDoneReport = true;
  1890. }
  1891. };
  1892. if (m_bNoMoveDoneReport)
  1893. {
  1894. SLEEP(CAM_MOVE_FIXED_WAIT_MS);
  1895. }
  1896. #ifdef DEBUG_EXTRA
  1897. LOG("Done Waiting Cam On Position!");
  1898. #endif
  1899. if (tData != NULL)
  1900. {
  1901. if (m_pBEH)
  1902. {
  1903. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  1904. if (tEVT)
  1905. {
  1906. #ifdef DEBUG_EXTRA
  1907. LOG("BCAST CAM ONPOS");
  1908. #endif
  1909. m_pBEH->broadcast(tEVT);
  1910. }
  1911. else
  1912. {
  1913. //free(tData);
  1914. delete(tData);
  1915. };
  1916. }
  1917. else
  1918. {
  1919. //free(tData);
  1920. delete(tData);
  1921. };
  1922. tData = NULL;
  1923. };
  1924. setWM(CAM_WS_READY);
  1925. break;
  1926. };
  1927. case CAM_WS_GO_HOMING_PRESET:
  1928. {
  1929. if (! m_bCameraOnHomePosition)
  1930. {
  1931. // For new Camera Presets
  1932. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  1933. if (TK)
  1934. {
  1935. #ifdef DEBUG_EXTRA
  1936. LOG("Go Home!");
  1937. LOG(TK);
  1938. #endif
  1939. if (! m_pProtocol->goPreset(TK))
  1940. {
  1941. LOG_ERROR("Cannot Issue goPreset Command!");
  1942. };
  1943. }
  1944. else
  1945. {
  1946. // Old Method
  1947. if (m_sCamData.home_token[0] != (char) 0)
  1948. {
  1949. #ifdef DEBUG_EXTRA
  1950. LOG("Go Home!");
  1951. LOG(m_sCamData.home_token);
  1952. #endif
  1953. if (! m_pProtocol->goPreset(m_sCamData.home_token))
  1954. {
  1955. LOG_ERROR("Cannot Issue goPreset Command!");
  1956. };
  1957. };
  1958. };
  1959. m_bCameraOnHomePosition = true;
  1960. m_bCurrentPresetCol = 0;
  1961. m_bCurrentPresetRow = 0;
  1962. };
  1963. setWM(CAM_WS_READY);
  1964. break;
  1965. };
  1966. case CAM_WS_GO_PRESET:
  1967. {
  1968. LOG("Go Preset!");
  1969. // if( m_sCamData.home_token[0] != (char)0 ) {
  1970. // LOG("Go Home!");
  1971. // LOG( m_sCamData.home_token );
  1972. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1973. // LOG_ERROR("Cannot Issue goPreset Command!");
  1974. // };
  1975. // };
  1976. setWM(CAM_WS_READY);
  1977. break;
  1978. };
  1979. case CAM_WS_SET_PRESET:
  1980. {
  1981. LOG("Set Preset!");
  1982. // if( m_sCamData.home_token[0] != (char)0 ) {
  1983. // LOG("Go Home!");
  1984. // LOG( m_sCamData.home_token );
  1985. // if( !m_pProtocol->goPreset( m_sCamData.home_token )){
  1986. // LOG_ERROR("Cannot Issue goPreset Command!");
  1987. // };
  1988. // };
  1989. setWM(CAM_WS_READY);
  1990. break;
  1991. };
  1992. case CAM_WS_BUSY:
  1993. {
  1994. // Handle Operation TO
  1995. setWM(CAM_WS_READY);
  1996. break;
  1997. };
  1998. case CAM_WS_READY:
  1999. {
  2000. #ifndef NO_CAMERA_FUSION_ENGINE
  2001. m_pCurrentROI = fusion_fetchROI();
  2002. #else
  2003. #endif
  2004. if (m_pCurrentROI != NULL)
  2005. {
  2006. #ifdef DEBUG_EXTRA
  2007. LOG("HAVE ROI to see!");
  2008. #endif
  2009. setWM(CAM_WS_GO_ABS_POSITION);
  2010. }
  2011. else
  2012. {
  2013. // Idle Work --->
  2014. if (m_sCamData.eco_ir)
  2015. {
  2016. m_mIRProtector.lock();
  2017. if (m_bIRIsOn)
  2018. {
  2019. if (m_bIRTO)
  2020. {
  2021. if (bNewSecondLoop)
  2022. {
  2023. m_bIRTO--;
  2024. }
  2025. }
  2026. else
  2027. {
  2028. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_OFF);
  2029. if (TK)
  2030. {
  2031. if (m_pProtocol->goPreset(TK))
  2032. {
  2033. m_bIRIsOn = false;
  2034. }
  2035. }
  2036. };
  2037. };
  2038. m_mIRProtector.unlock();
  2039. };
  2040. if (m_sCamData.homing_enable)
  2041. {
  2042. if (bNewSecondLoop)
  2043. {
  2044. if (m_nHomingCounter < m_sCamData.homing_to)
  2045. {
  2046. m_nHomingCounter++;
  2047. }
  2048. else
  2049. {
  2050. // LOG("Go Home Position!");
  2051. m_nHomingCounter = 0;
  2052. setWM(CAM_WS_GO_HOMING_PRESET);
  2053. break;
  2054. };
  2055. };
  2056. };
  2057. CAM_TASK * tTASK = fetchCamTask();
  2058. if (tTASK != NULL)
  2059. {
  2060. if (tTASK->movemet_needed)
  2061. {
  2062. CAM_PTZ_CMD * tCMD = tTASK->CMD;
  2063. if (tCMD)
  2064. {
  2065. switch (tCMD->CMD)
  2066. {
  2067. case PTZ_STOP:
  2068. {
  2069. m_nHomingCounter = 0;
  2070. setWM(CAM_WS_STOP_MOVE);
  2071. break;
  2072. };
  2073. case PTZ_MOVE:
  2074. {
  2075. if (tState != CAM_WS_AUTOMATED_MOVE)
  2076. {
  2077. m_nRelativeMoveCounter = 0;
  2078. }
  2079. m_bRelativeMoveMatrix = 0;
  2080. m_fRM_Pan = tCMD->PAN;
  2081. m_fRM_Tilt = tCMD->TILT;
  2082. m_fRM_Zoom = tCMD->ZOOM;
  2083. m_nHomingCounter = 0;
  2084. setWM(CAM_WS_AUTOMATED_MOVE);
  2085. break;
  2086. };
  2087. case PTZ_ABSOLUTE:
  2088. {
  2089. m_pCurrentROI = createCAM_ROI();
  2090. if (m_pCurrentROI != NULL)
  2091. {
  2092. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  2093. if (tTarget != NULL)
  2094. {
  2095. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  2096. tTarget->evt_db_ID = tTASK->source_evt_id;
  2097. m_pCurrentROI->targets.push_back(tTarget);
  2098. m_pCurrentROI->cam_orientation.pan = tCMD->PAN;
  2099. m_pCurrentROI->cam_orientation.tilt = tCMD->TILT;
  2100. m_pCurrentROI->cam_orientation.zoom = tCMD->ZOOM;
  2101. m_nHomingCounter = 0;
  2102. setWM(CAM_WS_GO_ABS_POSITION);
  2103. }
  2104. else
  2105. {
  2106. utilizeROI(m_pCurrentROI);
  2107. };
  2108. };
  2109. break;
  2110. };
  2111. case PTZ_RELOAD_CAMERA:
  2112. {
  2113. LOG("Exit Command Rcpt");
  2114. m_bThreadStatus = TH_ENDED;
  2115. };
  2116. };
  2117. free(tCMD);
  2118. tTASK->CMD = NULL;
  2119. };
  2120. }
  2121. else
  2122. {
  2123. LOG("No Movement -> INSTCAP");
  2124. // No Movement -> Just Capture
  2125. if (m_pBEH)
  2126. {
  2127. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, NULL);
  2128. if (tEVT)
  2129. {
  2130. tEVT->m_db_id = tTASK->source_evt_id;
  2131. m_pBEH->broadcast(tEVT);
  2132. };
  2133. };
  2134. };
  2135. free(tTASK);
  2136. }
  2137. else
  2138. {
  2139. // Lowest Priority to Web Client Commands
  2140. #ifdef DEBUG_EXTRA__
  2141. LOG("Check Web CMD!");
  2142. #endif
  2143. PUSHED = m_pDBC->getNodePushedCMD(m_sNodeData.node_id);
  2144. if (PUSHED != NULL)
  2145. {
  2146. #ifdef DEBUG_EXTRA__
  2147. LOG("HAVE Web CMD!");
  2148. #endif
  2149. m_pDBC->updateNodePushedCMD(PUSHED->id, DBC_CMD_READY_TO_CLEAN);
  2150. if ((PUSHED->CMD & ARGUS_CAM_PCMD_CMD_MASK) == ARGUS_CAM_PCMD_CMD_MASK)
  2151. {
  2152. switch (PUSHED->CMD)
  2153. {
  2154. case 0x80:
  2155. {
  2156. // Call Home Preset
  2157. LOG("CALL HOME PRESET!");
  2158. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_HOME);
  2159. if (TK)
  2160. {
  2161. LOG(TK);
  2162. if (m_pProtocol->goPreset(TK))
  2163. {
  2164. m_bCameraOnHomePosition = true;
  2165. m_nHomingCounter = 0;
  2166. };
  2167. };
  2168. break;
  2169. };
  2170. case 0x81:
  2171. {
  2172. // Call IR On Preset
  2173. LOG("CALL IR ON PRESET!");
  2174. m_mIRProtector.lock();
  2175. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_ON);
  2176. if (TK)
  2177. {
  2178. LOG(TK);
  2179. if (m_pProtocol->goPreset(TK))
  2180. {
  2181. m_bIRIsOn = true;
  2182. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2183. };
  2184. }
  2185. else
  2186. {
  2187. LOG("PRESET NOT FOUND!");
  2188. }
  2189. m_mIRProtector.unlock();
  2190. break;
  2191. };
  2192. case 0x82:
  2193. {
  2194. // Call IR Off Preset
  2195. LOG("CALL IR OFF PRESET!");
  2196. m_mIRProtector.lock();
  2197. CSTR TK = (m_sCamData.eco_ir) ? getPresetToken(ARGUS_PRESET_TYPE_IR_OFF) : getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2198. if (TK)
  2199. {
  2200. LOG(TK);
  2201. if (m_pProtocol->goPreset(TK))
  2202. {
  2203. m_bIRIsOn = false;
  2204. m_bIRTO = 0;
  2205. };
  2206. }
  2207. else
  2208. {
  2209. LOG("PRESET NOT FOUND!");
  2210. }
  2211. m_mIRProtector.unlock();
  2212. break;
  2213. };
  2214. case 0x83:
  2215. {
  2216. // Call IR Auto Preset
  2217. LOG("CALL IR AUTO PRESET!");
  2218. m_mIRProtector.lock();
  2219. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2220. if (TK)
  2221. {
  2222. LOG(TK);
  2223. if (m_pProtocol->goPreset(TK))
  2224. {
  2225. m_bIRIsOn = true;
  2226. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2227. };
  2228. }
  2229. else
  2230. {
  2231. LOG("PRESET NOT FOUND!");
  2232. }
  2233. m_mIRProtector.unlock();
  2234. break;
  2235. };
  2236. case 0x84:
  2237. {
  2238. // Call Wisher Preset
  2239. LOG("CALL WIPER PRESET!");
  2240. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_WISHER);
  2241. if (TK)
  2242. {
  2243. LOG(TK);
  2244. m_pProtocol->goPreset(TK);
  2245. };
  2246. break;
  2247. };
  2248. };
  2249. }
  2250. else if (tState == CAM_WS_READY || tState == CAM_WS_AUTOMATED_MOVE)
  2251. { // No Way to get here
  2252. // Direct Movment Mode
  2253. if (PUSHED->CMD == ARGUS_CAM_PCMD_STOP)
  2254. {
  2255. m_nHomingCounter = 0;
  2256. setWM(CAM_WS_STOP_MOVE);
  2257. }
  2258. else
  2259. {
  2260. //char tB[64];
  2261. if (tState != CAM_WS_AUTOMATED_MOVE)
  2262. {
  2263. m_nRelativeMoveCounter = 0;
  2264. }
  2265. m_bRelativeMoveMatrix = (PUSHED->CMD) >> 1;
  2266. m_nHomingCounter = 0;
  2267. setWM(CAM_WS_AUTOMATED_MOVE);
  2268. };
  2269. };
  2270. free(PUSHED);
  2271. PUSHED = NULL;
  2272. };
  2273. };
  2274. };
  2275. break;
  2276. };
  2277. case CAM_WS_END:
  2278. {
  2279. LOG("Exit Command Rcpt");
  2280. m_bThreadStatus = TH_ENDED;
  2281. break;
  2282. };
  2283. };
  2284. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  2285. };
  2286. #ifdef DEBUG
  2287. LOG("CTRL Loop End!");
  2288. #endif
  2289. }
  2290. bool deviceNodeCamera::setNoPositionWait(bool FL)
  2291. {
  2292. m_bSkeepPositioningWait = FL;
  2293. return true;
  2294. }
  2295. bool deviceNodeCamera::testFOV(C3D_DECART L)
  2296. {
  2297. // System Coords -> trans to local and go to funct
  2298. systemAnchor * tSystem = systemAnchor::getInstance();
  2299. if (tSystem == NULL)
  2300. {
  2301. return false;
  2302. }
  2303. C3D_SPH_DEGREES SL;
  2304. #ifdef DEBUG_EXTRA
  2305. LOG("Test FOV local Decart coords");
  2306. #endif
  2307. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2308. {
  2309. return false;
  2310. }
  2311. return testFOV(SL);
  2312. }
  2313. bool deviceNodeCamera::testFOV(C3D_WGS84_LOC L)
  2314. {
  2315. // Global Coords
  2316. systemAnchor * tSystem = systemAnchor::getInstance();
  2317. if (tSystem == NULL)
  2318. {
  2319. return false;
  2320. }
  2321. C3D_SPH_DEGREES SL;
  2322. #ifdef DEBUG_EXTRA
  2323. LOG("Test FOV WGS coords");
  2324. #endif
  2325. if (! tSystem->convertToLocal(&m_oPosition, L, SL))
  2326. {
  2327. return false;
  2328. }
  2329. return testFOV(SL);
  2330. }
  2331. bool deviceNodeCamera::testFOV(C3D_SPH_RADIANS L)
  2332. {
  2333. // Local Coords
  2334. C3D_SPH_DEGREES SL;
  2335. #ifdef DEBUG_EXTRA
  2336. LOG("Test FOV local Spherical(rad) coords");
  2337. #endif
  2338. spherical2spherical(L, SL);
  2339. return testFOV(SL);
  2340. }
  2341. bool deviceNodeCamera::testFOV(C3D_SPH_DEGREES SL)
  2342. {
  2343. #ifdef DEBUG_EXTRA
  2344. char tB[128];
  2345. #endif
  2346. #ifdef DEBUG_EXTRA
  2347. LOG("Test FOV local Spherical(deg) coords");
  2348. #endif
  2349. switch (m_sCamData.triggering_mode)
  2350. {
  2351. case CAM_TRIGGER_STRICT:
  2352. {
  2353. if (SL.r > m_fMaxRange || SL.r < m_fMinRange)
  2354. {
  2355. #ifdef DEBUG_EXTRA
  2356. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2357. LOG(tB);
  2358. #endif
  2359. return false;
  2360. };
  2361. break;
  2362. };
  2363. case CAM_TRIGGER_ALL_NEAR:
  2364. {
  2365. if (SL.r > m_fMaxRange)
  2366. {
  2367. #ifdef DEBUG_EXTRA
  2368. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2369. LOG(tB);
  2370. #endif
  2371. return false;
  2372. };
  2373. break;
  2374. };
  2375. case CAM_TRIGGER_ALL_FAR:
  2376. {
  2377. if (SL.r < m_fMinRange)
  2378. {
  2379. #ifdef DEBUG_EXTRA
  2380. sprintf(tB, "Target Range( %.3f.m ) is not in a Range %.3f.m <-> %.3f.m", SL.r, m_fMinRange, m_fMaxRange);
  2381. LOG(tB);
  2382. #endif
  2383. return false;
  2384. };
  2385. break;
  2386. };
  2387. case CAM_TRIGGER_ALL: return true;
  2388. };
  2389. // float HS = AC_imager_width[m_sCamData.imager_type];
  2390. // float VS = AC_imager_height[m_sCamData.imager_type];
  2391. // float HS2 = HS * HS;
  2392. // float VS2 = VS * VS;
  2393. // float IR = HS / VS;
  2394. // float IR2 = IR * IR;
  2395. if (m_sCamData.pan_angles == 0)
  2396. {
  2397. // Fixed Camera
  2398. float tA = fabsf(SL.fi);
  2399. float tZ = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2400. if (tA > tZ)
  2401. {
  2402. return false;
  2403. }
  2404. }
  2405. else
  2406. {
  2407. if (m_sCamData.pan_angles < 360)
  2408. { // If endless PAN -> PASSED
  2409. // Partial Pan
  2410. switch (m_sCamData.pan_model)
  2411. {
  2412. case 1:
  2413. {
  2414. // Regular Pan
  2415. float tFOV = 0.5f * (m_fMinHFov + (SL.r - m_fMinRange) * m_fR2FL);
  2416. float tRad = deg2rad(SL.fi);
  2417. #ifdef DEBUG_EXTRA
  2418. sprintf(tB, "FOV: %0.3f CC: %0.3f", tFOV, tRad);
  2419. LOG(tB);
  2420. #endif
  2421. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.pan_angles) + tFOV)))
  2422. {
  2423. return false;
  2424. }
  2425. break;
  2426. };
  2427. case 2:
  2428. {
  2429. break;
  2430. };
  2431. case 3:
  2432. {
  2433. break;
  2434. };
  2435. };
  2436. };
  2437. };
  2438. if (m_sCamData.tilt_angles == 0)
  2439. {
  2440. // Fixed Camera
  2441. if ((SL.tetta < (m_fMaxVFov / -2.0f)) || (SL.tetta > (m_fMaxVFov / 2.0f)))
  2442. {
  2443. return false;
  2444. }
  2445. }
  2446. else
  2447. {
  2448. if (m_sCamData.tilt_angles < 90)
  2449. { // if Cam CAN DO 90 -> Pass
  2450. // Partial Tilt
  2451. switch (m_sCamData.tilt_model)
  2452. {
  2453. case 1:
  2454. {
  2455. // Regular Tilt
  2456. float tFOV = 0.5f * (m_fMinVFov + (m_fMaxVFov - m_fMinVFov) * (SL.r / m_fMaxRange));
  2457. float tRad = deg2rad(SL.tetta);
  2458. if ((tRad < (-1.0 * tFOV)) || (tRad > (deg2rad(m_sCamData.tilt_angles) + tFOV)))
  2459. {
  2460. return false;
  2461. }
  2462. break;
  2463. };
  2464. case 2:
  2465. {
  2466. break;
  2467. };
  2468. };
  2469. };
  2470. };
  2471. return true;
  2472. }
  2473. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_WGS84_LOC L)
  2474. {
  2475. systemAnchor * tSystem = systemAnchor::getInstance();
  2476. if (tSystem == NULL)
  2477. {
  2478. return false;
  2479. }
  2480. C3D_SPH_DEGREES SL;
  2481. #ifdef DEBUG_EXTRA
  2482. LOG("Look at WGS coords!");
  2483. #endif
  2484. tSystem->convertToLocal(&m_oPosition, L, SL);
  2485. return lookAt(TargetRefID, SL);
  2486. }
  2487. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_DECART L)
  2488. {
  2489. systemAnchor * tSystem = systemAnchor::getInstance();
  2490. if (tSystem == NULL)
  2491. {
  2492. LOG_ERROR("Cant Get Anchor!");
  2493. return false;
  2494. }
  2495. C3D_SPH_DEGREES SL;
  2496. #ifdef DEBUG_EXTRA
  2497. LOG("Look at System Decart coords!");
  2498. #endif
  2499. tSystem->convertToLocal(&m_oPosition, L, SL);
  2500. return lookAt(TargetRefID, SL);
  2501. }
  2502. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_RADIANS L)
  2503. {
  2504. C3D_SPH_DEGREES SL;
  2505. #ifdef DEBUG_EXTRA
  2506. LOG("Look at spherical(rad) 2 spherical(deg) coords!");
  2507. #endif
  2508. spherical2spherical(L, SL);
  2509. return lookAt(TargetRefID, SL);
  2510. }
  2511. bool deviceNodeCamera::lookAt(uint32_t TargetRefID, C3D_SPH_DEGREES L)
  2512. {
  2513. if (m_bCameraIsStatic)
  2514. {
  2515. //DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) malloc(sizeof(DBC_SNAPSHOT_DATA));
  2516. DBC_SNAPSHOT_DATA * tData = new DBC_SNAPSHOT_DATA();
  2517. if (tData)
  2518. {
  2519. //memset(tData, 0, sizeof(DBC_SNAPSHOT_DATA));
  2520. tData->stream_id = m_sNodeData.aux_parameter;
  2521. tData->assoc_event_ids.push_back(TargetRefID);
  2522. if (m_pBEH)
  2523. {
  2524. EVENT_CONTAINER * tEVT = createBasicEvent(m_sNodeData.node_id, EVT_CLASS_CAMERA_CTRL, EVT_CAMERA_ON_POSITION, NULL, NULL, tData);
  2525. if (tEVT)
  2526. {
  2527. #ifdef DEBUG_EXTRA
  2528. LOG("INSTANT BCAST CAM ONPOS");
  2529. #endif
  2530. m_pBEH->broadcast(tEVT);
  2531. }
  2532. else
  2533. {
  2534. //free(tData);
  2535. delete(tData);
  2536. };
  2537. }
  2538. else
  2539. {
  2540. //free(tData);
  2541. delete(tData);
  2542. };
  2543. };
  2544. return true;
  2545. };
  2546. // char tB[128];
  2547. // sprintf(tB, "Target On %.3f %.3f %.3f", L.r, L.fi, L.tetta );
  2548. // LOG( tB );
  2549. // sprintf( tB, "Cam GoTo %.3f %.3f %.3f", fP, fT, fZ );
  2550. // LOG( tB );
  2551. #ifdef DEBUG_EXTRA
  2552. LOG("Look at local spherical deg. coords!(final)");
  2553. #endif
  2554. // if( getWM() == CAM_WS_READY ){
  2555. // C3D_ORIENT_ONVIF LL;
  2556. // spherical2onvif( L, LL, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles );
  2557. // return absoluteMove( TargetRefID, LL );
  2558. // };
  2559. // Camera is Busy
  2560. if (m_bShineOnTarget)
  2561. {
  2562. m_mIRProtector.lock();
  2563. if (! m_bIRIsOn)
  2564. {
  2565. CSTR TK = getPresetToken(ARGUS_PRESET_TYPE_IR_AUTO);
  2566. if (TK)
  2567. {
  2568. if (m_pProtocol->goPreset(TK))
  2569. {
  2570. m_bIRIsOn = true;
  2571. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2572. };
  2573. };
  2574. }
  2575. else
  2576. {
  2577. m_bIRTO = DEFAULT_SHINE_ON_TARGET_TO;
  2578. }
  2579. m_mIRProtector.unlock();
  2580. };
  2581. // targeting by Presets -> instant ROI
  2582. if (m_bNoAbsPositionning)
  2583. {
  2584. CAM_ROI * tROI = createCAM_ROI();
  2585. if (tROI != NULL)
  2586. {
  2587. CAM_TRACK_RECORD * tTarget = (CAM_TRACK_RECORD *) malloc(sizeof(CAM_TRACK_RECORD));
  2588. if (tTarget != NULL)
  2589. {
  2590. memset(tTarget, 0, sizeof(CAM_TRACK_RECORD));
  2591. tTarget->evt_db_ID = TargetRefID;
  2592. tROI->targets.push_back(tTarget);
  2593. tROI->ts_ms = getMilliseconds();
  2594. spherical2onvif(L, tROI->cam_orientation, m_fMaxRange, m_sCamData.pan_model, m_sCamData.pan_angles, m_sCamData.tilt_model, m_sCamData.tilt_angles);
  2595. tROI->cam_position.fi = L.fi;
  2596. tROI->cam_position.tetta = L.tetta;
  2597. tROI->cam_position.r = L.r;
  2598. #ifndef NO_CAMERA_FUSION_ENGINE
  2599. m_mROIsProtector.lock();
  2600. m_vROIS.push_back(tROI);
  2601. m_mROIsProtector.unlock();
  2602. #endif
  2603. }
  2604. else
  2605. {
  2606. LOG_ERROR("No MEM 4 TARGTEG DATA");
  2607. utilizeROI(tROI);
  2608. };
  2609. }
  2610. else
  2611. {
  2612. LOG_ERROR("No MEM 4 ROI");
  2613. }
  2614. return true;
  2615. };
  2616. #ifndef NO_CAMERA_FUSION_ENGINE
  2617. return fusion_consumeData(TargetRefID, L);
  2618. #else
  2619. return true;
  2620. #endif
  2621. }
  2622. bool deviceNodeCamera::makeSnap(uint32_t TargetRefID)
  2623. {
  2624. CAM_TASK * tTask = (CAM_TASK *) malloc(sizeof(CAM_TASK));
  2625. if (tTask)
  2626. {
  2627. memset(tTask, 0, sizeof(CAM_TASK));
  2628. tTask->CMD = (CAM_PTZ_CMD *) malloc(sizeof(CAM_PTZ_CMD));
  2629. if (tTask->CMD)
  2630. {
  2631. tTask->CMD->CMD = PTZ_CAPTURE_ONLY;
  2632. tTask->movemet_needed = false;
  2633. tTask->source_evt_id = TargetRefID;
  2634. return pushCamTask(tTask);
  2635. }
  2636. else
  2637. {
  2638. free(tTask);
  2639. };
  2640. };
  2641. return false;
  2642. }
  2643. bool deviceNodeCamera::connectToStream(uint32_t id)
  2644. {
  2645. /*
  2646. if( id ){
  2647. argusKernelModule * tKM = NULL;
  2648. argusKernel * tKRNL = NULL;
  2649. argusModuleFactory * tMF = NULL;
  2650. if( m_pDBC == NULL ) {
  2651. LOG_ERROR("No Database Connector!");
  2652. return false;
  2653. };
  2654. tKRNL = argusKernel::getInstance();
  2655. if( tKRNL ) {
  2656. tMF = tKRNL->getModuleFactory();
  2657. if( tMF ) {
  2658. tKM = tMF->getModule( ARGUS_MODULE_EMBED_VIDEO );
  2659. } else {
  2660. LOG_ERROR("No ModuleFactory Instance!");
  2661. return false;
  2662. };
  2663. } else {
  2664. LOG_ERROR("No Kernel Instance!");
  2665. return false;
  2666. };
  2667. if( tKM ) {
  2668. // New Module
  2669. DBC_MEDIA_STREAM_DATA *tDATA = m_pDBC->getMediaStreamData( id );
  2670. if( tDATA ) {
  2671. m_sMediaSource.mediaStreamId = id;
  2672. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2673. if (m_pSource != NULL) delete((videoAVLIBSource*)m_pSource);
  2674. m_pSource = (basicMediaSource *) new videoAVLIBSource(&m_sMediaSource);
  2675. } else {
  2676. if (m_pSource != NULL) delete((videoLIVE555Source*)m_pSource);
  2677. m_pSource = (basicMediaSource *) new videoLIVE555Source(&m_sMediaSource );
  2678. };
  2679. if( m_pSource != NULL ) {
  2680. char tMRL[1024];
  2681. #ifdef DEBUG
  2682. LOG("Source OK");
  2683. #endif
  2684. ((videoLIVE555Source*)m_pSource)->setWM( MS_MODE_STREAM_PROXY | MS_MODE_OVERLAYED_LOOPS | MS_MODE_DECODE_FRAMES );
  2685. int tW = ((argusModuleEmbedVideo*)tKM)->getWidh();
  2686. int tH = ((argusModuleEmbedVideo*)tKM)->getHeight();
  2687. int pix = -1;
  2688. bool in_16_9 = (((tDATA->width*100) / tDATA->height ) > 140 );
  2689. bool out_16_9 = (((tW*100) / tH ) > 140 );
  2690. updateOpticalParameters( tW, tH );
  2691. if( strlen( tDATA->c_type ) ){
  2692. };
  2693. //if( tW > 0 ){
  2694. // //m_pSource->setProportionalByWidth( tW );
  2695. // m_pSource->setSize( tW, tH );
  2696. //};
  2697. if( in_16_9 != out_16_9 ){
  2698. if( in_16_9 ){
  2699. tH = ((( tW * 1000 ) / 16 ) * 9 ) / 1000;
  2700. } else {
  2701. tW = ((( tH * 1000 ) / 3 ) * 4 ) / 1000;
  2702. };
  2703. };
  2704. if (strncmp(tDATA->mrl_proto, "v4l2", 4) == 0) {
  2705. sprintf( tMRL, tDATA->mrl );
  2706. } else {
  2707. DBC_CAM_DATA *tCAM = m_pDBC->getCamData( tDATA->source_id );
  2708. if( tCAM ) {
  2709. sprintf( tMRL, "%s://%s:%s@%s:%d%s", tDATA->mrl_proto, tCAM->username, tCAM->password, tCAM->ip, tDATA->mrl_port, tDATA->mrl );
  2710. free( tCAM );
  2711. } else {
  2712. LOG_ERROR("Error Getting CAM Data!");
  2713. delete( m_pSource );
  2714. m_pSource = NULL;
  2715. return false;
  2716. };
  2717. };
  2718. LOG( tMRL );
  2719. if( m_pSource->_open( tMRL, false, -1.0f, pix, tDATA->width, tDATA->height, tW, tH ) ){
  2720. m_bSourceConnected = m_pSource->attachDestination( (argusModuleEmbedVideo*) tKM );
  2721. if ( !m_bSourceConnected ) {
  2722. //delete( m_pSource );
  2723. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2724. delete((videoAVLIBSource*)m_pSource);
  2725. } else {
  2726. delete((videoLIVE555Source*)m_pSource);
  2727. };
  2728. m_pSource = NULL;
  2729. } else {
  2730. m_nActiveStreamID = id;
  2731. #ifdef DEBUG
  2732. LOG("Source is Connected to Output.");
  2733. #endif
  2734. };
  2735. } else {
  2736. LOG_ERROR("Can not Open Video Source!");
  2737. //delete( m_pSource );
  2738. if( strncmp(tDATA->mrl_proto, "v4l2", 4) == 0 ) {
  2739. delete((videoAVLIBSource*)m_pSource);
  2740. } else {
  2741. delete((videoLIVE555Source*)m_pSource);
  2742. };
  2743. m_pSource = NULL;
  2744. };
  2745. } else {
  2746. LOG_ERROR("Video Source Stream Failure!");
  2747. };
  2748. free( tDATA );
  2749. };
  2750. }
  2751. #ifdef DEBUG
  2752. else LOG("No Video Module!")
  2753. #endif
  2754. ;
  2755. };
  2756. LOG("Connect Finished!");
  2757. return m_bSourceConnected;
  2758. */
  2759. if ( id ) return true;
  2760. return true;
  2761. }
  2762. bool deviceNodeCamera::disconnectFromStream(void)
  2763. {
  2764. /*if( m_bSourceConnected ) {
  2765. if( m_pSource ){
  2766. delete( m_pSource );
  2767. m_pSource = NULL;
  2768. };
  2769. m_bSourceConnected = false;
  2770. };
  2771. return !m_bSourceConnected;
  2772. */
  2773. return true;
  2774. }
  2775. bool deviceNodeCamera::validateStream(void)
  2776. {
  2777. /*
  2778. if( m_bSourceConnected ) {
  2779. if( m_pSource ) {
  2780. if( m_pSource->isAttached() ){
  2781. if( m_pSource->getStatus() < MS_STOPED ) return true;
  2782. #ifdef DEBUG
  2783. LOG("Source is Finished!");
  2784. #endif
  2785. };
  2786. #ifdef DEBUG
  2787. LOG("Source is Disconnected!");
  2788. #endif
  2789. } else LOG_ERROR("No Source Object! Set Disconnected!");
  2790. disconnectFromStream();
  2791. };
  2792. return false;
  2793. */
  2794. return true;
  2795. }
  2796. /*
  2797. Which camera fields changed (DB record a -> b)?
  2798. - runtime state written while running (online, status, session, max_range) and discovery data (uuid, mac):
  2799. ignored. Before, any difference rebuilt the node: CAM_WS_INIT set m_sCamData.online = false before
  2800. re-creating the PTZ protocol and wrote the DB only afterwards, so a validate() during a slow ONVIF
  2801. re-init deleted the camera (and dropped its video stream); so did the ONVIF discovery writing online=1.
  2802. - cosmetic (name, hardware): taken over, no action
  2803. - PTZ protocol only (onvif_ctrl, onvif_access, port, auth_mode, ptz_token): the protocol is re-created
  2804. - anything else (ip, credentials and stream_id used by the stream, type, geometry ...): node rebuilt
  2805. */
  2806. typedef enum E_CAM_DATA_CHANGE
  2807. {
  2808. CAM_CHANGE_NONE = 0,
  2809. CAM_CHANGE_COSMETIC = 1,
  2810. CAM_CHANGE_PROTOCOL = 2,
  2811. CAM_CHANGE_NODE = 3
  2812. } CAM_DATA_CHANGE;
  2813. static CAM_DATA_CHANGE camDataChange(const DBC_CAM_DATA * a, const DBC_CAM_DATA * b, const char ** what)
  2814. {
  2815. DBC_CAM_DATA tA, tB;
  2816. memcpy(&tA, a, sizeof(tA));
  2817. memcpy(&tB, b, sizeof(tB));
  2818. *what = "";
  2819. // runtime / discovery fields: never a change
  2820. tA.online = tB.online;
  2821. tA.status = tB.status;
  2822. memcpy(tA.session, tB.session, sizeof(tA.session));
  2823. tA.max_range = tB.max_range;
  2824. memcpy(tA.uuid, tB.uuid, sizeof(tA.uuid));
  2825. memcpy(tA.mac, tB.mac, sizeof(tA.mac));
  2826. // PTZ protocol fields
  2827. bool tProto = (tA.port != tB.port) || (tA.auth_mode != tB.auth_mode) || strncmp(tA.onvif_ctrl, tB.onvif_ctrl, sizeof(tA.onvif_ctrl)) != 0 ||
  2828. strncmp(tA.onvif_access, tB.onvif_access, sizeof(tA.onvif_access)) != 0 || strncmp(tA.ptz_token, tB.ptz_token, sizeof(tA.ptz_token)) != 0;
  2829. tA.port = tB.port;
  2830. tA.auth_mode = tB.auth_mode;
  2831. memcpy(tA.onvif_ctrl, tB.onvif_ctrl, sizeof(tA.onvif_ctrl));
  2832. memcpy(tA.onvif_access, tB.onvif_access, sizeof(tA.onvif_access));
  2833. memcpy(tA.ptz_token, tB.ptz_token, sizeof(tA.ptz_token));
  2834. // cosmetic fields
  2835. bool tCosm = strncmp(tA.name, tB.name, sizeof(tA.name)) != 0 || strncmp(tA.hardware, tB.hardware, sizeof(tA.hardware)) != 0;
  2836. memcpy(tA.name, tB.name, sizeof(tA.name));
  2837. memcpy(tA.hardware, tB.hardware, sizeof(tA.hardware));
  2838. // everything else (also fields added later) needs the node rebuilt
  2839. if (memcmp(&tA, &tB, sizeof(tA)) != 0)
  2840. {
  2841. if (strncmp(a->ip, b->ip, sizeof(a->ip)) != 0)
  2842. {
  2843. *what = "ip";
  2844. }
  2845. else if (strncmp(a->username, b->username, sizeof(a->username)) != 0 || strncmp(a->password, b->password, sizeof(a->password)) != 0)
  2846. {
  2847. *what = "credentials";
  2848. }
  2849. else if (a->stream_id != b->stream_id)
  2850. {
  2851. *what = "stream";
  2852. }
  2853. else if (a->protocol != b->protocol)
  2854. {
  2855. *what = "control protocol";
  2856. }
  2857. else
  2858. {
  2859. *what = "camera parameters";
  2860. };
  2861. return CAM_CHANGE_NODE;
  2862. };
  2863. if (tProto)
  2864. {
  2865. *what = "PTZ access";
  2866. return CAM_CHANGE_PROTOCOL;
  2867. };
  2868. return tCosm ? CAM_CHANGE_COSMETIC : CAM_CHANGE_NONE;
  2869. }
  2870. // Camera thread: take over the fields validate() found changed (only cosmetic / PTZ protocol fields)
  2871. void deviceNodeCamera::applyPendingCamData(void)
  2872. {
  2873. if (! m_bPendingCamData)
  2874. {
  2875. return;
  2876. };
  2877. m_mPendingCamData.lock();
  2878. DBC_CAM_DATA * tN = &m_sPendingCamData;
  2879. memcpy(m_sCamData.name, tN->name, sizeof(m_sCamData.name));
  2880. memcpy(m_sCamData.hardware, tN->hardware, sizeof(m_sCamData.hardware));
  2881. m_sCamData.port = tN->port;
  2882. m_sCamData.auth_mode = tN->auth_mode;
  2883. memcpy(m_sCamData.onvif_ctrl, tN->onvif_ctrl, sizeof(m_sCamData.onvif_ctrl));
  2884. memcpy(m_sCamData.onvif_access, tN->onvif_access, sizeof(m_sCamData.onvif_access));
  2885. memcpy(m_sCamData.ptz_token, tN->ptz_token, sizeof(m_sCamData.ptz_token));
  2886. bool tReinit = m_bPendingProtoReinit;
  2887. m_bPendingCamData = false;
  2888. m_bPendingProtoReinit = false;
  2889. m_mPendingCamData.unlock();
  2890. if (tReinit && m_sCamData.protocol != 0)
  2891. {
  2892. LOG("PTZ access settings changed - re-initialising the PTZ protocol (camera and stream kept)");
  2893. m_mPtzHealth.lock();
  2894. m_sPtzHealth.next_retry = 0; // new settings: no back-off
  2895. m_sPtzHealth.failed_inits = 0;
  2896. m_mPtzHealth.unlock();
  2897. setWM(CAM_WS_INIT);
  2898. };
  2899. }
  2900. bool deviceNodeCamera::validate()
  2901. {
  2902. if (m_pDBC == NULL)
  2903. {
  2904. return false;
  2905. }
  2906. DBC_CAM_DATA * tDATA = m_pDBC->getCamData(m_sCamData.id);
  2907. if (tDATA == NULL)
  2908. {
  2909. // Probably a failed read (SQLite busy), not a deleted camera: rebuild only when it stays missing
  2910. if (++m_nNodeDataMisses < NODE_DATA_MISSES_TO_REBUILD)
  2911. {
  2912. return true;
  2913. };
  2914. LOG_ERROR("Camera record not found - rebuilding the camera");
  2915. return false;
  2916. }
  2917. const char * tWhat = "";
  2918. CAM_DATA_CHANGE tChange = camDataChange(&m_sCamData, tDATA, &tWhat);
  2919. if (tChange == CAM_CHANGE_NODE)
  2920. {
  2921. char tB[128];
  2922. snprintf(tB, sizeof(tB), "Camera settings changed (%s) - rebuilding the camera", tWhat);
  2923. LOG(tB);
  2924. free(tDATA);
  2925. return false;
  2926. };
  2927. if (tChange != CAM_CHANGE_NONE)
  2928. {
  2929. m_mPendingCamData.lock();
  2930. memcpy(&m_sPendingCamData, tDATA, sizeof(DBC_CAM_DATA));
  2931. m_bPendingProtoReinit = m_bPendingProtoReinit || (tChange == CAM_CHANGE_PROTOCOL);
  2932. m_bPendingCamData = true;
  2933. m_mPendingCamData.unlock();
  2934. };
  2935. free(tDATA);
  2936. // Only a real, lasting PTZ problem rebuilds the camera (and drops its stream)
  2937. m_mPtzHealth.lock();
  2938. bool tGiveUp = (m_sPtzHealth.state == PTZ_HEALTH_FAILED) && (m_sPtzHealth.failed_inits >= CAM_REBUILD_AFTER_FAILED_INITS);
  2939. uint32_t tFails = m_sPtzHealth.failed_inits + 1;
  2940. m_mPtzHealth.unlock();
  2941. if (tGiveUp)
  2942. {
  2943. char tB[128];
  2944. snprintf(tB, sizeof(tB), "PTZ protocol failed %u initialisations in a row - rebuilding the camera", tFails);
  2945. LOG_ERROR(tB);
  2946. return false;
  2947. };
  2948. if (! validateStream())
  2949. {
  2950. return false;
  2951. }
  2952. return deviceNode::validate();
  2953. }
  2954. bool deviceNodeCamera::load_presets(void)
  2955. {
  2956. bool bHaveIROFF = false;
  2957. bool bHaveIRON = false;
  2958. bool bHaveIRAUTO = false;
  2959. // Request PRESETS FROM CAMERA?! for some PTZ cameras without presets inpossible to
  2960. // operate by presets
  2961. if (m_pProtocol->getCameraPresets())
  2962. {
  2963. LOG("Camera Presets Loadded");
  2964. }
  2965. else
  2966. {
  2967. LOG_ERROR("Error while loadding Camera Presets!");
  2968. }
  2969. if (m_pDBC == NULL)
  2970. {
  2971. return false;
  2972. }
  2973. DBC_PRESETS_INFO * tData = m_pDBC->getCamPresetInfo(m_sCamData.id);
  2974. if (tData)
  2975. {
  2976. //memcpy(&m_sCamPresets, tData, sizeof(DBC_PRESETS_INFO));
  2977. m_sCamPresets.maxCount = tData->maxCount;
  2978. m_sCamPresets.gRows = tData->gRows;
  2979. m_sCamPresets.gCols = tData->gCols;
  2980. m_sCamPresets.presets.clear();
  2981. #ifdef DEBUG
  2982. char tB[128];
  2983. LOG("Loading Presets!");
  2984. sprintf(tB, "Cam Presets Info: CNT: %d Cols: %d Rows: %d ", m_sCamPresets.maxCount, m_sCamPresets.gCols, m_sCamPresets.gRows);
  2985. LOG(tB);
  2986. #endif
  2987. DBC_INT_LIST * tList = m_pDBC->listCamPresets(m_sCamData.id);
  2988. if (tList)
  2989. {
  2990. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  2991. {
  2992. CAM_PRESET * tData = m_pDBC->getCamPreset(*idx);
  2993. if (tData)
  2994. {
  2995. m_sCamPresets.presets.push_back(tData);
  2996. bHaveIRON = (tData->role == ARGUS_PRESET_TYPE_IR_ON) ? true : bHaveIRON;
  2997. bHaveIROFF = (tData->role == ARGUS_PRESET_TYPE_IR_OFF) ? true : bHaveIROFF;
  2998. bHaveIRAUTO = (tData->role == ARGUS_PRESET_TYPE_IR_AUTO) ? true : bHaveIRAUTO;
  2999. };
  3000. };
  3001. delete(tList);
  3002. };
  3003. if (m_sCamData.eco_ir)
  3004. {
  3005. m_bShineOnTarget = (bHaveIROFF && bHaveIRAUTO);
  3006. }
  3007. else
  3008. {
  3009. m_bShineOnTarget = false;
  3010. }
  3011. return true;
  3012. #ifdef DEBUG
  3013. }
  3014. else
  3015. {
  3016. LOG("No Camera Presets!");
  3017. #endif
  3018. };
  3019. return false;
  3020. }
  3021. void deviceNodeCamera::translateROI2PRESET(float P, float T, float Z, uint8_t & col, uint8_t & row)
  3022. {
  3023. //char tB[128];
  3024. if ( T ) Z = Z;
  3025. row = (uint8_t)(Z * ((float)m_sCamPresets.gRows));
  3026. if (P < 0.5)
  3027. {
  3028. P = P + 1.5f;
  3029. }
  3030. else
  3031. {
  3032. P = P - 0.5f;
  3033. };
  3034. col = (uint8_t) (P * ((float) (m_sCamPresets.gCols * (1 << row)) / 2.0f));
  3035. row++;
  3036. }
  3037. CSTR deviceNodeCamera::getPresetToken(uint8_t CCOL, uint8_t CROW)
  3038. {
  3039. if (m_sCamPresets.presets.size() > 0)
  3040. {
  3041. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  3042. {
  3043. if ((*idx)->role == ARGUS_PRESET_TYPE_POSITION)
  3044. {
  3045. if (((*idx)->row == CROW) && ((*idx)->col == CCOL))
  3046. {
  3047. return (CSTR) (*idx)->token;
  3048. }
  3049. }
  3050. }
  3051. }
  3052. return NULL;
  3053. }
  3054. CSTR deviceNodeCamera::getPresetToken(uint8_t specialRole)
  3055. {
  3056. if (m_sCamPresets.presets.size() > 0)
  3057. {
  3058. for (V_PRESETS::iterator idx = m_sCamPresets.presets.begin(); idx < m_sCamPresets.presets.end(); idx++)
  3059. {
  3060. if ((*idx)->role == specialRole)
  3061. {
  3062. return (CSTR) (*idx)->token;
  3063. }
  3064. }
  3065. }
  3066. return NULL;
  3067. }
  3068. #undef LOG
  3069. #undef LOG_ERROR
  3070. //#undef DEBUG_EXTRA