スマートホーム、一応完成

 RM miniとスマートプラグをPCから動かせるようになったのはすでに書いた通り。
doroyamada.hatenablog.jp
doroyamada.hatenablog.jp

 ではこれを統合して動かすには、ということで、いろいろ考えていたが、ウェブサーバ経由で動かすことに。ソースは以下。

<html>
<head>
<title>Smart Home</title>
<link rel="stylesheet" type="text/css" href="index.css">
<script type="text/javascript">
if(location.href.indexOf("?",0)> 1){
	setTimeout(function(){location.href = '.';}, 30*1000);
}
</script>
</head>
<body>
<h1>Controller of Home Appliances</h1>
<TABLE><TR valign="top"><TD WIDTH="220px">
<!--	<div class="left">-->
	<p class="button"><button class="bt_l" onClick='location.href="./index.php?event=neru"'>寝る</button></P>
	<p class="button"><button class="bt_l" onClick='location.href="./index.php?event=okita"'>起きた</button></P>
	<p class="button"><button class="bt_l" onClick='location.href="./index.php?event=kaetta"'>帰った</button></p>
	<p class="button"><button class="bt_l" onClick='location.href="./index.php?event=dekakeru"'>出かける</button></p>
</TD><TD>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=tv_toggle"'>TV</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=light_on"'>ライトon</button> <button class="bt" onClick='location.href="./index.php?event=light_off"'>ライトoff</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=aircon_on"'>エアコンon</button> <button class="bt" onClick='location.href="./index.php?event=aircon_off"'>エアコンoff</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=diga_toggle"'>DIGA</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=amp_toggle"'>アンプ</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=hdmi_toggle"'>HDMI</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=carpet_on"'>カーペットon</button> <button class="bt" onClick='location.href="./index.php?event=carpet_off"'>カーペットoff</button></p>
	<p class="button"><button class="bt" onClick='location.href="./index.php?event=heater_on"'>ヒーターon</button> <button class="bt" onClick='location.href="./index.php?event=heater_off"'>ヒーターoff</button></p>
</TD></TR></table>
<p class="test"><a href="./">Do nothing</a></p>
<HR>
<pre>
<?php
error_reporting(ALL);
$rmmini="python /home/kazz/python-broadlink/BlackBeanControl/BlackBeanControl.py";
$webhook_former = "https://maker.ifttt.com/trigger/";
$webhook_latter = "/with/key/xxxxxxxxxxxxxxxxxxxxxxxxx";
$airconfile = "/var/www/scripts/smarthome/status/aircon.txt";
$event = $_GET['event'];
switch($event) {
	case "okita":
	case "neru":
	case "kaetta":
	case "dekakeru":
	case "aircon_on":
		$event();
		break;
	case "tv_toggle":
	case "aircon_off":
	case "light_on":
	case "light_off":
	case "diga_toggle":
	case "hdmi_toggle":
	case "amp_toggle":
		rmmini($event);
		break;
	case "carpet_on":
	case "carpet_off":
	case "heater_on":
	case "heater_off":
		webhook($event);
		break;
	default:
		break;
}

function okita(){
	rmmini("light_on,diga_toggle,tv_toggle,amp_toggle,hdmi_toggle");
	aircon_on();
	webhook("carpet_on,heater_off");
	$hour = date("G");	#時。24時間単位。先頭にゼロを付けない。
	$week = date("w");	#0 (日曜)から 6 (土曜)

	if($hour < 10){	#朝
		if($week == 6 && $hour > 7)	{
			rmmini("diga_ch10");	#土曜日8時から10時なら読売テレビ	
		}else{
			rmmini("diga_ch6");	#他の日はABC
		}	#10時過ぎていたら変えない
	}
}

function neru(){
	rmmini("aircon_off,tv_toggle,diga_toggle,amp_toggle,hdmi_toggle,light_off");
	webhook("carpet_off,heater_on");
}

function kaetta(){
	rmmini("light_on,diga_toggle,tv_toggle,amp_toggle,hdmi_toggle");
	aircon_on();
	webhook("carpet_on");
}

function dekakeru(){
	rmmini("aircon_off,diga_toggle,tv_toggle,amp_toggle,hdmi_toggle,light_off");
	webhook("carpet_off,heater_off");
}

function aircon_on(){
	rmmini("aircon_on");
	file_put_contents($airconfile,1);	#オンにしたので1
}

function rmmini($commands){
	global $rmmini;
	$command_array = explode(",", $commands);
	foreach($command_array as $command){
		exec("$rmmini -c $command");
		echo "$command\n";
		usleep(500000);
	}
}

function webhook($commands){
	global $webhook_former,$webhook_latter;
	$command_array = explode(",", $commands);
	foreach($command_array as $command){
		$temp = file_get_contents("$webhook_former$command$webhook_latter");
		echo "$command\n";
		echo $temp;		
		usleep(200000);
	}
}


?>
</pre>
</body>
</html>

 最初はボタンをCSSだけで配置しようとしたけど、挫折してtableタグを使用した。
 エアコンがオンかオフか記録しようとしているけど、別の方法で感知することを検討中。

 スクリーンショットはこんな感じ。

 古いタブレットを専用に使っているけど、スリープからの復帰が素早くないのが軽くストレス。