From 89a57cef91b6a424fdcd51822b3c0893f19dd259 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 10 Feb 2021 20:40:05 +0100 Subject: [PATCH] . --- .idea/workspace.xml | 30 +++++---- config/rooms.php | 6 +- scripts/denon.php | 14 ++++- scripts/lib/denon.php | 137 +++++++++++++++++++++++++++++++++++------ scripts/lib/scenes.php | 8 ++- 5 files changed, 152 insertions(+), 43 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3664d14..b9865f0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,11 @@ - - - - - + + - - + diff --git a/config/rooms.php b/config/rooms.php index c7829d0..37415e4 100644 --- a/config/rooms.php +++ b/config/rooms.php @@ -66,9 +66,9 @@ function salon() ['type' => 'light', 'scene' => 'salon/lecturenocture', 'label' => 'Lecture nocturne'], ['type' => 'light', 'scene' => 'salon/cheminee', 'label' => 'Cheminée'], ]; - $all = array_merge($favorites, [ - - ]); + $all = array_merge($favorites, [['type' => 'separator', 'label' => 'Projecteur'], + ['type' => 'light', 'scene' => 'salon/projector/on', 'label' => 'Allumer'], + ['type' => 'light', 'scene' => 'salon/projector/off', 'label' => 'éteindre']]); return array('all' => $all, 'favorites' => $favorites); } diff --git a/scripts/denon.php b/scripts/denon.php index 4cc5667..bd19c73 100644 --- a/scripts/denon.php +++ b/scripts/denon.php @@ -1,9 +1,17 @@ ['method' => 'GET'], 'ssl' => ['verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true]]; - $context = stream_context_create($context); - return file_get_contents('https://192.168.13.42:10443/ajax/globals/' . $command . '&_' . time(), false, $context); -} - function denonAVRVolume($volume, $increase = '') { - $device = 2894; - $current = domoticzGetLevel($device); + $current = denonAVRGetVolume(); if ($increase === '+' || $increase === '-') { if ($increase === '+') { $volume += $current; @@ -27,39 +21,142 @@ function denonAVRVolume($volume, $increase = '') $volume = $current - $volume; } } - $volume = min(80, $volume); - domoticzSetLevel($device, $volume, true); + $volume = min(80, max(0, round($volume))); + if ($volume !== $current) { + echo _denonTelnet('MV' . $volume); + } return $current . '->' . $volume; } function denonAVROn($input = 'Media Player') { - _denonAVR('set_config?type=4&data=' . rawurlencode('1')); + if (!denonAVRGetPowerState()) { + _denonTelnet('ZMON'); + } denonAVRInput($input); } function denonAVRInput($input) { - $map = ['Media Player' => 5, 'CD' => 9, 'HEOS Music' => 13, 'AUX' => 7]; - $volumes = ['Media Player' => 59, 'HEOS Music' => 50, 'CD' => 30, 'AUX' => 40]; + $map = ['Media Player' => 'MPLAY', 'CBL/SAT' => 'SAT/CBL', 'Blu-Ray' => 'BD', 'AUX' => 'AUX1', 'HEOS Music' => 'NET', 'TV Audio' => 'TV']; if (isset($map[$input])) { - _denonAVR('set_config?type=7&data=' . rawurlencode('')); + $cmd = $map[$input]; + } else { + $cmd = $input; + } + $cmd = 'SI' . strtoupper($cmd); + if (denonAVRGetInput() !== $cmd) { + _denonTelnet($cmd); sleep(1); } + + $audioMode = ['Media Player' => 'MOVIE', 'AUX' => 'MUSIC', 'CD' => 'MUSIC']; + if (isset($audioMode[$input])) { + denonAVRSetAudioMode($audioMode[$input]); + usleep(1000000 * 0.5); + } + + $volumes = ['Media Player' => 60, 'HEOS Music' => 50, 'CD' => 30, 'AUX' => 26]; if (isset($volumes[$input])) { - denonAVRVolume($volumes[$input]); - sleep(1); - denonAVRVolume(1, '+'); + denonAVRVolume($volumes[$input], ''); + usleep(1000000 * 0.5); + } + + denonAVRSetSpeakerPreset(1); +} + +function denonAVRGetVolume() +{ + $res = _denonTelnet('MV?'); + $res = substr($res, 2); + if ($res > 100) { + $res /= 10; + } + return round($res); +} + +function denonAVRSetAudioMode($mode) +{ + $cmd = 'MS' . strtoupper($mode); + if ($cmd === denonAVRGetAudioMode()) { + return; + } + $res = _denonTelnet($cmd); + sleep(1); + return $res; +} + +function denonAVRSetSpeakerPreset($preset) +{ + $current = denonAVRGetSpeakerPreset(); + if ($preset == $current) { + return; } + _denonTelnet('SPPR ' . $preset); +} + +function denonAVRGetSpeakerPreset() +{ + $res = _denonTelnet('SPPR ?'); + $e = explode(' ', $res); + return $e[1]; +} + +function denonAVRGetAudioMode() +{ + return _denonTelnet('MS?'); +} + +function denonAVRGetInput() +{ + return _denonTelnet('SI?'); +} + +function denonAVRGetPowerState() +{ + return _denonTelnet('ZM?') === 'ZMON'; +} + +function _denonTelnet($cmd) +{ + $client = getDenonTelnetClient(); + $res = $client->execute($cmd); + if ($res->isError()) { + return false; + } + return trim($res->getResponseText()); } function denonAVROff() { - _denonAVR('set_config?type=4&data=' . rawurlencode('3')); + _denonTelnet('ZMOFF'); + _denonTelnet('Z2OFF'); + _denonTelnet('PWSTANDBY'); } -function epson($on = true) +function epson($on = true, $force = false) { + $currentState = getState('EpsonPowerState', '0') === '1'; + if (!$force && $currentState === $on) { + return; + } $cmd = $on ? 'PowerOn' : 'PowerOff'; sshCommand('/usr/local/bin/ir Epson ' . $cmd, 'salon'); + setState('EpsonPowerState', $on ? '1' : '0'); + if ($currentState !== $on && $on) { + sleep(15); + } +} + +/** + * @return \Graze\TelnetClient\TelnetClient + */ +function getDenonTelnetClient() +{ + global $denonTelnetClient; + if (!isset($denonTelnetClient)) { + $denonTelnetClient = Graze\TelnetClient\TelnetClient::factory(); + $denonTelnetClient->connect('192.168.13.42:23', '', '', "\r", 2); + } + return $denonTelnetClient; } \ No newline at end of file diff --git a/scripts/lib/scenes.php b/scripts/lib/scenes.php index 6f0e96f..e6eb859 100644 --- a/scripts/lib/scenes.php +++ b/scripts/lib/scenes.php @@ -334,6 +334,12 @@ $scenes = [ 'home/music/synchro/soiree' => [ ['type' => 'function', 'function' => 'synchroSoiree'], ], + 'salon/projector/on' => [ + ['type' => 'function', 'function' => 'epson', 'args' => [true, true]], + ], + 'salon/projector/off' => [ + ['type' => 'function', 'function' => 'epson', 'args' => [false, true]], + ], 'salon/on' => [ ['type' => 'hue', 'group' => $salon, 'scene' => 'Gte8sl76rzAD1wO', 'repeat' => 2], //['type' => 'insteon', 'command' => '0?113=I=0=0'], @@ -545,7 +551,7 @@ $scenes = [ ['type' => 'scene', 'scene' => 'bureau/screen/up'], ['type' => 'scene', 'scene' => 'bureau/rideaux/open'], ['type' => 'scene', 'scene' => 'bureau/auto'], - ['type' => 'ir', 'room' => 'bureausun', 'device' => 'Projector', 'command' => 'PowerOff'], +// ['type' => 'ir', 'room' => 'bureausun', 'device' => 'Projector', 'command' => 'PowerOff'], ['type' => 'scene', 'scene' => 'bureau/sound/off'], ], 'bureau/screen/down' => [ -- 2.39.5