物的模型服务

更新时间:2018-05-24 23:45:42

linkkit_set_value

定义描述

函数原型 int linkkit_set_value(linkkit_method_set_t method_set, const void* thing_id, const char* identifier, const void* value, const char* value_str)
描述 根据identifier设置物对象的 TSL 属性,如果标识符为struct类型、event output类型或者service output类型,使用点'.'分隔字段;例如"identifier1.identifier2"指向特定的项

请求参数

参数
类型
必填
描述
备注
method_set
linkkit_method_set_t
设置 TSL 属性的类型
linkkit_method_set_property_value:
设置物模型property value
linkkit_method_set_event_output_value:
设置物模型event output value
linkkit_method_set_service_output_value:
设置物模型 service output value
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得 linkkit 创建的 thing id
identifier
const char*
TSL 属性标示符
物对象 TSL 属性的标识符
value
const void*
输入的属性值
和 value_str, 二选一必填
value_str
const char*
输入的字符串格式的属性值
和 value, 二选一必填

示例

static int post_fault_alert(sample_context_t* sample)
{
    char event_output_identifier[64];
    snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s", EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);

    int errorCode = 0;

    //设置事件输出属性值
    linkkit_set_value(linkkit_method_set_event_output_value,
                      sample->thing,
                      event_output_identifier,
                      &errorCode, NULL);
    //将EVENT_ERROR_IDENTIFIER事件上报给云端
    return linkkit_trigger_event(sample->thing, EVENT_ERROR_IDENTIFIER, NULL);
}

返回值

详细描述
0 物对象 TSL 属性设置成功
-1 物对象 TSL 属性设置失败

linkkit_get_value

定义描述

函数原型 int linkkit_get_value(linkkit_method_get_t method_get, const void* thing_id, const char* identifier, void* value, char** value_str)
描述 根据identifier获取物对象的 TSL 属性

请求参数

参数
类型
必填
描述
备注
method_get
linkkit_method_get_t
获取的 TSL 属性的类型
linkkit_method_get_property_value:
获取物模型 property value
linkkit_method_get_event_output_value:
获取物模型 event output value
linkkit_method_get_service_input_value:
获取物模型 service input value
linkkit_method_get_service_output_value:
获取物模型 service output value
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得linkkit 创建的thing id
identifier
const char*
TSL 属性标识
物对象 TSL 属性的标识符

返回参数

名称 类型 描述
value void* 获取的属性值(和 value_str, 二选一必填)
value_str char** 字符串类型的 value (和 value, 二选一必填)

示例

static int thing_prop_changed(void* thing_id, char* property, void* ctx)
{
    char* value_str = NULL;
    char property_buf[64] = {0};

    /* get new property value */

    linkkit_get_value(linkkit_method_get_property_value, thing_id, property, NULL, &value_str);

    LINKKIT_PRINTF("#### property(%s) new value set: %s ####\n", property, value_str);

    /* do user's process logical here. */
    linkkit_trigger_event(thing_id, EVENT_PROPERTY_POST_IDENTIFIER, property);

    return 0;
}

返回值

详细描述
0 TSL 属性获取成功
-1 TSL 属性获取失败

linkkit_set_tsl

定义描述

函数原型 void* linkkit_set_tsl(const char* tsl, int tsl_len)
描述 从本地读取 TSL 文件,生成物的对象并添加到 linkkit 中

请求参数

参数
类型
必填
描述
备注
tsl
const char*
设备描述信息( JSON 类型)
tsl 必须和设备三元组所属的产品的 Alink JSON 格式协议相同
tsl_len
int
设备描述信息长度

示例

