本帖最后由 aktifin 于 2018-8-3 16:54 编辑
家中有一些早年购入的Lifesmart的开关和传感器,虽然现在能够稳定的接入homekit,米家(小爱同学)和天猫精灵,不过一直纠结于无法接入hass,没法联动控制,正好今天无事,发现原来封闭的Lifesmart已经推出了开放平台,研究了开放文档发现米家和天猫精灵都应该是通过开放平台接入的
平台地址:http://www.ilifesmart.com/open,文档地址:http://www.ilifesmart.com/open/docs/
由于实践过天猫精灵的接入过程,并结合论坛上的webhook文章,
自己摸索着搞定了设备的websocket通讯
不过不知能否通过websocket将设备接入hass,还请大神指教
登录认证界面js代码
<script>
var timestamp = Math.round(new Date().getTime()/1000).toString();
document.write(timestamp);
var sign = hex_md5("appkey=xxxxxxx&auth_callback=https://xxxx/ls/lifesmart.php&time=" + timestamp + "&apptoken=xxxxxx");
document.write("<br/>" + sign);
var link = "https://api.ilifesmart.com/app/auth.authorize?id=957&appkey=xxxxxx&time="+ timestamp +"&auth_callback=https://xxxx/ls/lifesmart.php&sign="+ sign +"&lang=zh"
document.write("<br/>" + link);
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
window.location.href=link;
};
</script>
WebSocket 认证js代码
<script type="text/javascript">
ls();
function ls() {
$(document).ready(function () {
function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}
var url_userid = GetQueryString("userid");
var url_usertoken = GetQueryString("usertoken");
var timestamp = Math.round(new Date().getTime()/1000).toString();/*=时间戳=*/
var sign = hex_md5("method:WbAuth,time:"+ timestamp +",userid:"+ url_userid +",usertoken:"+ url_usertoken +",appkey:xxxx,apptoken:xxxx");/*=生成签名=*/
ws = new WebSocket('wss://api.ilifesmart.com:8443/wsapp/'); /*=api地址=*/
ws.addEventListener('open', function (event) {
ws.send('{"id": 957,"method": "WbAuth","system": {"ver": "1.0","lang": "ch","userid": "'+ url_userid +'","appkey": "xxxx","time": ' + timestamp + ', "sign":"' + sign + '"}}');
});
ws.onmessage = function (event) {
document.write(event.data)
};
ws.onerror = function(event) {
document.write("WebSocketError!");
};
});
};
</script>
获取设备列表
<?php
$id = (int)$_GET['id'];
$method = $_GET['method'];
$ver = $_GET['ver'];
$lang = $_GET['lang'];
$userid = $_GET['userid'];
$usertoken = $_GET['usertoken'];
$appkey = 'xxxxxxx';
$apptoken = 'xxxxxxx';
$time = (int)time();
$sign_str = "method:$method,time:$time,userid:$userid,usertoken:$usertoken,appkey:$appkey,apptoken:$apptoken";
$sign = md5($sign_str);
$data = array(
'id' => $id,
'method' => $method,
'system' => array(
'ver' => $ver,
'lang' => $lang,
'userid' => $userid,
'appkey' => $appkey,
'time' => $time,
'sign' => $sign
)
);
header('Content-type:application/json');
$data1 = json_encode($data);
//echo $data1;
//echo "\n\n";
$opts = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
'content' => $data1
)
);
$url = "https://api.ilifesmart.com/app/api.EpGetAll";
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
echo $result;
// 写入文件
file_put_contents('alldevice.json', $result);
?>
|