设备建立 MQTT 连接后,可以根据场景需要进行物模型通信。
更多信息,请参考样例程序examples/iot/test_tm_property.c。
void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) { switch (recv->type) { // 客户端上报属性后,服务端给的回调 case IOT_TM_RECV_PROPERTY_SET_POST_REPLY: { DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t property_set id = %s, code = %d", recv->data.property_set_post_reply.msg_id, recv->data.property_set_post_reply.code); } break; } } // 初始化物模型模块 dm = iot_tm_init(); iot_tm_set_mqtt_handler(dm, mqtt_ctx); // 设置物模型属性上报回调 iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm); // 构建物模型消息对象 iot_tm_msg_t dm_msg2 = {0}; dm_msg2.type = IOT_TM_MSG_PROPERTY_POST; iot_tm_msg_property_post_t *property_post; iot_property_post_init(&property_post); iot_property_post_add_param_num(property_post, "default:ErrorCurrentThreshold", 0.8); iot_property_post_add_param_num(property_post, "default:OverVoltThreshold", 1); iot_property_post_add_param_json_str(property_post, "default:GeoLocation", "{\"altitude\":0,\"latitude\":-90,\"longitude\":-180}"); dm_msg2.data.property_post = property_post; // 发送消息 iot_tm_send(dm, &dm_msg2); // 释放内存 iot_property_post_free(property_post);
更多信息,请参考样例程序examples/iot/test_tm_property_set.c。
void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) { switch (recv->type) { // 服务器下发的属性设置消息 case IOT_TM_RECV_PROPERTY_SET: { DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t property_set.msg_id = %s service_call.params_json_str = %s", recv->data.property_set.msg_id, recv->data.property_set.params); // 接收服务端设置的属性之后需要给服务端发送回复收到 iot_tm_msg_t property_set_post_reply_msg = {0}; property_set_post_reply_msg.type = IOT_TM_MSG_PROPERTY_SET_REPLY; iot_tm_msg_property_set_post_reply_t *reply; iot_property_set_post_reply_init(&reply, recv->data.property_set.msg_id, 0); property_set_post_reply_msg.data.property_set_post_reply = reply; iot_tm_send(dm, &property_set_post_reply_msg); iot_property_set_post_reply_free(reply); } break; } } // 初始化物模型模块 dm = iot_tm_init(); iot_tm_set_mqtt_handler(dm, mqtt_ctx); // 设置物模型属性下发回调 iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm);
更多信息,请参考样例程序examples/iot/test_tm_event.c。
void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) { tm_handle_t *dm_handle = (tm_handle_t *) handler; switch (recv->type) { // 事件上报后 服务端给的回复 case IOT_TM_RECV_EVENT_POST_REPLY: { DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t property_set id = %s, code = %d module_key = %s, identifier = %s", recv->data.event_post_replay.msg_id, recv->data.event_post_replay.code, recv->data.event_post_replay.moudle_key, recv->data.event_post_replay.identifier); } break; } } // 初始化物模型模块 dm = iot_tm_init(); iot_tm_set_mqtt_handler(dm, mqtt_ctx); // 设置物模型事件上报回调 iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm); // 构造发生数据 iot_tm_msg_t dm_msg_test_event_num = {0}; dm_msg_test_event_num.type = IOT_TM_MSG_EVENT_POST; // 创建event数据 iot_tm_msg_event_post_t *event_test_num_post; iot_tm_msg_event_post_init(&event_test_num_post, "test_event", "event_test_num"); // 添加Num参数 iot_tm_msg_event_post_param_add_num(event_test_num_post, "test_num", 12); dm_msg_test_event_num.data.event_post = event_test_num_post; // 发送数据 ret = iot_tm_send(dm, &dm_msg_test_event_num); if (ret != CODE_SUCCESS) { DEVICE_LOGD("test_mqtt", "iot_tm_send error ret =%d ", ret); } // 释放内存 iot_tm_msg_event_post_free(event_test_num_post);
更多信息,请参考样例程序examples/iot/test_tm_service.c。
void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) { tm_handle_t *dm_handle = (tm_handle_t *) handler; switch (recv->type) { // 服务端下发的服务调用 case IOT_TM_RECV_SERVICE_CALL: { DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t service_call.uuid = %s service_call.params_json_str = %s", recv->data.service_call.topic_uuid, recv->data.service_call.params_json_str); // 需要判断对应的是哪个service然后做处理 if (strcmp("test_service_syn", recv->data.service_call.identifier) == 0) { // 收到之后 回复给服务端 iot_tm_msg_t service_reply_msg = {0}; service_reply_msg.type = IOT_TM_MSG_SERVICE_CALL_REPLY; iot_tm_msg_service_call_reply_t *reply; iot_tm_msg_service_call_reply_init(&reply, recv->data.service_call.module_key, recv->data.service_call.identifier, recv->data.service_call.topic_uuid, recv->data.service_call.msg_id, 0); iot_tm_msg_service_call_reply_set_prams_json_str(reply, "{\"test_num_out\":22}"); service_reply_msg.data.service_call_reply = reply; iot_tm_send(dm, &service_reply_msg); iot_tm_msg_service_call_reply_free(reply); } else if (strcmp("test_service_asyn", recv->data.service_call.identifier) == 0) { // 收到之后回复给服务端 iot_tm_msg_t service_reply_msg = {0}; service_reply_msg.type = IOT_TM_MSG_SERVICE_CALL_REPLY; iot_tm_msg_service_call_reply_t *reply; iot_tm_msg_service_call_reply_init(&reply, recv->data.service_call.module_key, recv->data.service_call.identifier, recv->data.service_call.topic_uuid, recv->data.service_call.msg_id, 0); iot_tm_msg_service_call_reply_set_prams_json_str(reply, "{\"test_string_out\":\"asdas\"}"); service_reply_msg.data.service_call_reply = reply; iot_tm_send(dm, &service_reply_msg); iot_tm_msg_service_call_reply_free(reply); } } break; } } // 初始化物模型模块 dm = iot_tm_init(); iot_tm_set_mqtt_handler(dm, mqtt_ctx); // 设置物模型服务调用回调 iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm);
更多信息,请参考样例程序examples/iot/test_tm_custom_topic.c。
// 监听自定义topic的数据 void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) { tm_handle_t *dm_handle = (tm_handle_t *) handler; switch (recv->type) { case IOT_TM_RECV_CUSTOM_TOPIC: { DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t custom_topic = %s data = %s", recv->data.custom_topic.custom_topic_suffix, recv->data.custom_topic.params_json_str); } break; } } // 初始化物模型模块 dm = iot_tm_init(); iot_tm_set_mqtt_handler(dm, mqtt_ctx); iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm); // 订阅自定义topic tm_sub_custom_topic(dm, "test_custom_topic"); tm_sub_custom_topic(dm, "test_topic_2"); // 发送自定义topic消息 iot_tm_msg_t dm_msg = {0}; dm_msg.type = IOT_TM_MSG_CUSTOM_TOPIC; iot_tm_msg_custom_topic_post_t *custom_topic_post; iot_tm_msg_aiot_tm_msg_custom_topic_post_init(&custom_topic_post, "test_custom_topic", "{\"test_bool\":1}"); dm_msg.data.custom_topic_post = custom_topic_post; iot_tm_send(dm, &dm_msg); iot_tm_msg_aiot_tm_msg_custom_topic_post_free(custom_topic_post);