Falls ihr TP-Link „Smart“ Sockets nutzt, ist dieses Skript vielleicht etwas für euch. Ich nutze diese, um einige nicht essentielle Verbrauchsmessungen an einem anderen Ort durchzuführen. Wer wirklich smart ist, nutzt solche Cloud-Geschichten selbstverständlich nicht für wichtige Dinge und/oder direkt vor Ort
<?
$user = "email@example.com";
$pass = "SuperSecret";
$uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
$url = "https://wap.tplinkcloud.com";
$token_id = 45760 /*[Skripte\TP-Link Cloud\TP-Link Cloud Skript\Token]*/; // eine String-Variable
$devlist_update_id = 36648 /*[Skripte\TP-Link Cloud\TP-Link Cloud Skript\LastDeviceListUpdate]*/; // eine Int-Variable
$devlist_cat_id = 25986 /*[Hardware-Instanzen\TP-Link Cloud]*/; // Kategorie wo die Geräte-Instanzen hin sollen
$token = GetValue($token_id);
if($token == "") update_token();
if($_IPS["SENDER"] == "WebFront" || $_IPS["SENDER"] == "RunScript") {
$inst = IPS_GetParent($_IPS["VARIABLE"]);
$device_id = GetValue(IPS_GetObjectIDByIdent("deviceId", $inst));
$as_url = GetValue(IPS_GetObjectIDByIdent("appServerUrl", $inst));
$rd = array(
"system" => array(
"set_relay_state" => array(
"state" => $_IPS["VALUE"] ? 1 : 0
)
)
);
$json = json_encode($rd);
$params = array(
"deviceId" => $device_id,
"requestData" => $json
);
$result = cloud_request("passthrough", $params, $as_url);
if($result) {
try {
$json = $result["responseData"];
$result = json_decode($json, true);
$err_code = $result["system"]["set_relay_state"]["err_code"];
if($err_code == 0) {
SetValue($_IPS["VARIABLE"], $_IPS["VALUE"]);
}
} catch(Exception $e) {}
}
} else if($_IPS["SENDER"] == "TimerEvent" || $_IPS["SENDER"] == "Execute") {
$device_list_update = GetValue($devlist_update_id);
if(time() > $devlist_update_id + 60) {
$device_list = cloud_request("getDeviceList");
if($device_list) {
$device_list = $device_list["deviceList"];
foreach($device_list as $device) {
$inst_id = update_local_device($device);
request_remote_device_status($inst_id, $device["deviceId"], $device["appServerUrl"]);
}
SetValue($devlist_update_id, time());
} else {
SetValue($token_id, "");
}
} else {
$devices = IPS_GetChildrenIDs($devlist_cat_id);
foreach($devices as $inst_id) {
$device_id = GetValue(IPS_GetObjectIDByIdent("deviceId", $inst_id));
$as_url = GetValue(IPS_GetObjectIDByIdent("appServerUrl", $inst_id));
request_remote_device_status($inst_id, $device_id, $as_url);
}
}
}
function request_remote_device_status($inst_id, $device_id, $as_url) {
$rd = array(
"system" => array(
"get_sysinfo" => null
),
"emeter" => array(
"get_realtime" => null
)
);
$json = json_encode($rd);
$params = array(
"deviceId" => $device_id,
"requestData" => $json
);
$result = cloud_request("passthrough", $params, $as_url);
if($result) {
try {
$json = $result["responseData"];
$result = json_decode($json, true);
//print_r($result);
$sysinfo = $result["system"]["get_sysinfo"];
$emeter = $result["emeter"]["get_realtime"];
$err_code = $sysinfo["err_code"];
if($err_code == 0) {
$status = $sysinfo["relay_state"];
update_var($inst_id, "Stromversorgung", "relay_state", $status, 0, true);
$current = $emeter["current"];
update_var($inst_id, "Stromaufnahme", "current", $current, 2);
$voltage = $emeter["voltage"];
update_var($inst_id, "Netzspannung", "voltage", $voltage, 2);
$power = $emeter["power"];
update_var($inst_id, "Leistung", "power", $power, 2);
$consumption = $emeter["total"];
update_var($inst_id, "Zähler", "total", $consumption, 2);
//print_r($emeter);
}
} catch(Exception $e) {}
}
}
function update_local_device($device) {
global $devlist_cat_id;
$ident = $device["deviceMac"];
$name = $device["alias"];
$inst_id = @IPS_GetObjectIDByIdent($ident, $devlist_cat_id);
if($inst_id === false) {
$inst_id = IPS_CreateInstance("{485D0419-BE97-4548-AA9C-C083EB82E61E}");
IPS_SetIdent($inst_id, $ident);
IPS_SetParent($inst_id, $devlist_cat_id);
}
if(IPS_GetName($inst_id) != $name) {
IPS_SetName($inst_id, $name);
}
foreach($device as $key => $value) {
switch($key) {
case "alias":
break;
case "fwVer":
update_var($inst_id, "Firmware-Version", $key, $value, 3);
break;
case "deviceType":
update_var($inst_id, "Gerätekategorie", $key, $value, 3);
break;
case "appServerUrl":
update_var($inst_id, "AppServer-URL", $key, $value, 3);
break;
case "deviceModel":
update_var($inst_id, "Modell", $key, $value, 3);
break;
case "status":
update_var($inst_id, "Online", $key, $value, 0);
break;
case "deviceId":
update_var($inst_id, "DeviceId", $key, $value, 3);
break;
default:
//echo $key . "=" . $value . "\r";
}
}
return $inst_id;
}
function update_var($parent_id, $name, $ident, $value, $ips_datatype, $action = false) {
$id = @IPS_GetObjectIDByIdent($ident, $parent_id);
if($id === false) {
$id = IPS_CreateVariable($ips_datatype);
IPS_SetIdent($id, $ident);
IPS_SetParent($id, $parent_id);
if($action) {
IPS_SetVariableCustomAction($id, $_IPS["SELF"]);
}
}
if(IPS_GetName($id) != $name) {
IPS_SetName($id, $name);
}
SetValue($id, $value);
}
function cloud_request($method, $params = null, $appServerUrl = null) {
global $url, $token;
$arr = array(
"method" => $method,
);
if($params != null) $arr["params"] = $params;
$json = json_encode($arr);
$headers = array();
$headers[] = 'Content-Type: application/json';
if($appServerUrl != null)
$req_url = $appServerUrl;
else
$req_url = $url;
$c = curl_init($req_url . "?token=" . $token);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $json);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c ,CURLOPT_TIMEOUT, 15000);
try {
@$response = curl_exec($c);
@curl_close($c);
$response = @json_decode($response, true);
return @$response["result"];
} catch(Exception $e) {
return false;
}
}
function update_token() {
global $url, $user, $pass, $uuid, $token, $token_id;
$arr = array(
"method" => "login",
"params" => array(
"cloudUserName" => $user,
"cloudPassword" => $pass,
"appType" => "Kasa_Android",
"terminalUUID" => $uuid
)
);
$json = json_encode($arr);
$headers = array();
$headers[] = 'Content-Type: application/json';
$c = curl_init($url);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($c, CURLOPT_TIMEOUT, 10);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $json);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
try {
$response = @curl_exec($c);
curl_close($c);
$response = json_decode($response, true);
$token = @$response["result"]["token"];
SetValue($token_id, $token);
return true;
} catch(Exception $e) {
return false;
}
}
?>