From: Vincent Date: Wed, 10 Jul 2019 06:09:40 +0000 (+0200) Subject: . X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=c70f165c187aaf0af0920408fb96ae7fc107b923;p=tortuga-home.git . --- diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 99a7706..8c518dd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,18 @@ - + - + + + + + + - + + + @@ -45,47 +52,47 @@ - - + + - - + + - + - - + + - + - - + + - + - - + + - + - - + + @@ -93,47 +100,44 @@ - - + + - + - - + + - + - - - - - + + - - + + - - + + - + - - + + @@ -153,10 +157,6 @@ - hideMaskTimeout - bureau/plasma/on - #mas - $shareroot .music cronSeen 8084 @@ -183,6 +183,10 @@ setex( set_time_limit ssh + matin + tvShowLibra + connectRedis()-> + tmdb. @@ -213,7 +217,6 @@ @@ -402,9 +406,9 @@ - - - @@ -1318,7 +1324,7 @@ - + @@ -1327,9 +1333,9 @@ - + - + @@ -1337,7 +1343,7 @@ - + @@ -1352,9 +1358,9 @@ - + - + @@ -1390,93 +1396,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1553,27 +1472,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -1595,13 +1493,6 @@ - - - - - - - @@ -1651,86 +1542,200 @@ - + + - - + + - + - - + + - - + + + + + + - + - - + + - + - - - - + - + - - + + + + + + + + + - - + + - + - - + + - + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/config/media.php b/config/media.php index df304b5..c3864a3 100644 --- a/config/media.php +++ b/config/media.php @@ -13,27 +13,35 @@ if ($device == 'salon' || $device == 'bureau') { } $tvshows = getTVShows(); -$shortcuts['sub-seriestv'] = $tvshows['shortcuts']; -$shortcuts = array_merge($shortcuts, $tvshows['subs']); -profile('Got TV Shows', __FILE__, __LINE__); +if (is_array($tvshows)) { + $shortcuts['sub-seriestv'] = $tvshows['shortcuts']; + $shortcuts = array_merge($shortcuts, $tvshows['subs']); + profile('Got TV Shows', __FILE__, __LINE__); +} $movies = getMovies(); -$shortcuts['sub-movies'] = $movies['shortcuts']; -$shortcuts = array_merge($shortcuts, $movies['subs']); -profile('Got movies', __FILE__, __LINE__); +if (is_array($movies)) { + $shortcuts['sub-movies'] = $movies['shortcuts']; + $shortcuts = array_merge($shortcuts, $movies['subs']); + profile('Got movies', __FILE__, __LINE__); +} $recent = getMediaRecentAdded(); -$shortcuts['sub-mediarecent'] = $recent['shortcuts']; -$shortcuts = array_merge($shortcuts, $recent['subs']); -profile('Got recents', __FILE__, __LINE__); +if (is_array($recent)) { + $shortcuts['sub-mediarecent'] = $recent['shortcuts']; + $shortcuts = array_merge($shortcuts, $recent['subs']); + profile('Got recents', __FILE__, __LINE__); +} -foreach ($media as $id => $mediagroup) { - if ($id == 'x' && !isset($_GET['x'])) { - continue; +if(is_array($media)) { + foreach ($media as $id => $mediagroup) { + if ($id == 'x' && !isset($_GET['x'])) { + continue; + } + $parentId = 'media-' . $id; + $shortcuts['media'][] = array('label' => $mediagroup['name'], 'type' => 'sub', 'sub' => $parentId); + $shortcuts = array_merge($shortcuts, getMediaFolderShortcuts($mediagroup, $parentId)); } - $parentId = 'media-' . $id; - $shortcuts['media'][] = array('label' => $mediagroup['name'], 'type' => 'sub', 'sub' => $parentId); - $shortcuts = array_merge($shortcuts, getMediaFolderShortcuts($mediagroup, $parentId)); } function getMediaFolderShortcuts($mediagroup, $parentId) diff --git a/config/music.php b/config/music.php index 1776f81..512f04d 100644 --- a/config/music.php +++ b/config/music.php @@ -9,108 +9,110 @@ profile('Got podcasts', __FILE__, __LINE__); function squeezeFavorites($favorites, &$shortcuts) { - foreach ($favorites as $s) { - if ($s['name'] == 'Radios') { - $label = 'Radios'; - $key = 'sub-radio'; - } else if ($s['name'] == 'Playlists') { - $label = 'Musique'; - $key = 'sub-playlists'; - } else if ($s['name'] == 'Ambiances') { - $label = 'Ambiance'; - $key = 'sub-ambiance'; - } else { - continue; - } - squeezeSubitems($s, $key, $label, $shortcuts); - } + if (is_array($favorites)) { + foreach ($favorites as $s) { + if ($s['name'] == 'Radios') { + $label = 'Radios'; + $key = 'sub-radio'; + } else if ($s['name'] == 'Playlists') { + $label = 'Musique'; + $key = 'sub-playlists'; + } else if ($s['name'] == 'Ambiances') { + $label = 'Ambiance'; + $key = 'sub-ambiance'; + } else { + continue; + } + squeezeSubitems($s, $key, $label, $shortcuts); + } + } } function squeezeSubitems($s, $key, $label, &$shortcuts) { - global $allmusics; + global $allmusics; - $t = array(); - foreach ($s['subitems'] as $subitem) { - if ($subitem['hasitems']) { - $id = $key . '-' . md5($subitem['name']); - $t[] = array('label' => $subitem['name'], 'type' => 'sub', 'sub' => $id); - squeezeSubitems($subitem, $id, $subitem['name'], $shortcuts); - continue; - } else { - $e = explode('.', $subitem['id']); - array_shift($e); - $id = $subitem['id']; - $type = 'squeeze'; - $commands = ['favorites playlist play item_id:' . $id, 'playlist repeat 2', 'playlist shuffle 1']; - if ($s['name'] == 'Ambiances') { - $type .= '_ambiance'; - array_unshift($commands, 'sync -'); - } - $a = array('type' => $type, 'commands' => $commands, 'label' => $subitem['name']); + $t = array(); + foreach ($s['subitems'] as $subitem) { + if ($subitem['hasitems']) { + $id = $key . '-' . md5($subitem['name']); + $t[] = array('label' => $subitem['name'], 'type' => 'sub', 'sub' => $id); + squeezeSubitems($subitem, $id, $subitem['name'], $shortcuts); + continue; + } else { + $e = explode('.', $subitem['id']); + array_shift($e); + $id = $subitem['id']; + $type = 'squeeze'; + $commands = ['favorites playlist play item_id:' . $id, 'playlist repeat 2', 'playlist shuffle 1']; + if ($s['name'] == 'Ambiances') { + $type .= '_ambiance'; + array_unshift($commands, 'sync -'); + } + $a = array('type' => $type, 'commands' => $commands, 'label' => $subitem['name']); - $allmusics[$subitem['name']] = $a; - $t[] = $a; - } - } - $shortcuts[$key] = array_merge([['type' => 'back', 'label' => $label]], $t); + $allmusics[$subitem['name']] = $a; + $t[] = $a; + } + } + $shortcuts[$key] = array_merge([['type' => 'back', 'label' => $label]], $t); } function podcastDir($podcasts, $label, $key, &$shortcuts) { - global $allmusics; + global $allmusics; - $k = 'sub-' . $key; - $t = []; + $k = 'sub-' . $key; + $t = []; - $hasDir = false; - if (!is_array($podcasts)) { - return; - } - foreach ($podcasts as $subitem) { - if ($subitem['hasitems']) { - if (count($subitem['subitems']) == 0) { + $hasDir = false; + if (!is_array($podcasts)) { + return; + } + foreach ($podcasts as $subitem) { + if ($subitem['hasitems']) { + if (count($subitem['subitems']) == 0) { - } elseif (count($subitem['subitems']) == 1) { - $t[] = audioElement($subitem['subitems'][0], 'podcasts'); - } else { - $subname = $subitem['name'] ? $subitem['name'] : $subitem['title']; - $subkey = $key . '-' . str_replace('.', '_', $subitem['id']); - $t[] = ['type' => 'sub', 'label' => $subname, 'sub' => $subkey]; - podcastDir($subitem['subitems'], $subname, $subkey, $shortcuts); - $hasDir = true; - } - } else { - $t[] = audioElement($subitem, 'podcasts'); - } - } + } elseif (count($subitem['subitems']) == 1) { + $t[] = audioElement($subitem['subitems'][0], 'podcasts'); + } else { + $subname = $subitem['name'] ? $subitem['name'] : $subitem['title']; + $subkey = $key . '-' . str_replace('.', '_', $subitem['id']); + $t[] = ['type' => 'sub', 'label' => $subname, 'sub' => $subkey]; + podcastDir($subitem['subitems'], $subname, $subkey, $shortcuts); + $hasDir = true; + } + } else { + $t[] = audioElement($subitem, 'podcasts'); + } + } - $shortcuts[$k] = array_merge([['type' => 'back', 'label' => $label]], $t); + $shortcuts[$k] = array_merge([['type' => 'back', 'label' => $label]], $t); } function audioElement($subitem, $app = 'favorites') { - global $allmusics; - $subname = isset($subitem['name']) && $subitem['name'] ? $subitem['name'] : $subitem['title']; - $e = explode('.', $subitem['id']); - array_shift($e); - $id = $subitem['id']; - $a = array('type' => 'squeeze', 'commands' => array($app . ' playlist play item_id:' . $id, 'playlist shuffle 1'), 'label' => $subname); - $allmusics[$subitem['title']] = $a; - return $a; + global $allmusics; + $subname = isset($subitem['name']) && $subitem['name'] ? $subitem['name'] : $subitem['title']; + $e = explode('.', $subitem['id']); + array_shift($e); + $id = $subitem['id']; + $a = array('type' => 'squeeze', 'commands' => array($app . ' playlist play item_id:' . $id, 'playlist shuffle 1'), 'label' => $subname); + $allmusics[$subitem['title']] = $a; + return $a; } $shortcuts['music'] = [ - ['type' => 'volume', 'volume' => '3%', 'increase' => '+', 'label' => ''], - ['type' => 'volume', 'volume' => '3%', 'increase' => '-', 'label' => ''], - ['type' => 'light', 'scene' => 'home/music/synchro/soiree', 'label' => 'Synchro Soirée'], - ['type' => 'sub', 'label' => 'Radios', 'sub' => 'radio'], - ['type' => 'sub', 'label' => 'Musique', 'sub' => 'playlists'], - ['type' => 'sub', 'label' => 'Ambiance', 'sub' => 'ambiance'], + ['type' => 'volume', 'volume' => '3%', 'increase' => '+', 'label' => ''], + ['type' => 'volume', 'volume' => '3%', 'increase' => '-', 'label' => ''], + ['type' => 'light', 'scene' => 'home/music/synchro/soiree', 'label' => 'Synchro Soirée'], + ['type' => 'sub', 'label' => 'Radios', 'sub' => 'radio'], + ['type' => 'sub', 'label' => 'Musique', 'sub' => 'playlists'], + ['type' => 'sub', 'label' => 'Ambiance', 'sub' => 'ambiance'], ]; if ($podcasts !== false) { - $shortcuts['music'][] = ['type' => 'sub', 'label' => 'Podcasts', 'sub' => 'podcasts']; + $shortcuts['music'][] = ['type' => 'sub', 'label' => 'Podcasts', 'sub' => 'podcasts']; } \ No newline at end of file diff --git a/scripts/cron/cron.php b/scripts/cron/cron.php index 708fdb5..a8e04c7 100644 --- a/scripts/cron/cron.php +++ b/scripts/cron/cron.php @@ -110,7 +110,6 @@ function cronMediaLibrary($cronmin) $media = array(); $shareroot = '/volume1/Share'; - $tmdbclient = getTmbdClient(); foreach ($directories as $name => $directory) { $dir = $shareroot . '/Videos/' . $directory['dir']; diff --git a/scripts/lib/domoticz.php b/scripts/lib/domoticz.php index 24269a2..c1bb989 100644 --- a/scripts/lib/domoticz.php +++ b/scripts/lib/domoticz.php @@ -16,14 +16,16 @@ function domoticzCmd($command, $priority = false) { $redis = connectRedis(); $queue_name = 'domoticz_queue'; - if ($priority) { - $queue_name .= '_priority'; - } + $queue = $redis->get($queue_name); if (!is_array($queue)) { $queue = []; } - $queue[] = $command; + if($priority){ + array_unshift($queue,$command); + }else{ + array_push($queue,$command); + } $redis->igbset($queue_name, $queue); $redis->publish('domoticz_event', 'handle_queue'); } \ No newline at end of file diff --git a/scripts/lib/harmony.php b/scripts/lib/harmony.php index f60c577..fcfa452 100644 --- a/scripts/lib/harmony.php +++ b/scripts/lib/harmony.php @@ -5,7 +5,7 @@ function harmonyRequest($uri, $method = 'GET', $hub = 'salon') { global $harmonyClient; - $ip = '192.168.1.' . (getState('device_salon_awake') ? '66' : '69'); + $ip = '192.168.1.66'; $uri = 'http://' . $ip . ':8282/hubs/' . $hub . '/' . ltrim($uri, '/'); diff --git a/scripts/lib/insteon.php b/scripts/lib/insteon.php index 654d1fb..5946c65 100644 --- a/scripts/lib/insteon.php +++ b/scripts/lib/insteon.php @@ -5,80 +5,81 @@ $insteonAuth = array('t1Kx8wuNWG', 'NA4Hq3v9cM'); $insteonCommandTimeout = 15; $insteonHasKnocked = false; -function insteonKnock() +function insteonCommand($command) { - global $insteonHasKnocked; - $insteonHasKnocked = true; - __insteonCommand('1cjdW20oprxzHDZ.htm'); + $redis = connectRedis(); + $queue = $redis->igbget('insteon_queue'); + if (!is_array($queue)) { + $queue = []; + } + $queue[] = $command; + $redis->igbset('insteon_queue', $queue); + $redis->publish('insteon_event', 'handle_queue'); } -function runInsteonCommand($command, $attemps = 3) +function runInsteonCommand($command, $attempts = 3) { - if ($attemps <= 0) { - return; - } + if ($attempts <= 0) { + return; + } - insteonClearBuffer(); - echo "\n" . '-- Command ' . $command . ' --' . "\n"; - echo "command : " . _insteonCommand($command) . "\n"; - for ($i = 1; $i <= 5; $i++) { - usleep(1000000 * 0.1); - $buff = insteonBuffer(); - echo $buff . "\n"; - if (stristr($buff, '0250') || stristr($buff, '0258')) { - insteonClearBuffer(); - return; - } - } - runInsteonCommand($command, $attemps - 1); + insteonClearBuffer(); + echo "\n" . '-- Command ' . $command . ' attempt #' . $attempts . ' --' . "\n"; + echo "command : " . _insteonCommand($command) . "\n"; + for ($i = 1; $i <= 5; $i++) { + usleep(1000000 * 0.1); + $buff = insteonBuffer(); + echo $buff . "\n"; + if (stristr($buff, '0250') || stristr($buff, '0258')) { + insteonClearBuffer(); + return; + } + } + runInsteonCommand($command, $attempts - 1); } -function insteonCommand($command) +function _insteonCommand($command) { - $redis = connectRedis(); - $queue = $redis->get('insteon_queue'); - if (!is_array($queue)) { - $queue = []; - } - $queue[] = $command; - $redis->igbset('insteon_queue', $queue); - $redis->publish('insteon_event','handle_queue'); + global $insteonHasKnocked; + if (!$insteonHasKnocked) { + insteonKnock(); + } + return __insteonCommand($command); } -function _insteonCommand($command) +function insteonKnock() { - global $insteonHasKnocked; - if (!$insteonHasKnocked) { - insteonKnock(); - } - return __insteonCommand($command); + global $insteonHasKnocked; + $insteonHasKnocked = true; + __insteonCommand('1cjdW20oprxzHDZ.htm'); } + function __insteonCommand($command) { - global $insteonBaseURL, $insteonAuth, $insteonCommandTimeout; - return getUrlContent($insteonBaseURL . '/' . $command, $insteonCommandTimeout, $insteonAuth); + global $insteonBaseURL, $insteonAuth, $insteonCommandTimeout; + return getUrlContent($insteonBaseURL . '/' . $command, $insteonCommandTimeout, $insteonAuth); } function insteonClearBuffer() { - return _insteonCommand('1?XB=M=1'); + return _insteonCommand('1?XB=M=1'); } function _insteonBufferEmpty() { - return trim(insteonBuffer(), '0') === ''; + return trim(insteonBuffer(), '0') === ''; } function insteonBuffer() { - global $insteonHasKnocked; - $x = _insteonCommand('buffstatus.xml'); - if ($x !== false && is_object($x)) { - $xml = simplexml_load_string($x); - return (string)($xml->BS); - } else { - $insteonHasKnocked = false; - return ''; - } + global $insteonHasKnocked; + $x = _insteonCommand('buffstatus.xml'); + if ($x !== false && is_object($x)) { + $xml = simplexml_load_string($x); + return (string)($xml->BS); + } else { + $insteonHasKnocked = false; + return ''; + } } \ No newline at end of file diff --git a/scripts/lib/redis.php b/scripts/lib/redis.php index a5540a9..f145544 100644 --- a/scripts/lib/redis.php +++ b/scripts/lib/redis.php @@ -1,8 +1,6 @@ attachCallback($channel, $events = new class($callback) + $events = new class($callback) { protected $_callback; + protected $_running = false; public function __construct($callback) { @@ -86,16 +85,24 @@ function redisEventListener($channel, $callback) public function __invoke($payload) { if ($payload == 'handle_queue') { + usleep(0.1 * 1000000); + if ($this->_running) { + return; + } + $this->_running = true; call_user_func($this->_callback); + $this->_running = false; } } - } - ); + }; + + $dispatcher->attachCallback($channel, $events); + try { - echo 'Run dispatcher'; + echo 'Run dispatcher' . "\n"; $dispatcher->run(); } catch (Exception $e) { - echo 'Error while running dispatcher : ' . $e->getMessage(); + echo 'Error while running dispatcher : ' . $e->getMessage() . "\n"; redisEventListener($channel, $callback); } } diff --git a/scripts/lib/ssh.php b/scripts/lib/ssh.php index 04b064c..6c6c9ce 100644 --- a/scripts/lib/ssh.php +++ b/scripts/lib/ssh.php @@ -35,14 +35,15 @@ function getSSHInstance($device = null, $sudo = false) function sshCommand($command, $device = null, $blocking = true, $sudo = false) { - $redis = connectRedis(); - $queue = $redis->get('ssh_queue'); - if (!is_array($queue)) { - $queue = []; - } - $queue[] = ['command' => $command, 'device' => $device, 'blocking' => $blocking, 'sudo' => $sudo]; - $redis->igbset('ssh_queue', $queue); - $redis->publish('ssh_event', 'handle_queue'); + return sshRunCommand($command, $device, $blocking, $sudo); +// $redis = connectRedis(); +// $queue = $redis->get('ssh_queue'); +// if (!is_array($queue)) { +// $queue = []; +// } +// $queue[] = ['command' => $command, 'device' => $device, 'blocking' => $blocking, 'sudo' => $sudo]; +// $redis->igbset('ssh_queue', $queue); +// $redis->publish('ssh_event', 'handle_queue'); } function sshRunCommand($command, $device = null, $blocking = true, $sudo = false) diff --git a/scripts/lib/tmdb.php b/scripts/lib/tmdb.php index 1e3edff..fe51e37 100644 --- a/scripts/lib/tmdb.php +++ b/scripts/lib/tmdb.php @@ -62,14 +62,13 @@ function getMediaRecentAdded() return _getMediaRecentAdded(); } -function _getTVShows($force = false) +function _getTVShows($force = false, $forceapi = false) { if ($force) { global $directories; - $library = new tvShowLibrary($directories); + $library = new tvShowLibrary($directories, $forceapi); $res = $library->getShortcuts(); connectRedis()->igbset('medialibrary.tvshows', $res); - return $res; } $res = connectRedis()->igbget('medialibrary.tvshows'); @@ -84,11 +83,11 @@ function getMovies() return _getMovies(); } -function _getMovies($force = false) +function _getMovies($force = false, $forceapi = false) { if ($force) { global $directories; - $library = new moviesLibrary($directories); + $library = new moviesLibrary($directories, $force); $res = $library->getShortcuts(); connectRedis()->igbset('medialibrary.movies', $res); return $res; @@ -112,9 +111,11 @@ class tvShowLibrary extends mediaLibrary { protected $tvShows = []; + protected $force = false; - public function __construct($directories) + public function __construct($directories, $force = false) { + $this->force = $force; foreach ($directories as $directory) { if ($directory['context'] != 'tvshow') { continue; @@ -140,19 +141,30 @@ class tvShowLibrary extends mediaLibrary public function getTVShowByID($id) { - $res = connectRedis()->igbget('tmdb.tv.' . $id); + if (!$this->force) { + try { + $res = connectRedis()->igbget('tmdb.tv.' . $id); + } catch (Exception $e) { + $res = false; + } + } else { + $res = false; + } + if ($res === false) { $client = getTmbdClient(); try { $tv = $client->getTvApi()->getTvshow($id, ['language' => 'fr-FR']); - connectRedis()->igbset('tmdb.tv.' . $id, $tv, ['EX' => 86400]); + connectRedis()->igbsetex('tmdb.tv.' . $id, 86400, $tv); return $tv; } catch (Exception $e) { + echo $e->getMessage(); return null; } } + return $res; } @@ -313,10 +325,12 @@ class mediaLibrary class moviesLibrary extends mediaLibrary { protected $movies = []; + protected $force = false; - - public function __construct($directories) + public function __construct($directories, $force = false) { + $this->force = $force; + foreach ($directories as $directory) { if ($directory['context'] != 'movie') { continue; @@ -439,6 +453,7 @@ class moviesLibrary extends mediaLibrary $genres = []; if (is_array($this->movies)) { foreach ($this->movies as $movie) { + foreach ($movie['data']['genres'] as $genre) { if (!isset($genres[$genre['name']])) { $genres[$genre['name']] = []; @@ -488,7 +503,11 @@ class moviesLibrary extends mediaLibrary public function getMovieByID($id) { - $res = connectRedis()->igbget('tmdb.movie.' . $id); + if (!$this->force) { + $res = connectRedis()->igbget('tmdb.movie.' . $id); + } else { + $res = false; + } if ($res === false) { $client = getTmbdClient(); try { diff --git a/scripts/tmdb.php b/scripts/tmdb.php new file mode 100644 index 0000000..d0d8edf --- /dev/null +++ b/scripts/tmdb.php @@ -0,0 +1,6 @@ +igbget($q); if (!is_array($queue)) { $queue = []; $redis->igbset($q, $queue); } if (!count($queue)) { - continue; + echo 'End processing queue' . "\n"; + break; } $command = array_shift($queue); + $redis->igbset($q, $queue); echo 'Run command ' . json_encode($command) . "\n"; runDomoticzCommand($command); - doneCommand($q, $command); usleep(1000000 * 1.5); } -} - -function doneCommand($q, $command) -{ - global $redis; - $queue = $redis->igbget($q); - $res = []; - foreach ($queue as $item) { - if ($item == $command) { - continue; - } - $res[] = $item; - } - $redis->igbset($q, $res); - echo 'Done command (' . $q . ') : ' . json_encode($command) . "\n"; } \ No newline at end of file diff --git a/servers/insteon.php b/servers/insteon.php index 39f2533..ed01b5a 100644 --- a/servers/insteon.php +++ b/servers/insteon.php @@ -11,40 +11,31 @@ echo 'Run home server insteon' . "\n"; $redis = connectRedis(); -redisEventListener('insteon_event','handleInsteonQueue'); +redisEventListener('insteon_event', 'handleInsteonQueue'); function handleInsteonQueue() { global $redis; - $queue = $redis->igbget('insteon_queue'); - if (!is_array($queue)) { - $queue = []; - $redis->igbset('insteon_queue', $queue); - } - if (!count($queue)) { - if (rand(1, 25) == 3) { - insteonKnock(); + + while (true) { + $queue = $redis->igbget('insteon_queue'); + if (!is_array($queue)) { + $queue = []; + $redis->igbset('insteon_queue', $queue); } - return 0.1; - } - $command = array_shift($queue); - echo 'Run command ' . $command . "\n"; - runInsteonCommand($command); - doneCommand($command); - usleep(1000000 * 0.1); -} - -function doneCommand($command) -{ - global $redis; - $queue = $redis->igbget('insteon_queue'); - $res = []; - foreach ($queue as $item) { - if ($item == $command) { - continue; + + if (!count($queue)) { + echo 'Queue empty ' . "\n"; + break; } - $res[] = $item; + + echo 'Handle Queue : current state ' . json_encode($queue) . "\n"; + + $command = array_shift($queue); + $redis->igbset('insteon_queue', $queue); + echo 'Run command ' . $command . "\n"; + runInsteonCommand($command); + usleep(1000000 * 0.5); } - $redis->igbset('insteon_queue', $res); - echo 'Done command : ' . $command . "\n"; + echo 'End of handling queue'."\n"; } \ No newline at end of file diff --git a/servers/ssh.php b/servers/ssh.php index 09c14cc..9ed18a4 100644 --- a/servers/ssh.php +++ b/servers/ssh.php @@ -10,26 +10,29 @@ ignore_user_abort(true); echo 'Run home server ssh' . "\n"; $redis = connectRedis(); -redisEventListener('ssh_event', 'handleSSHQueues'); +redisEventListener('ssh_event', 'handleSSHQueue'); $connections = []; -function handleSSHQueues() +function handleSSHQueue() { global $redis; $queue_name = 'ssh_queue'; - $queue = $redis->igbget($queue_name); - if (!is_array($queue)) { - $queue = []; + while (true) { + $queue = $redis->igbget($queue_name); + if (!is_array($queue)) { + $queue = []; + $redis->igbset($queue_name, $queue); + } + + if (!count($queue)) { + echo 'End processing queue' . "\n"; + break; + } + + $command = array_shift($queue); $redis->igbset($queue_name, $queue); + echo 'Run command ' . json_encode($command) . "\n"; + sshRunCommand($command['command'], $command['device'], $command['blocking'], $command['sudo']); } - - if (!count($queue)) { - return; - } - - $command = array_shift($queue); - $redis->igbset($queue_name, $queue); - echo 'Run command ' . json_encode($command) . "\n"; - sshRunCommand($command['command'], $command['device'], $command['blocking'], $command['sudo']); } \ No newline at end of file