<html>
<head>
<title>SmartSwitch</title>
<meta name="viewport" content="width=device-width, initial-scale=0.7">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet" type="text/css">
<style type="text/css">
body {
font-family: 'Open Sans', sans-serif;
font-size: 20px;
text-align:center;
margin: auto;
width: 300px;
}
input {
font-family: 'Open Sans', sans-serif;
font-size: 16px;
text-align:center;
}
h1 {
font-size: 30px;
font-family: 'Open Sans', sans-serif;
}
table {
width: 300px;
margin: 50px 0px 50px 0px;
}
.alert-box {
color:#555;
width: 260px;
font-size: 12px;
border-radius:10px;
padding:10px 10px 10px 36px;
margin-top:20px;
}
.alert-box span {
font-weight:bold;
text-transform:uppercase;
}
.error {
background:#ffecec url('./error.png') no-repeat 10px 50%;
border:1px solid #f5aca6;
}
.success {
background:#e9ffd9 url('./success.png') no-repeat 10px 50%;
border:1px solid #a6ca8a;
}
</style>
</head>
<body>
<h1>SmartSwitch</h1>
<table>
<?php
$email = "info@hex.nl";
$hash = "XXXXXX"; // SHA-1, maak aan op http://www.sha1-online.com
$action = $_GET['action'];
$smartswitch = "".$_GET['smartswitch']."";
$apparaat = $_GET['apparaat'];
$url = 'https://cloud.homewizard.com/account/login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "".$email.":".$hash."");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = json_decode($output, true);
$token = $output['session'];
$zurl = 'https://plug.homewizard.com/plugs';
$headers = array(
"Accept: */*",
"Content-Type: application/json; charset=utf-8",
"X-Session-Token: " . $token
);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_URL, $zurl);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$zoutput = curl_exec($ch2);
$zinfo = curl_getinfo($ch2);
curl_close($ch2);
$zoutput = json_decode($zoutput, true);
foreach($zoutput as $sp)
{
if($sp['name'] == $smartswitch)
{
$plug = $sp['id'];
$found1 = "<br />\n\n<small>".$smartswitch." = ".$sp['id']."</small>\n<br />\n\n";
}
}
foreach($zoutput['0']['devices'] as $sd)
{
if($sd['name'] == $apparaat)
{
$found2 = "<small>".$apparaat." = ".$sd['id']."</small>\n\n<br />\n";
$device = $sd['id'];
}
echo "<tr>\n<td>".$sd['name']."</td>\n<td><input type=\"button\" onclick=\"location.href='./?smartswitch=".$sp['name']."&apparaat=".$sd['name']."&action=On';\" value=\"on\" /></td>\n<td><input type=\"button\" onclick=\"location.href='./?smartswitch=".$sp['name']."&apparaat=".$sd['name']."&action=Off';\" value=\"off\" /></td>\n</tr>\n";
}
echo "</table>\n";
$curl = "https://plug.homewizard.com/plugs/".$plug."/devices/".$device."/action";
$headers = array(
"Accept: */*",
"Content-Type: application/json; charset=utf-8",
"X-Session-Token: " . $token
);
$data = array("action" => "".$_GET['action']."");
$data_string = json_encode($data);
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch3, CURLOPT_URL, $curl);
curl_setopt($ch3, CURLOPT_POST, 1);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch3, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$coutput = curl_exec($ch3);
$cinfo = curl_getinfo($ch3);
curl_close($ch3);
$coutput = json_decode($coutput, true);
if ($coutput['status'] == "Success")
{
$action = strtolower($action);
echo "\n<div class=\"alert-box success\"><span>success: </span>".$apparaat." turned <b>".$action."</b> successfully</div>";
} elseif ($coutput['message'] == "Request method 'POST' not supported") {
echo "";
} else {
echo "\n<div class=\"alert-box error\"><span>error: </span>".$coutput['message']."</div>";
}
?>
<!-- SmartSwitch script door Ruben Bolink, https://hex.nl, info@hex.nl -->
</body>
</html>