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.

2122 lines
58 KiB

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