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.

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