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.

6223 lines
155 KiB

Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 week ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel loop: move long blocking work out of it Every pass of the mojo kernel loop ran these synchronously; each could stall the loop (and get la_mojo killed by the watchdog): - Report cleanup (validateReports -> deleteReportDate): deleteRDFDir ran "rm -f -R <whole day>" (seconds to minutes on an SD card), daily or every second while the disk is above preserve_empty. The day is now deleted in a background thread (one at a time; calls during it return false). ~sqliteDBC waits for a running deletion (max 60 s). - MediaMTX (validateService -> restart): "systemctl restart" waited for the service to stop and start, every second if the config kept failing validation. Now "systemctl --no-block restart" (only queues the job), at most once per KH_MTX_RESTART_MIN_S (30 s). - IP UART serial provider (hardwareManager::validate -> create_SOL): a blocking connect() to an address that doesn't answer took ~2 min (SYN retries). Now non-blocking with SERIAL_SOL_CONNECT_TIMEOUT_MS (5 s), the socket mode restored afterwards, and the socket closed on failure (it leaked). - Ethernet2UART gateway nodes: hardwareManager::validate (kernel loop) validated and reconnected them again, although the node factory thread does it (deviceNode::validate). Connects of up to 5 s blocked the kernel loop, plus the wait for the node's connection lock. hardwareResource::selfValidating() (default false) is true for deviceETH2UART, and the hardware manager skips such resources. Tested: the connect timeout on its own (extracted unchanged). A peer that drops the SYN failed after 5002 ms ("Connection timed out") instead of ~2 min; refused and accepted connects are immediate; the socket is blocking again in every case. 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
1 day 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
1 day 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
1 day 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
1 day 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
1 day 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
1 day 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
1 day 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
1 day 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
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "SQLite_dataBinder.h"
  2. #include <atomic>
  3. #include <string>
  4. #include <thread>
  5. // A report day is being deleted in the background (deleteReportDate)
  6. static std::atomic<bool> s_bReportDayDeleting(false);
  7. #include "../../components/base/basicEvent.h"
  8. #define LOG(X) m_pLog->Log("SDBC", this, X)
  9. #ifdef DEBUG
  10. #define LOG_ERROR(X) \
  11. { \
  12. char _tBfrErrMSG2w34[256]; \
  13. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  14. m_pLog->Log("SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  15. }
  16. #else
  17. #define LOG_ERROR(X) m_pLog->Log("SDBC", this, _LOG_ERROR, X);
  18. #endif
  19. sqliteDBC * sqliteDBC::_instance = NULL;
  20. /**
  21. * Returns the single instance of the object.
  22. */
  23. sqliteDBC * sqliteDBC::getInstance(config_setting_t * Settings)
  24. {
  25. // check if the instance has been created yet
  26. if (0 == _instance)
  27. {
  28. // if not, then create it
  29. _instance = new sqliteDBC(Settings);
  30. };
  31. // return the single instance
  32. return _instance;
  33. }
  34. /*
  35. uint32_t sqliteDBC::getTsFromRDF( REPORT_DIRINFO &RDF ){
  36. struct tm ltm;
  37. memset( &ltm, 0, sizeof( struct tm ));
  38. ltm.tm_mday = atoi( RDF.DAY );
  39. ltm.tm_mon = atoi( RDF.MONTH ) - 1;
  40. ltm.tm_year = atoi( RDF.YEAR ) - 1900;
  41. return (uint32_t) timelocal( &ltm );
  42. };
  43. bool sqliteDBC::createAccessibleDir( CSTR path ){
  44. if( mkdir( path, STORAGE_DIR_MODE ) != 0 ) return false;
  45. chown( path, web_access_user, web_access_group );
  46. return true;
  47. };
  48. bool sqliteDBC::checkRDFDir( CSTR basePath, REPORT_DIRINFO &RDF, bool recreate ){
  49. char tBuff[MAX_PATH];
  50. sprintf( tBuff, "%s/%s", basePath, RDF.YEAR );
  51. #ifdef DEBUG
  52. LOG( tBuff );
  53. #endif
  54. if( fileExist( tBuff ) == 0 ){
  55. if ( recreate ) {
  56. if (!createAccessibleDir(tBuff)) return false;
  57. } else return false;
  58. };
  59. sprintf( tBuff, "%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH );
  60. #ifdef DEBUG
  61. LOG( tBuff );
  62. #endif
  63. if( fileExist( tBuff ) == 0 ){
  64. if ( recreate ) {
  65. if( !createAccessibleDir( tBuff ) ) return false;
  66. } else return false;
  67. };
  68. sprintf( tBuff, "%s/%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH, RDF.DAY );
  69. #ifdef DEBUG
  70. LOG( tBuff );
  71. #endif
  72. if( fileExist( tBuff ) == 0 ){
  73. if ( recreate ) {
  74. if( !createAccessibleDir( tBuff ) ) return false;
  75. } else return false;
  76. };
  77. return true;
  78. };
  79. */
  80. sqliteDBC::sqliteDBC(config_setting_t * Settings) :
  81. dataBinder()
  82. {
  83. m_pLog = basicLogCollection::getInstance();
  84. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DATABASE");
  85. m_bStoIsConnected = false;
  86. m_bMemIsConnected = false;
  87. m_bRepIsConnected = false;
  88. m_bStaIsConnected = false;
  89. //m_bLastEventWasClosed = false;
  90. m_pDataStorage = NULL;
  91. m_pDataMemory = NULL;
  92. m_pDataReports = NULL;
  93. m_pDataStatistics = NULL;
  94. m_nServerID = 1;
  95. memset(&m_sCRepInfo, 0, sizeof(REPORT_DIRINFO));
  96. memset(&m_sPRepInfo, 0, sizeof(REPORT_DIRINFO));
  97. m_nCRepHour = -1;
  98. pthread_mutexattr_t tAttr;
  99. pthread_mutexattr_init(&tAttr);
  100. pthread_mutexattr_settype(&tAttr, PTHREAD_MUTEX_RECURSIVE);
  101. pthread_mutex_init(&m_mReportLock, &tAttr);
  102. pthread_mutexattr_destroy(&tAttr);
  103. }
  104. sqliteDBC::~sqliteDBC()
  105. {
  106. // A report day still being deleted in the background uses this object (deleteReportStat)
  107. for (int i = 0; i < 600 && s_bReportDayDeleting; i++)
  108. {
  109. SLEEP(100);
  110. };
  111. if (m_pDataStorage)
  112. {
  113. delete(m_pDataStorage);
  114. }
  115. if (m_pDataMemory)
  116. {
  117. delete(m_pDataMemory);
  118. }
  119. if (m_pDataReports)
  120. {
  121. delete(m_pDataReports);
  122. }
  123. if (m_pDataStatistics)
  124. {
  125. delete(m_pDataStatistics);
  126. }
  127. m_bStoIsConnected = false;
  128. m_bMemIsConnected = false;
  129. m_bRepIsConnected = false;
  130. m_bStaIsConnected = false;
  131. _instance = NULL;
  132. pthread_mutex_destroy(&m_mReportLock);
  133. }
  134. bool sqliteDBC::connect(void)
  135. {
  136. if (m_pSettings == NULL)
  137. {
  138. return false;
  139. }
  140. config_setting_t * pSetting;
  141. const char * tBuff;
  142. // Disconnect if Connected
  143. m_bStoIsConnected = false;
  144. m_bMemIsConnected = false;
  145. m_bRepIsConnected = false;
  146. m_bStaIsConnected = false;
  147. //m_bLastEventWasClosed = false;
  148. bool bLastEventWasClosed = true; // Workaround
  149. // Apply New Config
  150. #ifdef DEBUG
  151. LOG("Reading DB Settings ... ");
  152. #endif
  153. if (! config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char **) &tBuff))
  154. {
  155. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  156. sprintf(m_pDbFile, "data/mysqlitedb.db");
  157. config_setting_set_string(pSetting, m_pDbFile);
  158. basicSettingsProvider::getInstance()->compromise();
  159. LOG("Data Storage not set - using default");
  160. }
  161. else
  162. {
  163. sprintf(m_pDbFile, tBuff);
  164. };
  165. if (! config_setting_lookup_string(m_pSettings, "REPORTS_DIR_NAME", (const char **) &tBuff))
  166. {
  167. pSetting = config_setting_add(m_pSettings, "REPORTS_DIR_NAME", CONFIG_TYPE_STRING);
  168. sprintf(m_pDbReportBase, "data/reports");
  169. config_setting_set_string(pSetting, m_pDbReportBase);
  170. basicSettingsProvider::getInstance()->compromise();
  171. LOG("Reports Storage not set - using default");
  172. }
  173. else
  174. {
  175. sprintf(m_pDbReportBase, tBuff);
  176. };
  177. if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char **) &tBuff))
  178. {
  179. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  180. sprintf(web_access_user, "www-data");
  181. config_setting_set_string(pSetting, web_access_user);
  182. basicSettingsProvider::getInstance()->compromise();
  183. LOG("Web Access User not set - using default");
  184. }
  185. else
  186. {
  187. sprintf(web_access_user, tBuff);
  188. };
  189. if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char **) &tBuff))
  190. {
  191. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  192. sprintf(web_access_group, "www-data");
  193. config_setting_set_string(pSetting, web_access_group);
  194. basicSettingsProvider::getInstance()->compromise();
  195. LOG("Web Access Group not set - using default");
  196. }
  197. else
  198. {
  199. sprintf(web_access_group, tBuff);
  200. };
  201. if (! fileExist(DB_SHMEM_POINT))
  202. {
  203. mkdir(DB_SHMEM_POINT, STORAGE_DIR_MODE); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  204. chown(DB_SHMEM_POINT, getUserUid(web_access_user), getUserGid(web_access_group)); // To make Accessed from PHP
  205. chmod(DB_SHMEM_POINT, STORAGE_DIR_MODE);
  206. };
  207. char tB[128];
  208. if (! config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **) &tBuff))
  209. {
  210. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  211. tBuff = tB;
  212. sprintf(tB, "mem_db.db");
  213. config_setting_set_string(pSetting, tB);
  214. basicSettingsProvider::getInstance()->compromise();
  215. LOG("Memory Storage not set - using default");
  216. };
  217. sprintf(m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff);
  218. basicSettingsProvider::getInstance()->validate();
  219. // Kill Connection if any
  220. if (m_pDataStorage)
  221. {
  222. delete(m_pDataStorage);
  223. }
  224. if (m_pDataMemory)
  225. {
  226. delete(m_pDataMemory);
  227. }
  228. if (m_pDataReports)
  229. {
  230. delete(m_pDataReports);
  231. }
  232. if (m_pDataStatistics)
  233. {
  234. delete(m_pDataStatistics);
  235. }
  236. m_pDataMemory = NULL;
  237. m_pDataStorage = NULL;
  238. m_pDataReports = NULL;
  239. m_pDataStatistics = NULL;
  240. #ifdef DEBUG
  241. LOG(m_pDbFile);
  242. #endif
  243. // Create New Connections
  244. m_pDataStorage = new SQLite_Connector();
  245. if (m_pDataStorage)
  246. {
  247. if (m_pDataStorage->Connect(m_pDbFile, 0, true))
  248. {
  249. #ifdef DEBUG
  250. LOG("DB File Opened");
  251. #endif
  252. chown(m_pDbFile, getUserUid(web_access_user), getUserGid(web_access_group));
  253. chmod(m_pDbFile, DB_FILE_MODE);
  254. m_bStoIsConnected = true;
  255. }
  256. else
  257. {
  258. LOG_ERROR("Cannot Connect to DB File");
  259. delete(m_pDataStorage);
  260. m_pDataStorage = NULL;
  261. LOG("Storage Deleted!");
  262. return false;
  263. };
  264. };
  265. #ifdef DEBUG
  266. LOG(m_pDbMemory);
  267. #endif
  268. if (! fileExist(m_pDbMemory))
  269. {
  270. // Always recreate
  271. unlink(m_pDbMemory);
  272. };
  273. m_pDataMemory = new SQLite_Connector();
  274. if (m_pDataMemory)
  275. {
  276. if (m_pDataMemory->Connect(m_pDbMemory, 0, true))
  277. {
  278. #ifdef DEBUG
  279. LOG("DB Memory Opened");
  280. #endif
  281. chown(m_pDbMemory, getUserUid(web_access_user), getUserGid(web_access_group));
  282. chmod(m_pDbMemory, DB_FILE_MODE);
  283. m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
  284. // Recreate at Register Server
  285. m_bMemIsConnected = true;
  286. }
  287. else
  288. {
  289. LOG_ERROR("Cannot Connect to DB Memory");
  290. delete(m_pDataStorage);
  291. delete(m_pDataMemory);
  292. m_bStoIsConnected = false;
  293. m_pDataStorage = NULL;
  294. m_pDataMemory = NULL;
  295. return false;
  296. };
  297. };
  298. sprintf(m_pDbStat, "%s/stat.db", m_pDbReportBase);
  299. m_pDataStatistics = new SQLite_Connector();
  300. if (m_pDataStatistics){
  301. if (fileExist(m_pDbStat)){
  302. m_bStaIsCreated = false;
  303. if (m_pDataStatistics->Connect(m_pDbStat, 0, true)){
  304. #ifdef DEBUG
  305. LOG(m_pDbStat);
  306. LOG("DB Statistics Opened");
  307. #endif
  308. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  309. m_bStaIsConnected = true;
  310. } else {
  311. LOG_ERROR("Cannot Connect to DB Statistics");
  312. delete(m_pDataStorage);
  313. delete(m_pDataMemory);
  314. delete(m_pDataStatistics);
  315. m_bMemIsConnected = false;
  316. m_bStoIsConnected = false;
  317. m_pDataStorage = NULL;
  318. m_pDataMemory = NULL;
  319. m_pDataStatistics = NULL;
  320. return false;
  321. };
  322. } else {
  323. m_bStaIsCreated = true;
  324. if (m_pDataStatistics->Connect(m_pDbStat, 0, true)){
  325. #ifdef DEBUG
  326. LOG("NEW DB Statistics Creating");
  327. #endif
  328. chown(m_pDbStat, getUserUid(web_access_user), getUserGid(web_access_group));
  329. chmod(m_pDbStat, DB_FILE_MODE);
  330. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  331. m_pDataStatistics->SilentQuery(
  332. "CREATE TABLE IF NOT EXISTS \"days\" (\"year\" INTEGER,\"month\" INTEGER,\"day\" INTEGER,\"evt_count\" INTEGER DEFAULT 0,\"session_count\" INTEGER DEFAULT 0,\"videos_count\" INTEGER DEFAULT 0,\"session_closed\" INTEGER "
  333. "DEFAULT 0, CONSTRAINT \"date_uni\" UNIQUE(\"year\",\"month\",\"day\") );");
  334. m_bStaIsConnected = true;
  335. } else {
  336. LOG_ERROR("Cannot Connect to DB Statistics");
  337. delete(m_pDataStorage);
  338. delete(m_pDataMemory);
  339. delete(m_pDataStatistics);
  340. m_bMemIsConnected = false;
  341. m_bStoIsConnected = false;
  342. m_pDataStorage = NULL;
  343. m_pDataMemory = NULL;
  344. m_pDataStatistics = NULL;
  345. return false;
  346. };
  347. };
  348. };
  349. m_pDataReports = new SQLite_Connector();
  350. if( m_pDataReports ){
  351. /*
  352. memset(&m_sPRepInfo, 0, sizeof(REPORT_DIRINFO));
  353. if (! m_bStaIsCreated ){
  354. #ifdef DEBUG
  355. LOG("OLD Statistics PRESENT Report Created!");
  356. #endif
  357. AAssoc * result = m_pDataStatistics->Query("SELECT * FROM `days` ORDER BY `year` DESC, `month` DESC, `day` DESC LIMIT 1");
  358. if (result){
  359. if (result->size() > 0){
  360. Assoc * cArr;
  361. Assoc::iterator it;
  362. cArr = result->at(0);
  363. it = cArr->find("year");
  364. if (it != cArr->end()){
  365. strncpy(m_sPRepInfo.YEAR, it->second.c_str(), 4);
  366. };
  367. it = cArr->find("month");
  368. if (it != cArr->end()){
  369. sprintf(m_sPRepInfo.MONTH, "%02d", atoi(it->second.c_str()));
  370. };
  371. it = cArr->find("day");
  372. if (it != cArr->end()){
  373. sprintf(m_sPRepInfo.DAY, "%02d", atoi(it->second.c_str()));
  374. };
  375. m_sPRepInfo.ts = getTsFromRDF(m_sPRepInfo);
  376. } else {
  377. #ifdef DEBUG
  378. LOG("No Rows Fetched from STAT!");
  379. #endif
  380. };
  381. };
  382. m_pDataMemory->deleteResult(result);
  383. };
  384. if( m_sPRepInfo.ts != 0 ){
  385. #ifdef DEBUG
  386. LOG("Have Old Reports!");
  387. #endif
  388. if (checkRDFDir(m_pDbReportBase, m_sPRepInfo, false) == 0){
  389. LOG_ERROR("Error Connecting to Prev Report!");
  390. bLastEventWasClosed = true;
  391. } else {
  392. #ifdef DEBUG
  393. LOG("Connect to old Report Database");
  394. #endif
  395. sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY);
  396. LOG(m_pDbReport);
  397. if (fileExist(m_pDbReport)) {
  398. SQLite_Connector * tDBC = new SQLite_Connector();
  399. if (tDBC) {
  400. if (tDBC->Connect(m_pDbReport, 0, true)){
  401. char tQBuf[CTEXT];
  402. AAssoc * result;
  403. //sprintf(tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO);
  404. sprintf( tQBuf, "SELECT `evt_type`, `ts`, `ts_ms` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1" );
  405. #ifdef DEBUG_ALL
  406. LOG(tQBuf);
  407. #endif
  408. result = tDBC->Query(tQBuf);
  409. if (result != NULL){
  410. if (result->size() > 0){
  411. Assoc * cArr;
  412. Assoc::iterator it;
  413. cArr = result->at(0);
  414. it = cArr->find("evt_type");
  415. if (it != cArr->end()){
  416. if (atoi(it->second.c_str()) != EVT_LRF_START_SESSION){
  417. bLastEventWasClosed = true;
  418. }
  419. };
  420. } else {
  421. bLastEventWasClosed = true;
  422. };
  423. };
  424. deleteResult(result);
  425. } else {
  426. LOG_ERROR("Error Connecting to Prev Report!");
  427. bLastEventWasClosed = true;
  428. };
  429. delete(tDBC);
  430. } else {
  431. LOG_ERROR("Error Create DB Connection Object!");
  432. bLastEventWasClosed = true;
  433. };
  434. } else {
  435. LOG_ERROR("Error No Prev Report Database!");
  436. bLastEventWasClosed = true;
  437. };
  438. };
  439. } else {
  440. bLastEventWasClosed = true;
  441. };
  442. */
  443. if( bLastEventWasClosed || true ){
  444. #ifdef DEBUG
  445. LOG("Last Session is Closed!");
  446. #endif
  447. time_t tNow = time(NULL);
  448. fillRDF(tNow, m_sCRepInfo);
  449. m_nCRepHour = getLocalHour(tNow);
  450. } else {
  451. #ifdef DEBUG
  452. LOG("Last Session is NOT Closed!");
  453. #endif
  454. memcpy(&m_sCRepInfo, &m_sPRepInfo, sizeof(REPORT_DIRINFO));
  455. };
  456. #ifdef DEBUG
  457. LOG(m_pDbReportBase);
  458. #endif
  459. if (! checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group))){
  460. LOG_ERROR("Cannot Open Report!!");
  461. return false;
  462. };
  463. char tRepName[32];
  464. snprintf(tRepName, sizeof(tRepName), REPORT_DB_NAME_FMT, m_nCRepHour < 0 ? 0 : m_nCRepHour);
  465. sprintf(m_pDbReport, "%s/%s/%s/%s/%s", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY, tRepName);
  466. #ifdef DEBUG
  467. LOG(m_pDbReport);
  468. #endif
  469. if (m_pDataReports->Connect(m_pDbReport, 0, true)){
  470. #ifdef DEBUG
  471. LOG("Report File Opened");
  472. #endif
  473. chown(m_pDbReport, getUserUid(web_access_user), getUserGid(web_access_group));
  474. chmod(m_pDbReport, DB_FILE_MODE);
  475. m_pDataReports->SilentQuery(
  476. "CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"node_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"lrf_id\" "
  477. "INTEGER, \"range_digital\" INTEGER, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, \"lat\" REAL, \"lon\" REAL, \"alt\" REAL, \"ref_id\" INTEGER, \"ref_uuid\" TEXT, PRIMARY KEY(\"id\" "
  478. "AUTOINCREMENT) )");
  479. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  480. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_videos\" ( \"evt_id\" INTEGER NOT NULL, \"video_id\" INTEGER NOT NULL, \"frame\" INTEGER DEFAULT 0, \"tss\" INTEGER, \"tms\" INTEGER )");
  481. m_pDataReports->SilentQuery(
  482. "CREATE TABLE IF NOT EXISTS \"pictures\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, "
  483. "\"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  484. m_pDataReports->SilentQuery(
  485. "CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" "
  486. "INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  487. //};
  488. m_bRepIsConnected = true;
  489. } else {
  490. LOG_ERROR("Cannot Connect to DB File");
  491. delete(m_pDataReports);
  492. m_pDataReports = NULL;
  493. LOG("Storage Deleted!");
  494. return false;
  495. };
  496. };
  497. return (m_bRepIsConnected && m_bMemIsConnected && m_bStoIsConnected && m_bStaIsConnected);
  498. }
  499. bool sqliteDBC::isDatabaseNew()
  500. {
  501. bool RetVal = true;
  502. if (m_bStoIsConnected)
  503. {
  504. char tQBuf[CTEXT];
  505. sprintf(tQBuf, "SELECT `os_info` FROM `system_props` LIMIT 1");
  506. #ifdef DEBUG_ALL
  507. LOG(tQBuf);
  508. #endif
  509. AAssoc * result;
  510. result = m_pDataStorage->Query(tQBuf);
  511. if (result != NULL)
  512. {
  513. if (result->size() > 0)
  514. {
  515. Assoc * cArr;
  516. Assoc::iterator it;
  517. cArr = result->at(0);
  518. it = cArr->find("os_info");
  519. if (it != cArr->end())
  520. {
  521. RetVal = (strlen(it->second.c_str()) < 5);
  522. };
  523. };
  524. };
  525. deleteResult(result);
  526. };
  527. return RetVal;
  528. }
  529. void sqliteDBC::initMemDB(uint32_t sid)
  530. {
  531. m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
  532. m_pDataMemory->SilentQuery(
  533. "CREATE TABLE \"nodes_qcq\" ( \
  534. \"id\" INTEGER NOT NULL,\
  535. \"node_id\" INTEGER NOT NULL,\
  536. \"cmd\" INTEGER NOT NULL,\
  537. \"status\" INTEGER DEFAULT 0,\
  538. \"parameter\" TEXT,\
  539. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  540. );");
  541. m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
  542. m_pDataMemory->SilentQuery(
  543. "CREATE TABLE \"servers\" (\
  544. \"id\" INTEGER NOT NULL,\
  545. \"ts\" INTEGER DEFAULT 0,\
  546. \"pid\" INTEGER DEFAULT 0,\
  547. \"hw_info\" TEXT,\
  548. \"os_info\" TEXT,\
  549. \"cpu_cores\" INTEGER DEFAULT 1,\
  550. \"stat_cpu1\" REAL DEFAULT 0.0,\
  551. \"stat_cpu5\" REAL DEFAULT 0.0,\
  552. \"stat_cpu15\" REAL DEFAULT 0.0,\
  553. \"stat_mem\" REAL DEFAULT 0.0,\
  554. \"stat_disk\" REAL DEFAULT 0.0,\
  555. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  556. \"val_mem_current\" INTEGER DEFAULT 0,\
  557. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  558. \"val_disk_current\" INTEGER DEFAULT 0,\
  559. \"temp0\" REAL DEFAULT 0.0,\
  560. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  561. );");
  562. char tB[128];
  563. sprintf(tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid);
  564. m_pDataMemory->SilentQuery(tB);
  565. }
  566. CSTR sqliteDBC::getCurrentReportDir(void)
  567. {
  568. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  569. CSTR tV = NULL;
  570. if (m_sCRepInfo.ts)
  571. {
  572. char tBuf[256];
  573. sprintf(tBuf, "%s/%s/%s", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  574. tV = strdup(tBuf);
  575. };
  576. return tV;
  577. }
  578. DBC_PUSHED_CMD * sqliteDBC::getNodePushedCMD(uint32_t NodeID)
  579. {
  580. DBC_PUSHED_CMD * RetVal = NULL;
  581. if (m_bMemIsConnected)
  582. {
  583. char QS[MAX_PATH];
  584. AAssoc * result;
  585. sprintf(QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID);
  586. #ifdef DEBUG_ALL
  587. LOG(QS);
  588. #endif
  589. result = m_pDataMemory->Query(QS);
  590. if (result)
  591. {
  592. if (result->size() > 0)
  593. {
  594. RetVal = (DBC_PUSHED_CMD *) malloc(sizeof(DBC_PUSHED_CMD));
  595. if (RetVal)
  596. {
  597. Assoc * cArr;
  598. Assoc::iterator it;
  599. memset(RetVal, 0, sizeof(DBC_PUSHED_CMD));
  600. cArr = result->at(0);
  601. it = cArr->find("id");
  602. if (it != cArr->end())
  603. {
  604. RetVal->id = atoi(it->second.c_str());
  605. };
  606. it = cArr->find("cmd");
  607. if (it != cArr->end())
  608. {
  609. RetVal->CMD = atoi(it->second.c_str());
  610. };
  611. it = cArr->find("parameter");
  612. if (it != cArr->end())
  613. {
  614. strncpy(RetVal->PARAM, it->second.c_str(), 256);
  615. RetVal->PARAM[255] = (char) 0;
  616. };
  617. };
  618. #ifdef DEBUG_ALL
  619. }
  620. else
  621. {
  622. LOG("No Rows Fetched!");
  623. #endif
  624. };
  625. };
  626. m_pDataMemory->deleteResult(result);
  627. };
  628. return RetVal;
  629. }
  630. bool sqliteDBC::updateModemManagerActiveCall(uint32_t MMID, int32_t callID)
  631. {
  632. if (m_bMemIsConnected)
  633. {
  634. char QS[MAX_PATH];
  635. sprintf(QS, "UPDATE `module_modem` SET `active_call`=%d WHERE `id`=%d ", callID, MMID);
  636. return m_pDataStorage->SilentQuery(QS);
  637. };
  638. return false;
  639. }
  640. bool sqliteDBC::updateModemManagerLTS(uint32_t MMID, uint32_t ts)
  641. {
  642. if (m_bMemIsConnected)
  643. {
  644. char QS[MAX_PATH];
  645. sprintf(QS, "UPDATE `module_modem` SET `last_trigger_ts`=%d WHERE `id`=%d ", ts, MMID);
  646. return m_pDataStorage->SilentQuery(QS);
  647. };
  648. return false;
  649. }
  650. bool sqliteDBC::updateModemManagerActivePhoneBookID(uint32_t MMID, uint32_t pbID)
  651. {
  652. if (m_bMemIsConnected)
  653. {
  654. char QS[MAX_PATH];
  655. sprintf(QS, "UPDATE `module_modem` SET `active_ph_id`=%d WHERE `id`=%d ", pbID, MMID);
  656. return m_pDataStorage->SilentQuery(QS);
  657. };
  658. return false;
  659. }
  660. bool sqliteDBC::updateModemManagerActiveSMS(uint32_t MMID, int32_t smsID)
  661. {
  662. if (m_bMemIsConnected)
  663. {
  664. char QS[MAX_PATH];
  665. sprintf(QS, "UPDATE `module_modem` SET `active_sms`=%d WHERE `id`=%d ", smsID, MMID);
  666. return m_pDataStorage->SilentQuery(QS);
  667. };
  668. return false;
  669. }
  670. bool sqliteDBC::updateNodePushedCMD(uint32_t CmdID, uint8_t status)
  671. {
  672. if (m_bMemIsConnected)
  673. {
  674. char QS[MAX_PATH];
  675. sprintf(QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID);
  676. return m_pDataMemory->SilentQuery(QS);
  677. };
  678. return false;
  679. }
  680. bool sqliteDBC::cleanNodePushedCMD(void)
  681. {
  682. if (m_bMemIsConnected)
  683. {
  684. return m_pDataMemory->SilentQuery("DELETE FROM `nodes_qcq` WHERE `status` > 200");
  685. };
  686. return false;
  687. }
  688. void sqliteDBC::deleteNIF(uint32_t NifID)
  689. {
  690. if (m_bStoIsConnected)
  691. {
  692. char tQBuf[MAX_PATH_LEN];
  693. sprintf(tQBuf, "DELETE FROM `network_interfaces` WHERE `id`=%d", NifID);
  694. #ifdef DEBUG_ALL
  695. LOG(tQBuf);
  696. #endif
  697. m_pDataStorage->SilentQuery(tQBuf);
  698. };
  699. }
  700. void sqliteDBC::deleteNIS(uint32_t NisID) {
  701. if (m_bStoIsConnected)
  702. {
  703. char tQBuf[MAX_PATH_LEN];
  704. sprintf(tQBuf, "DELETE FROM `network_infrastructure` WHERE `id`=%d", NisID);
  705. #ifdef DEBUG_ALL
  706. LOG(tQBuf);
  707. #endif
  708. m_pDataStorage->SilentQuery(tQBuf);
  709. };
  710. }
  711. void sqliteDBC::deleteNetwork(uint32_t NetID) {
  712. if (m_bStoIsConnected)
  713. {
  714. char tQBuf[MAX_PATH_LEN];
  715. sprintf(tQBuf, "DELETE FROM `networks` WHERE `id`=%d", NetID);
  716. #ifdef DEBUG_ALL
  717. LOG(tQBuf);
  718. #endif
  719. m_pDataStorage->SilentQuery(tQBuf);
  720. };
  721. }
  722. bool sqliteDBC::deleteReportStat(REPORT_DIRINFO & RDF)
  723. {
  724. if (m_bStaIsConnected)
  725. {
  726. char tQBuf[MAX_PATH_LEN];
  727. sprintf(tQBuf, "DELETE FROM `days` WHERE `year`=%s AND `month`=%s AND `day`=%s", RDF.YEAR, RDF.MONTH, RDF.DAY);
  728. #ifdef DEBUG_ALL
  729. LOG(tQBuf);
  730. #endif
  731. m_pDataStatistics->SilentQuery(tQBuf);
  732. return true;
  733. };
  734. return false;
  735. }
  736. void sqliteDBC::deleteResource(uint32_t ResourceID) {
  737. if (m_bStoIsConnected)
  738. {
  739. char tQBuf[MAX_PATH_LEN];
  740. sprintf(tQBuf, "DELETE FROM `resources` WHERE `id`=%d", ResourceID );
  741. #ifdef DEBUG_ALL
  742. LOG(tQBuf);
  743. #endif
  744. m_pDataStorage->SilentQuery(tQBuf);
  745. };
  746. }
  747. void sqliteDBC::deleteSensor(uint32_t SensorID) {
  748. if (m_bStoIsConnected)
  749. {
  750. char tQBuf[MAX_PATH_LEN];
  751. sprintf(tQBuf, "DELETE FROM `lrf_sensors` WHERE `id`=%d", SensorID );
  752. #ifdef DEBUG_ALL
  753. LOG(tQBuf);
  754. #endif
  755. m_pDataStorage->SilentQuery(tQBuf);
  756. };
  757. }
  758. void sqliteDBC::deleteUART(uint32_t UARTID) {
  759. if (m_bStoIsConnected)
  760. {
  761. char tQBuf[MAX_PATH_LEN];
  762. sprintf(tQBuf, "DELETE FROM `uart_settings` WHERE `id`=%d", UARTID );
  763. #ifdef DEBUG_ALL
  764. LOG(tQBuf);
  765. #endif
  766. m_pDataStorage->SilentQuery(tQBuf);
  767. };
  768. }
  769. bool sqliteDBC::updateServerUtilisation(DBC_SYS_LOAD_INFO * ptr)
  770. {
  771. if (m_bMemIsConnected)
  772. {
  773. char tQBuf[MAX_PATH_LEN];
  774. sprintf(tQBuf,
  775. "UPDATE `servers` SET `ts`=STRFTIME('%%s'), `cpu_cores`=%d, `stat_cpu1`=%0.1f, `stat_cpu5`=%0.1f, `stat_cpu15`=%0.1f, `stat_mem`=%0.1f, `stat_disk`=%0.1f, `temp0`=%0.1f, `val_mem_tottal`=%" PRIu64 ", `val_mem_current`=%" PRIu64
  776. ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
  777. ptr->CPU_CORES, ptr->cpu1, ptr->cpu5, ptr->cpu15, ptr->mem, ptr->disk, ptr->temperature, ptr->T_DISK, ptr->C_DISK, ptr->T_MEM, ptr->C_MEM, m_nServerID);
  778. #ifdef DEBUG_ALL
  779. LOG(tQBuf);
  780. #endif
  781. m_pDataMemory->SilentQuery(tQBuf);
  782. return true;
  783. };
  784. return false;
  785. }
  786. /********************************************************************
  787. * *
  788. * Slow DB Functions *
  789. * *
  790. ********************************************************************/
  791. bool sqliteDBC::addLRFDraw(uint32_t node_id, CSTR uniq_id, uint8_t lrf_id, uint8_t type)
  792. {
  793. if (m_bStoIsConnected)
  794. {
  795. char tQBuf[1024];
  796. sprintf(tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')", node_id, lrf_id, type, uniq_id);
  797. #ifdef DEBUG_ALL
  798. LOG(tQBuf);
  799. #endif
  800. m_pDataStorage->SilentQuery(tQBuf);
  801. return true;
  802. };
  803. return false;
  804. }
  805. bool sqliteDBC::addDeviceDraw(uint32_t DBID, uint8_t type, uint8_t lrf, void * th_data, uint32_t th_size)
  806. {
  807. if (m_bStoIsConnected)
  808. {
  809. if (type == 0)
  810. {
  811. char tQBuf[1024];
  812. CSTR uid = getUniqID_MD5();
  813. if (uid == NULL)
  814. {
  815. return false;
  816. }
  817. sprintf(tQBuf, "data/device_draws/%s.raw", uid);
  818. FILE * fd = fopen(tQBuf, "wb+");
  819. if (fd != NULL)
  820. {
  821. uint32_t sisi = th_size >> 2;
  822. fwrite(&sisi, 4, 1, fd);
  823. fwrite(th_data, 4, sisi, fd);
  824. fclose(fd);
  825. bool rV = addLRFDraw(DBID, uid, lrf, type);
  826. free(uid);
  827. return rV;
  828. };
  829. free(uid);
  830. }
  831. else
  832. {
  833. return addLRFDraw(DBID, (CSTR) th_data, lrf, type);
  834. };
  835. };
  836. return false;
  837. }
  838. /*
  839. * Opens (creates if needed) the report of the given day and local hour:
  840. * data/reports/YYYY/MM/DD/report_HH.db (REPORT_DB_NAME_FMT)
  841. * Pictures and videos stay in the day folder. SQLite_Connector::Connect() closes the
  842. * currently open report under its own lock first. Caller must hold m_mReportLock.
  843. */
  844. bool sqliteDBC::openHourReport(REPORT_DIRINFO & RDF, int hour)
  845. {
  846. char tName[32];
  847. char tPath[CLOG];
  848. char tQBuf[1024];
  849. if (! checkRDFDir(m_pDbReportBase, RDF, true, getUserUid(web_access_user), getUserGid(web_access_group)))
  850. {
  851. LOG_ERROR("Cannot create report directory!");
  852. return false;
  853. };
  854. if (hour < 0 || hour > 23)
  855. {
  856. LOG_ERROR("Invalid report hour!");
  857. return false;
  858. };
  859. snprintf(tName, sizeof(tName), REPORT_DB_NAME_FMT, hour);
  860. snprintf(tPath, sizeof(tPath), "%s/%s/%s/%s/%s", m_pDbReportBase, RDF.YEAR, RDF.MONTH, RDF.DAY, tName);
  861. LOG("Connect Report!");
  862. LOG(tPath);
  863. if (m_bRepIsConnected)
  864. {
  865. LOG("Disconnect Report!");
  866. };
  867. m_bRepIsConnected = false;
  868. if (! m_pDataReports->Connect(tPath, 0, true))
  869. {
  870. LOG_ERROR("Cannot open hour report!");
  871. return false;
  872. };
  873. strncpy(m_pDbReport, tPath, CLOG - 1);
  874. m_pDbReport[CLOG - 1] = (char) 0;
  875. chown(m_pDbReport, getUserUid(web_access_user), getUserGid(web_access_group));
  876. chmod(m_pDbReport, DB_FILE_MODE);
  877. m_pDataReports->SilentQuery(
  878. "CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"node_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT "
  879. "NULL, \"lrf_id\" INTEGER, \"range_digital\" INTEGER, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, \"lat\" REAL, \"lon\" REAL, \"alt\" REAL, \"ref_id\" INTEGER, \"ref_uuid\" "
  880. "TEXT, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  881. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  882. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_videos\" ( \"evt_id\" INTEGER NOT NULL, \"video_id\" INTEGER NOT NULL, \"frame\" INTEGER DEFAULT 0, \"tss\" INTEGER, \"tms\" INTEGER )");
  883. m_pDataReports->SilentQuery(
  884. "CREATE TABLE IF NOT EXISTS \"pictures\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT "
  885. "NOT NULL, \"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  886. m_pDataReports->SilentQuery(
  887. "CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT "
  888. "NULL, \"fps\" INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  889. memcpy(&m_sPRepInfo, &m_sCRepInfo, sizeof(REPORT_DIRINFO));
  890. memcpy(&m_sCRepInfo, &RDF, sizeof(REPORT_DIRINFO));
  891. m_nCRepHour = hour;
  892. m_bRepIsConnected = true;
  893. LOG("Report Connected!");
  894. // Create the day record (no session yet) - session counters are updated on SESSION_START / SESSION_END
  895. if (m_bStaIsConnected)
  896. {
  897. snprintf(tQBuf, sizeof(tQBuf), "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,0,1) ON CONFLICT(`year`,`month`,`day`) DO NOTHING",
  898. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  899. LOG(tQBuf);
  900. m_pDataStatistics->SilentQuery(tQBuf);
  901. };
  902. return true;
  903. }
  904. /*
  905. * Called by the kernel when the local hour changes (and on every SESSION_START): if the local
  906. * day or hour changed since the current report was opened, close it and open the current
  907. * hour's report_HH.db (in a new day folder at midnight).
  908. */
  909. bool sqliteDBC::switchReportToCurrentHour(void)
  910. {
  911. ReportGuard tGuard(m_mReportLock);
  912. if (m_pDataReports == NULL)
  913. {
  914. return false;
  915. };
  916. time_t tNow = time(NULL);
  917. REPORT_DIRINFO tRR; // initialized by fillRDF
  918. fillRDF(tNow, tRR);
  919. int tHour = getLocalHour(tNow);
  920. // Same day and same hour (also when the hour repeats at the end of daylight saving time)
  921. if (m_bRepIsConnected && (tRR.ts <= m_sCRepInfo.ts) && (tHour == m_nCRepHour))
  922. {
  923. return true; // already the current hour's report
  924. };
  925. if (m_bRepIsConnected && (tRR.ts < m_sCRepInfo.ts))
  926. {
  927. return true; // clock moved back to a previous day - keep the open report
  928. };
  929. char tB[128];
  930. snprintf(tB, sizeof(tB), "Report switch %s-%s-%s %02d -> %s-%s-%s %02d", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY, m_nCRepHour, tRR.YEAR, tRR.MONTH, tRR.DAY, tHour);
  931. LOG(tB);
  932. return openHourReport(tRR, tHour);
  933. }
  934. void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status)
  935. {
  936. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  937. // Session status changes are called as updateStats(false, false, SESSION_xxx): they must not be
  938. // filtered out by the evt/video condition (they were, so the session counters and the new-day
  939. // report switch on SESSION_START never ran).
  940. if (m_bStaIsConnected && ( evt || video || ( ses_status != SESSION_CONTINUE ) ))
  941. {
  942. char tQBuf[1024];
  943. switch (ses_status)
  944. {
  945. case SESSION_CONTINUE:
  946. {
  947. if (video)
  948. {
  949. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`videos_count`) VALUES(%s,%s,%s,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `videos_count`=`videos_count`+1", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  950. }
  951. else
  952. {
  953. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`evt_count`) VALUES(%s,%s,%s,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `evt_count`=`evt_count`+1", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  954. };
  955. #ifdef DEBUG_ALL
  956. LOG(tQBuf);
  957. #endif
  958. m_pDataStatistics->SilentQuery(tQBuf);
  959. break;
  960. };
  961. case SESSION_START:
  962. {
  963. // Close the previous report and open the current hour's one (if the hour changed)
  964. switchReportToCurrentHour();
  965. // Create New STAT record
  966. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,1,0) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_count`=`session_count`+1, `session_closed`=0", m_sCRepInfo.YEAR,
  967. m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  968. #ifdef DEBUG_ALL
  969. LOG(tQBuf);
  970. #endif
  971. m_pDataStatistics->SilentQuery(tQBuf);
  972. break;
  973. };
  974. case SESSION_END:
  975. {
  976. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,0,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_closed`=1", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  977. #ifdef DEBUG_ALL
  978. LOG(tQBuf);
  979. #endif
  980. m_pDataStatistics->SilentQuery(tQBuf);
  981. break;
  982. };
  983. case SESSION_DROP:
  984. {
  985. // sprintf( tQBuf, "UPDATE `days` SET `session_count`=`session_count`-1, `session_closed`=1 WHERE `year`='%s' AND `month`='%s' AND `day`='%s'",
  986. sprintf(tQBuf, "UPDATE `days` SET `session_closed`=1 WHERE `year`='%s' AND `month`='%s' AND `day`='%s'", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  987. #ifdef DEBUG_ALL
  988. LOG(tQBuf);
  989. #endif
  990. m_pDataStatistics->SilentQuery(tQBuf);
  991. break;
  992. };
  993. };
  994. };
  995. }
  996. bool sqliteDBC::dropSession()
  997. {
  998. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  999. if (m_bRepIsConnected)
  1000. {
  1001. char tQBuf[1024];
  1002. AAssoc * result;
  1003. #ifdef REMOVE_ONLY_SESSION_MARKS
  1004. sprintf(tQBuf, "SELECT `ts`, `ts_ms` FROM `events` WHERE `evt_type`=%d ORDER BY `ts` DESC,`ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION);
  1005. #else
  1006. sprintf(tQBuf, "SELECT `ts`, `ts_ms` FROM `events` WHERE `evt_type`=%d ORDER BY `ts` DESC,`ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION);
  1007. #endif
  1008. #ifdef DEBUG_ALL
  1009. LOG(tQBuf);
  1010. #endif
  1011. // (a duplicate Query() call whose result was never freed was removed here)
  1012. result = m_pDataReports->Query(tQBuf);
  1013. if (result != NULL)
  1014. {
  1015. uint32_t ts = 0;
  1016. uint32_t ms = 0;
  1017. uint64_t r = 0;
  1018. if (result->size() > 0)
  1019. {
  1020. Assoc * cArr;
  1021. Assoc::iterator it;
  1022. cArr = result->at(0);
  1023. it = cArr->find("ts");
  1024. if (it != cArr->end())
  1025. {
  1026. ts = atoi(it->second.c_str());
  1027. };
  1028. it = cArr->find("ts_ms");
  1029. if (it != cArr->end())
  1030. {
  1031. ms = atoi(it->second.c_str());
  1032. };
  1033. r = ts;
  1034. r = (r * 1000) + ms;
  1035. };
  1036. deleteResult(result);
  1037. if (r)
  1038. {
  1039. #ifdef REMOVE_ONLY_SESSION_MARKS
  1040. sprintf(tQBuf, "DELETE FROM `events` WHERE ((`ts`*1000+`ts_ms`) >= %" PRIu64 ") AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d )", r, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_TO, EVT_LRF_END_SESSION_MA);
  1041. #else
  1042. sprintf(tQBuf, "DELETE FROM `events` WHERE (`ts`*1000+`ts_ms`) >= %" PRIu64, r);
  1043. #endif
  1044. #ifdef DEBUG_ALL
  1045. LOG(tQBuf);
  1046. #endif
  1047. m_pDataReports->SilentQuery(tQBuf);
  1048. };
  1049. };
  1050. };
  1051. updateStats(false, false, SESSION_DROP);
  1052. return true;
  1053. }
  1054. uint32_t sqliteDBC::addEvent(DBC_EVT_RECORD * ptr)
  1055. {
  1056. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  1057. if (ptr == NULL)
  1058. {
  1059. return 0;
  1060. }
  1061. if (m_bRepIsConnected)
  1062. {
  1063. char tQBuf[1024];
  1064. switch (ptr->type)
  1065. {
  1066. case EVT_LRF_END_SESSION_TO:
  1067. case EVT_LRF_END_SESSION_MA:
  1068. updateStats(false, false, SESSION_END);
  1069. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1070. break;
  1071. case EVT_LRF_START_SESSION:
  1072. updateStats(false, false, SESSION_START);
  1073. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1074. break;
  1075. case EVT_FLT_LATF_CHK:
  1076. case EVT_FLT_LATF_UP_DETEC:
  1077. case EVT_FLT_LATF_BT_DETEC:
  1078. case EVT_FLT_LATF_TRP_DET:
  1079. case EVT_FLT_LATF_TRP_UPDT:
  1080. case EVT_FLT_LATF_SUSP_START:
  1081. case EVT_FLT_LATF_REGULAR:
  1082. case EVT_FLT_LATF_DISABLED:
  1083. case EVT_FLT_LATF_SUSP_DET:
  1084. case EVT_FLT_LATF_DIFF_DET:
  1085. case EVT_FLT_BUBBLE_POPED:
  1086. case EVT_FLT_BUBBLE_CREATED:
  1087. case EVT_FLT_BUBBLE_BLOCK_T:
  1088. case EVT_FLT_BUBBLE_BLOCK_P:
  1089. {
  1090. updateStats();
  1091. sprintf(tQBuf,
  1092. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)"
  1093. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1094. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1095. break;
  1096. };
  1097. case EVT_FLT_LATF_OP_DISABLED:
  1098. case EVT_FLT_LATF_OP_TD:
  1099. case EVT_FLT_LATF_OP_TG:
  1100. case EVT_FLT_LATF_OP_EXP:
  1101. case EVT_FLT_LATF_TRP_EXP:
  1102. case EVT_FLT_LATF_SUSP_NAK:
  1103. case EVT_FLT_LATF_SUSP_EXP:
  1104. {
  1105. updateStats();
  1106. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1107. break;
  1108. };
  1109. case EVT_OP_ACK_MASK:
  1110. case EVT_OP_NACK_MASK:
  1111. case EVT_OP_ON_MASK:
  1112. case EVT_OP_OFF_MASK:
  1113. case EVT_CONNECTED:
  1114. case EVT_DISCONNECTED:
  1115. case EVT_COMMUNICATION_JAMED:
  1116. case EVT_STUDY:
  1117. case EVT_BUSY:
  1118. case EVT_FLIP:
  1119. case EVT_READY:
  1120. case EVT_ERR_LRF_NO_IRQ:
  1121. case EVT_ERR_LRF_NO_ZERO:
  1122. case EVT_ERR_LRF_ANGLE:
  1123. case EVT_ERR_LRF_VOLTAGE:
  1124. case EVT_ERR_LRF_BROKEN:
  1125. case EVT_LRF_VIS_CHANGED:
  1126. case EVT_ERR_OVERFLOW:
  1127. case EVT_SST_OVERFLOW:
  1128. case EVT_VIDEO_FPS_DEGRADED:
  1129. case EVT_VIDEO_FPS_RAISED:
  1130. {
  1131. updateStats();
  1132. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1133. break;
  1134. };
  1135. case EVT_ROI_ACTIVE:
  1136. case EVT_TARGET_3D_WHITE:
  1137. case EVT_TARGET_3D_BLACK:
  1138. {
  1139. updateStats();
  1140. sprintf(tQBuf,
  1141. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)"
  1142. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1143. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->el, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1144. break;
  1145. };
  1146. case EVT_TARGET_2D_WHITE:
  1147. case EVT_TARGET_2D_BLACK:
  1148. {
  1149. updateStats();
  1150. sprintf(tQBuf,
  1151. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)"
  1152. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1153. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1154. break;
  1155. };
  1156. default: return 0;
  1157. };
  1158. #ifdef DEBUG_ALL
  1159. LOG(tQBuf);
  1160. #endif
  1161. if (m_bRepIsConnected)
  1162. {
  1163. return ptr->id = m_pDataReports->QueryID(tQBuf);
  1164. }
  1165. else
  1166. {
  1167. LOG_ERROR("Report DB Droped!");
  1168. }
  1169. };
  1170. return 0;
  1171. }
  1172. bool sqliteDBC::addIPCam(OBSERVER_RESULT * tPtr)
  1173. {
  1174. if (m_bStoIsConnected)
  1175. {
  1176. char tQBuf[2048];
  1177. char tIpBuf[32];
  1178. char tProto[32];
  1179. char tPath[CTEXT];
  1180. char * tPos;
  1181. char * tPos2;
  1182. uint16_t tPort = 0;
  1183. memcpy(tProto, tPtr->XAddrs, 5);
  1184. tProto[5] = (char) 0;
  1185. if (tProto[4] == ':')
  1186. {
  1187. tProto[4] = (char) 0;
  1188. }
  1189. memset(tPath, 0, CTEXT);
  1190. tPos = strstr(tPtr->XAddrs, "//");
  1191. if (tPos != NULL)
  1192. {
  1193. tPos2 = strstr(tPos + 2, ":");
  1194. if (tPos2 != NULL)
  1195. {
  1196. LOG("IP Port Defined!");
  1197. int len;//, len2, len3;
  1198. len = tPos2 - tPos - 2;
  1199. strncpy(tIpBuf, tPos + 2, len);
  1200. tIpBuf[len] = (char) 0;
  1201. tPort = atoi(tPos2 + 1);
  1202. strncpy(tPath, tPos + 2 + len, CTEXT);
  1203. }
  1204. else
  1205. {
  1206. tPos2 = strstr(tPos + 2, "/");
  1207. if (tPos2 != NULL)
  1208. {
  1209. int len;//, len2, len3;
  1210. len = tPos2 - tPos - 2;
  1211. strncpy(tIpBuf, tPos + 2, len);
  1212. tIpBuf[len] = (char) 0;
  1213. strncpy(tPath, tPos + 2 + len, CTEXT);
  1214. }
  1215. else
  1216. {
  1217. tPos2 = strstr(tPos + 2, "/");
  1218. LOG_ERROR("Pos 2 - err");
  1219. tIpBuf[0] = (char) 0;
  1220. };
  1221. };
  1222. }
  1223. else
  1224. {
  1225. LOG_ERROR("Pos 0 - err");
  1226. tIpBuf[0] = (char) 0;
  1227. };
  1228. if (tPort)
  1229. {
  1230. sprintf(tQBuf,
  1231. "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`port`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',%d,12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s', "
  1232. "`port`=%d",
  1233. tPtr->RefID,
  1234. // tPtr->XAddrs,
  1235. tPath, tIpBuf, tPort, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto,
  1236. // tPtr->XAddrs,
  1237. tIpBuf, tPort);
  1238. }
  1239. else
  1240. {
  1241. sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s'",
  1242. tPtr->RefID,
  1243. // tPtr->XAddrs,
  1244. tPath, tIpBuf, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto,
  1245. // tPtr->XAddrs,
  1246. tIpBuf);
  1247. }
  1248. #ifdef DEBUG_ALL
  1249. LOG(tQBuf);
  1250. #endif
  1251. m_pDataStorage->SilentQuery(tQBuf);
  1252. return true;
  1253. };
  1254. return false;
  1255. }
  1256. uint32_t sqliteDBC::addLocalCam(CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den)
  1257. {
  1258. uint32_t RetVal = 0;
  1259. if (m_bStoIsConnected)
  1260. {
  1261. char tQBuf[1024];
  1262. char tM5[33];
  1263. if (den) den = den;
  1264. CSTR tMD5 = MD5String(path);
  1265. if (tMD5 != NULL)
  1266. {
  1267. memcpy(tM5, tMD5, 33);
  1268. tM5[32] = (char) 0;
  1269. free(tMD5);
  1270. sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`type`,`status`,`hardware`,`name`,`ip`) VALUES (1,'%s',10,0,'%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1", tM5, hardware, name, path);
  1271. #ifdef DEBUG_ALL
  1272. LOG(tQBuf);
  1273. #endif
  1274. uint32_t camID = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1275. if (camID == 0)
  1276. {
  1277. camID = getCamId(tM5);
  1278. };
  1279. sprintf(tQBuf,
  1280. "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`mrl_proto`,`mrl`,`picture_proto`,`fps`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2',%d) ON CONFLICT(`source_id`,`token`) "
  1281. "DO UPDATE SET `valid`=1, img_width=%d, img_height=%d, `fps`=%d",
  1282. camID, name, tM5, width, height, path, fps, width, height, fps);
  1283. #ifdef DEBUG_ALL
  1284. LOG(tQBuf);
  1285. #endif
  1286. RetVal = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1287. if (RetVal == 0)
  1288. {
  1289. RetVal = getMediaStreamID(camID, tM5);
  1290. };
  1291. };
  1292. };
  1293. return RetVal;
  1294. }
  1295. uint32_t sqliteDBC::addLocalNetwork(uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast)
  1296. {
  1297. uint32_t RetVal = 0;
  1298. if (m_bStoIsConnected)
  1299. {
  1300. char tQBuf[MAX_PATH_LEN];
  1301. sprintf(tQBuf, "INSERT INTO `networks`(`nif_id`,`type`,`address`,`gateway`,`mask`,`broadcast`) VALUES (%d,%d,'%s','%s','%s','%s') ON CONFLICT(`nif_id`,`type`) DO UPDATE SET `address`='%s', `gateway`='%s', `mask`='%s', `broadcast`='%s'",
  1302. NIF_ID, net_type, address, gateway, mask, broadcast, address, gateway, mask, broadcast);
  1303. #ifdef DEBUG_ALL
  1304. LOG(tQBuf);
  1305. #endif
  1306. RetVal = m_pDataStorage->QueryID(tQBuf);
  1307. // On Update IF data no need to return ID
  1308. // if( RetVal == 0 ) {
  1309. // RetVal = getNetworkInterfaceID( tKey );
  1310. //};
  1311. };
  1312. return RetVal;
  1313. }
  1314. uint32_t sqliteDBC::addLocalNetworkInfrastructure(uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME)
  1315. {
  1316. uint32_t RetVal = 0;
  1317. if (m_bStoIsConnected)
  1318. {
  1319. char tQBuf[MAX_PATH_LEN];
  1320. bool ena_routing = true;
  1321. if (OUTBOUND_NIF == 0)
  1322. {
  1323. ena_routing = false;
  1324. };
  1325. if (ena_dhcp)
  1326. {
  1327. if (BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL)
  1328. {
  1329. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  1330. return 0;
  1331. };
  1332. sprintf(tQBuf,
  1333. "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`,`enable_dhcp`,`dhcp_range_begin`,`dhcp_range_end`,`dhcp_range_mask`,`dhcp_lease_time`) VALUES (%d,%d,%d,1,'%s','%s','%s','%s') ON CONFLICT(`nif_id`) DO "
  1334. "UPDATE SET `enable_routing`=%d, `outbond_nif`=%d, `enable_dhcp`=1, `dhcp_range_begin`='%s', `dhcp_range_end`='%s', `dhcp_range_mask`='%s', `dhcp_lease_time`='%s'",
  1335. NIF_ID, ena_routing, OUTBOUND_NIF, BRANG, ERANG, MASK, LEASE_TIME, ena_routing, OUTBOUND_NIF, BRANG, ERANG, MASK, LEASE_TIME);
  1336. }
  1337. else
  1338. {
  1339. if (ena_routing)
  1340. {
  1341. sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`) VALUES(%d,1,%d) ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=1, `outbond_nif`=%d", NIF_ID, OUTBOUND_NIF, OUTBOUND_NIF);
  1342. }
  1343. else
  1344. {
  1345. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  1346. return 0;
  1347. };
  1348. };
  1349. #ifdef DEBUG_ALL
  1350. LOG(tQBuf);
  1351. #endif
  1352. RetVal = m_pDataStorage->QueryID(tQBuf);
  1353. // On Update IF data no need to return ID
  1354. if (RetVal == 0)
  1355. {
  1356. RetVal = getNetworkInfrastructureID(NIF_ID);
  1357. };
  1358. };
  1359. return RetVal;
  1360. }
  1361. uint32_t sqliteDBC::addLocalNIF(CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id)
  1362. {
  1363. uint32_t RetVal = 0;
  1364. if (m_bStoIsConnected)
  1365. {
  1366. char tQBuf[MAX_PATH_LEN];
  1367. char * tKey = MD5String(device);
  1368. if (tKey != NULL)
  1369. {
  1370. sprintf(tQBuf, "INSERT INTO `network_interfaces` (`type`,`key`,`device_name`,`mac`,`mode`,`aux_id`) VALUES (%d,'%s','%s','%s',%d,%d) ON CONFLICT(`key`) DO UPDATE SET `mode`=%d,`aux_id`=%d,`mac`='%s'", dtype, tKey, device, mac, dmode,
  1371. aux_id, dmode, aux_id, mac);
  1372. #ifdef DEBUG_ALL
  1373. LOG(tQBuf);
  1374. #endif
  1375. RetVal = m_pDataStorage->QueryID(tQBuf);
  1376. if (RetVal == 0)
  1377. {
  1378. RetVal = getNIF_ID(tKey);
  1379. };
  1380. free(tKey);
  1381. };
  1382. };
  1383. return RetVal;
  1384. }
  1385. uint32_t sqliteDBC::addLocalUART(uint8_t utype, uint32_t speed, uint8_t bit, char parity, float stop_bit, CSTR PATH, uint8_t mode, uint16_t port)
  1386. {
  1387. uint32_t RetVal = 0;
  1388. if (m_bStoIsConnected)
  1389. {
  1390. char tQBuf[MAX_PATH_LEN];
  1391. sprintf(tQBuf, "INSERT INTO `uart_settings`(`node_id`,`type`,`speed`,`bit`,`parity`,`stop_bit`,`mode`,`port`,`path`) VALUES (%d,%d,%d,%d,'%c',%.1f,%d,%d,'%s') ON CONFLICT(`node_id`,`type`,`path`) DO UPDATE SET `id`=`id`", m_nServerNodeID,
  1392. utype, speed, bit, parity, stop_bit, mode, port, PATH);
  1393. #ifdef DEBUG_ALL
  1394. LOG(tQBuf);
  1395. #endif
  1396. RetVal = m_pDataStorage->QueryID(tQBuf);
  1397. if (RetVal == 0)
  1398. {
  1399. RetVal = getUartID(m_nServerNodeID, utype, PATH);
  1400. };
  1401. };
  1402. return RetVal;
  1403. }
  1404. uint32_t sqliteDBC::addLocalWiFi(uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id)
  1405. {
  1406. uint32_t RetVal = 0;
  1407. if (m_bStoIsConnected)
  1408. {
  1409. char tQBuf[MAX_PATH_LEN];
  1410. sprintf(tQBuf,
  1411. "INSERT INTO `wifis` (`nif_id`,`mode`,`aux_id`,`SSID`,`password`,`encryption_type`) VALUES (%d,%d,%d,'%s','%s',%d) ON CONFLICT (`nif_id`) DO UPDATE SET `mode`=%d, `aux_id`=%d, `SSID`='%s', `password`='%s', `encryption_type`=%d", NIF_ID,
  1412. mode, router_id, SSID, password, encryption, mode, router_id, SSID, password, encryption);
  1413. #ifdef DEBUG_ALL
  1414. LOG(tQBuf);
  1415. #endif
  1416. RetVal = m_pDataStorage->QueryID(tQBuf);
  1417. if (RetVal == 0)
  1418. {
  1419. RetVal = getWiFi_ID(NIF_ID);
  1420. };
  1421. };
  1422. return RetVal;
  1423. }
  1424. bool sqliteDBC::cleanLRFMask(uint32_t maskID, bool deleteMask)
  1425. {
  1426. if (m_bStoIsConnected)
  1427. {
  1428. char tQBuf[MAX_PATH_LEN];
  1429. // sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
  1430. #ifdef DEBUG_ALL
  1431. // LOG(tQBuf);
  1432. #endif
  1433. // m_pDataStorage->SilentQuery( tQBuf );
  1434. // sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
  1435. #ifdef DEBUG_ALL
  1436. // LOG(tQBuf);
  1437. #endif
  1438. // m_pDataStorage->SilentQuery( tQBuf );
  1439. sprintf(tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID);
  1440. #ifdef DEBUG
  1441. LOG(tQBuf);
  1442. #endif
  1443. m_pDataStorage->SilentQuery(tQBuf);
  1444. sprintf(tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID);
  1445. #ifdef DEBUG_ALL
  1446. LOG(tQBuf);
  1447. #endif
  1448. m_pDataStorage->SilentQuery(tQBuf);
  1449. // Delete Mask Itself!
  1450. if (deleteMask)
  1451. {
  1452. sprintf(tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID);
  1453. #ifdef DEBUG_ALL
  1454. LOG(tQBuf);
  1455. #endif
  1456. m_pDataStorage->SilentQuery(tQBuf);
  1457. };
  1458. return true;
  1459. };
  1460. return false;
  1461. }
  1462. bool sqliteDBC::cleanLRFWidthTable(uint32_t sensorID)
  1463. {
  1464. if (m_bStoIsConnected)
  1465. {
  1466. char tQBuf[MAX_PATH_LEN];
  1467. sprintf(tQBuf, "DELETE FROM `lrf_detection_width_tables` WHERE `aux_id`=%d", sensorID);
  1468. #ifdef DEBUG_ALL
  1469. LOG(tQBuf);
  1470. #endif
  1471. m_pDataStorage->SilentQuery(tQBuf);
  1472. return true;
  1473. };
  1474. return false;
  1475. }
  1476. bool sqliteDBC::cleanResources(bool all)
  1477. {
  1478. if (m_bStoIsConnected)
  1479. {
  1480. char tQBuf[MAX_PATH_LEN];
  1481. if (all)
  1482. {
  1483. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID);
  1484. }
  1485. else
  1486. {
  1487. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE ( `node_id`=%d ) AND ( `type`=100 OR `type`=101 OR `type`=103 OR `type`=120 )", m_nServerNodeID);
  1488. };
  1489. #ifdef DEBUG_ALL
  1490. LOG(tQBuf);
  1491. #endif
  1492. m_pDataStorage->SilentQuery(tQBuf);
  1493. return true;
  1494. };
  1495. return false;
  1496. }
  1497. bool sqliteDBC::wipeServerResources()
  1498. {
  1499. if (m_bStoIsConnected)
  1500. {
  1501. char tQBuf[MAX_PATH_LEN];
  1502. sprintf(tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID);
  1503. #ifdef DEBUG_ALL
  1504. LOG(tQBuf);
  1505. #endif
  1506. m_pDataStorage->SilentQuery(tQBuf);
  1507. return true;
  1508. };
  1509. return false;
  1510. }
  1511. uint32_t sqliteDBC::addLRFMask(uint32_t devID, uint8_t lrfID, uint8_t layerID)
  1512. {
  1513. uint32_t RetVal = 0;
  1514. if (m_bStoIsConnected)
  1515. {
  1516. char tQBuf[MAX_PATH_LEN];
  1517. sprintf(tQBuf, "INSERT INTO `lrf_masks`(`sensor_id`,`lrf_id`,`layer`) VALUES(%d,%d,%d) ON CONFLICT(`sensor_id`,`lrf_id`,`layer`) DO UPDATE SET `layer`=%d", devID, lrfID, layerID, layerID);
  1518. #ifdef DEBUG
  1519. LOG(tQBuf);
  1520. #endif
  1521. RetVal = m_pDataStorage->QueryID(tQBuf);
  1522. if (RetVal == 0)
  1523. {
  1524. RetVal = getLRFMaskID(devID, lrfID, layerID);
  1525. };
  1526. };
  1527. return RetVal;
  1528. }
  1529. uint32_t sqliteDBC::addLRFMaskPolygone(uint32_t maskID, uint8_t vertexID, uint8_t r, uint8_t g, uint8_t b)
  1530. {
  1531. uint32_t RetVal = 0;
  1532. if (m_bStoIsConnected)
  1533. {
  1534. char tQBuf[MAX_PATH_LEN];
  1535. sprintf(tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`vertex_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d,%d)", maskID, vertexID, r, g, b);
  1536. #ifdef DEBUG_ALL
  1537. LOG(tQBuf);
  1538. #endif
  1539. RetVal = m_pDataStorage->QueryID(tQBuf);
  1540. if (RetVal == 0)
  1541. {
  1542. RetVal = getLRFMaskPolygoneID(maskID, vertexID);
  1543. };
  1544. };
  1545. return RetVal;
  1546. }
  1547. bool sqliteDBC::addLRFMaskPolygonePoint(uint32_t polyID, int16_t x, int16_t y)
  1548. {
  1549. if (m_bStoIsConnected)
  1550. {
  1551. char tQBuf[MAX_PATH_LEN];
  1552. sprintf(tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)", polyID, x, y);
  1553. #ifdef DEBUG_ALL
  1554. LOG(tQBuf);
  1555. #endif
  1556. m_pDataStorage->SilentQuery(tQBuf);
  1557. return true;
  1558. };
  1559. return false;
  1560. }
  1561. bool sqliteDBC::addLRFWidthTable(uint32_t sensorID, PWIDTH_TABLE ptr)
  1562. {
  1563. if (m_bStoIsConnected)
  1564. {
  1565. char tQBuf[MAX_PATH_LEN];
  1566. float kK = 100.0f / ptr->nCount;
  1567. for (int i = 0; i < ptr->nCount; i++)
  1568. {
  1569. uint16_t CC = kK * i;
  1570. sprintf(tQBuf, "INSERT INTO `lrf_detection_width_tables`(`aux_id`,`width_k`,`range_k`) VALUES(%d,%d,%d)", sensorID, ptr->pTable[i], CC);
  1571. #ifdef DEBUG_ALL
  1572. LOG(tQBuf);
  1573. #endif
  1574. m_pDataStorage->SilentQuery(tQBuf);
  1575. };
  1576. return true;
  1577. };
  1578. return false;
  1579. }
  1580. bool sqliteDBC::addPicture(DBC_SNAPSHOT_DATA * ptr, CSTR ext)
  1581. {
  1582. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  1583. bool rVal = false;
  1584. if (ptr != NULL)
  1585. {
  1586. if (m_bRepIsConnected)
  1587. {
  1588. char tQBuf[2048];
  1589. uint32_t pic_id;
  1590. sprintf(tQBuf, "%s/%s/%s/%s/%s.%s", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY, ptr->md5, ext);
  1591. #ifdef DEBUG_ALL
  1592. LOG(tQBuf);
  1593. #endif
  1594. FILE * fd = fopen(tQBuf, "wb+");
  1595. if (fd)
  1596. {
  1597. fwrite(ptr->data, 1, ptr->data_size, fd);
  1598. fclose(fd);
  1599. chown(tQBuf, getUserUid(web_access_user), getUserGid(web_access_group));
  1600. }
  1601. else
  1602. {
  1603. LOG_ERROR("Error Saving Captured picture!");
  1604. return rVal;
  1605. };
  1606. sprintf(tQBuf, "INSERT INTO pictures(`media_stream_id`,`uts`,`uts_ms`,`md5`,`ext`) VALUES(%d,%d,0,'%s','%s')", ptr->stream_id, ptr->ts, ptr->md5, ext);
  1607. pic_id = (uint32_t) m_pDataReports->QueryID(tQBuf);
  1608. if (pic_id)
  1609. {
  1610. for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
  1611. {
  1612. sprintf(tQBuf, "INSERT INTO `events_2_pictures`(`evt_id`,`picture_id`) VALUES(%d,%d)", *idx, pic_id);
  1613. m_pDataReports->SilentQuery(tQBuf);
  1614. };
  1615. rVal = true;
  1616. };
  1617. };
  1618. };
  1619. return rVal;
  1620. }
  1621. uint32_t sqliteDBC::addResource(uint8_t type, CSTR devName, uint32_t auxID)
  1622. {
  1623. uint32_t RetVal = 0;
  1624. if (m_bStoIsConnected)
  1625. {
  1626. char tQBuf[2048];
  1627. sprintf(tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName);
  1628. CSTR tMD5 = MD5String(tQBuf);
  1629. if (tMD5)
  1630. {
  1631. sprintf(tQBuf, "INSERT INTO `resources`(`node_id`,`type`,`md5`,`aux_id`,`device`,`active`) VALUES (%d,%d,'%s',%d,'%s',1) ON CONFLICT(`md5`) DO UPDATE SET `active`=1, `aux_id`=%d", m_nServerNodeID, type, tMD5, auxID, devName, auxID);
  1632. free(tMD5);
  1633. #ifdef DEBUG_ALL
  1634. LOG(tQBuf);
  1635. #endif
  1636. RetVal = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1637. if (RetVal == 0)
  1638. {
  1639. RetVal = getResourceID(m_nServerNodeID, type, auxID);
  1640. };
  1641. };
  1642. };
  1643. return RetVal;
  1644. }
  1645. bool sqliteDBC::addVideoEvent(uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms)
  1646. {
  1647. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  1648. if (m_bRepIsConnected)
  1649. {
  1650. char tQBuf[2048];
  1651. sprintf(tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)", evtID, videoID, frame, tss, tms);
  1652. #ifdef DEBUG_ALL
  1653. LOG(tQBuf);
  1654. #endif
  1655. m_pDataReports->SilentQuery(tQBuf);
  1656. };
  1657. return false;
  1658. }
  1659. uint32_t sqliteDBC::addVideoRecord(uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps)
  1660. {
  1661. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  1662. uint32_t RetVal = 0;
  1663. if (m_bRepIsConnected)
  1664. {
  1665. updateStats(false, true);
  1666. char tQBuf[2048];
  1667. sprintf(tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)", streamID, md5, tss, tms, ext, fps);
  1668. #ifdef DEBUG_EXTRA
  1669. LOG(tQBuf);
  1670. #endif
  1671. RetVal = (uint32_t) m_pDataReports->QueryID(tQBuf);
  1672. };
  1673. return RetVal;
  1674. }
  1675. DBC_MODULE_EMBED_VIDEO_DATA * sqliteDBC::getEmbedVideoData(uint32_t ID)
  1676. {
  1677. DBC_MODULE_EMBED_VIDEO_DATA * tPtr = NULL;
  1678. if (m_bStoIsConnected)
  1679. {
  1680. char tQBuf[MAX_PATH_LEN];
  1681. AAssoc * result;
  1682. sprintf(tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID);
  1683. #ifdef DEBUG_ALL
  1684. LOG(tQBuf);
  1685. #endif
  1686. result = m_pDataStorage->Query(tQBuf);
  1687. if (result != NULL)
  1688. {
  1689. if (result->size() > 0)
  1690. {
  1691. Assoc * cArr;
  1692. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA *) malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1693. if (tPtr != NULL)
  1694. {
  1695. Assoc::iterator it;
  1696. memset(tPtr, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1697. cArr = result->at(0);
  1698. it = cArr->find("output_mode");
  1699. if (it != cArr->end())
  1700. {
  1701. tPtr->output_mode = atoi(it->second.c_str());
  1702. };
  1703. // Togo Bug
  1704. if (tPtr->output_mode == 0)
  1705. {
  1706. tPtr->output_mode = 2; // Video Output
  1707. }
  1708. it = cArr->find("web_port");
  1709. if (it != cArr->end())
  1710. {
  1711. tPtr->port = atoi(it->second.c_str());
  1712. };
  1713. it = cArr->find("width");
  1714. if (it != cArr->end())
  1715. {
  1716. tPtr->width = atoi(it->second.c_str());
  1717. };
  1718. it = cArr->find("height");
  1719. if (it != cArr->end())
  1720. {
  1721. tPtr->height = atoi(it->second.c_str());
  1722. };
  1723. it = cArr->find("compression_quality");
  1724. if (it != cArr->end())
  1725. {
  1726. tPtr->compression_quality = atoi(it->second.c_str());
  1727. };
  1728. it = cArr->find("show_ts");
  1729. if (it != cArr->end())
  1730. {
  1731. tPtr->show_ts = (bool) atoi(it->second.c_str());
  1732. };
  1733. it = cArr->find("ts_x");
  1734. if (it != cArr->end())
  1735. {
  1736. tPtr->ts_x = atoi(it->second.c_str());
  1737. };
  1738. it = cArr->find("ts_y");
  1739. if (it != cArr->end())
  1740. {
  1741. tPtr->ts_y = atoi(it->second.c_str());
  1742. };
  1743. it = cArr->find("ts_font_face");
  1744. if (it != cArr->end())
  1745. {
  1746. tPtr->ts_font_face = atoi(it->second.c_str());
  1747. };
  1748. it = cArr->find("ts_font_scale");
  1749. if (it != cArr->end())
  1750. {
  1751. tPtr->ts_font_scale = atof(it->second.c_str());
  1752. };
  1753. it = cArr->find("ts_font_thickness");
  1754. if (it != cArr->end())
  1755. {
  1756. tPtr->ts_font_thickness = atoi(it->second.c_str());
  1757. };
  1758. it = cArr->find("ts_line_type");
  1759. if (it != cArr->end())
  1760. {
  1761. tPtr->ts_line_type = atoi(it->second.c_str());
  1762. };
  1763. it = cArr->find("ts_transperant");
  1764. if (it != cArr->end())
  1765. {
  1766. tPtr->ts_transperant = (bool) atoi(it->second.c_str());
  1767. };
  1768. it = cArr->find("show_label");
  1769. if (it != cArr->end())
  1770. {
  1771. tPtr->show_label = (bool) atoi(it->second.c_str());
  1772. };
  1773. it = cArr->find("label_x");
  1774. if (it != cArr->end())
  1775. {
  1776. tPtr->label_x = atoi(it->second.c_str());
  1777. };
  1778. it = cArr->find("label_y");
  1779. if (it != cArr->end())
  1780. {
  1781. tPtr->label_y = atoi(it->second.c_str());
  1782. };
  1783. it = cArr->find("label_font_face");
  1784. if (it != cArr->end())
  1785. {
  1786. tPtr->label_font_face = atoi(it->second.c_str());
  1787. };
  1788. it = cArr->find("label_font_scale");
  1789. if (it != cArr->end())
  1790. {
  1791. tPtr->label_font_scale = atof(it->second.c_str());
  1792. };
  1793. it = cArr->find("label_font_thickness");
  1794. if (it != cArr->end())
  1795. {
  1796. tPtr->label_font_thickness = atoi(it->second.c_str());
  1797. };
  1798. it = cArr->find("label_line_type");
  1799. if (it != cArr->end())
  1800. {
  1801. tPtr->label_line_type = atoi(it->second.c_str());
  1802. };
  1803. it = cArr->find("label_transperant");
  1804. if (it != cArr->end())
  1805. {
  1806. tPtr->label_transperant = (bool) atoi(it->second.c_str());
  1807. };
  1808. it = cArr->find("show_logo");
  1809. if (it != cArr->end())
  1810. {
  1811. tPtr->show_logo = (bool) atoi(it->second.c_str());
  1812. };
  1813. it = cArr->find("logo_w");
  1814. if (it != cArr->end())
  1815. {
  1816. tPtr->logo_w = atoi(it->second.c_str());
  1817. };
  1818. it = cArr->find("logo_h");
  1819. if (it != cArr->end())
  1820. {
  1821. tPtr->logo_h = atoi(it->second.c_str());
  1822. };
  1823. it = cArr->find("logo_x");
  1824. if (it != cArr->end())
  1825. {
  1826. tPtr->logo_x = atoi(it->second.c_str());
  1827. };
  1828. it = cArr->find("logo_y");
  1829. if (it != cArr->end())
  1830. {
  1831. tPtr->logo_y = atoi(it->second.c_str());
  1832. };
  1833. it = cArr->find("logo_md5");
  1834. if (it != cArr->end())
  1835. {
  1836. strncpy(tPtr->logo_md5, it->second.c_str(), 32);
  1837. tPtr->logo_md5[32] = (char) 0;
  1838. };
  1839. // Live view (optional columns)
  1840. dbcEmbedVideoLiveDefaults(tPtr);
  1841. it = cArr->find("live_mode");
  1842. if (it != cArr->end() && ! it->second.empty())
  1843. {
  1844. tPtr->live_mode = (uint8_t) atoi(it->second.c_str());
  1845. };
  1846. it = cArr->find("live_port");
  1847. if (it != cArr->end() && atoi(it->second.c_str()) > 0)
  1848. {
  1849. tPtr->live_port = (uint16_t) atoi(it->second.c_str());
  1850. };
  1851. it = cArr->find("live_record");
  1852. if (it != cArr->end() && ! it->second.empty())
  1853. {
  1854. tPtr->live_record = (bool) atoi(it->second.c_str());
  1855. };
  1856. it = cArr->find("live_record_segment");
  1857. if (it != cArr->end() && atoi(it->second.c_str()) > 0)
  1858. {
  1859. tPtr->live_record_segment = (uint16_t) atoi(it->second.c_str());
  1860. };
  1861. it = cArr->find("live_bitrate");
  1862. if (it != cArr->end() && atoi(it->second.c_str()) > 0)
  1863. {
  1864. tPtr->live_bitrate = (uint16_t) atoi(it->second.c_str());
  1865. };
  1866. it = cArr->find("live_fps");
  1867. if (it != cArr->end() && atoi(it->second.c_str()) > 0)
  1868. {
  1869. tPtr->live_fps = (uint8_t) atoi(it->second.c_str());
  1870. };
  1871. it = cArr->find("live_ice_servers");
  1872. if (it != cArr->end())
  1873. {
  1874. strncpy(tPtr->live_ice_servers, it->second.c_str(), sizeof(tPtr->live_ice_servers) - 1);
  1875. tPtr->live_ice_servers[sizeof(tPtr->live_ice_servers) - 1] = (char) 0;
  1876. };
  1877. };
  1878. };
  1879. };
  1880. deleteResult(result);
  1881. };
  1882. return tPtr;
  1883. }
  1884. DBC_MEDIA_STREAM_DATA * sqliteDBC::getMediaStreamData(uint32_t ID)
  1885. {
  1886. DBC_MEDIA_STREAM_DATA * tVal = NULL;
  1887. if (m_bStoIsConnected)
  1888. {
  1889. char tQBuf[CTEXT];
  1890. AAssoc * result;
  1891. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID);
  1892. #ifdef DEBUG_ALL
  1893. LOG(tQBuf);
  1894. #endif
  1895. result = m_pDataStorage->Query(tQBuf);
  1896. if (result != NULL)
  1897. {
  1898. if (result->size() > 0)
  1899. {
  1900. Assoc * cArr;
  1901. tVal = (DBC_MEDIA_STREAM_DATA *) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1902. if (tVal != NULL)
  1903. {
  1904. memset(tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA));
  1905. Assoc::iterator it;
  1906. cArr = result->at(0);
  1907. tVal->id = ID;
  1908. it = cArr->find("source_id");
  1909. if (it != cArr->end())
  1910. {
  1911. tVal->source_id = atoi(it->second.c_str());
  1912. };
  1913. it = cArr->find("source_type");
  1914. if (it != cArr->end())
  1915. {
  1916. sprintf(tVal->source_type, it->second.c_str());
  1917. };
  1918. it = cArr->find("valid");
  1919. if (it != cArr->end())
  1920. {
  1921. tVal->valid = (bool) atoi(it->second.c_str());
  1922. };
  1923. it = cArr->find("token");
  1924. if (it != cArr->end())
  1925. {
  1926. sprintf(tVal->token, it->second.c_str());
  1927. };
  1928. it = cArr->find("img_width");
  1929. if (it != cArr->end())
  1930. {
  1931. tVal->width = atoi(it->second.c_str());
  1932. };
  1933. it = cArr->find("img_height");
  1934. if (it != cArr->end())
  1935. {
  1936. tVal->height = atoi(it->second.c_str());
  1937. };
  1938. it = cArr->find("fps");
  1939. if (it != cArr->end())
  1940. {
  1941. tVal->fps = atoi(it->second.c_str());
  1942. };
  1943. it = cArr->find("compression_type");
  1944. if (it != cArr->end())
  1945. {
  1946. sprintf(tVal->c_type, it->second.c_str());
  1947. };
  1948. it = cArr->find("mrl_proto");
  1949. if (it != cArr->end())
  1950. {
  1951. sprintf(tVal->mrl_proto, it->second.c_str());
  1952. };
  1953. it = cArr->find("mrl_port");
  1954. if (it != cArr->end())
  1955. {
  1956. tVal->mrl_port = atoi(it->second.c_str());
  1957. };
  1958. it = cArr->find("mrl");
  1959. if (it != cArr->end())
  1960. {
  1961. sprintf(tVal->mrl, it->second.c_str());
  1962. };
  1963. };
  1964. };
  1965. };
  1966. deleteResult(result);
  1967. };
  1968. return tVal;
  1969. }
  1970. uint32_t sqliteDBC::getMediaStreamID(uint32_t source_id, CSTR token)
  1971. {
  1972. uint32_t RetVal = 0;
  1973. if (m_bStoIsConnected)
  1974. {
  1975. char tQBuf[CTEXT];
  1976. AAssoc * result;
  1977. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token);
  1978. #ifdef DEBUG_ALL
  1979. LOG(tQBuf);
  1980. #endif
  1981. result = m_pDataStorage->Query(tQBuf);
  1982. if (result != NULL)
  1983. {
  1984. if (result->size() > 0)
  1985. {
  1986. Assoc * cArr;
  1987. Assoc::iterator it;
  1988. cArr = result->at(0);
  1989. it = cArr->find("id");
  1990. if (it != cArr->end())
  1991. {
  1992. RetVal = atoi(it->second.c_str());
  1993. };
  1994. };
  1995. };
  1996. deleteResult(result);
  1997. };
  1998. return RetVal;
  1999. }
  2000. DBC_MODEM_MANAGER_DATA * sqliteDBC::getModemManagerData(uint32_t ID)
  2001. {
  2002. DBC_MODEM_MANAGER_DATA * tVal = NULL;
  2003. if (m_bStoIsConnected)
  2004. {
  2005. char tQBuf[CTEXT];
  2006. AAssoc * result;
  2007. sprintf(tQBuf, "SELECT * FROM `module_modem` WHERE `id`=%d", ID);
  2008. #ifdef DEBUG_ALL
  2009. LOG(tQBuf);
  2010. #endif
  2011. result = m_pDataStorage->Query(tQBuf);
  2012. if (result != NULL)
  2013. {
  2014. if (result->size() > 0)
  2015. {
  2016. Assoc * cArr;
  2017. tVal = (DBC_MODEM_MANAGER_DATA *) malloc(sizeof(DBC_MODEM_MANAGER_DATA));
  2018. if (tVal != NULL)
  2019. {
  2020. memset(tVal, 0, sizeof(DBC_MODEM_MANAGER_DATA));
  2021. tVal->id = ID;
  2022. Assoc::iterator it;
  2023. cArr = result->at(0);
  2024. it = cArr->find("modem_id");
  2025. if (it != cArr->end())
  2026. {
  2027. tVal->modem_id = atoi(it->second.c_str());
  2028. };
  2029. it = cArr->find("evt_mode");
  2030. if (it != cArr->end())
  2031. {
  2032. tVal->evt_mode = atoi(it->second.c_str());
  2033. };
  2034. it = cArr->find("time_window");
  2035. if (it != cArr->end())
  2036. {
  2037. tVal->time_window = atoi(it->second.c_str());
  2038. };
  2039. it = cArr->find("sms_enabled");
  2040. if (it != cArr->end())
  2041. {
  2042. tVal->sms_enabled = (bool) atoi(it->second.c_str());
  2043. };
  2044. it = cArr->find("call_enabled");
  2045. if (it != cArr->end())
  2046. {
  2047. tVal->call_enabled = (bool) atoi(it->second.c_str());
  2048. };
  2049. it = cArr->find("call_hangup_wait");
  2050. if (it != cArr->end())
  2051. {
  2052. tVal->call_hangup_wait = atoi(it->second.c_str());
  2053. };
  2054. };
  2055. };
  2056. };
  2057. deleteResult(result);
  2058. };
  2059. return tVal;
  2060. }
  2061. DBC_MODEM_PHONEBOOK_RECORD * sqliteDBC::getModemPhonebookRecord(uint32_t ID)
  2062. {
  2063. DBC_MODEM_PHONEBOOK_RECORD * tVal = NULL;
  2064. if (m_bStoIsConnected)
  2065. {
  2066. char tQBuf[CTEXT];
  2067. AAssoc * result;
  2068. sprintf(tQBuf, "SELECT * FROM `module_modem_phonebook` WHERE `id`=%d", ID);
  2069. #ifdef DEBUG_ALL
  2070. LOG(tQBuf);
  2071. #endif
  2072. result = m_pDataStorage->Query(tQBuf);
  2073. if (result != NULL)
  2074. {
  2075. if (result->size() > 0)
  2076. {
  2077. Assoc * cArr;
  2078. tVal = (DBC_MODEM_PHONEBOOK_RECORD *) malloc(sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  2079. if (tVal != NULL)
  2080. {
  2081. memset(tVal, 0, sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  2082. Assoc::iterator it;
  2083. cArr = result->at(0);
  2084. it = cArr->find("id");
  2085. if (it != cArr->end())
  2086. {
  2087. tVal->id = atoi(it->second.c_str());
  2088. };
  2089. it = cArr->find("number");
  2090. if (it != cArr->end())
  2091. {
  2092. strncpy(tVal->number, it->second.c_str(), 63);
  2093. tVal->number[63] = (char) 0;
  2094. };
  2095. it = cArr->find("mode");
  2096. if (it != cArr->end())
  2097. {
  2098. tVal->mode = atoi(it->second.c_str());
  2099. };
  2100. it = cArr->find("trig_special");
  2101. if (it != cArr->end())
  2102. {
  2103. tVal->trigger_special = atoi(it->second.c_str());
  2104. };
  2105. it = cArr->find("label");
  2106. if (it != cArr->end())
  2107. {
  2108. strncpy(tVal->label, it->second.c_str(), 63);
  2109. tVal->label[63] = (char) 0;
  2110. };
  2111. it = cArr->find("custom_msg");
  2112. if (it != cArr->end())
  2113. {
  2114. strncpy(tVal->custom_msg, it->second.c_str(), 127);
  2115. tVal->custom_msg[127] = (char) 0;
  2116. };
  2117. };
  2118. };
  2119. };
  2120. deleteResult(result);
  2121. };
  2122. return tVal;
  2123. }
  2124. DBC_MODULE_DATA * sqliteDBC::getModuleData(uint32_t ID)
  2125. {
  2126. DBC_MODULE_DATA * tVal = NULL;
  2127. if (m_bStoIsConnected)
  2128. {
  2129. char tQBuf[CTEXT];
  2130. AAssoc * result;
  2131. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID);
  2132. #ifdef DEBUG_ALL
  2133. LOG(tQBuf);
  2134. #endif
  2135. result = m_pDataStorage->Query(tQBuf);
  2136. if (result != NULL)
  2137. {
  2138. if (result->size() > 0)
  2139. {
  2140. Assoc * cArr;
  2141. tVal = (DBC_MODULE_DATA *) malloc(sizeof(DBC_MODULE_DATA));
  2142. if (tVal != NULL)
  2143. {
  2144. Assoc::iterator it;
  2145. cArr = result->at(0);
  2146. it = cArr->find("id");
  2147. if (it != cArr->end())
  2148. {
  2149. tVal->module_id = atoi(it->second.c_str());
  2150. };
  2151. it = cArr->find("aux_id");
  2152. if (it != cArr->end())
  2153. {
  2154. tVal->aux_id = atoi(it->second.c_str());
  2155. };
  2156. it = cArr->find("type");
  2157. if (it != cArr->end())
  2158. {
  2159. tVal->type = atoi(it->second.c_str());
  2160. };
  2161. it = cArr->find("enabled");
  2162. if (it != cArr->end())
  2163. {
  2164. tVal->enabled = (bool) atoi(it->second.c_str());
  2165. };
  2166. };
  2167. };
  2168. };
  2169. deleteResult(result);
  2170. };
  2171. return tVal;
  2172. }
  2173. DBC_LRF_INI * sqliteDBC::getINIData(uint32_t lrfID)
  2174. {
  2175. DBC_LRF_INI * tVal = NULL;
  2176. if (m_bStoIsConnected)
  2177. {
  2178. char tQBuf[CTEXT];
  2179. AAssoc * result;
  2180. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID);
  2181. #ifdef DEBUG_ALL
  2182. LOG(tQBuf);
  2183. #endif
  2184. result = m_pDataStorage->Query(tQBuf);
  2185. if (result != NULL)
  2186. {
  2187. if (result->size() > 0)
  2188. {
  2189. Assoc * cArr;
  2190. tVal = (DBC_LRF_INI *) malloc(sizeof(DBC_LRF_INI));
  2191. if (tVal != NULL)
  2192. {
  2193. memset(tVal, 0, sizeof(DBC_LRF_INI));
  2194. Assoc::iterator it;
  2195. cArr = result->at(0);
  2196. it = cArr->find("AUTORESET_ENABLE");
  2197. if (it != cArr->end())
  2198. {
  2199. tVal->_AUTO_RESET_ENABLE = (bool) atoi(it->second.c_str());
  2200. };
  2201. it = cArr->find("AUTORESET_INTERLEAVE");
  2202. if (it != cArr->end())
  2203. {
  2204. tVal->_AUTO_RESET_INTERLEAVE = atoi(it->second.c_str());
  2205. };
  2206. it = cArr->find("AUTORESET_TIMEOUT");
  2207. if (it != cArr->end())
  2208. {
  2209. tVal->_AUTO_RESET_TIMEOUT = atoi(it->second.c_str());
  2210. };
  2211. it = cArr->find("BOARD_TEMPERATURE");
  2212. if (it != cArr->end())
  2213. {
  2214. tVal->_BOARD_TEMPERATURE = atoi(it->second.c_str());
  2215. };
  2216. it = cArr->find("BOARD_ZEROSHIFT");
  2217. if (it != cArr->end())
  2218. {
  2219. tVal->_BOARD_ZEROSHIFT = atoi(it->second.c_str());
  2220. };
  2221. it = cArr->find("BOARD_SPEED");
  2222. if (it != cArr->end())
  2223. {
  2224. tVal->_BOARD_SPEED = atoi(it->second.c_str());
  2225. };
  2226. it = cArr->find("RUN_DIR");
  2227. if (it != cArr->end())
  2228. {
  2229. tVal->_RUN_DIR = atoi(it->second.c_str());
  2230. };
  2231. it = cArr->find("NO_BLACK_TARGETS");
  2232. if (it != cArr->end())
  2233. {
  2234. tVal->_NO_BLACK_TARGETS = atoi(it->second.c_str());
  2235. };
  2236. it = cArr->find("JUST_MOVING_TARGETS");
  2237. if (it != cArr->end())
  2238. {
  2239. tVal->_JUST_MOVING_TARGETS = (bool) atoi(it->second.c_str());
  2240. };
  2241. it = cArr->find("JUST_MOVING_RADIAL");
  2242. if (it != cArr->end())
  2243. {
  2244. tVal->_JUST_MOVING_RADIAL = (bool) atoi(it->second.c_str());
  2245. };
  2246. it = cArr->find("JUST_MOVING_DISTANCE");
  2247. if (it != cArr->end())
  2248. {
  2249. tVal->_JUST_MOVING_DISTANCE = (bool) atoi(it->second.c_str());
  2250. };
  2251. it = cArr->find("FREEZ_FIRST_TH");
  2252. if (it != cArr->end())
  2253. {
  2254. tVal->_FREEZ_FIRST_TH = (bool) atoi(it->second.c_str());
  2255. };
  2256. it = cArr->find("SAVE_FIRST_TH");
  2257. if (it != cArr->end())
  2258. {
  2259. tVal->_SAVE_FIRST_TH = (bool) atoi(it->second.c_str());
  2260. };
  2261. };
  2262. };
  2263. };
  2264. deleteResult(result);
  2265. };
  2266. return tVal;
  2267. }
  2268. bool sqliteDBC::getEventSessionStatus()
  2269. {
  2270. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  2271. bool rv = false;
  2272. if (m_bRepIsConnected)
  2273. {
  2274. char tQBuf[CTEXT];
  2275. AAssoc * result;
  2276. // Since Unimodule -> no need to check NodeID
  2277. // sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `node_id`=%d AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ) ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", nodeID, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA,
  2278. // EVT_LRF_END_SESSION_TO );
  2279. sprintf(tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO);
  2280. #ifdef DEBUG_ALL
  2281. LOG(tQBuf);
  2282. #endif
  2283. result = m_pDataReports->Query(tQBuf);
  2284. if (result != NULL)
  2285. {
  2286. if (result->size() > 0)
  2287. {
  2288. Assoc * cArr;
  2289. Assoc::iterator it;
  2290. cArr = result->at(0);
  2291. it = cArr->find("evt_type");
  2292. if (it != cArr->end())
  2293. {
  2294. if (atoi(it->second.c_str()) == EVT_LRF_START_SESSION)
  2295. {
  2296. rv = true;
  2297. }
  2298. };
  2299. };
  2300. };
  2301. deleteResult(result);
  2302. };
  2303. return rv;
  2304. }
  2305. /*
  2306. * The Last TS is from the current report (as latest)
  2307. */
  2308. uint32_t sqliteDBC::getLastEventTS()
  2309. {
  2310. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  2311. uint32_t rv = 0;
  2312. if (m_bRepIsConnected)
  2313. {
  2314. char tQBuf[CTEXT];
  2315. AAssoc * result;
  2316. sprintf(tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1");
  2317. #ifdef DEBUG_ALL
  2318. LOG(tQBuf);
  2319. #endif
  2320. result = m_pDataReports->Query(tQBuf);
  2321. if (result != NULL)
  2322. {
  2323. if (result->size() > 0)
  2324. {
  2325. Assoc * cArr;
  2326. Assoc::iterator it;
  2327. cArr = result->at(0);
  2328. it = cArr->find("ts");
  2329. if (it != cArr->end())
  2330. {
  2331. rv = atoi(it->second.c_str());
  2332. };
  2333. };
  2334. };
  2335. deleteResult(result);
  2336. };
  2337. return rv;
  2338. }
  2339. /*
  2340. * The first TS is generaly must show a DAY of first registered event so TS fetched
  2341. * from general statistics
  2342. */
  2343. uint32_t sqliteDBC::getFirstEventTS()
  2344. {
  2345. uint32_t rv = 0;
  2346. if (m_bStaIsConnected)
  2347. {
  2348. char tQBuf[CTEXT];
  2349. AAssoc * result;
  2350. sprintf(tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1");
  2351. #ifdef DEBUG_ALL
  2352. LOG(tQBuf);
  2353. #endif
  2354. result = m_pDataStatistics->Query(tQBuf);
  2355. if (result != NULL)
  2356. {
  2357. if (result->size() > 0)
  2358. {
  2359. REPORT_DIRINFO RDF;// = {0};
  2360. memset(&RDF, 0, sizeof(REPORT_DIRINFO));
  2361. Assoc * cArr;
  2362. Assoc::iterator it;
  2363. cArr = result->at(0);
  2364. it = cArr->find("year");
  2365. if (it != cArr->end())
  2366. {
  2367. strncpy(RDF.YEAR, it->second.c_str(), 4);
  2368. };
  2369. it = cArr->find("month");
  2370. if (it != cArr->end())
  2371. {
  2372. strncpy(RDF.MONTH, it->second.c_str(), 2);
  2373. };
  2374. it = cArr->find("day");
  2375. if (it != cArr->end())
  2376. {
  2377. strncpy(RDF.DAY, it->second.c_str(), 2);
  2378. };
  2379. rv = getTsFromRDF(RDF);
  2380. };
  2381. };
  2382. deleteResult(result);
  2383. };
  2384. return rv;
  2385. }
  2386. /*
  2387. Called from the kernel loop (validateReports: daily cleanup, or every second while the disk is above
  2388. preserve_empty). deleteRDFDir() runs "rm -f -R <whole day>" - many seconds (minutes on a big SD card):
  2389. the kernel loop stalled and the watchdog killed la_mojo. The day is deleted in a background thread;
  2390. while it runs, further calls return false (the oldest day is still listed until it is done).
  2391. */
  2392. bool sqliteDBC::deleteReportDate(uint32_t ts)
  2393. {
  2394. if (ts)
  2395. {
  2396. bool tFalse = false;
  2397. if (! s_bReportDayDeleting.compare_exchange_strong(tFalse, true))
  2398. {
  2399. return false; // a day is being deleted
  2400. };
  2401. REPORT_DIRINFO tRDF;// = {0};
  2402. fillRDF(ts, tRDF);
  2403. std::string tBase(m_pDbReportBase);
  2404. LOG("Delete RDF Dir (background)");
  2405. try
  2406. {
  2407. std::thread([this, tBase, tRDF]() mutable {
  2408. pthread_setname_np(pthread_self(), "ReportDelete");
  2409. if (deleteRDFDir((CSTR) tBase.c_str(), tRDF))
  2410. {
  2411. LOG("Dir Deleted OK");
  2412. deleteReportStat(tRDF);
  2413. };
  2414. s_bReportDayDeleting = false;
  2415. }).detach();
  2416. }
  2417. catch (const std::exception & e)
  2418. {
  2419. s_bReportDayDeleting = false;
  2420. LOG_ERROR("Can not start the report delete thread");
  2421. return false;
  2422. };
  2423. return true;
  2424. };
  2425. return false;
  2426. }
  2427. DBC_LRF_DATA * sqliteDBC::getLRFData(uint32_t ID)
  2428. {
  2429. DBC_LRF_DATA * tVal = NULL;
  2430. if (m_bStoIsConnected)
  2431. {
  2432. char tQBuf[CTEXT];
  2433. AAssoc * result;
  2434. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID);
  2435. #ifdef DEBUG_ALL
  2436. LOG(tQBuf);
  2437. #endif
  2438. result = m_pDataStorage->Query(tQBuf);
  2439. if (result != NULL)
  2440. {
  2441. if (result->size() > 0)
  2442. {
  2443. Assoc * cArr;
  2444. tVal = (DBC_LRF_DATA *) malloc(sizeof(DBC_LRF_DATA));
  2445. if (tVal != NULL)
  2446. {
  2447. memset(tVal, 0, sizeof(DBC_LRF_DATA));
  2448. Assoc::iterator it;
  2449. tVal->id = ID;
  2450. cArr = result->at(0);
  2451. it = cArr->find("data_status");
  2452. if (it != cArr->end())
  2453. {
  2454. tVal->data_status = atoi(it->second.c_str());
  2455. };
  2456. it = cArr->find("serial");
  2457. if (it != cArr->end())
  2458. {
  2459. strncpy(tVal->serial, it->second.c_str(), sizeof(tVal->serial) - 1);
  2460. tVal->serial[sizeof(tVal->serial) - 1] = (char) 0;
  2461. };
  2462. it = cArr->find("version");
  2463. if (it != cArr->end())
  2464. {
  2465. strncpy(tVal->version, it->second.c_str(), sizeof(tVal->version) - 1);
  2466. tVal->version[sizeof(tVal->version) - 1] = (char) 0;
  2467. };
  2468. it = cArr->find("x");
  2469. if (it != cArr->end())
  2470. {
  2471. tVal->x = atof(it->second.c_str());
  2472. };
  2473. it = cArr->find("y");
  2474. if (it != cArr->end())
  2475. {
  2476. tVal->y = atof(it->second.c_str());
  2477. };
  2478. it = cArr->find("z");
  2479. if (it != cArr->end())
  2480. {
  2481. tVal->z = atof(it->second.c_str());
  2482. };
  2483. it = cArr->find("x_rot");
  2484. if (it != cArr->end())
  2485. {
  2486. tVal->x_rot = atof(it->second.c_str());
  2487. };
  2488. it = cArr->find("y_rot");
  2489. if (it != cArr->end())
  2490. {
  2491. tVal->y_rot = atof(it->second.c_str());
  2492. };
  2493. it = cArr->find("z_rot");
  2494. if (it != cArr->end())
  2495. {
  2496. tVal->z_rot = atof(it->second.c_str());
  2497. };
  2498. it = cArr->find("uart_id");
  2499. if (it != cArr->end())
  2500. {
  2501. tVal->uart_id = atoi(it->second.c_str());
  2502. };
  2503. it = cArr->find("device_id");
  2504. if (it != cArr->end())
  2505. {
  2506. tVal->device_id = atoi(it->second.c_str());
  2507. };
  2508. it = cArr->find("ip");
  2509. if (it != cArr->end())
  2510. {
  2511. sprintf(tVal->master_host, it->second.c_str());
  2512. };
  2513. it = cArr->find("aux_ip");
  2514. if (it != cArr->end())
  2515. {
  2516. sprintf(tVal->slave_host, it->second.c_str());
  2517. };
  2518. it = cArr->find("port");
  2519. if (it != cArr->end())
  2520. {
  2521. tVal->master_port = atoi(it->second.c_str());
  2522. };
  2523. it = cArr->find("aux_port");
  2524. if (it != cArr->end())
  2525. {
  2526. tVal->slave_port = atoi(it->second.c_str());
  2527. };
  2528. it = cArr->find("auto_draw");
  2529. if (it != cArr->end())
  2530. {
  2531. tVal->auto_draw = atoi(it->second.c_str());
  2532. };
  2533. it = cArr->find("filter_id");
  2534. if (it != cArr->end())
  2535. {
  2536. tVal->filter_id = atoi(it->second.c_str());
  2537. };
  2538. it = cArr->find("enabled_lrf");
  2539. if (it != cArr->end())
  2540. {
  2541. tVal->enabled_lrf = atoi(it->second.c_str());
  2542. };
  2543. it = cArr->find("max_range");
  2544. if (it != cArr->end())
  2545. {
  2546. tVal->max_range = atof(it->second.c_str());
  2547. };
  2548. it = cArr->find("base_shift");
  2549. if (it != cArr->end())
  2550. {
  2551. tVal->base_shift = atoi(it->second.c_str());
  2552. };
  2553. // it = cArr->find("mirror_shift");
  2554. // if( it != cArr->end() ){
  2555. // tVal->mirror_shift = atoi( it->second.c_str() );
  2556. // };
  2557. it = cArr->find("detection_profile_id");
  2558. if (it != cArr->end())
  2559. {
  2560. tVal->detection_profile_id = atoi(it->second.c_str());
  2561. };
  2562. it = cArr->find("prefered_camera_id");
  2563. if (it != cArr->end())
  2564. {
  2565. tVal->prefered_camera_id = atoi(it->second.c_str());
  2566. };
  2567. /*
  2568. it = cArr->find("draw_lrf_id");
  2569. if( it != cArr->end() ){
  2570. tVal->draw_lrf_id = atoi( it->second.c_str() );
  2571. };
  2572. it = cArr->find("width_table_id");
  2573. if( it != cArr->end() ){
  2574. tVal->width_table_id = atoi( it->second.c_str() );
  2575. };
  2576. it = cArr->find("zoom_table_id");
  2577. if( it != cArr->end() ){
  2578. tVal->zoom_table_id = atoi( it->second.c_str() );
  2579. };
  2580. it = cArr->find("embd_cam");
  2581. if( it != cArr->end() ){
  2582. tVal->embd_cam = atoi( it->second.c_str() );
  2583. };
  2584. it = cArr->find("embd_cam_auto");
  2585. if( it != cArr->end() ){
  2586. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  2587. };
  2588. it = cArr->find("embd_dc");
  2589. if( it != cArr->end() ){
  2590. tVal->embd_dc = atoi( it->second.c_str() );
  2591. };
  2592. it = cArr->find("autoreset");
  2593. if( it != cArr->end() ){
  2594. tVal->autoreset = (bool)atoi( it->second.c_str() );
  2595. };
  2596. it = cArr->find("force_settings");
  2597. if( it != cArr->end() ){
  2598. tVal->force_settings = (bool)atoi( it->second.c_str() );
  2599. };
  2600. */
  2601. };
  2602. };
  2603. };
  2604. deleteResult(result);
  2605. };
  2606. return tVal;
  2607. }
  2608. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master)
  2609. {
  2610. PLGS_2D_PARAMETERS retVal = NULL;
  2611. if (m_bStoIsConnected)
  2612. {
  2613. char tQBuf[CTEXT];
  2614. AAssoc * result;
  2615. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID, (master) ? 1 : 0);
  2616. #ifdef DEBUG_ALL
  2617. LOG(tQBuf);
  2618. #endif
  2619. result = m_pDataStorage->Query(tQBuf);
  2620. if (result != NULL)
  2621. {
  2622. if (result->size() > 0)
  2623. {
  2624. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  2625. memset(retVal, 0, sizeof(LGS_2D_PARAMETERS));
  2626. Assoc * cArr;
  2627. Assoc::iterator it;
  2628. cArr = result->at(0);
  2629. it = cArr->find("order");
  2630. if (it != cArr->end())
  2631. {
  2632. retVal->nOrder = atoi(it->second.c_str());
  2633. };
  2634. it = cArr->find("offset");
  2635. if (it != cArr->end())
  2636. {
  2637. retVal->nOffset = atoi(it->second.c_str());
  2638. };
  2639. it = cArr->find("delta");
  2640. if (it != cArr->end())
  2641. {
  2642. retVal->nDelta = atoi(it->second.c_str());
  2643. };
  2644. it = cArr->find("flip");
  2645. if (it != cArr->end())
  2646. {
  2647. retVal->nFlip = atoi(it->second.c_str());
  2648. };
  2649. it = cArr->find("width");
  2650. if (it != cArr->end())
  2651. {
  2652. retVal->nWidth = atoi(it->second.c_str());
  2653. };
  2654. it = cArr->find("sst");
  2655. if (it != cArr->end())
  2656. {
  2657. retVal->bSST = atoi(it->second.c_str());
  2658. };
  2659. it = cArr->find("sun");
  2660. if (it != cArr->end())
  2661. {
  2662. retVal->nSun = atoi(it->second.c_str());
  2663. };
  2664. };
  2665. };
  2666. deleteResult(result);
  2667. };
  2668. return retVal;
  2669. }
  2670. RELAY_PARAMETERS * sqliteDBC::getLRFDryContact(uint32_t lrfID)
  2671. {
  2672. RELAY_PARAMETERS * retVal = NULL;
  2673. if (m_bStoIsConnected)
  2674. {
  2675. char tQBuf[CTEXT];
  2676. AAssoc * result;
  2677. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID);
  2678. #ifdef DEBUG_ALL
  2679. LOG(tQBuf);
  2680. #endif
  2681. result = m_pDataStorage->Query(tQBuf);
  2682. if (result != NULL)
  2683. {
  2684. if (result->size() > 0)
  2685. {
  2686. retVal = (RELAY_PARAMETERS *) malloc(sizeof(RELAY_PARAMETERS));
  2687. if (retVal)
  2688. {
  2689. Assoc * cArr;
  2690. Assoc::iterator it;
  2691. cArr = result->at(0);
  2692. it = cArr->find("dc_auto");
  2693. if (it != cArr->end())
  2694. {
  2695. retVal->bAuto = atoi(it->second.c_str());
  2696. };
  2697. it = cArr->find("dc_no");
  2698. if (it != cArr->end())
  2699. {
  2700. retVal->bNormallyClose = (atoi(it->second.c_str()) == 0);
  2701. };
  2702. it = cArr->find("dc_window");
  2703. if (it != cArr->end())
  2704. {
  2705. retVal->nAutoDelay = atoi(it->second.c_str());
  2706. };
  2707. };
  2708. };
  2709. };
  2710. deleteResult(result);
  2711. };
  2712. return retVal;
  2713. }
  2714. uint8_t sqliteDBC::getLRFDataStatus(uint32_t ID)
  2715. {
  2716. uint8_t RetVal = 0;
  2717. if (m_bStoIsConnected)
  2718. {
  2719. char tQBuf[CTEXT];
  2720. AAssoc * result;
  2721. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID);
  2722. #ifdef DEBUG_ALL
  2723. LOG(tQBuf);
  2724. #endif
  2725. result = m_pDataStorage->Query(tQBuf);
  2726. if (result != NULL)
  2727. {
  2728. if (result->size() > 0)
  2729. {
  2730. Assoc * cArr;
  2731. Assoc::iterator it;
  2732. cArr = result->at(0);
  2733. it = cArr->find("data_status");
  2734. if (it != cArr->end())
  2735. {
  2736. RetVal = atoi(it->second.c_str());
  2737. };
  2738. };
  2739. };
  2740. deleteResult(result);
  2741. };
  2742. return RetVal;
  2743. }
  2744. uint32_t sqliteDBC::getLRFMaskID(uint32_t deviceID, uint8_t lrfID, uint8_t layer)
  2745. {
  2746. uint32_t RetVal = 0;
  2747. if (m_bStoIsConnected)
  2748. {
  2749. char tQBuf[CTEXT];
  2750. AAssoc * result;
  2751. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer);
  2752. #ifdef DEBUG_ALL
  2753. LOG(tQBuf);
  2754. #endif
  2755. result = m_pDataStorage->Query(tQBuf);
  2756. if (result != NULL)
  2757. {
  2758. if (result->size() > 0)
  2759. {
  2760. Assoc * cArr;
  2761. Assoc::iterator it;
  2762. cArr = result->at(0);
  2763. it = cArr->find("id");
  2764. if (it != cArr->end())
  2765. {
  2766. RetVal = atoi(it->second.c_str());
  2767. };
  2768. };
  2769. };
  2770. deleteResult(result);
  2771. };
  2772. return RetVal;
  2773. }
  2774. uint32_t sqliteDBC::getLRFMaskPolygoneID(uint32_t maskID, uint8_t vertecID)
  2775. {
  2776. uint32_t RetVal = 0;
  2777. if (m_bStoIsConnected)
  2778. {
  2779. char tQBuf[CTEXT];
  2780. AAssoc * result;
  2781. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID);
  2782. #ifdef DEBUG_ALL
  2783. LOG(tQBuf);
  2784. #endif
  2785. result = m_pDataStorage->Query(tQBuf);
  2786. if (result != NULL)
  2787. {
  2788. if (result->size() > 0)
  2789. {
  2790. Assoc * cArr;
  2791. Assoc::iterator it;
  2792. cArr = result->at(0);
  2793. it = cArr->find("id");
  2794. if (it != cArr->end())
  2795. {
  2796. RetVal = atoi(it->second.c_str());
  2797. };
  2798. };
  2799. };
  2800. deleteResult(result);
  2801. };
  2802. return RetVal;
  2803. }
  2804. uint8_t sqliteDBC::getLRFMasksStatus(uint32_t deviceID)
  2805. {
  2806. uint8_t RetVal = 0;
  2807. if (m_bStoIsConnected)
  2808. {
  2809. char tQBuf[CTEXT];
  2810. AAssoc * result;
  2811. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID);
  2812. #ifdef DEBUG_ALL
  2813. LOG(tQBuf);
  2814. #endif
  2815. result = m_pDataStorage->Query(tQBuf);
  2816. if (result != NULL)
  2817. {
  2818. if (result->size() > 0)
  2819. {
  2820. Assoc * cArr;
  2821. Assoc::iterator it;
  2822. for (size_t i = 0; i < result->size(); i++)
  2823. {
  2824. cArr = result->at(i);
  2825. it = cArr->find("layer");
  2826. if (it != cArr->end())
  2827. {
  2828. RetVal |= (1 << atoi(it->second.c_str()));
  2829. };
  2830. };
  2831. };
  2832. };
  2833. deleteResult(result);
  2834. };
  2835. return RetVal;
  2836. }
  2837. DBC_LRF_UPLOAD_DATA * sqliteDBC::getLRFUploadData(uint32_t upID)
  2838. {
  2839. DBC_LRF_UPLOAD_DATA * RetVal = NULL;
  2840. if (m_bStoIsConnected)
  2841. {
  2842. char tQBuf[CTEXT];
  2843. AAssoc * result;
  2844. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID);
  2845. #ifdef DEBUG_ALL
  2846. LOG(tQBuf);
  2847. #endif
  2848. result = m_pDataStorage->Query(tQBuf);
  2849. if (result->size() > 0)
  2850. {
  2851. Assoc * cArr;
  2852. RetVal = (DBC_LRF_UPLOAD_DATA *) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2853. if (RetVal != NULL)
  2854. {
  2855. memset(RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2856. Assoc::iterator it;
  2857. RetVal->id = upID;
  2858. cArr = result->at(0);
  2859. it = cArr->find("md5");
  2860. if (it != cArr->end())
  2861. {
  2862. strncpy(RetVal->md5, it->second.c_str(), 32);
  2863. };
  2864. it = cArr->find("file");
  2865. if (it != cArr->end())
  2866. {
  2867. strncpy(RetVal->fname, it->second.c_str(), 16);
  2868. };
  2869. it = cArr->find("issue_reset");
  2870. if (it != cArr->end())
  2871. {
  2872. RetVal->transfer_mode = atoi(it->second.c_str());
  2873. };
  2874. };
  2875. };
  2876. deleteResult(result);
  2877. };
  2878. return RetVal;
  2879. }
  2880. DBC_WIDTH_TABLE * sqliteDBC::getLRFWidthTable(uint32_t lrfID)
  2881. {
  2882. DBC_WIDTH_TABLE * retVal = NULL;
  2883. if (m_bStoIsConnected)
  2884. {
  2885. retVal = (DBC_WIDTH_TABLE *) malloc(sizeof(DBC_WIDTH_TABLE));
  2886. if (retVal)
  2887. {
  2888. char tQBuf[CTEXT];
  2889. AAssoc * result;
  2890. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID);
  2891. #ifdef DEBUG_ALL
  2892. LOG(tQBuf);
  2893. #endif
  2894. memset(retVal, 0, sizeof(DBC_WIDTH_TABLE));
  2895. result = m_pDataStorage->Query(tQBuf);
  2896. if (result != NULL)
  2897. {
  2898. if (result->size() > 0)
  2899. {
  2900. retVal->count = result->size();
  2901. retVal->wt_records = (DBC_WT_RECORD *) calloc(retVal->count, sizeof(DBC_WT_RECORD));
  2902. if (retVal->wt_records)
  2903. {
  2904. Assoc * cArr;
  2905. Assoc::iterator it;
  2906. for (int i = 0; i < retVal->count; i++)
  2907. {
  2908. cArr = result->at(i);
  2909. it = cArr->find("range_k");
  2910. if (it != cArr->end())
  2911. {
  2912. retVal->wt_records[i].rangePercent = atoi(it->second.c_str());
  2913. };
  2914. it = cArr->find("width_k");
  2915. if (it != cArr->end())
  2916. {
  2917. retVal->wt_records[i].widthPercent = atoi(it->second.c_str());
  2918. };
  2919. };
  2920. }
  2921. else
  2922. {
  2923. // retVal->count = 0;
  2924. LOG_ERROR("No Memory To create WT Record!");
  2925. free(retVal);
  2926. retVal = NULL;
  2927. };
  2928. };
  2929. };
  2930. deleteResult(result);
  2931. }
  2932. else
  2933. {
  2934. LOG_ERROR("No Memory To create WT!");
  2935. };
  2936. };
  2937. return retVal;
  2938. }
  2939. uint32_t sqliteDBC::getNodeAuxParam(uint32_t NodeID)
  2940. {
  2941. uint32_t retVal = 0;
  2942. if (m_bStoIsConnected)
  2943. {
  2944. char tQBuf[CTEXT];
  2945. AAssoc * result;
  2946. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID);
  2947. #ifdef DEBUG_ALL
  2948. LOG(tQBuf);
  2949. #endif
  2950. result = m_pDataStorage->Query(tQBuf);
  2951. if (result != NULL)
  2952. {
  2953. if (result->size() > 0)
  2954. {
  2955. Assoc * cArr;
  2956. Assoc::iterator it;
  2957. cArr = result->at(0);
  2958. it = cArr->find("aux_parameter");
  2959. if (it != cArr->end())
  2960. {
  2961. retVal = atoi(it->second.c_str());
  2962. };
  2963. };
  2964. };
  2965. deleteResult(result);
  2966. };
  2967. return retVal;
  2968. }
  2969. DBC_NODE_DATA * sqliteDBC::getNodeData(uint32_t ID)
  2970. {
  2971. DBC_NODE_DATA * tVal = NULL;
  2972. if (m_bStoIsConnected)
  2973. {
  2974. char tQBuf[CTEXT];
  2975. AAssoc * result;
  2976. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID);
  2977. #ifdef DEBUG_ALL
  2978. LOG(tQBuf);
  2979. #endif
  2980. result = m_pDataStorage->Query(tQBuf);
  2981. if (result != NULL)
  2982. {
  2983. if (result->size() > 0)
  2984. {
  2985. Assoc * cArr;
  2986. tVal = (DBC_NODE_DATA *) malloc(sizeof(DBC_NODE_DATA));
  2987. if (tVal != NULL)
  2988. {
  2989. memset(tVal, 0, sizeof(DBC_NODE_DATA));
  2990. Assoc::iterator it;
  2991. tVal->node_id = ID;
  2992. cArr = result->at(0);
  2993. it = cArr->find("type");
  2994. if (it != cArr->end())
  2995. {
  2996. tVal->type = /*(NODE_TYPE)*/ atoi(it->second.c_str());
  2997. };
  2998. it = cArr->find("aux_id");
  2999. if (it != cArr->end())
  3000. {
  3001. tVal->aux_id = atoi(it->second.c_str());
  3002. };
  3003. it = cArr->find("aux_parameter");
  3004. if (it != cArr->end())
  3005. {
  3006. tVal->aux_parameter = atoi(it->second.c_str());
  3007. };
  3008. it = cArr->find("online");
  3009. if (it != cArr->end())
  3010. {
  3011. tVal->online = (bool) atoi(it->second.c_str());
  3012. };
  3013. it = cArr->find("enable");
  3014. if (it != cArr->end())
  3015. {
  3016. tVal->enabled = (bool) atoi(it->second.c_str());
  3017. };
  3018. it = cArr->find("name");
  3019. if (it != cArr->end())
  3020. {
  3021. sprintf(tVal->name, it->second.c_str());
  3022. };
  3023. };
  3024. };
  3025. };
  3026. deleteResult(result);
  3027. };
  3028. return tVal;
  3029. }
  3030. uint32_t sqliteDBC::getNodeIdByTypeAndAux(uint8_t type, uint32_t aux_id)
  3031. {
  3032. uint32_t retVal = 0;
  3033. if (m_bStoIsConnected)
  3034. {
  3035. char tQBuf[CTEXT];
  3036. AAssoc * result;
  3037. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  3038. #ifdef DEBUG_ALL
  3039. LOG(tQBuf);
  3040. #endif
  3041. result = m_pDataStorage->Query(tQBuf);
  3042. if (result != NULL)
  3043. {
  3044. if (result->size() > 0)
  3045. {
  3046. Assoc * cArr;
  3047. Assoc::iterator it;
  3048. cArr = result->at(0);
  3049. it = cArr->find("id");
  3050. if (it != cArr->end())
  3051. {
  3052. retVal = atoi(it->second.c_str());
  3053. };
  3054. };
  3055. };
  3056. deleteResult(result);
  3057. };
  3058. return retVal;
  3059. }
  3060. CSTR sqliteDBC::getNodeName(uint32_t NodeID)
  3061. {
  3062. CSTR retVal = NULL;
  3063. if (m_bStoIsConnected)
  3064. {
  3065. char tQBuf[CTEXT];
  3066. AAssoc * result;
  3067. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID);
  3068. #ifdef DEBUG_ALL
  3069. LOG(tQBuf);
  3070. #endif
  3071. result = m_pDataStorage->Query(tQBuf);
  3072. if (result != NULL)
  3073. {
  3074. if (result->size() > 0)
  3075. {
  3076. Assoc * cArr;
  3077. Assoc::iterator it;
  3078. cArr = result->at(0);
  3079. it = cArr->find("name");
  3080. if (it != cArr->end())
  3081. {
  3082. retVal = strdup(it->second.c_str());
  3083. };
  3084. };
  3085. };
  3086. deleteResult(result);
  3087. };
  3088. return retVal;
  3089. }
  3090. DBC_NET_IF_DATA * sqliteDBC::getNIF_Data(uint32_t nif_id)
  3091. {
  3092. DBC_NET_IF_DATA * tVal = NULL;
  3093. if (m_bStoIsConnected)
  3094. {
  3095. char tQBuf[CTEXT];
  3096. AAssoc * result;
  3097. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id);
  3098. #ifdef DEBUG_ALL
  3099. LOG(tQBuf);
  3100. #endif
  3101. result = m_pDataStorage->Query(tQBuf);
  3102. if (result != NULL)
  3103. {
  3104. if (result->size() > 0)
  3105. {
  3106. Assoc * cArr;
  3107. tVal = (DBC_NET_IF_DATA *) malloc(sizeof(DBC_NET_IF_DATA));
  3108. if (tVal != NULL)
  3109. {
  3110. Assoc::iterator it;
  3111. tVal->id = nif_id;
  3112. cArr = result->at(0);
  3113. it = cArr->find("type");
  3114. if (it != cArr->end())
  3115. {
  3116. tVal->type = atoi(it->second.c_str());
  3117. };
  3118. it = cArr->find("mode");
  3119. if (it != cArr->end())
  3120. {
  3121. tVal->mode = atoi(it->second.c_str());
  3122. };
  3123. it = cArr->find("device_name");
  3124. if (it != cArr->end())
  3125. {
  3126. strncpy(tVal->device_name, it->second.c_str(), 11);
  3127. tVal->device_name[10] = (char) 0;
  3128. };
  3129. it = cArr->find("mac");
  3130. if (it != cArr->end())
  3131. {
  3132. strncpy(tVal->mac, it->second.c_str(), 33);
  3133. tVal->mac[32] = (char) 0;
  3134. };
  3135. it = cArr->find("aux_id");
  3136. if (it != cArr->end())
  3137. {
  3138. tVal->aux_id = atoi(it->second.c_str());
  3139. };
  3140. };
  3141. };
  3142. };
  3143. deleteResult(result);
  3144. };
  3145. return tVal;
  3146. }
  3147. uint32_t sqliteDBC::getNIF_ID(CSTR key)
  3148. {
  3149. uint32_t tVal = 0;
  3150. if (m_bStoIsConnected)
  3151. {
  3152. char tQBuf[CTEXT];
  3153. AAssoc * result;
  3154. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key);
  3155. #ifdef DEBUG_ALL
  3156. LOG(tQBuf);
  3157. #endif
  3158. result = m_pDataStorage->Query(tQBuf);
  3159. if (result != NULL)
  3160. {
  3161. if (result->size() > 0)
  3162. {
  3163. Assoc * cArr;
  3164. Assoc::iterator it;
  3165. cArr = result->at(0);
  3166. it = cArr->find("id");
  3167. if (it != cArr->end())
  3168. {
  3169. tVal = atoi(it->second.c_str());
  3170. };
  3171. };
  3172. };
  3173. deleteResult(result);
  3174. };
  3175. return tVal;
  3176. }
  3177. uint32_t sqliteDBC::getNIF_IDbyDevice(CSTR device)
  3178. {
  3179. uint32_t tVal = 0;
  3180. if (m_bStoIsConnected)
  3181. {
  3182. char tQBuf[CTEXT];
  3183. AAssoc * result;
  3184. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device);
  3185. #ifdef DEBUG_ALL
  3186. LOG(tQBuf);
  3187. #endif
  3188. result = m_pDataStorage->Query(tQBuf);
  3189. if (result != NULL)
  3190. {
  3191. if (result->size() > 0)
  3192. {
  3193. Assoc * cArr;
  3194. Assoc::iterator it;
  3195. cArr = result->at(0);
  3196. it = cArr->find("id");
  3197. if (it != cArr->end())
  3198. {
  3199. tVal = atoi(it->second.c_str());
  3200. };
  3201. };
  3202. };
  3203. deleteResult(result);
  3204. };
  3205. return tVal;
  3206. }
  3207. DBC_NETWORK_DATA * sqliteDBC::getNetworkData(uint32_t net_id)
  3208. {
  3209. DBC_NETWORK_DATA * tVal = NULL;
  3210. if (m_bStoIsConnected)
  3211. {
  3212. char tQBuf[CTEXT];
  3213. AAssoc * result;
  3214. sprintf(tQBuf, "SELECT * FROM `networks` WHERE `id`=%d", net_id);
  3215. #ifdef DEBUG
  3216. LOG(tQBuf);
  3217. #endif
  3218. result = m_pDataStorage->Query(tQBuf);
  3219. if (result != NULL)
  3220. {
  3221. if (result->size() > 0)
  3222. {
  3223. tVal = (DBC_NETWORK_DATA *) malloc(sizeof(DBC_NETWORK_DATA));
  3224. if (tVal)
  3225. {
  3226. Assoc * cArr;
  3227. Assoc::iterator it;
  3228. memset(tVal, 0, sizeof(DBC_NETWORK_DATA));
  3229. tVal->id = net_id;
  3230. cArr = result->at(0);
  3231. it = cArr->find("nif_id");
  3232. if (it != cArr->end())
  3233. {
  3234. tVal->nif_id = atoi(it->second.c_str());
  3235. };
  3236. it = cArr->find("type");
  3237. if (it != cArr->end())
  3238. {
  3239. tVal->type = (NETWORK_TYPE) atoi(it->second.c_str());
  3240. };
  3241. it = cArr->find("address");
  3242. if (it != cArr->end())
  3243. {
  3244. strncpy(tVal->address, it->second.c_str(), 64);
  3245. tVal->address[64] = (char) 0;
  3246. };
  3247. it = cArr->find("mask");
  3248. if (it != cArr->end())
  3249. {
  3250. strncpy(tVal->netmask, it->second.c_str(), 64);
  3251. tVal->netmask[64] = (char) 0;
  3252. };
  3253. it = cArr->find("broadcast");
  3254. if (it != cArr->end())
  3255. {
  3256. strncpy(tVal->broadcast, it->second.c_str(), 64);
  3257. tVal->broadcast[64] = (char) 0;
  3258. };
  3259. it = cArr->find("gateway");
  3260. if (it != cArr->end())
  3261. {
  3262. strncpy(tVal->gateway, it->second.c_str(), 64);
  3263. tVal->gateway[64] = (char) 0;
  3264. };
  3265. };
  3266. };
  3267. };
  3268. deleteResult(result);
  3269. };
  3270. return tVal;
  3271. }
  3272. uint32_t sqliteDBC::getNetworkInfrastructureID(uint32_t nif_id)
  3273. {
  3274. uint32_t tVal = 0;
  3275. if (m_bStoIsConnected)
  3276. {
  3277. char tQBuf[CTEXT];
  3278. AAssoc * result;
  3279. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id);
  3280. #ifdef DEBUG_ALL
  3281. LOG(tQBuf);
  3282. #endif
  3283. result = m_pDataStorage->Query(tQBuf);
  3284. if (result != NULL)
  3285. {
  3286. if (result->size() > 0)
  3287. {
  3288. Assoc * cArr;
  3289. Assoc::iterator it;
  3290. cArr = result->at(0);
  3291. it = cArr->find("id");
  3292. if (it != cArr->end())
  3293. {
  3294. tVal = atoi(it->second.c_str());
  3295. };
  3296. };
  3297. };
  3298. deleteResult(result);
  3299. };
  3300. return tVal;
  3301. }
  3302. TARGET_SEGMENT * sqliteDBC::getRegisteredBubble(uint32_t ID)
  3303. {
  3304. TARGET_SEGMENT * tVal = NULL;
  3305. if (m_bStoIsConnected)
  3306. {
  3307. char tQBuf[CTEXT];
  3308. AAssoc * result;
  3309. sprintf(tQBuf, "SELECT * FROM `target_bubbles` WHERE `id`=%d", ID);
  3310. #ifdef DEBUG_ALL
  3311. LOG(tQBuf);
  3312. #endif
  3313. result = m_pDataStorage->Query(tQBuf);
  3314. if (result != NULL)
  3315. {
  3316. if (result->size() > 0)
  3317. {
  3318. tVal = (TARGET_SEGMENT *) malloc(sizeof(TARGET_SEGMENT));
  3319. if (tVal)
  3320. {
  3321. Assoc * cArr;
  3322. Assoc::iterator it;
  3323. memset(tVal, 0, sizeof(TARGET_SEGMENT));
  3324. tVal->db_id = ID;
  3325. cArr = result->at(0);
  3326. it = cArr->find("CENTER_X");
  3327. if (it != cArr->end())
  3328. {
  3329. tVal->system_center.x = atof(it->second.c_str());
  3330. };
  3331. it = cArr->find("CENTER_Y");
  3332. if (it != cArr->end())
  3333. {
  3334. tVal->system_center.y = atof(it->second.c_str());
  3335. };
  3336. it = cArr->find("CENTER_Z");
  3337. if (it != cArr->end())
  3338. {
  3339. tVal->system_center.z = atof(it->second.c_str());
  3340. };
  3341. it = cArr->find("BOUNDARY");
  3342. if (it != cArr->end())
  3343. {
  3344. tVal->boundary = atof(it->second.c_str());
  3345. };
  3346. it = cArr->find("BLOCK_COUNS");
  3347. if (it != cArr->end())
  3348. {
  3349. tVal->hit_count = atoi(it->second.c_str());
  3350. };
  3351. };
  3352. };
  3353. };
  3354. deleteResult(result);
  3355. };
  3356. return tVal;
  3357. }
  3358. REPORT_DIRINFO * sqliteDBC::getReportDirInfo(uint32_t id)
  3359. {
  3360. REPORT_DIRINFO * tVal = NULL;
  3361. if( !m_bStaIsCreated && m_bStaIsConnected ){
  3362. char tQBuf[CTEXT];
  3363. AAssoc * result;
  3364. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id);
  3365. #ifdef DEBUG_ALL
  3366. LOG(tQBuf);
  3367. #endif
  3368. result = m_pDataStatistics->Query(tQBuf);
  3369. //result = m_pDataStorage->Query(tQBuf);
  3370. if (result != NULL)
  3371. {
  3372. if (result->size() > 0)
  3373. {
  3374. tVal = (REPORT_DIRINFO *) malloc(sizeof(REPORT_DIRINFO));
  3375. if (tVal)
  3376. {
  3377. Assoc * cArr;
  3378. Assoc::iterator it;
  3379. memset(tVal, 0, sizeof(REPORT_DIRINFO));
  3380. cArr = result->at(0);
  3381. it = cArr->find("year");
  3382. if (it != cArr->end())
  3383. {
  3384. strncpy(tVal->YEAR, it->second.c_str(), 5);
  3385. };
  3386. it = cArr->find("month");
  3387. if (it != cArr->end())
  3388. {
  3389. strncpy(tVal->MONTH, it->second.c_str(), 3);
  3390. };
  3391. it = cArr->find("day");
  3392. if (it != cArr->end())
  3393. {
  3394. strncpy(tVal->DAY, it->second.c_str(), 3);
  3395. };
  3396. };
  3397. };
  3398. };
  3399. deleteResult(result);
  3400. };
  3401. return tVal;
  3402. }
  3403. DBC_RESOURCE_DATA * sqliteDBC::getResourceData(uint32_t ResourceID)
  3404. {
  3405. DBC_RESOURCE_DATA * tVal = NULL;
  3406. if (m_bStoIsConnected)
  3407. {
  3408. char tQBuf[CTEXT];
  3409. AAssoc * result;
  3410. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID);
  3411. #ifdef DEBUG_ALL
  3412. LOG(tQBuf);
  3413. #endif
  3414. result = m_pDataStorage->Query(tQBuf);
  3415. if (result != NULL)
  3416. {
  3417. if (result->size() > 0)
  3418. {
  3419. Assoc * cArr;
  3420. /// @todo Valgrind tells us that tVal is lost and never freed
  3421. tVal = (DBC_RESOURCE_DATA *) malloc(sizeof(DBC_RESOURCE_DATA));
  3422. if (tVal != NULL)
  3423. {
  3424. memset(tVal, 0, sizeof(DBC_RESOURCE_DATA));
  3425. Assoc::iterator it;
  3426. cArr = result->at(0);
  3427. tVal->id = ResourceID;
  3428. it = cArr->find("node_id");
  3429. if (it != cArr->end())
  3430. {
  3431. tVal->node_id = atoi(it->second.c_str());
  3432. };
  3433. it = cArr->find("type");
  3434. if (it != cArr->end())
  3435. {
  3436. tVal->type = atoi(it->second.c_str());
  3437. };
  3438. it = cArr->find("aux_id");
  3439. if (it != cArr->end())
  3440. {
  3441. tVal->aux_id = atoi(it->second.c_str());
  3442. };
  3443. it = cArr->find("device");
  3444. if (it != cArr->end())
  3445. {
  3446. strncpy(tVal->device, it->second.c_str(), 64);
  3447. tVal->device[63] = (char) 0;
  3448. };
  3449. };
  3450. };
  3451. };
  3452. deleteResult(result);
  3453. };
  3454. return tVal;
  3455. }
  3456. uint32_t sqliteDBC::getResourceID(uint32_t NodeID, uint8_t type, uint32_t auxID)
  3457. {
  3458. uint32_t RetVal = 0;
  3459. if (m_bStoIsConnected)
  3460. {
  3461. char tQBuf[CTEXT];
  3462. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID);
  3463. #ifdef DEBUG_ALL
  3464. LOG(tQBuf);
  3465. #endif
  3466. AAssoc * result;
  3467. result = m_pDataStorage->Query(tQBuf);
  3468. if (result != NULL)
  3469. {
  3470. if (result->size() > 0)
  3471. {
  3472. Assoc * cArr;
  3473. Assoc::iterator it;
  3474. cArr = result->at(0);
  3475. it = cArr->find("id");
  3476. if (it != cArr->end())
  3477. {
  3478. RetVal = atoi(it->second.c_str());
  3479. };
  3480. };
  3481. };
  3482. deleteResult(result);
  3483. };
  3484. return RetVal;
  3485. }
  3486. DBC_SITE_DATA * sqliteDBC::getSiteData(uint32_t ID)
  3487. {
  3488. /** @todo Valgrind is reporting an important memory leak in this method.
  3489. * See these details:
  3490. *
  3491. * ~~~~
  3492. ==00:00:04:04.218 26021== 28,304 bytes in 122 blocks are definitely lost in loss record 3,966 of 3,972
  3493. ==00:00:04:04.218 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  3494. ==00:00:04:04.218 26021== by 0x23452A: sqliteDBC::getSiteData(unsigned int) (SQLite_dataBinder.cpp:2981)
  3495. ==00:00:04:04.218 26021== by 0x1DA681: mojoKernel::validateReports() (mojoKernel.cpp:1347)
  3496. ==00:00:04:04.218 26021== by 0x1D7D16: mojoKernel::m_fThreadProc() (mojoKernel.cpp:511)
  3497. ==00:00:04:04.218 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  3498. ==00:00:04:04.218 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  3499. ==00:00:04:04.218 26021== by 0x7874ADE: clone (clone.S:95)
  3500. * ~~~~
  3501. */
  3502. DBC_SITE_DATA * tVal = NULL;
  3503. if (m_bStoIsConnected)
  3504. {
  3505. char tQBuf[CTEXT];
  3506. AAssoc * result;
  3507. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  3508. #ifdef DEBUG_ALL
  3509. LOG(tQBuf);
  3510. #endif
  3511. result = m_pDataStorage->Query(tQBuf);
  3512. if (result != NULL)
  3513. {
  3514. if (result->size() > 0)
  3515. {
  3516. Assoc * cArr;
  3517. tVal = (DBC_SITE_DATA *) malloc(sizeof(DBC_SITE_DATA));
  3518. if (tVal != NULL)
  3519. {
  3520. memset(tVal, 0, sizeof(DBC_SITE_DATA));
  3521. Assoc::iterator it;
  3522. cArr = result->at(0);
  3523. it = cArr->find("type");
  3524. if (it != cArr->end())
  3525. {
  3526. tVal->account_id = atoi(it->second.c_str());
  3527. };
  3528. it = cArr->find("name");
  3529. if (it != cArr->end())
  3530. {
  3531. sprintf(tVal->name, it->second.c_str());
  3532. };
  3533. // it = cArr->find("server_ident");
  3534. // if( it != cArr->end() ){
  3535. // sprintf( tVal->ident, it->second.c_str() );
  3536. // };
  3537. it = cArr->find("anchor_lat");
  3538. if (it != cArr->end())
  3539. {
  3540. tVal->lat = atof(it->second.c_str());
  3541. };
  3542. it = cArr->find("anchor_lon");
  3543. if (it != cArr->end())
  3544. {
  3545. tVal->lon = atof(it->second.c_str());
  3546. };
  3547. it = cArr->find("anchor_alt");
  3548. if (it != cArr->end())
  3549. {
  3550. tVal->alt = atof(it->second.c_str());
  3551. };
  3552. it = cArr->find("hemisphere");
  3553. if (it != cArr->end())
  3554. {
  3555. tVal->SH = (it->second.c_str()[0] == 'S');
  3556. };
  3557. it = cArr->find("map_id");
  3558. if (it != cArr->end())
  3559. {
  3560. tVal->map_id = atoi(it->second.c_str());
  3561. };
  3562. it = cArr->find("local_binding");
  3563. if (it != cArr->end())
  3564. {
  3565. tVal->local_binding = (bool) atoi(it->second.c_str());
  3566. };
  3567. it = cArr->find("work_mode");
  3568. if (it != cArr->end())
  3569. {
  3570. tVal->work_mode = atoi(it->second.c_str());
  3571. };
  3572. it = cArr->find("report_cleanup");
  3573. if (it != cArr->end())
  3574. {
  3575. tVal->report_cleanup = atoi(it->second.c_str());
  3576. };
  3577. it = cArr->find("preserve_empty");
  3578. if (it != cArr->end())
  3579. {
  3580. tVal->preserve_empty = atoi(it->second.c_str());
  3581. };
  3582. it = cArr->find("remote_ip");
  3583. if (it != cArr->end())
  3584. {
  3585. sprintf(tVal->remote_ip, it->second.c_str());
  3586. };
  3587. it = cArr->find("remote_port");
  3588. if (it != cArr->end())
  3589. {
  3590. tVal->remote_port = atoi(it->second.c_str());
  3591. };
  3592. it = cArr->find("remote_backup_ip");
  3593. if (it != cArr->end())
  3594. {
  3595. sprintf(tVal->remote_backup_ip, it->second.c_str());
  3596. };
  3597. it = cArr->find("remote_backup_port");
  3598. if (it != cArr->end())
  3599. {
  3600. tVal->remote_port = atoi(it->second.c_str());
  3601. };
  3602. it = cArr->find("use_remote_server");
  3603. if (it != cArr->end())
  3604. {
  3605. tVal->use_remote_server = atoi(it->second.c_str());
  3606. };
  3607. it = cArr->find("skip_local_report");
  3608. if (it != cArr->end())
  3609. {
  3610. tVal->skip_local_report = atoi(it->second.c_str());
  3611. };
  3612. };
  3613. };
  3614. };
  3615. deleteResult(result);
  3616. };
  3617. return tVal;
  3618. }
  3619. uint32_t sqliteDBC::getServerID(CSTR identity)
  3620. {
  3621. uint32_t RetVal = 0;
  3622. if (m_bStoIsConnected)
  3623. {
  3624. char tQBuf[CTEXT];
  3625. sprintf(tQBuf, "SELECT `id` FROM `servers` WHERE `identity`='%s'", identity);
  3626. #ifdef DEBUG_ALL
  3627. LOG(tQBuf);
  3628. #endif
  3629. AAssoc * result;
  3630. result = m_pDataStorage->Query(tQBuf);
  3631. if (result != NULL)
  3632. {
  3633. if (result->size() > 0)
  3634. {
  3635. Assoc * cArr;
  3636. Assoc::iterator it;
  3637. cArr = result->at(0);
  3638. it = cArr->find("id");
  3639. if (it != cArr->end())
  3640. {
  3641. RetVal = atoi(it->second.c_str());
  3642. };
  3643. };
  3644. };
  3645. deleteResult(result);
  3646. };
  3647. return RetVal;
  3648. }
  3649. uint32_t sqliteDBC::getServerNode(uint32_t SID)
  3650. {
  3651. uint32_t RetVal = 0;
  3652. if (m_bStoIsConnected)
  3653. {
  3654. char tQBuf[CTEXT];
  3655. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID);
  3656. #ifdef DEBUG_ALL
  3657. LOG(tQBuf);
  3658. #endif
  3659. AAssoc * result;
  3660. result = m_pDataStorage->Query(tQBuf);
  3661. if (result != NULL)
  3662. {
  3663. if (result->size() > 0)
  3664. {
  3665. Assoc * cArr;
  3666. Assoc::iterator it;
  3667. cArr = result->at(0);
  3668. it = cArr->find("id");
  3669. if (it != cArr->end())
  3670. {
  3671. RetVal = atoi(it->second.c_str());
  3672. };
  3673. };
  3674. };
  3675. deleteResult(result);
  3676. };
  3677. return RetVal;
  3678. }
  3679. DBC_TARGET_FLITER_DATA * sqliteDBC::getTargetFilterData(uint32_t ID)
  3680. {
  3681. DBC_TARGET_FLITER_DATA * tVal = NULL;
  3682. if (m_bStoIsConnected)
  3683. {
  3684. char tQBuf[CTEXT];
  3685. AAssoc * result;
  3686. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID);
  3687. #ifdef DEBUG_ALL
  3688. LOG(tQBuf);
  3689. #endif
  3690. result = m_pDataStorage->Query(tQBuf);
  3691. if (result != NULL)
  3692. {
  3693. if (result->size() > 0)
  3694. {
  3695. Assoc * cArr;
  3696. tVal = (DBC_TARGET_FLITER_DATA *) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  3697. if (tVal != NULL)
  3698. {
  3699. memset(tVal, 0, sizeof(DBC_TARGET_FLITER_DATA));
  3700. Assoc::iterator it;
  3701. tVal->id = ID;
  3702. cArr = result->at(0);
  3703. it = cArr->find("type");
  3704. if (it != cArr->end())
  3705. {
  3706. tVal->type = (TARGET_FILTER_TYPE) atoi(it->second.c_str());
  3707. };
  3708. it = cArr->find("aux_id");
  3709. if (it != cArr->end())
  3710. {
  3711. tVal->aux_id = atoi(it->second.c_str());
  3712. };
  3713. it = cArr->find("enabled");
  3714. if (it != cArr->end())
  3715. {
  3716. tVal->enabled = (bool) atoi(it->second.c_str());
  3717. };
  3718. };
  3719. };
  3720. };
  3721. deleteResult(result);
  3722. };
  3723. return tVal;
  3724. }
  3725. TRAIN_FILTER_DATA * sqliteDBC::getTrainFilterData(uint32_t ID)
  3726. {
  3727. TRAIN_FILTER_DATA * tVal = NULL;
  3728. if (m_bStoIsConnected)
  3729. {
  3730. char tQBuf[CTEXT];
  3731. AAssoc * result;
  3732. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID);
  3733. #ifdef DEBUG_ALL
  3734. LOG(tQBuf);
  3735. #endif
  3736. result = m_pDataStorage->Query(tQBuf);
  3737. if (result != NULL)
  3738. {
  3739. if (result->size() > 0)
  3740. {
  3741. Assoc * cArr;
  3742. tVal = (TRAIN_FILTER_DATA *) malloc(sizeof(TRAIN_FILTER_DATA));
  3743. if (tVal != NULL)
  3744. {
  3745. memset(tVal, 0, sizeof(TRAIN_FILTER_DATA));
  3746. Assoc::iterator it;
  3747. cArr = result->at(0);
  3748. it = cArr->find("UPPER_SENSOR");
  3749. if (it != cArr->end())
  3750. {
  3751. tVal->nUpperSensorID = atoi(it->second.c_str());
  3752. };
  3753. it = cArr->find("LOWER_SENSOR");
  3754. if (it != cArr->end())
  3755. {
  3756. tVal->nBottomSensorID = atoi(it->second.c_str());
  3757. };
  3758. it = cArr->find("TRAIN_DETECT");
  3759. if (it != cArr->end())
  3760. {
  3761. tVal->bDetectTrain = (bool) atoi(it->second.c_str());
  3762. };
  3763. it = cArr->find("TRAIN_WIDTH");
  3764. if (it != cArr->end())
  3765. {
  3766. tVal->nTrainWidth = atoi(it->second.c_str());
  3767. };
  3768. it = cArr->find("DECISION_TIME");
  3769. if (it != cArr->end())
  3770. {
  3771. tVal->nDecisionTime = 1000 * atoi(it->second.c_str());
  3772. };
  3773. // In case Wrong Parameter (less then Second)
  3774. if (tVal->nDecisionTime < 1000)
  3775. {
  3776. tVal->nDecisionTime = 10000;
  3777. };
  3778. it = cArr->find("SUSPECT_TIME");
  3779. if (it != cArr->end())
  3780. {
  3781. tVal->nSuspectTime = 1000 * atoi(it->second.c_str());
  3782. };
  3783. // In case Wrong Parameter (less then Second)
  3784. if (tVal->nSuspectTime < 1000)
  3785. {
  3786. tVal->nSuspectTime = 10000;
  3787. };
  3788. it = cArr->find("TRACK_TRAIN");
  3789. if (it != cArr->end())
  3790. {
  3791. tVal->bTrackTrain = (bool) atoi(it->second.c_str());
  3792. };
  3793. it = cArr->find("AUX_IP");
  3794. if (it != cArr->end())
  3795. {
  3796. strncpy(tVal->aux_ip, it->second.c_str(), 15);
  3797. };
  3798. it = cArr->find("AUX_PORT");
  3799. if (it != cArr->end())
  3800. {
  3801. tVal->aux_port = atoi(it->second.c_str());
  3802. };
  3803. #ifdef EFT_AUTO_RESET_UPPER_SENSOR
  3804. it = cArr->find("UPPER_RESTART");
  3805. if (it != cArr->end())
  3806. {
  3807. tVal->nUpperSensorRestartTO = atoi(it->second.c_str()) * 3600; // Hours -> Seconds
  3808. }
  3809. else
  3810. {
  3811. tVal->nUpperSensorRestartTO = 24 * 3600;
  3812. };
  3813. #endif
  3814. #ifdef EFT_MINIMAL_RANGE_SUSPICIOUS
  3815. it = cArr->find("MINIMAL_LEFT_RANGE");
  3816. if (it != cArr->end())
  3817. {
  3818. tVal->fLeftMinimalRange = atof(it->second.c_str());
  3819. }
  3820. else
  3821. {
  3822. tVal->fLeftMinimalRange = 0.0;
  3823. };
  3824. it = cArr->find("MINIMAL_RIGHT_RANGE");
  3825. if (it != cArr->end())
  3826. {
  3827. tVal->fRightMinimalRange = atof(it->second.c_str());
  3828. }
  3829. else
  3830. {
  3831. tVal->fRightMinimalRange = 0.0;
  3832. };
  3833. #endif
  3834. it = cArr->find("DEBUG");
  3835. if (it != cArr->end())
  3836. {
  3837. tVal->bDebugMode = (bool) atoi(it->second.c_str());
  3838. };
  3839. };
  3840. };
  3841. };
  3842. deleteResult(result);
  3843. };
  3844. return tVal;
  3845. }
  3846. bool sqliteDBC::cleanRegisteredBubbles(void)
  3847. {
  3848. if (m_bStoIsConnected)
  3849. {
  3850. char tQBuf[MAX_PATH_LEN];
  3851. sprintf(tQBuf, "DELETE FROM `target_bubbles`");
  3852. return m_pDataStorage->SilentQuery(tQBuf);
  3853. };
  3854. return false;
  3855. }
  3856. DBC_UART_DATA * sqliteDBC::getUartData(uint32_t UartID)
  3857. {
  3858. DBC_UART_DATA * tVal = NULL;
  3859. if (m_bStoIsConnected)
  3860. {
  3861. char tQBuf[MAX_PATH_LEN];
  3862. AAssoc * result;
  3863. sprintf(tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID);
  3864. #ifdef DEBUG_ALL
  3865. LOG(tQBuf);
  3866. #endif
  3867. result = m_pDataStorage->Query(tQBuf);
  3868. if (result != NULL)
  3869. {
  3870. if (result->size() > 0)
  3871. {
  3872. Assoc * cArr;
  3873. tVal = (DBC_UART_DATA *) malloc(sizeof(DBC_UART_DATA));
  3874. if (tVal != NULL)
  3875. {
  3876. Assoc::iterator it;
  3877. cArr = result->at(0);
  3878. it = cArr->find("type");
  3879. if (it != cArr->end())
  3880. {
  3881. tVal->type = atoi(it->second.c_str());
  3882. };
  3883. it = cArr->find("speed");
  3884. if (it != cArr->end())
  3885. {
  3886. tVal->speed = atoi(it->second.c_str());
  3887. };
  3888. it = cArr->find("mode");
  3889. if (it != cArr->end())
  3890. {
  3891. tVal->mode = atoi(it->second.c_str());
  3892. };
  3893. it = cArr->find("port");
  3894. if (it != cArr->end())
  3895. {
  3896. tVal->port = atoi(it->second.c_str());
  3897. };
  3898. it = cArr->find("path");
  3899. if (it != cArr->end())
  3900. {
  3901. strncpy(tVal->path, it->second.c_str(), 128);
  3902. };
  3903. };
  3904. };
  3905. };
  3906. deleteResult(result);
  3907. };
  3908. return tVal;
  3909. }
  3910. uint32_t sqliteDBC::getUartID(uint32_t NodeID, uint8_t Type, CSTR path)
  3911. {
  3912. uint32_t RetVal = 0;
  3913. if (m_bStoIsConnected)
  3914. {
  3915. char tQBuf[CTEXT];
  3916. sprintf(tQBuf, "SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path);
  3917. #ifdef DEBUG_ALL
  3918. LOG(tQBuf);
  3919. #endif
  3920. AAssoc * result;
  3921. result = m_pDataStorage->Query(tQBuf);
  3922. if (result != NULL)
  3923. {
  3924. if (result->size() > 0)
  3925. {
  3926. Assoc * cArr;
  3927. Assoc::iterator it;
  3928. cArr = result->at(0);
  3929. it = cArr->find("id");
  3930. if (it != cArr->end())
  3931. {
  3932. RetVal = atoi(it->second.c_str());
  3933. };
  3934. };
  3935. };
  3936. deleteResult(result);
  3937. };
  3938. return RetVal;
  3939. }
  3940. uint32_t sqliteDBC::getWiFi_ID(uint32_t nif_id)
  3941. {
  3942. uint32_t tVal = 0;
  3943. if (m_bStoIsConnected)
  3944. {
  3945. char tQBuf[CTEXT];
  3946. AAssoc * result;
  3947. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id);
  3948. #ifdef DEBUG_ALL
  3949. LOG(tQBuf);
  3950. #endif
  3951. result = m_pDataStorage->Query(tQBuf);
  3952. if (result != NULL)
  3953. {
  3954. if (result->size() > 0)
  3955. {
  3956. Assoc * cArr;
  3957. Assoc::iterator it;
  3958. cArr = result->at(0);
  3959. it = cArr->find("id");
  3960. if (it != cArr->end())
  3961. {
  3962. tVal = atoi(it->second.c_str());
  3963. };
  3964. };
  3965. };
  3966. deleteResult(result);
  3967. };
  3968. return tVal;
  3969. }
  3970. DBC_INT_LIST * sqliteDBC::listCameras()
  3971. {
  3972. DBC_INT_LIST * tVal = NULL;
  3973. if (m_bStoIsConnected)
  3974. {
  3975. char tQBuf[CTEXT];
  3976. AAssoc * result;
  3977. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `status`=3");
  3978. #ifdef DEBUG_ALL
  3979. LOG(tQBuf);
  3980. #endif
  3981. result = m_pDataStorage->Query(tQBuf);
  3982. if (result != NULL)
  3983. {
  3984. if (result->size() > 0)
  3985. {
  3986. Assoc * cArr;
  3987. tVal = new DBC_INT_LIST();
  3988. if (tVal != NULL)
  3989. {
  3990. Assoc::iterator it;
  3991. for (size_t i = 0; i < result->size(); i++)
  3992. {
  3993. cArr = result->at(i);
  3994. it = cArr->find("id");
  3995. if (it != cArr->end())
  3996. {
  3997. tVal->push_back(atoi(it->second.c_str()));
  3998. };
  3999. };
  4000. };
  4001. };
  4002. };
  4003. deleteResult(result);
  4004. };
  4005. return tVal;
  4006. }
  4007. DBC_INT_LIST * sqliteDBC::listCamPresets(uint32_t SETID)
  4008. {
  4009. DBC_INT_LIST * tVal = NULL;
  4010. if (m_bStoIsConnected)
  4011. {
  4012. char tQBuf[CTEXT];
  4013. AAssoc * result;
  4014. sprintf(tQBuf, "SELECT `id` FROM `cameras_presets` WHERE `preset_id`=%d", SETID);
  4015. #ifdef DEBUG_ALL
  4016. LOG(tQBuf);
  4017. #endif
  4018. result = m_pDataStorage->Query(tQBuf);
  4019. if (result != NULL)
  4020. {
  4021. if (result->size() > 0)
  4022. {
  4023. Assoc * cArr;
  4024. tVal = new DBC_INT_LIST();
  4025. if (tVal != NULL)
  4026. {
  4027. Assoc::iterator it;
  4028. for (size_t i = 0; i < result->size(); i++)
  4029. {
  4030. cArr = result->at(i);
  4031. it = cArr->find("id");
  4032. if (it != cArr->end())
  4033. {
  4034. tVal->push_back(atoi(it->second.c_str()));
  4035. };
  4036. };
  4037. };
  4038. };
  4039. };
  4040. deleteResult(result);
  4041. };
  4042. return tVal;
  4043. }
  4044. DBC_INT_LIST * sqliteDBC::listFilters(uint32_t ServerID)
  4045. {
  4046. DBC_INT_LIST * tVal = NULL;
  4047. if (ServerID) ServerID = ServerID;
  4048. if (m_bStoIsConnected)
  4049. {
  4050. char tQBuf[CTEXT];
  4051. AAssoc * result;
  4052. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1");
  4053. #ifdef DEBUG_ALL
  4054. LOG(tQBuf);
  4055. #endif
  4056. result = m_pDataStorage->Query(tQBuf);
  4057. if (result != NULL)
  4058. {
  4059. if (result->size() > 0)
  4060. {
  4061. Assoc * cArr;
  4062. tVal = new DBC_INT_LIST();
  4063. if (tVal != NULL)
  4064. {
  4065. Assoc::iterator it;
  4066. for (size_t i = 0; i < result->size(); i++)
  4067. {
  4068. cArr = result->at(i);
  4069. it = cArr->find("id");
  4070. if (it != cArr->end())
  4071. {
  4072. tVal->push_back(atoi(it->second.c_str()));
  4073. };
  4074. };
  4075. };
  4076. };
  4077. };
  4078. deleteResult(result);
  4079. };
  4080. return tVal;
  4081. }
  4082. DBC_INT_LIST * sqliteDBC::listLRFSensors()
  4083. {
  4084. DBC_INT_LIST * tVal = NULL;
  4085. if (m_bStoIsConnected)
  4086. {
  4087. char tQBuf[CTEXT];
  4088. AAssoc * result;
  4089. sprintf(tQBuf, "SELECT `id` FROM `lrf_sensors` WHERE `data_status`>0");
  4090. #ifdef DEBUG_ALL
  4091. LOG(tQBuf);
  4092. #endif
  4093. result = m_pDataStorage->Query(tQBuf);
  4094. if (result != NULL)
  4095. {
  4096. if (result->size() > 0)
  4097. {
  4098. Assoc * cArr;
  4099. tVal = new DBC_INT_LIST();
  4100. if (tVal != NULL)
  4101. {
  4102. Assoc::iterator it;
  4103. for (size_t i = 0; i < result->size(); i++)
  4104. {
  4105. cArr = result->at(i);
  4106. it = cArr->find("id");
  4107. if (it != cArr->end())
  4108. {
  4109. tVal->push_back(atoi(it->second.c_str()));
  4110. };
  4111. };
  4112. };
  4113. };
  4114. };
  4115. deleteResult(result);
  4116. };
  4117. return tVal;
  4118. }
  4119. DBC_INT_LIST * sqliteDBC::listLRFUploads(uint32_t deviceID)
  4120. {
  4121. DBC_INT_LIST * tVal = NULL;
  4122. if (m_bStoIsConnected)
  4123. {
  4124. char tQBuf[CTEXT];
  4125. AAssoc * result;
  4126. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID);
  4127. #ifdef DEBUG_ALL
  4128. LOG(tQBuf);
  4129. #endif
  4130. result = m_pDataStorage->Query(tQBuf);
  4131. if (result != NULL)
  4132. {
  4133. if (result->size() > 0)
  4134. {
  4135. Assoc * cArr;
  4136. tVal = new DBC_INT_LIST();
  4137. if (tVal != NULL)
  4138. {
  4139. Assoc::iterator it;
  4140. for (size_t i = 0; i < result->size(); i++)
  4141. {
  4142. cArr = result->at(i);
  4143. it = cArr->find("id");
  4144. if (it != cArr->end())
  4145. {
  4146. tVal->push_back(atoi(it->second.c_str()));
  4147. };
  4148. };
  4149. };
  4150. };
  4151. };
  4152. deleteResult(result);
  4153. };
  4154. return tVal;
  4155. }
  4156. DBC_INT_LIST * sqliteDBC::listMediaStreams(uint32_t SiteId, uint8_t listMode)
  4157. {
  4158. DBC_INT_LIST * tVal = NULL;
  4159. if ( SiteId ) SiteId = SiteId;
  4160. if (m_bStoIsConnected)
  4161. {
  4162. char tQBuf[CTEXT];
  4163. AAssoc * result;
  4164. switch (listMode)
  4165. {
  4166. case LIST_VALID_RECORDS : sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13,14,15) AND `ms`.`valid`=1 AND `nd`.`online`=1"); break;
  4167. case LIST_INVALID_RECORDS: sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13,14,15) AND `ms`.`valid`=1 AND `nd`.`online`=0"); break;
  4168. default : sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13,14,15) AND `ms`.`valid`=1");
  4169. };
  4170. #ifdef DEBUG_ALL
  4171. LOG(tQBuf);
  4172. #endif
  4173. result = m_pDataStorage->Query(tQBuf);
  4174. if (result != NULL)
  4175. {
  4176. if (result->size() > 0)
  4177. {
  4178. Assoc * cArr;
  4179. tVal = new DBC_INT_LIST();
  4180. if (tVal != NULL)
  4181. {
  4182. Assoc::iterator it;
  4183. for (size_t i = 0; i < result->size(); i++)
  4184. {
  4185. cArr = result->at(i);
  4186. it = cArr->find("id");
  4187. if (it != cArr->end())
  4188. {
  4189. tVal->push_back(atoi(it->second.c_str()));
  4190. };
  4191. };
  4192. };
  4193. };
  4194. };
  4195. deleteResult(result);
  4196. };
  4197. return tVal;
  4198. }
  4199. DBC_INT_LIST * sqliteDBC::listModules()
  4200. {
  4201. DBC_INT_LIST * tVal = NULL;
  4202. if (m_bStoIsConnected)
  4203. {
  4204. AAssoc * result;
  4205. result = m_pDataStorage->Query("SELECT `id` FROM `modules` WHERE `enabled`=1");
  4206. if (result != NULL)
  4207. {
  4208. if (result->size() > 0)
  4209. {
  4210. Assoc * cArr;
  4211. tVal = new DBC_INT_LIST();
  4212. if (tVal != NULL)
  4213. {
  4214. Assoc::iterator it;
  4215. for (size_t i = 0; i < result->size(); i++)
  4216. {
  4217. cArr = result->at(i);
  4218. it = cArr->find("id");
  4219. if (it != cArr->end())
  4220. {
  4221. tVal->push_back(atoi(it->second.c_str()));
  4222. };
  4223. };
  4224. };
  4225. };
  4226. };
  4227. deleteResult(result);
  4228. };
  4229. return tVal;
  4230. }
  4231. DBC_INT_LIST * sqliteDBC::listNetworks(uint32_t nif_id)
  4232. {
  4233. DBC_INT_LIST * tVal = NULL;
  4234. if (m_bStoIsConnected)
  4235. {
  4236. char tQBuf[CTEXT];
  4237. AAssoc * result;
  4238. sprintf(tQBuf, "SELECT `id` FROM `networks` WHERE `nif_id`=%d", nif_id);
  4239. #ifdef DEBUG_ALL
  4240. LOG(tQBuf);
  4241. #endif
  4242. result = m_pDataStorage->Query(tQBuf);
  4243. if (result != NULL)
  4244. {
  4245. if (result->size() > 0)
  4246. {
  4247. Assoc * cArr;
  4248. tVal = new DBC_INT_LIST();
  4249. if (tVal != NULL)
  4250. {
  4251. Assoc::iterator it;
  4252. for (size_t i = 0; i < result->size(); i++)
  4253. {
  4254. cArr = result->at(i);
  4255. it = cArr->find("id");
  4256. if (it != cArr->end())
  4257. {
  4258. tVal->push_back(atoi(it->second.c_str()));
  4259. };
  4260. };
  4261. };
  4262. };
  4263. };
  4264. deleteResult(result);
  4265. };
  4266. return tVal;
  4267. }
  4268. DBC_INT_LIST * sqliteDBC::listNewModules(DBC_INT_LIST * old_ids)
  4269. {
  4270. DBC_INT_LIST * tVal = NULL;
  4271. if (m_bStoIsConnected)
  4272. {
  4273. char tParam[CTEXT];
  4274. // memset( tParam, 0, CTEXT );
  4275. sprintf(tParam, "0");
  4276. if (old_ids != NULL)
  4277. {
  4278. char tBuffer[1024];
  4279. for (size_t i = 0; i < old_ids->size(); i++)
  4280. {
  4281. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4282. strncpy(tParam, tBuffer, CTEXT);
  4283. };
  4284. };
  4285. char tQBuf[2048];
  4286. AAssoc * result;
  4287. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam);
  4288. #ifdef DEBUG_ALL
  4289. LOG(tQBuf);
  4290. #endif
  4291. result = m_pDataStorage->Query(tQBuf);
  4292. if (result != NULL)
  4293. {
  4294. if (result->size() > 0)
  4295. {
  4296. Assoc * cArr;
  4297. tVal = new DBC_INT_LIST();
  4298. if (tVal != NULL)
  4299. {
  4300. Assoc::iterator it;
  4301. for (size_t i = 0; i < result->size(); i++)
  4302. {
  4303. cArr = result->at(i);
  4304. it = cArr->find("id");
  4305. if (it != cArr->end())
  4306. {
  4307. tVal->push_back(atoi(it->second.c_str()));
  4308. };
  4309. };
  4310. };
  4311. };
  4312. };
  4313. deleteResult(result);
  4314. };
  4315. return tVal;
  4316. }
  4317. DBC_INT_LIST * sqliteDBC::listNewNodes(DBC_INT_LIST * old_ids)
  4318. {
  4319. DBC_INT_LIST * tVal = NULL;
  4320. if (m_bStoIsConnected)
  4321. {
  4322. char tParam[CTEXT];
  4323. // memset( tParam, 0, CTEXT );
  4324. sprintf(tParam, "0");
  4325. if (old_ids != NULL)
  4326. {
  4327. char tBuffer[1024];
  4328. for (size_t i = 0; i < old_ids->size(); i++)
  4329. {
  4330. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4331. strncpy(tParam, tBuffer, CTEXT);
  4332. };
  4333. };
  4334. char tQBuf[2048];
  4335. AAssoc * result;
  4336. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam);
  4337. #ifdef DEBUG_ALL
  4338. LOG(tQBuf);
  4339. #endif
  4340. result = m_pDataStorage->Query(tQBuf);
  4341. if (result != NULL)
  4342. {
  4343. if (result->size() > 0)
  4344. {
  4345. Assoc * cArr;
  4346. tVal = new DBC_INT_LIST();
  4347. if (tVal != NULL)
  4348. {
  4349. Assoc::iterator it;
  4350. for (size_t i = 0; i < result->size(); i++)
  4351. {
  4352. cArr = result->at(i);
  4353. it = cArr->find("id");
  4354. if (it != cArr->end())
  4355. {
  4356. tVal->push_back(atoi(it->second.c_str()));
  4357. };
  4358. };
  4359. };
  4360. };
  4361. };
  4362. deleteResult(result);
  4363. };
  4364. return tVal;
  4365. }
  4366. DBC_INT_LIST * sqliteDBC::listNodes(bool all)
  4367. {
  4368. DBC_INT_LIST * tVal = NULL;
  4369. if (m_bStoIsConnected)
  4370. {
  4371. char tQBuf[1024];
  4372. AAssoc * result;
  4373. if (all)
  4374. {
  4375. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  4376. }
  4377. else
  4378. {
  4379. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1");
  4380. };
  4381. #ifdef DEBUG_ALL
  4382. LOG(tQBuf);
  4383. #endif
  4384. result = m_pDataStorage->Query(tQBuf);
  4385. if (result != NULL)
  4386. {
  4387. if (result->size() > 0)
  4388. {
  4389. Assoc * cArr;
  4390. tVal = new DBC_INT_LIST();
  4391. if (tVal != NULL)
  4392. {
  4393. Assoc::iterator it;
  4394. for (size_t i = 0; i < result->size(); i++)
  4395. {
  4396. cArr = result->at(i);
  4397. it = cArr->find("id");
  4398. if (it != cArr->end())
  4399. {
  4400. tVal->push_back(atoi(it->second.c_str()));
  4401. };
  4402. };
  4403. };
  4404. };
  4405. };
  4406. deleteResult(result);
  4407. };
  4408. return tVal;
  4409. }
  4410. DBC_INT_LIST * sqliteDBC::listNodeFilters(uint32_t NodeID)
  4411. {
  4412. DBC_INT_LIST * tVal = NULL;
  4413. if (m_bStoIsConnected)
  4414. {
  4415. char tQBuf[1024];
  4416. AAssoc * result;
  4417. sprintf(tQBuf, "SELECT `filter_id` FROM `node_target_filters` WHERE `node_id`=%d ORDER BY `order` ASC", NodeID);
  4418. #ifdef DEBUG_ALL
  4419. LOG(tQBuf);
  4420. #endif
  4421. result = m_pDataStorage->Query(tQBuf);
  4422. if (result != NULL)
  4423. {
  4424. if (result->size() > 0)
  4425. {
  4426. Assoc * cArr;
  4427. tVal = new DBC_INT_LIST();
  4428. if (tVal != NULL)
  4429. {
  4430. Assoc::iterator it;
  4431. for (size_t i = 0; i < result->size(); i++)
  4432. {
  4433. cArr = result->at(i);
  4434. it = cArr->find("filter_id");
  4435. if (it != cArr->end())
  4436. {
  4437. tVal->push_back(atoi(it->second.c_str()));
  4438. };
  4439. };
  4440. };
  4441. };
  4442. };
  4443. deleteResult(result);
  4444. };
  4445. return tVal;
  4446. }
  4447. DBC_INT_LIST * sqliteDBC::listOlderReportStat(CSTR Y, CSTR M, CSTR D)
  4448. {
  4449. DBC_INT_LIST * tVal = NULL;
  4450. if (m_bStaIsConnected)
  4451. {
  4452. char tQBuf[MAX_PATH_LEN];
  4453. AAssoc * result;
  4454. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE ((`year`*12*31)+(`month`*31)+`day`)<=((%s*12*31)+(%s*31)+%s) ORDER BY `ROWID`", Y, M, D);
  4455. result = m_pDataStatistics->Query(tQBuf);
  4456. if (result != NULL)
  4457. {
  4458. if (result->size() > 0)
  4459. {
  4460. Assoc * cArr;
  4461. tVal = new DBC_INT_LIST();
  4462. if (tVal != NULL)
  4463. {
  4464. Assoc::iterator it;
  4465. for (size_t i = 0; i < result->size(); i++)
  4466. {
  4467. cArr = result->at(i);
  4468. it = cArr->find("ROWID");
  4469. if (it != cArr->end())
  4470. {
  4471. tVal->push_back(atoi(it->second.c_str()));
  4472. };
  4473. };
  4474. };
  4475. };
  4476. };
  4477. deleteResult(result);
  4478. };
  4479. return tVal;
  4480. }
  4481. DBC_INT_LIST * sqliteDBC::listOlderReportStat(uint32_t TS)
  4482. {
  4483. DBC_INT_LIST * tVal = NULL;
  4484. if (m_bStaIsConnected)
  4485. {
  4486. char tQBuf[MAX_PATH_LEN];
  4487. AAssoc * result;
  4488. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS);
  4489. result = m_pDataStatistics->Query(tQBuf);
  4490. if (result != NULL)
  4491. {
  4492. if (result->size() > 0)
  4493. {
  4494. Assoc * cArr;
  4495. tVal = new DBC_INT_LIST();
  4496. if (tVal != NULL)
  4497. {
  4498. Assoc::iterator it;
  4499. for (size_t i = 0; i < result->size(); i++)
  4500. {
  4501. cArr = result->at(i);
  4502. it = cArr->find("ROWID");
  4503. if (it != cArr->end())
  4504. {
  4505. tVal->push_back(atoi(it->second.c_str()));
  4506. };
  4507. };
  4508. };
  4509. };
  4510. };
  4511. deleteResult(result);
  4512. };
  4513. return tVal;
  4514. }
  4515. DBC_INT_LIST * sqliteDBC::listPhonebook()
  4516. {
  4517. DBC_INT_LIST * tVal = NULL;
  4518. if (m_bStoIsConnected)
  4519. {
  4520. char tQBuf[CTEXT];
  4521. AAssoc * result;
  4522. sprintf(tQBuf, "SELECT `id` FROM `module_modem_phonebook`");
  4523. #ifdef DEBUG_ALL
  4524. LOG(tQBuf);
  4525. #endif
  4526. result = m_pDataStorage->Query(tQBuf);
  4527. if (result != NULL)
  4528. {
  4529. if (result->size() > 0)
  4530. {
  4531. Assoc * cArr;
  4532. tVal = new DBC_INT_LIST();
  4533. if (tVal != NULL)
  4534. {
  4535. Assoc::iterator it;
  4536. for (size_t i = 0; i < result->size(); i++)
  4537. {
  4538. cArr = result->at(i);
  4539. it = cArr->find("id");
  4540. if (it != cArr->end())
  4541. {
  4542. tVal->push_back(atoi(it->second.c_str()));
  4543. };
  4544. };
  4545. };
  4546. };
  4547. };
  4548. deleteResult(result);
  4549. };
  4550. return tVal;
  4551. }
  4552. DBC_INT_LIST * sqliteDBC::listFrozenBubbles(uint8_t cc)
  4553. {
  4554. DBC_INT_LIST * tVal = NULL;
  4555. if (m_bStoIsConnected)
  4556. {
  4557. char tQBuf[CTEXT];
  4558. AAssoc * result;
  4559. if (cc)
  4560. {
  4561. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCK_COUNS`>%d", cc);
  4562. }
  4563. else
  4564. {
  4565. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCKED`=1");
  4566. }
  4567. #ifdef DEBUG_ALL
  4568. LOG(tQBuf);
  4569. #endif
  4570. result = m_pDataStorage->Query(tQBuf);
  4571. if (result != NULL)
  4572. {
  4573. if (result->size() > 0)
  4574. {
  4575. Assoc * cArr;
  4576. tVal = new DBC_INT_LIST();
  4577. if (tVal != NULL)
  4578. {
  4579. Assoc::iterator it;
  4580. for (size_t i = 0; i < result->size(); i++)
  4581. {
  4582. cArr = result->at(i);
  4583. it = cArr->find("id");
  4584. if (it != cArr->end())
  4585. {
  4586. tVal->push_back(atoi(it->second.c_str()));
  4587. };
  4588. };
  4589. };
  4590. };
  4591. };
  4592. deleteResult(result);
  4593. };
  4594. return tVal;
  4595. }
  4596. DBC_INT_LIST * sqliteDBC::listResources(uint32_t NodeID)
  4597. {
  4598. DBC_INT_LIST * tVal = NULL;
  4599. if (m_bStoIsConnected)
  4600. {
  4601. char tQBuf[CTEXT];
  4602. AAssoc * result;
  4603. if (NodeID == 0)
  4604. {
  4605. NodeID = m_nServerNodeID;
  4606. }
  4607. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID);
  4608. #ifdef DEBUG_ALL
  4609. LOG(tQBuf);
  4610. #endif
  4611. result = m_pDataStorage->Query(tQBuf);
  4612. if (result != NULL)
  4613. {
  4614. if (result->size() > 0)
  4615. {
  4616. Assoc * cArr;
  4617. tVal = new DBC_INT_LIST();
  4618. if (tVal != NULL)
  4619. {
  4620. Assoc::iterator it;
  4621. for (size_t i = 0; i < result->size(); i++)
  4622. {
  4623. cArr = result->at(i);
  4624. it = cArr->find("id");
  4625. if (it != cArr->end())
  4626. {
  4627. tVal->push_back(atoi(it->second.c_str()));
  4628. };
  4629. };
  4630. };
  4631. };
  4632. };
  4633. deleteResult(result);
  4634. };
  4635. return tVal;
  4636. }
  4637. bool sqliteDBC::pushMemCMD(uint32_t NODE_ID, uint8_t CMD, CSTR DATA)
  4638. {
  4639. if (m_bMemIsConnected)
  4640. {
  4641. char QS[MAX_PATH];
  4642. sprintf(QS, "INSERT INTO `nodes_qcq`(`node_id`,`cmd`,`status`,`parameter`) VALUES(%d,%d,0,%s)", NODE_ID, CMD, DATA);
  4643. #ifdef DEBUG_ALL
  4644. LOG(QS);
  4645. #endif
  4646. m_pDataMemory->SilentQuery(QS);
  4647. return true;
  4648. };
  4649. return false;
  4650. }
  4651. void sqliteDBC::registerServer(uint32_t SiteID, CSTR identity, int PID, uint32_t * ServID, uint32_t * ServNode)
  4652. {
  4653. if (m_bStoIsConnected)
  4654. {
  4655. m_nSiteID = SiteID;
  4656. char tQBuf[MAX_PATH_LEN];
  4657. #ifdef DEFAULT_SERVER_ID
  4658. sprintf(tQBuf, "INSERT INTO `servers`(`id`,`site_id`,`identity`,`version_string`) VALUES (%d,%d,'%s','%s') ON CONFLICT(`id`) DO UPDATE SET `identity`='%s',`version_string`='%s'", DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity,
  4659. _BUILD_STRING_);
  4660. #else
  4661. sprintf(tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'", SiteID, identity, _BUILD_STRING_, _BUILD_STRING_);
  4662. #endif
  4663. #ifdef DEBUG_ALL
  4664. LOG(tQBuf);
  4665. #endif
  4666. m_pDataStorage->SilentQuery(tQBuf);
  4667. #ifdef DEFAULT_SERVER_ID
  4668. m_nServerID = DEFAULT_SERVER_ID;
  4669. #else
  4670. m_nServerID = getServerID(identity);
  4671. #endif
  4672. *ServID = m_nServerID;
  4673. if (m_nServerID)
  4674. {
  4675. initMemDB(m_nServerID);
  4676. sprintf(tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID);
  4677. #ifdef DEBUG_ALL
  4678. LOG(tQBuf);
  4679. #endif
  4680. m_pDataMemory->SilentQuery(tQBuf);
  4681. sprintf(tQBuf, "INSERT INTO `nodes`(`aux_id`,`type`,`online`,`name`) VALUES (%d, 0,1,'Controller') ON CONFLICT(`type`,`aux_id`) DO UPDATE SET `online`=1", *ServID);
  4682. #ifdef DEBUG_ALL
  4683. LOG(tQBuf);
  4684. #endif
  4685. m_nServerNodeID = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  4686. if (m_nServerNodeID == 0)
  4687. {
  4688. m_nServerNodeID = getServerNode(m_nServerID);
  4689. };
  4690. *ServNode = m_nServerNodeID;
  4691. // Clenup Nodes and services ...
  4692. sprintf(tQBuf, "UPDATE `nodes` SET `online`=0");
  4693. #ifdef DEBUG_ALL
  4694. LOG(tQBuf);
  4695. #endif
  4696. m_pDataStorage->SilentQuery(tQBuf);
  4697. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `manual`=0");
  4698. #ifdef DEBUG_ALL
  4699. LOG(tQBuf);
  4700. #endif
  4701. m_pDataStorage->SilentQuery(tQBuf);
  4702. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )");
  4703. #ifdef DEBUG_ALL
  4704. LOG(tQBuf);
  4705. #endif
  4706. m_pDataStorage->SilentQuery(tQBuf);
  4707. // sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  4708. #ifdef DEBUG_ALL
  4709. // LOG(tQBuf);
  4710. #endif
  4711. // m_pDataStorage->SilentQuery( tQBuf );
  4712. // sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  4713. #ifdef DEBUG_ALL
  4714. // LOG(tQBuf);
  4715. #endif
  4716. // m_pDataStorage->SilentQuery( tQBuf );
  4717. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  4718. #ifdef DEBUG_ALL
  4719. // LOG(tQBuf);
  4720. #endif
  4721. // m_pDataStorage->SilentQuery( tQBuf );
  4722. // cleanResources( tServerNodeID );
  4723. setNodeOnline(m_nServerNodeID);
  4724. };
  4725. };
  4726. }
  4727. bool sqliteDBC::setLRFDataStatus(uint32_t lrfID, uint8_t ST)
  4728. {
  4729. if (m_bStoIsConnected)
  4730. {
  4731. char tQBuf[MAX_PATH_LEN];
  4732. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID);
  4733. #ifdef DEBUG_ALL
  4734. LOG(tQBuf);
  4735. #endif
  4736. m_pDataStorage->SilentQuery(tQBuf);
  4737. return true;
  4738. };
  4739. return false;
  4740. }
  4741. bool sqliteDBC::setLRFDetectionProfile(uint32_t lrfID, uint8_t profileID)
  4742. {
  4743. if (m_bStoIsConnected)
  4744. {
  4745. char tQBuf[MAX_PATH_LEN];
  4746. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID);
  4747. #ifdef DEBUG_ALL
  4748. LOG(tQBuf);
  4749. #endif
  4750. m_pDataStorage->SilentQuery(tQBuf);
  4751. return true;
  4752. };
  4753. return false;
  4754. }
  4755. bool sqliteDBC::setLRFMaskStatus(uint32_t maskID, bool status)
  4756. {
  4757. if (m_bStoIsConnected)
  4758. {
  4759. char tQBuf[MAX_PATH_LEN];
  4760. if (status)
  4761. {
  4762. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID);
  4763. }
  4764. else
  4765. {
  4766. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID);
  4767. };
  4768. #ifdef DEBUG_ALL
  4769. LOG(tQBuf);
  4770. #endif
  4771. m_pDataStorage->SilentQuery(tQBuf);
  4772. return true;
  4773. };
  4774. return false;
  4775. }
  4776. bool sqliteDBC::setLRFAltSerial(uint32_t lrfID, CSTR Serial)
  4777. {
  4778. if (m_bStoIsConnected)
  4779. {
  4780. char tQBuf[MAX_PATH_LEN];
  4781. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID);
  4782. #ifdef DEBUG_ALL
  4783. LOG(tQBuf);
  4784. #endif
  4785. m_pDataStorage->SilentQuery(tQBuf);
  4786. return true;
  4787. };
  4788. return false;
  4789. }
  4790. bool sqliteDBC::setLRFAltVerison(uint32_t lrfID, CSTR version)
  4791. {
  4792. if (m_bStoIsConnected)
  4793. {
  4794. char tQBuf[MAX_PATH_LEN];
  4795. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID);
  4796. #ifdef DEBUG_ALL
  4797. LOG(tQBuf);
  4798. #endif
  4799. m_pDataStorage->SilentQuery(tQBuf);
  4800. return true;
  4801. };
  4802. return false;
  4803. }
  4804. bool sqliteDBC::setNodeOffline(uint32_t NodeID)
  4805. {
  4806. if (m_bStoIsConnected)
  4807. {
  4808. char tQBuf[CTEXT];
  4809. if (NodeID)
  4810. {
  4811. sprintf(tQBuf, "UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID);
  4812. #ifdef DEBUG_ALL
  4813. LOG(tQBuf);
  4814. #endif
  4815. m_pDataStorage->SilentQuery(tQBuf);
  4816. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID);
  4817. #ifdef DEBUG_ALL
  4818. LOG(tQBuf);
  4819. #endif
  4820. m_pDataStorage->SilentQuery(tQBuf);
  4821. return true;
  4822. };
  4823. };
  4824. return false;
  4825. }
  4826. bool sqliteDBC::setNodeOnline(uint32_t NodeID, uint8_t status)
  4827. {
  4828. if (m_bStoIsConnected)
  4829. {
  4830. char tQBuf[CTEXT];
  4831. if (NodeID)
  4832. {
  4833. sprintf(tQBuf, "UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID);
  4834. #ifdef DEBUG_ALL
  4835. LOG(tQBuf);
  4836. #endif
  4837. m_pDataStorage->SilentQuery(tQBuf);
  4838. sprintf(tQBuf, "UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID);
  4839. #ifdef DEBUG_ALL
  4840. LOG(tQBuf);
  4841. #endif
  4842. m_pDataStorage->SilentQuery(tQBuf);
  4843. return true;
  4844. };
  4845. };
  4846. return false;
  4847. }
  4848. bool sqliteDBC::setRessourceActive(uint32_t RessourceID, bool Status)
  4849. {
  4850. if (m_bStoIsConnected)
  4851. {
  4852. char tQBuf[CTEXT];
  4853. if (RessourceID)
  4854. {
  4855. sprintf(tQBuf, "UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID);
  4856. #ifdef DEBUG_ALL
  4857. LOG(tQBuf);
  4858. #endif
  4859. m_pDataStorage->SilentQuery(tQBuf);
  4860. return true;
  4861. };
  4862. };
  4863. return false;
  4864. }
  4865. bool sqliteDBC::setStreamValid(uint32_t streamID, bool status)
  4866. {
  4867. if (m_bStoIsConnected)
  4868. {
  4869. char tQBuf[MAX_PATH_LEN];
  4870. if (status)
  4871. {
  4872. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  4873. }
  4874. else
  4875. {
  4876. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  4877. };
  4878. #ifdef DEBUG_ALL
  4879. LOG(tQBuf);
  4880. #endif
  4881. m_pDataStorage->SilentQuery(tQBuf);
  4882. return true;
  4883. };
  4884. return false;
  4885. }
  4886. void sqliteDBC::unregisterServer(void)
  4887. {
  4888. if (m_bStoIsConnected)
  4889. {
  4890. char tQBuf[MAX_PATH_LEN];
  4891. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0");
  4892. #ifdef DEBUG_ALL
  4893. LOG(tQBuf);
  4894. #endif
  4895. m_pDataStorage->SilentQuery(tQBuf);
  4896. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0");
  4897. #ifdef DEBUG_ALL
  4898. LOG(tQBuf);
  4899. #endif
  4900. m_pDataStorage->SilentQuery(tQBuf);
  4901. };
  4902. setNodeOffline(m_nServerNodeID);
  4903. }
  4904. bool sqliteDBC::updateSystemInfo(DBC_SYS_INFO * ptr)
  4905. {
  4906. if (m_bStoIsConnected)
  4907. {
  4908. char tQBuf[MAX_PATH_LEN];
  4909. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d", ptr->OS_INFO, ptr->HW_INFO, m_nSiteID);
  4910. #ifdef DEBUG_ALL
  4911. LOG(tQBuf);
  4912. #endif
  4913. m_pDataStorage->SilentQuery(tQBuf);
  4914. return true;
  4915. };
  4916. return false;
  4917. }
  4918. bool sqliteDBC::fillShadow2D(uint32_t ID, SHADOW & SH)
  4919. {
  4920. if (m_bStoIsConnected)
  4921. {
  4922. char tQBuf[CTEXT];
  4923. AAssoc * result;
  4924. sprintf(tQBuf, "SELECT `lv`.`id`, `lm`.`layer`, `lm`.`status` FROM `lrf_mask_vertexes` `lv` LEFT JOIN `lrf_masks` `lm` ON (`lm`.`id` = `lv`.`mask_id`) WHERE `lm`.`sensor_id`=%d ORDER BY `lv`.`id`", ID);
  4925. #ifdef DEBUG_ALL
  4926. LOG(tQBuf);
  4927. #endif
  4928. result = m_pDataStorage->Query(tQBuf);
  4929. if (result != NULL)
  4930. {
  4931. if (result->size() > 0)
  4932. {
  4933. SH.m_nEnabled = 0xFFFFFFFF;
  4934. SH.m_nPolygonCount = result->size();
  4935. SH.pPolygones = (SHADOW_POLY *) calloc(SH.m_nPolygonCount, sizeof(SHADOW_POLY));
  4936. if (SH.pPolygones)
  4937. {
  4938. Assoc * cArr;
  4939. for (uint8_t idx = 0; idx < result->size(); idx++)
  4940. {
  4941. Assoc::iterator it;
  4942. cArr = result->at(idx);
  4943. it = cArr->find("id");
  4944. if (it != cArr->end())
  4945. {
  4946. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  4947. };
  4948. it = cArr->find("layer");
  4949. if (it != cArr->end())
  4950. {
  4951. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  4952. };
  4953. it = cArr->find("status");
  4954. if (it != cArr->end())
  4955. {
  4956. SH.m_nEnabled &= (it->second.c_str()[0] == 'E') ? 0xFFFFFFFF : ~(1 << SH.pPolygones[idx].bType);
  4957. };
  4958. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID);
  4959. #ifdef DEBUG_ALL
  4960. LOG(tQBuf);
  4961. #endif
  4962. AAssoc * v_result = m_pDataStorage->Query(tQBuf);
  4963. if (v_result != NULL)
  4964. {
  4965. if (v_result->size() > 0)
  4966. {
  4967. SH.pPolygones[idx].wCount = v_result->size();
  4968. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc(SH.pPolygones[idx].wCount, sizeof(HZ_POINT));
  4969. if (SH.pPolygones[idx].pPoint)
  4970. {
  4971. for (uint32_t zdx = 0; zdx < SH.pPolygones[idx].wCount; zdx++)
  4972. {
  4973. cArr = v_result->at(zdx);
  4974. it = cArr->find("x");
  4975. if (it != cArr->end())
  4976. {
  4977. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  4978. };
  4979. it = cArr->find("y");
  4980. if (it != cArr->end())
  4981. {
  4982. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  4983. };
  4984. LOG("Point Added!");
  4985. };
  4986. }
  4987. else
  4988. {
  4989. LOG("No mem for Points!");
  4990. };
  4991. }
  4992. else
  4993. {
  4994. LOG("No Points!");
  4995. SH.pPolygones[idx].wCount = 0;
  4996. SH.pPolygones[idx].pPoint = NULL;
  4997. };
  4998. deleteResult(v_result);
  4999. };
  5000. };
  5001. };
  5002. };
  5003. deleteResult(result);
  5004. };
  5005. return true;
  5006. };
  5007. return false;
  5008. }
  5009. bool sqliteDBC::getAppDebugMode()
  5010. {
  5011. bool rVal = false;
  5012. if (m_bStoIsConnected)
  5013. {
  5014. char tQBuf[CTEXT];
  5015. AAssoc * result;
  5016. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1");
  5017. #ifdef DEBUG_ALL
  5018. LOG(tQBuf);
  5019. #endif
  5020. result = m_pDataStorage->Query(tQBuf);
  5021. if (result != NULL)
  5022. {
  5023. if (result->size() > 0)
  5024. {
  5025. Assoc * cArr;
  5026. Assoc::iterator it;
  5027. cArr = result->at(0);
  5028. it = cArr->find("work_mode");
  5029. if (it != cArr->end())
  5030. {
  5031. rVal = (bool) atoi(it->second.c_str());
  5032. };
  5033. };
  5034. };
  5035. deleteResult(result);
  5036. };
  5037. return rVal;
  5038. }
  5039. DBC_ATC_MODULE_DATA * sqliteDBC::getAttentionModuleData( uint32_t id ){
  5040. DBC_ATC_MODULE_DATA * tVal = NULL;
  5041. if (m_bStoIsConnected)
  5042. {
  5043. char tQBuf[CTEXT];
  5044. AAssoc * result;
  5045. sprintf(tQBuf, "SELECT * FROM `module_attention` WHERE `id`=%d", id);
  5046. #ifdef DEBUG_ALL
  5047. LOG(tQBuf);
  5048. #endif
  5049. result = m_pDataStorage->Query(tQBuf);
  5050. if (result != NULL)
  5051. {
  5052. if (result->size() > 0)
  5053. {
  5054. Assoc * cArr;
  5055. tVal = (DBC_ATC_MODULE_DATA *) malloc(sizeof(DBC_ATC_MODULE_DATA));
  5056. if (tVal != NULL)
  5057. {
  5058. memset(tVal, 0, sizeof(DBC_ATC_MODULE_DATA));
  5059. ;
  5060. Assoc::iterator it;
  5061. cArr = result->at(0);
  5062. it = cArr->find("rest");
  5063. if (it != cArr->end())
  5064. {
  5065. tVal->rest_time = atoi(it->second.c_str());
  5066. };
  5067. it = cArr->find("persist");
  5068. if (it != cArr->end())
  5069. {
  5070. tVal->persist_time = atoi(it->second.c_str());
  5071. };
  5072. it = cArr->find("range");
  5073. if (it != cArr->end())
  5074. {
  5075. tVal->attention_zone_range = atof(it->second.c_str());
  5076. };
  5077. };
  5078. };
  5079. };
  5080. deleteResult(result);
  5081. };
  5082. return tVal;
  5083. }
  5084. REST_SETTINGS *sqliteDBC::getBoschData( uint32_t id ){
  5085. REST_SETTINGS * tVal = NULL;
  5086. if (m_bStoIsConnected){
  5087. char tQBuf[CTEXT];
  5088. AAssoc * result;
  5089. sprintf(tQBuf, "SELECT * FROM `module_bvms` WHERE `id`=%d", id);
  5090. #ifdef DEBUG_ALL
  5091. LOG(tQBuf);
  5092. #endif
  5093. result = m_pDataStorage->Query(tQBuf);
  5094. if (result != NULL)
  5095. {
  5096. if (result->size() > 0)
  5097. {
  5098. Assoc * cArr;
  5099. tVal = (REST_SETTINGS *) malloc(sizeof(REST_SETTINGS));
  5100. if (tVal != NULL)
  5101. {
  5102. memset(tVal, 0, sizeof(REST_SETTINGS));
  5103. ;
  5104. Assoc::iterator it;
  5105. cArr = result->at(0);
  5106. it = cArr->find("ip");
  5107. if (it != cArr->end())
  5108. {
  5109. strncpy(tVal->server_ip, it->second.c_str(), 48);
  5110. };
  5111. it = cArr->find("port");
  5112. if (it != cArr->end())
  5113. {
  5114. tVal->port = atoi(it->second.c_str());
  5115. };
  5116. it = cArr->find("api_uri");
  5117. if (it != cArr->end())
  5118. {
  5119. strncpy(tVal->rest_api, it->second.c_str(), 256);
  5120. };
  5121. it = cArr->find("auth_mode");
  5122. if (it != cArr->end())
  5123. {
  5124. tVal->auth_mode = atoi(it->second.c_str());
  5125. };
  5126. it = cArr->find("access_user");
  5127. if (it != cArr->end())
  5128. {
  5129. strncpy(tVal->access_user, it->second.c_str(), 48);
  5130. };
  5131. it = cArr->find("access_pass");
  5132. if (it != cArr->end())
  5133. {
  5134. strncpy(tVal->access_pass, it->second.c_str(), 48);
  5135. };
  5136. it = cArr->find("detection_evt_ena");
  5137. if (it != cArr->end())
  5138. {
  5139. tVal->detection_enabled = (bool) atoi(it->second.c_str());
  5140. };
  5141. it = cArr->find("detection_evt_id");
  5142. if (it != cArr->end())
  5143. {
  5144. tVal->detection_src = atoi(it->second.c_str());
  5145. };
  5146. it = cArr->find("camera_evt_ena");
  5147. if (it != cArr->end())
  5148. {
  5149. tVal->camera_enabled = (bool) atoi(it->second.c_str());
  5150. };
  5151. it = cArr->find("camera_evt_id");
  5152. if (it != cArr->end())
  5153. {
  5154. tVal->camera_src = atoi(it->second.c_str());
  5155. };
  5156. it = cArr->find("use_ssl");
  5157. if (it != cArr->end())
  5158. {
  5159. tVal->use_ssl = (bool) atoi(it->second.c_str());
  5160. } else {
  5161. tVal->use_ssl = 1;
  5162. }
  5163. };
  5164. };
  5165. };
  5166. deleteResult(result);
  5167. };
  5168. return tVal;
  5169. }
  5170. BUBBLE_FILTER_DATA * sqliteDBC::getBubbleFilterData(uint32_t id)
  5171. {
  5172. BUBBLE_FILTER_DATA * tVal = NULL;
  5173. if (m_bStoIsConnected)
  5174. {
  5175. char tQBuf[CTEXT];
  5176. AAssoc * result;
  5177. sprintf(tQBuf, "SELECT * FROM `target_filter_bubble` WHERE `id`=%d", id);
  5178. #ifdef DEBUG_ALL
  5179. LOG(tQBuf);
  5180. #endif
  5181. result = m_pDataStorage->Query(tQBuf);
  5182. if (result != NULL)
  5183. {
  5184. if (result->size() > 0)
  5185. {
  5186. Assoc * cArr;
  5187. tVal = (BUBBLE_FILTER_DATA *) malloc(sizeof(BUBBLE_FILTER_DATA));
  5188. if (tVal != NULL)
  5189. {
  5190. memset(tVal, 0, sizeof(BUBBLE_FILTER_DATA));
  5191. ;
  5192. Assoc::iterator it;
  5193. tVal->id = id;
  5194. cArr = result->at(0);
  5195. it = cArr->find("BEGINNING_RANGE");
  5196. if (it != cArr->end())
  5197. {
  5198. tVal->beginning_range = atof(it->second.c_str());
  5199. };
  5200. it = cArr->find("MAX_RANGE");
  5201. if (it != cArr->end())
  5202. {
  5203. tVal->maximal_range = atof(it->second.c_str());
  5204. };
  5205. it = cArr->find("MIN_BLOCK_COUNS");
  5206. if (it != cArr->end())
  5207. {
  5208. tVal->min_block_counts = atoi(it->second.c_str());
  5209. };
  5210. #ifdef BUBBLE_TIME_BUCKET
  5211. it = cArr->find("BUCKET_TIME");
  5212. if (it != cArr->end())
  5213. {
  5214. tVal->bucket_time = atoi(it->second.c_str());
  5215. };
  5216. #endif
  5217. it = cArr->find("TTL");
  5218. if (it != cArr->end())
  5219. {
  5220. tVal->ttl = atoi(it->second.c_str());
  5221. };
  5222. it = cArr->find("T2T");
  5223. if (it != cArr->end())
  5224. {
  5225. tVal->t2t = atoi(it->second.c_str());
  5226. };
  5227. it = cArr->find("PROPOGATE");
  5228. if (it != cArr->end())
  5229. {
  5230. tVal->propogate = atoi(it->second.c_str());
  5231. };
  5232. it = cArr->find("METHOD");
  5233. if (it != cArr->end())
  5234. {
  5235. tVal->method = atoi(it->second.c_str());
  5236. };
  5237. it = cArr->find("ENABLE_EVENT");
  5238. if (it != cArr->end())
  5239. {
  5240. tVal->enable_evt = (bool) atoi(it->second.c_str());
  5241. };
  5242. };
  5243. #ifdef BUBBLE_TIME_BUCKET
  5244. if (tVal->bucket_time < BUBBLE_FILTER_MIN_BUCKET_TIME)
  5245. {
  5246. tVal->bucket_time = BUBBLE_FILTER_MIN_BUCKET_TIME;
  5247. }
  5248. #endif
  5249. };
  5250. };
  5251. deleteResult(result);
  5252. };
  5253. return tVal;
  5254. }
  5255. DBC_CAM_DATA * sqliteDBC::getCamData(uint32_t ID)
  5256. {
  5257. DBC_CAM_DATA * tVal = NULL;
  5258. if (m_bStoIsConnected)
  5259. {
  5260. char tQBuf[CTEXT];
  5261. AAssoc * result;
  5262. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID);
  5263. #ifdef DEBUG_ALL
  5264. LOG(tQBuf);
  5265. #endif
  5266. result = m_pDataStorage->Query(tQBuf);
  5267. if (result != NULL)
  5268. {
  5269. if (result->size() > 0)
  5270. {
  5271. Assoc * cArr;
  5272. tVal = (DBC_CAM_DATA *) malloc(sizeof(DBC_CAM_DATA));
  5273. if (tVal != NULL)
  5274. {
  5275. memset(tVal, 0, sizeof(DBC_CAM_DATA));
  5276. tVal->id = ID;
  5277. Assoc::iterator it;
  5278. cArr = result->at(0);
  5279. it = cArr->find("type");
  5280. if (it != cArr->end())
  5281. {
  5282. tVal->type = atoi(it->second.c_str());
  5283. };
  5284. it = cArr->find("online");
  5285. if (it != cArr->end())
  5286. {
  5287. tVal->online = (bool) atoi(it->second.c_str());
  5288. };
  5289. it = cArr->find("stream_id");
  5290. if (it != cArr->end())
  5291. {
  5292. tVal->stream_id = atoi(it->second.c_str());
  5293. };
  5294. it = cArr->find("hardware");
  5295. if (it != cArr->end())
  5296. {
  5297. strncpy(tVal->hardware, it->second.c_str(), 32);
  5298. };
  5299. it = cArr->find("name");
  5300. if (it != cArr->end())
  5301. {
  5302. strncpy(tVal->name, it->second.c_str(), 64);
  5303. };
  5304. it = cArr->find("status");
  5305. if (it != cArr->end())
  5306. {
  5307. tVal->status = atoi(it->second.c_str());
  5308. };
  5309. it = cArr->find("session");
  5310. if (it != cArr->end())
  5311. {
  5312. strncpy(tVal->session, it->second.c_str(), 32);
  5313. };
  5314. it = cArr->find("protocol");
  5315. if (it != cArr->end())
  5316. {
  5317. tVal->protocol = atoi(it->second.c_str());
  5318. };
  5319. it = cArr->find("protocol_aux");
  5320. if (it != cArr->end())
  5321. {
  5322. tVal->protocol_aux = atoi(it->second.c_str());
  5323. };
  5324. it = cArr->find("uart_id");
  5325. if (it != cArr->end())
  5326. {
  5327. tVal->uart_id = atoi(it->second.c_str());
  5328. };
  5329. it = cArr->find("x");
  5330. if (it != cArr->end())
  5331. {
  5332. tVal->x = atof(it->second.c_str());
  5333. };
  5334. it = cArr->find("y");
  5335. if (it != cArr->end())
  5336. {
  5337. tVal->y = atof(it->second.c_str());
  5338. };
  5339. it = cArr->find("z");
  5340. if (it != cArr->end())
  5341. {
  5342. tVal->z = atof(it->second.c_str());
  5343. };
  5344. it = cArr->find("x_rot");
  5345. if (it != cArr->end())
  5346. {
  5347. tVal->x_rot = atof(it->second.c_str());
  5348. };
  5349. it = cArr->find("y_rot");
  5350. if (it != cArr->end())
  5351. {
  5352. tVal->y_rot = atof(it->second.c_str());
  5353. };
  5354. it = cArr->find("z_rot");
  5355. if (it != cArr->end())
  5356. {
  5357. tVal->z_rot = atof(it->second.c_str());
  5358. };
  5359. it = cArr->find("imager_type");
  5360. if (it != cArr->end())
  5361. {
  5362. tVal->imager_type = atoi(it->second.c_str());
  5363. };
  5364. it = cArr->find("lens_f");
  5365. if (it != cArr->end())
  5366. {
  5367. tVal->lens_f = atoi(it->second.c_str());
  5368. };
  5369. it = cArr->find("ptz_speed_step");
  5370. if (it != cArr->end())
  5371. {
  5372. tVal->ptz_speed_step = atof(it->second.c_str());
  5373. };
  5374. it = cArr->find("pan_model");
  5375. if (it != cArr->end())
  5376. {
  5377. tVal->pan_model = atoi(it->second.c_str());
  5378. };
  5379. it = cArr->find("pan_angles");
  5380. if (it != cArr->end())
  5381. {
  5382. tVal->pan_angles = atoi(it->second.c_str());
  5383. };
  5384. it = cArr->find("tilt_model");
  5385. if (it != cArr->end())
  5386. {
  5387. tVal->tilt_model = atoi(it->second.c_str());
  5388. };
  5389. it = cArr->find("tilt_angles");
  5390. if (it != cArr->end())
  5391. {
  5392. tVal->tilt_angles = atoi(it->second.c_str());
  5393. };
  5394. it = cArr->find("zoom_model");
  5395. if (it != cArr->end())
  5396. {
  5397. tVal->zoom_model = atoi(it->second.c_str());
  5398. };
  5399. it = cArr->find("zoom");
  5400. if (it != cArr->end())
  5401. {
  5402. tVal->zoom = atof(it->second.c_str());
  5403. };
  5404. it = cArr->find("max_range");
  5405. if (it != cArr->end())
  5406. {
  5407. tVal->max_range = atof(it->second.c_str());
  5408. };
  5409. it = cArr->find("access_login");
  5410. if (it != cArr->end())
  5411. {
  5412. strncpy(tVal->username, it->second.c_str(), 16);
  5413. };
  5414. it = cArr->find("access_pass");
  5415. if (it != cArr->end())
  5416. {
  5417. strncpy(tVal->password, it->second.c_str(), 16);
  5418. };
  5419. it = cArr->find("uuid");
  5420. if (it != cArr->end())
  5421. {
  5422. strncpy(tVal->uuid, it->second.c_str(), 64);
  5423. };
  5424. it = cArr->find("mac");
  5425. if (it != cArr->end())
  5426. {
  5427. strncpy(tVal->mac, it->second.c_str(), 16);
  5428. };
  5429. it = cArr->find("ip");
  5430. if (it != cArr->end())
  5431. {
  5432. strncpy(tVal->ip, it->second.c_str(), 16);
  5433. };
  5434. it = cArr->find("port");
  5435. if (it != cArr->end())
  5436. {
  5437. tVal->port = atoi(it->second.c_str());
  5438. };
  5439. it = cArr->find("onvif_access");
  5440. if (it != cArr->end())
  5441. {
  5442. strncpy(tVal->onvif_access, it->second.c_str(), 5);
  5443. };
  5444. it = cArr->find("onvif_ctrl");
  5445. if (it != cArr->end())
  5446. {
  5447. strncpy(tVal->onvif_ctrl, it->second.c_str(), 250);
  5448. };
  5449. it = cArr->find("ptz_token");
  5450. if (it != cArr->end())
  5451. {
  5452. strncpy(tVal->ptz_token, it->second.c_str(), 64);
  5453. };
  5454. it = cArr->find("trigger_mode");
  5455. if (it != cArr->end())
  5456. {
  5457. tVal->triggering_mode = atoi(it->second.c_str());
  5458. };
  5459. it = cArr->find("auth_mode");
  5460. if (it != cArr->end())
  5461. {
  5462. tVal->auth_mode = atoi(it->second.c_str());
  5463. };
  5464. it = cArr->find("ptz_axel");
  5465. if (it != cArr->end())
  5466. {
  5467. tVal->ptz_axel = atoi(it->second.c_str());
  5468. };
  5469. it = cArr->find("manual");
  5470. if (it != cArr->end())
  5471. {
  5472. tVal->manual = (bool) atoi(it->second.c_str());
  5473. };
  5474. it = cArr->find("homing_enable");
  5475. if (it != cArr->end())
  5476. {
  5477. tVal->homing_enable = (bool) atoi(it->second.c_str());
  5478. };
  5479. it = cArr->find("homing_to");
  5480. if (it != cArr->end())
  5481. {
  5482. tVal->homing_to = atoi(it->second.c_str());
  5483. };
  5484. it = cArr->find("home_token");
  5485. if (it != cArr->end())
  5486. {
  5487. strncpy(tVal->home_token, it->second.c_str(), 32);
  5488. };
  5489. it = cArr->find("eco_ir");
  5490. if (it != cArr->end())
  5491. {
  5492. tVal->eco_ir = (bool) atoi(it->second.c_str());
  5493. };
  5494. it = cArr->find("prefered_events_only");
  5495. if (it != cArr->end())
  5496. {
  5497. tVal->prefered_events_only = (bool) atoi(it->second.c_str());
  5498. };
  5499. it = cArr->find("flip_mode");
  5500. if (it != cArr->end())
  5501. {
  5502. tVal->flip_mode = atoi(it->second.c_str());
  5503. };
  5504. };
  5505. };
  5506. };
  5507. deleteResult(result);
  5508. };
  5509. return tVal;
  5510. }
  5511. uint32_t sqliteDBC::getCamId(CSTR UUID)
  5512. {
  5513. uint32_t RetVal = 0;
  5514. if (m_bStoIsConnected)
  5515. {
  5516. char tQBuf[CTEXT];
  5517. AAssoc * result;
  5518. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID);
  5519. #ifdef DEBUG_ALL
  5520. LOG(tQBuf);
  5521. #endif
  5522. result = m_pDataStorage->Query(tQBuf);
  5523. if (result != NULL)
  5524. {
  5525. if (result->size() > 0)
  5526. {
  5527. Assoc * cArr;
  5528. Assoc::iterator it;
  5529. cArr = result->at(0);
  5530. it = cArr->find("id");
  5531. if (it != cArr->end())
  5532. {
  5533. RetVal = atoi(it->second.c_str());
  5534. };
  5535. };
  5536. };
  5537. deleteResult(result);
  5538. };
  5539. return RetVal;
  5540. }
  5541. CAM_PRESET * sqliteDBC::getCamPreset(uint32_t SETID)
  5542. {
  5543. CAM_PRESET * tVal = NULL;
  5544. if (m_bStoIsConnected)
  5545. {
  5546. char tQBuf[CTEXT];
  5547. AAssoc * result;
  5548. sprintf(tQBuf, "SELECT * FROM `cameras_presets` WHERE `id`=%d", SETID);
  5549. #ifdef DEBUG_ALL
  5550. LOG(tQBuf);
  5551. #endif
  5552. result = m_pDataStorage->Query(tQBuf);
  5553. if (result != NULL)
  5554. {
  5555. if (result->size() > 0)
  5556. {
  5557. Assoc * cArr;
  5558. tVal = (CAM_PRESET *) malloc(sizeof(CAM_PRESET));
  5559. if (tVal != NULL)
  5560. {
  5561. memset(tVal, 0, sizeof(CAM_PRESET));
  5562. tVal->id = SETID;
  5563. Assoc::iterator it;
  5564. cArr = result->at(0);
  5565. it = cArr->find("token");
  5566. if (it != cArr->end())
  5567. {
  5568. strncpy(tVal->token, it->second.c_str(), 2);
  5569. tVal->token[3] = (char) 0;
  5570. };
  5571. it = cArr->find("role");
  5572. if (it != cArr->end())
  5573. {
  5574. tVal->role = atoi(it->second.c_str());
  5575. };
  5576. it = cArr->find("row");
  5577. if (it != cArr->end())
  5578. {
  5579. tVal->row = atoi(it->second.c_str());
  5580. };
  5581. it = cArr->find("column");
  5582. if (it != cArr->end())
  5583. {
  5584. tVal->col = atoi(it->second.c_str());
  5585. };
  5586. };
  5587. };
  5588. };
  5589. deleteResult(result);
  5590. };
  5591. return tVal;
  5592. }
  5593. DBC_PRESETS_INFO * sqliteDBC::getCamPresetInfo(uint32_t CamID)
  5594. {
  5595. DBC_PRESETS_INFO * tVal = NULL;
  5596. if (m_bStoIsConnected)
  5597. {
  5598. char tQBuf[CTEXT];
  5599. AAssoc * result;
  5600. sprintf(tQBuf, "SELECT * FROM `cameras_preset_sets` WHERE `camera_id`=%d", CamID);
  5601. #ifdef DEBUG_ALL
  5602. LOG(tQBuf);
  5603. #endif
  5604. result = m_pDataStorage->Query(tQBuf);
  5605. if (result != NULL)
  5606. {
  5607. if (result->size() > 0)
  5608. {
  5609. Assoc * cArr;
  5610. tVal = (DBC_PRESETS_INFO *) malloc(sizeof(DBC_PRESETS_INFO));
  5611. if (tVal != NULL)
  5612. {
  5613. memset(tVal, 0, sizeof(DBC_PRESETS_INFO));
  5614. Assoc::iterator it;
  5615. cArr = result->at(0);
  5616. it = cArr->find("maxCount");
  5617. if (it != cArr->end())
  5618. {
  5619. tVal->maxCount = atoi(it->second.c_str());
  5620. };
  5621. it = cArr->find("p_segs");
  5622. if (it != cArr->end())
  5623. {
  5624. tVal->gCols = atoi(it->second.c_str());
  5625. };
  5626. it = cArr->find("z_segs");
  5627. if (it != cArr->end())
  5628. {
  5629. tVal->gRows = atoi(it->second.c_str());
  5630. };
  5631. };
  5632. };
  5633. };
  5634. deleteResult(result);
  5635. };
  5636. return tVal;
  5637. }
  5638. CSTR sqliteDBC::getDataParam(uint8_t pid)
  5639. {
  5640. switch (pid)
  5641. {
  5642. case 0: return strdup(m_pDbFile); break;
  5643. case 1: return strdup(web_access_user); break;
  5644. case 2: return strdup(web_access_group); break;
  5645. };
  5646. return NULL;
  5647. }
  5648. bool sqliteDBC::updateIPCam(uint32_t CamID, CSTR Addr)
  5649. {
  5650. if (m_bStoIsConnected)
  5651. {
  5652. // onvif_ctrl = path of the device service only (as la_mojo's own discovery INSERT and the web UI
  5653. // store it; la_mojo builds http://ip[:port]onvif_ctrl). The whole XAddrs list was stored: the record
  5654. // changed on every discovery round and the camera node was rebuilt (its stream dropped).
  5655. char tPath[256] = "/onvif/device_service";
  5656. {
  5657. const char * tH = strstr(Addr, "//");
  5658. if (tH != NULL)
  5659. {
  5660. const char * tP = strchr(tH + 2, '/');
  5661. if (tP != NULL)
  5662. {
  5663. size_t tLen = strcspn(tP, " \t\r\n"); // first XAddr only
  5664. if (tLen > 0 && tLen < sizeof(tPath) && memchr(tP, '\'', tLen) == NULL)
  5665. {
  5666. memcpy(tPath, tP, tLen);
  5667. tPath[tLen] = 0;
  5668. };
  5669. };
  5670. };
  5671. }
  5672. char tQBuf[MAX_PATH_LEN];
  5673. char tIpBuf[CTEXT];
  5674. uint16_t tPort = 0;
  5675. // host[:port] of the FIRST XAddr only: the old parser searched ':' in the whole list (a second, IPv6
  5676. // XAddr or a ':' in the path gave a broken IP / port)
  5677. tIpBuf[0] = (char) 0;
  5678. {
  5679. const char * tH = strstr(Addr, "//");
  5680. if (tH != NULL)
  5681. {
  5682. tH += 2;
  5683. size_t tLen = strcspn(tH, "/ \t\r\n");
  5684. if (tLen > 0 && tLen < sizeof(tIpBuf))
  5685. {
  5686. memcpy(tIpBuf, tH, tLen);
  5687. tIpBuf[tLen] = (char) 0;
  5688. char * tC = strrchr(tIpBuf, ':');
  5689. if (tC != NULL && strchr(tIpBuf, ']') == NULL) // not [IPv6]
  5690. {
  5691. *tC = (char) 0;
  5692. tPort = (uint16_t) atoi(tC + 1);
  5693. };
  5694. };
  5695. };
  5696. }
  5697. if (tIpBuf[0] == (char) 0 || strchr(tIpBuf, '\'') != NULL)
  5698. {
  5699. LOG_ERROR("updateIPCam: no address in the discovery result - camera not updated");
  5700. return false;
  5701. };
  5702. if (tPort)
  5703. {
  5704. snprintf(tQBuf, sizeof(tQBuf), "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d", tPath, tIpBuf, tPort, CamID);
  5705. }
  5706. else
  5707. {
  5708. snprintf(tQBuf, sizeof(tQBuf), "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d", tPath, tIpBuf, CamID);
  5709. }
  5710. #ifdef DEBUG_ALL
  5711. LOG(tQBuf);
  5712. #endif
  5713. m_pDataStorage->SilentQuery(tQBuf);
  5714. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5715. #ifdef DEBUG_ALL
  5716. LOG(tQBuf);
  5717. #endif
  5718. m_pDataStorage->SilentQuery(tQBuf);
  5719. return true;
  5720. };
  5721. return false;
  5722. }
  5723. bool sqliteDBC::updateCamRange(uint32_t camID, float range)
  5724. {
  5725. if (m_bStoIsConnected)
  5726. {
  5727. char tQBuf[MAX_PATH_LEN];
  5728. sprintf(tQBuf, "UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d", range, camID);
  5729. #ifdef DEBUG_ALL
  5730. LOG(tQBuf);
  5731. #endif
  5732. m_pDataStorage->SilentQuery(tQBuf);
  5733. return true;
  5734. };
  5735. return false;
  5736. }
  5737. bool sqliteDBC::updateCamOnlineStatus(uint32_t camID, bool online)
  5738. {
  5739. if (m_bStoIsConnected)
  5740. {
  5741. char tQBuf[MAX_PATH_LEN];
  5742. if (online)
  5743. {
  5744. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID);
  5745. }
  5746. else
  5747. {
  5748. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID);
  5749. };
  5750. m_pDataStorage->SilentQuery(tQBuf);
  5751. if (online)
  5752. {
  5753. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  5754. }
  5755. else
  5756. {
  5757. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID);
  5758. };
  5759. m_pDataStorage->SilentQuery(tQBuf);
  5760. return true;
  5761. };
  5762. return false;
  5763. }
  5764. bool sqliteDBC::updateIPCam(CSTR uuid, CSTR Addr)
  5765. {
  5766. if (m_bStoIsConnected)
  5767. {
  5768. uint32_t cID = getCamId(uuid);
  5769. if (cID)
  5770. {
  5771. return updateIPCam(cID, Addr);
  5772. };
  5773. };
  5774. return false;
  5775. }
  5776. bool sqliteDBC::updateLocalCam(CSTR uuid)
  5777. {
  5778. if (m_bStoIsConnected)
  5779. {
  5780. uint32_t cID = getCamId(uuid);
  5781. if (cID)
  5782. {
  5783. return updateLocalCam(cID);
  5784. };
  5785. };
  5786. return false;
  5787. }
  5788. bool sqliteDBC::updateLocalCam(uint32_t CamID)
  5789. {
  5790. if (m_bStoIsConnected)
  5791. {
  5792. char tQBuf[MAX_PATH_LEN];
  5793. sprintf(tQBuf, "UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID);
  5794. #ifdef DEBUG_ALL
  5795. LOG(tQBuf);
  5796. #endif
  5797. m_pDataStorage->SilentQuery(tQBuf);
  5798. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5799. #ifdef DEBUG_ALL
  5800. LOG(tQBuf);
  5801. #endif
  5802. m_pDataStorage->SilentQuery(tQBuf);
  5803. return true;
  5804. };
  5805. return false;
  5806. }
  5807. bool sqliteDBC::updateLRFComStat(uint32_t lrfID, uint32_t ErrorPerMinute)
  5808. {
  5809. if (m_bStoIsConnected)
  5810. {
  5811. char tQBuf[MAX_PATH_LEN];
  5812. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID);
  5813. #ifdef DEBUG_ALL
  5814. LOG(tQBuf);
  5815. #endif
  5816. m_pDataStorage->SilentQuery(tQBuf);
  5817. return true;
  5818. };
  5819. return false;
  5820. }
  5821. bool sqliteDBC::updateLRFFrozenStatus(uint32_t lrfID, bool FFT, bool SFT)
  5822. {
  5823. if (m_bStoIsConnected)
  5824. {
  5825. char tQBuf[MAX_PATH_LEN];
  5826. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID);
  5827. #ifdef DEBUG_ALL
  5828. LOG(tQBuf);
  5829. #endif
  5830. m_pDataStorage->SilentQuery(tQBuf);
  5831. return true;
  5832. };
  5833. return false;
  5834. }
  5835. bool sqliteDBC::updateLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master, uint16_t nOrder, uint16_t nOffset, uint16_t nDelta, uint16_t nWidth, uint16_t nFlip, uint16_t nSun, uint8_t bSST, uint32_t GP, uint32_t spikes)
  5836. {
  5837. if (m_bStoIsConnected)
  5838. {
  5839. char tQBuf[MAX_PATH_LEN];
  5840. if (master)
  5841. {
  5842. sprintf(tQBuf,
  5843. "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,1,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET "
  5844. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5845. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5846. }
  5847. else
  5848. {
  5849. sprintf(tQBuf,
  5850. "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET "
  5851. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5852. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5853. };
  5854. #ifdef DEBUG_ALL
  5855. LOG(tQBuf);
  5856. #endif
  5857. m_pDataStorage->SilentQuery(tQBuf);
  5858. return true;
  5859. };
  5860. return false;
  5861. }
  5862. bool sqliteDBC::updateLRFDryContact(uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow)
  5863. {
  5864. if (m_bStoIsConnected)
  5865. {
  5866. char tQBuf[MAX_PATH_LEN];
  5867. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID);
  5868. #ifdef DEBUG_ALL
  5869. LOG(tQBuf);
  5870. #endif
  5871. m_pDataStorage->SilentQuery(tQBuf);
  5872. return true;
  5873. };
  5874. return false;
  5875. }
  5876. bool sqliteDBC::updateLRFSerial(uint32_t lrfID, CSTR Serial)
  5877. {
  5878. if (m_bStoIsConnected)
  5879. {
  5880. char tQBuf[MAX_PATH_LEN];
  5881. CSTR tES = m_pDataStorage->escapeString(Serial);
  5882. if (tES)
  5883. {
  5884. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID);
  5885. free(tES);
  5886. #ifdef DEBUG_ALL
  5887. LOG(tQBuf);
  5888. #endif
  5889. m_pDataStorage->SilentQuery(tQBuf);
  5890. return true;
  5891. };
  5892. };
  5893. return false;
  5894. }
  5895. bool sqliteDBC::updateLRFUpload(uint32_t id, uint8_t status)
  5896. {
  5897. if (m_bStoIsConnected)
  5898. {
  5899. char tQBuf[MAX_PATH_LEN];
  5900. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id);
  5901. #ifdef DEBUG_ALL
  5902. LOG(tQBuf);
  5903. #endif
  5904. m_pDataStorage->SilentQuery(tQBuf);
  5905. return true;
  5906. };
  5907. return false;
  5908. }
  5909. bool sqliteDBC::updateLRFUnfinishedUploads(uint32_t deviceID)
  5910. {
  5911. if (m_bStoIsConnected)
  5912. {
  5913. char tQBuf[MAX_PATH_LEN];
  5914. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID);
  5915. #ifdef DEBUG_ALL
  5916. LOG(tQBuf);
  5917. #endif
  5918. m_pDataStorage->SilentQuery(tQBuf);
  5919. return true;
  5920. };
  5921. return false;
  5922. }
  5923. bool sqliteDBC::updateLRFVerison(uint32_t lrfID, CSTR version)
  5924. {
  5925. if (m_bStoIsConnected)
  5926. {
  5927. char tQBuf[MAX_PATH_LEN];
  5928. CSTR tES = m_pDataStorage->escapeString(version);
  5929. if (tES)
  5930. {
  5931. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID);
  5932. free(tES);
  5933. #ifdef DEBUG_ALL
  5934. LOG(tQBuf);
  5935. #endif
  5936. m_pDataStorage->SilentQuery(tQBuf);
  5937. return true;
  5938. };
  5939. };
  5940. return false;
  5941. }
  5942. uint32_t sqliteDBC::getRegisteredBubbleId(float X, float Y, float Z)
  5943. {
  5944. uint32_t RetVal = 0;
  5945. if (m_bStoIsConnected)
  5946. {
  5947. char tQBuf[MAX_PATH_LEN];
  5948. sprintf(tQBuf,
  5949. "SELECT `id` FROM `target_bubbles` WHERE ( ABS(`CENTER_X`-(%0.3f)) < `BOUNDARY` ) AND "
  5950. "( ABS(`CENTER_Y`-(%0.3f)) < `BOUNDARY` ) AND "
  5951. "( ABS(`CENTER_Z`-(%0.3f)) < `BOUNDARY` ) LIMIT 1",
  5952. X, Y, Z);
  5953. #ifdef DEBUG
  5954. LOG(tQBuf);
  5955. #endif
  5956. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5957. if (result != NULL)
  5958. {
  5959. if (result->size() > 0)
  5960. {
  5961. Assoc * cArr;
  5962. Assoc::iterator it;
  5963. cArr = result->at(0);
  5964. it = cArr->find("id");
  5965. if (it != cArr->end())
  5966. {
  5967. RetVal = atoi(it->second.c_str());
  5968. };
  5969. };
  5970. };
  5971. deleteResult(result);
  5972. };
  5973. return RetVal;
  5974. }
  5975. uint8_t sqliteDBC::updateRegisteredBubble(TARGET_SEGMENT * ptr)
  5976. {
  5977. uint8_t RetVal = 0;
  5978. if (ptr == NULL)
  5979. {
  5980. return RetVal;
  5981. }
  5982. if (m_bStoIsConnected)
  5983. {
  5984. char tQBuf[MAX_PATH_LEN];
  5985. uint32_t tID = getRegisteredBubbleId(ptr->system_center.x, ptr->system_center.y, ptr->system_center.z);
  5986. if (tID)
  5987. {
  5988. // Old record exists
  5989. sprintf(tQBuf, "UPDATE `target_bubbles` SET `CENTER_X`=%0.3f, `CENTER_Y`=%0.3f, `CENTER_Z`=%0.3f, `BOUNDARY`=%0.3f, `BLOCK_COUNS`=`BLOCK_COUNS`+1 WHERE `id`=%d RETURNING `BLOCK_COUNS`", ptr->system_center.x, ptr->system_center.y,
  5990. ptr->system_center.z, ptr->boundary, tID);
  5991. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5992. if (result != NULL)
  5993. {
  5994. if (result->size() > 0)
  5995. {
  5996. Assoc * cArr;
  5997. Assoc::iterator it;
  5998. cArr = result->at(0);
  5999. it = cArr->find("BLOCK_COUNS");
  6000. if (it != cArr->end())
  6001. {
  6002. RetVal = atoi(it->second.c_str());
  6003. };
  6004. };
  6005. };
  6006. deleteResult(result);
  6007. }
  6008. else
  6009. {
  6010. // Create New Record for Bubble
  6011. sprintf(tQBuf, "INSERT INTO `target_bubbles`(`CENTER_X`,`CENTER_Y`,`CENTER_Z`,`BOUNDARY`,`BLOCK_COUNS`) VALUES(%0.3f,%0.3f,%0.3f,%0.3f,1)", ptr->system_center.x, ptr->system_center.y, ptr->system_center.z, ptr->boundary);
  6012. m_pDataStorage->SilentQuery(tQBuf);
  6013. };
  6014. };
  6015. return RetVal;
  6016. }
  6017. bool sqliteDBC::updateServerIdentity(uint32_t m_nServerID, CSTR m_sIdentity)
  6018. {
  6019. if (m_bStoIsConnected)
  6020. {
  6021. char tQBuf[MAX_PATH_LEN];
  6022. CSTR tES = m_pDataStorage->escapeString(m_sIdentity);
  6023. if (tES)
  6024. {
  6025. sprintf(tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID);
  6026. free(tES);
  6027. #ifdef DEBUG_ALL
  6028. LOG(tQBuf);
  6029. #endif
  6030. m_pDataStorage->SilentQuery(tQBuf);
  6031. return true;
  6032. };
  6033. };
  6034. return false;
  6035. }
  6036. #undef LOG
  6037. #undef LOG_ERROR