#define TSL_STRING "{\"schema\":\"http://aliyun/iot/thing/desc/schema\",\"profile\":{\"productKey\":\"a1nmfrdo3MI\",\"deviceName\":\"light_demo_for_ilop_device_test\"},\"link\":\"/sys/a1nmfrdo3MI/light_demo_for_ilop_device_test/thing/\",\"services\":[{\"outputData\":[],\"identifier\":\"set\",\"inputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"}],\"method\":\"thing.service.property.set\",\"name\":\"set\",\"required\":true,\"callType\":\"sync\",\"desc\":\"属性设置\"},{\"outputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"}],\"identifier\":\"get\",\"inputData\":[\"LightSwitch\",\"WorkMode\"],\"method\":\"thing.service.property.get\",\"name\":\"get\",\"required\":true,\"callType\":\"sync\",\"desc\":\"属性获取\"},{\"outputData\":[],\"identifier\":\"SetLightSwitchTimer\",\"inputData\":[{\"identifier\":\"Timer\",\"dataType\":{\"specs\":{\"unit\":\"min\",\"min\":\"0\",\"unitName\":\"分钟\",\"max\":\"1440\"},\"type\":\"int\"},\"name\":\"计时器\"},{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"}],\"method\":\"thing.service.SetLightSwitchTimer\",\"name\":\"设置主灯开关倒计时\",\"required\":true,\"callType\":\"async\"},{\"outputData\":[],\"identifier\":\"StopCountdown\",\"inputData\":[],\"method\":\"thing.service.StopCountdown\",\"name\":\"停止倒计时\",\"required\":true,\"callType\":\"async\"},{\"outputData\":[],\"identifier\":\"FlipLightSwitch\",\"inputData\":[],\"method\":\"thing.service.FlipLightSwitch\",\"name\":\"翻转主灯开关\",\"required\":true,\"callType\":\"async\"}],\"properties\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\",\"accessMode\":\"rw\",\"required\":true},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\",\"accessMode\":\"rw\",\"required\":true}],\"events\":[{\"outputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"}],\"identifier\":\"post\",\"method\":\"thing.event.property.post\",\"name\":\"post\",\"type\":\"info\",\"required\":true,\"desc\":\"属性上报\"},{\"outputData\":[{\"identifier\":\"errorCode\",\"dataType\":{\"specs\":{\"0\":\"正常\"},\"type\":\"enum\"},\"name\":\"故障代码\"}],\"identifier\":\"Error\",\"method\":\"thing.event.Error.post\",\"name\":\"故障上报\",\"type\":\"info\",\"required\":true}]}"

linkkit_set_tsl(TSL_STRING, strlen(TSL_STRING));

返回值

详细描述
NULL 失败
other 物的唯一标识符

linkkit_answer_service

定义描述

函数原型
#ifdef RRPC_ENABLED
int linkkit_answer_service(const void* thing_id, const char* service_identifier, int response_id, int code, int rrpc);
#else
int linkkit_answer_service(const void* thing_id, const char* service_identifier, int response_id, int code);
#endif /* RRPC_ENABLED */
描述
对云端服务请求进行回应

请求参数

参数
类型
必填
描述
备注
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得 linkkit 创建的 thing id
service_identifer
const char*
服务标识符
用户应该从 linkkit 的回调函数获得此标识符,使用此函数生成回复码发送给服务端
response_id
int
回复 ID
response_id 应该从linkkit 回调函数中获得此参数
code
int
回复状态码
200: 成功
400: 失败
[rrpc]
int
rrpc服务
0:普通服务调用
非0:rrpc服务调用
宏定义RRPC_ENABLED此参数有效

示例

#ifdef RRPC_ENABLED
static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id, int rrpc)
#else
static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id)
#endif /* RRPC_ENABLED */
{
    char identifier[128] = {0};

    /*
     * get iutput value.
     */
    snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "transparency");
    linkkit_get_value(linkkit_method_get_service_input_value, thing, identifier, &sample->service_custom_input_transparency, NULL);

    /*
     * set output value according to user's process result.
     */

    snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "Contrastratio");

    sample->service_custom_output_contrastratio = sample->service_custom_input_transparency >= 0 ? sample->service_custom_input_transparency : sample->service_custom_input_transparency * -1;

    linkkit_set_value(linkkit_method_set_service_output_value, thing, identifier, &sample->service_custom_output_contrastratio, NULL);
#ifdef RRPC_ENABLED
    linkkit_answer_service(thing, service_identifier, request_id, 200, rrpc);
#else
    linkkit_answer_service(thing, service_identifier, request_id, 200);
