From 3bab75272a96326a06e3a3afb5cb344646702912 Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 14 Nov 2020 15:15:31 +0100 Subject: [PATCH] . --- .idea/workspace.xml | 37 +++++++----- scripts/lib/lib.php | 1 + scripts/lib/off.php | 3 +- scripts/lib/proc.php | 131 +++++++++++++++++++++++++++++++++++++++++ scripts/lib/scenes.php | 8 +++ scripts/lib/shield.php | 19 +++++- servers/logcat.php | 28 +++++++++ servers/logcatb.php | 4 ++ servers/logcats.php | 4 ++ 9 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 scripts/lib/proc.php create mode 100644 servers/logcat.php create mode 100644 servers/logcatb.php create mode 100644 servers/logcats.php diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6d39c3c..99ed3d4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,15 @@ + + + + + + + @@ -1287,22 +1296,22 @@ - - + + - + - + - + diff --git a/scripts/lib/lib.php b/scripts/lib/lib.php index dcf3b14..1232d51 100644 --- a/scripts/lib/lib.php +++ b/scripts/lib/lib.php @@ -43,6 +43,7 @@ require_once ROOT . '/scripts/lib/ifttt.php'; require_once ROOT . '/scripts/lib/router.php'; require_once ROOT . '/scripts/lib/flowerpower.php'; require_once ROOT . '/scripts/lib/denon.php'; +require_once ROOT . '/scripts/lib/proc.php'; require_once ROOT . '/scripts/lib/shield.php'; diff --git a/scripts/lib/off.php b/scripts/lib/off.php index fa905ba..dbae0fc 100644 --- a/scripts/lib/off.php +++ b/scripts/lib/off.php @@ -33,8 +33,7 @@ function offSalon() function offBureau() { - domoticzSwitch(1, false); - irsend('bureau', 'Projector', 'PowerOff'); + execScene('bureau/media/stop'); } function offSdb() diff --git a/scripts/lib/proc.php b/scripts/lib/proc.php new file mode 100644 index 0000000..733eacd --- /dev/null +++ b/scripts/lib/proc.php @@ -0,0 +1,131 @@ +open($process); + } + } + + /** + * create the internal pipes for reading and writing to the opened process + */ + public function open($process) + { + $descriptors = array( + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ); + $this->_proc_resource = proc_open($process, $descriptors, $pipes); + if (!is_resource($this->_proc_resource)) { + throw new \exception('proc_open() failed to create a resource'); + } + $this->_pipes = $pipes; + return $this; + } + + /** + * close the opened pipes+process and return the return code of that process + */ + public function close() + { + fclose($this->_pipes[0]); + fclose($this->_pipes[1]); + $return_code = proc_close($this->_proc_resource); + return $return_code; + } + + /** + * set the rules, and assoc array where the keys are strings + * of 'expected text' and the values are closures to be executed + * once the expected string has been matched + */ + public function on(array $cases) + { + $this->_cases = $cases; + return $this; + } + + /** + * write to the opened processes input stream + */ + public function write($text) + { + fwrite($this->_pipes[0], $text); + return $this; + } + + /** + * write() + newline + */ + public function writeln($text) + { + return $this->write($text . PHP_EOL); + } + + /** + * run the script that was previously open()d and apply the expectation rules + */ + public function run() + { + $buffer = ''; + while ($block = stream_get_contents($this->_pipes[1], 20)) { + $e = explode("\n", $block); + $buffer .= $e[0]; + $linesToParse = []; + if (count($e) > 1) { + for ($i = 1; $i < count($e); $i++) { + $linesToParse[] = $buffer; + $buffer = $e[$i]; + } + } + foreach ($linesToParse as $line) { + $this->_parseLine($line); + } + } + } + + protected function _parseLine($line) + { + if ($this->tooLate($line)) { + return; + } + + foreach ($this->_cases as $expected_text => $closure) { + if (strpos($line, $expected_text)) { + $closure(); + } + } + } + + protected function tooLate($line) + { + // Time 11-14 14:31:35.586 + $month = (int)substr($line, 0, 2); + $day = (int)substr($line, 3, 2); + $hour = (int)substr($line, 6, 2); + $min = (int)substr($line, 9, 2); + $sec = (int)substr($line, 12, 2); + + $t = mktime($hour, $min, $sec, $month, $day, date('Y')); + $c = time(); + + return $t < $c - 5 || $t > $c; + } +} \ No newline at end of file diff --git a/scripts/lib/scenes.php b/scripts/lib/scenes.php index 0d33d3d..2c5060c 100644 --- a/scripts/lib/scenes.php +++ b/scripts/lib/scenes.php @@ -497,6 +497,14 @@ $scenes = [ ['type' => 'function', 'function' => 'bureauAuto', 'args' => [false]], ['type' => 'ir', 'room' => 'bureausun', 'device' => 'Projector', 'command' => 'PowerOn'], ], + 'bureau/media/stop' => [ + /* domoticzSwitch(1, false); + irsend('bureau', 'Projector', 'PowerOff');*/ + ['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'], + ], 'bureau/screen/down' => [ ['type' => 'domoticz', 'device' => '1', 'command' => true, 'priority' => true], ], diff --git a/scripts/lib/shield.php b/scripts/lib/shield.php index 495e4d0..a9c3554 100644 --- a/scripts/lib/shield.php +++ b/scripts/lib/shield.php @@ -261,13 +261,30 @@ function shieldShareIntent($content, $device = null) shieldCommand('shell am start', '-a android.intent.action.VIEW -d \'"' . $content . '"\'', $device); } +function shieldLogcat($cases = [], $device = null) +{ + shieldConnect(false, $device); + $proc = new proc(); + $cmd=_adbcmd('logcat', '', $device); + echo $cmd."\n"; + $proc->open($cmd); + $proc->on($cases); + $proc->run(); +} + function _adb($command, $params = '', $device = null) +{ + $c = _adbcmd($command, $params, $device); + echo $c . ' :: ' . `$c` . "\n"; +} + +function _adbcmd($command, $params, $device = null) { $device = getDevice($device); $c = 'adb -s ' . $device['shield'] . ':5555 ' . $command; if ($params) { $c .= ' ' . $params; } - echo $c . ' :: ' . `$c` . "\n"; + return $c; } \ No newline at end of file diff --git a/servers/logcat.php b/servers/logcat.php new file mode 100644 index 0000000..61ab15d --- /dev/null +++ b/servers/logcat.php @@ -0,0 +1,28 @@ + function () use ($device) { + echo 'sleep' . "\n"; + off($device); + }, + 'Waking up from sleep' => function () use ($device) { + echo 'wakeup' . "\n"; + shieldRunActivity($device); + }, +]; + +echo 'Run home server logcat ' . $device . "\n"; + +while (true) { + shieldLogcat($cases, $device); + echo 'Logcat ended' . "\n----\n"; +} diff --git a/servers/logcatb.php b/servers/logcatb.php new file mode 100644 index 0000000..aed22b0 --- /dev/null +++ b/servers/logcatb.php @@ -0,0 +1,4 @@ +#!/usr/bin/php +