物模型开发

//此处说明该模块包含的详细功能模块

设备可以使用物模型功能,实现属性上报(如上报设备状态)、事件上报(上报设备异常或错误)和服务调用(通过云端调用设备提供的服务)。

设备属性

  • 设备属性上报
cJSON *params = cJSON_CreateObject();
cJSON_AddNumberToObject(params, "WorkMode", 1);

char *mid = malloc(UUID_LEN);
memset(mid, 0, UUID_LEN);
int ret = IotClient_UploadProperties(params, &mid);
IotLogInfo("upload properties mid:%s", mid);
if (ret != 0) {
    IotLogInfo("post value error:%d", ret);
}
free(mid);

设备事件

  • 设备事件上报
cJSON *params = cJSON_CreateObject();
cJSON_AddNumberToObject(params, "WorkMode", 1);

char *mid = malloc(UUID_LEN);
memset(mid, 0, UUID_LEN);
int ret = IotClient_UploadEvents(params, "system.msg.transform.upload", &mid);
IotLogInfo("upload events mid:%s", mid);
if (ret != 0) {
    IotLogInfo("post value error:%d", ret);
}
free(mid);

设备服务

  • 设备属性获取
int cbGetProperties(cJSON *inParams, cJSON *outParams) {

    cJSON *params = inParams->child;
    if (!params) {
        return -1;
    }

    while (params) {
        char *key = params->valuestring;
        if (!key) {
            break;
        }

        if (!strcmp("PowerSwitch", key)) {
            cJSON_AddBoolToObject(outParams, key, true);
        } else if (!strcmp("WorkMode", key)) {
            cJSON_AddNumberToObject(outParams, key, 1);
        } else if (!strcmp("TargetTemperature", key)) {
            cJSON_AddNumberToObject(outParams, key, 26);
        } else if (!strcmp("CurrentTemperature", key)) {
            cJSON_AddNumberToObject(outParams, key, 28);
        } else if (!strcmp("WindSpeed", key)) {
            cJSON_AddNumberToObject(outParams, key, 2);
        } else if (!strcmp("SleepMode", key)) {
            cJSON_AddBoolToObject(outParams, key, false);
        } else if (!strcmp("SleepTime", key)) {
            cJSON_AddNumberToObject(outParams, key, 8);
        }
        params = params->next;
    }
    return 0;
}
  • 设备属性设置
int cbSetProperties(cJSON *inParams, char **msg) {

    cJSON *params = inParams->child;
    if (params == NULL) {
        return -1;
    }

    char *key = params->string;
    if (!strcmp("PowerSwitch", key)) {
        if (cJSON_False != params->type && cJSON_True != params->type) {
            return -1;
        }

        IotLogInfo("setProperties PowerSwitch:%d", params->valueint);
    } else if (!strcmp("WorkMode", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties WorkMode:%d", params->valueint);
    } else if (!strcmp("TargetTemperature", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties TargetTemperature:%d", params->valuedouble);
    } else if (!strcmp("CurrentTemperature", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties CurrentTemperature:%d", params->valuedouble);
    } else if (!strcmp("WindSpeed", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties WindSpeed:%d", params->valueint);
    } else if (!strcmp("SleepMode", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties SleepMode:%d", params->valueint);
    } else if (!strcmp("SleepTime", key)) {
        if (cJSON_Number != params->type) {
            return -1;
        }

        IotLogInfo("setProperties SleepTime:%d", params->valueint);
    }

    *msg = "set properties ok";
    return 0;
}
  • 设备自定义服务
int cbCallServices(cJSON *inParams, cJSON *outParams, char **msg) {

    char *identifier = inParams->child->valuestring;
    if (!identifier) {
        return -1;
    }

    if (!strcmp("AdjustTemperature", identifier)) {
        cJSON *paramsJson = cJSON_GetObjectItem(inParams, "params");
        if (!paramsJson) {
            return -1;
        }

        cJSON *tmpJson = paramsJson->child;

        while (tmpJson) {
            char *key = tmpJson->string;
            if (!key) {
                break;
            }
            IotLogInfo("key1=%s", key);

            if (!strcmp("Up", key)) {
                if (tmpJson->type == cJSON_Number) {
                    IotLogInfo("Up=%d", tmpJson->valueint);
                }
            } else if (!strcmp("Down", key)) {
                if (tmpJson->type == cJSON_Number) {
                    IotLogInfo("Down=%d", tmpJson->valueint);
                }
            }

            tmpJson = tmpJson->next;
        }
    } else if (!strcmp("AdjustFanSpeed", identifier)) {
        cJSON *paramsJson = cJSON_GetObjectItem(inParams, "params");
        if (!paramsJson) {
            return -1;
        }

        cJSON *tmpJson = paramsJson->child;

        while (tmpJson) {
            char *key = tmpJson->string;
            if (!key) {
                break;
            }
            IotLogInfo("key1=%s", key);

            if (!strcmp("Up", key)) {
                if (tmpJson->type == cJSON_Number) {
                    IotLogInfo("Up=%d", tmpJson->valueint);
                }
            } else if (!strcmp("Down", key)) {
                if (tmpJson->type == cJSON_Number) {
                    IotLogInfo("Down=%d", tmpJson->valueint);
                }
            }

            tmpJson = tmpJson->next;
        }
    }

    IotLogInfo("identifier=%s", identifier);

    if (outParams) {
        cJSON_AddBoolToObject(outParams, "status", 1);
    }

    *msg = "call service ok";

    return 0;
}

results matching ""

    No results matching ""