1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * This is a simple script to shutdown a frontend when it is ideling in the mainmenu |
---|
5 | * I wrote this because i want to shutdown my frontent when the timer i can set in mythtv was reached and returned to the main menu |
---|
6 | */ |
---|
7 | |
---|
8 | $host = "192.168.0.25"; |
---|
9 | $port = 6546; |
---|
10 | $socketTimeOut = 30; |
---|
11 | $idleMenuSecs = 300; |
---|
12 | $shutDownProgramm = "sudo /sbin/halt"; |
---|
13 | |
---|
14 | // try to open the socket |
---|
15 | $fp = false; |
---|
16 | while($fp == false) { |
---|
17 | $fp=@fsockopen($host,$port, $errno, $errstr, $socketTimeOut); |
---|
18 | sleep(5); |
---|
19 | } |
---|
20 | |
---|
21 | // read the menu :) |
---|
22 | readTerminal(&$fp,$bla=''); |
---|
23 | |
---|
24 | echo "Connected to the server: ".$host.":".$port."\n"; |
---|
25 | |
---|
26 | // start the loop here :) |
---|
27 | readMenuStatus(&$fp,&$idleMenuSecs); |
---|
28 | |
---|
29 | // loop exit ? well call the shutdown command :) |
---|
30 | $ls = system($shutDownProgramm); |
---|
31 | die(); |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * This looks at the status of the menu |
---|
36 | */ |
---|
37 | function readMenuStatus(&$fp,&$idleMenuSecs,&$inMainMenu = false) { |
---|
38 | |
---|
39 | echo "Sending command: query location\n"; |
---|
40 | |
---|
41 | $status = ''; |
---|
42 | sendCommand(&$fp,'query location',&$status); |
---|
43 | echo "Current Menu is: ".$status."\n"; |
---|
44 | |
---|
45 | // we are in the mainmenu |
---|
46 | if(strpos($status,"mainmenu") !== false) { |
---|
47 | if($inMainMenu == false) { |
---|
48 | // first time in mainmenu |
---|
49 | echo "In mainmenu, next time i will shutdown the system\n"; |
---|
50 | $inMainMenu = true; |
---|
51 | } else { |
---|
52 | // still in mainmenu ? call shutdown |
---|
53 | echo "Shutting down system !\n"; |
---|
54 | fclose($fp); |
---|
55 | return; |
---|
56 | } |
---|
57 | } else { |
---|
58 | $inMainMenu = false; |
---|
59 | } |
---|
60 | |
---|
61 | sleep($idleMenuSecs); |
---|
62 | |
---|
63 | readMenuStatus(&$fp,&$idleMenuSecs,&$inMainMenu); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | * reads data from the terminal |
---|
69 | */ |
---|
70 | function readTerminal(&$fp,&$r) { |
---|
71 | |
---|
72 | $r=''; |
---|
73 | do { |
---|
74 | $r.=fread($fp,1000); |
---|
75 | $s=socket_get_status($fp); |
---|
76 | } while ($s['unread_bytes']); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * sends command to the terminal |
---|
81 | */ |
---|
82 | function sendCommand(&$fp,$c,&$r) { |
---|
83 | if ($fp) { |
---|
84 | fputs($fp,"$c\r"); |
---|
85 | fputs($fp,"\n"); |
---|
86 | sleep(1); |
---|
87 | readTerminal(&$fp,&$r); |
---|
88 | $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r); |
---|
89 | } |
---|
90 | return $fp?1:0; |
---|
91 | } |
---|
92 | |
---|
93 | ?> |
---|