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.

2113 lines
58 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
12 months ago
4 years ago
​
​
​
​
​
12 months ago
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 hour ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 hour ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
4 years ago
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Event heap: fix hangs on shutdown (signal / web exit) and dispatch deadlocks Why the shutdown could hang: the event heap was never stopped, and its thread called every handler's dispatchEvent() while holding the handler list mutex. ~mojoKernel deletes the node / module factories, filters, media helper ... - all of them event handlers - while the heap kept calling into them (half-destroyed objects, destroyed mutexes); the deleting thread then waited forever in removeHandler() for the mutex the dispatch thread held. A dispatchEvent() that created or deleted a handler deadlocked the dispatch thread itself (non-recursive mutex). basicEventHeap: - Handlers are called without the list locked (snapshot + "still registered" check per handler); removeHandler() waits until the handler is no longer being called (5 s cap, logged), except when called from that very dispatchEvent(). - New pause() / resume() / flush() / clearQueue() and destroyInstance() (stops the thread; leaves it alone if a handler is stuck instead of hanging); existingInstance(); getInstance() is thread-safe. basicEventHandler: - detachEvents(): stop delivery first thing in a destructor (the base destructor runs only after the derived members are gone); basicEventHandler(false) + attachEvents(): register at the end of the constructor (registration in the base constructor lets the heap call into an object whose members are not built yet). Defaults unchanged. - Removal never creates a new heap (late destructors after exit). la_mojo: - ~mojoKernel: deliver queued events (2 s), pause the heap before deleting the components, drop what was queued during the teardown; the next kernel (restart) resumes it. - mojoBackend: destroyInstance() before the log is deleted at exit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 hour ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
​
​
​
​
​
​
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
la_mojo: close the day report at local midnight and open the next day's Before, the report (data/reports/YYYY/MM/DD/report.db, plus pictures and videos stored next to it) only moved to a new day inside updateStats(SESSION_START) - and that branch never ran, because updateStats() skipped every call with evt=false/video=false, which is how all session status changes are reported. So the report only changed day when the process restarted; everything after midnight went into the previous day's report. - mojoKernel::handleDayChange() (every second from validateReports): at local midnight an open session that started on the previous day is closed (END event goes into the old report). If a video segment with events is still recording, wait for it at most DAY_CHANGE_MAX_VIDEO_WAIT (90 s) so the video is stored next to its events. After the END event is written, the report is switched to the new day. The next event starts a new session in the new day's report. - sqliteDBC::switchReportToCurrentDay() / openDayReport(): opens (creates) the current local day's report directory and database, retried until it succeeds. Report state is protected by a recursive mutex because the switch runs on the kernel thread while events are written on the event thread. - updateStats(): no longer drops SESSION_START/END/DROP calls, so the session counters in stat.db are updated and SESSION_START also switches the report if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
3 days ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "mojoKernel.h"
  16. static int getLocalReportPeriod(void);
  17. #include "../common/kernelEventFilterAPF.h"
  18. #include "../common/kernelEventFilterBubble.h"
  19. #include "../common/kernelEventFilterTrain.h"
  20. #include "../common/kernelModuleEmbedVideo.h"
  21. #include "../data/json/json_tool.h"
  22. #include <json/reader.h>
  23. #include <json/value.h>
  24. #include <json/writer.h>
  25. #include <strings.h>
  26. #include <sys/time.h>
  27. #include <time.h>
  28. #ifdef USE_MYSQL
  29. #include "../common/MySQL_dataBinder.h"
  30. #else
  31. #include "../common/SQLite_dataBinder.h"
  32. #endif
  33. #define LOG(X) kernelPrototype::m_pLog->Log("KRNL", this, X)
  34. #ifdef DEBUG
  35. #define LOG_ERROR(X) \
  36. { \
  37. char _tBfrErrMSG234[256]; \
  38. sprintf(_tBfrErrMSG234, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  39. kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, _tBfrErrMSG234); \
  40. }
  41. #else
  42. #define LOG_ERROR(X) kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, X);
  43. #endif
  44. mojoKernel * mojoKernel::_instance = NULL;
  45. mojoKernel * mojoKernel::getInstance(config_setting_t * ptr)
  46. {
  47. if (_instance == NULL)
  48. {
  49. _instance = new mojoKernel(ptr);
  50. };
  51. return _instance;
  52. }
  53. mojoKernel::mojoKernel(config_setting_t * ptr) :
  54. kernelPrototype(ptr)
  55. {
  56. m_pEventHeap = basicEventHeap::getInstance();
  57. m_pEventHeap->resume(); // paused by the previous kernel's destructor (restart)
  58. memset(&m_sSysInfo, 0, sizeof(DBC_SYS_LOAD_INFO));
  59. m_pShadow = NULL;
  60. m_pSystemAnchor = NULL;
  61. m_bSessionStarted = false;
  62. m_bSessionMustPreserve = false;
  63. m_bFirstSession = true;
  64. m_bWorkWithRemoteServer = false;
  65. m_bKeepLocalReport = true;
  66. m_bMediaMode = 0;
  67. m_bApplicationFirstRun = false;
  68. m_bForceCloseSessionOnEndOfTheDay = false;
  69. m_mDateOfToday = getLocalDay();
  70. m_nReportPeriod = getLocalReportPeriod();
  71. m_bReportSwitchPending = false;
  72. m_tReportSwitchDetected = 0;
  73. m_nSessionPeriod = -1;
  74. m_pRPC = NULL;
  75. #ifndef LRF_PROXY_ONLY
  76. m_pMediaHelper = NULL;
  77. #endif
  78. #ifdef EMBED_AUTORESET
  79. m_bEnableAutoReset = false;
  80. m_nSessionCounter = 0;
  81. m_nAutoResetTime = 0;
  82. #endif
  83. #ifdef PEREODIC_SENSORS_RESET
  84. m_bEnablePereodicReset = false;
  85. m_nPereodicResetTime = 0;
  86. m_nPereodicResetCounter = 0;
  87. #endif
  88. // setWM( WM_NONE );
  89. }
  90. mojoKernel::~mojoKernel()
  91. {
  92. WaitFinish(true);
  93. stopSession(false);
  94. // Deliver what is queued (session end, ...), then stop dispatching BEFORE the components are
  95. // deleted: every node / module / filter is an event handler, and the heap kept calling
  96. // dispatchEvent() on objects being destroyed - the shutdown could hang (signal, web exit)
  97. m_pEventHeap->flush(2000);
  98. if (! m_pEventHeap->pause())
  99. {
  100. LOG_ERROR("Event heap did not pause - a handler is stuck in dispatchEvent()");
  101. };
  102. #ifndef LRF_PROXY_ONLY
  103. if ( m_pMediaHelper ) {
  104. delete( m_pMediaHelper );
  105. }
  106. #endif
  107. #ifdef DEBUG
  108. LOG(" Deleting Node Factory...");
  109. #endif
  110. if (m_pNodeFactory != NULL)
  111. {
  112. delete(m_pNodeFactory);
  113. }
  114. #ifdef DEBUG
  115. LOG(" Deleting Module Factory...");
  116. #endif
  117. if (m_pModuleFactory != NULL)
  118. {
  119. delete(m_pModuleFactory);
  120. }
  121. #ifdef DEBUG
  122. LOG(" Deleting Target Filters...");
  123. #endif
  124. unloadFilters();
  125. if (EXIT_CODE_KERNEL_CLEAR == getExitCause())
  126. {
  127. #ifdef DEBUG
  128. LOG("Unregister Server");
  129. #endif
  130. m_pDBC->unregisterServer();
  131. #ifdef DEBUG
  132. // LOG("Close DataBase Connection...");
  133. #endif
  134. // if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  135. // m_pDBC = NULL;
  136. }
  137. else
  138. {
  139. if (EXIT_CODE_DETECT_HARDWARE == getExitCause())
  140. {
  141. #ifdef DEBUG
  142. LOG("HW PNP Detection Requested...");
  143. #endif
  144. // m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  145. m_pHardwareManager->enumerateHardware();
  146. // m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  147. };
  148. #ifdef DEBUG
  149. // LOG("Close DataBase Connection...");
  150. #endif
  151. // if( m_pDBC != NULL ) delete( m_pDBC );
  152. // m_pDBC = NULL;
  153. };
  154. delete(m_pHardwareManager);
  155. // while(1){
  156. // char tB[200];
  157. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  158. // LOG( "BopBop!" );
  159. // SLEEP(1000);
  160. // };
  161. if (m_pRPC)
  162. {
  163. #ifdef DEBUG
  164. LOG("Deleting Remote Server Connection...");
  165. #endif
  166. delete(m_pRPC);
  167. #ifdef DEBUG
  168. LOG("Remote Server Connection Destroyed!");
  169. #endif
  170. }
  171. #ifdef LINUX_KERNEL_WATCHDOG
  172. LOG("Disable WD!");
  173. if (m_iWatchdogFd > -1)
  174. {
  175. char tB[64];
  176. sprintf(tB, "-1\n");
  177. // ToDo: Make Error Handling here!
  178. write(m_iWatchdogFd, &tB, strlen(tB));
  179. };
  180. LOG("WatchDog turned Off!");
  181. #endif
  182. // Events broadcast while the components were deleted must not reach the next kernel (restart)
  183. m_pEventHeap->clearQueue();
  184. _instance = NULL;
  185. }
  186. bool mojoKernel::create()
  187. {
  188. m_bSessionStarted = false;
  189. m_bSessionMustPreserve = false;
  190. m_bFirstSession = true;
  191. if (m_pSettings != NULL)
  192. {
  193. config_setting_t * pSetting;
  194. int tVal;
  195. const char * tBuf;
  196. char tBuffer[128];
  197. LOG("Getting Settings ...");
  198. if (! config_setting_lookup_string(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf))
  199. {
  200. char * tVar;
  201. char * tTS;
  202. tTS = getTStr(NULL);
  203. tVar = MD5String(tTS);
  204. free(tTS);
  205. sprintf(m_sIdentity, tVar);
  206. pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  207. config_setting_set_string(pSetting, tVar);
  208. free(tVar);
  209. basicSettingsProvider::getInstance()->compromise();
  210. LOG("Server Identity not defined - generating new!");
  211. }
  212. else
  213. {
  214. sprintf(m_sIdentity, tBuf);
  215. sprintf(tBuffer, "Server Identity Read From Settings: %s", tBuf);
  216. LOG(tBuffer);
  217. };
  218. if (! config_setting_lookup_int(m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal))
  219. {
  220. tVal = m_nInstanceID = 1;
  221. pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT);
  222. config_setting_set_int(pSetting, tVal);
  223. basicSettingsProvider::getInstance()->compromise();
  224. LOG("Site ID not defined - using [1] as number!");
  225. }
  226. else
  227. {
  228. m_nInstanceID = tVal;
  229. sprintf(tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID);
  230. LOG(tBuffer);
  231. };
  232. #ifdef EMBED_AUTORESET
  233. if (! config_setting_lookup_int(m_pSettings, "AUTORESET", &tVal))
  234. {
  235. m_bEnableAutoReset = false;
  236. tVal = m_nAutoResetTime = 0;
  237. pSetting = config_setting_add(m_pSettings, "AUTORESET", CONFIG_TYPE_INT);
  238. config_setting_set_int(pSetting, tVal);
  239. basicSettingsProvider::getInstance()->compromise();
  240. LOG("Autoreset not defined - Set Disabled!");
  241. }
  242. else
  243. {
  244. m_nAutoResetTime = tVal;
  245. m_bEnableAutoReset = (m_nAutoResetTime > 0);
  246. sprintf(tBuffer, "App Autoreset Read From Settings: %d", m_nAutoResetTime);
  247. LOG(tBuffer);
  248. };
  249. #endif
  250. #ifdef PEREODIC_SENSORS_RESET
  251. if (! config_setting_lookup_int(m_pSettings, "PEREODICRESET", &tVal))
  252. {
  253. m_bEnablePereodicReset = false;
  254. tVal = m_nPereodicResetTime = 0;
  255. pSetting = config_setting_add(m_pSettings, "PEREODICRESET", CONFIG_TYPE_INT);
  256. config_setting_set_int(pSetting, tVal);
  257. basicSettingsProvider::getInstance()->compromise();
  258. LOG("Pereodic Sensor Reset not defined - Set Disabled!");
  259. }
  260. else
  261. {
  262. m_nPereodicResetTime = tVal;
  263. m_bEnablePereodicReset = (m_nPereodicResetTime > 0);
  264. sprintf(tBuffer, "Pereodic Sensor Reset Read From Settings: %d", m_nPereodicResetTime);
  265. LOG(tBuffer);
  266. };
  267. #endif
  268. }
  269. else
  270. {
  271. LOG_ERROR("Failed Read Config Settings!");
  272. return false;
  273. };
  274. basicSettingsProvider::getInstance()->validate();
  275. LOG("Register Server ... ");
  276. m_pDBC->registerServer(m_nInstanceID, m_sIdentity, getpid(), &m_nServerID, &m_nServerNodeID);
  277. char tBuffer[128];
  278. sprintf(tBuffer, "Server Registered as [%d] nodeID[%d]", m_nServerID, m_nServerNodeID);
  279. LOG(tBuffer);
  280. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  281. if (tSiteData)
  282. {
  283. C3D_WGS84_LOC t;
  284. t.lat = tSiteData->lat;
  285. t.lon = tSiteData->lon;
  286. t.alt = tSiteData->alt;
  287. m_pSystemAnchor = systemAnchor::getInstance( &t, tSiteData->local_binding );
  288. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  289. free(tSiteData);
  290. }
  291. else
  292. {
  293. LOG_ERROR("Error Loading Site DATA!");
  294. C3D_WGS84_LOC t;
  295. t.lat = 0;
  296. t.lon = 0;
  297. t.alt = 0;
  298. memset(&m_sSiteData, 0, sizeof(DBC_SITE_DATA));
  299. m_pSystemAnchor = systemAnchor::getInstance( &t, true );
  300. };
  301. m_bKeepLocalReport = ! m_sSiteData.skip_local_report;
  302. m_bDebugMode = m_pDBC->getAppDebugMode();
  303. if (m_bDebugMode)
  304. {
  305. m_bKeepLocalReport = true; // Forced in case of debug mode
  306. }
  307. /// m_bWorkWithRemoteServer = m_sSiteData.use_remote_server; ->
  308. /// since Connection is delayed - must be not set at beginning
  309. // Check date/Time if correct!
  310. // Just in case that we have local reports
  311. if (m_bKeepLocalReport)
  312. {
  313. #ifdef DEBUG
  314. LOG("Working With Local Reports");
  315. #endif
  316. // On some systems the System Date may be in past cause last session was interrupted by power loose
  317. uint32_t tTS = m_pDBC->getLastEventTS();
  318. if (tTS)
  319. {
  320. // Have last TS
  321. struct timespec tS;// = {0}; will filed by gettime function
  322. if (clock_gettime(CLOCK_REALTIME, &tS) == 0)
  323. {
  324. // time_t cTS = time(NULL);
  325. if (tTS > tS.tv_sec)
  326. {
  327. // System Time is in the Past
  328. LOG("Correcting System Time!");
  329. tS.tv_sec = tTS;
  330. tS.tv_nsec = tTS * 1000000;
  331. if (clock_settime(CLOCK_REALTIME, &tS) != 0)
  332. {
  333. LOG_ERROR("Time Correction Failed!");
  334. }
  335. };
  336. };
  337. #ifdef DEBUG
  338. }
  339. else
  340. {
  341. LOG("No Last TS fetched from DB!");
  342. #endif
  343. };
  344. m_bSessionStarted = m_pDBC->getEventSessionStatus();//m_nServerNodeID);
  345. stopSession(false);
  346. };
  347. m_pHardwareManager = hardwareManager::getInstance();
  348. loadFilters();
  349. m_pModuleFactory = kernelModuleFactory::getInstance(m_nInstanceID);
  350. if (! m_pModuleFactory)
  351. {
  352. LOG_ERROR("Module Factory Failure!");
  353. return false;
  354. };
  355. m_pModuleFactory->loadModules();
  356. // Reald Media Mode ( Video/Pictures )
  357. #ifndef LRF_PROXY_ONLY
  358. m_pMediaHelper = new kernelHelperMediaMTX();
  359. if ( m_pMediaHelper ) {
  360. m_pMediaHelper->create();
  361. };
  362. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  363. if (tMO)
  364. {
  365. m_bMediaMode = tMO->getMediaMode();
  366. }
  367. else
  368. {
  369. LOG_ERROR("No Embed Video Processing Module!");
  370. };
  371. #else
  372. LOG("MOJO is DATA PROXY Processing Module!");
  373. m_bMediaMode = 0;
  374. #endif
  375. m_pNodeFactory = deviceNodeFactory::getInstance();
  376. if (! m_pNodeFactory)
  377. {
  378. LOG_ERROR("Node Factory Failure!");
  379. return false;
  380. };
  381. if ((m_bApplicationFirstRun = m_pDBC->isDatabaseNew()))
  382. {
  383. LOG("DATABASE IS NEW - Will Detect Hardware!");
  384. }
  385. else
  386. {
  387. LOG("DATABASE IS OLD - Will SKIP Detection of Hardware!");
  388. }
  389. // Must Be moved elsewhere To be connected after all Nodes are Loadded ?? at Node's Validation ??
  390. /*
  391. if ( m_bWorkWithRemoteServer ) {
  392. #ifdef DEBUG
  393. LOG("Working With Remote Server");
  394. #endif
  395. if ( m_pRPC = new mojoRemoteClient( this ) ) {
  396. if( m_bWorkWithRemoteServer = m_pRPC->create() ){
  397. } else LOG_ERROR("Cannot Connect to Remote Server!");
  398. } else m_bWorkWithRemoteServer = false;
  399. };
  400. */
  401. return init();
  402. }
  403. #include "../common/deviceNode2D.h"
  404. void mojoKernel::releaseExplicitChannel( uint32_t node_id, uint32_t channel_id ){
  405. if ( m_pNodeFactory ) {
  406. deviceNode ** tA = NULL;
  407. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  408. for ( uint32_t i = 0; i < tCNT; i++ ) {
  409. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  410. };
  411. if (tCNT){
  412. free(tA);
  413. tA = NULL;
  414. };
  415. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  416. for ( uint32_t i = 0; i < tCNT; i++ ) {
  417. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  418. };
  419. if (tCNT){
  420. free(tA);
  421. tA = NULL;
  422. };
  423. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  424. for ( uint32_t i = 0; i < tCNT; i++ ) {
  425. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  426. };
  427. if (tCNT){
  428. free(tA);
  429. tA = NULL;
  430. };
  431. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  432. for ( uint32_t i = 0; i < tCNT; i++ ) {
  433. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelReady( channel_id );
  434. };
  435. if (tCNT){
  436. free(tA);
  437. tA = NULL;
  438. };
  439. };
  440. }
  441. void mojoKernel::requestExplicitChannel( uint32_t node_id, uint32_t channel_id, uint8_t seconds_to ){
  442. if ( m_pNodeFactory ) {
  443. deviceNode ** tA = NULL;
  444. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  445. for ( uint32_t i = 0; i < tCNT; i++ ) {
  446. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  447. };
  448. if (tCNT){
  449. free(tA);
  450. tA = NULL;
  451. };
  452. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  453. for ( uint32_t i = 0; i < tCNT; i++ ) {
  454. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  455. };
  456. if (tCNT){
  457. free(tA);
  458. tA = NULL;
  459. };
  460. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  461. for ( uint32_t i = 0; i < tCNT; i++ ) {
  462. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  463. };
  464. if (tCNT){
  465. free(tA);
  466. tA = NULL;
  467. };
  468. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  469. for ( uint32_t i = 0; i < tCNT; i++ ) {
  470. if ( tA[i]->getNodeID() != node_id ) ( (device2DSensor *)tA[i] )->setChannelBusy( channel_id, seconds_to );
  471. };
  472. if (tCNT){
  473. free(tA);
  474. tA = NULL;
  475. };
  476. };
  477. }
  478. void mojoKernel::startSession()
  479. {
  480. m_mSessionProtector.lock();
  481. m_nNoAlarmCounter = 0;
  482. if (! m_bSessionStarted)
  483. {
  484. if (! m_bFirstSession)
  485. {
  486. if (! m_bSessionMustPreserve)
  487. {
  488. m_pDBC->dropSession();
  489. }
  490. m_bSessionMustPreserve = false;
  491. }
  492. else
  493. {
  494. m_bFirstSession = false;
  495. }
  496. uint64_t TS_64 = getMilliseconds() - 20; // -20 msec before
  497. uint32_t ts_s = TS_64 / 1000;
  498. uint16_t ts_ms = TS_64 % 1000;
  499. EVENT_CONTAINER * bEVT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_LRF_START_SESSION, NULL, NULL, NULL, ts_s, ts_ms);
  500. if (bEVT)
  501. {
  502. m_pEventHeap->broadcast(bEVT);
  503. };
  504. m_bSessionStarted = true;
  505. m_nSessionPeriod = getLocalReportPeriod();
  506. #ifdef EMBED_AUTORESET
  507. m_nSessionCounter = 0;
  508. #endif
  509. };
  510. m_mSessionProtector.unlock();
  511. }
  512. void mojoKernel::stopSession(bool forced)
  513. {
  514. m_mSessionProtector.lock();
  515. if (m_bSessionStarted)
  516. {
  517. uint64_t TS_64 = getMilliseconds() + 20; // +20 msec after
  518. uint32_t ts_s = TS_64 / 1000;
  519. uint16_t ts_ms = TS_64 % 1000;
  520. EVENT_CONTAINER * bEVT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, forced ? EVT_LRF_END_SESSION_MA : EVT_LRF_END_SESSION_TO, NULL, NULL, NULL, ts_s, ts_ms);
  521. if (bEVT)
  522. {
  523. m_pEventHeap->broadcast(bEVT);
  524. };
  525. m_bSessionStarted = false;
  526. #ifdef EMBED_AUTORESET
  527. m_nSessionCounter = 0;
  528. #endif
  529. };
  530. m_mSessionProtector.unlock();
  531. }
  532. void mojoKernel::validateSession(){
  533. m_mSessionProtector.lock();
  534. if( m_bSessionStarted ){
  535. if( m_nNoAlarmCounter < NOTARGET_SESSION_CLOSE ){
  536. m_nNoAlarmCounter++;
  537. } else {
  538. m_mSessionProtector.unlock();
  539. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  540. m_mSessionProtector.lock();
  541. };
  542. #ifdef EMBED_AUTORESET
  543. if (m_bEnableAutoReset) {
  544. if (m_nSessionCounter < m_nAutoResetTime) {
  545. m_nSessionCounter++;
  546. } else {
  547. m_nSessionCounter = 0;
  548. // Reset All Sensors
  549. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  550. if (tL){
  551. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  552. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  553. };
  554. delete(tL);
  555. };
  556. m_nNoAlarmCounter = NOTARGET_SESSION_CLOSE; // Session will be closed on next validation
  557. };
  558. };
  559. #endif
  560. };
  561. m_mSessionProtector.unlock();
  562. #ifdef PEREODIC_SENSORS_RESET
  563. if ( m_bEnablePereodicReset ) {
  564. m_nPereodicResetCounter++;
  565. if ( m_nPereodicResetCounter > m_nPereodicResetTime ) {
  566. m_nPereodicResetCounter = 0;
  567. // Reset All Sensors
  568. #ifdef DEBUG
  569. LOG("Pereodic Reset to All Sensors");
  570. #endif
  571. DBC_INT_LIST * tL = m_pDBC->listNodes(false);
  572. if (tL){
  573. for (DBC_INT_LIST::iterator idx = tL->begin(); idx < tL->end(); idx++){
  574. m_pDBC->pushMemCMD(*idx, 1, "'A'");
  575. };
  576. delete(tL);
  577. };
  578. };
  579. };
  580. #endif
  581. }
  582. #ifdef __MOJO_REMOTE_CLIENT_HEADER__
  583. bool mojoKernel::getTrainPresence()
  584. {
  585. bool RetValue = false;
  586. if (m_vTargetFilters.size())
  587. {
  588. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  589. {
  590. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  591. {
  592. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  593. RetValue = tFT->getTrainStatus();
  594. break;
  595. };
  596. };
  597. };
  598. return RetValue;
  599. }
  600. bool mojoKernel::fillMojoInfo(ARGUS_DATA_MOJO_INFO * ptr)
  601. {
  602. if (ptr == NULL)
  603. {
  604. return false;
  605. }
  606. memset(ptr, 0, sizeof(ARGUS_DATA_MOJO_INFO));
  607. ptr->MOJO_WORK_MODE = (m_bKeepLocalReport ? 4 : 0) | (m_bMediaMode ? 2 : 0) | (m_bDebugMode ? 1 : 0);
  608. if (m_vTargetFilters.size())
  609. {
  610. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  611. {
  612. #ifdef DEBUG_EXTRA
  613. LOG("check Filter");
  614. #endif
  615. if ((*idx)->getType() == TF_LA_METRO_TRAIN_FILTER)
  616. {
  617. #ifdef DEBUG_EXTRA
  618. LOG("T Filter Found!");
  619. #endif
  620. kernelEventFilterTrain * tFT = (kernelEventFilterTrain *) (*idx);
  621. ptr->UPPER_ID = tFT->getUpperId();
  622. ptr->BOTTOM_ID = tFT->getBottomId();
  623. ptr->MOJO_WORK_MODE |= (tFT->getMode() << 3);
  624. break;
  625. };
  626. };
  627. };
  628. #ifndef LRF_PROXY_ONLY
  629. if ( m_pMediaHelper ) {
  630. m_pMediaHelper->validateService();
  631. };
  632. #endif
  633. deviceNode ** tA = NULL;
  634. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  635. if (tCNT)
  636. {
  637. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  638. // ptr->CAM_ID = tCAM->getNodeID();
  639. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  640. // ptr->CAM_ID = tA[0]->getDeviceID();
  641. ptr->CAM_ID = tA[0]->getNodeID();
  642. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  643. }
  644. else
  645. {
  646. LOG("FIMO Cannot Get ARGUS_ANALOG_STATIC_CAMERA Camera!");
  647. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
  648. if (tCNT)
  649. {
  650. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  651. // ptr->CAM_ID = tCAM->getNodeID();
  652. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  653. // ptr->CAM_ID = tA[0]->getDeviceID();
  654. ptr->CAM_ID = tA[0]->getNodeID();
  655. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  656. }
  657. else
  658. {
  659. LOG("FIMO Cannot Get ARGUS_ANALOG_PTZ_CAMERA Camera!");
  660. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
  661. if (tCNT)
  662. {
  663. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  664. // ptr->CAM_ID = tCAM->getNodeID();
  665. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  666. // ptr->CAM_ID = tA[0]->getDeviceID();
  667. ptr->CAM_ID = tA[0]->getNodeID();
  668. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  669. }
  670. else
  671. {
  672. LOG("FIMO Cannot Get ARGUS_IP_STATIC_CAMERA Camera!");
  673. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
  674. if (tCNT)
  675. {
  676. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  677. // ptr->CAM_ID = tCAM->getNodeID();
  678. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  679. // ptr->CAM_ID = tA[0]->getDeviceID();
  680. ptr->CAM_ID = tA[0]->getNodeID();
  681. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  682. }
  683. else
  684. {
  685. LOG("FIMO Cannot Get ARGUS_IP_PTZ_CAMERA Camera!");
  686. tCNT = m_pNodeFactory->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
  687. if (tCNT)
  688. {
  689. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  690. // ptr->CAM_ID = tCAM->getNodeID();
  691. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  692. // ptr->CAM_ID = tA[0]->getDeviceID();
  693. ptr->CAM_ID = tA[0]->getNodeID();
  694. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  695. }
  696. else
  697. {
  698. LOG("FIMO Cannot Get ARGUS_IP_PRESET_CAMERA Camera!");
  699. tCNT = m_pNodeFactory->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
  700. if (tCNT)
  701. {
  702. // deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[0];
  703. // ptr->CAM_ID = tCAM->getNodeID();
  704. // ptr->CAM_STREAM_ID = tCAM->getCamStream();
  705. // ptr->CAM_ID = tA[0]->getDeviceID();
  706. ptr->CAM_ID = tA[0]->getNodeID();
  707. ptr->CAM_STREAM_ID = tA[0]->getDeviceParam();
  708. }
  709. else
  710. {
  711. LOG("FIMO Cannot Get ARGUS_ANALOG_PRESET_CAMERA Camera!");
  712. LOG("No Camera Defined In the system!");
  713. };
  714. };
  715. };
  716. };
  717. };
  718. };
  719. if (tCNT)
  720. {
  721. free(tA);
  722. tA = NULL;
  723. };
  724. ptr->CPU_COUNT = m_sSysInfo.CPU_CORES;
  725. ptr->TOT_DISK = m_sSysInfo.T_DISK;
  726. ptr->TOT_MEMO = m_sSysInfo.T_MEM;
  727. strncpy(ptr->TLS_VERSION, _BUILD_STRING_, 64);
  728. ptr->TLS_VERSION[63] = (char) 0;
  729. strncpy(ptr->LLS_VERSION, m_pHardwareManager->getSysInfo()->OS_INFO, 64);
  730. ptr->LLS_VERSION[63] = (char) 0;
  731. LOG("FIMO Overall info");
  732. return true;
  733. }
  734. #endif
  735. void mojoKernel::getSystemUtilization(DBC_SYS_LOAD_INFO * ptr)
  736. {
  737. if (ptr == NULL)
  738. {
  739. return;
  740. }
  741. m_mSysInfoProtector.lock();
  742. memcpy(ptr, &m_sSysInfo, sizeof(DBC_SYS_LOAD_INFO));
  743. m_mSysInfoProtector.unlock();
  744. }
  745. void mojoKernel::m_fThreadProc(void){
  746. time_t tTimeTSeconds = 0;
  747. m_bThreadStatus = TH_WORKING;
  748. pthread_setname_np( pthread_self(), "Kernel" );
  749. while (m_bThreadStatus < TH_ENDED){
  750. m_tTimeTSeconds = time(NULL);
  751. // Each Second
  752. if (m_tTimeTSeconds != tTimeTSeconds){
  753. tTimeTSeconds = m_tTimeTSeconds;
  754. if (m_bApplicationFirstRun){
  755. if (m_pHardwareManager != NULL){
  756. LOG_ERROR("Restart and Detect HardWare!");
  757. // Must be shutdowned all service that uses hardware prior to detection!
  758. m_bThreadStatus = TH_ENDED;
  759. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  760. continue;
  761. };
  762. };
  763. #ifdef LINUX_KERNEL_WATCHDOG
  764. if (m_iWatchdogFd > -1){
  765. char tB[64];
  766. sprintf(tB, "4000\n");
  767. // ToDo: Make Error Handling here!
  768. write(m_iWatchdogFd, &tB, strlen(tB));
  769. };
  770. #endif
  771. m_bDebugMode = m_pDBC->getAppDebugMode();
  772. validateFilters();
  773. validateSession();
  774. validateReports();
  775. //??
  776. if (! m_pHardwareManager->validate()){
  777. LOG("Reload Mojo Node Due to Memory Consumption!");
  778. m_bThreadStatus = TH_ENDED;
  779. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  780. continue;
  781. };
  782. m_mSysInfoProtector.lock();
  783. m_pHardwareManager->updateSysInfo(&m_sSysInfo);
  784. m_mSysInfoProtector.unlock();
  785. #ifndef LRF_PROXY_ONLY
  786. if ( m_pMediaHelper ) {
  787. m_pMediaHelper->validateService();
  788. };
  789. #endif
  790. if (m_sSiteData.use_remote_server){
  791. if (m_pRPC){
  792. m_pRPC->validate();
  793. m_bWorkWithRemoteServer = (m_pRPC->getConnectionStatus() == CS_CONNECTED);
  794. } else {
  795. // Checking if Nodes are Loadded
  796. if (m_pNodeFactory->nodesAreLoaded()) {
  797. #ifdef DEBUG
  798. LOG("Initial Connect to Remote Server");
  799. #endif
  800. if ((m_pRPC = new mojoRemoteClient(this))) {
  801. if (! (m_bWorkWithRemoteServer = m_pRPC->create())) {
  802. LOG_ERROR("Cannot Connect to Remote Server!");
  803. }
  804. } else {
  805. m_bWorkWithRemoteServer = false;
  806. }
  807. };
  808. };
  809. };
  810. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD(m_nServerNodeID); // Kernel
  811. if (tCMD != NULL) {
  812. m_pDBC->updateNodePushedCMD(tCMD->id, DBC_CMD_STARTED);
  813. switch (tCMD->CMD) {
  814. case CMD_KRNL_RELOAD:{
  815. LOG("Reload Mojo Node!");
  816. m_bThreadStatus = TH_ENDED;
  817. setExitCause(EXIT_CODE_RELOAD_SERVICES);
  818. break;
  819. };
  820. case CMD_KRNL_RESTART:{
  821. LOG("Restart Mojo Node!");
  822. m_bThreadStatus = TH_ENDED;
  823. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  824. break;
  825. };
  826. case CMD_KRNL_FACTORY_RESET:{
  827. LOG("Restart Mojo -> Factory Reset!");
  828. m_bThreadStatus = TH_ENDED;
  829. setExitCause(EXIT_CODE_FACTORY_RESET);
  830. break;
  831. };
  832. case CMD_NODES_RELOAD:{
  833. if (m_pNodeFactory != NULL){
  834. m_pNodeFactory->reloadNodes();
  835. };
  836. break;
  837. };
  838. case CMD_NODE_RELOAD:{
  839. uint32_t tNodeID = atoi(tCMD->PARAM);
  840. if (m_pNodeFactory != NULL){
  841. m_pNodeFactory->reloadNode(tNodeID);
  842. };
  843. break;
  844. };
  845. case CMD_KRNL_PNP:{
  846. if (m_pHardwareManager != NULL){
  847. LOG_ERROR("Restart and Detect HardWare!");
  848. // Must be shutdowned all service that uses hardware prior to detection!
  849. m_bThreadStatus = TH_ENDED;
  850. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  851. };
  852. break;
  853. };
  854. case CMD_KRNL_UPDATE_IDENT:{
  855. LOG("Restart Mojo -> Update Ident!");
  856. sprintf(m_sIdentity, tCMD->PARAM);
  857. config_setting_remove(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY);
  858. config_setting_t * pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  859. config_setting_set_string(pSetting, tCMD->PARAM);
  860. basicSettingsProvider::getInstance()->compromise();
  861. basicSettingsProvider::getInstance()->validate();
  862. m_pDBC->updateServerIdentity(m_nServerID, m_sIdentity);
  863. m_bThreadStatus = TH_ENDED;
  864. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  865. break;
  866. };
  867. case CMD_KRNL_REPLACE_DATABASE:{
  868. LOG("Restart Mojo -> Replace Database!");
  869. CSTR tNAME = m_pDBC->getDataParam(0);
  870. CSTR tUSER = m_pDBC->getDataParam(1);
  871. CSTR tGROUP = m_pDBC->getDataParam(2);
  872. if (tNAME){
  873. char tB[512];
  874. sprintf(tB, "./data/%s.db", tCMD->PARAM);
  875. if (fileExist(tB)){
  876. LOG("Restart Mojo -> Close Database!");
  877. // Closing Database
  878. delete(m_pDBC);
  879. m_pDBC = NULL;
  880. LOG("Restart Mojo -> Delete Database!");
  881. // unlink( tNAME );
  882. LOG("Restart Mojo -> Copy Database!");
  883. fileCopy(tB, tNAME);
  884. LOG("Restart Mojo -> Chown Database!");
  885. ch_chown(tNAME, tUSER, tGROUP);
  886. LOG("Restart Mojo -> Delete Old Database!");
  887. unlink(tB);
  888. m_bThreadStatus = TH_ENDED;
  889. LOG("Restart Mojo -> Exit!");
  890. setExitCause(EXIT_CODE_NO_DATABASE); // EXIT_CODE_RELOAD_SERVICES );
  891. m_bThreadStatus = TH_ENDED;
  892. } else {
  893. LOG_ERROR("New Database Does not Exists!");
  894. };
  895. // free( tNAME );
  896. // free( tUSER );
  897. // free( tGROUP );
  898. };
  899. break;
  900. };
  901. case CMD_KRNL_SYNC_TIME:{
  902. uint32_t uts = atoi(tCMD->PARAM);
  903. if (uts){
  904. #if (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 31)
  905. time_t ts = (time_t) uts;
  906. if (stime(&ts) == 0){
  907. // Restert Server;
  908. m_bThreadStatus = TH_ENDED;
  909. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  910. };
  911. #else
  912. timespec ts;
  913. ts.tv_sec = (time_t) uts;
  914. ts.tv_nsec = 0;
  915. if (clock_settime(CLOCK_REALTIME, &ts) == 0){
  916. // Restert Server;
  917. m_bThreadStatus = TH_ENDED;
  918. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  919. };
  920. #endif
  921. };
  922. break;
  923. };
  924. case CMD_KRNL_SYSTEM_TASK:{
  925. break;
  926. };
  927. case NODE_FORCE_FINISH_ALARM:{
  928. stopSession();
  929. break;
  930. };
  931. case CMD_FORCE_FREEZE_BUBBLES:{
  932. LOG("Freeze Bubbles NCQ");
  933. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_BLOCK);
  934. if (tTT){
  935. m_pEventHeap->broadcast(tTT);
  936. };
  937. break;
  938. };
  939. case CMD_FORCE_POP_BUBBLE:{
  940. LOG("POP Bubbles NCQ");
  941. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_DEL_FB);
  942. if (tTT){
  943. m_pEventHeap->broadcast(tTT);
  944. };
  945. break;
  946. };
  947. case CMD_FORCE_RELOAD_BUBBLES:{
  948. LOG("Reload Bubbles NCQ");
  949. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_RELOAD);
  950. if (tTT){
  951. m_pEventHeap->broadcast(tTT);
  952. };
  953. break;
  954. };
  955. case CMD_FORCE_SENORS_RESET:{
  956. LOG("Reset All Sensors NCQ");
  957. if ( m_pNodeFactory ) {
  958. deviceNode ** tA = NULL;
  959. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  960. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  961. if (tCNT){
  962. free(tA);
  963. tA = NULL;
  964. };
  965. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  966. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  967. if (tCNT){
  968. free(tA);
  969. tA = NULL;
  970. };
  971. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  972. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  973. if (tCNT){
  974. free(tA);
  975. tA = NULL;
  976. };
  977. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  978. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  979. if (tCNT){
  980. free(tA);
  981. tA = NULL;
  982. };
  983. };
  984. stopSession();
  985. break;
  986. };
  987. };
  988. free(tCMD);
  989. };
  990. if ( m_pDBC ){
  991. m_pDBC->cleanNodePushedCMD();
  992. }
  993. };
  994. SLEEP(SLEEP_KERNEL_INTERVAL);
  995. };
  996. }
  997. void mojoKernel::loadFilters()
  998. {
  999. if (m_pDBC == NULL)
  1000. {
  1001. return;
  1002. }
  1003. #ifdef DEBUG
  1004. LOG("Loadding Filters ...");
  1005. #endif
  1006. DBC_INT_LIST * tList = m_pDBC->listFilters(m_nServerID);
  1007. if (tList != NULL)
  1008. {
  1009. if (tList->size() > 0)
  1010. {
  1011. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  1012. {
  1013. #ifdef DEBUG_EXTRA
  1014. LOG("Load Filter!");
  1015. #endif
  1016. DBC_TARGET_FLITER_DATA * tData = NULL;
  1017. tData = m_pDBC->getTargetFilterData(*idx);
  1018. if (tData != NULL)
  1019. {
  1020. #ifdef DEBUG_EXTRA
  1021. LOG("Filter Data OK!");
  1022. #endif
  1023. switch (tData->type)
  1024. {
  1025. case TF_LA_METRO_TRAIN_FILTER:
  1026. {
  1027. #ifdef DEBUG
  1028. LOG("Create Train Filter");
  1029. #endif
  1030. kernelEventFilterTrain * tFilter = new kernelEventFilterTrain(*idx);
  1031. if (tFilter != NULL)
  1032. {
  1033. if (tFilter->initFilter())
  1034. {
  1035. #ifdef DEBUG
  1036. LOG("Add Train Filter");
  1037. #endif
  1038. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1039. }
  1040. else
  1041. {
  1042. #ifdef DEBUG
  1043. LOG_ERROR("Error in Filter Init!");
  1044. #endif
  1045. delete(tFilter);
  1046. };
  1047. };
  1048. break;
  1049. };
  1050. case TF_AUTHORISED_PEOPLE_FILTER:
  1051. {
  1052. LOG("Create APF");
  1053. kernelEventFilterAPF * tFilter = new kernelEventFilterAPF(*idx);
  1054. if (tFilter != NULL)
  1055. {
  1056. if (tFilter->initFilter())
  1057. {
  1058. #ifdef DEBUG
  1059. LOG("Add APF");
  1060. #endif
  1061. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1062. }
  1063. else
  1064. {
  1065. #ifdef DEBUG
  1066. LOG_ERROR("Error in Filter Init!");
  1067. #endif
  1068. delete(tFilter);
  1069. };
  1070. };
  1071. break;
  1072. };
  1073. case TF_BUBBLE_SEGMENTATION_FILTER:
  1074. {
  1075. #ifdef DEBUG
  1076. LOG("Create Bubble Filter");
  1077. #endif
  1078. kernelEventFilterBubble * tFilter = new kernelEventFilterBubble(*idx);
  1079. if (tFilter != NULL)
  1080. {
  1081. #ifdef DEBUG_EXTRA
  1082. LOG("Init Bubble Filter");
  1083. #endif
  1084. if (tFilter->initFilter())
  1085. {
  1086. #ifdef DEBUG
  1087. LOG("Add Bubble Filter");
  1088. #endif
  1089. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1090. }
  1091. else
  1092. {
  1093. #ifdef DEBUG
  1094. LOG_ERROR("Error in Filter Init!");
  1095. #endif
  1096. delete(tFilter);
  1097. };
  1098. };
  1099. break;
  1100. };
  1101. case TF_YOLO_FILTER:
  1102. {
  1103. break;
  1104. };
  1105. default: LOG("Unknoun Filter"); break;
  1106. };
  1107. #ifdef DEBUG_EXTRA
  1108. LOG("Delete Filter Data");
  1109. #endif
  1110. free(tData);
  1111. };
  1112. };
  1113. #ifdef DEBUG_EXTRA
  1114. LOG("Clear Filter List");
  1115. #endif
  1116. tList->clear();
  1117. };
  1118. #ifdef DEBUG_EXTRA
  1119. LOG("Delete Filter List");
  1120. #endif
  1121. delete(tList);
  1122. #ifdef DEBUG
  1123. }
  1124. else
  1125. {
  1126. LOG("No Target Filters Assigned!");
  1127. #endif
  1128. };
  1129. #ifdef DEBUG
  1130. LOG("Done Load Filters!");
  1131. #endif
  1132. }
  1133. void mojoKernel::unloadFilters()
  1134. {
  1135. if (m_vTargetFilters.size() > 0)
  1136. {
  1137. #ifdef DEBUG
  1138. LOG("Have Filters to Unload");
  1139. #endif
  1140. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1141. {
  1142. switch ((*idx)->getType())
  1143. {
  1144. case TF_LA_METRO_TRAIN_FILTER:
  1145. {
  1146. delete((kernelEventFilterTrain *) (*idx));
  1147. break;
  1148. };
  1149. case TF_AUTHORISED_PEOPLE_FILTER:
  1150. {
  1151. delete((kernelEventFilterAPF *) (*idx));
  1152. break;
  1153. };
  1154. case TF_BUBBLE_SEGMENTATION_FILTER:
  1155. {
  1156. delete((kernelEventFilterBubble *) (*idx));
  1157. break;
  1158. };
  1159. case TF_YOLO_FILTER:
  1160. {
  1161. // ToDo: Must be changed according to Steph's code
  1162. // delete( (kernelEventFilterYOLO*)(*idx) );
  1163. break;
  1164. };
  1165. default:
  1166. {
  1167. char tBuffer[64];
  1168. sprintf(tBuffer, "Unknown Filter: %d", (*idx)->getType());
  1169. LOG(tBuffer);
  1170. };
  1171. };
  1172. };
  1173. m_vTargetFilters.clear();
  1174. LOG("Unload Filters Done!");
  1175. #ifdef DEBUG
  1176. }
  1177. else
  1178. {
  1179. LOG("No Filters to Unload!");
  1180. #endif
  1181. };
  1182. }
  1183. void mojoKernel::validateFilters()
  1184. {
  1185. if (m_vTargetFilters.size() > 0)
  1186. {
  1187. #ifdef DEBUG_EXTRA
  1188. LOG("Filters Validation");
  1189. #endif
  1190. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1191. {
  1192. if (! (*idx)->validate())
  1193. {
  1194. LOG_ERROR("Filter is Invalid!");
  1195. /*switch ((*idx)->getType()) {
  1196. case TF_LA_METRO_TRAIN_FILTER:{
  1197. delete( (kernelEventFilterTrain*)(*idx) );
  1198. break;
  1199. };
  1200. case TF_AUTHORISED_PEOPLE_FILTER:{
  1201. delete( (kernelEventFilterAPF*)(*idx) );
  1202. break;
  1203. };
  1204. default:{
  1205. char tBuffer[64];
  1206. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  1207. LOG( tBuffer );
  1208. };
  1209. };
  1210. */
  1211. };
  1212. };
  1213. };
  1214. }
  1215. bool mojoKernel::dispatchEvent(EVENT_CONTAINER * Evt)
  1216. {
  1217. if (Evt == NULL)
  1218. {
  1219. return false;
  1220. }
  1221. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1222. {
  1223. m_pRPC->addEvent(Evt);
  1224. }
  1225. if (! m_bKeepLocalReport)
  1226. {
  1227. return true;
  1228. }
  1229. if (m_pDBC == NULL)
  1230. {
  1231. return false;
  1232. }
  1233. if( m_bForceCloseSessionOnEndOfTheDay && m_bSessionStarted ) {
  1234. #ifndef LRF_PROXY_ONLY
  1235. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1236. if( tMO ){
  1237. if( !tMO->isProcessingVideoStream() ) {
  1238. stopSession();
  1239. m_bForceCloseSessionOnEndOfTheDay = false;
  1240. };
  1241. } else {
  1242. LOG_ERROR("No Embed Video Processing Module!");
  1243. };
  1244. #else
  1245. stopSession();
  1246. m_bForceCloseSessionOnEndOfTheDay = false;
  1247. #endif
  1248. }
  1249. switch (Evt->m_eClass)
  1250. {
  1251. case EVT_CLASS_SYSTEM:
  1252. switch (Evt->m_eType)
  1253. {
  1254. case EVT_LRF_START_SESSION:
  1255. case EVT_LRF_END_SESSION_TO:
  1256. case EVT_LRF_END_SESSION_MA:
  1257. {
  1258. DBC_EVT_RECORD tRecord;
  1259. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1260. tRecord.ts_s = Evt->m_UMSTS / 1000;
  1261. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  1262. tRecord.node_id = m_nServerNodeID;
  1263. tRecord.type = Evt->m_eType;
  1264. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1265. m_pDBC->addEvent(&tRecord);
  1266. break;
  1267. };
  1268. default: break;
  1269. };
  1270. break;
  1271. /*
  1272. case EVT_CLASS_EXTERNAL_CMD:
  1273. switch( Evt->m_eType ){
  1274. case EVT_FH_JAMER_ON:{
  1275. uint32_t TS_A, TS_B;
  1276. TS_A = Evt->m_UMSTS / 1000;
  1277. TS_B = Evt->m_UMSTS % 1000;
  1278. #ifdef SIMULATE_KERNEL_TARGETS
  1279. m_bJamingTO = SIMULATE_JAMER_TO;
  1280. #endif
  1281. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1282. if( tData ) {
  1283. if( m_pDBC->addEvent(
  1284. 0,
  1285. Evt->m_UUID,
  1286. TS_A,
  1287. TS_B,
  1288. EVT_FH_JAMER_ON,
  1289. 0,
  1290. 0,
  1291. 0,
  1292. 0,
  1293. 0,
  1294. 0,
  1295. 0,
  1296. 0,
  1297. 0,
  1298. 0,
  1299. tData->evtUUID
  1300. ) ){
  1301. //Simulate FH Module Event;
  1302. EVENT_CONTAINER *ttt;
  1303. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1304. if( nnn ){
  1305. nnn->location.lat = 0;
  1306. nnn->location.lon = 0;
  1307. nnn->location.alt = 0;
  1308. sprintf( nnn->missionID, tData->evtUUID );
  1309. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  1310. if( ttt ) {
  1311. m_pEventHeap->broadcast( ttt );
  1312. } else {
  1313. free( nnn );
  1314. };
  1315. };
  1316. };
  1317. free(tData);
  1318. };
  1319. break;
  1320. };
  1321. case EVT_FH_MISSION_END:{
  1322. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1323. if( tData ) {
  1324. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1325. if( 1 / *tEVT* / ) {
  1326. uint32_t TS_A, TS_B;
  1327. TS_A = Evt->m_UMSTS / 1000;
  1328. TS_B = Evt->m_UMSTS % 1000;
  1329. if( m_pDBC->addEvent(
  1330. 0,
  1331. Evt->m_UUID,
  1332. TS_A,
  1333. TS_B,
  1334. EVT_FH_LANDED ) ){
  1335. //Simulate FH Module Event;
  1336. EVENT_CONTAINER *ttt;
  1337. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1338. if( nnn ){
  1339. nnn->location.lat = 0;
  1340. nnn->location.lon = 0;
  1341. nnn->location.alt = 0;
  1342. sprintf( nnn->missionID, tData->evtUUID );
  1343. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  1344. if( ttt ) {
  1345. m_pEventHeap->broadcast( ttt );
  1346. } else {
  1347. free( nnn );
  1348. };
  1349. };
  1350. };
  1351. //free( tEVT );
  1352. } else {
  1353. LOG_ERROR("Linked Event Was Not Found!");
  1354. };
  1355. };
  1356. break;
  1357. }
  1358. case EVT_FH_MISSION_BEGIN:{
  1359. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1360. #ifdef SIMULATE_KERNEL_TARGETS
  1361. m_bLandingTO = SIMULATE_LANDING_TO;
  1362. #endif
  1363. if( tData ) {
  1364. #ifdef SIMULATE_KERNEL_TARGETS
  1365. sprintf( m_cCurrentMission, tData->evtUUID );
  1366. #endif
  1367. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1368. if( tEVT ) {
  1369. uint32_t TS_A, TS_B;
  1370. TS_A = Evt->m_UMSTS / 1000;
  1371. TS_B = Evt->m_UMSTS % 1000;
  1372. if( Evt->m_db_id = m_pDBC->addEvent(
  1373. 0,
  1374. Evt->m_UUID,
  1375. TS_A,
  1376. TS_B,
  1377. EVT_FH_FLY_TO,
  1378. 0,
  1379. 0,
  1380. 0,
  1381. 0,
  1382. 0,
  1383. 0,
  1384. tEVT->lat,
  1385. tEVT->lon,
  1386. tEVT->alt,
  1387. tEVT->id,
  1388. tEVT->uuid
  1389. ) ){
  1390. //Simulate FH Module Event;
  1391. EVENT_CONTAINER * ttt;
  1392. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1393. if( nnn ){
  1394. nnn->location.lat = tEVT->lat;
  1395. nnn->location.lon = tEVT->lon;
  1396. nnn->location.alt = tEVT->alt;
  1397. sprintf( nnn->missionID, tData->evtUUID );
  1398. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  1399. if( ttt ) {
  1400. m_pEventHeap->broadcast( ttt );
  1401. } else {
  1402. free( nnn );
  1403. };
  1404. };
  1405. };
  1406. free( tEVT );
  1407. } else {
  1408. LOG_ERROR("Linked Event Was Not Found!");
  1409. };
  1410. };
  1411. break;
  1412. };
  1413. };
  1414. break;
  1415. */
  1416. case EVT_CLASS_VIDEO:
  1417. {
  1418. switch (Evt->m_eType)
  1419. {
  1420. case EVT_CAPTURE_DONE:
  1421. case EVT_CAPTURE_END:
  1422. {
  1423. if (Evt->m_pEvtData)
  1424. {
  1425. handleVideo((VIDEO_SEGMENT_INFO *) Evt->m_pEvtData);
  1426. // Dont free Data - it points to static struct of Encoder!!
  1427. memset(Evt->m_pEvtData, 0, sizeof(VIDEO_SEGMENT_INFO));
  1428. Evt->m_pEvtData = NULL;
  1429. #ifdef DEBUG_EXTRA
  1430. LOG("VIDEO_SEGMENT_INFO Cleared!");
  1431. #endif
  1432. };
  1433. break;
  1434. };
  1435. case EVT_VIDEO_FPS_DEGRADED:
  1436. case EVT_VIDEO_FPS_RAISED:
  1437. {
  1438. if (! m_bDebugMode)
  1439. {
  1440. break;
  1441. }
  1442. DBC_EVT_RECORD tRecord;
  1443. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1444. uint32_t TS_A, TS_B;
  1445. TS_A = Evt->m_UMSTS / 1000;
  1446. TS_B = Evt->m_UMSTS % 1000;
  1447. tRecord.node_id = Evt->m_nSourceNodeID;
  1448. tRecord.type = Evt->m_eType;
  1449. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1450. tRecord.ts_s = TS_A;
  1451. tRecord.ts_ms = TS_B;
  1452. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1453. break;
  1454. };
  1455. default: break;
  1456. };
  1457. break;
  1458. };
  1459. case EVT_CLASS_SENSOR_CLD:
  1460. case EVT_CLASS_SENSOR_LGS:
  1461. case EVT_CLASS_SENSOR_LFS:
  1462. case EVT_CLASS_SENSOR_MND:
  1463. case EVT_CLASS_SENSOR_PLS_210:
  1464. case EVT_CLASS_SENSOR_PLS_360:
  1465. case EVT_CLASS_SENSOR_ALS:
  1466. case EVT_CLASS_SENSOR_3D:
  1467. case EVT_CLASS_SENSOR_ACC:
  1468. #ifdef DEBUG_EXTRA
  1469. char tB[128];
  1470. sprintf(tB, "Evt Class: %d Evt Type: %d ", Evt->m_eClass, Evt->m_eType);
  1471. LOG(tB);
  1472. #endif
  1473. switch (Evt->m_eType)
  1474. {
  1475. case EVT_TARGET_1D:
  1476. {
  1477. // startSession();
  1478. preserveSession();
  1479. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1480. if (tDATA != NULL)
  1481. {
  1482. DBC_EVT_RECORD tRecord;
  1483. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1484. uint32_t TS_A, TS_B;
  1485. TS_A = Evt->m_UMSTS / 1000;
  1486. TS_B = Evt->m_UMSTS % 1000;
  1487. tRecord.node_id = Evt->m_nSourceNodeID;
  1488. tRecord.type = Evt->m_eType;
  1489. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1490. tRecord.ts_s = TS_A;
  1491. tRecord.ts_ms = TS_B;
  1492. tRecord.range_digital = tDATA->digital;
  1493. tRecord.ra = tDATA->local_target.r;
  1494. tRecord.area = tDATA->area;
  1495. tRecord.lat = tDATA->global_target.lat;
  1496. tRecord.lon = tDATA->global_target.lon;
  1497. tRecord.alt = tDATA->global_target.alt;
  1498. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1499. };
  1500. break;
  1501. };
  1502. // While Camera Always Records Video on each Absolute Move
  1503. // an Event Must be generated to see video in report
  1504. case EVT_ROI_ACTIVE: // if( !m_bDebugMode ) break;
  1505. case EVT_TARGET_2D_BLACK:
  1506. case EVT_TARGET_2D_WHITE:
  1507. {
  1508. // startSession();
  1509. preserveSession();
  1510. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1511. if (tDATA != NULL)
  1512. {
  1513. DBC_EVT_RECORD tRecord;
  1514. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1515. uint32_t TS_A, TS_B;
  1516. TS_A = Evt->m_UMSTS / 1000;
  1517. TS_B = Evt->m_UMSTS % 1000;
  1518. tRecord.node_id = Evt->m_nSourceNodeID;
  1519. tRecord.type = Evt->m_eType;
  1520. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1521. tRecord.ts_s = TS_A;
  1522. tRecord.ts_ms = TS_B;
  1523. tRecord.lrf_id = tDATA->lrf_id;
  1524. tRecord.range_digital = tDATA->digital;
  1525. tRecord.az = tDATA->local_target.fi;
  1526. tRecord.ra = tDATA->local_target.r;
  1527. tRecord.area = tDATA->area;
  1528. tRecord.lat = tDATA->global_target.lat;
  1529. tRecord.lon = tDATA->global_target.lon;
  1530. tRecord.alt = tDATA->global_target.alt;
  1531. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1532. };
  1533. break;
  1534. };
  1535. case EVT_TARGET_3D_BLACK:
  1536. case EVT_TARGET_3D_WHITE:
  1537. {
  1538. // startSession();
  1539. preserveSession();
  1540. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1541. if (tDATA != NULL)
  1542. {
  1543. DBC_EVT_RECORD tRecord;
  1544. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1545. uint32_t TS_A, TS_B;
  1546. TS_A = Evt->m_UMSTS / 1000;
  1547. TS_B = Evt->m_UMSTS % 1000;
  1548. tRecord.node_id = Evt->m_nSourceNodeID;
  1549. tRecord.type = Evt->m_eType;
  1550. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1551. tRecord.ts_s = TS_A;
  1552. tRecord.ts_ms = TS_B;
  1553. tRecord.lrf_id = tDATA->lrf_id;
  1554. tRecord.range_digital = tDATA->digital;
  1555. tRecord.az = tDATA->local_target.fi;
  1556. tRecord.el = tDATA->local_target.tetta;
  1557. tRecord.ra = tDATA->local_target.r;
  1558. tRecord.area = tDATA->area;
  1559. tRecord.lat = tDATA->global_target.lat;
  1560. tRecord.lon = tDATA->global_target.lon;
  1561. tRecord.alt = tDATA->global_target.alt;
  1562. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1563. };
  1564. break;
  1565. };
  1566. case EVT_ERR_LRF_NO_IRQ:
  1567. case EVT_ERR_LRF_NO_ZERO:
  1568. case EVT_ERR_LRF_ANGLE:
  1569. case EVT_ERR_LRF_VOLTAGE:
  1570. case EVT_ERR_LRF_BROKEN:
  1571. case EVT_LRF_VIS_CHANGED:
  1572. case EVT_ERR_OVERFLOW:
  1573. case EVT_SST_OVERFLOW:
  1574. case EVT_COMMUNICATION_JAMED:
  1575. if (! m_bDebugMode)
  1576. {
  1577. break;
  1578. }
  1579. /* fall through */
  1580. case EVT_OP_ACK_MASK:
  1581. case EVT_OP_NACK_MASK:
  1582. case EVT_OP_ON_MASK:
  1583. case EVT_OP_OFF_MASK:
  1584. case EVT_CONNECTED:
  1585. case EVT_DISCONNECTED:
  1586. {
  1587. DBC_EVT_RECORD tRecord;
  1588. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1589. uint32_t TS_A, TS_B;
  1590. TS_A = Evt->m_UMSTS / 1000;
  1591. TS_B = Evt->m_UMSTS % 1000;
  1592. tRecord.node_id = Evt->m_nSourceNodeID;
  1593. tRecord.type = Evt->m_eType;
  1594. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1595. tRecord.ts_s = TS_A;
  1596. tRecord.ts_ms = TS_B;
  1597. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1598. break;
  1599. };
  1600. case EVT_FLIP:
  1601. {
  1602. if (! m_bDebugMode)
  1603. {
  1604. break;
  1605. }
  1606. DBC_EVT_RECORD tRecord;
  1607. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1608. uint32_t TS_A, TS_B;
  1609. TS_A = Evt->m_UMSTS / 1000;
  1610. TS_B = Evt->m_UMSTS % 1000;
  1611. tRecord.node_id = Evt->m_nSourceNodeID;
  1612. tRecord.type = Evt->m_eType;
  1613. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1614. tRecord.ts_s = TS_A;
  1615. tRecord.ts_ms = TS_B;
  1616. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1617. break;
  1618. };
  1619. default: break;
  1620. };
  1621. break;
  1622. case EVT_CLASS_KRNL_FILTER_LATF:
  1623. {
  1624. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1625. if ((Evt->m_eType > EVT_FLT_LATF_DIFF_DET) || (tDATA == NULL))
  1626. {
  1627. // AUX messages (no additional target data)
  1628. DBC_EVT_RECORD tRecord;
  1629. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1630. uint32_t TS_A, TS_B;
  1631. TS_A = Evt->m_UMSTS / 1000;
  1632. TS_B = Evt->m_UMSTS % 1000;
  1633. tRecord.node_id = m_nServerNodeID;
  1634. tRecord.type = Evt->m_eType;
  1635. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1636. tRecord.ts_s = TS_A;
  1637. tRecord.ts_ms = TS_B;
  1638. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1639. }
  1640. else
  1641. {
  1642. // If First Suspiciouse -> Start session to collect Fliter Messages
  1643. // if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1644. // Regular LATF with target Info
  1645. DBC_EVT_RECORD tRecord;
  1646. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1647. uint32_t TS_A, TS_B;
  1648. TS_A = Evt->m_UMSTS / 1000;
  1649. TS_B = Evt->m_UMSTS % 1000;
  1650. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1651. tRecord.type = Evt->m_eType;
  1652. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1653. tRecord.ts_s = TS_A;
  1654. tRecord.ts_ms = TS_B;
  1655. tRecord.lrf_id = tDATA->lrf_id;
  1656. tRecord.range_digital = tDATA->digital;
  1657. tRecord.az = tDATA->local_target.fi;
  1658. tRecord.el = tDATA->local_target.tetta;
  1659. tRecord.ra = tDATA->local_target.r;
  1660. tRecord.area = tDATA->area;
  1661. tRecord.lat = 0;
  1662. tRecord.lon = 0;
  1663. tRecord.alt = 0;
  1664. #ifdef DEBUG_EXTRA
  1665. char tB[243];
  1666. sprintf(tB, "Az: %0.3f El: %0.3f Ra: %0.3f Dig: %d Width: %0.3f", tRecord.az, tRecord.el, tRecord.ra, tRecord.range_digital, tRecord.area);
  1667. LOG(tB);
  1668. #endif
  1669. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1670. };
  1671. break;
  1672. };
  1673. case EVT_CLASS_KRNL_FILTER_BUBBLE:
  1674. {
  1675. if (Evt->m_eType == EVT_FLT_BUBBLE_POPED || Evt->m_eType == EVT_FLT_BUBBLE_CREATED || Evt->m_eType == EVT_FLT_BUBBLE_BLOCK_T || Evt->m_eType == EVT_FLT_BUBBLE_BLOCK_P)
  1676. {
  1677. preserveSession();
  1678. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1679. DBC_EVT_RECORD tRecord;
  1680. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1681. uint32_t TS_A, TS_B;
  1682. TS_A = Evt->m_UMSTS / 1000;
  1683. TS_B = Evt->m_UMSTS % 1000;
  1684. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1685. tRecord.type = Evt->m_eType;
  1686. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1687. tRecord.ts_s = TS_A;
  1688. tRecord.ts_ms = TS_B;
  1689. tRecord.lrf_id = tDATA->lrf_id;
  1690. tRecord.range_digital = tDATA->digital;
  1691. tRecord.az = tDATA->local_target.fi;
  1692. tRecord.el = tDATA->local_target.tetta;
  1693. tRecord.ra = tDATA->local_target.r;
  1694. tRecord.area = tDATA->area;
  1695. tRecord.lat = 0;
  1696. tRecord.lon = 0;
  1697. tRecord.alt = 0;
  1698. #ifdef DEBUG_EXTRA
  1699. char tB[243];
  1700. sprintf(tB, "Az: %0.3f El: %0.3f Ra: %0.3f Dig: %d Width: %0.3f", tRecord.az, tRecord.el, tRecord.ra, tRecord.range_digital, tRecord.area);
  1701. LOG(tB);
  1702. #endif
  1703. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1704. };
  1705. break;
  1706. };
  1707. case EVT_CLASS_REMOTE_SERVER_CMD:
  1708. {
  1709. switch (Evt->m_eType)
  1710. {
  1711. case EVT_RPC_FRIZE_ACTIVE_B:
  1712. {
  1713. // getFilterId
  1714. break;
  1715. };
  1716. default: break;
  1717. };
  1718. break;
  1719. };
  1720. default: break;
  1721. };
  1722. return true;
  1723. }
  1724. /*
  1725. typedef struct S_VIDEO_SEGMENT_INFO{
  1726. uint32_t stream_id;
  1727. uint32_t streamTSS;
  1728. uint32_t streamTMS;
  1729. DBC_INT_LIST eventIDS;
  1730. DBC_INT_LIST eventFrames;
  1731. DBC_INT_LIST eventTSS;
  1732. DBC_INT_LIST eventTMS;
  1733. char recordMD5[33];
  1734. char recordEXT[6];
  1735. uint8_t streamFPS;
  1736. char scratchPath[PATH_MAX];
  1737. uint32_t dataSize;
  1738. uint32_t db_id;
  1739. }VIDEO_SEGMENT_INFO;
  1740. */
  1741. void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr)
  1742. {
  1743. if (fileExist(ptr->scratchPath))
  1744. {
  1745. bool bKeepVideoFile = false;
  1746. if (ptr->recordMD5[0])
  1747. {
  1748. if (m_bKeepLocalReport)
  1749. {
  1750. // if wm is debug -> add all to database, else send to server
  1751. ptr->db_id = m_pDBC->addVideoRecord(ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS);
  1752. if (ptr->db_id)
  1753. {
  1754. #ifdef DEBUG_EXTRA
  1755. LOG("Video File Registered");
  1756. #endif
  1757. for (size_t idx = 0; idx < ptr->eventIDS.size(); idx++)
  1758. {
  1759. #ifdef DEBUG_EXTRA
  1760. LOG("Binding Event to a Video File!");
  1761. #endif
  1762. if (m_pDBC->addVideoEvent(ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx]))
  1763. {
  1764. #ifdef DEBUG_EXTRA
  1765. LOG("OK");
  1766. #endif
  1767. };
  1768. };
  1769. CSTR tP = m_pDBC->getCurrentReportDir();
  1770. if (tP)
  1771. {
  1772. char tB[MAX_PATH];
  1773. char dB[MAX_PATH];
  1774. // sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1775. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT);
  1776. fileCopy(ptr->scratchPath, tB);
  1777. ch_chown(tB, "nobody", "nogroup");
  1778. chmod(tB, 0664);
  1779. #ifdef DEBUG_EXTRA
  1780. LOG("Video Saved");
  1781. #endif
  1782. free(tP);
  1783. }
  1784. else
  1785. {
  1786. LOG_ERROR("Cannot Get Report Path from DB!");
  1787. };
  1788. }
  1789. else
  1790. {
  1791. LOG_ERROR("Failure in registration video!");
  1792. };
  1793. };
  1794. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1795. {
  1796. bKeepVideoFile = m_pRPC->addVideo(ptr);
  1797. }
  1798. };
  1799. if (! bKeepVideoFile)
  1800. {
  1801. unlink(ptr->scratchPath);
  1802. #ifdef DEBUG_EXTRA
  1803. LOG("Video Scratch Deleted!");
  1804. #endif
  1805. };
  1806. } else {
  1807. LOG_ERROR("Video File is Missing!");
  1808. };
  1809. //In case end of the day Force Session Close after Video File is saved
  1810. if ( m_bForceCloseSessionOnEndOfTheDay ) {
  1811. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  1812. m_bForceCloseSessionOnEndOfTheDay = false;
  1813. };
  1814. }
  1815. /*
  1816. * Local "report period" = day-of-month * 100 + hour: changes every local hour (and at midnight).
  1817. */
  1818. static int getLocalReportPeriod(void){
  1819. time_t t = time(NULL);
  1820. tm lt;
  1821. if ( localtime_r( &t, &lt ) == NULL ) return -1;
  1822. return lt.tm_mday * 100 + lt.tm_hour;
  1823. }
  1824. /*
  1825. * Called every second from the kernel thread.
  1826. * Every local hour the current report (data/reports/YYYY/MM/DD/report_HH.db) is closed and
  1827. * the next hour's report is opened - at midnight in the next day's directory.
  1828. *
  1829. * Order of operations:
  1830. * 1. an open session that started in the previous hour is closed (END event goes into the old
  1831. * report) - if a video segment with events is still recording, wait for it at most
  1832. * REPORT_SWITCH_MAX_VIDEO_WAIT seconds so the video is stored next to its events;
  1833. * 2. once the END event is written (event thread), the report is switched to the new hour.
  1834. * The next event opens a new session in the new report.
  1835. * A session started after the hour change already switched the report itself (SESSION_START).
  1836. */
  1837. void mojoKernel::handleReportPeriodChange(void){
  1838. int cp = getLocalReportPeriod();
  1839. if ( cp < 0 ) return;
  1840. time_t now = time(NULL);
  1841. if ( !m_bReportSwitchPending ) {
  1842. if ( m_nReportPeriod == cp ) return;
  1843. m_bReportSwitchPending = true;
  1844. m_tReportSwitchDetected = now;
  1845. LOG("Local hour changed - closing hour report");
  1846. };
  1847. bool bTimeOut = ( now - m_tReportSwitchDetected ) >= REPORT_SWITCH_MAX_VIDEO_WAIT;
  1848. if ( m_bSessionStarted && ( m_nSessionPeriod != cp ) ) {
  1849. // Session belongs to the previous hour - close it in the previous hour's report
  1850. m_bForceCloseSessionOnEndOfTheDay = true;
  1851. #ifndef LRF_PROXY_ONLY
  1852. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1853. if ( tMO && tMO->isProcessingVideoStream() ) {
  1854. if ( !bTimeOut ) return; // let the running recording finish into the old report
  1855. LOG_ERROR("Hour change: video still recording - forcing session close");
  1856. };
  1857. #endif
  1858. stopSession( true );
  1859. return; // switch on a next call, after the END event is written by the event thread
  1860. };
  1861. // Wait until the END of the previous session is stored in the old report
  1862. if ( !m_bSessionStarted && m_pDBC->getEventSessionStatus() && !bTimeOut ) return;
  1863. if ( m_pDBC->switchReportToCurrentHour() ) {
  1864. m_nReportPeriod = cp;
  1865. m_bReportSwitchPending = false;
  1866. m_bForceCloseSessionOnEndOfTheDay = false;
  1867. LOG("Report switched to the new hour");
  1868. } else {
  1869. LOG_ERROR("Hour report switch failed - will retry");
  1870. };
  1871. }
  1872. bool mojoKernel::validateReports(void){
  1873. /// @todo Stephane needs to follow this function...
  1874. if ( m_bKeepLocalReport ){
  1875. // Every local hour: close the hour report and open the next one (next day's folder at midnight)
  1876. handleReportPeriodChange();
  1877. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  1878. if (tSiteData){
  1879. // Check if settings has been changed in DB
  1880. if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty){
  1881. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  1882. };
  1883. free(tSiteData);
  1884. } else {
  1885. LOG_ERROR("Error Loading Site DATA!");
  1886. return false;
  1887. };
  1888. if (m_sSiteData.report_cleanup) {
  1889. cleanUpReportDir(m_sSiteData.report_cleanup);
  1890. };
  1891. if (m_sSiteData.preserve_empty) {
  1892. float tT = 100.0f * (1.0f - (1.0f / ((float) m_sSiteData.preserve_empty)));
  1893. #ifdef DEBUG_EXTRA
  1894. char tB[128];
  1895. sprintf(tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty);
  1896. LOG(tB);
  1897. #endif
  1898. if (m_sSysInfo.disk > tT) {
  1899. #ifdef DEBUG_EXTRA
  1900. LOG("Delete Some Reports!");
  1901. #endif
  1902. // delete some oldest report date
  1903. m_pDBC->deleteReportDate(m_pDBC->getFirstEventTS());
  1904. };
  1905. };
  1906. };
  1907. return true;
  1908. }
  1909. void mojoKernel::backupReports(uint32_t days)
  1910. {
  1911. if (! m_bKeepLocalReport)
  1912. {
  1913. return;
  1914. }
  1915. REPORT_DIRINFO tRDI;// = {0};
  1916. //memset( &tRDI, 0, sizeof(REPORT_DIRINFO)); will be initialized by fillRDF
  1917. uint32_t cTS = (uint32_t) time(NULL);
  1918. cTS -= 86400 * days;
  1919. fillRDF(cTS, tRDI);
  1920. DBC_INT_LIST * TL = m_pDBC->listOlderReportStat(tRDI.YEAR, tRDI.MONTH, tRDI.DAY);
  1921. if (TL)
  1922. {
  1923. for (DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++)
  1924. {
  1925. REPORT_DIRINFO * tRDI = m_pDBC->getReportDirInfo(*idx);
  1926. if (tRDI)
  1927. {
  1928. char tB[MAX_PATH];
  1929. char dB[MAX_PATH];
  1930. sprintf(tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1931. char tPATH[MAX_PATH];
  1932. sprintf(tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  1933. if (fileExist(tB))
  1934. {
  1935. if (! fileExist(tPATH))
  1936. {
  1937. // Try Create Job if sucess then create dir to skip next validation
  1938. if (createCronJob(0, tPATH, cTS))
  1939. {
  1940. // Create Backup Dir
  1941. uid_t uid = getUserUid(m_pDBC->getDataParam(1));
  1942. gid_t gid = getUserGid(m_pDBC->getDataParam(2));
  1943. if (createAccessibleDir(tPATH, STORAGE_DIR_MODE, uid, gid))
  1944. {
  1945. // Create Just One Backup Per Time!
  1946. free(tRDI);
  1947. break;
  1948. };
  1949. };
  1950. }; // else proceed next
  1951. };
  1952. free(tRDI);
  1953. };
  1954. };
  1955. delete(TL);
  1956. };
  1957. }
  1958. void mojoKernel::cleanUpReportDir(uint32_t days)
  1959. {
  1960. // Cleanup Current reports no Backup
  1961. uint32_t oTS = m_pDBC->getFirstEventTS();
  1962. uint32_t cTS = (uint32_t) time(NULL);
  1963. cTS -= 86400 * days;
  1964. #ifdef DEBUG_EXTRA
  1965. char tB[128];
  1966. sprintf(tB, "Oldest %d border %d", oTS, cTS);
  1967. LOG(tB);
  1968. #endif
  1969. if (oTS <= cTS)
  1970. {
  1971. #ifdef DEBUG_EXTRA
  1972. LOG("Deleting Old Reports");
  1973. #endif
  1974. /// @todo Stephane needs to see when this gets called...
  1975. m_pDBC->deleteReportDate(oTS);
  1976. };
  1977. }
  1978. bool mojoKernel::createCronJob(uint8_t job, CSTR param_str, int param_int)
  1979. {
  1980. if (! fileExist("/srv/etc/cron.lock"))
  1981. {
  1982. Json::Value root;
  1983. // Json::Value data;
  1984. switch (job)
  1985. {
  1986. case CRON_JOB_CREATE_BACKUP:
  1987. {
  1988. root["operation"] = CRON_JOB_CREATE_BACKUP;
  1989. // data["number"] = 1;
  1990. root["ts"] = param_int;
  1991. root["backup_dst"] = std::string(param_str);
  1992. break;
  1993. };
  1994. // No default JOB!
  1995. default: return false;
  1996. };
  1997. FILE * fd = fopen("/srv/etc/cron_.json", "w+");
  1998. if (fd)
  1999. {
  2000. if (HUMAN_FRIENDLY_JSON)
  2001. {
  2002. //Json::FastWriter writer;
  2003. //const std::string json_file = writer.write(root);
  2004. //fprintf(fd, json_file.c_str());
  2005. Json::StreamWriterBuilder builder;
  2006. const std::string json_file = Json::writeString(builder, root);
  2007. fprintf(fd, json_file.c_str());
  2008. }
  2009. else
  2010. {
  2011. Json::StreamWriterBuilder builder;
  2012. const std::string json_file = Json::writeString(builder, root);
  2013. fprintf(fd, json_file.c_str());
  2014. };
  2015. fclose(fd);
  2016. return true;
  2017. };
  2018. };
  2019. return false;
  2020. }
  2021. #undef LOG
  2022. #undef LOG_ERROR