Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Dec 8, 2024
1 parent 5667d05 commit b51cdd8
Show file tree
Hide file tree
Showing 546 changed files with 73,613 additions and 63 deletions.
8 changes: 8 additions & 0 deletions www/controllers/App/Config/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ public static function get()
if (!defined('DB')) {
define('DB', DB_DIR . '/motionui.sqlite');
}
// Websocket server database
if (!defined('WS_DB')) {
define('WS_DB', DB_DIR . "/repomanager-ws.db");
}
if (!defined('LOGS_DIR')) {
define('LOGS_DIR', DATA_DIR . '/logs');
}
// Websocket server logs dir
if (!defined('WS_LOGS_DIR')) {
define('WS_LOGS_DIR', LOGS_DIR . '/websocket');
}
if (!defined('CAMERAS_DIR')) {
define('CAMERAS_DIR', DATA_DIR . '/cameras');
}
Expand Down
4 changes: 3 additions & 1 deletion www/controllers/App/Config/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static function get()
* If an update is available, generate a new notification
*/
if (UPDATE_AVAILABLE) {
$message = '<span class="yellowtext">A new release is available: <b>' . GIT_VERSION . '</b>.</span><br><br>Please update your docker image by following the steps documented <a href="' . PROJECT_UPDATE_DOC_URL . '">here</a></span>';
$message = '<p>A new release is available: <a href="' . PROJECT_GIT_REPO . '/releases/latest" target="_blank" rel="noopener noreferrer" title="See changelog"><code>' . GIT_VERSION . '</code> <img src="/assets/icons/external-link.svg" class="icon" /></a></p>';
$message .= '<p>Please update your docker image by following the steps documented <b><a href="' . PROJECT_UPDATE_DOC_URL . '" target="_blank" rel="noopener noreferrer"><code>here</code></b> <img src="/assets/icons/external-link.svg" class="icon" /></a></p>';

$NOTIFICATION_MESSAGES[] = array('Title' => 'Update available', 'Message' => $message);
$NOTIFICATION++;
}
Expand Down
4 changes: 4 additions & 0 deletions www/controllers/App/Structure/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static function create()
mkdir(LOGS_DIR, 0770, true);
}

if (!is_dir(WS_LOGS_DIR)) {
mkdir(WS_LOGS_DIR, 0770, true);
}