#endif /* RRPC_ENABLED */

    return 0;
}
#ifdef RRPC_ENABLED
static int thing_call_service(void* thing_id, char* service, int request_id, int rrpc, void* ctx)
#else
static int thing_call_service(void* thing_id, char* service, int request_id, void* ctx)
#endif /* RRPC_ENABLED */
{
    sample_context_t* sample = ctx;

    LINKKIT_PRINTF("service(%s) requested, id: thing@%p, request id:%d\n",
                   service, thing_id, request_id);

    if (strcmp(service, "Custom") == 0) {
#ifdef RRPC_ENABLED
        handle_service_custom(sample, thing_id, service, request_id, rrpc);
#else
        handle_service_custom(sample, thing_id, service, request_id);
#endif /* RRPC_ENABLED */
    }

    return 0;
}

返回值

详细描述
0 成功
-1 失败

linkkit_invoke_raw_service

定义描述

函数原型 int linkkit_invoke_raw_service(const void* thing_id, int is_up_raw, void* raw_data, int raw_data_length)
描述 向云端发送裸数据

请求参数

参数
类型
必填
描述
备注
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得 linkkit 创建的 thing id
is_up_ram
int
消息类型
非0: raw 上行
0: raw 下行回复
raw_data
void*
裸数据
发送给云端的裸数据
raw_data_length
int
裸数据长度
发送给云端的裸数据长度

示例

static int raw_data_arrived(void* thing_id, void* data, int len, void* ctx)
{
    char raw_data[128] = {0};

    LINKKIT_PRINTF("raw data arrived,len:%d\n", len);

    /* do user's raw data process logical here. */

    /* ............................... */

    /* user's raw data process logical complete */

    snprintf(raw_data, sizeof(raw_data), "test down raw reply data %lld", HAL_UptimeMs());

    linkkit_invoke_raw_service(thing_id, 0, raw_data, strlen(raw_data));

    return 0;
}

返回值

详细描述
0 成功
-1 失败

linkkit_trigger_event

定义描述

函数原型 int linkkit_trigger_event(const void* thing_id, const char* event_identifier, const char* property_identifier)
描述 上报设备事件到云端

请求参数

参数
类型
必填
描述
备注
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得 linkkit 创建的thing id
event_identifier
const char*
事件标示符
上报的事件的标识符
property_identifier
const char*
属性标示符
如果 event_identifier所属字段的 method 为 "event.property.post", 则上报 property_identifier 对应的 property,否则上报所有的 property

示例

static int post_event_error(sample_context_t* sample)
{
    char event_output_identifier[64];
    snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s", EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);

    int errorCode = 0;
    linkkit_set_value(linkkit_method_set_event_output_value,
                      sample->thing,
                      event_output_identifier,
                      &errorCode, NULL);

    return linkkit_trigger_event(sample->thing, EVENT_ERROR_IDENTIFIER, NULL);
}

返回值

详细描述
0 成功
-1 失败

linkkit_trigger_deviceinfo_operate

定义描述

函数原型 int linkkit_trigger_deviceinfo_operate(const void* thing_id, const char* params, linkkit_deviceinfo_operate_t linkkit_deviceinfo_operation);
描述 向云端更新拓展属性

请求参数

参数
类型
必填
描述
备注
thing_id
const void*
物的唯一标识符
物对象的标识符,可从 thing_create 回调获得 linkkit 创建的thing id
params
const char*
拓展属性
需要在控制台拓展信息新增属性后方可使用
linkkit_deviceinfo_operation
linkkit_deviceinfo_operate_t
设备拓展信息操作
linkkit_deviceinfo_operate_update:
更新拓展属性
linkkit_deviceinfo_operate_delete:
删除拓展属性

示例

//需要在云控制台增加拓展属性Key:Temperature
if (now % 30 == 0 && is_active(sample_ctx)) {
    ret = linkkit_trigger_deviceinfo_operate(sample_ctx->thing, "[{\"attrKey\":\"Temperature\",\"attrValue\":\"36.8\"}]", linkkit_deviceinfo_operate_update);
}

返回值

详细描述
0 成功
-1 失败

results matching ""

    No results matching ""