之前说到,天猫的协议调整了导致发现不了设备,我这边修改了一下平台的代码,统一了天猫发现设备的协议格式;
type TmDeviceV2 struct {
DeviceId string `json:"deviceId"`
DeviceName string `json:"deviceName"`
DeviceType string `json:"deviceType"`
Brand string `json:"brand"`
Model string `json:"model"`
Zone string `json:"zone"`
Icon string `json:"icon"`
Properties []map[string]interface{} `json:"properties"`
Actions []string `json:"actions"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
func NewTmDeviceDiscoveryResp(messageId string, devices []TmDevice) Common {
var newDevs = make([]TmDeviceV2, 0)
for _, device := range devices {
var properties = make([]map[string]interface{}, 0)
var actions = make([]string, 0)
for _, productMap := range device.VoiceProduct.FunctionMap {
p := map[string]interface{}{
"name": productMap.VoiceCode,
"value": device.Status[productMap.VoiceCode],
}
//INT、DOUBLE、TEXT、ENUM、BOOL
switch productMap.VDataType {
case "INT", "DOUBLE":
actions = append(actions, fmt.Sprintf("Set%v", productMap.VoiceCode))
actions = append(actions, fmt.Sprintf("Adjust%v", productMap.VoiceCode))
case "TEXT", "ENUM":
actions = append(actions, fmt.Sprintf("Set%v", productMap.VoiceCode))
case "BOOL":
actions = append(actions, fmt.Sprintf("TurnOn"))
actions = append(actions, fmt.Sprintf("TurnOff"))
if iotutil.ToString(p["value"]) == "true" {
p["value"] = "on"
} else {
p["value"] = "off"
}
}
//productMap.VoiceCode
properties = append(properties, p)
}
actions = append(actions, fmt.Sprintf("%v", "Query"))
actions = iotutil.RemoveRepeatElement(actions)
newDevs = append(newDevs, TmDeviceV2{
DeviceId: device.DeviceId,
DeviceName: device.DeviceName,
DeviceType: device.DeviceType,
Brand: device.Brand,
Model: device.Model,
Zone: device.Zone,
Icon: device.Icon,
Properties: properties,
Actions: actions,
})
}
iotlogger.LogHelper.Errorf("NewTmDeviceDiscoveryResp: %v", iotutil.ToString(newDevs))
return Common{
Header: Header{
Namespace: TmIotDeviceDiscovery,
Name: TmIotDeviceDiscoveryResp,
MessageId: messageId,
PayLoadVersion: 2,
},
Payload: DevicesRespPayload{
Devices: newDevs,
},
}
}
另外需要注意的是平台他对天猫的配置参数需要注意;