|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MiJia struct {
|
|
|
|
|
Certificate Authorize
|
|
|
|
|
HomeData Home
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMiJia(Certificate Authorize) MiJia {
|
|
|
|
|
return MiJia{
|
|
|
|
|
Certificate: Certificate,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取设备列表
|
|
|
|
|
func (mi *MiJia) Devices() {
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"getVirtualModel": false,
|
|
|
|
|
"getHuamiDevices": 0,
|
|
|
|
|
}
|
|
|
|
|
PostData("/home/device_list", data, mi.Certificate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mi *MiJia) Rooms() {
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"fg": false,
|
|
|
|
|
"fetch_share": true,
|
|
|
|
|
"fetch_share_dev": true,
|
|
|
|
|
"limit": 300,
|
|
|
|
|
"app_ver": 7,
|
|
|
|
|
}
|
|
|
|
|
jsonData := PostData("/v2/homeroom/gethome", data, mi.Certificate)
|
|
|
|
|
json.Unmarshal(jsonData, &mi.HomeData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取场景列表
|
|
|
|
|
func (mi *MiJia) Scenes(roomIdx int) {
|
|
|
|
|
HomeId := mi.HomeData.Result.Homelist[roomIdx].Id
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"home_id": HomeId,
|
|
|
|
|
}
|
|
|
|
|
PostData("/appgateway/miot/appsceneservice/AppSceneService/GetSceneList", data, mi.Certificate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据房间获取耗材列表
|
|
|
|
|
func (mi *MiJia) Consumables(roomIdx int) {
|
|
|
|
|
HomeId := mi.HomeData.Result.Homelist[roomIdx].Id
|
|
|
|
|
Homei, err := strconv.Atoi(HomeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"home_id": Homei,
|
|
|
|
|
"owner_id": mi.Certificate.UserId,
|
|
|
|
|
}
|
|
|
|
|
PostData("/v2/home/standard_consumable_items", data, mi.Certificate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mi *MiJia) RunScene(SceneId string) {
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"scene_id": SceneId,
|
|
|
|
|
"trigger_key": "user.click",
|
|
|
|
|
}
|
|
|
|
|
PostData("/appgateway/miot/appsceneservice/AppSceneService/RunScene", data, mi.Certificate)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type HomeList struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Bssid string `json:"bssid"`
|
|
|
|
|
Dids []string `json:"dids"`
|
|
|
|
|
TempDids interface{} `json:"temp_dids"`
|
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
|
Shareflag int `json:"shareflag"`
|
|
|
|
|
PermitLevel int `json:"permit_level"`
|
|
|
|
|
Status int `json:"status"`
|
|
|
|
|
Background string `json:"background"`
|
|
|
|
|
SmartRoomBackground string `json:"smart_room_background"`
|
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
|
CityId int `json:"city_id"`
|
|
|
|
|
Address string `json:"address"`
|
|
|
|
|
CreateTime int `json:"create_time"`
|
|
|
|
|
Roomlist []struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Bssid string `json:"bssid"`
|
|
|
|
|
Parentid string `json:"parentid"`
|
|
|
|
|
Dids []string `json:"dids"`
|
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
|
Background string `json:"background"`
|
|
|
|
|
Shareflag int `json:"shareflag"`
|
|
|
|
|
CreateTime int `json:"create_time"`
|
|
|
|
|
}
|
|
|
|
|
Uid int64 `json:"uid"`
|
|
|
|
|
AppearHomeList interface{} `json:"appear_home_list"`
|
|
|
|
|
PopupFlag int `json:"popup_flag"`
|
|
|
|
|
PopupTimeStamp int `json:"popup_time_stamp"`
|
|
|
|
|
CarDid string `json:"car_did"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Home struct {
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Result struct {
|
|
|
|
|
Homelist []HomeList `json:"homelist"`
|
|
|
|
|
HasMore bool `json:"has_more"`
|
|
|
|
|
MaxId string `json:"max_id"`
|
|
|
|
|
} `json:"result"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mi *MiJia) GetDeviceVar(devs []map[string]interface{}) {
|
|
|
|
|
//参数说明
|
|
|
|
|
//did: 设备ID
|
|
|
|
|
//siid: 功能分类ID
|
|
|
|
|
//piid: 设备属性ID
|
|
|
|
|
//aiid: 设备方法ID
|
|
|
|
|
//从下述网站查询
|
|
|
|
|
//米家产品库,网站不稳定,不知道siid和piid无法使用`getDevAtt`和`setDevAtt`
|
|
|
|
|
//一个取巧的办法是使用在米家APP手动设置批量控制,然后使用`runScene`
|
|
|
|
|
//https://home.miot-spec.com/
|
|
|
|
|
//
|
|
|
|
|
//获取全部设备列表函数`getDevices`返回结果说明
|
|
|
|
|
//返回结果说明
|
|
|
|
|
//name: 设备名称
|
|
|
|
|
//did: 设备ID
|
|
|
|
|
//isOnline: 设备是否在线
|
|
|
|
|
//model: 设备产品型号, 根据这个去米家产品库查该产品相关的信息
|
|
|
|
|
//
|
|
|
|
|
//获取设备属性,一次可以请求多个
|
|
|
|
|
//Atts = getDevAtt([{"did":"111111111","siid":2,"piid":1},{"did":"111111111","siid":2,"piid":2},
|
|
|
|
|
// {"did":"111111111","siid":2,"piid":3},{"did":"111111111","siid":2,"piid":4}])
|
|
|
|
|
//print(Atts)
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"params": devs,
|
|
|
|
|
}
|
|
|
|
|
PostData("/miotspec/prop/get", data, mi.Certificate)
|
|
|
|
|
}
|