You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.6 KiB
Go

5 months ago
package mijia
7 months ago
import (
"encoding/json"
5 months ago
"mijia/model"
7 months ago
"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)
}
func (mi *MiJia) GetDeviceVar(devs []map[string]interface{}) ([]model.DeviceVarResult, error) {
data := map[string]interface{}{
"params": devs,
}
res := PostData("/miotspec/prop/get", data, mi.Certificate)
var Var model.DeviceVar
err := json.Unmarshal(res, &Var)
if err != nil {
return []model.DeviceVarResult{}, err
}
return Var.Result, nil
}
func (mi *MiJia) SetDeviceVar(devs []map[string]interface{}) {
data := map[string]interface{}{
"params": devs,
}
PostData("/miotspec/prop/set", data, mi.Certificate)
}
func (mi *MiJia) RunAction(data map[string]interface{}) {
//uri = "/miotspec/action"
//params = dict(params={"did": self.device_id, "siid": sid, "piid": pid, "aiid": aid, "in": values})
//result = self.http_request(uri, params)
//request_code = result.get("code")
//if not request_code:
//data = result.get("result")
//code = result.get("code")
//if not code:
//return dict(code=0, msg="success", data=data)
//else:
//return dict(code=code, msg="error", data=dict())
//return dict(code=request_code, msg="request error", data=dict())
}