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.

6140 lines
152 KiB

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