function main() { $config = $GLOBALS['config']; $vera = new Vera($config); $plex = new Plex($config); $state = 'stopped'; $pauseTime = 0; while (1) { $nowPlaying = $plex->get('status/sessions'); $isPlaying = false; if ($nowPlaying['size'] == 1) { $client = $nowPlaying->Video; //if ($client->Player['machineIdentifier'] == '') { // exit("No [machineIdentifier] available. Exiting..."); //} if ($client->Player['machineIdentifier'] == $config['plex']['machineIdentifier']) { if ($client->Player['state'] == 'playing') { $pauseTime = 0; $isPlaying = true; if ($state != 'playing') { $state = 'playing'; //$logger.info("Device: {$client->Player['title']}. Video has started. Dimming lights."); print "Device: {$client->Player['title']}. Video has started. Dimming lights.\n"; $vera->transition($state); } } else { if ($client->Player['state'] == 'paused') { if ($pauseTime == 0) { $pauseTime = time(); } if (time() - $pauseTime > 1) { $isPlaying = true; if ($state != 'paused') { $state = 'paused'; //$logger.info("Device: {$client->Player['title']}. Video is paused. Restoring lights."); print "Device: {$client->Player['title']}. Video is paused. Restoring lights.\n"; $vera->transition($state); } } } } } } else { if ($nowPlaying['size'] >= 2) { foreach ($nowPlaying as $client) { if ($client->Player['machineIdentifier'] == $config['plex']['machineIdentifier']) { if ($client->Player['state'] == 'playing') { $pauseTime = 0; $isPlaying = true; if ($state != 'playing') { $state = 'playing'; //$logger.info("Device: {$client->Player['title']}. Video has started. Dimming lights."); print "Device: {$client->Player['title']}. Video has started. Dimming lights.\n"; $vera->transition($state); } } else { if ($client->Player['state'] == 'paused') { if ($pauseTime == 0) { $pauseTime = time(); } if (time() - $pauseTime > 1) { $isPlaying = true; if ($state != 'paused') { $state = 'paused'; //$logger.info("Device: {$client->Player['title']}. Video is paused. Restoring lights."); print "Device: {$client->Player['title']}. Video is paused. Restoring lights.\n"; $vera->transition($state); } } } } } } } else { if ($isPlaying == false && $state != 'stopped') { $state = 'stopped'; $isPlaying = false; //$logger.info("Device: {$client->Player['title']}. Video is stopped. Turning lights back on.") print "Device: {$client->Player['title']}. Video is stopped. Turning lights back on.\n"; $vera->transition($state); } } } // sleep for 1.0 seconds usleep(1000000); } }