feat: State→Props 命名统一 + 新增 Plug/Gateway 设备 + 分类系统重构
**命名统一(State → Props)** - 所有设备类型:XxxState → XxxProps,GetState() → GetProps(),FetchState() → FetchProps() - 回调接口:OnStateChanged → OnPropsChanged - Bridge 层类型别名、示例代码、文档同步更名 **新增设备类型** - KindPlug + Plug 驱动:智能插座/插头(TurnOn/TurnOff/IsOn/OnPropsChanged) - KindGateway + Gateway 驱动:网关设备类型识别 - KindOccupancySensor 从 KindSensor 独立,添加 OccupancyProps **设备分类系统改进** - classifyByModel 改为按 key 长度降序匹配(防止 sensor 先于 sensor-occupy) - 新增下划线变体识别(sensor_occupy、sensor_temp 等) - outlet 重新归类为 Plug(非 Switch) - 新增 Or() 组合过滤器 **连接桥增强** - 设备创建成功后自动订阅属性变更 - 新增 nil 防护检查 - 支持 Plug/Speaker/Gateway/Occupancy 类型的 FetchProps 刷新 - 日志级别 Info → Debug **AC 驱动增强** - 新增 HasPower()、GetElectricPower()、GetPowerConsumption() 功率接口 - ACProps 新增 ElectricPower、PowerConsumption 字段 - valMapper:Desc 为空时 fallback 用 Name,key 统一小写 **MIoT 核心** - 设备列表刷新改用 reflect.DeepEqual 全字段变更检测 - 移除 BLE/代理设备默认在线 hack - RefreshDeviceAllProps 增加离线检查 - 新增 miot_errors.go:MIoT 错误码定义 - 全局日志级别 Info→Debug/Warn(降低噪音) **示例与文档** - 所有示例代码同步 API 变更 - 传感器示例改用 Or 组合过滤器 - ARCH_PLAN 等迁移文档同步更新
This commit is contained in:
@@ -503,7 +503,7 @@ type AirConditioner interface {
|
||||
GetEco(ctx context.Context) (bool, error)
|
||||
|
||||
// 订阅(不需要 ctx,订阅是本地注册操作)
|
||||
OnStateChanged(handler func(state ACState)) (subID string, err error)
|
||||
OnPropsChanged(handler func(state ACState)) (subID string, err error)
|
||||
}
|
||||
|
||||
// ACMode 空调模式
|
||||
@@ -581,7 +581,7 @@ type Light interface {
|
||||
SetColor(ctx context.Context, r, g, b int) error // 0-255
|
||||
GetColor(ctx context.Context) (r, g, b int, error)
|
||||
|
||||
OnStateChanged(handler func(state LightState)) (subID string, err error)
|
||||
OnPropsChanged(handler func(state LightState)) (subID string, err error)
|
||||
}
|
||||
|
||||
type LightState struct {
|
||||
@@ -606,7 +606,7 @@ type Switch interface {
|
||||
Toggle(ctx context.Context) error
|
||||
IsOn(ctx context.Context) (bool, error)
|
||||
|
||||
OnStateChanged(handler func(on bool)) (subID string, err error)
|
||||
OnPropsChanged(handler func(on bool)) (subID string, err error)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1059,7 +1059,7 @@ func main() {
|
||||
d.SetTargetTemp(ctx, 26)
|
||||
d.SetFanSpeed(ctx, devices.SpeedAuto)
|
||||
// 订阅状态变化
|
||||
d.OnStateChanged(func(state devices.ACState) {
|
||||
d.OnPropsChanged(func(state devices.ACState) {
|
||||
fmt.Printf("空调 %s: 模式=%s 温度=%.1f°C\n",
|
||||
dev.Name(), state.Mode, state.TargetTemp)
|
||||
})
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
**不修改的方法**(纯本地操作):
|
||||
- SubProp / UnsubProp / SubEvent / UnsubEvent / SubDeviceState / UnsubDeviceState
|
||||
- OnStateChanged
|
||||
- OnPropsChanged
|
||||
- NewClient / NewDevice 等工厂方法
|
||||
|
||||
---
|
||||
@@ -229,9 +229,9 @@ func (m *PropertyMapper) ToRaw(enum string) (interface{}, bool)
|
||||
|
||||
---
|
||||
|
||||
### #9 各设备接口 OnStateChanged 返回值不统一
|
||||
### #9 各设备接口 OnPropsChanged 返回值不统一
|
||||
|
||||
**问题**:OnStateChanged 返回 `(string, error)`,但 SubProp/SubEvent 返回 `(string, error)` — 这里它们是统一的。但缺少对 subID 语义的说明。
|
||||
**问题**:OnPropsChanged 返回 `(string, error)`,但 SubProp/SubEvent 返回 `(string, error)` — 这里它们是统一的。但缺少对 subID 语义的说明。
|
||||
|
||||
**修复方案**:注释统一说明 subID 是订阅标识符,可用于取消。
|
||||
|
||||
@@ -276,7 +276,7 @@ func (m *PropertyMapper) ToRaw(enum string) (interface{}, bool)
|
||||
| 5 | P1 | types.go | HomeInfo 加 CityID/Longitude/Latitude/Address |
|
||||
| 6 | P1 | client.go | Client 去掉 homes 缓存字段 |
|
||||
| 7 | P1 | specs/mapper.go(新) | PropertyMapper 双向值映射 |
|
||||
| 9 | P2 | devices/*.go | OnStateChanged 统一命名返回值 subID |
|
||||
| 9 | P2 | devices/*.go | OnPropsChanged 统一命名返回值 subID |
|
||||
| 11 | P2 | ARCH_PLAN.md 使用示例 | 更新为 ctx + factory + type switch 模式 |
|
||||
|
||||
---
|
||||
|
||||
+45
-33
@@ -4,6 +4,7 @@ package miot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -103,12 +104,6 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
|
||||
info[k] = v
|
||||
}
|
||||
|
||||
// BLE/proxy devices default online
|
||||
connectType := getConnectType(info)
|
||||
if connectType == 23 || connectType == 8 || connectType == 12 || connectType == 0 {
|
||||
info["online"] = true
|
||||
}
|
||||
|
||||
// Handle sub-devices: .s\d+ suffix
|
||||
match := subDevPattern.FindString(did)
|
||||
if match != "" {
|
||||
@@ -165,20 +160,21 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
|
||||
} else {
|
||||
c.deviceListCache[did] = cloneMap(info)
|
||||
}
|
||||
c.lgr.Infof("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
|
||||
c.lgr.Debugf("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
|
||||
changed = true
|
||||
} else {
|
||||
// Check if online status changed
|
||||
newOnline := getOnline(info)
|
||||
oldOnline := getOnline(oldInfo)
|
||||
if newOnline != oldOnline {
|
||||
oldInfo["online"] = newOnline
|
||||
c.deviceListCloud[did] = oldInfo
|
||||
if cacheInfo, ok := c.deviceListCache[did]; ok {
|
||||
cacheInfo["online"] = newOnline
|
||||
// 检测并同步所有变更字段(name/model/nickname 等)
|
||||
for k, v := range info {
|
||||
if oldV, ok := oldInfo[k]; !ok || !reflect.DeepEqual(oldV, v) {
|
||||
oldInfo[k] = v
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
c.deviceListCloud[did] = oldInfo
|
||||
if cacheInfo, ok := c.deviceListCache[did]; ok {
|
||||
for k, v := range info {
|
||||
cacheInfo[k] = v
|
||||
}
|
||||
c.lgr.Infof("[MIoTClient] Device online changed: %s online=%v\n", did, newOnline)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -277,27 +273,43 @@ func (c *MIoTClient) aggregateDeviceState(did string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Aggregate from three sources
|
||||
// OR 聚合:任一来源显式 online=true → 在线;
|
||||
// 否则任一来源显式 online=false → 离线;
|
||||
// 否则(无来源提供 online 字段)→ 保留缓存旧值,缓存也没有则默认在线。
|
||||
online := false
|
||||
explicitTrue := false
|
||||
explicitFalse := false
|
||||
|
||||
if cloudInfo, exists := c.deviceListCloud[did]; exists {
|
||||
if o, ok := cloudInfo["online"].(bool); ok && o {
|
||||
online = true
|
||||
sources := []map[string]interface{}{
|
||||
c.deviceListCloud[did],
|
||||
c.deviceListGateway[did],
|
||||
c.deviceListLan[did],
|
||||
}
|
||||
for _, info := range sources {
|
||||
if o, ok := info["online"].(bool); ok {
|
||||
if o {
|
||||
explicitTrue = true
|
||||
} else {
|
||||
explicitFalse = true
|
||||
}
|
||||
} else if _, exists := info["online"]; exists {
|
||||
c.lgr.Debugf("[MIoTClient] %s online type mismatch: %v (%T)\n", did, info["online"], info["online"])
|
||||
}
|
||||
}
|
||||
if !online {
|
||||
if gwInfo, exists := c.deviceListGateway[did]; exists {
|
||||
if o, ok := gwInfo["online"].(bool); ok && o {
|
||||
online = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !online {
|
||||
if lanInfo, exists := c.deviceListLan[did]; exists {
|
||||
if o, ok := lanInfo["online"].(bool); ok && o {
|
||||
online = true
|
||||
}
|
||||
|
||||
switch {
|
||||
case explicitTrue:
|
||||
online = true
|
||||
case explicitFalse:
|
||||
online = false
|
||||
default:
|
||||
if oldOnline, ok := cacheInfo["online"].(bool); ok {
|
||||
online = oldOnline
|
||||
} else {
|
||||
online = true // 云列表里有但没 online 字段,假设在线
|
||||
}
|
||||
c.lgr.Debugf("[MIoTClient] %s no online field from cloud/gw/lan, fallback online=%v (cache=%v)\n",
|
||||
did, online, cacheInfo["online"])
|
||||
}
|
||||
|
||||
cacheInfo["online"] = online
|
||||
|
||||
+17
-13
@@ -21,6 +21,9 @@ func (c *MIoTClient) RefreshDeviceAllProps(did string) error {
|
||||
if !exists {
|
||||
return fmt.Errorf("device %s not found in cache", did)
|
||||
}
|
||||
if !getOnline(info) {
|
||||
return fmt.Errorf("device %s is offline", did)
|
||||
}
|
||||
|
||||
device := c.getOrCreateDevice(did, info)
|
||||
if device == nil {
|
||||
@@ -41,7 +44,7 @@ func (c *MIoTClient) RefreshDeviceAllProps(did string) error {
|
||||
}
|
||||
}
|
||||
|
||||
c.lgr.Infof("[MIoTClient] RefreshDeviceAllProps: %s → %d props enqueued\n", did, count)
|
||||
c.lgr.Debugf("[MIoTClient] RefreshDeviceAllProps: %s → %d props enqueued\n", did, count)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -59,7 +62,7 @@ func (c *MIoTClient) RequestRefreshProp(did string, siid, piid int) {
|
||||
|
||||
// Check if already in refresh list
|
||||
if _, exists := c.refreshPropsList[key]; exists {
|
||||
c.lgr.Infof("[MIoTClient] RequestRefreshProp: SKIPPED (already queued) %s\n", key)
|
||||
c.lgr.Debugf("[MIoTClient] RequestRefreshProp: SKIPPED (already queued) %s\n", key)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,7 +74,7 @@ func (c *MIoTClient) RequestRefreshProp(did string, siid, piid int) {
|
||||
|
||||
// Schedule refresh if not already scheduled
|
||||
if c.refreshPropsTimer == nil {
|
||||
c.lgr.Infof("[MIoTClient] RequestRefreshProp: Scheduling refresh timer for %s\n", key)
|
||||
c.lgr.Debugf("[MIoTClient] RequestRefreshProp: Scheduling refresh timer for %s\n", key)
|
||||
// Reset retry count for new batch
|
||||
c.refreshRetryCount = 0
|
||||
|
||||
@@ -80,14 +83,14 @@ func (c *MIoTClient) RequestRefreshProp(did string, siid, piid int) {
|
||||
c.refreshPropsTimer = nil
|
||||
c.mu.Unlock()
|
||||
|
||||
c.lgr.Infof("[MIoTClient] RequestRefreshProp: Timer fired, calling refreshPropsHandler()\n")
|
||||
c.lgr.Debugf("[MIoTClient] RequestRefreshProp: Timer fired, calling refreshPropsHandler()\n")
|
||||
c.refreshPropsHandler()
|
||||
})
|
||||
} else {
|
||||
c.lgr.Infof("[MIoTClient] RequestRefreshProp: Timer already scheduled, added to queue: %s\n", key)
|
||||
c.lgr.Debugf("[MIoTClient] RequestRefreshProp: Timer already scheduled, added to queue: %s\n", key)
|
||||
}
|
||||
|
||||
c.lgr.Infof("[MIoTClient] RequestRefreshProp: QUEUED %s (queue size: %d)\n", key, len(c.refreshPropsList))
|
||||
c.lgr.Debugf("[MIoTClient] RequestRefreshProp: QUEUED %s (queue size: %d)\n", key, len(c.refreshPropsList))
|
||||
}
|
||||
|
||||
// refreshPropsHandler handles property refresh from cloud.
|
||||
@@ -107,7 +110,7 @@ func (c *MIoTClient) refreshPropsHandler() {
|
||||
c.refreshPropsList = make(map[string]map[string]interface{})
|
||||
c.mu.Unlock()
|
||||
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: START, %d props to refresh\n", len(propsToRefresh))
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: START, %d props to refresh\n", len(propsToRefresh))
|
||||
|
||||
// Group props by source (cloud / gateway / lan)
|
||||
cloudParams := make([]PropParam, 0)
|
||||
@@ -192,7 +195,7 @@ func (c *MIoTClient) refreshPropsHandler() {
|
||||
return
|
||||
}
|
||||
allResults = append(allResults, results...)
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: cloud batch %d returned %d results\n", i/150+1, len(results))
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: cloud batch %d returned %d results\n", i/150+1, len(results))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +226,7 @@ func (c *MIoTClient) refreshPropsHandler() {
|
||||
continue
|
||||
}
|
||||
allResults = append(allResults, results...)
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: gateway %s returned %d results\n", groupID, len(results))
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: gateway %s returned %d results\n", groupID, len(results))
|
||||
}
|
||||
|
||||
// LAN source
|
||||
@@ -242,7 +245,7 @@ func (c *MIoTClient) refreshPropsHandler() {
|
||||
}
|
||||
} else {
|
||||
allResults = append(allResults, results...)
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: LAN returned %d results\n", len(results))
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: LAN returned %d results\n", len(results))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,16 +255,17 @@ func (c *MIoTClient) refreshPropsHandler() {
|
||||
c.refreshRetryCount = 0
|
||||
c.mu.Unlock()
|
||||
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: total %d results\n", len(allResults))
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: total %d results\n", len(allResults))
|
||||
|
||||
// Dispatch results to subscribers
|
||||
for _, result := range allResults {
|
||||
if result.Code != 0 {
|
||||
c.lgr.Errorf("[MIoTClient] refreshPropsHandler: FAILED %s/%d/%d, code=%d\n", result.DID, result.Siid, result.Piid, result.Code)
|
||||
c.lgr.Errorf("[MIoTClient] refreshPropsHandler: FAILED %s/%d/%d, code=%d (%s)\n",
|
||||
result.DID, result.Siid, result.Piid, result.Code, ErrDesc(result.Code))
|
||||
continue
|
||||
}
|
||||
|
||||
c.lgr.Infof("[MIoTClient] refreshPropsHandler: SUCCESS %s/%d/%d = %v\n", result.DID, result.Siid, result.Piid, result.Value)
|
||||
c.lgr.Debugf("[MIoTClient] refreshPropsHandler: SUCCESS %s/%d/%d = %v\n", result.DID, result.Siid, result.Piid, result.Value)
|
||||
params := map[string]interface{}{
|
||||
"did": result.DID,
|
||||
"siid": result.Siid,
|
||||
|
||||
+10
-10
@@ -25,7 +25,7 @@ func (c *MIoTClient) SubProp(did string, handler func(map[string]interface{}, in
|
||||
subID := fmt.Sprintf("%p", handler)
|
||||
c.subTree.Sub(topic, handler, nil)
|
||||
|
||||
c.lgr.Infof("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
c.lgr.Debugf("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
|
||||
c.mu.Unlock()
|
||||
|
||||
@@ -52,7 +52,7 @@ func (c *MIoTClient) UnsubProp(did string, subID string) {
|
||||
// TODO: Implement proper tracking of per-device subscription count
|
||||
// For now, just log
|
||||
|
||||
c.lgr.Infof("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
|
||||
c.lgr.Debugf("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
|
||||
}
|
||||
|
||||
// SubEvent subscribes to device events.
|
||||
@@ -69,7 +69,7 @@ func (c *MIoTClient) SubEvent(did string, handler func(map[string]interface{}, i
|
||||
subID := fmt.Sprintf("%p", handler)
|
||||
c.subTree.Sub(topic, handler, nil)
|
||||
|
||||
c.lgr.Infof("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
c.lgr.Debugf("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
|
||||
return subID
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func (c *MIoTClient) UnsubEvent(did string, subID string) {
|
||||
// Remove from subTree by subID
|
||||
c.subTree.Unsub(subID)
|
||||
|
||||
c.lgr.Infof("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
|
||||
c.lgr.Debugf("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
|
||||
}
|
||||
|
||||
// updateDeviceMsgSub updates device message subscription.
|
||||
@@ -260,7 +260,7 @@ func (c *MIoTClient) handleMIPSMessage(did string, payload []byte) {
|
||||
// Fallback: binary MIPS protocol (used by gateway mipsLocal)
|
||||
msg := UnpackMipsMessage(payload)
|
||||
if msg == nil || msg.Payload == "" {
|
||||
c.lgr.Infof("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ func (c *MIoTClient) dispatchMIPSMessage(did string, params map[string]interface
|
||||
} else if _, ok := params["eiid"]; ok {
|
||||
c.OnEventMsg(params, nil)
|
||||
} else {
|
||||
c.lgr.Infof("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
|
||||
// Fallback: binary MIPS protocol
|
||||
msg := UnpackMipsMessage(payload)
|
||||
if msg == nil || msg.Payload == "" {
|
||||
c.lgr.Infof("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal([]byte(msg.Payload), ¶ms); err != nil {
|
||||
@@ -326,7 +326,7 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
|
||||
}
|
||||
|
||||
if len(state) == 0 {
|
||||
c.lgr.Infof("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -372,7 +372,7 @@ func (c *MIoTClient) OnPropMsg(params map[string]interface{}, ctx interface{}) {
|
||||
}
|
||||
|
||||
if did == "" {
|
||||
c.lgr.Infof("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
|
||||
c.lgr.Debugf("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -424,7 +424,7 @@ func (c *MIoTClient) OnEventMsg(params map[string]interface{}, ctx interface{})
|
||||
}
|
||||
|
||||
if did == "" {
|
||||
c.lgr.Infof("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
|
||||
c.lgr.Debugf("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+29
-29
@@ -156,7 +156,7 @@ func newLanDevice(mgr *MIoTLan, did, tokenHex, ip string) (*LanDevice, error) {
|
||||
d.updateKeepAlive(LanDeviceDead)
|
||||
})
|
||||
|
||||
mgr.lgr.Infof("[lan] device added: did=%s", d.DID)
|
||||
mgr.lgr.Debugf("[lan] device added: did=%s", d.DID)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ func (d *LanDevice) KeepAlive(ip, ifName string) {
|
||||
d.IP = ip
|
||||
if d.IfName != ifName {
|
||||
d.IfName = ifName
|
||||
d.manager.lgr.Infof("[lan] device if_name changed: %s, did=%s", d.IfName, d.DID)
|
||||
d.manager.lgr.Debugf("[lan] device if_name changed: %s, did=%s", d.IfName, d.DID)
|
||||
}
|
||||
d.updateKeepAlive(LanDeviceFresh)
|
||||
}
|
||||
@@ -316,12 +316,12 @@ func (d *LanDevice) Subscribe() {
|
||||
func (d *LanDevice) subscribeHandler(msg map[string]interface{}, subTS int) {
|
||||
result, _ := msg["result"].(map[string]interface{})
|
||||
if result == nil {
|
||||
d.manager.lgr.Infof("[lan] subscribe error: no result, did=%s, msg=%v", d.DID, msg)
|
||||
d.manager.lgr.Warnf("[lan] subscribe error: no result, did=%s, msg=%v", d.DID, msg)
|
||||
return
|
||||
}
|
||||
code, _ := result["code"].(float64)
|
||||
if code != 0 {
|
||||
d.manager.lgr.Infof("[lan] subscribe error: code=%v, did=%s, msg=%v", code, d.DID, msg)
|
||||
d.manager.lgr.Warnf("[lan] subscribe error: code=%v, did=%s, msg=%v", code, d.DID, msg)
|
||||
return
|
||||
}
|
||||
d.subscribed = true
|
||||
@@ -330,7 +330,7 @@ func (d *LanDevice) subscribeHandler(msg map[string]interface{}, subTS int) {
|
||||
"online": d.online,
|
||||
"push_available": d.subscribed,
|
||||
})
|
||||
d.manager.lgr.Infof("[lan] subscribe success: if=%s, did=%s", d.IfName, d.DID)
|
||||
d.manager.lgr.Debugf("[lan] subscribe success: if=%s, did=%s", d.IfName, d.DID)
|
||||
}
|
||||
|
||||
// Unsubscribe sends a miIO.unsub request to the device.
|
||||
@@ -351,11 +351,11 @@ func (d *LanDevice) Unsubscribe() {
|
||||
result, _ := msg["result"].(map[string]interface{})
|
||||
if result != nil {
|
||||
if code, _ := result["code"].(float64); code != 0 {
|
||||
d.manager.lgr.Infof("[lan] unsubscribe error: code=%v, did=%s, msg=%v", code, d.DID, msg)
|
||||
d.manager.lgr.Warnf("[lan] unsubscribe error: code=%v, did=%s, msg=%v", code, d.DID, msg)
|
||||
return
|
||||
}
|
||||
}
|
||||
d.manager.lgr.Infof("[lan] unsubscribe success: if=%s, did=%s", d.IfName, d.DID)
|
||||
d.manager.lgr.Debugf("[lan] unsubscribe success: if=%s, did=%s", d.IfName, d.DID)
|
||||
}, nil, 5000)
|
||||
d.subscribed = false
|
||||
d.manager.broadcastDeviceState(d.DID, map[string]interface{}{
|
||||
@@ -374,7 +374,7 @@ func (d *LanDevice) OnDelete() {
|
||||
d.unstableTimer.Stop()
|
||||
d.unstableTimer = nil
|
||||
}
|
||||
d.manager.lgr.Infof("[lan] device deleted: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] device deleted: did=%s", d.DID)
|
||||
}
|
||||
|
||||
// UpdateInfo updates the device token if it has changed.
|
||||
@@ -396,12 +396,12 @@ func (d *LanDevice) UpdateInfo(info map[string]interface{}) {
|
||||
aesIV := md5Sum(append(aesKey[:], d.Token...))
|
||||
block, err := aes.NewCipher(aesKey[:])
|
||||
if err != nil {
|
||||
d.manager.lgr.Infof("[lan] update token cipher error: %v, did=%s", err, d.DID)
|
||||
d.manager.lgr.Warnf("[lan] update token cipher error: %v, did=%s", err, d.DID)
|
||||
return
|
||||
}
|
||||
d.cipher = block
|
||||
d.aesIV = aesIV[:]
|
||||
d.manager.lgr.Infof("[lan] token updated: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] token updated: did=%s", d.DID)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -413,7 +413,7 @@ func (d *LanDevice) updateKeepAlive(state LanDeviceState) {
|
||||
d.state = state
|
||||
|
||||
if d.state != LanDeviceFresh {
|
||||
d.manager.lgr.Infof("[lan] device status: did=%s, state=%s", d.DID, d.state)
|
||||
d.manager.lgr.Debugf("[lan] device status: did=%s, state=%s", d.DID, d.state)
|
||||
}
|
||||
|
||||
// Cancel existing timer
|
||||
@@ -441,11 +441,11 @@ func (d *LanDevice) updateKeepAlive(state LanDeviceState) {
|
||||
})
|
||||
// Fast ping
|
||||
if d.IfName == "" {
|
||||
d.manager.lgr.Infof("[lan] if_name not set for device: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] if_name not set for device: did=%s", d.DID)
|
||||
return
|
||||
}
|
||||
if d.IP == "" {
|
||||
d.manager.lgr.Infof("[lan] ip not set for device: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] ip not set for device: did=%s", d.DID)
|
||||
return
|
||||
}
|
||||
d.manager.ping(d.IfName, d.IP)
|
||||
@@ -464,7 +464,7 @@ func (d *LanDevice) nextKATimeout() float64 {
|
||||
}
|
||||
|
||||
func (d *LanDevice) changeOnline(online bool) {
|
||||
d.manager.lgr.Infof("[lan] change online: did=%s, online=%v", d.DID, online)
|
||||
d.manager.lgr.Debugf("[lan] change online: did=%s, online=%v", d.DID, online)
|
||||
tsNow := int(time.Now().Unix())
|
||||
d.onlineHistory = append(d.onlineHistory, onlineEvent{ts: tsNow, online: online})
|
||||
if len(d.onlineHistory) > networkUnstableCntTh {
|
||||
@@ -483,9 +483,9 @@ func (d *LanDevice) changeOnline(online bool) {
|
||||
float64(tsNow-d.onlineHistory[0].ts) > networkUnstableTimeTh {
|
||||
d.setOnline(true)
|
||||
} else {
|
||||
d.manager.lgr.Infof("[lan] unstable device detected: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] unstable device detected: did=%s", d.DID)
|
||||
d.unstableTimer = time.AfterFunc(time.Duration(networkUnstableResumeTh*float64(time.Second)), func() {
|
||||
d.manager.lgr.Infof("[lan] unstable resume threshold passed: did=%s", d.DID)
|
||||
d.manager.lgr.Debugf("[lan] unstable resume threshold passed: did=%s", d.DID)
|
||||
d.setOnline(true)
|
||||
})
|
||||
}
|
||||
@@ -872,7 +872,7 @@ func (m *MIoTLan) createSocketLocked(ifName string) {
|
||||
|
||||
conn, err := createBoundSocket(info.IP, m.localPort)
|
||||
if err != nil {
|
||||
m.lgr.Infof("[lan] create socket error: if=%s, err=%v", ifName, err)
|
||||
m.lgr.Warnf("[lan] create socket error: if=%s, err=%v", ifName, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -944,7 +944,7 @@ func (m *MIoTLan) send(ifName, targetIP string, data []byte) {
|
||||
} else {
|
||||
conn, ok := m.socks[ifName]
|
||||
if !ok {
|
||||
m.lgr.Infof("[lan] invalid socket: if=%s", ifName)
|
||||
m.lgr.Debugf("[lan] invalid socket: if=%s", ifName)
|
||||
return
|
||||
}
|
||||
conn.WriteToUDP(data, &net.UDPAddr{IP: net.ParseIP(targetIP), Port: otPort})
|
||||
@@ -1118,7 +1118,7 @@ func (m *MIoTLan) handleRawMessage(data []byte, dataLen int, ip, ifName string)
|
||||
if dataLen > otProbeLen {
|
||||
msg, err := dev.DecryptPacket(data[:dataLen])
|
||||
if err != nil {
|
||||
m.lgr.Infof("[lan] decrypt error: did=%s, err=%v", did, err)
|
||||
m.lgr.Debugf("[lan] decrypt error: did=%s, err=%v", did, err)
|
||||
return
|
||||
}
|
||||
m.handleMessage(did, msg)
|
||||
@@ -1128,7 +1128,7 @@ func (m *MIoTLan) handleRawMessage(data []byte, dataLen int, ip, ifName string)
|
||||
func (m *MIoTLan) handleMessage(did string, msg map[string]interface{}) {
|
||||
msgID, ok := msg["id"]
|
||||
if !ok {
|
||||
m.lgr.Infof("[lan] message without id: did=%s, msg=%v", did, msg)
|
||||
m.lgr.Debugf("[lan] message without id: did=%s, msg=%v", did, msg)
|
||||
return
|
||||
}
|
||||
id, ok := toInt(msgID)
|
||||
@@ -1169,7 +1169,7 @@ func (m *MIoTLan) handleMessage(did string, msg map[string]interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
m.lgr.Infof("[lan] message: did=%s, method=%s", did, method)
|
||||
m.lgr.Debugf("[lan] message: did=%s, method=%s", did, method)
|
||||
|
||||
switch method {
|
||||
case "properties_changed":
|
||||
@@ -1264,7 +1264,7 @@ func (m *MIoTLan) scanDevices() {
|
||||
|
||||
scanTime := m.nextScanTime()
|
||||
m.scanTimer = time.AfterFunc(time.Duration(scanTime*float64(time.Second)), m.scanDevices)
|
||||
m.lgr.Infof("[lan] next scan in %.0fs", scanTime)
|
||||
m.lgr.Debugf("[lan] next scan in %.0fs", scanTime)
|
||||
}
|
||||
|
||||
func (m *MIoTLan) nextScanTime() float64 {
|
||||
@@ -1411,7 +1411,7 @@ func (m *MIoTLan) UpdateDevices(devices map[string]map[string]interface{}) {
|
||||
}
|
||||
model, _ := info["model"].(string)
|
||||
if model != "" && m.profileModels[model] {
|
||||
m.lgr.Infof("[lan] model not supported for LAN ctrl: did=%s, model=%s", did, model)
|
||||
m.lgr.Debugf("[lan] model not supported for LAN ctrl: did=%s, model=%s", did, model)
|
||||
continue
|
||||
}
|
||||
if existing, ok := m.devices[did]; ok {
|
||||
@@ -1419,13 +1419,13 @@ func (m *MIoTLan) UpdateDevices(devices map[string]map[string]interface{}) {
|
||||
} else {
|
||||
tokenStr, _ := info["token"].(string)
|
||||
if len(tokenStr) != 32 {
|
||||
m.lgr.Infof("[lan] invalid token for device: did=%s", did)
|
||||
m.lgr.Debugf("[lan] invalid token for device: did=%s", did)
|
||||
continue
|
||||
}
|
||||
ip, _ := info["ip"].(string)
|
||||
dev, err := newLanDevice(m, did, tokenStr, ip)
|
||||
if err != nil {
|
||||
m.lgr.Infof("[lan] create device error: did=%s, err=%v", did, err)
|
||||
m.lgr.Warnf("[lan] create device error: did=%s, err=%v", did, err)
|
||||
continue
|
||||
}
|
||||
m.devices[did] = dev
|
||||
@@ -1491,7 +1491,7 @@ func (m *MIoTLan) SubProp(did string, handler func(map[string]interface{}, inter
|
||||
}
|
||||
m.cmdCh <- func() {
|
||||
m.matcher.Sub(key, handler, ctx)
|
||||
m.lgr.Infof("[lan] sub prop: key=%s", key)
|
||||
m.lgr.Debugf("[lan] sub prop: key=%s", key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1503,7 +1503,7 @@ func (m *MIoTLan) UnsubProp(did string, siid, piid int) {
|
||||
}
|
||||
m.cmdCh <- func() {
|
||||
m.matcher.Unsub(key)
|
||||
m.lgr.Infof("[lan] unsub prop: key=%s", key)
|
||||
m.lgr.Debugf("[lan] unsub prop: key=%s", key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1515,7 +1515,7 @@ func (m *MIoTLan) SubEvent(did string, handler func(map[string]interface{}, inte
|
||||
}
|
||||
m.cmdCh <- func() {
|
||||
m.matcher.Sub(key, handler, ctx)
|
||||
m.lgr.Infof("[lan] sub event: key=%s", key)
|
||||
m.lgr.Debugf("[lan] sub event: key=%s", key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1527,7 +1527,7 @@ func (m *MIoTLan) UnsubEvent(did string, siid, eiid int) {
|
||||
}
|
||||
m.cmdCh <- func() {
|
||||
m.matcher.Unsub(key)
|
||||
m.lgr.Infof("[lan] unsub event: key=%s", key)
|
||||
m.lgr.Debugf("[lan] unsub event: key=%s", key)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -289,7 +289,7 @@ func (s *MipsService) runBrowser() {
|
||||
// Run the blocking Query in a goroutine so we can detect stop.
|
||||
go func() {
|
||||
if err := mdns.Query(params); err != nil {
|
||||
s.lgr.Infof("[mdns] query error: %v", err)
|
||||
s.lgr.Debugf("[mdns] query error: %v", err)
|
||||
}
|
||||
close(entriesCh)
|
||||
}()
|
||||
@@ -311,7 +311,7 @@ func (s *MipsService) runBrowser() {
|
||||
func (s *MipsService) handleEntry(entry *mdns.ServiceEntry) {
|
||||
svc, err := entryToServiceData(entry)
|
||||
if err != nil {
|
||||
s.lgr.Infof("[mdns] skip entry: %v", err)
|
||||
s.lgr.Debugf("[mdns] skip entry: %v", err)
|
||||
return
|
||||
}
|
||||
if !svc.validService() {
|
||||
|
||||
@@ -530,7 +530,7 @@ func (n *MIoTNetwork) getNetworkInterfaces() map[string]*NetworkInfo {
|
||||
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
n.lgr.Infof("[network] get interfaces error: %v", err)
|
||||
n.lgr.Warnf("[network] get interfaces error: %v", err)
|
||||
return results
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user