public function waitForMpd() { $this->jobPhase = 1; $recursionInterval = 3; // seconds $mpd = new \Slimpd\modules\mpd\mpd(); $status = $mpd->cmd('status'); if (isset($status['updating_db'])) { if ($this->waitingLoop === 0) { $this->waitingLoop = time(); // fake total items with total seconds $this->itemCountTotal = (int) $this->maxWaitingTime; $this->beginJob(array(), __FUNCTION__); } if (time() - $this->waitingLoop > $this->maxWaitingTime) { cliLog('max waiting time (' . $this->maxWaitingTime . ' sec) for mpd reached. exiting now...', 1, 'red', TRUE); $this->finishJob(NULL, __FUNCTION__); \Slim\Slim::getInstance()->stop(); } $this->itemCountProcessed = time() - $this->waitingLoop; $this->itemCountChecked = time() - $this->waitingLoop; $this->updateJob(array(), __FUNCTION__); //cliLog('waiting '. (time()-$this->waitingLoop - $this->maxWaitingTime)*-1 .' sec. until mpd\'s internal database-update has finished'); sleep($recursionInterval); // recursion return $this->waitForMpd(); } if ($this->waitingLoop > 0) { $this->itemCountProcessed = $this->itemCountTotal; cliLog('mpd seems to be ready. continuing...', 1, 'green'); $this->finishJob(NULL, __FUNCTION__); } return; }
$mpd = new \Slimpd\modules\mpd\mpd(); # TODO: mpd-version check, v 0.20 has 'duration' included in status() # @see: http://www.musicpd.org/doc/protocol/command_reference.html#status_commands $config['mpd']['status'] = $mpd->cmd('status'); $config['mpd']['status']['duration'] = $mpd->cmd('currentsong')['Time']; $percent = $config['mpd']['status']['elapsed'] / ($config['mpd']['status']['duration'] / 100); $config['mpd']['status']['percent'] = $percent >= 0 && $percent <= 100 ? $percent : 0; echo json_encode($config['mpd']['status']); $app->stop(); }); foreach (['mpdplayer', 'localplayer', 'widget-trackcontrol'] as $markupSnippet) { $app->get('/markup/' . $markupSnippet, function () use($app, $config, $markupSnippet) { // maybe we cant find item in mpd or mysql database because it has ben accessed via filebrowser $itemRelativePath = ''; if ($markupSnippet === 'mpdplayer') { $mpd = new \Slimpd\modules\mpd\mpd(); $config['item'] = $mpd->getCurrentlyPlayedTrack(); } else { if (is_numeric($app->request->get('item')) === TRUE) { $search = array('id' => (int) $app->request->get('item')); } else { $search = array('relativePathHash' => getFilePathHash($app->request->get('item'))); $itemRelativePath = $app->request->get('item'); } $config['item'] = \Slimpd\Track::getInstanceByAttributes($search); } if (is_null($config['item']) === FALSE && $config['item']->getId() > 0) { $config['renderitems'] = array('genres' => \Slimpd\Genre::getInstancesForRendering($config['item']), 'labels' => \Slimpd\Label::getInstancesForRendering($config['item']), 'artists' => \Slimpd\Artist::getInstancesForRendering($config['item']), 'albums' => \Slimpd\Album::getInstancesForRendering($config['item'])); $itemRelativePath = $config['item']->getRelativePath(); } else { // playing track has not been imported in slimpd database yet...