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.

6178 lines
153 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
9 hours 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
6 days 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
6 days 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
9 hours 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
6 days 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
9 hours 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
9 hours 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
9 hours 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
9 hours 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
9 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
2 months 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. };
  1840. };
  1841. };
  1842. deleteResult(result);
  1843. };
  1844. return tPtr;
  1845. }
  1846. DBC_MEDIA_STREAM_DATA * sqliteDBC::getMediaStreamData(uint32_t ID)
  1847. {
  1848. DBC_MEDIA_STREAM_DATA * tVal = NULL;
  1849. if (m_bStoIsConnected)
  1850. {
  1851. char tQBuf[CTEXT];
  1852. AAssoc * result;
  1853. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID);
  1854. #ifdef DEBUG_ALL
  1855. LOG(tQBuf);
  1856. #endif
  1857. result = m_pDataStorage->Query(tQBuf);
  1858. if (result != NULL)
  1859. {
  1860. if (result->size() > 0)
  1861. {
  1862. Assoc * cArr;
  1863. tVal = (DBC_MEDIA_STREAM_DATA *) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1864. if (tVal != NULL)
  1865. {
  1866. memset(tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA));
  1867. Assoc::iterator it;
  1868. cArr = result->at(0);
  1869. tVal->id = ID;
  1870. it = cArr->find("source_id");
  1871. if (it != cArr->end())
  1872. {
  1873. tVal->source_id = atoi(it->second.c_str());
  1874. };
  1875. it = cArr->find("source_type");
  1876. if (it != cArr->end())
  1877. {
  1878. sprintf(tVal->source_type, it->second.c_str());
  1879. };
  1880. it = cArr->find("valid");
  1881. if (it != cArr->end())
  1882. {
  1883. tVal->valid = (bool) atoi(it->second.c_str());
  1884. };
  1885. it = cArr->find("token");
  1886. if (it != cArr->end())
  1887. {
  1888. sprintf(tVal->token, it->second.c_str());
  1889. };
  1890. it = cArr->find("img_width");
  1891. if (it != cArr->end())
  1892. {
  1893. tVal->width = atoi(it->second.c_str());
  1894. };
  1895. it = cArr->find("img_height");
  1896. if (it != cArr->end())
  1897. {
  1898. tVal->height = atoi(it->second.c_str());
  1899. };
  1900. it = cArr->find("fps");
  1901. if (it != cArr->end())
  1902. {
  1903. tVal->fps = atoi(it->second.c_str());
  1904. };
  1905. it = cArr->find("compression_type");
  1906. if (it != cArr->end())
  1907. {
  1908. sprintf(tVal->c_type, it->second.c_str());
  1909. };
  1910. it = cArr->find("mrl_proto");
  1911. if (it != cArr->end())
  1912. {
  1913. sprintf(tVal->mrl_proto, it->second.c_str());
  1914. };
  1915. it = cArr->find("mrl_port");
  1916. if (it != cArr->end())
  1917. {
  1918. tVal->mrl_port = atoi(it->second.c_str());
  1919. };
  1920. it = cArr->find("mrl");
  1921. if (it != cArr->end())
  1922. {
  1923. sprintf(tVal->mrl, it->second.c_str());
  1924. };
  1925. };
  1926. };
  1927. };
  1928. deleteResult(result);
  1929. };
  1930. return tVal;
  1931. }
  1932. uint32_t sqliteDBC::getMediaStreamID(uint32_t source_id, CSTR token)
  1933. {
  1934. uint32_t RetVal = 0;
  1935. if (m_bStoIsConnected)
  1936. {
  1937. char tQBuf[CTEXT];
  1938. AAssoc * result;
  1939. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token);
  1940. #ifdef DEBUG_ALL
  1941. LOG(tQBuf);
  1942. #endif
  1943. result = m_pDataStorage->Query(tQBuf);
  1944. if (result != NULL)
  1945. {
  1946. if (result->size() > 0)
  1947. {
  1948. Assoc * cArr;
  1949. Assoc::iterator it;
  1950. cArr = result->at(0);
  1951. it = cArr->find("id");
  1952. if (it != cArr->end())
  1953. {
  1954. RetVal = atoi(it->second.c_str());
  1955. };
  1956. };
  1957. };
  1958. deleteResult(result);
  1959. };
  1960. return RetVal;
  1961. }
  1962. DBC_MODEM_MANAGER_DATA * sqliteDBC::getModemManagerData(uint32_t ID)
  1963. {
  1964. DBC_MODEM_MANAGER_DATA * tVal = NULL;
  1965. if (m_bStoIsConnected)
  1966. {
  1967. char tQBuf[CTEXT];
  1968. AAssoc * result;
  1969. sprintf(tQBuf, "SELECT * FROM `module_modem` WHERE `id`=%d", ID);
  1970. #ifdef DEBUG_ALL
  1971. LOG(tQBuf);
  1972. #endif
  1973. result = m_pDataStorage->Query(tQBuf);
  1974. if (result != NULL)
  1975. {
  1976. if (result->size() > 0)
  1977. {
  1978. Assoc * cArr;
  1979. tVal = (DBC_MODEM_MANAGER_DATA *) malloc(sizeof(DBC_MODEM_MANAGER_DATA));
  1980. if (tVal != NULL)
  1981. {
  1982. memset(tVal, 0, sizeof(DBC_MODEM_MANAGER_DATA));
  1983. tVal->id = ID;
  1984. Assoc::iterator it;
  1985. cArr = result->at(0);
  1986. it = cArr->find("modem_id");
  1987. if (it != cArr->end())
  1988. {
  1989. tVal->modem_id = atoi(it->second.c_str());
  1990. };
  1991. it = cArr->find("evt_mode");
  1992. if (it != cArr->end())
  1993. {
  1994. tVal->evt_mode = atoi(it->second.c_str());
  1995. };
  1996. it = cArr->find("time_window");
  1997. if (it != cArr->end())
  1998. {
  1999. tVal->time_window = atoi(it->second.c_str());
  2000. };
  2001. it = cArr->find("sms_enabled");
  2002. if (it != cArr->end())
  2003. {
  2004. tVal->sms_enabled = (bool) atoi(it->second.c_str());
  2005. };
  2006. it = cArr->find("call_enabled");
  2007. if (it != cArr->end())
  2008. {
  2009. tVal->call_enabled = (bool) atoi(it->second.c_str());
  2010. };
  2011. it = cArr->find("call_hangup_wait");
  2012. if (it != cArr->end())
  2013. {
  2014. tVal->call_hangup_wait = atoi(it->second.c_str());
  2015. };
  2016. };
  2017. };
  2018. };
  2019. deleteResult(result);
  2020. };
  2021. return tVal;
  2022. }
  2023. DBC_MODEM_PHONEBOOK_RECORD * sqliteDBC::getModemPhonebookRecord(uint32_t ID)
  2024. {
  2025. DBC_MODEM_PHONEBOOK_RECORD * tVal = NULL;
  2026. if (m_bStoIsConnected)
  2027. {
  2028. char tQBuf[CTEXT];
  2029. AAssoc * result;
  2030. sprintf(tQBuf, "SELECT * FROM `module_modem_phonebook` WHERE `id`=%d", ID);
  2031. #ifdef DEBUG_ALL
  2032. LOG(tQBuf);
  2033. #endif
  2034. result = m_pDataStorage->Query(tQBuf);
  2035. if (result != NULL)
  2036. {
  2037. if (result->size() > 0)
  2038. {
  2039. Assoc * cArr;
  2040. tVal = (DBC_MODEM_PHONEBOOK_RECORD *) malloc(sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  2041. if (tVal != NULL)
  2042. {
  2043. memset(tVal, 0, sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  2044. Assoc::iterator it;
  2045. cArr = result->at(0);
  2046. it = cArr->find("id");
  2047. if (it != cArr->end())
  2048. {
  2049. tVal->id = atoi(it->second.c_str());
  2050. };
  2051. it = cArr->find("number");
  2052. if (it != cArr->end())
  2053. {
  2054. strncpy(tVal->number, it->second.c_str(), 63);
  2055. tVal->number[63] = (char) 0;
  2056. };
  2057. it = cArr->find("mode");
  2058. if (it != cArr->end())
  2059. {
  2060. tVal->mode = atoi(it->second.c_str());
  2061. };
  2062. it = cArr->find("trig_special");
  2063. if (it != cArr->end())
  2064. {
  2065. tVal->trigger_special = atoi(it->second.c_str());
  2066. };
  2067. it = cArr->find("label");
  2068. if (it != cArr->end())
  2069. {
  2070. strncpy(tVal->label, it->second.c_str(), 63);
  2071. tVal->label[63] = (char) 0;
  2072. };
  2073. it = cArr->find("custom_msg");
  2074. if (it != cArr->end())
  2075. {
  2076. strncpy(tVal->custom_msg, it->second.c_str(), 127);
  2077. tVal->custom_msg[127] = (char) 0;
  2078. };
  2079. };
  2080. };
  2081. };
  2082. deleteResult(result);
  2083. };
  2084. return tVal;
  2085. }
  2086. DBC_MODULE_DATA * sqliteDBC::getModuleData(uint32_t ID)
  2087. {
  2088. DBC_MODULE_DATA * tVal = NULL;
  2089. if (m_bStoIsConnected)
  2090. {
  2091. char tQBuf[CTEXT];
  2092. AAssoc * result;
  2093. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID);
  2094. #ifdef DEBUG_ALL
  2095. LOG(tQBuf);
  2096. #endif
  2097. result = m_pDataStorage->Query(tQBuf);
  2098. if (result != NULL)
  2099. {
  2100. if (result->size() > 0)
  2101. {
  2102. Assoc * cArr;
  2103. tVal = (DBC_MODULE_DATA *) malloc(sizeof(DBC_MODULE_DATA));
  2104. if (tVal != NULL)
  2105. {
  2106. Assoc::iterator it;
  2107. cArr = result->at(0);
  2108. it = cArr->find("id");
  2109. if (it != cArr->end())
  2110. {
  2111. tVal->module_id = atoi(it->second.c_str());
  2112. };
  2113. it = cArr->find("aux_id");
  2114. if (it != cArr->end())
  2115. {
  2116. tVal->aux_id = atoi(it->second.c_str());
  2117. };
  2118. it = cArr->find("type");
  2119. if (it != cArr->end())
  2120. {
  2121. tVal->type = atoi(it->second.c_str());
  2122. };
  2123. it = cArr->find("enabled");
  2124. if (it != cArr->end())
  2125. {
  2126. tVal->enabled = (bool) atoi(it->second.c_str());
  2127. };
  2128. };
  2129. };
  2130. };
  2131. deleteResult(result);
  2132. };
  2133. return tVal;
  2134. }
  2135. DBC_LRF_INI * sqliteDBC::getINIData(uint32_t lrfID)
  2136. {
  2137. DBC_LRF_INI * tVal = NULL;
  2138. if (m_bStoIsConnected)
  2139. {
  2140. char tQBuf[CTEXT];
  2141. AAssoc * result;
  2142. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID);
  2143. #ifdef DEBUG_ALL
  2144. LOG(tQBuf);
  2145. #endif
  2146. result = m_pDataStorage->Query(tQBuf);
  2147. if (result != NULL)
  2148. {
  2149. if (result->size() > 0)
  2150. {
  2151. Assoc * cArr;
  2152. tVal = (DBC_LRF_INI *) malloc(sizeof(DBC_LRF_INI));
  2153. if (tVal != NULL)
  2154. {
  2155. memset(tVal, 0, sizeof(DBC_LRF_INI));
  2156. Assoc::iterator it;
  2157. cArr = result->at(0);
  2158. it = cArr->find("AUTORESET_ENABLE");
  2159. if (it != cArr->end())
  2160. {
  2161. tVal->_AUTO_RESET_ENABLE = (bool) atoi(it->second.c_str());
  2162. };
  2163. it = cArr->find("AUTORESET_INTERLEAVE");
  2164. if (it != cArr->end())
  2165. {
  2166. tVal->_AUTO_RESET_INTERLEAVE = atoi(it->second.c_str());
  2167. };
  2168. it = cArr->find("AUTORESET_TIMEOUT");
  2169. if (it != cArr->end())
  2170. {
  2171. tVal->_AUTO_RESET_TIMEOUT = atoi(it->second.c_str());
  2172. };
  2173. it = cArr->find("BOARD_TEMPERATURE");
  2174. if (it != cArr->end())
  2175. {
  2176. tVal->_BOARD_TEMPERATURE = atoi(it->second.c_str());
  2177. };
  2178. it = cArr->find("BOARD_ZEROSHIFT");
  2179. if (it != cArr->end())
  2180. {
  2181. tVal->_BOARD_ZEROSHIFT = atoi(it->second.c_str());
  2182. };
  2183. it = cArr->find("BOARD_SPEED");
  2184. if (it != cArr->end())
  2185. {
  2186. tVal->_BOARD_SPEED = atoi(it->second.c_str());
  2187. };
  2188. it = cArr->find("RUN_DIR");
  2189. if (it != cArr->end())
  2190. {
  2191. tVal->_RUN_DIR = atoi(it->second.c_str());
  2192. };
  2193. it = cArr->find("NO_BLACK_TARGETS");
  2194. if (it != cArr->end())
  2195. {
  2196. tVal->_NO_BLACK_TARGETS = atoi(it->second.c_str());
  2197. };
  2198. it = cArr->find("JUST_MOVING_TARGETS");
  2199. if (it != cArr->end())
  2200. {
  2201. tVal->_JUST_MOVING_TARGETS = (bool) atoi(it->second.c_str());
  2202. };
  2203. it = cArr->find("JUST_MOVING_RADIAL");
  2204. if (it != cArr->end())
  2205. {
  2206. tVal->_JUST_MOVING_RADIAL = (bool) atoi(it->second.c_str());
  2207. };
  2208. it = cArr->find("JUST_MOVING_DISTANCE");
  2209. if (it != cArr->end())
  2210. {
  2211. tVal->_JUST_MOVING_DISTANCE = (bool) atoi(it->second.c_str());
  2212. };
  2213. it = cArr->find("FREEZ_FIRST_TH");
  2214. if (it != cArr->end())
  2215. {
  2216. tVal->_FREEZ_FIRST_TH = (bool) atoi(it->second.c_str());
  2217. };
  2218. it = cArr->find("SAVE_FIRST_TH");
  2219. if (it != cArr->end())
  2220. {
  2221. tVal->_SAVE_FIRST_TH = (bool) atoi(it->second.c_str());
  2222. };
  2223. };
  2224. };
  2225. };
  2226. deleteResult(result);
  2227. };
  2228. return tVal;
  2229. }
  2230. bool sqliteDBC::getEventSessionStatus()
  2231. {
  2232. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  2233. bool rv = false;
  2234. if (m_bRepIsConnected)
  2235. {
  2236. char tQBuf[CTEXT];
  2237. AAssoc * result;
  2238. // Since Unimodule -> no need to check NodeID
  2239. // 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,
  2240. // EVT_LRF_END_SESSION_TO );
  2241. 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);
  2242. #ifdef DEBUG_ALL
  2243. LOG(tQBuf);
  2244. #endif
  2245. result = m_pDataReports->Query(tQBuf);
  2246. if (result != NULL)
  2247. {
  2248. if (result->size() > 0)
  2249. {
  2250. Assoc * cArr;
  2251. Assoc::iterator it;
  2252. cArr = result->at(0);
  2253. it = cArr->find("evt_type");
  2254. if (it != cArr->end())
  2255. {
  2256. if (atoi(it->second.c_str()) == EVT_LRF_START_SESSION)
  2257. {
  2258. rv = true;
  2259. }
  2260. };
  2261. };
  2262. };
  2263. deleteResult(result);
  2264. };
  2265. return rv;
  2266. }
  2267. /*
  2268. * The Last TS is from the current report (as latest)
  2269. */
  2270. uint32_t sqliteDBC::getLastEventTS()
  2271. {
  2272. ReportGuard tReportGuard(m_mReportLock); // see switchReportToCurrentHour()
  2273. uint32_t rv = 0;
  2274. if (m_bRepIsConnected)
  2275. {
  2276. char tQBuf[CTEXT];
  2277. AAssoc * result;
  2278. sprintf(tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1");
  2279. #ifdef DEBUG_ALL
  2280. LOG(tQBuf);
  2281. #endif
  2282. result = m_pDataReports->Query(tQBuf);
  2283. if (result != NULL)
  2284. {
  2285. if (result->size() > 0)
  2286. {
  2287. Assoc * cArr;
  2288. Assoc::iterator it;
  2289. cArr = result->at(0);
  2290. it = cArr->find("ts");
  2291. if (it != cArr->end())
  2292. {
  2293. rv = atoi(it->second.c_str());
  2294. };
  2295. };
  2296. };
  2297. deleteResult(result);
  2298. };
  2299. return rv;
  2300. }
  2301. /*
  2302. * The first TS is generaly must show a DAY of first registered event so TS fetched
  2303. * from general statistics
  2304. */
  2305. uint32_t sqliteDBC::getFirstEventTS()
  2306. {
  2307. uint32_t rv = 0;
  2308. if (m_bStaIsConnected)
  2309. {
  2310. char tQBuf[CTEXT];
  2311. AAssoc * result;
  2312. sprintf(tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1");
  2313. #ifdef DEBUG_ALL
  2314. LOG(tQBuf);
  2315. #endif
  2316. result = m_pDataStatistics->Query(tQBuf);
  2317. if (result != NULL)
  2318. {
  2319. if (result->size() > 0)
  2320. {
  2321. REPORT_DIRINFO RDF;// = {0};
  2322. memset(&RDF, 0, sizeof(REPORT_DIRINFO));
  2323. Assoc * cArr;
  2324. Assoc::iterator it;
  2325. cArr = result->at(0);
  2326. it = cArr->find("year");
  2327. if (it != cArr->end())
  2328. {
  2329. strncpy(RDF.YEAR, it->second.c_str(), 4);
  2330. };
  2331. it = cArr->find("month");
  2332. if (it != cArr->end())
  2333. {
  2334. strncpy(RDF.MONTH, it->second.c_str(), 2);
  2335. };
  2336. it = cArr->find("day");
  2337. if (it != cArr->end())
  2338. {
  2339. strncpy(RDF.DAY, it->second.c_str(), 2);
  2340. };
  2341. rv = getTsFromRDF(RDF);
  2342. };
  2343. };
  2344. deleteResult(result);
  2345. };
  2346. return rv;
  2347. }
  2348. /*
  2349. Called from the kernel loop (validateReports: daily cleanup, or every second while the disk is above
  2350. preserve_empty). deleteRDFDir() runs "rm -f -R <whole day>" - many seconds (minutes on a big SD card):
  2351. the kernel loop stalled and the watchdog killed la_mojo. The day is deleted in a background thread;
  2352. while it runs, further calls return false (the oldest day is still listed until it is done).
  2353. */
  2354. bool sqliteDBC::deleteReportDate(uint32_t ts)
  2355. {
  2356. if (ts)
  2357. {
  2358. bool tFalse = false;
  2359. if (! s_bReportDayDeleting.compare_exchange_strong(tFalse, true))
  2360. {
  2361. return false; // a day is being deleted
  2362. };
  2363. REPORT_DIRINFO tRDF;// = {0};
  2364. fillRDF(ts, tRDF);
  2365. std::string tBase(m_pDbReportBase);
  2366. LOG("Delete RDF Dir (background)");
  2367. try
  2368. {
  2369. std::thread([this, tBase, tRDF]() mutable {
  2370. pthread_setname_np(pthread_self(), "ReportDelete");
  2371. if (deleteRDFDir((CSTR) tBase.c_str(), tRDF))
  2372. {
  2373. LOG("Dir Deleted OK");
  2374. deleteReportStat(tRDF);
  2375. };
  2376. s_bReportDayDeleting = false;
  2377. }).detach();
  2378. }
  2379. catch (const std::exception & e)
  2380. {
  2381. s_bReportDayDeleting = false;
  2382. LOG_ERROR("Can not start the report delete thread");
  2383. return false;
  2384. };
  2385. return true;
  2386. };
  2387. return false;
  2388. }
  2389. DBC_LRF_DATA * sqliteDBC::getLRFData(uint32_t ID)
  2390. {
  2391. DBC_LRF_DATA * tVal = NULL;
  2392. if (m_bStoIsConnected)
  2393. {
  2394. char tQBuf[CTEXT];
  2395. AAssoc * result;
  2396. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID);
  2397. #ifdef DEBUG_ALL
  2398. LOG(tQBuf);
  2399. #endif
  2400. result = m_pDataStorage->Query(tQBuf);
  2401. if (result != NULL)
  2402. {
  2403. if (result->size() > 0)
  2404. {
  2405. Assoc * cArr;
  2406. tVal = (DBC_LRF_DATA *) malloc(sizeof(DBC_LRF_DATA));
  2407. if (tVal != NULL)
  2408. {
  2409. memset(tVal, 0, sizeof(DBC_LRF_DATA));
  2410. Assoc::iterator it;
  2411. tVal->id = ID;
  2412. cArr = result->at(0);
  2413. it = cArr->find("data_status");
  2414. if (it != cArr->end())
  2415. {
  2416. tVal->data_status = atoi(it->second.c_str());
  2417. };
  2418. it = cArr->find("serial");
  2419. if (it != cArr->end())
  2420. {
  2421. strncpy(tVal->serial, it->second.c_str(), sizeof(tVal->serial) - 1);
  2422. tVal->serial[sizeof(tVal->serial) - 1] = (char) 0;
  2423. };
  2424. it = cArr->find("version");
  2425. if (it != cArr->end())
  2426. {
  2427. strncpy(tVal->version, it->second.c_str(), sizeof(tVal->version) - 1);
  2428. tVal->version[sizeof(tVal->version) - 1] = (char) 0;
  2429. };
  2430. it = cArr->find("x");
  2431. if (it != cArr->end())
  2432. {
  2433. tVal->x = atof(it->second.c_str());
  2434. };
  2435. it = cArr->find("y");
  2436. if (it != cArr->end())
  2437. {
  2438. tVal->y = atof(it->second.c_str());
  2439. };
  2440. it = cArr->find("z");
  2441. if (it != cArr->end())
  2442. {
  2443. tVal->z = atof(it->second.c_str());
  2444. };
  2445. it = cArr->find("x_rot");
  2446. if (it != cArr->end())
  2447. {
  2448. tVal->x_rot = atof(it->second.c_str());
  2449. };
  2450. it = cArr->find("y_rot");
  2451. if (it != cArr->end())
  2452. {
  2453. tVal->y_rot = atof(it->second.c_str());
  2454. };
  2455. it = cArr->find("z_rot");
  2456. if (it != cArr->end())
  2457. {
  2458. tVal->z_rot = atof(it->second.c_str());
  2459. };
  2460. it = cArr->find("uart_id");
  2461. if (it != cArr->end())
  2462. {
  2463. tVal->uart_id = atoi(it->second.c_str());
  2464. };
  2465. it = cArr->find("device_id");
  2466. if (it != cArr->end())
  2467. {
  2468. tVal->device_id = atoi(it->second.c_str());
  2469. };
  2470. it = cArr->find("ip");
  2471. if (it != cArr->end())
  2472. {
  2473. sprintf(tVal->master_host, it->second.c_str());
  2474. };
  2475. it = cArr->find("aux_ip");
  2476. if (it != cArr->end())
  2477. {
  2478. sprintf(tVal->slave_host, it->second.c_str());
  2479. };
  2480. it = cArr->find("port");
  2481. if (it != cArr->end())
  2482. {
  2483. tVal->master_port = atoi(it->second.c_str());
  2484. };
  2485. it = cArr->find("aux_port");
  2486. if (it != cArr->end())
  2487. {
  2488. tVal->slave_port = atoi(it->second.c_str());
  2489. };
  2490. it = cArr->find("auto_draw");
  2491. if (it != cArr->end())
  2492. {
  2493. tVal->auto_draw = atoi(it->second.c_str());
  2494. };
  2495. it = cArr->find("filter_id");
  2496. if (it != cArr->end())
  2497. {
  2498. tVal->filter_id = atoi(it->second.c_str());
  2499. };
  2500. it = cArr->find("enabled_lrf");
  2501. if (it != cArr->end())
  2502. {
  2503. tVal->enabled_lrf = atoi(it->second.c_str());
  2504. };
  2505. it = cArr->find("max_range");
  2506. if (it != cArr->end())
  2507. {
  2508. tVal->max_range = atof(it->second.c_str());
  2509. };
  2510. it = cArr->find("base_shift");
  2511. if (it != cArr->end())
  2512. {
  2513. tVal->base_shift = atoi(it->second.c_str());
  2514. };
  2515. // it = cArr->find("mirror_shift");
  2516. // if( it != cArr->end() ){
  2517. // tVal->mirror_shift = atoi( it->second.c_str() );
  2518. // };
  2519. it = cArr->find("detection_profile_id");
  2520. if (it != cArr->end())
  2521. {
  2522. tVal->detection_profile_id = atoi(it->second.c_str());
  2523. };
  2524. it = cArr->find("prefered_camera_id");
  2525. if (it != cArr->end())
  2526. {
  2527. tVal->prefered_camera_id = atoi(it->second.c_str());
  2528. };
  2529. /*
  2530. it = cArr->find("draw_lrf_id");
  2531. if( it != cArr->end() ){
  2532. tVal->draw_lrf_id = atoi( it->second.c_str() );
  2533. };
  2534. it = cArr->find("width_table_id");
  2535. if( it != cArr->end() ){
  2536. tVal->width_table_id = atoi( it->second.c_str() );
  2537. };
  2538. it = cArr->find("zoom_table_id");
  2539. if( it != cArr->end() ){
  2540. tVal->zoom_table_id = atoi( it->second.c_str() );
  2541. };
  2542. it = cArr->find("embd_cam");
  2543. if( it != cArr->end() ){
  2544. tVal->embd_cam = atoi( it->second.c_str() );
  2545. };
  2546. it = cArr->find("embd_cam_auto");
  2547. if( it != cArr->end() ){
  2548. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  2549. };
  2550. it = cArr->find("embd_dc");
  2551. if( it != cArr->end() ){
  2552. tVal->embd_dc = atoi( it->second.c_str() );
  2553. };
  2554. it = cArr->find("autoreset");
  2555. if( it != cArr->end() ){
  2556. tVal->autoreset = (bool)atoi( it->second.c_str() );
  2557. };
  2558. it = cArr->find("force_settings");
  2559. if( it != cArr->end() ){
  2560. tVal->force_settings = (bool)atoi( it->second.c_str() );
  2561. };
  2562. */
  2563. };
  2564. };
  2565. };
  2566. deleteResult(result);
  2567. };
  2568. return tVal;
  2569. }
  2570. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master)
  2571. {
  2572. PLGS_2D_PARAMETERS retVal = NULL;
  2573. if (m_bStoIsConnected)
  2574. {
  2575. char tQBuf[CTEXT];
  2576. AAssoc * result;
  2577. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID, (master) ? 1 : 0);
  2578. #ifdef DEBUG_ALL
  2579. LOG(tQBuf);
  2580. #endif
  2581. result = m_pDataStorage->Query(tQBuf);
  2582. if (result != NULL)
  2583. {
  2584. if (result->size() > 0)
  2585. {
  2586. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  2587. memset(retVal, 0, sizeof(LGS_2D_PARAMETERS));
  2588. Assoc * cArr;
  2589. Assoc::iterator it;
  2590. cArr = result->at(0);
  2591. it = cArr->find("order");
  2592. if (it != cArr->end())
  2593. {
  2594. retVal->nOrder = atoi(it->second.c_str());
  2595. };
  2596. it = cArr->find("offset");
  2597. if (it != cArr->end())
  2598. {
  2599. retVal->nOffset = atoi(it->second.c_str());
  2600. };
  2601. it = cArr->find("delta");
  2602. if (it != cArr->end())
  2603. {
  2604. retVal->nDelta = atoi(it->second.c_str());
  2605. };
  2606. it = cArr->find("flip");
  2607. if (it != cArr->end())
  2608. {
  2609. retVal->nFlip = atoi(it->second.c_str());
  2610. };
  2611. it = cArr->find("width");
  2612. if (it != cArr->end())
  2613. {
  2614. retVal->nWidth = atoi(it->second.c_str());
  2615. };
  2616. it = cArr->find("sst");
  2617. if (it != cArr->end())
  2618. {
  2619. retVal->bSST = atoi(it->second.c_str());
  2620. };
  2621. it = cArr->find("sun");
  2622. if (it != cArr->end())
  2623. {
  2624. retVal->nSun = atoi(it->second.c_str());
  2625. };
  2626. };
  2627. };
  2628. deleteResult(result);
  2629. };
  2630. return retVal;
  2631. }
  2632. RELAY_PARAMETERS * sqliteDBC::getLRFDryContact(uint32_t lrfID)
  2633. {
  2634. RELAY_PARAMETERS * retVal = NULL;
  2635. if (m_bStoIsConnected)
  2636. {
  2637. char tQBuf[CTEXT];
  2638. AAssoc * result;
  2639. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID);
  2640. #ifdef DEBUG_ALL
  2641. LOG(tQBuf);
  2642. #endif
  2643. result = m_pDataStorage->Query(tQBuf);
  2644. if (result != NULL)
  2645. {
  2646. if (result->size() > 0)
  2647. {
  2648. retVal = (RELAY_PARAMETERS *) malloc(sizeof(RELAY_PARAMETERS));
  2649. if (retVal)
  2650. {
  2651. Assoc * cArr;
  2652. Assoc::iterator it;
  2653. cArr = result->at(0);
  2654. it = cArr->find("dc_auto");
  2655. if (it != cArr->end())
  2656. {
  2657. retVal->bAuto = atoi(it->second.c_str());
  2658. };
  2659. it = cArr->find("dc_no");
  2660. if (it != cArr->end())
  2661. {
  2662. retVal->bNormallyClose = (atoi(it->second.c_str()) == 0);
  2663. };
  2664. it = cArr->find("dc_window");
  2665. if (it != cArr->end())
  2666. {
  2667. retVal->nAutoDelay = atoi(it->second.c_str());
  2668. };
  2669. };
  2670. };
  2671. };
  2672. deleteResult(result);
  2673. };
  2674. return retVal;
  2675. }
  2676. uint8_t sqliteDBC::getLRFDataStatus(uint32_t ID)
  2677. {
  2678. uint8_t RetVal = 0;
  2679. if (m_bStoIsConnected)
  2680. {
  2681. char tQBuf[CTEXT];
  2682. AAssoc * result;
  2683. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID);
  2684. #ifdef DEBUG_ALL
  2685. LOG(tQBuf);
  2686. #endif
  2687. result = m_pDataStorage->Query(tQBuf);
  2688. if (result != NULL)
  2689. {
  2690. if (result->size() > 0)
  2691. {
  2692. Assoc * cArr;
  2693. Assoc::iterator it;
  2694. cArr = result->at(0);
  2695. it = cArr->find("data_status");
  2696. if (it != cArr->end())
  2697. {
  2698. RetVal = atoi(it->second.c_str());
  2699. };
  2700. };
  2701. };
  2702. deleteResult(result);
  2703. };
  2704. return RetVal;
  2705. }
  2706. uint32_t sqliteDBC::getLRFMaskID(uint32_t deviceID, uint8_t lrfID, uint8_t layer)
  2707. {
  2708. uint32_t RetVal = 0;
  2709. if (m_bStoIsConnected)
  2710. {
  2711. char tQBuf[CTEXT];
  2712. AAssoc * result;
  2713. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer);
  2714. #ifdef DEBUG_ALL
  2715. LOG(tQBuf);
  2716. #endif
  2717. result = m_pDataStorage->Query(tQBuf);
  2718. if (result != NULL)
  2719. {
  2720. if (result->size() > 0)
  2721. {
  2722. Assoc * cArr;
  2723. Assoc::iterator it;
  2724. cArr = result->at(0);
  2725. it = cArr->find("id");
  2726. if (it != cArr->end())
  2727. {
  2728. RetVal = atoi(it->second.c_str());
  2729. };
  2730. };
  2731. };
  2732. deleteResult(result);
  2733. };
  2734. return RetVal;
  2735. }
  2736. uint32_t sqliteDBC::getLRFMaskPolygoneID(uint32_t maskID, uint8_t vertecID)
  2737. {
  2738. uint32_t RetVal = 0;
  2739. if (m_bStoIsConnected)
  2740. {
  2741. char tQBuf[CTEXT];
  2742. AAssoc * result;
  2743. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID);
  2744. #ifdef DEBUG_ALL
  2745. LOG(tQBuf);
  2746. #endif
  2747. result = m_pDataStorage->Query(tQBuf);
  2748. if (result != NULL)
  2749. {
  2750. if (result->size() > 0)
  2751. {
  2752. Assoc * cArr;
  2753. Assoc::iterator it;
  2754. cArr = result->at(0);
  2755. it = cArr->find("id");
  2756. if (it != cArr->end())
  2757. {
  2758. RetVal = atoi(it->second.c_str());
  2759. };
  2760. };
  2761. };
  2762. deleteResult(result);
  2763. };
  2764. return RetVal;
  2765. }
  2766. uint8_t sqliteDBC::getLRFMasksStatus(uint32_t deviceID)
  2767. {
  2768. uint8_t RetVal = 0;
  2769. if (m_bStoIsConnected)
  2770. {
  2771. char tQBuf[CTEXT];
  2772. AAssoc * result;
  2773. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID);
  2774. #ifdef DEBUG_ALL
  2775. LOG(tQBuf);
  2776. #endif
  2777. result = m_pDataStorage->Query(tQBuf);
  2778. if (result != NULL)
  2779. {
  2780. if (result->size() > 0)
  2781. {
  2782. Assoc * cArr;
  2783. Assoc::iterator it;
  2784. for (size_t i = 0; i < result->size(); i++)
  2785. {
  2786. cArr = result->at(i);
  2787. it = cArr->find("layer");
  2788. if (it != cArr->end())
  2789. {
  2790. RetVal |= (1 << atoi(it->second.c_str()));
  2791. };
  2792. };
  2793. };
  2794. };
  2795. deleteResult(result);
  2796. };
  2797. return RetVal;
  2798. }
  2799. DBC_LRF_UPLOAD_DATA * sqliteDBC::getLRFUploadData(uint32_t upID)
  2800. {
  2801. DBC_LRF_UPLOAD_DATA * RetVal = NULL;
  2802. if (m_bStoIsConnected)
  2803. {
  2804. char tQBuf[CTEXT];
  2805. AAssoc * result;
  2806. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID);
  2807. #ifdef DEBUG_ALL
  2808. LOG(tQBuf);
  2809. #endif
  2810. result = m_pDataStorage->Query(tQBuf);
  2811. if (result->size() > 0)
  2812. {
  2813. Assoc * cArr;
  2814. RetVal = (DBC_LRF_UPLOAD_DATA *) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2815. if (RetVal != NULL)
  2816. {
  2817. memset(RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2818. Assoc::iterator it;
  2819. RetVal->id = upID;
  2820. cArr = result->at(0);
  2821. it = cArr->find("md5");
  2822. if (it != cArr->end())
  2823. {
  2824. strncpy(RetVal->md5, it->second.c_str(), 32);
  2825. };
  2826. it = cArr->find("file");
  2827. if (it != cArr->end())
  2828. {
  2829. strncpy(RetVal->fname, it->second.c_str(), 16);
  2830. };
  2831. it = cArr->find("issue_reset");
  2832. if (it != cArr->end())
  2833. {
  2834. RetVal->transfer_mode = atoi(it->second.c_str());
  2835. };
  2836. };
  2837. };
  2838. deleteResult(result);
  2839. };
  2840. return RetVal;
  2841. }
  2842. DBC_WIDTH_TABLE * sqliteDBC::getLRFWidthTable(uint32_t lrfID)
  2843. {
  2844. DBC_WIDTH_TABLE * retVal = NULL;
  2845. if (m_bStoIsConnected)
  2846. {
  2847. retVal = (DBC_WIDTH_TABLE *) malloc(sizeof(DBC_WIDTH_TABLE));
  2848. if (retVal)
  2849. {
  2850. char tQBuf[CTEXT];
  2851. AAssoc * result;
  2852. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID);
  2853. #ifdef DEBUG_ALL
  2854. LOG(tQBuf);
  2855. #endif
  2856. memset(retVal, 0, sizeof(DBC_WIDTH_TABLE));
  2857. result = m_pDataStorage->Query(tQBuf);
  2858. if (result != NULL)
  2859. {
  2860. if (result->size() > 0)
  2861. {
  2862. retVal->count = result->size();
  2863. retVal->wt_records = (DBC_WT_RECORD *) calloc(retVal->count, sizeof(DBC_WT_RECORD));
  2864. if (retVal->wt_records)
  2865. {
  2866. Assoc * cArr;
  2867. Assoc::iterator it;
  2868. for (int i = 0; i < retVal->count; i++)
  2869. {
  2870. cArr = result->at(i);
  2871. it = cArr->find("range_k");
  2872. if (it != cArr->end())
  2873. {
  2874. retVal->wt_records[i].rangePercent = atoi(it->second.c_str());
  2875. };
  2876. it = cArr->find("width_k");
  2877. if (it != cArr->end())
  2878. {
  2879. retVal->wt_records[i].widthPercent = atoi(it->second.c_str());
  2880. };
  2881. };
  2882. }
  2883. else
  2884. {
  2885. // retVal->count = 0;
  2886. LOG_ERROR("No Memory To create WT Record!");
  2887. free(retVal);
  2888. retVal = NULL;
  2889. };
  2890. };
  2891. };
  2892. deleteResult(result);
  2893. }
  2894. else
  2895. {
  2896. LOG_ERROR("No Memory To create WT!");
  2897. };
  2898. };
  2899. return retVal;
  2900. }
  2901. uint32_t sqliteDBC::getNodeAuxParam(uint32_t NodeID)
  2902. {
  2903. uint32_t retVal = 0;
  2904. if (m_bStoIsConnected)
  2905. {
  2906. char tQBuf[CTEXT];
  2907. AAssoc * result;
  2908. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID);
  2909. #ifdef DEBUG_ALL
  2910. LOG(tQBuf);
  2911. #endif
  2912. result = m_pDataStorage->Query(tQBuf);
  2913. if (result != NULL)
  2914. {
  2915. if (result->size() > 0)
  2916. {
  2917. Assoc * cArr;
  2918. Assoc::iterator it;
  2919. cArr = result->at(0);
  2920. it = cArr->find("aux_parameter");
  2921. if (it != cArr->end())
  2922. {
  2923. retVal = atoi(it->second.c_str());
  2924. };
  2925. };
  2926. };
  2927. deleteResult(result);
  2928. };
  2929. return retVal;
  2930. }
  2931. DBC_NODE_DATA * sqliteDBC::getNodeData(uint32_t ID)
  2932. {
  2933. DBC_NODE_DATA * tVal = NULL;
  2934. if (m_bStoIsConnected)
  2935. {
  2936. char tQBuf[CTEXT];
  2937. AAssoc * result;
  2938. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID);
  2939. #ifdef DEBUG_ALL
  2940. LOG(tQBuf);
  2941. #endif
  2942. result = m_pDataStorage->Query(tQBuf);
  2943. if (result != NULL)
  2944. {
  2945. if (result->size() > 0)
  2946. {
  2947. Assoc * cArr;
  2948. tVal = (DBC_NODE_DATA *) malloc(sizeof(DBC_NODE_DATA));
  2949. if (tVal != NULL)
  2950. {
  2951. memset(tVal, 0, sizeof(DBC_NODE_DATA));
  2952. Assoc::iterator it;
  2953. tVal->node_id = ID;
  2954. cArr = result->at(0);
  2955. it = cArr->find("type");
  2956. if (it != cArr->end())
  2957. {
  2958. tVal->type = /*(NODE_TYPE)*/ atoi(it->second.c_str());
  2959. };
  2960. it = cArr->find("aux_id");
  2961. if (it != cArr->end())
  2962. {
  2963. tVal->aux_id = atoi(it->second.c_str());
  2964. };
  2965. it = cArr->find("aux_parameter");
  2966. if (it != cArr->end())
  2967. {
  2968. tVal->aux_parameter = atoi(it->second.c_str());
  2969. };
  2970. it = cArr->find("online");
  2971. if (it != cArr->end())
  2972. {
  2973. tVal->online = (bool) atoi(it->second.c_str());
  2974. };
  2975. it = cArr->find("enable");
  2976. if (it != cArr->end())
  2977. {
  2978. tVal->enabled = (bool) atoi(it->second.c_str());
  2979. };
  2980. it = cArr->find("name");
  2981. if (it != cArr->end())
  2982. {
  2983. sprintf(tVal->name, it->second.c_str());
  2984. };
  2985. };
  2986. };
  2987. };
  2988. deleteResult(result);
  2989. };
  2990. return tVal;
  2991. }
  2992. uint32_t sqliteDBC::getNodeIdByTypeAndAux(uint8_t type, uint32_t aux_id)
  2993. {
  2994. uint32_t retVal = 0;
  2995. if (m_bStoIsConnected)
  2996. {
  2997. char tQBuf[CTEXT];
  2998. AAssoc * result;
  2999. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  3000. #ifdef DEBUG_ALL
  3001. LOG(tQBuf);
  3002. #endif
  3003. result = m_pDataStorage->Query(tQBuf);
  3004. if (result != NULL)
  3005. {
  3006. if (result->size() > 0)
  3007. {
  3008. Assoc * cArr;
  3009. Assoc::iterator it;
  3010. cArr = result->at(0);
  3011. it = cArr->find("id");
  3012. if (it != cArr->end())
  3013. {
  3014. retVal = atoi(it->second.c_str());
  3015. };
  3016. };
  3017. };
  3018. deleteResult(result);
  3019. };
  3020. return retVal;
  3021. }
  3022. CSTR sqliteDBC::getNodeName(uint32_t NodeID)
  3023. {
  3024. CSTR retVal = NULL;
  3025. if (m_bStoIsConnected)
  3026. {
  3027. char tQBuf[CTEXT];
  3028. AAssoc * result;
  3029. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID);
  3030. #ifdef DEBUG_ALL
  3031. LOG(tQBuf);
  3032. #endif
  3033. result = m_pDataStorage->Query(tQBuf);
  3034. if (result != NULL)
  3035. {
  3036. if (result->size() > 0)
  3037. {
  3038. Assoc * cArr;
  3039. Assoc::iterator it;
  3040. cArr = result->at(0);
  3041. it = cArr->find("name");
  3042. if (it != cArr->end())
  3043. {
  3044. retVal = strdup(it->second.c_str());
  3045. };
  3046. };
  3047. };
  3048. deleteResult(result);
  3049. };
  3050. return retVal;
  3051. }
  3052. DBC_NET_IF_DATA * sqliteDBC::getNIF_Data(uint32_t nif_id)
  3053. {
  3054. DBC_NET_IF_DATA * tVal = NULL;
  3055. if (m_bStoIsConnected)
  3056. {
  3057. char tQBuf[CTEXT];
  3058. AAssoc * result;
  3059. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id);
  3060. #ifdef DEBUG_ALL
  3061. LOG(tQBuf);
  3062. #endif
  3063. result = m_pDataStorage->Query(tQBuf);
  3064. if (result != NULL)
  3065. {
  3066. if (result->size() > 0)
  3067. {
  3068. Assoc * cArr;
  3069. tVal = (DBC_NET_IF_DATA *) malloc(sizeof(DBC_NET_IF_DATA));
  3070. if (tVal != NULL)
  3071. {
  3072. Assoc::iterator it;
  3073. tVal->id = nif_id;
  3074. cArr = result->at(0);
  3075. it = cArr->find("type");
  3076. if (it != cArr->end())
  3077. {
  3078. tVal->type = atoi(it->second.c_str());
  3079. };
  3080. it = cArr->find("mode");
  3081. if (it != cArr->end())
  3082. {
  3083. tVal->mode = atoi(it->second.c_str());
  3084. };
  3085. it = cArr->find("device_name");
  3086. if (it != cArr->end())
  3087. {
  3088. strncpy(tVal->device_name, it->second.c_str(), 11);
  3089. tVal->device_name[10] = (char) 0;
  3090. };
  3091. it = cArr->find("mac");
  3092. if (it != cArr->end())
  3093. {
  3094. strncpy(tVal->mac, it->second.c_str(), 33);
  3095. tVal->mac[32] = (char) 0;
  3096. };
  3097. it = cArr->find("aux_id");
  3098. if (it != cArr->end())
  3099. {
  3100. tVal->aux_id = atoi(it->second.c_str());
  3101. };
  3102. };
  3103. };
  3104. };
  3105. deleteResult(result);
  3106. };
  3107. return tVal;
  3108. }
  3109. uint32_t sqliteDBC::getNIF_ID(CSTR key)
  3110. {
  3111. uint32_t tVal = 0;
  3112. if (m_bStoIsConnected)
  3113. {
  3114. char tQBuf[CTEXT];
  3115. AAssoc * result;
  3116. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key);
  3117. #ifdef DEBUG_ALL
  3118. LOG(tQBuf);
  3119. #endif
  3120. result = m_pDataStorage->Query(tQBuf);
  3121. if (result != NULL)
  3122. {
  3123. if (result->size() > 0)
  3124. {
  3125. Assoc * cArr;
  3126. Assoc::iterator it;
  3127. cArr = result->at(0);
  3128. it = cArr->find("id");
  3129. if (it != cArr->end())
  3130. {
  3131. tVal = atoi(it->second.c_str());
  3132. };
  3133. };
  3134. };
  3135. deleteResult(result);
  3136. };
  3137. return tVal;
  3138. }
  3139. uint32_t sqliteDBC::getNIF_IDbyDevice(CSTR device)
  3140. {
  3141. uint32_t tVal = 0;
  3142. if (m_bStoIsConnected)
  3143. {
  3144. char tQBuf[CTEXT];
  3145. AAssoc * result;
  3146. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device);
  3147. #ifdef DEBUG_ALL
  3148. LOG(tQBuf);
  3149. #endif
  3150. result = m_pDataStorage->Query(tQBuf);
  3151. if (result != NULL)
  3152. {
  3153. if (result->size() > 0)
  3154. {
  3155. Assoc * cArr;
  3156. Assoc::iterator it;
  3157. cArr = result->at(0);
  3158. it = cArr->find("id");
  3159. if (it != cArr->end())
  3160. {
  3161. tVal = atoi(it->second.c_str());
  3162. };
  3163. };
  3164. };
  3165. deleteResult(result);
  3166. };
  3167. return tVal;
  3168. }
  3169. DBC_NETWORK_DATA * sqliteDBC::getNetworkData(uint32_t net_id)
  3170. {
  3171. DBC_NETWORK_DATA * tVal = NULL;
  3172. if (m_bStoIsConnected)
  3173. {
  3174. char tQBuf[CTEXT];
  3175. AAssoc * result;
  3176. sprintf(tQBuf, "SELECT * FROM `networks` WHERE `id`=%d", net_id);
  3177. #ifdef DEBUG
  3178. LOG(tQBuf);
  3179. #endif
  3180. result = m_pDataStorage->Query(tQBuf);
  3181. if (result != NULL)
  3182. {
  3183. if (result->size() > 0)
  3184. {
  3185. tVal = (DBC_NETWORK_DATA *) malloc(sizeof(DBC_NETWORK_DATA));
  3186. if (tVal)
  3187. {
  3188. Assoc * cArr;
  3189. Assoc::iterator it;
  3190. memset(tVal, 0, sizeof(DBC_NETWORK_DATA));
  3191. tVal->id = net_id;
  3192. cArr = result->at(0);
  3193. it = cArr->find("nif_id");
  3194. if (it != cArr->end())
  3195. {
  3196. tVal->nif_id = atoi(it->second.c_str());
  3197. };
  3198. it = cArr->find("type");
  3199. if (it != cArr->end())
  3200. {
  3201. tVal->type = (NETWORK_TYPE) atoi(it->second.c_str());
  3202. };
  3203. it = cArr->find("address");
  3204. if (it != cArr->end())
  3205. {
  3206. strncpy(tVal->address, it->second.c_str(), 64);
  3207. tVal->address[64] = (char) 0;
  3208. };
  3209. it = cArr->find("mask");
  3210. if (it != cArr->end())
  3211. {
  3212. strncpy(tVal->netmask, it->second.c_str(), 64);
  3213. tVal->netmask[64] = (char) 0;
  3214. };
  3215. it = cArr->find("broadcast");
  3216. if (it != cArr->end())
  3217. {
  3218. strncpy(tVal->broadcast, it->second.c_str(), 64);
  3219. tVal->broadcast[64] = (char) 0;
  3220. };
  3221. it = cArr->find("gateway");
  3222. if (it != cArr->end())
  3223. {
  3224. strncpy(tVal->gateway, it->second.c_str(), 64);
  3225. tVal->gateway[64] = (char) 0;
  3226. };
  3227. };
  3228. };
  3229. };
  3230. deleteResult(result);
  3231. };
  3232. return tVal;
  3233. }
  3234. uint32_t sqliteDBC::getNetworkInfrastructureID(uint32_t nif_id)
  3235. {
  3236. uint32_t tVal = 0;
  3237. if (m_bStoIsConnected)
  3238. {
  3239. char tQBuf[CTEXT];
  3240. AAssoc * result;
  3241. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id);
  3242. #ifdef DEBUG_ALL
  3243. LOG(tQBuf);
  3244. #endif
  3245. result = m_pDataStorage->Query(tQBuf);
  3246. if (result != NULL)
  3247. {
  3248. if (result->size() > 0)
  3249. {
  3250. Assoc * cArr;
  3251. Assoc::iterator it;
  3252. cArr = result->at(0);
  3253. it = cArr->find("id");
  3254. if (it != cArr->end())
  3255. {
  3256. tVal = atoi(it->second.c_str());
  3257. };
  3258. };
  3259. };
  3260. deleteResult(result);
  3261. };
  3262. return tVal;
  3263. }
  3264. TARGET_SEGMENT * sqliteDBC::getRegisteredBubble(uint32_t ID)
  3265. {
  3266. TARGET_SEGMENT * tVal = NULL;
  3267. if (m_bStoIsConnected)
  3268. {
  3269. char tQBuf[CTEXT];
  3270. AAssoc * result;
  3271. sprintf(tQBuf, "SELECT * FROM `target_bubbles` WHERE `id`=%d", ID);
  3272. #ifdef DEBUG_ALL
  3273. LOG(tQBuf);
  3274. #endif
  3275. result = m_pDataStorage->Query(tQBuf);
  3276. if (result != NULL)
  3277. {
  3278. if (result->size() > 0)
  3279. {
  3280. tVal = (TARGET_SEGMENT *) malloc(sizeof(TARGET_SEGMENT));
  3281. if (tVal)
  3282. {
  3283. Assoc * cArr;
  3284. Assoc::iterator it;
  3285. memset(tVal, 0, sizeof(TARGET_SEGMENT));
  3286. tVal->db_id = ID;
  3287. cArr = result->at(0);
  3288. it = cArr->find("CENTER_X");
  3289. if (it != cArr->end())
  3290. {
  3291. tVal->system_center.x = atof(it->second.c_str());
  3292. };
  3293. it = cArr->find("CENTER_Y");
  3294. if (it != cArr->end())
  3295. {
  3296. tVal->system_center.y = atof(it->second.c_str());
  3297. };
  3298. it = cArr->find("CENTER_Z");
  3299. if (it != cArr->end())
  3300. {
  3301. tVal->system_center.z = atof(it->second.c_str());
  3302. };
  3303. it = cArr->find("BOUNDARY");
  3304. if (it != cArr->end())
  3305. {
  3306. tVal->boundary = atof(it->second.c_str());
  3307. };
  3308. it = cArr->find("BLOCK_COUNS");
  3309. if (it != cArr->end())
  3310. {
  3311. tVal->hit_count = atoi(it->second.c_str());
  3312. };
  3313. };
  3314. };
  3315. };
  3316. deleteResult(result);
  3317. };
  3318. return tVal;
  3319. }
  3320. REPORT_DIRINFO * sqliteDBC::getReportDirInfo(uint32_t id)
  3321. {
  3322. REPORT_DIRINFO * tVal = NULL;
  3323. if( !m_bStaIsCreated && m_bStaIsConnected ){
  3324. char tQBuf[CTEXT];
  3325. AAssoc * result;
  3326. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id);
  3327. #ifdef DEBUG_ALL
  3328. LOG(tQBuf);
  3329. #endif
  3330. result = m_pDataStatistics->Query(tQBuf);
  3331. //result = m_pDataStorage->Query(tQBuf);
  3332. if (result != NULL)
  3333. {
  3334. if (result->size() > 0)
  3335. {
  3336. tVal = (REPORT_DIRINFO *) malloc(sizeof(REPORT_DIRINFO));
  3337. if (tVal)
  3338. {
  3339. Assoc * cArr;
  3340. Assoc::iterator it;
  3341. memset(tVal, 0, sizeof(REPORT_DIRINFO));
  3342. cArr = result->at(0);
  3343. it = cArr->find("year");
  3344. if (it != cArr->end())
  3345. {
  3346. strncpy(tVal->YEAR, it->second.c_str(), 5);
  3347. };
  3348. it = cArr->find("month");
  3349. if (it != cArr->end())
  3350. {
  3351. strncpy(tVal->MONTH, it->second.c_str(), 3);
  3352. };
  3353. it = cArr->find("day");
  3354. if (it != cArr->end())
  3355. {
  3356. strncpy(tVal->DAY, it->second.c_str(), 3);
  3357. };
  3358. };
  3359. };
  3360. };
  3361. deleteResult(result);
  3362. };
  3363. return tVal;
  3364. }
  3365. DBC_RESOURCE_DATA * sqliteDBC::getResourceData(uint32_t ResourceID)
  3366. {
  3367. DBC_RESOURCE_DATA * tVal = NULL;
  3368. if (m_bStoIsConnected)
  3369. {
  3370. char tQBuf[CTEXT];
  3371. AAssoc * result;
  3372. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID);
  3373. #ifdef DEBUG_ALL
  3374. LOG(tQBuf);
  3375. #endif
  3376. result = m_pDataStorage->Query(tQBuf);
  3377. if (result != NULL)
  3378. {
  3379. if (result->size() > 0)
  3380. {
  3381. Assoc * cArr;
  3382. /// @todo Valgrind tells us that tVal is lost and never freed
  3383. tVal = (DBC_RESOURCE_DATA *) malloc(sizeof(DBC_RESOURCE_DATA));
  3384. if (tVal != NULL)
  3385. {
  3386. memset(tVal, 0, sizeof(DBC_RESOURCE_DATA));
  3387. Assoc::iterator it;
  3388. cArr = result->at(0);
  3389. tVal->id = ResourceID;
  3390. it = cArr->find("node_id");
  3391. if (it != cArr->end())
  3392. {
  3393. tVal->node_id = atoi(it->second.c_str());
  3394. };
  3395. it = cArr->find("type");
  3396. if (it != cArr->end())
  3397. {
  3398. tVal->type = atoi(it->second.c_str());
  3399. };
  3400. it = cArr->find("aux_id");
  3401. if (it != cArr->end())
  3402. {
  3403. tVal->aux_id = atoi(it->second.c_str());
  3404. };
  3405. it = cArr->find("device");
  3406. if (it != cArr->end())
  3407. {
  3408. strncpy(tVal->device, it->second.c_str(), 64);
  3409. tVal->device[63] = (char) 0;
  3410. };
  3411. };
  3412. };
  3413. };
  3414. deleteResult(result);
  3415. };
  3416. return tVal;
  3417. }
  3418. uint32_t sqliteDBC::getResourceID(uint32_t NodeID, uint8_t type, uint32_t auxID)
  3419. {
  3420. uint32_t RetVal = 0;
  3421. if (m_bStoIsConnected)
  3422. {
  3423. char tQBuf[CTEXT];
  3424. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID);
  3425. #ifdef DEBUG_ALL
  3426. LOG(tQBuf);
  3427. #endif
  3428. AAssoc * result;
  3429. result = m_pDataStorage->Query(tQBuf);
  3430. if (result != NULL)
  3431. {
  3432. if (result->size() > 0)
  3433. {
  3434. Assoc * cArr;
  3435. Assoc::iterator it;
  3436. cArr = result->at(0);
  3437. it = cArr->find("id");
  3438. if (it != cArr->end())
  3439. {
  3440. RetVal = atoi(it->second.c_str());
  3441. };
  3442. };
  3443. };
  3444. deleteResult(result);
  3445. };
  3446. return RetVal;
  3447. }
  3448. DBC_SITE_DATA * sqliteDBC::getSiteData(uint32_t ID)
  3449. {
  3450. /** @todo Valgrind is reporting an important memory leak in this method.
  3451. * See these details:
  3452. *
  3453. * ~~~~
  3454. ==00:00:04:04.218 26021== 28,304 bytes in 122 blocks are definitely lost in loss record 3,966 of 3,972
  3455. ==00:00:04:04.218 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  3456. ==00:00:04:04.218 26021== by 0x23452A: sqliteDBC::getSiteData(unsigned int) (SQLite_dataBinder.cpp:2981)
  3457. ==00:00:04:04.218 26021== by 0x1DA681: mojoKernel::validateReports() (mojoKernel.cpp:1347)
  3458. ==00:00:04:04.218 26021== by 0x1D7D16: mojoKernel::m_fThreadProc() (mojoKernel.cpp:511)
  3459. ==00:00:04:04.218 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  3460. ==00:00:04:04.218 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  3461. ==00:00:04:04.218 26021== by 0x7874ADE: clone (clone.S:95)
  3462. * ~~~~
  3463. */
  3464. DBC_SITE_DATA * tVal = NULL;
  3465. if (m_bStoIsConnected)
  3466. {
  3467. char tQBuf[CTEXT];
  3468. AAssoc * result;
  3469. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  3470. #ifdef DEBUG_ALL
  3471. LOG(tQBuf);
  3472. #endif
  3473. result = m_pDataStorage->Query(tQBuf);
  3474. if (result != NULL)
  3475. {
  3476. if (result->size() > 0)
  3477. {
  3478. Assoc * cArr;
  3479. tVal = (DBC_SITE_DATA *) malloc(sizeof(DBC_SITE_DATA));
  3480. if (tVal != NULL)
  3481. {
  3482. memset(tVal, 0, sizeof(DBC_SITE_DATA));
  3483. Assoc::iterator it;
  3484. cArr = result->at(0);
  3485. it = cArr->find("type");
  3486. if (it != cArr->end())
  3487. {
  3488. tVal->account_id = atoi(it->second.c_str());
  3489. };
  3490. it = cArr->find("name");
  3491. if (it != cArr->end())
  3492. {
  3493. sprintf(tVal->name, it->second.c_str());
  3494. };
  3495. // it = cArr->find("server_ident");
  3496. // if( it != cArr->end() ){
  3497. // sprintf( tVal->ident, it->second.c_str() );
  3498. // };
  3499. it = cArr->find("anchor_lat");
  3500. if (it != cArr->end())
  3501. {
  3502. tVal->lat = atof(it->second.c_str());
  3503. };
  3504. it = cArr->find("anchor_lon");
  3505. if (it != cArr->end())
  3506. {
  3507. tVal->lon = atof(it->second.c_str());
  3508. };
  3509. it = cArr->find("anchor_alt");
  3510. if (it != cArr->end())
  3511. {
  3512. tVal->alt = atof(it->second.c_str());
  3513. };
  3514. it = cArr->find("hemisphere");
  3515. if (it != cArr->end())
  3516. {
  3517. tVal->SH = (it->second.c_str()[0] == 'S');
  3518. };
  3519. it = cArr->find("map_id");
  3520. if (it != cArr->end())
  3521. {
  3522. tVal->map_id = atoi(it->second.c_str());
  3523. };
  3524. it = cArr->find("local_binding");
  3525. if (it != cArr->end())
  3526. {
  3527. tVal->local_binding = (bool) atoi(it->second.c_str());
  3528. };
  3529. it = cArr->find("work_mode");
  3530. if (it != cArr->end())
  3531. {
  3532. tVal->work_mode = atoi(it->second.c_str());
  3533. };
  3534. it = cArr->find("report_cleanup");
  3535. if (it != cArr->end())
  3536. {
  3537. tVal->report_cleanup = atoi(it->second.c_str());
  3538. };
  3539. it = cArr->find("preserve_empty");
  3540. if (it != cArr->end())
  3541. {
  3542. tVal->preserve_empty = atoi(it->second.c_str());
  3543. };
  3544. it = cArr->find("remote_ip");
  3545. if (it != cArr->end())
  3546. {
  3547. sprintf(tVal->remote_ip, it->second.c_str());
  3548. };
  3549. it = cArr->find("remote_port");
  3550. if (it != cArr->end())
  3551. {
  3552. tVal->remote_port = atoi(it->second.c_str());
  3553. };
  3554. it = cArr->find("remote_backup_ip");
  3555. if (it != cArr->end())
  3556. {
  3557. sprintf(tVal->remote_backup_ip, it->second.c_str());
  3558. };
  3559. it = cArr->find("remote_backup_port");
  3560. if (it != cArr->end())
  3561. {
  3562. tVal->remote_port = atoi(it->second.c_str());
  3563. };
  3564. it = cArr->find("use_remote_server");
  3565. if (it != cArr->end())
  3566. {
  3567. tVal->use_remote_server = atoi(it->second.c_str());
  3568. };
  3569. it = cArr->find("skip_local_report");
  3570. if (it != cArr->end())
  3571. {
  3572. tVal->skip_local_report = atoi(it->second.c_str());
  3573. };
  3574. };
  3575. };
  3576. };
  3577. deleteResult(result);
  3578. };
  3579. return tVal;
  3580. }
  3581. uint32_t sqliteDBC::getServerID(CSTR identity)
  3582. {
  3583. uint32_t RetVal = 0;
  3584. if (m_bStoIsConnected)
  3585. {
  3586. char tQBuf[CTEXT];
  3587. sprintf(tQBuf, "SELECT `id` FROM `servers` WHERE `identity`='%s'", identity);
  3588. #ifdef DEBUG_ALL
  3589. LOG(tQBuf);
  3590. #endif
  3591. AAssoc * result;
  3592. result = m_pDataStorage->Query(tQBuf);
  3593. if (result != NULL)
  3594. {
  3595. if (result->size() > 0)
  3596. {
  3597. Assoc * cArr;
  3598. Assoc::iterator it;
  3599. cArr = result->at(0);
  3600. it = cArr->find("id");
  3601. if (it != cArr->end())
  3602. {
  3603. RetVal = atoi(it->second.c_str());
  3604. };
  3605. };
  3606. };
  3607. deleteResult(result);
  3608. };
  3609. return RetVal;
  3610. }
  3611. uint32_t sqliteDBC::getServerNode(uint32_t SID)
  3612. {
  3613. uint32_t RetVal = 0;
  3614. if (m_bStoIsConnected)
  3615. {
  3616. char tQBuf[CTEXT];
  3617. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID);
  3618. #ifdef DEBUG_ALL
  3619. LOG(tQBuf);
  3620. #endif
  3621. AAssoc * result;
  3622. result = m_pDataStorage->Query(tQBuf);
  3623. if (result != NULL)
  3624. {
  3625. if (result->size() > 0)
  3626. {
  3627. Assoc * cArr;
  3628. Assoc::iterator it;
  3629. cArr = result->at(0);
  3630. it = cArr->find("id");
  3631. if (it != cArr->end())
  3632. {
  3633. RetVal = atoi(it->second.c_str());
  3634. };
  3635. };
  3636. };
  3637. deleteResult(result);
  3638. };
  3639. return RetVal;
  3640. }
  3641. DBC_TARGET_FLITER_DATA * sqliteDBC::getTargetFilterData(uint32_t ID)
  3642. {
  3643. DBC_TARGET_FLITER_DATA * tVal = NULL;
  3644. if (m_bStoIsConnected)
  3645. {
  3646. char tQBuf[CTEXT];
  3647. AAssoc * result;
  3648. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID);
  3649. #ifdef DEBUG_ALL
  3650. LOG(tQBuf);
  3651. #endif
  3652. result = m_pDataStorage->Query(tQBuf);
  3653. if (result != NULL)
  3654. {
  3655. if (result->size() > 0)
  3656. {
  3657. Assoc * cArr;
  3658. tVal = (DBC_TARGET_FLITER_DATA *) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  3659. if (tVal != NULL)
  3660. {
  3661. memset(tVal, 0, sizeof(DBC_TARGET_FLITER_DATA));
  3662. Assoc::iterator it;
  3663. tVal->id = ID;
  3664. cArr = result->at(0);
  3665. it = cArr->find("type");
  3666. if (it != cArr->end())
  3667. {
  3668. tVal->type = (TARGET_FILTER_TYPE) atoi(it->second.c_str());
  3669. };
  3670. it = cArr->find("aux_id");
  3671. if (it != cArr->end())
  3672. {
  3673. tVal->aux_id = atoi(it->second.c_str());
  3674. };
  3675. it = cArr->find("enabled");
  3676. if (it != cArr->end())
  3677. {
  3678. tVal->enabled = (bool) atoi(it->second.c_str());
  3679. };
  3680. };
  3681. };
  3682. };
  3683. deleteResult(result);
  3684. };
  3685. return tVal;
  3686. }
  3687. TRAIN_FILTER_DATA * sqliteDBC::getTrainFilterData(uint32_t ID)
  3688. {
  3689. TRAIN_FILTER_DATA * tVal = NULL;
  3690. if (m_bStoIsConnected)
  3691. {
  3692. char tQBuf[CTEXT];
  3693. AAssoc * result;
  3694. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID);
  3695. #ifdef DEBUG_ALL
  3696. LOG(tQBuf);
  3697. #endif
  3698. result = m_pDataStorage->Query(tQBuf);
  3699. if (result != NULL)
  3700. {
  3701. if (result->size() > 0)
  3702. {
  3703. Assoc * cArr;
  3704. tVal = (TRAIN_FILTER_DATA *) malloc(sizeof(TRAIN_FILTER_DATA));
  3705. if (tVal != NULL)
  3706. {
  3707. memset(tVal, 0, sizeof(TRAIN_FILTER_DATA));
  3708. Assoc::iterator it;
  3709. cArr = result->at(0);
  3710. it = cArr->find("UPPER_SENSOR");
  3711. if (it != cArr->end())
  3712. {
  3713. tVal->nUpperSensorID = atoi(it->second.c_str());
  3714. };
  3715. it = cArr->find("LOWER_SENSOR");
  3716. if (it != cArr->end())
  3717. {
  3718. tVal->nBottomSensorID = atoi(it->second.c_str());
  3719. };
  3720. it = cArr->find("TRAIN_DETECT");
  3721. if (it != cArr->end())
  3722. {
  3723. tVal->bDetectTrain = (bool) atoi(it->second.c_str());
  3724. };
  3725. it = cArr->find("TRAIN_WIDTH");
  3726. if (it != cArr->end())
  3727. {
  3728. tVal->nTrainWidth = atoi(it->second.c_str());
  3729. };
  3730. it = cArr->find("DECISION_TIME");
  3731. if (it != cArr->end())
  3732. {
  3733. tVal->nDecisionTime = 1000 * atoi(it->second.c_str());
  3734. };
  3735. // In case Wrong Parameter (less then Second)
  3736. if (tVal->nDecisionTime < 1000)
  3737. {
  3738. tVal->nDecisionTime = 10000;
  3739. };
  3740. it = cArr->find("SUSPECT_TIME");
  3741. if (it != cArr->end())
  3742. {
  3743. tVal->nSuspectTime = 1000 * atoi(it->second.c_str());
  3744. };
  3745. // In case Wrong Parameter (less then Second)
  3746. if (tVal->nSuspectTime < 1000)
  3747. {
  3748. tVal->nSuspectTime = 10000;
  3749. };
  3750. it = cArr->find("TRACK_TRAIN");
  3751. if (it != cArr->end())
  3752. {
  3753. tVal->bTrackTrain = (bool) atoi(it->second.c_str());
  3754. };
  3755. it = cArr->find("AUX_IP");
  3756. if (it != cArr->end())
  3757. {
  3758. strncpy(tVal->aux_ip, it->second.c_str(), 15);
  3759. };
  3760. it = cArr->find("AUX_PORT");
  3761. if (it != cArr->end())
  3762. {
  3763. tVal->aux_port = atoi(it->second.c_str());
  3764. };
  3765. #ifdef EFT_AUTO_RESET_UPPER_SENSOR
  3766. it = cArr->find("UPPER_RESTART");
  3767. if (it != cArr->end())
  3768. {
  3769. tVal->nUpperSensorRestartTO = atoi(it->second.c_str()) * 3600; // Hours -> Seconds
  3770. }
  3771. else
  3772. {
  3773. tVal->nUpperSensorRestartTO = 24 * 3600;
  3774. };
  3775. #endif
  3776. #ifdef EFT_MINIMAL_RANGE_SUSPICIOUS
  3777. it = cArr->find("MINIMAL_LEFT_RANGE");
  3778. if (it != cArr->end())
  3779. {
  3780. tVal->fLeftMinimalRange = atof(it->second.c_str());
  3781. }
  3782. else
  3783. {
  3784. tVal->fLeftMinimalRange = 0.0;
  3785. };
  3786. it = cArr->find("MINIMAL_RIGHT_RANGE");
  3787. if (it != cArr->end())
  3788. {
  3789. tVal->fRightMinimalRange = atof(it->second.c_str());
  3790. }
  3791. else
  3792. {
  3793. tVal->fRightMinimalRange = 0.0;
  3794. };
  3795. #endif
  3796. it = cArr->find("DEBUG");
  3797. if (it != cArr->end())
  3798. {
  3799. tVal->bDebugMode = (bool) atoi(it->second.c_str());
  3800. };
  3801. };
  3802. };
  3803. };
  3804. deleteResult(result);
  3805. };
  3806. return tVal;
  3807. }
  3808. bool sqliteDBC::cleanRegisteredBubbles(void)
  3809. {
  3810. if (m_bStoIsConnected)
  3811. {
  3812. char tQBuf[MAX_PATH_LEN];
  3813. sprintf(tQBuf, "DELETE FROM `target_bubbles`");
  3814. return m_pDataStorage->SilentQuery(tQBuf);
  3815. };
  3816. return false;
  3817. }
  3818. DBC_UART_DATA * sqliteDBC::getUartData(uint32_t UartID)
  3819. {
  3820. DBC_UART_DATA * tVal = NULL;
  3821. if (m_bStoIsConnected)
  3822. {
  3823. char tQBuf[MAX_PATH_LEN];
  3824. AAssoc * result;
  3825. sprintf(tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID);
  3826. #ifdef DEBUG_ALL
  3827. LOG(tQBuf);
  3828. #endif
  3829. result = m_pDataStorage->Query(tQBuf);
  3830. if (result != NULL)
  3831. {
  3832. if (result->size() > 0)
  3833. {
  3834. Assoc * cArr;
  3835. tVal = (DBC_UART_DATA *) malloc(sizeof(DBC_UART_DATA));
  3836. if (tVal != NULL)
  3837. {
  3838. Assoc::iterator it;
  3839. cArr = result->at(0);
  3840. it = cArr->find("type");
  3841. if (it != cArr->end())
  3842. {
  3843. tVal->type = atoi(it->second.c_str());
  3844. };
  3845. it = cArr->find("speed");
  3846. if (it != cArr->end())
  3847. {
  3848. tVal->speed = atoi(it->second.c_str());
  3849. };
  3850. it = cArr->find("mode");
  3851. if (it != cArr->end())
  3852. {
  3853. tVal->mode = atoi(it->second.c_str());
  3854. };
  3855. it = cArr->find("port");
  3856. if (it != cArr->end())
  3857. {
  3858. tVal->port = atoi(it->second.c_str());
  3859. };
  3860. it = cArr->find("path");
  3861. if (it != cArr->end())
  3862. {
  3863. strncpy(tVal->path, it->second.c_str(), 128);
  3864. };
  3865. };
  3866. };
  3867. };
  3868. deleteResult(result);
  3869. };
  3870. return tVal;
  3871. }
  3872. uint32_t sqliteDBC::getUartID(uint32_t NodeID, uint8_t Type, CSTR path)
  3873. {
  3874. uint32_t RetVal = 0;
  3875. if (m_bStoIsConnected)
  3876. {
  3877. char tQBuf[CTEXT];
  3878. sprintf(tQBuf, "SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path);
  3879. #ifdef DEBUG_ALL
  3880. LOG(tQBuf);
  3881. #endif
  3882. AAssoc * result;
  3883. result = m_pDataStorage->Query(tQBuf);
  3884. if (result != NULL)
  3885. {
  3886. if (result->size() > 0)
  3887. {
  3888. Assoc * cArr;
  3889. Assoc::iterator it;
  3890. cArr = result->at(0);
  3891. it = cArr->find("id");
  3892. if (it != cArr->end())
  3893. {
  3894. RetVal = atoi(it->second.c_str());
  3895. };
  3896. };
  3897. };
  3898. deleteResult(result);
  3899. };
  3900. return RetVal;
  3901. }
  3902. uint32_t sqliteDBC::getWiFi_ID(uint32_t nif_id)
  3903. {
  3904. uint32_t tVal = 0;
  3905. if (m_bStoIsConnected)
  3906. {
  3907. char tQBuf[CTEXT];
  3908. AAssoc * result;
  3909. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id);
  3910. #ifdef DEBUG_ALL
  3911. LOG(tQBuf);
  3912. #endif
  3913. result = m_pDataStorage->Query(tQBuf);
  3914. if (result != NULL)
  3915. {
  3916. if (result->size() > 0)
  3917. {
  3918. Assoc * cArr;
  3919. Assoc::iterator it;
  3920. cArr = result->at(0);
  3921. it = cArr->find("id");
  3922. if (it != cArr->end())
  3923. {
  3924. tVal = atoi(it->second.c_str());
  3925. };
  3926. };
  3927. };
  3928. deleteResult(result);
  3929. };
  3930. return tVal;
  3931. }
  3932. DBC_INT_LIST * sqliteDBC::listCameras()
  3933. {
  3934. DBC_INT_LIST * tVal = NULL;
  3935. if (m_bStoIsConnected)
  3936. {
  3937. char tQBuf[CTEXT];
  3938. AAssoc * result;
  3939. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `status`=3");
  3940. #ifdef DEBUG_ALL
  3941. LOG(tQBuf);
  3942. #endif
  3943. result = m_pDataStorage->Query(tQBuf);
  3944. if (result != NULL)
  3945. {
  3946. if (result->size() > 0)
  3947. {
  3948. Assoc * cArr;
  3949. tVal = new DBC_INT_LIST();
  3950. if (tVal != NULL)
  3951. {
  3952. Assoc::iterator it;
  3953. for (size_t i = 0; i < result->size(); i++)
  3954. {
  3955. cArr = result->at(i);
  3956. it = cArr->find("id");
  3957. if (it != cArr->end())
  3958. {
  3959. tVal->push_back(atoi(it->second.c_str()));
  3960. };
  3961. };
  3962. };
  3963. };
  3964. };
  3965. deleteResult(result);
  3966. };
  3967. return tVal;
  3968. }
  3969. DBC_INT_LIST * sqliteDBC::listCamPresets(uint32_t SETID)
  3970. {
  3971. DBC_INT_LIST * tVal = NULL;
  3972. if (m_bStoIsConnected)
  3973. {
  3974. char tQBuf[CTEXT];
  3975. AAssoc * result;
  3976. sprintf(tQBuf, "SELECT `id` FROM `cameras_presets` WHERE `preset_id`=%d", SETID);
  3977. #ifdef DEBUG_ALL
  3978. LOG(tQBuf);
  3979. #endif
  3980. result = m_pDataStorage->Query(tQBuf);
  3981. if (result != NULL)
  3982. {
  3983. if (result->size() > 0)
  3984. {
  3985. Assoc * cArr;
  3986. tVal = new DBC_INT_LIST();
  3987. if (tVal != NULL)
  3988. {
  3989. Assoc::iterator it;
  3990. for (size_t i = 0; i < result->size(); i++)
  3991. {
  3992. cArr = result->at(i);
  3993. it = cArr->find("id");
  3994. if (it != cArr->end())
  3995. {
  3996. tVal->push_back(atoi(it->second.c_str()));
  3997. };
  3998. };
  3999. };
  4000. };
  4001. };
  4002. deleteResult(result);
  4003. };
  4004. return tVal;
  4005. }
  4006. DBC_INT_LIST * sqliteDBC::listFilters(uint32_t ServerID)
  4007. {
  4008. DBC_INT_LIST * tVal = NULL;
  4009. if (ServerID) ServerID = ServerID;
  4010. if (m_bStoIsConnected)
  4011. {
  4012. char tQBuf[CTEXT];
  4013. AAssoc * result;
  4014. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1");
  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::listLRFSensors()
  4045. {
  4046. DBC_INT_LIST * tVal = NULL;
  4047. if (m_bStoIsConnected)
  4048. {
  4049. char tQBuf[CTEXT];
  4050. AAssoc * result;
  4051. sprintf(tQBuf, "SELECT `id` FROM `lrf_sensors` WHERE `data_status`>0");
  4052. #ifdef DEBUG_ALL
  4053. LOG(tQBuf);
  4054. #endif
  4055. result = m_pDataStorage->Query(tQBuf);
  4056. if (result != NULL)
  4057. {
  4058. if (result->size() > 0)
  4059. {
  4060. Assoc * cArr;
  4061. tVal = new DBC_INT_LIST();
  4062. if (tVal != NULL)
  4063. {
  4064. Assoc::iterator it;
  4065. for (size_t i = 0; i < result->size(); i++)
  4066. {
  4067. cArr = result->at(i);
  4068. it = cArr->find("id");
  4069. if (it != cArr->end())
  4070. {
  4071. tVal->push_back(atoi(it->second.c_str()));
  4072. };
  4073. };
  4074. };
  4075. };
  4076. };
  4077. deleteResult(result);
  4078. };
  4079. return tVal;
  4080. }
  4081. DBC_INT_LIST * sqliteDBC::listLRFUploads(uint32_t deviceID)
  4082. {
  4083. DBC_INT_LIST * tVal = NULL;
  4084. if (m_bStoIsConnected)
  4085. {
  4086. char tQBuf[CTEXT];
  4087. AAssoc * result;
  4088. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID);
  4089. #ifdef DEBUG_ALL
  4090. LOG(tQBuf);
  4091. #endif
  4092. result = m_pDataStorage->Query(tQBuf);
  4093. if (result != NULL)
  4094. {
  4095. if (result->size() > 0)
  4096. {
  4097. Assoc * cArr;
  4098. tVal = new DBC_INT_LIST();
  4099. if (tVal != NULL)
  4100. {
  4101. Assoc::iterator it;
  4102. for (size_t i = 0; i < result->size(); i++)
  4103. {
  4104. cArr = result->at(i);
  4105. it = cArr->find("id");
  4106. if (it != cArr->end())
  4107. {
  4108. tVal->push_back(atoi(it->second.c_str()));
  4109. };
  4110. };
  4111. };
  4112. };
  4113. };
  4114. deleteResult(result);
  4115. };
  4116. return tVal;
  4117. }
  4118. DBC_INT_LIST * sqliteDBC::listMediaStreams(uint32_t SiteId, uint8_t listMode)
  4119. {
  4120. DBC_INT_LIST * tVal = NULL;
  4121. if ( SiteId ) SiteId = SiteId;
  4122. if (m_bStoIsConnected)
  4123. {
  4124. char tQBuf[CTEXT];
  4125. AAssoc * result;
  4126. switch (listMode)
  4127. {
  4128. 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;
  4129. 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;
  4130. 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");
  4131. };
  4132. #ifdef DEBUG_ALL
  4133. LOG(tQBuf);
  4134. #endif
  4135. result = m_pDataStorage->Query(tQBuf);
  4136. if (result != NULL)
  4137. {
  4138. if (result->size() > 0)
  4139. {
  4140. Assoc * cArr;
  4141. tVal = new DBC_INT_LIST();
  4142. if (tVal != NULL)
  4143. {
  4144. Assoc::iterator it;
  4145. for (size_t i = 0; i < result->size(); i++)
  4146. {
  4147. cArr = result->at(i);
  4148. it = cArr->find("id");
  4149. if (it != cArr->end())
  4150. {
  4151. tVal->push_back(atoi(it->second.c_str()));
  4152. };
  4153. };
  4154. };
  4155. };
  4156. };
  4157. deleteResult(result);
  4158. };
  4159. return tVal;
  4160. }
  4161. DBC_INT_LIST * sqliteDBC::listModules()
  4162. {
  4163. DBC_INT_LIST * tVal = NULL;
  4164. if (m_bStoIsConnected)
  4165. {
  4166. AAssoc * result;
  4167. result = m_pDataStorage->Query("SELECT `id` FROM `modules` WHERE `enabled`=1");
  4168. if (result != NULL)
  4169. {
  4170. if (result->size() > 0)
  4171. {
  4172. Assoc * cArr;
  4173. tVal = new DBC_INT_LIST();
  4174. if (tVal != NULL)
  4175. {
  4176. Assoc::iterator it;
  4177. for (size_t i = 0; i < result->size(); i++)
  4178. {
  4179. cArr = result->at(i);
  4180. it = cArr->find("id");
  4181. if (it != cArr->end())
  4182. {
  4183. tVal->push_back(atoi(it->second.c_str()));
  4184. };
  4185. };
  4186. };
  4187. };
  4188. };
  4189. deleteResult(result);
  4190. };
  4191. return tVal;
  4192. }
  4193. DBC_INT_LIST * sqliteDBC::listNetworks(uint32_t nif_id)
  4194. {
  4195. DBC_INT_LIST * tVal = NULL;
  4196. if (m_bStoIsConnected)
  4197. {
  4198. char tQBuf[CTEXT];
  4199. AAssoc * result;
  4200. sprintf(tQBuf, "SELECT `id` FROM `networks` WHERE `nif_id`=%d", nif_id);
  4201. #ifdef DEBUG_ALL
  4202. LOG(tQBuf);
  4203. #endif
  4204. result = m_pDataStorage->Query(tQBuf);
  4205. if (result != NULL)
  4206. {
  4207. if (result->size() > 0)
  4208. {
  4209. Assoc * cArr;
  4210. tVal = new DBC_INT_LIST();
  4211. if (tVal != NULL)
  4212. {
  4213. Assoc::iterator it;
  4214. for (size_t i = 0; i < result->size(); i++)
  4215. {
  4216. cArr = result->at(i);
  4217. it = cArr->find("id");
  4218. if (it != cArr->end())
  4219. {
  4220. tVal->push_back(atoi(it->second.c_str()));
  4221. };
  4222. };
  4223. };
  4224. };
  4225. };
  4226. deleteResult(result);
  4227. };
  4228. return tVal;
  4229. }
  4230. DBC_INT_LIST * sqliteDBC::listNewModules(DBC_INT_LIST * old_ids)
  4231. {
  4232. DBC_INT_LIST * tVal = NULL;
  4233. if (m_bStoIsConnected)
  4234. {
  4235. char tParam[CTEXT];
  4236. // memset( tParam, 0, CTEXT );
  4237. sprintf(tParam, "0");
  4238. if (old_ids != NULL)
  4239. {
  4240. char tBuffer[1024];
  4241. for (size_t i = 0; i < old_ids->size(); i++)
  4242. {
  4243. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4244. strncpy(tParam, tBuffer, CTEXT);
  4245. };
  4246. };
  4247. char tQBuf[2048];
  4248. AAssoc * result;
  4249. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam);
  4250. #ifdef DEBUG_ALL
  4251. LOG(tQBuf);
  4252. #endif
  4253. result = m_pDataStorage->Query(tQBuf);
  4254. if (result != NULL)
  4255. {
  4256. if (result->size() > 0)
  4257. {
  4258. Assoc * cArr;
  4259. tVal = new DBC_INT_LIST();
  4260. if (tVal != NULL)
  4261. {
  4262. Assoc::iterator it;
  4263. for (size_t i = 0; i < result->size(); i++)
  4264. {
  4265. cArr = result->at(i);
  4266. it = cArr->find("id");
  4267. if (it != cArr->end())
  4268. {
  4269. tVal->push_back(atoi(it->second.c_str()));
  4270. };
  4271. };
  4272. };
  4273. };
  4274. };
  4275. deleteResult(result);
  4276. };
  4277. return tVal;
  4278. }
  4279. DBC_INT_LIST * sqliteDBC::listNewNodes(DBC_INT_LIST * old_ids)
  4280. {
  4281. DBC_INT_LIST * tVal = NULL;
  4282. if (m_bStoIsConnected)
  4283. {
  4284. char tParam[CTEXT];
  4285. // memset( tParam, 0, CTEXT );
  4286. sprintf(tParam, "0");
  4287. if (old_ids != NULL)
  4288. {
  4289. char tBuffer[1024];
  4290. for (size_t i = 0; i < old_ids->size(); i++)
  4291. {
  4292. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4293. strncpy(tParam, tBuffer, CTEXT);
  4294. };
  4295. };
  4296. char tQBuf[2048];
  4297. AAssoc * result;
  4298. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam);
  4299. #ifdef DEBUG_ALL
  4300. LOG(tQBuf);
  4301. #endif
  4302. result = m_pDataStorage->Query(tQBuf);
  4303. if (result != NULL)
  4304. {
  4305. if (result->size() > 0)
  4306. {
  4307. Assoc * cArr;
  4308. tVal = new DBC_INT_LIST();
  4309. if (tVal != NULL)
  4310. {
  4311. Assoc::iterator it;
  4312. for (size_t i = 0; i < result->size(); i++)
  4313. {
  4314. cArr = result->at(i);
  4315. it = cArr->find("id");
  4316. if (it != cArr->end())
  4317. {
  4318. tVal->push_back(atoi(it->second.c_str()));
  4319. };
  4320. };
  4321. };
  4322. };
  4323. };
  4324. deleteResult(result);
  4325. };
  4326. return tVal;
  4327. }
  4328. DBC_INT_LIST * sqliteDBC::listNodes(bool all)
  4329. {
  4330. DBC_INT_LIST * tVal = NULL;
  4331. if (m_bStoIsConnected)
  4332. {
  4333. char tQBuf[1024];
  4334. AAssoc * result;
  4335. if (all)
  4336. {
  4337. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  4338. }
  4339. else
  4340. {
  4341. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1");
  4342. };
  4343. #ifdef DEBUG_ALL
  4344. LOG(tQBuf);
  4345. #endif
  4346. result = m_pDataStorage->Query(tQBuf);
  4347. if (result != NULL)
  4348. {
  4349. if (result->size() > 0)
  4350. {
  4351. Assoc * cArr;
  4352. tVal = new DBC_INT_LIST();
  4353. if (tVal != NULL)
  4354. {
  4355. Assoc::iterator it;
  4356. for (size_t i = 0; i < result->size(); i++)
  4357. {
  4358. cArr = result->at(i);
  4359. it = cArr->find("id");
  4360. if (it != cArr->end())
  4361. {
  4362. tVal->push_back(atoi(it->second.c_str()));
  4363. };
  4364. };
  4365. };
  4366. };
  4367. };
  4368. deleteResult(result);
  4369. };
  4370. return tVal;
  4371. }
  4372. DBC_INT_LIST * sqliteDBC::listNodeFilters(uint32_t NodeID)
  4373. {
  4374. DBC_INT_LIST * tVal = NULL;
  4375. if (m_bStoIsConnected)
  4376. {
  4377. char tQBuf[1024];
  4378. AAssoc * result;
  4379. sprintf(tQBuf, "SELECT `filter_id` FROM `node_target_filters` WHERE `node_id`=%d ORDER BY `order` ASC", NodeID);
  4380. #ifdef DEBUG_ALL
  4381. LOG(tQBuf);
  4382. #endif
  4383. result = m_pDataStorage->Query(tQBuf);
  4384. if (result != NULL)
  4385. {
  4386. if (result->size() > 0)
  4387. {
  4388. Assoc * cArr;
  4389. tVal = new DBC_INT_LIST();
  4390. if (tVal != NULL)
  4391. {
  4392. Assoc::iterator it;
  4393. for (size_t i = 0; i < result->size(); i++)
  4394. {
  4395. cArr = result->at(i);
  4396. it = cArr->find("filter_id");
  4397. if (it != cArr->end())
  4398. {
  4399. tVal->push_back(atoi(it->second.c_str()));
  4400. };
  4401. };
  4402. };
  4403. };
  4404. };
  4405. deleteResult(result);
  4406. };
  4407. return tVal;
  4408. }
  4409. DBC_INT_LIST * sqliteDBC::listOlderReportStat(CSTR Y, CSTR M, CSTR D)
  4410. {
  4411. DBC_INT_LIST * tVal = NULL;
  4412. if (m_bStaIsConnected)
  4413. {
  4414. char tQBuf[MAX_PATH_LEN];
  4415. AAssoc * result;
  4416. 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);
  4417. result = m_pDataStatistics->Query(tQBuf);
  4418. if (result != NULL)
  4419. {
  4420. if (result->size() > 0)
  4421. {
  4422. Assoc * cArr;
  4423. tVal = new DBC_INT_LIST();
  4424. if (tVal != NULL)
  4425. {
  4426. Assoc::iterator it;
  4427. for (size_t i = 0; i < result->size(); i++)
  4428. {
  4429. cArr = result->at(i);
  4430. it = cArr->find("ROWID");
  4431. if (it != cArr->end())
  4432. {
  4433. tVal->push_back(atoi(it->second.c_str()));
  4434. };
  4435. };
  4436. };
  4437. };
  4438. };
  4439. deleteResult(result);
  4440. };
  4441. return tVal;
  4442. }
  4443. DBC_INT_LIST * sqliteDBC::listOlderReportStat(uint32_t TS)
  4444. {
  4445. DBC_INT_LIST * tVal = NULL;
  4446. if (m_bStaIsConnected)
  4447. {
  4448. char tQBuf[MAX_PATH_LEN];
  4449. AAssoc * result;
  4450. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS);
  4451. result = m_pDataStatistics->Query(tQBuf);
  4452. if (result != NULL)
  4453. {
  4454. if (result->size() > 0)
  4455. {
  4456. Assoc * cArr;
  4457. tVal = new DBC_INT_LIST();
  4458. if (tVal != NULL)
  4459. {
  4460. Assoc::iterator it;
  4461. for (size_t i = 0; i < result->size(); i++)
  4462. {
  4463. cArr = result->at(i);
  4464. it = cArr->find("ROWID");
  4465. if (it != cArr->end())
  4466. {
  4467. tVal->push_back(atoi(it->second.c_str()));
  4468. };
  4469. };
  4470. };
  4471. };
  4472. };
  4473. deleteResult(result);
  4474. };
  4475. return tVal;
  4476. }
  4477. DBC_INT_LIST * sqliteDBC::listPhonebook()
  4478. {
  4479. DBC_INT_LIST * tVal = NULL;
  4480. if (m_bStoIsConnected)
  4481. {
  4482. char tQBuf[CTEXT];
  4483. AAssoc * result;
  4484. sprintf(tQBuf, "SELECT `id` FROM `module_modem_phonebook`");
  4485. #ifdef DEBUG_ALL
  4486. LOG(tQBuf);
  4487. #endif
  4488. result = m_pDataStorage->Query(tQBuf);
  4489. if (result != NULL)
  4490. {
  4491. if (result->size() > 0)
  4492. {
  4493. Assoc * cArr;
  4494. tVal = new DBC_INT_LIST();
  4495. if (tVal != NULL)
  4496. {
  4497. Assoc::iterator it;
  4498. for (size_t i = 0; i < result->size(); i++)
  4499. {
  4500. cArr = result->at(i);
  4501. it = cArr->find("id");
  4502. if (it != cArr->end())
  4503. {
  4504. tVal->push_back(atoi(it->second.c_str()));
  4505. };
  4506. };
  4507. };
  4508. };
  4509. };
  4510. deleteResult(result);
  4511. };
  4512. return tVal;
  4513. }
  4514. DBC_INT_LIST * sqliteDBC::listFrozenBubbles(uint8_t cc)
  4515. {
  4516. DBC_INT_LIST * tVal = NULL;
  4517. if (m_bStoIsConnected)
  4518. {
  4519. char tQBuf[CTEXT];
  4520. AAssoc * result;
  4521. if (cc)
  4522. {
  4523. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCK_COUNS`>%d", cc);
  4524. }
  4525. else
  4526. {
  4527. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCKED`=1");
  4528. }
  4529. #ifdef DEBUG_ALL
  4530. LOG(tQBuf);
  4531. #endif
  4532. result = m_pDataStorage->Query(tQBuf);
  4533. if (result != NULL)
  4534. {
  4535. if (result->size() > 0)
  4536. {
  4537. Assoc * cArr;
  4538. tVal = new DBC_INT_LIST();
  4539. if (tVal != NULL)
  4540. {
  4541. Assoc::iterator it;
  4542. for (size_t i = 0; i < result->size(); i++)
  4543. {
  4544. cArr = result->at(i);
  4545. it = cArr->find("id");
  4546. if (it != cArr->end())
  4547. {
  4548. tVal->push_back(atoi(it->second.c_str()));
  4549. };
  4550. };
  4551. };
  4552. };
  4553. };
  4554. deleteResult(result);
  4555. };
  4556. return tVal;
  4557. }
  4558. DBC_INT_LIST * sqliteDBC::listResources(uint32_t NodeID)
  4559. {
  4560. DBC_INT_LIST * tVal = NULL;
  4561. if (m_bStoIsConnected)
  4562. {
  4563. char tQBuf[CTEXT];
  4564. AAssoc * result;
  4565. if (NodeID == 0)
  4566. {
  4567. NodeID = m_nServerNodeID;
  4568. }
  4569. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID);
  4570. #ifdef DEBUG_ALL
  4571. LOG(tQBuf);
  4572. #endif
  4573. result = m_pDataStorage->Query(tQBuf);
  4574. if (result != NULL)
  4575. {
  4576. if (result->size() > 0)
  4577. {
  4578. Assoc * cArr;
  4579. tVal = new DBC_INT_LIST();
  4580. if (tVal != NULL)
  4581. {
  4582. Assoc::iterator it;
  4583. for (size_t i = 0; i < result->size(); i++)
  4584. {
  4585. cArr = result->at(i);
  4586. it = cArr->find("id");
  4587. if (it != cArr->end())
  4588. {
  4589. tVal->push_back(atoi(it->second.c_str()));
  4590. };
  4591. };
  4592. };
  4593. };
  4594. };
  4595. deleteResult(result);
  4596. };
  4597. return tVal;
  4598. }
  4599. bool sqliteDBC::pushMemCMD(uint32_t NODE_ID, uint8_t CMD, CSTR DATA)
  4600. {
  4601. if (m_bMemIsConnected)
  4602. {
  4603. char QS[MAX_PATH];
  4604. sprintf(QS, "INSERT INTO `nodes_qcq`(`node_id`,`cmd`,`status`,`parameter`) VALUES(%d,%d,0,%s)", NODE_ID, CMD, DATA);
  4605. #ifdef DEBUG_ALL
  4606. LOG(QS);
  4607. #endif
  4608. m_pDataMemory->SilentQuery(QS);
  4609. return true;
  4610. };
  4611. return false;
  4612. }
  4613. void sqliteDBC::registerServer(uint32_t SiteID, CSTR identity, int PID, uint32_t * ServID, uint32_t * ServNode)
  4614. {
  4615. if (m_bStoIsConnected)
  4616. {
  4617. m_nSiteID = SiteID;
  4618. char tQBuf[MAX_PATH_LEN];
  4619. #ifdef DEFAULT_SERVER_ID
  4620. 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,
  4621. _BUILD_STRING_);
  4622. #else
  4623. 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_);
  4624. #endif
  4625. #ifdef DEBUG_ALL
  4626. LOG(tQBuf);
  4627. #endif
  4628. m_pDataStorage->SilentQuery(tQBuf);
  4629. #ifdef DEFAULT_SERVER_ID
  4630. m_nServerID = DEFAULT_SERVER_ID;
  4631. #else
  4632. m_nServerID = getServerID(identity);
  4633. #endif
  4634. *ServID = m_nServerID;
  4635. if (m_nServerID)
  4636. {
  4637. initMemDB(m_nServerID);
  4638. sprintf(tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID);
  4639. #ifdef DEBUG_ALL
  4640. LOG(tQBuf);
  4641. #endif
  4642. m_pDataMemory->SilentQuery(tQBuf);
  4643. 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);
  4644. #ifdef DEBUG_ALL
  4645. LOG(tQBuf);
  4646. #endif
  4647. m_nServerNodeID = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  4648. if (m_nServerNodeID == 0)
  4649. {
  4650. m_nServerNodeID = getServerNode(m_nServerID);
  4651. };
  4652. *ServNode = m_nServerNodeID;
  4653. // Clenup Nodes and services ...
  4654. sprintf(tQBuf, "UPDATE `nodes` SET `online`=0");
  4655. #ifdef DEBUG_ALL
  4656. LOG(tQBuf);
  4657. #endif
  4658. m_pDataStorage->SilentQuery(tQBuf);
  4659. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `manual`=0");
  4660. #ifdef DEBUG_ALL
  4661. LOG(tQBuf);
  4662. #endif
  4663. m_pDataStorage->SilentQuery(tQBuf);
  4664. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )");
  4665. #ifdef DEBUG_ALL
  4666. LOG(tQBuf);
  4667. #endif
  4668. m_pDataStorage->SilentQuery(tQBuf);
  4669. // sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  4670. #ifdef DEBUG_ALL
  4671. // LOG(tQBuf);
  4672. #endif
  4673. // m_pDataStorage->SilentQuery( tQBuf );
  4674. // sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  4675. #ifdef DEBUG_ALL
  4676. // LOG(tQBuf);
  4677. #endif
  4678. // m_pDataStorage->SilentQuery( tQBuf );
  4679. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  4680. #ifdef DEBUG_ALL
  4681. // LOG(tQBuf);
  4682. #endif
  4683. // m_pDataStorage->SilentQuery( tQBuf );
  4684. // cleanResources( tServerNodeID );
  4685. setNodeOnline(m_nServerNodeID);
  4686. };
  4687. };
  4688. }
  4689. bool sqliteDBC::setLRFDataStatus(uint32_t lrfID, uint8_t ST)
  4690. {
  4691. if (m_bStoIsConnected)
  4692. {
  4693. char tQBuf[MAX_PATH_LEN];
  4694. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID);
  4695. #ifdef DEBUG_ALL
  4696. LOG(tQBuf);
  4697. #endif
  4698. m_pDataStorage->SilentQuery(tQBuf);
  4699. return true;
  4700. };
  4701. return false;
  4702. }
  4703. bool sqliteDBC::setLRFDetectionProfile(uint32_t lrfID, uint8_t profileID)
  4704. {
  4705. if (m_bStoIsConnected)
  4706. {
  4707. char tQBuf[MAX_PATH_LEN];
  4708. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID);
  4709. #ifdef DEBUG_ALL
  4710. LOG(tQBuf);
  4711. #endif
  4712. m_pDataStorage->SilentQuery(tQBuf);
  4713. return true;
  4714. };
  4715. return false;
  4716. }
  4717. bool sqliteDBC::setLRFMaskStatus(uint32_t maskID, bool status)
  4718. {
  4719. if (m_bStoIsConnected)
  4720. {
  4721. char tQBuf[MAX_PATH_LEN];
  4722. if (status)
  4723. {
  4724. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID);
  4725. }
  4726. else
  4727. {
  4728. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID);
  4729. };
  4730. #ifdef DEBUG_ALL
  4731. LOG(tQBuf);
  4732. #endif
  4733. m_pDataStorage->SilentQuery(tQBuf);
  4734. return true;
  4735. };
  4736. return false;
  4737. }
  4738. bool sqliteDBC::setLRFAltSerial(uint32_t lrfID, CSTR Serial)
  4739. {
  4740. if (m_bStoIsConnected)
  4741. {
  4742. char tQBuf[MAX_PATH_LEN];
  4743. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID);
  4744. #ifdef DEBUG_ALL
  4745. LOG(tQBuf);
  4746. #endif
  4747. m_pDataStorage->SilentQuery(tQBuf);
  4748. return true;
  4749. };
  4750. return false;
  4751. }
  4752. bool sqliteDBC::setLRFAltVerison(uint32_t lrfID, CSTR version)
  4753. {
  4754. if (m_bStoIsConnected)
  4755. {
  4756. char tQBuf[MAX_PATH_LEN];
  4757. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID);
  4758. #ifdef DEBUG_ALL
  4759. LOG(tQBuf);
  4760. #endif
  4761. m_pDataStorage->SilentQuery(tQBuf);
  4762. return true;
  4763. };
  4764. return false;
  4765. }
  4766. bool sqliteDBC::setNodeOffline(uint32_t NodeID)
  4767. {
  4768. if (m_bStoIsConnected)
  4769. {
  4770. char tQBuf[CTEXT];
  4771. if (NodeID)
  4772. {
  4773. sprintf(tQBuf, "UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID);
  4774. #ifdef DEBUG_ALL
  4775. LOG(tQBuf);
  4776. #endif
  4777. m_pDataStorage->SilentQuery(tQBuf);
  4778. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID);
  4779. #ifdef DEBUG_ALL
  4780. LOG(tQBuf);
  4781. #endif
  4782. m_pDataStorage->SilentQuery(tQBuf);
  4783. return true;
  4784. };
  4785. };
  4786. return false;
  4787. }
  4788. bool sqliteDBC::setNodeOnline(uint32_t NodeID, uint8_t status)
  4789. {
  4790. if (m_bStoIsConnected)
  4791. {
  4792. char tQBuf[CTEXT];
  4793. if (NodeID)
  4794. {
  4795. sprintf(tQBuf, "UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID);
  4796. #ifdef DEBUG_ALL
  4797. LOG(tQBuf);
  4798. #endif
  4799. m_pDataStorage->SilentQuery(tQBuf);
  4800. sprintf(tQBuf, "UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID);
  4801. #ifdef DEBUG_ALL
  4802. LOG(tQBuf);
  4803. #endif
  4804. m_pDataStorage->SilentQuery(tQBuf);
  4805. return true;
  4806. };
  4807. };
  4808. return false;
  4809. }
  4810. bool sqliteDBC::setRessourceActive(uint32_t RessourceID, bool Status)
  4811. {
  4812. if (m_bStoIsConnected)
  4813. {
  4814. char tQBuf[CTEXT];
  4815. if (RessourceID)
  4816. {
  4817. sprintf(tQBuf, "UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID);
  4818. #ifdef DEBUG_ALL
  4819. LOG(tQBuf);
  4820. #endif
  4821. m_pDataStorage->SilentQuery(tQBuf);
  4822. return true;
  4823. };
  4824. };
  4825. return false;
  4826. }
  4827. bool sqliteDBC::setStreamValid(uint32_t streamID, bool status)
  4828. {
  4829. if (m_bStoIsConnected)
  4830. {
  4831. char tQBuf[MAX_PATH_LEN];
  4832. if (status)
  4833. {
  4834. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  4835. }
  4836. else
  4837. {
  4838. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  4839. };
  4840. #ifdef DEBUG_ALL
  4841. LOG(tQBuf);
  4842. #endif
  4843. m_pDataStorage->SilentQuery(tQBuf);
  4844. return true;
  4845. };
  4846. return false;
  4847. }
  4848. void sqliteDBC::unregisterServer(void)
  4849. {
  4850. if (m_bStoIsConnected)
  4851. {
  4852. char tQBuf[MAX_PATH_LEN];
  4853. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0");
  4854. #ifdef DEBUG_ALL
  4855. LOG(tQBuf);
  4856. #endif
  4857. m_pDataStorage->SilentQuery(tQBuf);
  4858. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0");
  4859. #ifdef DEBUG_ALL
  4860. LOG(tQBuf);
  4861. #endif
  4862. m_pDataStorage->SilentQuery(tQBuf);
  4863. };
  4864. setNodeOffline(m_nServerNodeID);
  4865. }
  4866. bool sqliteDBC::updateSystemInfo(DBC_SYS_INFO * ptr)
  4867. {
  4868. if (m_bStoIsConnected)
  4869. {
  4870. char tQBuf[MAX_PATH_LEN];
  4871. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d", ptr->OS_INFO, ptr->HW_INFO, m_nSiteID);
  4872. #ifdef DEBUG_ALL
  4873. LOG(tQBuf);
  4874. #endif
  4875. m_pDataStorage->SilentQuery(tQBuf);
  4876. return true;
  4877. };
  4878. return false;
  4879. }
  4880. bool sqliteDBC::fillShadow2D(uint32_t ID, SHADOW & SH)
  4881. {
  4882. if (m_bStoIsConnected)
  4883. {
  4884. char tQBuf[CTEXT];
  4885. AAssoc * result;
  4886. 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);
  4887. #ifdef DEBUG_ALL
  4888. LOG(tQBuf);
  4889. #endif
  4890. result = m_pDataStorage->Query(tQBuf);
  4891. if (result != NULL)
  4892. {
  4893. if (result->size() > 0)
  4894. {
  4895. SH.m_nEnabled = 0xFFFFFFFF;
  4896. SH.m_nPolygonCount = result->size();
  4897. SH.pPolygones = (SHADOW_POLY *) calloc(SH.m_nPolygonCount, sizeof(SHADOW_POLY));
  4898. if (SH.pPolygones)
  4899. {
  4900. Assoc * cArr;
  4901. for (uint8_t idx = 0; idx < result->size(); idx++)
  4902. {
  4903. Assoc::iterator it;
  4904. cArr = result->at(idx);
  4905. it = cArr->find("id");
  4906. if (it != cArr->end())
  4907. {
  4908. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  4909. };
  4910. it = cArr->find("layer");
  4911. if (it != cArr->end())
  4912. {
  4913. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  4914. };
  4915. it = cArr->find("status");
  4916. if (it != cArr->end())
  4917. {
  4918. SH.m_nEnabled &= (it->second.c_str()[0] == 'E') ? 0xFFFFFFFF : ~(1 << SH.pPolygones[idx].bType);
  4919. };
  4920. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID);
  4921. #ifdef DEBUG_ALL
  4922. LOG(tQBuf);
  4923. #endif
  4924. AAssoc * v_result = m_pDataStorage->Query(tQBuf);
  4925. if (v_result != NULL)
  4926. {
  4927. if (v_result->size() > 0)
  4928. {
  4929. SH.pPolygones[idx].wCount = v_result->size();
  4930. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc(SH.pPolygones[idx].wCount, sizeof(HZ_POINT));
  4931. if (SH.pPolygones[idx].pPoint)
  4932. {
  4933. for (uint32_t zdx = 0; zdx < SH.pPolygones[idx].wCount; zdx++)
  4934. {
  4935. cArr = v_result->at(zdx);
  4936. it = cArr->find("x");
  4937. if (it != cArr->end())
  4938. {
  4939. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  4940. };
  4941. it = cArr->find("y");
  4942. if (it != cArr->end())
  4943. {
  4944. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  4945. };
  4946. LOG("Point Added!");
  4947. };
  4948. }
  4949. else
  4950. {
  4951. LOG("No mem for Points!");
  4952. };
  4953. }
  4954. else
  4955. {
  4956. LOG("No Points!");
  4957. SH.pPolygones[idx].wCount = 0;
  4958. SH.pPolygones[idx].pPoint = NULL;
  4959. };
  4960. deleteResult(v_result);
  4961. };
  4962. };
  4963. };
  4964. };
  4965. deleteResult(result);
  4966. };
  4967. return true;
  4968. };
  4969. return false;
  4970. }
  4971. bool sqliteDBC::getAppDebugMode()
  4972. {
  4973. bool rVal = false;
  4974. if (m_bStoIsConnected)
  4975. {
  4976. char tQBuf[CTEXT];
  4977. AAssoc * result;
  4978. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1");
  4979. #ifdef DEBUG_ALL
  4980. LOG(tQBuf);
  4981. #endif
  4982. result = m_pDataStorage->Query(tQBuf);
  4983. if (result != NULL)
  4984. {
  4985. if (result->size() > 0)
  4986. {
  4987. Assoc * cArr;
  4988. Assoc::iterator it;
  4989. cArr = result->at(0);
  4990. it = cArr->find("work_mode");
  4991. if (it != cArr->end())
  4992. {
  4993. rVal = (bool) atoi(it->second.c_str());
  4994. };
  4995. };
  4996. };
  4997. deleteResult(result);
  4998. };
  4999. return rVal;
  5000. }
  5001. DBC_ATC_MODULE_DATA * sqliteDBC::getAttentionModuleData( uint32_t id ){
  5002. DBC_ATC_MODULE_DATA * tVal = NULL;
  5003. if (m_bStoIsConnected)
  5004. {
  5005. char tQBuf[CTEXT];
  5006. AAssoc * result;
  5007. sprintf(tQBuf, "SELECT * FROM `module_attention` WHERE `id`=%d", id);
  5008. #ifdef DEBUG_ALL
  5009. LOG(tQBuf);
  5010. #endif
  5011. result = m_pDataStorage->Query(tQBuf);
  5012. if (result != NULL)
  5013. {
  5014. if (result->size() > 0)
  5015. {
  5016. Assoc * cArr;
  5017. tVal = (DBC_ATC_MODULE_DATA *) malloc(sizeof(DBC_ATC_MODULE_DATA));
  5018. if (tVal != NULL)
  5019. {
  5020. memset(tVal, 0, sizeof(DBC_ATC_MODULE_DATA));
  5021. ;
  5022. Assoc::iterator it;
  5023. cArr = result->at(0);
  5024. it = cArr->find("rest");
  5025. if (it != cArr->end())
  5026. {
  5027. tVal->rest_time = atoi(it->second.c_str());
  5028. };
  5029. it = cArr->find("persist");
  5030. if (it != cArr->end())
  5031. {
  5032. tVal->persist_time = atoi(it->second.c_str());
  5033. };
  5034. it = cArr->find("range");
  5035. if (it != cArr->end())
  5036. {
  5037. tVal->attention_zone_range = atof(it->second.c_str());
  5038. };
  5039. };
  5040. };
  5041. };
  5042. deleteResult(result);
  5043. };
  5044. return tVal;
  5045. }
  5046. REST_SETTINGS *sqliteDBC::getBoschData( uint32_t id ){
  5047. REST_SETTINGS * tVal = NULL;
  5048. if (m_bStoIsConnected){
  5049. char tQBuf[CTEXT];
  5050. AAssoc * result;
  5051. sprintf(tQBuf, "SELECT * FROM `module_bvms` WHERE `id`=%d", id);
  5052. #ifdef DEBUG_ALL
  5053. LOG(tQBuf);
  5054. #endif
  5055. result = m_pDataStorage->Query(tQBuf);
  5056. if (result != NULL)
  5057. {
  5058. if (result->size() > 0)
  5059. {
  5060. Assoc * cArr;
  5061. tVal = (REST_SETTINGS *) malloc(sizeof(REST_SETTINGS));
  5062. if (tVal != NULL)
  5063. {
  5064. memset(tVal, 0, sizeof(REST_SETTINGS));
  5065. ;
  5066. Assoc::iterator it;
  5067. cArr = result->at(0);
  5068. it = cArr->find("ip");
  5069. if (it != cArr->end())
  5070. {
  5071. strncpy(tVal->server_ip, it->second.c_str(), 48);
  5072. };
  5073. it = cArr->find("port");
  5074. if (it != cArr->end())
  5075. {
  5076. tVal->port = atoi(it->second.c_str());
  5077. };
  5078. it = cArr->find("api_uri");
  5079. if (it != cArr->end())
  5080. {
  5081. strncpy(tVal->rest_api, it->second.c_str(), 256);
  5082. };
  5083. it = cArr->find("auth_mode");
  5084. if (it != cArr->end())
  5085. {
  5086. tVal->auth_mode = atoi(it->second.c_str());
  5087. };
  5088. it = cArr->find("access_user");
  5089. if (it != cArr->end())
  5090. {
  5091. strncpy(tVal->access_user, it->second.c_str(), 48);
  5092. };
  5093. it = cArr->find("access_pass");
  5094. if (it != cArr->end())
  5095. {
  5096. strncpy(tVal->access_pass, it->second.c_str(), 48);
  5097. };
  5098. it = cArr->find("detection_evt_ena");
  5099. if (it != cArr->end())
  5100. {
  5101. tVal->detection_enabled = (bool) atoi(it->second.c_str());
  5102. };
  5103. it = cArr->find("detection_evt_id");
  5104. if (it != cArr->end())
  5105. {
  5106. tVal->detection_src = atoi(it->second.c_str());
  5107. };
  5108. it = cArr->find("camera_evt_ena");
  5109. if (it != cArr->end())
  5110. {
  5111. tVal->camera_enabled = (bool) atoi(it->second.c_str());
  5112. };
  5113. it = cArr->find("camera_evt_id");
  5114. if (it != cArr->end())
  5115. {
  5116. tVal->camera_src = atoi(it->second.c_str());
  5117. };
  5118. it = cArr->find("use_ssl");
  5119. if (it != cArr->end())
  5120. {
  5121. tVal->use_ssl = (bool) atoi(it->second.c_str());
  5122. } else {
  5123. tVal->use_ssl = 1;
  5124. }
  5125. };
  5126. };
  5127. };
  5128. deleteResult(result);
  5129. };
  5130. return tVal;
  5131. }
  5132. BUBBLE_FILTER_DATA * sqliteDBC::getBubbleFilterData(uint32_t id)
  5133. {
  5134. BUBBLE_FILTER_DATA * tVal = NULL;
  5135. if (m_bStoIsConnected)
  5136. {
  5137. char tQBuf[CTEXT];
  5138. AAssoc * result;
  5139. sprintf(tQBuf, "SELECT * FROM `target_filter_bubble` WHERE `id`=%d", id);
  5140. #ifdef DEBUG_ALL
  5141. LOG(tQBuf);
  5142. #endif
  5143. result = m_pDataStorage->Query(tQBuf);
  5144. if (result != NULL)
  5145. {
  5146. if (result->size() > 0)
  5147. {
  5148. Assoc * cArr;
  5149. tVal = (BUBBLE_FILTER_DATA *) malloc(sizeof(BUBBLE_FILTER_DATA));
  5150. if (tVal != NULL)
  5151. {
  5152. memset(tVal, 0, sizeof(BUBBLE_FILTER_DATA));
  5153. ;
  5154. Assoc::iterator it;
  5155. tVal->id = id;
  5156. cArr = result->at(0);
  5157. it = cArr->find("BEGINNING_RANGE");
  5158. if (it != cArr->end())
  5159. {
  5160. tVal->beginning_range = atof(it->second.c_str());
  5161. };
  5162. it = cArr->find("MAX_RANGE");
  5163. if (it != cArr->end())
  5164. {
  5165. tVal->maximal_range = atof(it->second.c_str());
  5166. };
  5167. it = cArr->find("MIN_BLOCK_COUNS");
  5168. if (it != cArr->end())
  5169. {
  5170. tVal->min_block_counts = atoi(it->second.c_str());
  5171. };
  5172. #ifdef BUBBLE_TIME_BUCKET
  5173. it = cArr->find("BUCKET_TIME");
  5174. if (it != cArr->end())
  5175. {
  5176. tVal->bucket_time = atoi(it->second.c_str());
  5177. };
  5178. #endif
  5179. it = cArr->find("TTL");
  5180. if (it != cArr->end())
  5181. {
  5182. tVal->ttl = atoi(it->second.c_str());
  5183. };
  5184. it = cArr->find("T2T");
  5185. if (it != cArr->end())
  5186. {
  5187. tVal->t2t = atoi(it->second.c_str());
  5188. };
  5189. it = cArr->find("PROPOGATE");
  5190. if (it != cArr->end())
  5191. {
  5192. tVal->propogate = atoi(it->second.c_str());
  5193. };
  5194. it = cArr->find("METHOD");
  5195. if (it != cArr->end())
  5196. {
  5197. tVal->method = atoi(it->second.c_str());
  5198. };
  5199. it = cArr->find("ENABLE_EVENT");
  5200. if (it != cArr->end())
  5201. {
  5202. tVal->enable_evt = (bool) atoi(it->second.c_str());
  5203. };
  5204. };
  5205. #ifdef BUBBLE_TIME_BUCKET
  5206. if (tVal->bucket_time < BUBBLE_FILTER_MIN_BUCKET_TIME)
  5207. {
  5208. tVal->bucket_time = BUBBLE_FILTER_MIN_BUCKET_TIME;
  5209. }
  5210. #endif
  5211. };
  5212. };
  5213. deleteResult(result);
  5214. };
  5215. return tVal;
  5216. }
  5217. DBC_CAM_DATA * sqliteDBC::getCamData(uint32_t ID)
  5218. {
  5219. DBC_CAM_DATA * tVal = NULL;
  5220. if (m_bStoIsConnected)
  5221. {
  5222. char tQBuf[CTEXT];
  5223. AAssoc * result;
  5224. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID);
  5225. #ifdef DEBUG_ALL
  5226. LOG(tQBuf);
  5227. #endif
  5228. result = m_pDataStorage->Query(tQBuf);
  5229. if (result != NULL)
  5230. {
  5231. if (result->size() > 0)
  5232. {
  5233. Assoc * cArr;
  5234. tVal = (DBC_CAM_DATA *) malloc(sizeof(DBC_CAM_DATA));
  5235. if (tVal != NULL)
  5236. {
  5237. memset(tVal, 0, sizeof(DBC_CAM_DATA));
  5238. tVal->id = ID;
  5239. Assoc::iterator it;
  5240. cArr = result->at(0);
  5241. it = cArr->find("type");
  5242. if (it != cArr->end())
  5243. {
  5244. tVal->type = atoi(it->second.c_str());
  5245. };
  5246. it = cArr->find("online");
  5247. if (it != cArr->end())
  5248. {
  5249. tVal->online = (bool) atoi(it->second.c_str());
  5250. };
  5251. it = cArr->find("stream_id");
  5252. if (it != cArr->end())
  5253. {
  5254. tVal->stream_id = atoi(it->second.c_str());
  5255. };
  5256. it = cArr->find("hardware");
  5257. if (it != cArr->end())
  5258. {
  5259. strncpy(tVal->hardware, it->second.c_str(), 32);
  5260. };
  5261. it = cArr->find("name");
  5262. if (it != cArr->end())
  5263. {
  5264. strncpy(tVal->name, it->second.c_str(), 64);
  5265. };
  5266. it = cArr->find("status");
  5267. if (it != cArr->end())
  5268. {
  5269. tVal->status = atoi(it->second.c_str());
  5270. };
  5271. it = cArr->find("session");
  5272. if (it != cArr->end())
  5273. {
  5274. strncpy(tVal->session, it->second.c_str(), 32);
  5275. };
  5276. it = cArr->find("protocol");
  5277. if (it != cArr->end())
  5278. {
  5279. tVal->protocol = atoi(it->second.c_str());
  5280. };
  5281. it = cArr->find("protocol_aux");
  5282. if (it != cArr->end())
  5283. {
  5284. tVal->protocol_aux = atoi(it->second.c_str());
  5285. };
  5286. it = cArr->find("uart_id");
  5287. if (it != cArr->end())
  5288. {
  5289. tVal->uart_id = atoi(it->second.c_str());
  5290. };
  5291. it = cArr->find("x");
  5292. if (it != cArr->end())
  5293. {
  5294. tVal->x = atof(it->second.c_str());
  5295. };
  5296. it = cArr->find("y");
  5297. if (it != cArr->end())
  5298. {
  5299. tVal->y = atof(it->second.c_str());
  5300. };
  5301. it = cArr->find("z");
  5302. if (it != cArr->end())
  5303. {
  5304. tVal->z = atof(it->second.c_str());
  5305. };
  5306. it = cArr->find("x_rot");
  5307. if (it != cArr->end())
  5308. {
  5309. tVal->x_rot = atof(it->second.c_str());
  5310. };
  5311. it = cArr->find("y_rot");
  5312. if (it != cArr->end())
  5313. {
  5314. tVal->y_rot = atof(it->second.c_str());
  5315. };
  5316. it = cArr->find("z_rot");
  5317. if (it != cArr->end())
  5318. {
  5319. tVal->z_rot = atof(it->second.c_str());
  5320. };
  5321. it = cArr->find("imager_type");
  5322. if (it != cArr->end())
  5323. {
  5324. tVal->imager_type = atoi(it->second.c_str());
  5325. };
  5326. it = cArr->find("lens_f");
  5327. if (it != cArr->end())
  5328. {
  5329. tVal->lens_f = atoi(it->second.c_str());
  5330. };
  5331. it = cArr->find("ptz_speed_step");
  5332. if (it != cArr->end())
  5333. {
  5334. tVal->ptz_speed_step = atof(it->second.c_str());
  5335. };
  5336. it = cArr->find("pan_model");
  5337. if (it != cArr->end())
  5338. {
  5339. tVal->pan_model = atoi(it->second.c_str());
  5340. };
  5341. it = cArr->find("pan_angles");
  5342. if (it != cArr->end())
  5343. {
  5344. tVal->pan_angles = atoi(it->second.c_str());
  5345. };
  5346. it = cArr->find("tilt_model");
  5347. if (it != cArr->end())
  5348. {
  5349. tVal->tilt_model = atoi(it->second.c_str());
  5350. };
  5351. it = cArr->find("tilt_angles");
  5352. if (it != cArr->end())
  5353. {
  5354. tVal->tilt_angles = atoi(it->second.c_str());
  5355. };
  5356. it = cArr->find("zoom_model");
  5357. if (it != cArr->end())
  5358. {
  5359. tVal->zoom_model = atoi(it->second.c_str());
  5360. };
  5361. it = cArr->find("zoom");
  5362. if (it != cArr->end())
  5363. {
  5364. tVal->zoom = atof(it->second.c_str());
  5365. };
  5366. it = cArr->find("max_range");
  5367. if (it != cArr->end())
  5368. {
  5369. tVal->max_range = atof(it->second.c_str());
  5370. };
  5371. it = cArr->find("access_login");
  5372. if (it != cArr->end())
  5373. {
  5374. strncpy(tVal->username, it->second.c_str(), 16);
  5375. };
  5376. it = cArr->find("access_pass");
  5377. if (it != cArr->end())
  5378. {
  5379. strncpy(tVal->password, it->second.c_str(), 16);
  5380. };
  5381. it = cArr->find("uuid");
  5382. if (it != cArr->end())
  5383. {
  5384. strncpy(tVal->uuid, it->second.c_str(), 64);
  5385. };
  5386. it = cArr->find("mac");
  5387. if (it != cArr->end())
  5388. {
  5389. strncpy(tVal->mac, it->second.c_str(), 16);
  5390. };
  5391. it = cArr->find("ip");
  5392. if (it != cArr->end())
  5393. {
  5394. strncpy(tVal->ip, it->second.c_str(), 16);
  5395. };
  5396. it = cArr->find("port");
  5397. if (it != cArr->end())
  5398. {
  5399. tVal->port = atoi(it->second.c_str());
  5400. };
  5401. it = cArr->find("onvif_access");
  5402. if (it != cArr->end())
  5403. {
  5404. strncpy(tVal->onvif_access, it->second.c_str(), 5);
  5405. };
  5406. it = cArr->find("onvif_ctrl");
  5407. if (it != cArr->end())
  5408. {
  5409. strncpy(tVal->onvif_ctrl, it->second.c_str(), 250);
  5410. };
  5411. it = cArr->find("ptz_token");
  5412. if (it != cArr->end())
  5413. {
  5414. strncpy(tVal->ptz_token, it->second.c_str(), 64);
  5415. };
  5416. it = cArr->find("trigger_mode");
  5417. if (it != cArr->end())
  5418. {
  5419. tVal->triggering_mode = atoi(it->second.c_str());
  5420. };
  5421. it = cArr->find("auth_mode");
  5422. if (it != cArr->end())
  5423. {
  5424. tVal->auth_mode = atoi(it->second.c_str());
  5425. };
  5426. it = cArr->find("ptz_axel");
  5427. if (it != cArr->end())
  5428. {
  5429. tVal->ptz_axel = atoi(it->second.c_str());
  5430. };
  5431. it = cArr->find("manual");
  5432. if (it != cArr->end())
  5433. {
  5434. tVal->manual = (bool) atoi(it->second.c_str());
  5435. };
  5436. it = cArr->find("homing_enable");
  5437. if (it != cArr->end())
  5438. {
  5439. tVal->homing_enable = (bool) atoi(it->second.c_str());
  5440. };
  5441. it = cArr->find("homing_to");
  5442. if (it != cArr->end())
  5443. {
  5444. tVal->homing_to = atoi(it->second.c_str());
  5445. };
  5446. it = cArr->find("home_token");
  5447. if (it != cArr->end())
  5448. {
  5449. strncpy(tVal->home_token, it->second.c_str(), 32);
  5450. };
  5451. it = cArr->find("eco_ir");
  5452. if (it != cArr->end())
  5453. {
  5454. tVal->eco_ir = (bool) atoi(it->second.c_str());
  5455. };
  5456. it = cArr->find("prefered_events_only");
  5457. if (it != cArr->end())
  5458. {
  5459. tVal->prefered_events_only = (bool) atoi(it->second.c_str());
  5460. };
  5461. it = cArr->find("flip_mode");
  5462. if (it != cArr->end())
  5463. {
  5464. tVal->flip_mode = atoi(it->second.c_str());
  5465. };
  5466. };
  5467. };
  5468. };
  5469. deleteResult(result);
  5470. };
  5471. return tVal;
  5472. }
  5473. uint32_t sqliteDBC::getCamId(CSTR UUID)
  5474. {
  5475. uint32_t RetVal = 0;
  5476. if (m_bStoIsConnected)
  5477. {
  5478. char tQBuf[CTEXT];
  5479. AAssoc * result;
  5480. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID);
  5481. #ifdef DEBUG_ALL
  5482. LOG(tQBuf);
  5483. #endif
  5484. result = m_pDataStorage->Query(tQBuf);
  5485. if (result != NULL)
  5486. {
  5487. if (result->size() > 0)
  5488. {
  5489. Assoc * cArr;
  5490. Assoc::iterator it;
  5491. cArr = result->at(0);
  5492. it = cArr->find("id");
  5493. if (it != cArr->end())
  5494. {
  5495. RetVal = atoi(it->second.c_str());
  5496. };
  5497. };
  5498. };
  5499. deleteResult(result);
  5500. };
  5501. return RetVal;
  5502. }
  5503. CAM_PRESET * sqliteDBC::getCamPreset(uint32_t SETID)
  5504. {
  5505. CAM_PRESET * tVal = NULL;
  5506. if (m_bStoIsConnected)
  5507. {
  5508. char tQBuf[CTEXT];
  5509. AAssoc * result;
  5510. sprintf(tQBuf, "SELECT * FROM `cameras_presets` WHERE `id`=%d", SETID);
  5511. #ifdef DEBUG_ALL
  5512. LOG(tQBuf);
  5513. #endif
  5514. result = m_pDataStorage->Query(tQBuf);
  5515. if (result != NULL)
  5516. {
  5517. if (result->size() > 0)
  5518. {
  5519. Assoc * cArr;
  5520. tVal = (CAM_PRESET *) malloc(sizeof(CAM_PRESET));
  5521. if (tVal != NULL)
  5522. {
  5523. memset(tVal, 0, sizeof(CAM_PRESET));
  5524. tVal->id = SETID;
  5525. Assoc::iterator it;
  5526. cArr = result->at(0);
  5527. it = cArr->find("token");
  5528. if (it != cArr->end())
  5529. {
  5530. strncpy(tVal->token, it->second.c_str(), 2);
  5531. tVal->token[3] = (char) 0;
  5532. };
  5533. it = cArr->find("role");
  5534. if (it != cArr->end())
  5535. {
  5536. tVal->role = atoi(it->second.c_str());
  5537. };
  5538. it = cArr->find("row");
  5539. if (it != cArr->end())
  5540. {
  5541. tVal->row = atoi(it->second.c_str());
  5542. };
  5543. it = cArr->find("column");
  5544. if (it != cArr->end())
  5545. {
  5546. tVal->col = atoi(it->second.c_str());
  5547. };
  5548. };
  5549. };
  5550. };
  5551. deleteResult(result);
  5552. };
  5553. return tVal;
  5554. }
  5555. DBC_PRESETS_INFO * sqliteDBC::getCamPresetInfo(uint32_t CamID)
  5556. {
  5557. DBC_PRESETS_INFO * tVal = NULL;
  5558. if (m_bStoIsConnected)
  5559. {
  5560. char tQBuf[CTEXT];
  5561. AAssoc * result;
  5562. sprintf(tQBuf, "SELECT * FROM `cameras_preset_sets` WHERE `camera_id`=%d", CamID);
  5563. #ifdef DEBUG_ALL
  5564. LOG(tQBuf);
  5565. #endif
  5566. result = m_pDataStorage->Query(tQBuf);
  5567. if (result != NULL)
  5568. {
  5569. if (result->size() > 0)
  5570. {
  5571. Assoc * cArr;
  5572. tVal = (DBC_PRESETS_INFO *) malloc(sizeof(DBC_PRESETS_INFO));
  5573. if (tVal != NULL)
  5574. {
  5575. memset(tVal, 0, sizeof(DBC_PRESETS_INFO));
  5576. Assoc::iterator it;
  5577. cArr = result->at(0);
  5578. it = cArr->find("maxCount");
  5579. if (it != cArr->end())
  5580. {
  5581. tVal->maxCount = atoi(it->second.c_str());
  5582. };
  5583. it = cArr->find("p_segs");
  5584. if (it != cArr->end())
  5585. {
  5586. tVal->gCols = atoi(it->second.c_str());
  5587. };
  5588. it = cArr->find("z_segs");
  5589. if (it != cArr->end())
  5590. {
  5591. tVal->gRows = atoi(it->second.c_str());
  5592. };
  5593. };
  5594. };
  5595. };
  5596. deleteResult(result);
  5597. };
  5598. return tVal;
  5599. }
  5600. CSTR sqliteDBC::getDataParam(uint8_t pid)
  5601. {
  5602. switch (pid)
  5603. {
  5604. case 0: return strdup(m_pDbFile); break;
  5605. case 1: return strdup(web_access_user); break;
  5606. case 2: return strdup(web_access_group); break;
  5607. };
  5608. return NULL;
  5609. }
  5610. bool sqliteDBC::updateIPCam(uint32_t CamID, CSTR Addr)
  5611. {
  5612. if (m_bStoIsConnected)
  5613. {
  5614. char tQBuf[MAX_PATH_LEN];
  5615. char tIpBuf[CTEXT];
  5616. char * tPos;
  5617. char * tPos2;
  5618. uint16_t tPort = 0;
  5619. tPos = strstr(Addr, "//");
  5620. if (tPos != NULL)
  5621. {
  5622. tPos2 = strstr(tPos + 2, ":");
  5623. if (tPos2 != NULL)
  5624. {
  5625. //LOG("IP Port Defined!");
  5626. int len;//, len2, len3;
  5627. len = tPos2 - tPos - 2;
  5628. strncpy(tIpBuf, tPos + 2, len);
  5629. tIpBuf[len] = (char) 0;
  5630. tPort = atoi(tPos2 + 1);
  5631. // strncpy( tPath, tPos + 2 + len, CTEXT );
  5632. }
  5633. else
  5634. {
  5635. tPos2 = strstr(tPos + 2, "/");
  5636. if (tPos2 != NULL)
  5637. {
  5638. int len;//, len2, len3;
  5639. len = tPos2 - tPos - 2;
  5640. strncpy(tIpBuf, tPos + 2, len);
  5641. tIpBuf[len] = (char) 0;
  5642. // strncpy( tPath, tPos + 2 + len, CTEXT );
  5643. }
  5644. else
  5645. {
  5646. tPos2 = strstr(tPos + 2, "/");
  5647. //LOG_ERROR("Pos 2 - err");
  5648. tIpBuf[0] = (char) 0;
  5649. };
  5650. };
  5651. }
  5652. else
  5653. {
  5654. //LOG_ERROR("Pos 0 - err");
  5655. tIpBuf[0] = (char) 0;
  5656. };
  5657. if (tPort)
  5658. {
  5659. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d", Addr, tIpBuf, tPort, CamID);
  5660. }
  5661. else
  5662. {
  5663. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d", Addr, tIpBuf, CamID);
  5664. }
  5665. #ifdef DEBUG_ALL
  5666. LOG(tQBuf);
  5667. #endif
  5668. m_pDataStorage->SilentQuery(tQBuf);
  5669. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5670. #ifdef DEBUG_ALL
  5671. LOG(tQBuf);
  5672. #endif
  5673. m_pDataStorage->SilentQuery(tQBuf);
  5674. return true;
  5675. };
  5676. return false;
  5677. }
  5678. bool sqliteDBC::updateCamRange(uint32_t camID, float range)
  5679. {
  5680. if (m_bStoIsConnected)
  5681. {
  5682. char tQBuf[MAX_PATH_LEN];
  5683. sprintf(tQBuf, "UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d", range, camID);
  5684. #ifdef DEBUG_ALL
  5685. LOG(tQBuf);
  5686. #endif
  5687. m_pDataStorage->SilentQuery(tQBuf);
  5688. return true;
  5689. };
  5690. return false;
  5691. }
  5692. bool sqliteDBC::updateCamOnlineStatus(uint32_t camID, bool online)
  5693. {
  5694. if (m_bStoIsConnected)
  5695. {
  5696. char tQBuf[MAX_PATH_LEN];
  5697. if (online)
  5698. {
  5699. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID);
  5700. }
  5701. else
  5702. {
  5703. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID);
  5704. };
  5705. m_pDataStorage->SilentQuery(tQBuf);
  5706. if (online)
  5707. {
  5708. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  5709. }
  5710. else
  5711. {
  5712. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID);
  5713. };
  5714. m_pDataStorage->SilentQuery(tQBuf);
  5715. return true;
  5716. };
  5717. return false;
  5718. }
  5719. bool sqliteDBC::updateIPCam(CSTR uuid, CSTR Addr)
  5720. {
  5721. if (m_bStoIsConnected)
  5722. {
  5723. uint32_t cID = getCamId(uuid);
  5724. if (cID)
  5725. {
  5726. return updateIPCam(cID, Addr);
  5727. };
  5728. };
  5729. return false;
  5730. }
  5731. bool sqliteDBC::updateLocalCam(CSTR uuid)
  5732. {
  5733. if (m_bStoIsConnected)
  5734. {
  5735. uint32_t cID = getCamId(uuid);
  5736. if (cID)
  5737. {
  5738. return updateLocalCam(cID);
  5739. };
  5740. };
  5741. return false;
  5742. }
  5743. bool sqliteDBC::updateLocalCam(uint32_t CamID)
  5744. {
  5745. if (m_bStoIsConnected)
  5746. {
  5747. char tQBuf[MAX_PATH_LEN];
  5748. sprintf(tQBuf, "UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID);
  5749. #ifdef DEBUG_ALL
  5750. LOG(tQBuf);
  5751. #endif
  5752. m_pDataStorage->SilentQuery(tQBuf);
  5753. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5754. #ifdef DEBUG_ALL
  5755. LOG(tQBuf);
  5756. #endif
  5757. m_pDataStorage->SilentQuery(tQBuf);
  5758. return true;
  5759. };
  5760. return false;
  5761. }
  5762. bool sqliteDBC::updateLRFComStat(uint32_t lrfID, uint32_t ErrorPerMinute)
  5763. {
  5764. if (m_bStoIsConnected)
  5765. {
  5766. char tQBuf[MAX_PATH_LEN];
  5767. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID);
  5768. #ifdef DEBUG_ALL
  5769. LOG(tQBuf);
  5770. #endif
  5771. m_pDataStorage->SilentQuery(tQBuf);
  5772. return true;
  5773. };
  5774. return false;
  5775. }
  5776. bool sqliteDBC::updateLRFFrozenStatus(uint32_t lrfID, bool FFT, bool SFT)
  5777. {
  5778. if (m_bStoIsConnected)
  5779. {
  5780. char tQBuf[MAX_PATH_LEN];
  5781. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID);
  5782. #ifdef DEBUG_ALL
  5783. LOG(tQBuf);
  5784. #endif
  5785. m_pDataStorage->SilentQuery(tQBuf);
  5786. return true;
  5787. };
  5788. return false;
  5789. }
  5790. 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)
  5791. {
  5792. if (m_bStoIsConnected)
  5793. {
  5794. char tQBuf[MAX_PATH_LEN];
  5795. if (master)
  5796. {
  5797. sprintf(tQBuf,
  5798. "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 "
  5799. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5800. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5801. }
  5802. else
  5803. {
  5804. sprintf(tQBuf,
  5805. "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 "
  5806. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5807. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5808. };
  5809. #ifdef DEBUG_ALL
  5810. LOG(tQBuf);
  5811. #endif
  5812. m_pDataStorage->SilentQuery(tQBuf);
  5813. return true;
  5814. };
  5815. return false;
  5816. }
  5817. bool sqliteDBC::updateLRFDryContact(uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow)
  5818. {
  5819. if (m_bStoIsConnected)
  5820. {
  5821. char tQBuf[MAX_PATH_LEN];
  5822. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID);
  5823. #ifdef DEBUG_ALL
  5824. LOG(tQBuf);
  5825. #endif
  5826. m_pDataStorage->SilentQuery(tQBuf);
  5827. return true;
  5828. };
  5829. return false;
  5830. }
  5831. bool sqliteDBC::updateLRFSerial(uint32_t lrfID, CSTR Serial)
  5832. {
  5833. if (m_bStoIsConnected)
  5834. {
  5835. char tQBuf[MAX_PATH_LEN];
  5836. CSTR tES = m_pDataStorage->escapeString(Serial);
  5837. if (tES)
  5838. {
  5839. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID);
  5840. free(tES);
  5841. #ifdef DEBUG_ALL
  5842. LOG(tQBuf);
  5843. #endif
  5844. m_pDataStorage->SilentQuery(tQBuf);
  5845. return true;
  5846. };
  5847. };
  5848. return false;
  5849. }
  5850. bool sqliteDBC::updateLRFUpload(uint32_t id, uint8_t status)
  5851. {
  5852. if (m_bStoIsConnected)
  5853. {
  5854. char tQBuf[MAX_PATH_LEN];
  5855. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id);
  5856. #ifdef DEBUG_ALL
  5857. LOG(tQBuf);
  5858. #endif
  5859. m_pDataStorage->SilentQuery(tQBuf);
  5860. return true;
  5861. };
  5862. return false;
  5863. }
  5864. bool sqliteDBC::updateLRFUnfinishedUploads(uint32_t deviceID)
  5865. {
  5866. if (m_bStoIsConnected)
  5867. {
  5868. char tQBuf[MAX_PATH_LEN];
  5869. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID);
  5870. #ifdef DEBUG_ALL
  5871. LOG(tQBuf);
  5872. #endif
  5873. m_pDataStorage->SilentQuery(tQBuf);
  5874. return true;
  5875. };
  5876. return false;
  5877. }
  5878. bool sqliteDBC::updateLRFVerison(uint32_t lrfID, CSTR version)
  5879. {
  5880. if (m_bStoIsConnected)
  5881. {
  5882. char tQBuf[MAX_PATH_LEN];
  5883. CSTR tES = m_pDataStorage->escapeString(version);
  5884. if (tES)
  5885. {
  5886. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID);
  5887. free(tES);
  5888. #ifdef DEBUG_ALL
  5889. LOG(tQBuf);
  5890. #endif
  5891. m_pDataStorage->SilentQuery(tQBuf);
  5892. return true;
  5893. };
  5894. };
  5895. return false;
  5896. }
  5897. uint32_t sqliteDBC::getRegisteredBubbleId(float X, float Y, float Z)
  5898. {
  5899. uint32_t RetVal = 0;
  5900. if (m_bStoIsConnected)
  5901. {
  5902. char tQBuf[MAX_PATH_LEN];
  5903. sprintf(tQBuf,
  5904. "SELECT `id` FROM `target_bubbles` WHERE ( ABS(`CENTER_X`-(%0.3f)) < `BOUNDARY` ) AND "
  5905. "( ABS(`CENTER_Y`-(%0.3f)) < `BOUNDARY` ) AND "
  5906. "( ABS(`CENTER_Z`-(%0.3f)) < `BOUNDARY` ) LIMIT 1",
  5907. X, Y, Z);
  5908. #ifdef DEBUG
  5909. LOG(tQBuf);
  5910. #endif
  5911. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5912. if (result != NULL)
  5913. {
  5914. if (result->size() > 0)
  5915. {
  5916. Assoc * cArr;
  5917. Assoc::iterator it;
  5918. cArr = result->at(0);
  5919. it = cArr->find("id");
  5920. if (it != cArr->end())
  5921. {
  5922. RetVal = atoi(it->second.c_str());
  5923. };
  5924. };
  5925. };
  5926. deleteResult(result);
  5927. };
  5928. return RetVal;
  5929. }
  5930. uint8_t sqliteDBC::updateRegisteredBubble(TARGET_SEGMENT * ptr)
  5931. {
  5932. uint8_t RetVal = 0;
  5933. if (ptr == NULL)
  5934. {
  5935. return RetVal;
  5936. }
  5937. if (m_bStoIsConnected)
  5938. {
  5939. char tQBuf[MAX_PATH_LEN];
  5940. uint32_t tID = getRegisteredBubbleId(ptr->system_center.x, ptr->system_center.y, ptr->system_center.z);
  5941. if (tID)
  5942. {
  5943. // Old record exists
  5944. 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,
  5945. ptr->system_center.z, ptr->boundary, tID);
  5946. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5947. if (result != NULL)
  5948. {
  5949. if (result->size() > 0)
  5950. {
  5951. Assoc * cArr;
  5952. Assoc::iterator it;
  5953. cArr = result->at(0);
  5954. it = cArr->find("BLOCK_COUNS");
  5955. if (it != cArr->end())
  5956. {
  5957. RetVal = atoi(it->second.c_str());
  5958. };
  5959. };
  5960. };
  5961. deleteResult(result);
  5962. }
  5963. else
  5964. {
  5965. // Create New Record for Bubble
  5966. 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);
  5967. m_pDataStorage->SilentQuery(tQBuf);
  5968. };
  5969. };
  5970. return RetVal;
  5971. }
  5972. bool sqliteDBC::updateServerIdentity(uint32_t m_nServerID, CSTR m_sIdentity)
  5973. {
  5974. if (m_bStoIsConnected)
  5975. {
  5976. char tQBuf[MAX_PATH_LEN];
  5977. CSTR tES = m_pDataStorage->escapeString(m_sIdentity);
  5978. if (tES)
  5979. {
  5980. sprintf(tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID);
  5981. free(tES);
  5982. #ifdef DEBUG_ALL
  5983. LOG(tQBuf);
  5984. #endif
  5985. m_pDataStorage->SilentQuery(tQBuf);
  5986. return true;
  5987. };
  5988. };
  5989. return false;
  5990. }
  5991. #undef LOG
  5992. #undef LOG_ERROR