if (!is_dir(CAMERAS_DIR)) {
mkdir(CAMERAS_DIR, 0770, true);

Expand Down
38 changes: 24 additions & 14 deletions www/controllers/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,21 @@ public function run()
* Execute autostart
*/
if ($this->autostart == 'enabled') {
$this->runService('autostart');
$this->runService('autostart', 'autostart');
}

/**
* Execute timelapse
*/
if ($this->timelapse === true) {
$this->runService('timelapse');
$this->runService('timelapse', 'timelapse');
}

/**
* Start websocket server
*/
$this->runService('websocket server', 'wss');

/**
* Clean timelapse and events depending on retention
*/
Expand Down Expand Up @@ -372,28 +377,33 @@ public function run()
/**
* Run this service with the specified parameter
*/
private function runService(string $parameter)
private function runService(string $name, string $parameter, bool $force = false)
{
try {
/**
* Check if the service with specified parameter is already running (a php process must be running)
* Check if the service with specified parameter is already running to avoid running it twice
* A php process must be running
*
* If force != false, then the service will be run even if it is already running (e.g: for running multiple scheduled tasks at the same time)
*/
$myprocess = new \Controllers\Process("/usr/bin/ps aux | grep 'motionui." . $parameter . "' | grep -v grep");
$myprocess->execute();
$content = $myprocess->getOutput();
$myprocess->close();
if ($force === false) {
$myprocess = new \Controllers\Process('/usr/bin/ps aux | grep "repomanager.' . $parameter . '" | grep -v grep');
$myprocess->execute();
$content = $myprocess->getOutput();
$myprocess->close();

/**
* Quit if there is already a process running
*/
if ($myprocess->getExitCode() == 0) {
return;
/**
* Quit if there is already a process running
*/
if ($myprocess->getExitCode() == 0) {
return;
}
}

/**
* Else, run the service with the specified parameter
*/
echo $this->getDate() . " Running service with parameter '" . $parameter . "'..." . PHP_EOL;
echo $this->getDate() . ' Running ' . $name . '...' . PHP_EOL;

$myprocess = new \Controllers\Process("/usr/bin/php " . ROOT . "/tools/service.php '" . $parameter . "' >/dev/null 2>/dev/null &");
$myprocess->execute();
Expand Down
36 changes: 36 additions & 0 deletions www/controllers/Websocket/BrowserClient/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Controllers\Websocket\BrowserClient;

use Exception;

/**
* Class Process extends WebsocketServer to gain access to its methods
*/
class Process extends \Controllers\Websocket\WebsocketServer
{
/**
* Reload containers for all browser clients
*/
public function reload($socket)
{
// Get all containers that need to be reloaded
$containers = $this->layoutContainerReloadController->get();

// Quit if there are no containers to reload
if (empty($containers)) {
return;
}

// For each container, send a reload request to all browser clients
foreach ($containers as $container) {
$this->broadcast($socket, 'browser-client', array(
'type' => 'reload-container',
'container' => $container['Container']
));
}

// Clean up the layout container state
$this->layoutContainerReloadController->clean();
}
}
134 changes: 134 additions & 0 deletions www/controllers/Websocket/Socket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Controllers\Websocket;

/**
* Composer autoload
*/
require ROOT . '/libs/vendor/autoload.php';

use Exception;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

/**
* Class Socker extends WebsocketServer to gain access to its methods
*/
class Socket extends WebsocketServer implements MessageComponentInterface
{
protected $clients;

/**
* Initialize socket
* Basically like a constructor but to avoid conflicts with the parent constructor
*/
public function initialize()
{
/**
* Initialize clients storage
*/
$this->clients = new \SplObjectStorage;

/**
* Clean database from old connections
* (e.g. connections that were not removed from database because of a crash or a bug)
*/
try {
$this->cleanWsConnections();
} catch (Exception $e) {
$this->log('Error while cleaning database from old connections: ' . $e->getMessage());
}
}

/**
* Return all websocket clients
*/
public function getClients()
{
return $this->clients;
}

/**
* On websocket connection open
*/
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
$this->log('[connection #' . $conn->resourceId . '] New connection!');

/**
* Adding connection Id to database
*/
try {
$this->newWsConnection($conn->resourceId);
} catch (Exception $e) {
$this->log('[connection #' . $conn->resourceId . '] Error while adding connection to database: ' . $e->getMessage());

/**
* Send a message to the client to inform that the connection is not allowed, and close it
* TODO: Add an error Id to the message
*/
$conn->send(json_encode(array('error' => "You've been connected but an error occured on the server side. Please try again later.")));
$conn->close();
}
}

/**
* On websocket message received
*/
public function onMessage(ConnectionInterface $conn, $message)
{
/**
* Decode JSON message
*/
try {
$message = json_decode($message, true);
} catch (Exception $e) {
$this->log('[connection #' . $conn->resourceId . '] Error while decoding message: ' . $e->getMessage());
return;
}

/**
* If the client is sending its connection type
*/
if (!empty($message['connection-type'])) {
// Connection type must be either 'browser-client' or 'android-client'
if (!in_array($message['connection-type'], array('browser-client', 'android-client'))) {
$this->log('[connection #' . $conn->resourceId . '] Invalid connection type: ' . $message['connection-type']);

// Close connection
$conn->close();
}

// Set connection type in database
$this->setWsConnectionType($conn->resourceId, $message['connection-type']);
}
}

/**
* On websocket connection close
*/
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
$this->log('[connection #' . $conn->resourceId . '] Connection closed');

/**
* Removing connection Id from database
*/
try {
$this->deleteWsConnection($conn->resourceId);
} catch (Exception $e) {
$this->log('[connection #' . $conn->resourceId . '] Error while removing connection from database: ' . $e->getMessage());
}
}

/**
* On websocket connection error
*/
public function onError(ConnectionInterface $conn, \Exception $e)
{
$this->log('[connection #' . $conn->resourceId . '] An error occured with connection: ' . $e->getMessage());
$conn->close();
}
}
Loading

0 comments on commit b51cdd8

Please sign in to comment.