Files
xiaomihome/miot/miot_client_api.go
T
4566704 b808e51b3e refactor: 重大架构升级 — HomeID/RoomID int64化 + 设备驱动重构 + 结构化日志
主要变更:

**类型系统重构**
- HomeInfo.ID/RoomInfo.ID/HomeID 从 string 改为 int64,json tag 使用 ,string
- GetHome/GetRooms/GetRoomDevices 接口同步修改
- 新增 int64FromStr 工具函数,更新 parseHomeInfo 解析逻辑

**新增 Speaker 设备**
- 新增 KindSpeaker 类型
- classify 映射覆盖 speaker/bluetooth-speaker/alarm-clock/media-player/playback/music

**设备驱动重构**
- AC 驱动彻底重写:从硬编码常量改为基于 SPEC ValueList 的动态 valMapper
- 所有设备(Fan/Humidifier/Light/OccupancySensor/Switch/TempHumiditySensor/Thermostat/Vacuum/WaterHeater/Cover)新增 GetState() 和 FetchState(ctx) 接口
- base.go 新增 specResolver.format() 方法

**MIoT 模块增强**
- MIoTClient 新增 homeIDs 字段,支持按家庭过滤设备
- RefreshDevices 支持 homeIDs 参数
- SpecStdLib 引入 logger.Logger,log.Printf 全部迁移为结构化日志

**其他**
- Config 新增 RedirectURL 和 HomeIDs 字段
- AuthInfo.ExpiresIn 类型从 int 改为 int32
- UUID 生成简化为 hex.EncodeToString
- 新增 github.com/sirupsen/logrus 依赖
- .gitignore 新增 examples/*/data/ 忽略规则
- 删除已提交的 config.json 敏感配置
2026-07-13 10:40:20 +08:00

123 lines
3.5 KiB
Go

// Package miot provides MIoT core client for Xiaomi Home devices.
// It's a Golang port of MIoTClient from xiaomihome-py/miot/.
package miot
import (
"fmt"
)
// ============================================================================
// Cloud API calls (aligned with Python miot_client.py)
// ============================================================================
// SetProp sets a device property via cloud API.
// Aligns with Python: set_prop_async.
// did: device ID
// siid: service IID
// piid: property IID
// value: property value
// Returns: error if failed.
func (c *MIoTClient) SetProp(did string, siid, piid int, value interface{}) error {
if c.http == nil {
return fmt.Errorf("HTTP client not available")
}
params := []PropParam{
{DID: did, Siid: siid, Piid: piid, Value: value},
}
results, err := c.http.SetProp(params)
if err != nil {
return err
}
// code=0 或 code=1 均视为成功(0=读取成功,1=设置成功)
if len(results) > 0 && results[0].Code != 0 && results[0].Code != 1 {
return fmt.Errorf("set property failed, code=%d", results[0].Code)
}
c.lgr.Infof("[MIoTClient] SetProp: %s/%d/%d = %v\n", did, siid, piid, value)
return nil
}
// GetProp gets a device property via cloud API.
// Aligns with Python: get_prop_async.
// did: device ID
// siid: service IID
// piid: property IID
// Returns: property value and error.
func (c *MIoTClient) GetProp(did string, siid, piid int) (interface{}, error) {
if c.http == nil {
return nil, fmt.Errorf("HTTP client not available")
}
params := []PropParam{
{DID: did, Siid: siid, Piid: piid},
}
results, err := c.http.GetProps(params)
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, fmt.Errorf("no result for property %d.%d", siid, piid)
}
if results[0].Code != 0 {
return nil, fmt.Errorf("get property failed, code=%d", results[0].Code)
}
c.lgr.Infof("[MIoTClient] GetProp: %s/%d/%d = %v\n", did, siid, piid, results[0].Value)
return results[0].Value, nil
}
// Action executes a device action via cloud API.
// Aligns with Python: action_async.
// did: device ID
// siid: service IID
// aiid: action IID
// inList: input parameters
// Returns: action result and error.
func (c *MIoTClient) Action(did string, siid, aiid int, inList []interface{}) (map[string]interface{}, error) {
if c.http == nil {
return nil, fmt.Errorf("HTTP client not available")
}
result, err := c.http.DoAction(did, siid, aiid, inList)
if err != nil {
return nil, err
}
c.lgr.Infof("[MIoTClient] Action: %s/%d/%d, result=%v\n", did, siid, aiid, result)
return result, nil
}
// ============================================================================
// Batch operations (aligned with Python miot_client.py)
// ============================================================================
// SetProps sets multiple device properties in batch.
// Aligns with Python: set_props_async.
// params: slice of PropParam
// Returns: slice of PropResult and error.
func (c *MIoTClient) SetProps(params []PropParam) ([]PropResult, error) {
if c.http == nil {
return nil, fmt.Errorf("HTTP client not available")
}
return c.http.SetProp(params)
}
// GetProps gets multiple device properties in batch.
// Aligns with Python: get_props_async.
// params: slice of PropParam
// Returns: slice of PropResult and error.
func (c *MIoTClient) GetProps(params []PropParam) ([]PropResult, error) {
if c.http == nil {
return nil, fmt.Errorf("HTTP client not available")
}
return c.http.GetProps(params)
}