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.

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