物模型通信
使用示例
建立回调类(method)示例:
建立连接后,收到对应数据后会触发下述定义的回调函数。 请根据业务需要处理。
type ReceiveDataHandle struct {
}
func NewReceiveDataHandle() *ReceiveDataHandle {
return &ReceiveDataHandle{}
}
//处理心跳回复 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceiveHeartResp(client *comm.Client, productId, deviceId string, data *comm.DasHeartResp) {
//获取到额外自定义参数示例。 结果请自行断言。
_ = client.GetConnExtraParam()
fmt.Println("HandleReceiveHeartResp. ")
}
//处理时间校准回复 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceiveNtpResp(client *comm.Client, productId, deviceId string, data *comm.DasNTPResp) {
fmt.Println("HandleReceiveNtpResp. ")
}
//处理属性上报回复 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceivePropertiesUpResp(client *comm.Client, productId, deviceId string, data *comm.DasDevicePropertiesUpResponse) {
fmt.Println("HandleReceivePropertiesUpResp. ")
}
//处理属性批量上报回复 productId, deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceivePropertiesUpBatchResp(client *comm.Client, productId, deviceId string, data *comm.DasDevicePropertiesUpBatchResponse) {
fmt.Println("HandleReceivePropertiesUpBatchResp. ")
}
//处理属性全量查询请求 productId, deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceivePropertiesGetAll(client *comm.Client, productId, deviceId string, data *comm.DasDevicePropertiesGetAll) {
fmt.Println("HandleReceivePropertiesGetAll. ")
}
//处理属性查询请求 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceivePropertiesGet(client *comm.Client, productId, deviceId string, data *comm.DasDevicePropertiesGet) {
fmt.Println("HandleReceivePropertiesGet. ")
}
//处理属性写值请求 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceivePropertiesSet(client *comm.Client, productId, deviceId string, data *comm.DasDevicePropertiesSet) {
fmt.Println("HandleReceivePropertiesSet. ")
}
//处理服务下发请求 productId,deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceiveServiceSet(client *comm.Client, productId, deviceId string, data *comm.DasDeviceServiceSet) {
fmt.Println("HandleReceiveServiceSet. ")
}
//处理设备状态全量查询请求 productId, deviceId 为topic中id
func (p *ReceiveDataHandle) HandleReceiveDeviceStatusGetAll(client *comm.Client, productId, deviceId string, data *comm.DasDeviceStatusGetAll) {
fmt.Println("HandleReceiveDeviceStatusGetAll. ")
}
发送各类型数据示例:
//发送心跳. 填自身的产品id和设备id
err = client.SendHeart("productId", "deviceId")
if err != nil {
t.Errorf("err: %s", err.Error())
}
//发送时间校准. 填自身的产品id和设备id
err = client.SendNtp("productId", "deviceId")
if err != nil {
t.Errorf("err: %s", err.Error())
}
//属性上报. 填自身的产品id和设备id和数据
err = client.SendPropertiesUp("productId", "deviceId", &comm.DasDevicePropertiesUp{
DasProjectServiceMqttReq: comm.DasProjectServiceMqttReq{
Id: "", //这边生成唯一id
Version: comm.DAS_PROJECT_AIOT_VERSION,
Method: comm.DAS_DEVICE_PROPERTIES_UP_METHOD,
},
Sys: nil, //这边填自身业务数据
Params: nil, //这边填自身业务数据
})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//属性上报批量. 填自身的产品id和设备id和数据
err = client.SendPropertiesUpBatch("productId", "deviceId", &comm.DasDevicePropertiesUpBatch{
DasProjectServiceMqttReq: comm.DasProjectServiceMqttReq{
Id: "", //这边生成唯一id
Version: comm.DAS_PROJECT_AIOT_VERSION,
Method: comm.DAS_DEVICE_PROPERTIES_UP_BATCH_METHOD,
},
Sys: nil, //这边填自身业务数据
Params: nil, //这边填自身业务数据
})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//属性写值回复. 填自身的产品id和设备id和数据
err = client.SendPropertiesSetResponse("productId", "deviceId", &comm.DasDevicePropertiesSetResponse{DasProjectServiceMqttResponse: comm.DasProjectServiceMqttResponse{
Code: comm.RESPONSE_CODE_SUCCESS,
Data: make(map[string]interface{}),
Id: "", //这边填收到的包中的id
Message: comm.RESPONSE_CODE_SUCCESS_MESSAGE,
Method: comm.DAS_DEVICE_PROPERTIES_SET_METHOD,
Version: comm.DAS_PROJECT_AIOT_VERSION,
}})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//属性查询回复. 填自身的产品id和设备id和数据
err = client.SendPropertiesGetResponse("productId", "deviceId", &comm.DasDevicePropertiesGetResponse{DasProjectServiceMqttResponse: comm.DasProjectServiceMqttResponse{
Code: comm.RESPONSE_CODE_SUCCESS,
Data: nil, //这边填写属性值
Id: "", //这边填收到的包中的id
Message: comm.RESPONSE_CODE_SUCCESS_MESSAGE,
Method: comm.DAS_DEVICE_PROPERTIES_GET_METHOD,
Version: comm.DAS_PROJECT_AIOT_VERSION,
}})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//事件上报. 填自身的产品id和设备id和数据
err = client.SendEventUp("productId", "deviceId", &comm.DasDeviceEventUp{
DasProjectServiceMqttReq: comm.DasProjectServiceMqttReq{
Id: "", //这边生成唯一id
Version: comm.DAS_PROJECT_AIOT_VERSION,
Method: comm.DAS_DEVICE_EVENT_UP_METHOD,
},
Sys: nil, //这边填自身业务数据
Identifier: "", //这边填自身业务数据
Params: nil, //这边填自身业务数据
})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//服务下发回复. 填自身的产品id和设备id和数据
err = client.SendServiceSetResponse("productId", "deviceId", &comm.DasDeviceServiceSetResponse{
DasProjectServiceMqttResponse: comm.DasProjectServiceMqttResponse{
Code: comm.RESPONSE_CODE_SUCCESS,
Data: nil, //这边填写属性值
Id: "", //这边填收到的包中的id
Message: comm.RESPONSE_CODE_SUCCESS_MESSAGE,
Method: comm.DAS_DEVICE_SERVER_SET_METHOD,
Version: comm.DAS_PROJECT_AIOT_VERSION,
},
Identifier: "",
})
if err != nil {
t.Errorf("err: %s", err.Error())
}
//设备在线状态上报. 填自身的产品id和设备id和数据
err = client.SendDeviceStatusUp("productId", "deviceId", &comm.DasDeviceStatusUp{
Id: "",
Time: 0,
Devices: nil,
})
if err != nil {
t.Errorf("err: %s", err.Error())
}
最后修改时间: a year ago