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.

2222 lines
62 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
5 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days 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
5 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
4 years ago
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days 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
5 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
Kernel watchdog: feed from its own thread while the kernel loop makes progress watchDogDaemon kills a client that misses its deadline (signal 9). The mojo kernel loop sent "4000" (next update within 4 s) at the start of each pass and then ran all validations in the same thread. A pass of about 3 s killed la_mojo, for example a gateway connect (5 s), rm -rf of a report day, or a synchronous systemctl restart of MediaMTX. kernelPrototype - A feeder thread (wdStart) sends KERNEL_WD_FEED_MS (4 s) every second while the kernel loop reports progress (wdStep). A loop without progress for KERNEL_WD_STALL_MS (60 s) is a hang: the feeding stops, logged as "Kernel loop stalled in '<step>' for N s - watchdog no longer fed", and the daemon kills the process as before. - wdStep() logs steps slower than 1 s ("Kernel step '<step>' took N ms"). The feeder logs a step that runs longer than 5 s every 5 s ("Kernel loop busy in '<step>' for N s"), so the step is named before any kill. - wdExtend(): stops feeding and grants a one-time deadline. - send(MSG_NOSIGNAL) instead of write(): a watchDogDaemon that restarted closed the socket, and the next write() killed la_mojo with SIGPIPE. A lost connection is logged and closed. - The socket is closed when connect() fails (it leaked). - The feeder is opt-in: servaKernel is unchanged. mojoKernel - The loop calls wdStart() and names each step: debug mode, filters, session, reports, hardwareManager::validate, updateSysInfo, media helper, remote server, kernel commands, cleanNodePushedCMD. - ~mojoKernel grants KERNEL_WD_SHUTDOWN_MS (120 s) before deleting the factories / nodes / threads. Before, the 4 s of the last feed ran out and the daemon killed la_mojo in the middle of a reload / restart / exit. "-1" at the end goes through wdSend(). Tested with the real watchDogDaemon and a test kernel built on kernelPrototype (stall limit 8 s for the test): - old behaviour, one 6 s step: killed (137); - new, normal passes: runs to the end; - new, one 6 s step: not killed; logs "busy ... 5 s" and "took 6000 ms"; - new, a step that hangs: logs "stalled in '<step>'", then killed (137). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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
1 week 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 <cmath>
  16. #include "mojoKernel.h"
  17. static int getLocalReportPeriod(void);
  18. #include "../common/kernelEventFilterAPF.h"
  19. #include "../common/kernelEventFilterBubble.h"
  20. #include "../common/kernelEventFilterTrain.h"
  21. #include "../common/kernelModuleEmbedVideo.h"
  22. #include "../data/json/json_tool.h"
  23. #include <json/reader.h>
  24. #include <json/value.h>
  25. #include <json/writer.h>
  26. #include <strings.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #ifdef USE_MYSQL
  30. #include "../common/MySQL_dataBinder.h"
  31. #else
  32. #include "../common/SQLite_dataBinder.h"
  33. #endif
  34. #define LOG(X) kernelPrototype::m_pLog->Log("KRNL", this, X)
  35. #ifdef DEBUG
  36. #define LOG_ERROR(X) \
  37. { \
  38. char _tBfrErrMSG234[256]; \
  39. sprintf(_tBfrErrMSG234, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  40. kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, _tBfrErrMSG234); \
  41. }
  42. #else
  43. #define LOG_ERROR(X) kernelPrototype::m_pLog->Log("KRNL", this, _LOG_ERROR, X);
  44. #endif
  45. mojoKernel * mojoKernel::_instance = NULL;
  46. mojoKernel * mojoKernel::getInstance(config_setting_t * ptr)
  47. {
  48. if (_instance == NULL)
  49. {
  50. _instance = new mojoKernel(ptr);
  51. };
  52. return _instance;
  53. }
  54. mojoKernel::mojoKernel(config_setting_t * ptr) :
  55. kernelPrototype(ptr)
  56. {
  57. m_pEventHeap = basicEventHeap::getInstance();
  58. m_pEventHeap->resume(); // paused by the previous kernel's destructor (restart)
  59. memset(&m_sSysInfo, 0, sizeof(DBC_SYS_LOAD_INFO));
  60. m_pShadow = NULL;
  61. m_pSystemAnchor = NULL;
  62. m_bSessionStarted = false;
  63. m_bSessionMustPreserve = false;
  64. m_bFirstSession = true;
  65. m_bWorkWithRemoteServer = false;
  66. m_bKeepLocalReport = true;
  67. m_bMediaMode = 0;
  68. m_bApplicationFirstRun = false;
  69. m_bForceCloseSessionOnEndOfTheDay = false;
  70. m_mDateOfToday = getLocalDay();
  71. m_nReportPeriod = getLocalReportPeriod();
  72. m_bReportSwitchPending = false;
  73. m_tReportSwitchDetected = 0;
  74. m_nSessionPeriod = -1;
  75. m_pRPC = NULL;
  76. #ifndef LRF_PROXY_ONLY
  77. m_pMediaHelper = NULL;
  78. #endif
  79. #ifdef EMBED_AUTORESET
  80. m_bEnableAutoReset = false;
  81. m_nSessionCounter = 0;
  82. m_nAutoResetTime = 0;
  83. #endif
  84. #ifdef PEREODIC_SENSORS_RESET
  85. m_bEnablePereodicReset = false;
  86. m_nPereodicResetTime = 0;
  87. m_nPereodicResetCounter = 0;
  88. #endif
  89. // setWM( WM_NONE );
  90. }
  91. mojoKernel::~mojoKernel()
  92. {
  93. #ifdef LINUX_KERNEL_WATCHDOG
  94. // Deleting the factories / nodes / threads takes longer than one feed period: grant the shutdown its
  95. // own deadline instead of the last 4 s (the daemon killed la_mojo in the middle of a reload / exit)
  96. wdExtend(KERNEL_WD_SHUTDOWN_MS);
  97. #endif
  98. WaitFinish(true);
  99. stopSession(false);
  100. // Deliver what is queued (session end, ...), then stop dispatching BEFORE the components are
  101. // deleted: every node / module / filter is an event handler, and the heap kept calling
  102. // dispatchEvent() on objects being destroyed - the shutdown could hang (signal, web exit)
  103. m_pEventHeap->flush(2000);
  104. if (! m_pEventHeap->pause())
  105. {
  106. LOG_ERROR("Event heap did not pause - a handler is stuck in dispatchEvent()");
  107. };
  108. #ifndef LRF_PROXY_ONLY
  109. if ( m_pMediaHelper ) {
  110. delete( m_pMediaHelper );
  111. }
  112. #endif
  113. #ifdef DEBUG
  114. LOG(" Deleting Node Factory...");
  115. #endif
  116. if (m_pNodeFactory != NULL)
  117. {
  118. delete(m_pNodeFactory);
  119. }
  120. #ifdef DEBUG
  121. LOG(" Deleting Module Factory...");
  122. #endif
  123. if (m_pModuleFactory != NULL)
  124. {
  125. delete(m_pModuleFactory);
  126. }
  127. #ifdef DEBUG
  128. LOG(" Deleting Target Filters...");
  129. #endif
  130. unloadFilters();
  131. if (EXIT_CODE_KERNEL_CLEAR == getExitCause())
  132. {
  133. #ifdef DEBUG
  134. LOG("Unregister Server");
  135. #endif
  136. m_pDBC->unregisterServer();
  137. #ifdef DEBUG
  138. // LOG("Close DataBase Connection...");
  139. #endif
  140. // if( m_pDBC != NULL ) delete( m_pDBC );//Deleting in Backend
  141. // m_pDBC = NULL;
  142. }
  143. else
  144. {
  145. if (EXIT_CODE_DETECT_HARDWARE == getExitCause())
  146. {
  147. #ifdef DEBUG
  148. LOG("HW PNP Detection Requested...");
  149. #endif
  150. // m_pDBC->updateNodePushedCMD( tCMD->id, 30 );
  151. m_pHardwareManager->enumerateHardware();
  152. // m_pDBC->updateNodePushedCMD( tCMD->id, 60 );
  153. };
  154. #ifdef DEBUG
  155. // LOG("Close DataBase Connection...");
  156. #endif
  157. // if( m_pDBC != NULL ) delete( m_pDBC );
  158. // m_pDBC = NULL;
  159. };
  160. delete(m_pHardwareManager);
  161. // while(1){
  162. // char tB[200];
  163. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  164. // LOG( "BopBop!" );
  165. // SLEEP(1000);
  166. // };
  167. if (m_pRPC)
  168. {
  169. #ifdef DEBUG
  170. LOG("Deleting Remote Server Connection...");
  171. #endif
  172. delete(m_pRPC);
  173. #ifdef DEBUG
  174. LOG("Remote Server Connection Destroyed!");
  175. #endif
  176. }
  177. #ifdef LINUX_KERNEL_WATCHDOG
  178. LOG("Disable WD!");
  179. wdSend(-1); // MSG_NOSIGNAL: no SIGPIPE if the daemon is gone
  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. #ifdef LINUX_KERNEL_WATCHDOG
  750. wdStart(); // the watchdog is fed by its own thread while this loop reports progress (wdStep)
  751. #define KWD_STEP(X) wdStep(X)
  752. #else
  753. #define KWD_STEP(X)
  754. #endif
  755. while (m_bThreadStatus < TH_ENDED){
  756. KWD_STEP("idle");
  757. m_tTimeTSeconds = time(NULL);
  758. // Each Second
  759. if (m_tTimeTSeconds != tTimeTSeconds){
  760. tTimeTSeconds = m_tTimeTSeconds;
  761. if (m_bApplicationFirstRun){
  762. if (m_pHardwareManager != NULL){
  763. LOG_ERROR("Restart and Detect HardWare!");
  764. // Must be shutdowned all service that uses hardware prior to detection!
  765. m_bThreadStatus = TH_ENDED;
  766. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  767. continue;
  768. };
  769. };
  770. KWD_STEP("debug mode");
  771. m_bDebugMode = m_pDBC->getAppDebugMode();
  772. KWD_STEP("validateFilters");
  773. validateFilters();
  774. KWD_STEP("validateSession");
  775. validateSession();
  776. KWD_STEP("validateReports");
  777. validateReports();
  778. KWD_STEP("hardwareManager::validate");
  779. if (! m_pHardwareManager->validate()){
  780. LOG("Reload Mojo Node Due to Memory Consumption!");
  781. m_bThreadStatus = TH_ENDED;
  782. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  783. continue;
  784. };
  785. KWD_STEP("updateSysInfo");
  786. m_mSysInfoProtector.lock();
  787. m_pHardwareManager->updateSysInfo(&m_sSysInfo);
  788. m_mSysInfoProtector.unlock();
  789. #ifndef LRF_PROXY_ONLY
  790. if ( m_pMediaHelper ) {
  791. KWD_STEP("mediaHelper::validateService");
  792. m_pMediaHelper->validateService();
  793. };
  794. #endif
  795. KWD_STEP("remote server");
  796. if (m_sSiteData.use_remote_server){
  797. if (m_pRPC){
  798. m_pRPC->validate();
  799. m_bWorkWithRemoteServer = (m_pRPC->getConnectionStatus() == CS_CONNECTED);
  800. } else {
  801. // Checking if Nodes are Loadded
  802. if (m_pNodeFactory->nodesAreLoaded()) {
  803. #ifdef DEBUG
  804. LOG("Initial Connect to Remote Server");
  805. #endif
  806. if ((m_pRPC = new mojoRemoteClient(this))) {
  807. if (! (m_bWorkWithRemoteServer = m_pRPC->create())) {
  808. LOG_ERROR("Cannot Connect to Remote Server!");
  809. }
  810. } else {
  811. m_bWorkWithRemoteServer = false;
  812. }
  813. };
  814. };
  815. };
  816. KWD_STEP("kernel commands");
  817. DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD(m_nServerNodeID); // Kernel
  818. if (tCMD != NULL) {
  819. m_pDBC->updateNodePushedCMD(tCMD->id, DBC_CMD_STARTED);
  820. switch (tCMD->CMD) {
  821. case CMD_KRNL_RELOAD:{
  822. LOG("Reload Mojo Node!");
  823. m_bThreadStatus = TH_ENDED;
  824. setExitCause(EXIT_CODE_RELOAD_SERVICES);
  825. break;
  826. };
  827. case CMD_KRNL_RESTART:{
  828. LOG("Restart Mojo Node!");
  829. m_bThreadStatus = TH_ENDED;
  830. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  831. break;
  832. };
  833. case CMD_KRNL_FACTORY_RESET:{
  834. LOG("Restart Mojo -> Factory Reset!");
  835. m_bThreadStatus = TH_ENDED;
  836. setExitCause(EXIT_CODE_FACTORY_RESET);
  837. break;
  838. };
  839. case CMD_NODES_RELOAD:{
  840. if (m_pNodeFactory != NULL){
  841. m_pNodeFactory->reloadNodes();
  842. };
  843. break;
  844. };
  845. case CMD_NODE_RELOAD:{
  846. uint32_t tNodeID = atoi(tCMD->PARAM);
  847. if (m_pNodeFactory != NULL){
  848. m_pNodeFactory->reloadNode(tNodeID);
  849. };
  850. break;
  851. };
  852. case CMD_KRNL_PNP:{
  853. if (m_pHardwareManager != NULL){
  854. LOG_ERROR("Restart and Detect HardWare!");
  855. // Must be shutdowned all service that uses hardware prior to detection!
  856. m_bThreadStatus = TH_ENDED;
  857. setExitCause(EXIT_CODE_DETECT_HARDWARE); // Go -> Detect Hardware in BACKEND
  858. };
  859. break;
  860. };
  861. case CMD_KRNL_UPDATE_IDENT:{
  862. LOG("Restart Mojo -> Update Ident!");
  863. sprintf(m_sIdentity, tCMD->PARAM);
  864. config_setting_remove(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY);
  865. config_setting_t * pSetting = config_setting_add(m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING);
  866. config_setting_set_string(pSetting, tCMD->PARAM);
  867. basicSettingsProvider::getInstance()->compromise();
  868. basicSettingsProvider::getInstance()->validate();
  869. m_pDBC->updateServerIdentity(m_nServerID, m_sIdentity);
  870. m_bThreadStatus = TH_ENDED;
  871. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  872. break;
  873. };
  874. case CMD_KRNL_REPLACE_DATABASE:{
  875. LOG("Restart Mojo -> Replace Database!");
  876. CSTR tNAME = m_pDBC->getDataParam(0);
  877. CSTR tUSER = m_pDBC->getDataParam(1);
  878. CSTR tGROUP = m_pDBC->getDataParam(2);
  879. if (tNAME){
  880. char tB[512];
  881. sprintf(tB, "./data/%s.db", tCMD->PARAM);
  882. if (fileExist(tB)){
  883. LOG("Restart Mojo -> Close Database!");
  884. // Closing Database
  885. delete(m_pDBC);
  886. m_pDBC = NULL;
  887. LOG("Restart Mojo -> Delete Database!");
  888. // unlink( tNAME );
  889. LOG("Restart Mojo -> Copy Database!");
  890. fileCopy(tB, tNAME);
  891. LOG("Restart Mojo -> Chown Database!");
  892. ch_chown(tNAME, tUSER, tGROUP);
  893. LOG("Restart Mojo -> Delete Old Database!");
  894. unlink(tB);
  895. m_bThreadStatus = TH_ENDED;
  896. LOG("Restart Mojo -> Exit!");
  897. setExitCause(EXIT_CODE_NO_DATABASE); // EXIT_CODE_RELOAD_SERVICES );
  898. m_bThreadStatus = TH_ENDED;
  899. } else {
  900. LOG_ERROR("New Database Does not Exists!");
  901. };
  902. // free( tNAME );
  903. // free( tUSER );
  904. // free( tGROUP );
  905. };
  906. break;
  907. };
  908. case CMD_KRNL_SYNC_TIME:{
  909. uint32_t uts = atoi(tCMD->PARAM);
  910. if (uts){
  911. #if (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 31)
  912. time_t ts = (time_t) uts;
  913. if (stime(&ts) == 0){
  914. // Restert Server;
  915. m_bThreadStatus = TH_ENDED;
  916. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  917. };
  918. #else
  919. timespec ts;
  920. ts.tv_sec = (time_t) uts;
  921. ts.tv_nsec = 0;
  922. if (clock_settime(CLOCK_REALTIME, &ts) == 0){
  923. // Restert Server;
  924. m_bThreadStatus = TH_ENDED;
  925. setExitCause(EXIT_CODE_KERNEL_CLEAR);
  926. };
  927. #endif
  928. };
  929. break;
  930. };
  931. case CMD_KRNL_SYSTEM_TASK:{
  932. break;
  933. };
  934. case CMD_KRNL_MAP_ROI:{
  935. mapROI(tCMD->PARAM);
  936. break;
  937. };
  938. case NODE_FORCE_FINISH_ALARM:{
  939. stopSession();
  940. break;
  941. };
  942. case CMD_FORCE_FREEZE_BUBBLES:{
  943. LOG("Freeze Bubbles NCQ");
  944. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_BLOCK);
  945. if (tTT){
  946. m_pEventHeap->broadcast(tTT);
  947. };
  948. break;
  949. };
  950. case CMD_FORCE_POP_BUBBLE:{
  951. LOG("POP Bubbles NCQ");
  952. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_DEL_FB);
  953. if (tTT){
  954. m_pEventHeap->broadcast(tTT);
  955. };
  956. break;
  957. };
  958. case CMD_FORCE_RELOAD_BUBBLES:{
  959. LOG("Reload Bubbles NCQ");
  960. EVENT_CONTAINER * tTT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_FLT_BUBBLES_RELOAD);
  961. if (tTT){
  962. m_pEventHeap->broadcast(tTT);
  963. };
  964. break;
  965. };
  966. case CMD_FORCE_SENORS_RESET:{
  967. LOG("Reset All Sensors NCQ");
  968. if ( m_pNodeFactory ) {
  969. deviceNode ** tA = NULL;
  970. uint32_t tCNT = m_pNodeFactory->fetchNodes(ARGUS_ALS_SENSOR, tA );
  971. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  972. if (tCNT){
  973. free(tA);
  974. tA = NULL;
  975. };
  976. tCNT = m_pNodeFactory->fetchNodes(ARGUS_LFS_SENSOR, tA );
  977. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  978. if (tCNT){
  979. free(tA);
  980. tA = NULL;
  981. };
  982. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_200_SENSOR, tA );
  983. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  984. if (tCNT){
  985. free(tA);
  986. tA = NULL;
  987. };
  988. tCNT = m_pNodeFactory->fetchNodes(ARGUS_PLS_360_SENSOR, tA );
  989. for ( uint32_t i = 0; i < tCNT; i++ ) ( (device2DSensor *)tA[i] )->reset();
  990. if (tCNT){
  991. free(tA);
  992. tA = NULL;
  993. };
  994. };
  995. stopSession();
  996. break;
  997. };
  998. };
  999. free(tCMD);
  1000. };
  1001. if ( m_pDBC ){
  1002. KWD_STEP("cleanNodePushedCMD");
  1003. m_pDBC->cleanNodePushedCMD();
  1004. }
  1005. };
  1006. SLEEP(SLEEP_KERNEL_INTERVAL);
  1007. };
  1008. KWD_STEP("exit"); // the destructor grants the shutdown its own deadline (wdExtend)
  1009. #undef KWD_STEP
  1010. }
  1011. void mojoKernel::loadFilters()
  1012. {
  1013. if (m_pDBC == NULL)
  1014. {
  1015. return;
  1016. }
  1017. #ifdef DEBUG
  1018. LOG("Loadding Filters ...");
  1019. #endif
  1020. DBC_INT_LIST * tList = m_pDBC->listFilters(m_nServerID);
  1021. if (tList != NULL)
  1022. {
  1023. if (tList->size() > 0)
  1024. {
  1025. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  1026. {
  1027. #ifdef DEBUG_EXTRA
  1028. LOG("Load Filter!");
  1029. #endif
  1030. DBC_TARGET_FLITER_DATA * tData = NULL;
  1031. tData = m_pDBC->getTargetFilterData(*idx);
  1032. if (tData != NULL)
  1033. {
  1034. #ifdef DEBUG_EXTRA
  1035. LOG("Filter Data OK!");
  1036. #endif
  1037. switch (tData->type)
  1038. {
  1039. case TF_LA_METRO_TRAIN_FILTER:
  1040. {
  1041. #ifdef DEBUG
  1042. LOG("Create Train Filter");
  1043. #endif
  1044. kernelEventFilterTrain * tFilter = new kernelEventFilterTrain(*idx);
  1045. if (tFilter != NULL)
  1046. {
  1047. if (tFilter->initFilter())
  1048. {
  1049. #ifdef DEBUG
  1050. LOG("Add Train Filter");
  1051. #endif
  1052. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1053. }
  1054. else
  1055. {
  1056. #ifdef DEBUG
  1057. LOG_ERROR("Error in Filter Init!");
  1058. #endif
  1059. delete(tFilter);
  1060. };
  1061. };
  1062. break;
  1063. };
  1064. case TF_AUTHORISED_PEOPLE_FILTER:
  1065. {
  1066. LOG("Create APF");
  1067. kernelEventFilterAPF * tFilter = new kernelEventFilterAPF(*idx);
  1068. if (tFilter != NULL)
  1069. {
  1070. if (tFilter->initFilter())
  1071. {
  1072. #ifdef DEBUG
  1073. LOG("Add APF");
  1074. #endif
  1075. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1076. }
  1077. else
  1078. {
  1079. #ifdef DEBUG
  1080. LOG_ERROR("Error in Filter Init!");
  1081. #endif
  1082. delete(tFilter);
  1083. };
  1084. };
  1085. break;
  1086. };
  1087. case TF_BUBBLE_SEGMENTATION_FILTER:
  1088. {
  1089. #ifdef DEBUG
  1090. LOG("Create Bubble Filter");
  1091. #endif
  1092. kernelEventFilterBubble * tFilter = new kernelEventFilterBubble(*idx);
  1093. if (tFilter != NULL)
  1094. {
  1095. #ifdef DEBUG_EXTRA
  1096. LOG("Init Bubble Filter");
  1097. #endif
  1098. if (tFilter->initFilter())
  1099. {
  1100. #ifdef DEBUG
  1101. LOG("Add Bubble Filter");
  1102. #endif
  1103. m_vTargetFilters.push_back((kernelEventFilter *) tFilter);
  1104. }
  1105. else
  1106. {
  1107. #ifdef DEBUG
  1108. LOG_ERROR("Error in Filter Init!");
  1109. #endif
  1110. delete(tFilter);
  1111. };
  1112. };
  1113. break;
  1114. };
  1115. case TF_YOLO_FILTER:
  1116. {
  1117. break;
  1118. };
  1119. default: LOG("Unknoun Filter"); break;
  1120. };
  1121. #ifdef DEBUG_EXTRA
  1122. LOG("Delete Filter Data");
  1123. #endif
  1124. free(tData);
  1125. };
  1126. };
  1127. #ifdef DEBUG_EXTRA
  1128. LOG("Clear Filter List");
  1129. #endif
  1130. tList->clear();
  1131. };
  1132. #ifdef DEBUG_EXTRA
  1133. LOG("Delete Filter List");
  1134. #endif
  1135. delete(tList);
  1136. #ifdef DEBUG
  1137. }
  1138. else
  1139. {
  1140. LOG("No Target Filters Assigned!");
  1141. #endif
  1142. };
  1143. #ifdef DEBUG
  1144. LOG("Done Load Filters!");
  1145. #endif
  1146. }
  1147. void mojoKernel::unloadFilters()
  1148. {
  1149. if (m_vTargetFilters.size() > 0)
  1150. {
  1151. #ifdef DEBUG
  1152. LOG("Have Filters to Unload");
  1153. #endif
  1154. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1155. {
  1156. switch ((*idx)->getType())
  1157. {
  1158. case TF_LA_METRO_TRAIN_FILTER:
  1159. {
  1160. delete((kernelEventFilterTrain *) (*idx));
  1161. break;
  1162. };
  1163. case TF_AUTHORISED_PEOPLE_FILTER:
  1164. {
  1165. delete((kernelEventFilterAPF *) (*idx));
  1166. break;
  1167. };
  1168. case TF_BUBBLE_SEGMENTATION_FILTER:
  1169. {
  1170. delete((kernelEventFilterBubble *) (*idx));
  1171. break;
  1172. };
  1173. case TF_YOLO_FILTER:
  1174. {
  1175. // ToDo: Must be changed according to Steph's code
  1176. // delete( (kernelEventFilterYOLO*)(*idx) );
  1177. break;
  1178. };
  1179. default:
  1180. {
  1181. char tBuffer[64];
  1182. sprintf(tBuffer, "Unknown Filter: %d", (*idx)->getType());
  1183. LOG(tBuffer);
  1184. };
  1185. };
  1186. };
  1187. m_vTargetFilters.clear();
  1188. LOG("Unload Filters Done!");
  1189. #ifdef DEBUG
  1190. }
  1191. else
  1192. {
  1193. LOG("No Filters to Unload!");
  1194. #endif
  1195. };
  1196. }
  1197. void mojoKernel::validateFilters()
  1198. {
  1199. if (m_vTargetFilters.size() > 0)
  1200. {
  1201. #ifdef DEBUG_EXTRA
  1202. LOG("Filters Validation");
  1203. #endif
  1204. for (vectorKernelEventFilters::iterator idx = m_vTargetFilters.begin(); idx < m_vTargetFilters.end(); idx++)
  1205. {
  1206. if (! (*idx)->validate())
  1207. {
  1208. LOG_ERROR("Filter is Invalid!");
  1209. /*switch ((*idx)->getType()) {
  1210. case TF_LA_METRO_TRAIN_FILTER:{
  1211. delete( (kernelEventFilterTrain*)(*idx) );
  1212. break;
  1213. };
  1214. case TF_AUTHORISED_PEOPLE_FILTER:{
  1215. delete( (kernelEventFilterAPF*)(*idx) );
  1216. break;
  1217. };
  1218. default:{
  1219. char tBuffer[64];
  1220. sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() );
  1221. LOG( tBuffer );
  1222. };
  1223. };
  1224. */
  1225. };
  1226. };
  1227. };
  1228. }
  1229. /*
  1230. CMD_KRNL_MAP_ROI: a click on the monitoring map (Mojo_Web doApplicationOperation 15).
  1231. PARAM "<format>;<a>;<b>":
  1232. - format 0: WGS84, a = lat, b = lon (degrees) -> system coordinates through the site anchor (UTM, meters);
  1233. - format 1: local binding (bitmap map), a = y, b = x in map units: the map shows a node at
  1234. anchor + its local coordinates, so system = map - anchor.
  1235. The point is sent as EVT_ROI_ACTIVE (EVT_CLASS_SYSTEM) like a sensor's ROI: it is an event of the
  1236. report (dispatchEvent) and every camera that can see it (testFOV) looks at it.
  1237. */
  1238. bool mojoKernel::mapROI(const char * param)
  1239. {
  1240. char tB[256];
  1241. int tFormat = -1;
  1242. double tA = 0, tC = 0;
  1243. if (param == NULL || sscanf(param, "%d;%lf;%lf", &tFormat, &tA, &tC) != 3 || (tFormat != 0 && tFormat != 1) || ! std::isfinite(tA) || ! std::isfinite(tC))
  1244. {
  1245. snprintf(tB, sizeof(tB), "Map ROI: wrong parameter [%.64s]", param ? param : "");
  1246. LOG_ERROR(tB);
  1247. return false;
  1248. };
  1249. if (m_pSystemAnchor == NULL)
  1250. {
  1251. LOG_ERROR("Map ROI: no system anchor!");
  1252. return false;
  1253. };
  1254. bool tLocal = (tFormat == 1);
  1255. if (tLocal != m_pSystemAnchor->beLocal())
  1256. {
  1257. // the map type was changed after the start: the anchor still uses the other system
  1258. snprintf(tB, sizeof(tB), "Map ROI: the map is %s, the site anchor is %s - ignored (restart needed)", tLocal ? "local" : "WGS84", m_pSystemAnchor->beLocal() ? "local" : "WGS84");
  1259. LOG_ERROR(tB);
  1260. return false;
  1261. };
  1262. TARGET_DATA * tData = (TARGET_DATA *) malloc(sizeof(TARGET_DATA)); // freed with the event
  1263. if (tData == NULL)
  1264. {
  1265. return false;
  1266. };
  1267. memset(tData, 0, sizeof(TARGET_DATA));
  1268. tData->source_position.lat = m_pSystemAnchor->getLat();
  1269. tData->source_position.lon = m_pSystemAnchor->getLon();
  1270. tData->source_position.alt = m_pSystemAnchor->getAlt();
  1271. if (tLocal)
  1272. {
  1273. tData->system_target.x = (C3D_FLOAT) (tC - m_pSystemAnchor->getX());
  1274. tData->system_target.y = (C3D_FLOAT) (tA - m_pSystemAnchor->getY());
  1275. tData->system_target.z = 0;
  1276. // as the sensors do in a local system
  1277. tData->global_target.lat = tData->system_target.y;
  1278. tData->global_target.lon = tData->system_target.x;
  1279. tData->global_target.alt = tData->system_target.z;
  1280. }
  1281. else
  1282. {
  1283. tData->global_target.lat = (C3D_FLOAT) tA;
  1284. tData->global_target.lon = (C3D_FLOAT) tC;
  1285. tData->global_target.alt = m_pSystemAnchor->getZ(); // no height on the map: the site's ground level
  1286. m_pSystemAnchor->convertToSystem(tData->global_target, tData->system_target);
  1287. };
  1288. snprintf(tB, sizeof(tB), "Map ROI: %s %.7f %.7f -> system x:%.2f y:%.2f z:%.2f", tLocal ? "local" : "WGS84", tA, tC, tData->system_target.x, tData->system_target.y, tData->system_target.z);
  1289. LOG(tB);
  1290. startSession();
  1291. EVENT_CONTAINER * bEVT = createBasicEvent(m_nServerNodeID, EVT_CLASS_SYSTEM, EVT_ROI_ACTIVE, NULL, NULL, tData);
  1292. if (bEVT == NULL)
  1293. {
  1294. LOG_ERROR("Map ROI: event creation failure!");
  1295. free(tData);
  1296. return false;
  1297. };
  1298. m_pEventHeap->broadcast(bEVT);
  1299. return true;
  1300. }
  1301. bool mojoKernel::dispatchEvent(EVENT_CONTAINER * Evt)
  1302. {
  1303. if (Evt == NULL)
  1304. {
  1305. return false;
  1306. }
  1307. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1308. {
  1309. m_pRPC->addEvent(Evt);
  1310. }
  1311. if (! m_bKeepLocalReport)
  1312. {
  1313. return true;
  1314. }
  1315. if (m_pDBC == NULL)
  1316. {
  1317. return false;
  1318. }
  1319. if( m_bForceCloseSessionOnEndOfTheDay && m_bSessionStarted ) {
  1320. #ifndef LRF_PROXY_ONLY
  1321. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1322. if( tMO ){
  1323. if( !tMO->isProcessingVideoStream() ) {
  1324. stopSession();
  1325. m_bForceCloseSessionOnEndOfTheDay = false;
  1326. };
  1327. } else {
  1328. LOG_ERROR("No Embed Video Processing Module!");
  1329. };
  1330. #else
  1331. stopSession();
  1332. m_bForceCloseSessionOnEndOfTheDay = false;
  1333. #endif
  1334. }
  1335. switch (Evt->m_eClass)
  1336. {
  1337. case EVT_CLASS_SYSTEM:
  1338. switch (Evt->m_eType)
  1339. {
  1340. case EVT_ROI_ACTIVE:
  1341. {
  1342. // Map ROI (mapROI): an event of the report like a sensor's ROI, the cameras look at the point
  1343. preserveSession();
  1344. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1345. if (tDATA != NULL)
  1346. {
  1347. DBC_EVT_RECORD tRecord;
  1348. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1349. tRecord.node_id = Evt->m_nSourceNodeID;
  1350. tRecord.type = Evt->m_eType;
  1351. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1352. tRecord.ts_s = Evt->m_UMSTS / 1000;
  1353. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  1354. tRecord.lat = tDATA->global_target.lat;
  1355. tRecord.lon = tDATA->global_target.lon;
  1356. tRecord.alt = tDATA->global_target.alt;
  1357. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1358. };
  1359. break;
  1360. };
  1361. case EVT_LRF_START_SESSION:
  1362. case EVT_LRF_END_SESSION_TO:
  1363. case EVT_LRF_END_SESSION_MA:
  1364. {
  1365. DBC_EVT_RECORD tRecord;
  1366. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1367. tRecord.ts_s = Evt->m_UMSTS / 1000;
  1368. tRecord.ts_ms = Evt->m_UMSTS % 1000;
  1369. tRecord.node_id = m_nServerNodeID;
  1370. tRecord.type = Evt->m_eType;
  1371. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1372. m_pDBC->addEvent(&tRecord);
  1373. break;
  1374. };
  1375. default: break;
  1376. };
  1377. break;
  1378. /*
  1379. case EVT_CLASS_EXTERNAL_CMD:
  1380. switch( Evt->m_eType ){
  1381. case EVT_FH_JAMER_ON:{
  1382. uint32_t TS_A, TS_B;
  1383. TS_A = Evt->m_UMSTS / 1000;
  1384. TS_B = Evt->m_UMSTS % 1000;
  1385. #ifdef SIMULATE_KERNEL_TARGETS
  1386. m_bJamingTO = SIMULATE_JAMER_TO;
  1387. #endif
  1388. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1389. if( tData ) {
  1390. if( m_pDBC->addEvent(
  1391. 0,
  1392. Evt->m_UUID,
  1393. TS_A,
  1394. TS_B,
  1395. EVT_FH_JAMER_ON,
  1396. 0,
  1397. 0,
  1398. 0,
  1399. 0,
  1400. 0,
  1401. 0,
  1402. 0,
  1403. 0,
  1404. 0,
  1405. 0,
  1406. tData->evtUUID
  1407. ) ){
  1408. //Simulate FH Module Event;
  1409. EVENT_CONTAINER *ttt;
  1410. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1411. if( nnn ){
  1412. nnn->location.lat = 0;
  1413. nnn->location.lon = 0;
  1414. nnn->location.alt = 0;
  1415. sprintf( nnn->missionID, tData->evtUUID );
  1416. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_JAMER_ON, NULL, NULL, nnn );
  1417. if( ttt ) {
  1418. m_pEventHeap->broadcast( ttt );
  1419. } else {
  1420. free( nnn );
  1421. };
  1422. };
  1423. };
  1424. free(tData);
  1425. };
  1426. break;
  1427. };
  1428. case EVT_FH_MISSION_END:{
  1429. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1430. if( tData ) {
  1431. //DBC_EVT_DATA * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1432. if( 1 / *tEVT* / ) {
  1433. uint32_t TS_A, TS_B;
  1434. TS_A = Evt->m_UMSTS / 1000;
  1435. TS_B = Evt->m_UMSTS % 1000;
  1436. if( m_pDBC->addEvent(
  1437. 0,
  1438. Evt->m_UUID,
  1439. TS_A,
  1440. TS_B,
  1441. EVT_FH_LANDED ) ){
  1442. //Simulate FH Module Event;
  1443. EVENT_CONTAINER *ttt;
  1444. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1445. if( nnn ){
  1446. nnn->location.lat = 0;
  1447. nnn->location.lon = 0;
  1448. nnn->location.alt = 0;
  1449. sprintf( nnn->missionID, tData->evtUUID );
  1450. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_LANDED, NULL, NULL, nnn );
  1451. if( ttt ) {
  1452. m_pEventHeap->broadcast( ttt );
  1453. } else {
  1454. free( nnn );
  1455. };
  1456. };
  1457. };
  1458. //free( tEVT );
  1459. } else {
  1460. LOG_ERROR("Linked Event Was Not Found!");
  1461. };
  1462. };
  1463. break;
  1464. }
  1465. case EVT_FH_MISSION_BEGIN:{
  1466. FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData;
  1467. #ifdef SIMULATE_KERNEL_TARGETS
  1468. m_bLandingTO = SIMULATE_LANDING_TO;
  1469. #endif
  1470. if( tData ) {
  1471. #ifdef SIMULATE_KERNEL_TARGETS
  1472. sprintf( m_cCurrentMission, tData->evtUUID );
  1473. #endif
  1474. DBC_EVT_RECORD * tEVT = m_pDBC->getEventByUUID( tData->targetUUID );
  1475. if( tEVT ) {
  1476. uint32_t TS_A, TS_B;
  1477. TS_A = Evt->m_UMSTS / 1000;
  1478. TS_B = Evt->m_UMSTS % 1000;
  1479. if( Evt->m_db_id = m_pDBC->addEvent(
  1480. 0,
  1481. Evt->m_UUID,
  1482. TS_A,
  1483. TS_B,
  1484. EVT_FH_FLY_TO,
  1485. 0,
  1486. 0,
  1487. 0,
  1488. 0,
  1489. 0,
  1490. 0,
  1491. tEVT->lat,
  1492. tEVT->lon,
  1493. tEVT->alt,
  1494. tEVT->id,
  1495. tEVT->uuid
  1496. ) ){
  1497. //Simulate FH Module Event;
  1498. EVENT_CONTAINER * ttt;
  1499. FH_MISSION_DATA * nnn = (FH_MISSION_DATA *) malloc( sizeof(FH_MISSION_DATA));
  1500. if( nnn ){
  1501. nnn->location.lat = tEVT->lat;
  1502. nnn->location.lon = tEVT->lon;
  1503. nnn->location.alt = tEVT->alt;
  1504. sprintf( nnn->missionID, tData->evtUUID );
  1505. ttt = createBasicEvent( 0, EVT_CLASS_FLY_HUNTER, EVT_FH_MISSION_BEGIN, NULL, NULL, nnn );
  1506. if( ttt ) {
  1507. m_pEventHeap->broadcast( ttt );
  1508. } else {
  1509. free( nnn );
  1510. };
  1511. };
  1512. };
  1513. free( tEVT );
  1514. } else {
  1515. LOG_ERROR("Linked Event Was Not Found!");
  1516. };
  1517. };
  1518. break;
  1519. };
  1520. };
  1521. break;
  1522. */
  1523. case EVT_CLASS_VIDEO:
  1524. {
  1525. switch (Evt->m_eType)
  1526. {
  1527. case EVT_CAPTURE_DONE:
  1528. case EVT_CAPTURE_END:
  1529. {
  1530. if (Evt->m_pEvtData)
  1531. {
  1532. handleVideo((VIDEO_SEGMENT_INFO *) Evt->m_pEvtData);
  1533. // Every sender allocates a copy with new (streamHandler encoder, kernelModuleEmbedVideo
  1534. // recorder clips); the remote client copies what it needs (init)
  1535. delete (VIDEO_SEGMENT_INFO *) Evt->m_pEvtData;
  1536. Evt->m_pEvtData = NULL;
  1537. #ifdef DEBUG_EXTRA
  1538. LOG("VIDEO_SEGMENT_INFO Cleared!");
  1539. #endif
  1540. };
  1541. break;
  1542. };
  1543. case EVT_VIDEO_FPS_DEGRADED:
  1544. case EVT_VIDEO_FPS_RAISED:
  1545. {
  1546. if (! m_bDebugMode)
  1547. {
  1548. break;
  1549. }
  1550. DBC_EVT_RECORD tRecord;
  1551. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1552. uint32_t TS_A, TS_B;
  1553. TS_A = Evt->m_UMSTS / 1000;
  1554. TS_B = Evt->m_UMSTS % 1000;
  1555. tRecord.node_id = Evt->m_nSourceNodeID;
  1556. tRecord.type = Evt->m_eType;
  1557. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1558. tRecord.ts_s = TS_A;
  1559. tRecord.ts_ms = TS_B;
  1560. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1561. break;
  1562. };
  1563. default: break;
  1564. };
  1565. break;
  1566. };
  1567. case EVT_CLASS_SENSOR_CLD:
  1568. case EVT_CLASS_SENSOR_LGS:
  1569. case EVT_CLASS_SENSOR_LFS:
  1570. case EVT_CLASS_SENSOR_MND:
  1571. case EVT_CLASS_SENSOR_PLS_210:
  1572. case EVT_CLASS_SENSOR_PLS_360:
  1573. case EVT_CLASS_SENSOR_ALS:
  1574. case EVT_CLASS_SENSOR_3D:
  1575. case EVT_CLASS_SENSOR_ACC:
  1576. #ifdef DEBUG_EXTRA
  1577. char tB[128];
  1578. sprintf(tB, "Evt Class: %d Evt Type: %d ", Evt->m_eClass, Evt->m_eType);
  1579. LOG(tB);
  1580. #endif
  1581. switch (Evt->m_eType)
  1582. {
  1583. case EVT_TARGET_1D:
  1584. {
  1585. // startSession();
  1586. preserveSession();
  1587. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1588. if (tDATA != NULL)
  1589. {
  1590. DBC_EVT_RECORD tRecord;
  1591. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1592. uint32_t TS_A, TS_B;
  1593. TS_A = Evt->m_UMSTS / 1000;
  1594. TS_B = Evt->m_UMSTS % 1000;
  1595. tRecord.node_id = Evt->m_nSourceNodeID;
  1596. tRecord.type = Evt->m_eType;
  1597. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1598. tRecord.ts_s = TS_A;
  1599. tRecord.ts_ms = TS_B;
  1600. tRecord.range_digital = tDATA->digital;
  1601. tRecord.ra = tDATA->local_target.r;
  1602. tRecord.area = tDATA->area;
  1603. tRecord.lat = tDATA->global_target.lat;
  1604. tRecord.lon = tDATA->global_target.lon;
  1605. tRecord.alt = tDATA->global_target.alt;
  1606. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1607. };
  1608. break;
  1609. };
  1610. // While Camera Always Records Video on each Absolute Move
  1611. // an Event Must be generated to see video in report
  1612. case EVT_ROI_ACTIVE: // if( !m_bDebugMode ) break;
  1613. case EVT_TARGET_2D_BLACK:
  1614. case EVT_TARGET_2D_WHITE:
  1615. {
  1616. // startSession();
  1617. preserveSession();
  1618. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1619. if (tDATA != NULL)
  1620. {
  1621. DBC_EVT_RECORD tRecord;
  1622. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1623. uint32_t TS_A, TS_B;
  1624. TS_A = Evt->m_UMSTS / 1000;
  1625. TS_B = Evt->m_UMSTS % 1000;
  1626. tRecord.node_id = Evt->m_nSourceNodeID;
  1627. tRecord.type = Evt->m_eType;
  1628. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1629. tRecord.ts_s = TS_A;
  1630. tRecord.ts_ms = TS_B;
  1631. tRecord.lrf_id = tDATA->lrf_id;
  1632. tRecord.range_digital = tDATA->digital;
  1633. tRecord.az = tDATA->local_target.fi;
  1634. tRecord.ra = tDATA->local_target.r;
  1635. tRecord.area = tDATA->area;
  1636. tRecord.lat = tDATA->global_target.lat;
  1637. tRecord.lon = tDATA->global_target.lon;
  1638. tRecord.alt = tDATA->global_target.alt;
  1639. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1640. };
  1641. break;
  1642. };
  1643. case EVT_TARGET_3D_BLACK:
  1644. case EVT_TARGET_3D_WHITE:
  1645. {
  1646. // startSession();
  1647. preserveSession();
  1648. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1649. if (tDATA != NULL)
  1650. {
  1651. DBC_EVT_RECORD tRecord;
  1652. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1653. uint32_t TS_A, TS_B;
  1654. TS_A = Evt->m_UMSTS / 1000;
  1655. TS_B = Evt->m_UMSTS % 1000;
  1656. tRecord.node_id = Evt->m_nSourceNodeID;
  1657. tRecord.type = Evt->m_eType;
  1658. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1659. tRecord.ts_s = TS_A;
  1660. tRecord.ts_ms = TS_B;
  1661. tRecord.lrf_id = tDATA->lrf_id;
  1662. tRecord.range_digital = tDATA->digital;
  1663. tRecord.az = tDATA->local_target.fi;
  1664. tRecord.el = tDATA->local_target.tetta;
  1665. tRecord.ra = tDATA->local_target.r;
  1666. tRecord.area = tDATA->area;
  1667. tRecord.lat = tDATA->global_target.lat;
  1668. tRecord.lon = tDATA->global_target.lon;
  1669. tRecord.alt = tDATA->global_target.alt;
  1670. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1671. };
  1672. break;
  1673. };
  1674. case EVT_ERR_LRF_NO_IRQ:
  1675. case EVT_ERR_LRF_NO_ZERO:
  1676. case EVT_ERR_LRF_ANGLE:
  1677. case EVT_ERR_LRF_VOLTAGE:
  1678. case EVT_ERR_LRF_BROKEN:
  1679. case EVT_LRF_VIS_CHANGED:
  1680. case EVT_ERR_OVERFLOW:
  1681. case EVT_SST_OVERFLOW:
  1682. case EVT_COMMUNICATION_JAMED:
  1683. if (! m_bDebugMode)
  1684. {
  1685. break;
  1686. }
  1687. /* fall through */
  1688. case EVT_OP_ACK_MASK:
  1689. case EVT_OP_NACK_MASK:
  1690. case EVT_OP_ON_MASK:
  1691. case EVT_OP_OFF_MASK:
  1692. case EVT_CONNECTED:
  1693. case EVT_DISCONNECTED:
  1694. {
  1695. DBC_EVT_RECORD tRecord;
  1696. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1697. uint32_t TS_A, TS_B;
  1698. TS_A = Evt->m_UMSTS / 1000;
  1699. TS_B = Evt->m_UMSTS % 1000;
  1700. tRecord.node_id = Evt->m_nSourceNodeID;
  1701. tRecord.type = Evt->m_eType;
  1702. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1703. tRecord.ts_s = TS_A;
  1704. tRecord.ts_ms = TS_B;
  1705. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1706. break;
  1707. };
  1708. case EVT_FLIP:
  1709. {
  1710. if (! m_bDebugMode)
  1711. {
  1712. break;
  1713. }
  1714. DBC_EVT_RECORD tRecord;
  1715. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1716. uint32_t TS_A, TS_B;
  1717. TS_A = Evt->m_UMSTS / 1000;
  1718. TS_B = Evt->m_UMSTS % 1000;
  1719. tRecord.node_id = Evt->m_nSourceNodeID;
  1720. tRecord.type = Evt->m_eType;
  1721. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1722. tRecord.ts_s = TS_A;
  1723. tRecord.ts_ms = TS_B;
  1724. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1725. break;
  1726. };
  1727. default: break;
  1728. };
  1729. break;
  1730. case EVT_CLASS_KRNL_FILTER_LATF:
  1731. {
  1732. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1733. if ((Evt->m_eType > EVT_FLT_LATF_DIFF_DET) || (tDATA == NULL))
  1734. {
  1735. // AUX messages (no additional target data)
  1736. DBC_EVT_RECORD tRecord;
  1737. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1738. uint32_t TS_A, TS_B;
  1739. TS_A = Evt->m_UMSTS / 1000;
  1740. TS_B = Evt->m_UMSTS % 1000;
  1741. tRecord.node_id = m_nServerNodeID;
  1742. tRecord.type = Evt->m_eType;
  1743. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1744. tRecord.ts_s = TS_A;
  1745. tRecord.ts_ms = TS_B;
  1746. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1747. }
  1748. else
  1749. {
  1750. // If First Suspiciouse -> Start session to collect Fliter Messages
  1751. // if( Evt->m_eType == EVT_FLT_LATF_SUSP_START ) startSession();
  1752. // Regular LATF with target Info
  1753. DBC_EVT_RECORD tRecord;
  1754. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1755. uint32_t TS_A, TS_B;
  1756. TS_A = Evt->m_UMSTS / 1000;
  1757. TS_B = Evt->m_UMSTS % 1000;
  1758. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1759. tRecord.type = Evt->m_eType;
  1760. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1761. tRecord.ts_s = TS_A;
  1762. tRecord.ts_ms = TS_B;
  1763. tRecord.lrf_id = tDATA->lrf_id;
  1764. tRecord.range_digital = tDATA->digital;
  1765. tRecord.az = tDATA->local_target.fi;
  1766. tRecord.el = tDATA->local_target.tetta;
  1767. tRecord.ra = tDATA->local_target.r;
  1768. tRecord.area = tDATA->area;
  1769. tRecord.lat = 0;
  1770. tRecord.lon = 0;
  1771. tRecord.alt = 0;
  1772. #ifdef DEBUG_EXTRA
  1773. char tB[243];
  1774. 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);
  1775. LOG(tB);
  1776. #endif
  1777. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1778. };
  1779. break;
  1780. };
  1781. case EVT_CLASS_KRNL_FILTER_BUBBLE:
  1782. {
  1783. 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)
  1784. {
  1785. preserveSession();
  1786. TARGET_DATA * tDATA = (TARGET_DATA *) Evt->m_pEvtData;
  1787. DBC_EVT_RECORD tRecord;
  1788. memset(&tRecord, 0, sizeof(DBC_EVT_RECORD));
  1789. uint32_t TS_A, TS_B;
  1790. TS_A = Evt->m_UMSTS / 1000;
  1791. TS_B = Evt->m_UMSTS % 1000;
  1792. tRecord.node_id = Evt->m_nSourceNodeID; // m_nServerNodeID;//??
  1793. tRecord.type = Evt->m_eType;
  1794. strncpy(tRecord.uuid, Evt->m_UUID, 64);
  1795. tRecord.ts_s = TS_A;
  1796. tRecord.ts_ms = TS_B;
  1797. tRecord.lrf_id = tDATA->lrf_id;
  1798. tRecord.range_digital = tDATA->digital;
  1799. tRecord.az = tDATA->local_target.fi;
  1800. tRecord.el = tDATA->local_target.tetta;
  1801. tRecord.ra = tDATA->local_target.r;
  1802. tRecord.area = tDATA->area;
  1803. tRecord.lat = 0;
  1804. tRecord.lon = 0;
  1805. tRecord.alt = 0;
  1806. #ifdef DEBUG_EXTRA
  1807. char tB[243];
  1808. 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);
  1809. LOG(tB);
  1810. #endif
  1811. Evt->m_db_id = m_pDBC->addEvent(&tRecord);
  1812. };
  1813. break;
  1814. };
  1815. case EVT_CLASS_REMOTE_SERVER_CMD:
  1816. {
  1817. switch (Evt->m_eType)
  1818. {
  1819. case EVT_RPC_FRIZE_ACTIVE_B:
  1820. {
  1821. // getFilterId
  1822. break;
  1823. };
  1824. default: break;
  1825. };
  1826. break;
  1827. };
  1828. default: break;
  1829. };
  1830. return true;
  1831. }
  1832. /*
  1833. typedef struct S_VIDEO_SEGMENT_INFO{
  1834. uint32_t stream_id;
  1835. uint32_t streamTSS;
  1836. uint32_t streamTMS;
  1837. DBC_INT_LIST eventIDS;
  1838. DBC_INT_LIST eventFrames;
  1839. DBC_INT_LIST eventTSS;
  1840. DBC_INT_LIST eventTMS;
  1841. char recordMD5[33];
  1842. char recordEXT[6];
  1843. uint8_t streamFPS;
  1844. char scratchPath[PATH_MAX];
  1845. uint32_t dataSize;
  1846. uint32_t db_id;
  1847. }VIDEO_SEGMENT_INFO;
  1848. */
  1849. void mojoKernel::handleVideo(VIDEO_SEGMENT_INFO * ptr)
  1850. {
  1851. if (fileExist(ptr->scratchPath))
  1852. {
  1853. bool bKeepVideoFile = false;
  1854. if (ptr->recordMD5[0])
  1855. {
  1856. if (m_bKeepLocalReport)
  1857. {
  1858. // if wm is debug -> add all to database, else send to server
  1859. ptr->db_id = m_pDBC->addVideoRecord(ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS);
  1860. if (ptr->db_id)
  1861. {
  1862. #ifdef DEBUG_EXTRA
  1863. LOG("Video File Registered");
  1864. #endif
  1865. for (size_t idx = 0; idx < ptr->eventIDS.size(); idx++)
  1866. {
  1867. #ifdef DEBUG_EXTRA
  1868. LOG("Binding Event to a Video File!");
  1869. #endif
  1870. if (m_pDBC->addVideoEvent(ptr->eventIDS[idx], ptr->db_id, ptr->eventFrames[idx], ptr->eventTSS[idx], ptr->eventTMS[idx]))
  1871. {
  1872. #ifdef DEBUG_EXTRA
  1873. LOG("OK");
  1874. #endif
  1875. };
  1876. };
  1877. CSTR tP = m_pDBC->getCurrentReportDir();
  1878. if (tP)
  1879. {
  1880. char tB[MAX_PATH];
  1881. char dB[MAX_PATH];
  1882. // sprintf( tB, "%s/data/videos/%s.%s", getcwd(dB,MAX_PATH), ptr->recordMD5, ptr->recordEXT );
  1883. sprintf(tB, "%s/data/reports/%s/%s.%s", getcwd(dB, MAX_PATH), tP, ptr->recordMD5, ptr->recordEXT);
  1884. fileCopy(ptr->scratchPath, tB);
  1885. ch_chown(tB, "nobody", "nogroup");
  1886. chmod(tB, 0664);
  1887. #ifdef DEBUG_EXTRA
  1888. LOG("Video Saved");
  1889. #endif
  1890. free(tP);
  1891. }
  1892. else
  1893. {
  1894. LOG_ERROR("Cannot Get Report Path from DB!");
  1895. };
  1896. }
  1897. else
  1898. {
  1899. LOG_ERROR("Failure in registration video!");
  1900. };
  1901. };
  1902. if (m_bWorkWithRemoteServer && (m_pRPC != NULL))
  1903. {
  1904. bKeepVideoFile = m_pRPC->addVideo(ptr);
  1905. }
  1906. };
  1907. if (! bKeepVideoFile)
  1908. {
  1909. unlink(ptr->scratchPath);
  1910. #ifdef DEBUG_EXTRA
  1911. LOG("Video Scratch Deleted!");
  1912. #endif
  1913. };
  1914. } else {
  1915. LOG_ERROR("Video File is Missing!");
  1916. };
  1917. //In case end of the day Force Session Close after Video File is saved
  1918. if ( m_bForceCloseSessionOnEndOfTheDay ) {
  1919. stopSession( m_bForceCloseSessionOnEndOfTheDay );
  1920. m_bForceCloseSessionOnEndOfTheDay = false;
  1921. };
  1922. }
  1923. /*
  1924. * Local "report period" = day-of-month * 100 + hour: changes every local hour (and at midnight).
  1925. */
  1926. static int getLocalReportPeriod(void){
  1927. time_t t = time(NULL);
  1928. tm lt;
  1929. if ( localtime_r( &t, &lt ) == NULL ) return -1;
  1930. return lt.tm_mday * 100 + lt.tm_hour;
  1931. }
  1932. /*
  1933. * Called every second from the kernel thread.
  1934. * Every local hour the current report (data/reports/YYYY/MM/DD/report_HH.db) is closed and
  1935. * the next hour's report is opened - at midnight in the next day's directory.
  1936. *
  1937. * Order of operations:
  1938. * 1. an open session that started in the previous hour is closed (END event goes into the old
  1939. * report) - if a video segment with events is still recording, wait for it at most
  1940. * REPORT_SWITCH_MAX_VIDEO_WAIT seconds so the video is stored next to its events;
  1941. * 2. once the END event is written (event thread), the report is switched to the new hour.
  1942. * The next event opens a new session in the new report.
  1943. * A session started after the hour change already switched the report itself (SESSION_START).
  1944. */
  1945. void mojoKernel::handleReportPeriodChange(void){
  1946. int cp = getLocalReportPeriod();
  1947. if ( cp < 0 ) return;
  1948. time_t now = time(NULL);
  1949. if ( !m_bReportSwitchPending ) {
  1950. if ( m_nReportPeriod == cp ) return;
  1951. m_bReportSwitchPending = true;
  1952. m_tReportSwitchDetected = now;
  1953. LOG("Local hour changed - closing hour report");
  1954. };
  1955. bool bTimeOut = ( now - m_tReportSwitchDetected ) >= REPORT_SWITCH_MAX_VIDEO_WAIT;
  1956. if ( m_bSessionStarted && ( m_nSessionPeriod != cp ) ) {
  1957. // Session belongs to the previous hour - close it in the previous hour's report
  1958. m_bForceCloseSessionOnEndOfTheDay = true;
  1959. #ifndef LRF_PROXY_ONLY
  1960. kernelModuleEmbedVideo * tMO = (kernelModuleEmbedVideo *) m_pModuleFactory->getModule(KERNEL_MODULE_EMBED_VIDEO);
  1961. if ( tMO && tMO->isProcessingVideoStream() ) {
  1962. if ( !bTimeOut ) return; // let the running recording finish into the old report
  1963. LOG_ERROR("Hour change: video still recording - forcing session close");
  1964. };
  1965. #endif
  1966. stopSession( true );
  1967. return; // switch on a next call, after the END event is written by the event thread
  1968. };
  1969. // Wait until the END of the previous session is stored in the old report
  1970. if ( !m_bSessionStarted && m_pDBC->getEventSessionStatus() && !bTimeOut ) return;
  1971. if ( m_pDBC->switchReportToCurrentHour() ) {
  1972. m_nReportPeriod = cp;
  1973. m_bReportSwitchPending = false;
  1974. m_bForceCloseSessionOnEndOfTheDay = false;
  1975. LOG("Report switched to the new hour");
  1976. } else {
  1977. LOG_ERROR("Hour report switch failed - will retry");
  1978. };
  1979. }
  1980. bool mojoKernel::validateReports(void){
  1981. /// @todo Stephane needs to follow this function...
  1982. if ( m_bKeepLocalReport ){
  1983. // Every local hour: close the hour report and open the next one (next day's folder at midnight)
  1984. handleReportPeriodChange();
  1985. DBC_SITE_DATA * tSiteData = m_pDBC->getSiteData(1);
  1986. if (tSiteData){
  1987. // Check if settings has been changed in DB
  1988. if (tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty){
  1989. memcpy(&m_sSiteData, tSiteData, sizeof(DBC_SITE_DATA));
  1990. };
  1991. free(tSiteData);
  1992. } else {
  1993. LOG_ERROR("Error Loading Site DATA!");
  1994. return false;
  1995. };
  1996. if (m_sSiteData.report_cleanup) {
  1997. cleanUpReportDir(m_sSiteData.report_cleanup);
  1998. };
  1999. if (m_sSiteData.preserve_empty) {
  2000. float tT = 100.0f * (1.0f - (1.0f / ((float) m_sSiteData.preserve_empty)));
  2001. #ifdef DEBUG_EXTRA
  2002. char tB[128];
  2003. sprintf(tB, "Check Disk Size: %0.3f %0.3f %d [%d]", m_sSysInfo.disk, tT, m_sSiteData.preserve_empty);
  2004. LOG(tB);
  2005. #endif
  2006. if (m_sSysInfo.disk > tT) {
  2007. #ifdef DEBUG_EXTRA
  2008. LOG("Delete Some Reports!");
  2009. #endif
  2010. // delete some oldest report date
  2011. m_pDBC->deleteReportDate(m_pDBC->getFirstEventTS());
  2012. };
  2013. };
  2014. };
  2015. return true;
  2016. }
  2017. void mojoKernel::backupReports(uint32_t days)
  2018. {
  2019. if (! m_bKeepLocalReport)
  2020. {
  2021. return;
  2022. }
  2023. REPORT_DIRINFO tRDI;// = {0};
  2024. //memset( &tRDI, 0, sizeof(REPORT_DIRINFO)); will be initialized by fillRDF
  2025. uint32_t cTS = (uint32_t) time(NULL);
  2026. cTS -= 86400 * days;
  2027. fillRDF(cTS, tRDI);
  2028. DBC_INT_LIST * TL = m_pDBC->listOlderReportStat(tRDI.YEAR, tRDI.MONTH, tRDI.DAY);
  2029. if (TL)
  2030. {
  2031. for (DBC_INT_LIST::iterator idx = TL->begin(); idx < TL->end(); idx++)
  2032. {
  2033. REPORT_DIRINFO * tRDI = m_pDBC->getReportDirInfo(*idx);
  2034. if (tRDI)
  2035. {
  2036. char tB[MAX_PATH];
  2037. char dB[MAX_PATH];
  2038. sprintf(tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  2039. char tPATH[MAX_PATH];
  2040. sprintf(tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY);
  2041. if (fileExist(tB))
  2042. {
  2043. if (! fileExist(tPATH))
  2044. {
  2045. // Try Create Job if sucess then create dir to skip next validation
  2046. if (createCronJob(0, tPATH, cTS))
  2047. {
  2048. // Create Backup Dir
  2049. uid_t uid = getUserUid(m_pDBC->getDataParam(1));
  2050. gid_t gid = getUserGid(m_pDBC->getDataParam(2));
  2051. if (createAccessibleDir(tPATH, STORAGE_DIR_MODE, uid, gid))
  2052. {
  2053. // Create Just One Backup Per Time!
  2054. free(tRDI);
  2055. break;
  2056. };
  2057. };
  2058. }; // else proceed next
  2059. };
  2060. free(tRDI);
  2061. };
  2062. };
  2063. delete(TL);
  2064. };
  2065. }
  2066. void mojoKernel::cleanUpReportDir(uint32_t days)
  2067. {
  2068. // Cleanup Current reports no Backup
  2069. uint32_t oTS = m_pDBC->getFirstEventTS();
  2070. uint32_t cTS = (uint32_t) time(NULL);
  2071. cTS -= 86400 * days;
  2072. #ifdef DEBUG_EXTRA
  2073. char tB[128];
  2074. sprintf(tB, "Oldest %d border %d", oTS, cTS);
  2075. LOG(tB);
  2076. #endif
  2077. if (oTS <= cTS)
  2078. {
  2079. #ifdef DEBUG_EXTRA
  2080. LOG("Deleting Old Reports");
  2081. #endif
  2082. /// @todo Stephane needs to see when this gets called...
  2083. m_pDBC->deleteReportDate(oTS);
  2084. };
  2085. }
  2086. bool mojoKernel::createCronJob(uint8_t job, CSTR param_str, int param_int)
  2087. {
  2088. if (! fileExist("/srv/etc/cron.lock"))
  2089. {
  2090. Json::Value root;
  2091. // Json::Value data;
  2092. switch (job)
  2093. {
  2094. case CRON_JOB_CREATE_BACKUP:
  2095. {
  2096. root["operation"] = CRON_JOB_CREATE_BACKUP;
  2097. // data["number"] = 1;
  2098. root["ts"] = param_int;
  2099. root["backup_dst"] = std::string(param_str);
  2100. break;
  2101. };
  2102. // No default JOB!
  2103. default: return false;
  2104. };
  2105. FILE * fd = fopen("/srv/etc/cron_.json", "w+");
  2106. if (fd)
  2107. {
  2108. if (HUMAN_FRIENDLY_JSON)
  2109. {
  2110. //Json::FastWriter writer;
  2111. //const std::string json_file = writer.write(root);
  2112. //fprintf(fd, json_file.c_str());
  2113. Json::StreamWriterBuilder builder;
  2114. const std::string json_file = Json::writeString(builder, root);
  2115. fprintf(fd, json_file.c_str());
  2116. }
  2117. else
  2118. {
  2119. Json::StreamWriterBuilder builder;
  2120. const std::string json_file = Json::writeString(builder, root);
  2121. fprintf(fd, json_file.c_str());
  2122. };
  2123. fclose(fd);
  2124. return true;
  2125. };
  2126. };
  2127. return false;
  2128. }
  2129. #undef LOG
  2130. #undef LOG_ERROR