WebSocket 是一种网络传输协定,可在单个 TCP 连贯上进行全双工通信,位于 OSI 模型的应用层。WebSocket 协定在 2011 年由 IETF 标准化为 RFC 6455,后由 RFC 7936 补充标准。
WebSocket 使得客户端和服务器之间的数据交换变得更加简略,容许服务端被动向客户端推送数据。在 WebSocket API 中,浏览器和服务器只须要实现一次握手,两者之间就能够创立持久性的连贯,并进行双向数据传输。本文次要跟大家分享一下智汀家庭云的WebSocket API 音讯定义。
1.音讯定义
通常,一个 WebSocket 音讯格局如下:
{ "id": 1, "domain": "", "service": "", "service_data": { "device_id": 1 }}
·id: 音讯ID,必填,服务端会返回对应 ID 的后果
·domain: plugin或者插件id
- 设施相干命令
2.1插件设施状态变更
{ "type": "attribute_change", "identity": "2762071932", "instance_id": 2, "attr": { "attribute": "power", "val": "on", "val_type": "string" }}
2.2发现设施
申请:Request
{ "id": 1, "service": "discover"}
响应:Response
{ "id": 1, "type": "", "result": { "device": { "name": "zhiting_M1", "identity": "hijklmn", "model": "M1", "manufacturer": "zhiting", "plugin_id": "demo" } }, "success": true}
2.3获取设施属性
申请:Request
{ "id": 1, "domain": "zhiting", "service": "get_attributes", "identity": "2762071932"}
响应:Response
{ "id": 1, "result": { "identity": "2762071932", "device": { "name": "", "identity": "2762071932", "instances": [ { "type": "light_bulb", "instance_id": 0, "attrs": [ { "attribute": "power", "val": "on", "val_type": "string" }, { "attribute": "brightness", "val": 55, "val_type": "int" }, { "attribute": "color_temp", "val": 3500, "val_type": "int" } ] } ] } }, "success": true}
2.4 设置设施属性
申请:Request
{ "id": 1, "domain": "zhiting", "service": "set_attributes", "identity": "2762071932", "service_data": { "attributes": [ { "instance_id": 1, "attribute": "power", "val": "on" } ] }}
响应:Response
{ "id": 1, "type": "response", "success": true, "error": "error"}
3.装置插件
{ "id": 1, "domain": "plugin", "service": "install", "service_data": { "plugin_id": "plugin_id" }}
{ "id": 1, "success": true}
以上,感激大家观看,感兴趣的能够在评论区给小编留言哦。