Plex if player at IP is status, then tell it to pause, play or select

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Plex if player at IP is status, then tell it to pause, play or select

Post by ben53252642 »

Simple script I wrote for use with my Aeotec Wallmote switch which only sends an "On" signal to Domoticz.

Domoticz Switch if turned on:

If Plex is playing = pause
if Plex is paused = play
if Plex is stopped = press select

This gives me 3 control functions using just a single button on my Wallmote.

Call the php script from a Lua device script:

lua script

Code: Select all

commandArray = {}
if (devicechanged['Wallmote Plex'] == 'On') then
os.execute("php -f /root/scripts/plex/control.php &")
end
return commandArray
control.php

Code: Select all

<?php

// Configuration
$plexserver = "192.168.0.4";
$plexclient = "192.168.0.38";

//Disable PHP error reporting
error_reporting(0);

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_URL => "https://$plexserver:32400/status/sessions",
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$resp = curl_exec($curl);
curl_close($curl);
$arr = json_decode($resp);

// Navigate the JSON to get the data
foreach($arr as $key=>$value){
foreach($value as $key=>$value){
foreach($value as $key=>$value){
$playerip = ($arr->MediaContainer->Video[$key]->Player->address);
$playerstatus = ($arr->MediaContainer->Video[$key]->Player->state);
};
};
};

// If Target Player IP present
if ($playerip == "$plexclient") {
// Check if its Playing
if ($playerstatus == 'playing') {
echo "Pausing player: $plexclient\n";
$command = "playback/pause";
} elseif ($playerstatus == 'paused') {
echo "Playing player: $plexclient\n";
$command = "playback/play";
};
} else {
echo "Pressing select on player: $plexclient\n";
$command = "navigation/select";
};

// Send command to the player
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_URL => "https://$plexserver:32400/system/players/$plexclient/$command",
));
curl_exec($curl);
curl_close($curl);

?>
Notes:
1) The php curl command is set to allow unsigned SSL certificates.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest