/** * Run it! */ public function run() { $player = $this->plex->getPlayer(); if (!empty($player)) { WS::log()->debug('Current player - ' . $player['title'] . ':' . $player['state']); $this->lastPlayer = $player; switch ($player['state']) { case 'playing': $this->wemo->off($player['title']); break; case 'paused': $this->wemo->dim($player['title'], WS::config()->get('dim_on_pause', 40)); break; default: $this->wemo->on($player['title']); break; } } elseif (!empty($this->lastPlayer)) { $this->wemo->on($this->lastPlayer['title']); } WS::log()->debug("Running... [" . $this->wemo->getStatus() . "]"); }
/** * InternalException constructor. * * @param string $msg */ public function __construct($msg) { WS::log()->error($msg); return parent::__construct($msg, 500); }
/** * Dim (dimmable) wemo device(s) for a player * * @param string $player * @param int $percent Dim level (0-100%) * * @return bool * @throws \a15lam\Exceptions\WemoException */ public function dim($player, $percent = 40) { if ($this->played === true) { $wemo = $this->getMapByPlayer($player); WS::log()->info('Lights turning on. Movie paused, take a quick break!'); /** @type WemoInterface $device */ foreach ($wemo as $device) { if ($device->isDimmable()) { WS::log()->debug('[dim] Device dimmable. Dimming to ' . $percent . '%'); $device->dim($percent); } else { WS::log()->debug('[dim] Device not dimmable. Turning on.'); $device->On(); } } $this->played = false; } else { return false; } }
/** * Gets player (client) device name. * * @return string|null * @throws \a15lam\Exceptions\PlexException */ public function getPlayer() { $status = $this->getStatus(); if (is_array($status)) { $info = isset($status['MediaContainer']) ? isset($status['MediaContainer']['Video']) ? isset($status['MediaContainer']['Video']['Player_attr']) ? $status['MediaContainer']['Video']['Player_attr'] : null : null : null; if (!empty($info)) { return $info; } } WS::log()->debug("No status returned from Plex server. Probably no media is playing."); return null; }
<?php require __DIR__ . '/vendor/autoload.php'; date_default_timezone_set(\a15lam\WemoPlex\Workspace::config()->get('timezone')); $_int = 2; //2 seconds $watcher = new \a15lam\WemoPlex\Watcher(); while (true) { if (\a15lam\WemoPlex\Workspace::config()->get('on_time', false)) { $startTime = strtotime(\a15lam\WemoPlex\Workspace::config()->get('start_time', '06:00 PM')); $endTime = strtotime(\a15lam\WemoPlex\Workspace::config()->get('end_time', '06:00 AM NEXT DAY')); if (time() >= $startTime && time() <= $endTime) { $watcher->run(); } } else { $watcher->run(); } sleep($_int); }