From 19b9fa58712c4412649e7335a95473b302f34fed Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 18 Jan 2022 21:18:26 +0800 Subject: [PATCH 1/2] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/include/sensor_napi_utils.h | 7 + interfaces/plugin/src/sensor_js.cpp | 109 ++++++++++++++- interfaces/plugin/src/sensor_napi_utils.cpp | 58 +++++++- .../test/unittest/ExampleJsunit.test.js | 125 ++++++++++++++++++ 4 files changed, 297 insertions(+), 2 deletions(-) mode change 100755 => 100644 interfaces/plugin/include/sensor_napi_utils.h mode change 100755 => 100644 interfaces/plugin/src/sensor_js.cpp mode change 100755 => 100644 interfaces/plugin/src/sensor_napi_utils.cpp diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h old mode 100755 new mode 100644 index d654973e..46edf63a --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -83,6 +83,8 @@ typedef enum CallbackDataType { CREATE_QUATERNION = 9, GET_DIRECTION = 10, ROTATION_INCLINATION_MATRIX = 11, + GET_SENSOR_LIST = 12, + GET_SINGLE_SENSOR = 13 } CallbackDataType; struct AsyncCallbackInfo { @@ -93,6 +95,7 @@ struct AsyncCallbackInfo { CallbackData data; BusinessError error; CallbackDataType type; + vector sensorInfos; }; using ConvertDataFunc = void(*)(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]); @@ -143,4 +146,8 @@ void ConvertToRotationMatrix(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, void ConvertToSensorData(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]); void CreateNapiArray(napi_env env, float *data, int32_t dataLength, napi_value result); + +void ConvertToSensorInfos(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]); + +void ConvertToSingleSensor(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]); #endif // SENSOR_NAPI_UTILS_H diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp old mode 100755 new mode 100644 index 0a9f5682..d58ceb06 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -697,6 +697,111 @@ static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info) return nullptr; } +static napi_value GetSensorList(napi_env env, napi_callback_info info) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, NULL)); + if (argc < 0 || argc > 1) { + HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); + return nullptr; + } + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .type = GET_SENSOR_LIST, + }; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); + asyncCallbackInfo->type = FAIL; + asyncCallbackInfo->error.code = ret; + } else { + for (int32_t i = 0; i < count; i++) { + asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i)); + } + } + + if (argc == 0) { + napi_deferred deferred = nullptr; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + EmitPromiseWork(asyncCallbackInfo); + return promise; + } + if (!IsMatchType(env, args[0], napi_function)) { + HiLog::Error(LABEL, "%{public}s argument should be napi_function type", __func__); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + return nullptr; + } + napi_create_reference(env, args[0], 1, &asyncCallbackInfo->callback[0]); + EmitAsyncCallbackWork(asyncCallbackInfo); + return nullptr; +} + +static napi_value GetSingleSensor(napi_env env, napi_callback_info info) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + size_t argc = 2; + napi_value args[2] = { 0 }; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, NULL)); + if (argc < 1 || argc > 2 || !IsMatchType(env, args[0], napi_number)) { + HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); + return nullptr; + } + AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .type = GET_SINGLE_SENSOR, + }; + SensorInfo *sensorInfos = nullptr; + int32_t count = 0; + int32_t ret = GetAllSensors(&sensorInfos, &count); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s get sensorlist failed", __func__); + asyncCallbackInfo->type = FAIL; + asyncCallbackInfo->error.code = ret; + } else { + int32_t sensorTypeId = GetCppInt32(args[0], env); + for (int32_t i = 0; i < count; i++) { + if (sensorInfos[i].sensorTypeId == sensorTypeId) { + asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i)); + break; + } + } + if (asyncCallbackInfo->sensorInfos.empty()) { + HiLog::Error(LABEL, "%{public}s not find sensorTypeId: %{public}d", __func__, sensorTypeId); + asyncCallbackInfo->type = FAIL; + } + } + if (argc == 1) { + napi_deferred deferred = nullptr; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + EmitPromiseWork(asyncCallbackInfo); + return promise; + } + if (!IsMatchType(env, args[1], napi_function)) { + HiLog::Error(LABEL, "%{public}s argument should be napi_function type!", __func__); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + return nullptr; + } + napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); + EmitAsyncCallbackWork(asyncCallbackInfo); + return nullptr; +} + EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) @@ -712,7 +817,9 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("createQuaternion", CreateQuaternion), DECLARE_NAPI_FUNCTION("getAltitude", GetAltitude), DECLARE_NAPI_FUNCTION("getGeomagneticDip", GetGeomagneticDip), - DECLARE_NAPI_FUNCTION("createRotationMatrix", CreateRotationMatrix) + DECLARE_NAPI_FUNCTION("createRotationMatrix", CreateRotationMatrix), + DECLARE_NAPI_FUNCTION("getSensorList", GetSensorList), + DECLARE_NAPI_FUNCTION("getSingleSensor", GetSingleSensor), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc)); return exports; diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp old mode 100755 new mode 100644 index 35f6435c..50393fa5 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -171,7 +171,9 @@ std::map g_convertfuncList = { {TRANSFORM_COORDINATE_SYSTEM, ConvertToArray}, {CREATE_QUATERNION, ConvertToArray}, {GET_DIRECTION, ConvertToArray}, - {ROTATION_INCLINATION_MATRIX, ConvertToRotationMatrix} + {ROTATION_INCLINATION_MATRIX, ConvertToRotationMatrix}, + {GET_SENSOR_LIST, ConvertToSensorInfos}, + {GET_SINGLE_SENSOR, ConvertToSingleSensor}, }; void getJsonObject(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result) @@ -205,6 +207,60 @@ void getJsonObject(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_valu napi_set_named_property(env, result, "totalIntensity", totalIntensity); } +void ConvertToSensorInfo(napi_env env, SensorInfo sensorInfo, napi_value result) +{ + napi_value sensorName = nullptr; + napi_create_string_latin1(env, sensorInfo.sensorName, NAPI_AUTO_LENGTH, &sensorName); + napi_set_named_property(env, result, "sensorName", sensorName); + + napi_value vendorName = nullptr; + napi_create_string_latin1(env, sensorInfo.vendorName, NAPI_AUTO_LENGTH, &vendorName); + napi_set_named_property(env, result, "vendorName", vendorName); + + napi_value firmwareVersion = nullptr; + napi_create_string_latin1(env, sensorInfo.firmwareVersion, NAPI_AUTO_LENGTH, &firmwareVersion); + napi_set_named_property(env, result, "firmwareVersion", firmwareVersion); + + napi_value hardwareVersion = nullptr; + napi_create_string_latin1(env, sensorInfo.hardwareVersion, NAPI_AUTO_LENGTH, &hardwareVersion); + napi_set_named_property(env, result, "hardwareVersion", hardwareVersion); + + napi_value sensorTypeId = nullptr; + napi_create_double(env, sensorInfo.sensorTypeId, &sensorTypeId); + napi_set_named_property(env, result, "sensorTypeId", sensorTypeId); + + napi_value maxRange = nullptr; + napi_create_double(env, sensorInfo.maxRange, &maxRange); + napi_set_named_property(env, result, "maxRange", maxRange); + + napi_value precision = nullptr; + napi_create_double(env, sensorInfo.precision, &precision); + napi_set_named_property(env, result, "precision", precision); + + napi_value power = nullptr; + napi_create_double(env, sensorInfo.power, &power); + napi_set_named_property(env, result, "power", power); +} + +void ConvertToSingleSensor(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]) +{ + napi_get_undefined(env, &result[0]); + napi_create_object(env, &result[1]); + ConvertToSensorInfo(env, asyncCallbackInfo->sensorInfos[0], result[1]); +} + +void ConvertToSensorInfos(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]) +{ + napi_get_undefined(env, &result[0]); + napi_create_array(env, &result[1]); + for (size_t i = 0; i < asyncCallbackInfo->sensorInfos.size(); i++) { + napi_value sensorInfo = nullptr; + napi_create_object(env, &sensorInfo); + ConvertToSensorInfo(env, asyncCallbackInfo->sensorInfos[i], sensorInfo); + napi_set_element(env, result[1], i, sensorInfo); + } +} + void ConvertToFailData(napi_env env, AsyncCallbackInfo *asyncCallbackInfo, napi_value result[2]) { result[0] = GreateBusinessError(env, asyncCallbackInfo->error.code, asyncCallbackInfo->error.message, diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index 0aed39b0..8889bba6 100644 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -2902,4 +2902,129 @@ describe("SensorJsTest", function () { done() }); }) + + /* + * @tc.name: SensorJsTest_111 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_111", 0, async function (done) { + console.info("---------------------------SensorJsTest_111----------------------------------"); + sensor.getSensorList().then((data) => { + console.info("---------------------------SensorJsTest_111 callback in-----------" + data.length); + for (var i = 0; i < data.length; i++) { + console.info("SensorJsTest_111 " + JSON.stringify(data[i])); + } + expect(true).assertTrue(); + done(); + }, (error)=>{ + console.info('SensorJsTest_111 failed'); + expect(false).assertTrue(); + done(); + }); + }) + + /* + * @tc.name: SensorJsTest_112 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_112", 0, async function (done) { + console.info("---------------------------SensorJsTest_112----------------------------------"); + sensor.getSensorList((error, data) => { + if (error) { + console.info('SensorJsTest_112 failed'); + expect(false).assertTrue(); + } else { + console.info("---------------------------SensorJsTest_112 callback in-----------" + data.length); + for (var i = 0; i < data.length; i++) { + console.info("SensorJsTest_112 " + JSON.stringify(data[i])); + } + expect(true).assertTrue(); + } + done() + }); + }) + + /* + * @tc.name: SensorJsTest_113 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_113", 0, async function (done) { + console.info("---------------------------SensorJsTest_113----------------------------------"); + sensor.getSingleSensor(0, (error, data) => { + if (error) { + console.info('SensorJsTest_113 failed'); + expect(false).assertTrue(); + } else { + console.info("---------------------------SensorJsTest_113 callback in-----------" + data.length); + console.info("SensorJsTest_113 " + JSON.stringify(data)); + expect(true).assertTrue(); + } + done() + }); + }) + + /* + * @tc.name: SensorJsTest_114 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_114", 0, async function (done) { + console.info("---------------------------SensorJsTest_114----------------------------------"); + sensor.getSingleSensor(-1, (error, data) => { + if (error) { + console.info('SensorJsTest_114 failed'); + expect(true).assertTrue(); + } else { + console.info("---------------------------SensorJsTest_114 callback in-----------" + data.length); + console.info("SensorJsTest_114 " + JSON.stringify(data)); + expect(false).assertTrue(); + } + done() + }); + }) + + /* + * @tc.name: SensorJsTest_115 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_115", 0, async function (done) { + console.info("---------------------------SensorJsTest_115----------------------------------"); + sensor.getSingleSensor(0).then((data) => { + console.info("SensorJsTest_115 " + JSON.stringify(data)); + expect(true).assertTrue(); + done() + }, (error)=>{ + console.info('SensorJsTest_115 failed'); + expect(false).assertTrue(); + done() + }); + }) + + /* + * @tc.name: SensorJsTest_116 + * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.require: AR000GH2TR + * @tc.author: + */ + it("SensorJsTest_116", 0, async function (done) { + console.info("---------------------------SensorJsTest_116----------------------------------"); + sensor.getSingleSensor(-1).then((data) => { + console.info("SensorJsTest_116 " + JSON.stringify(data)); + expect(false).assertTrue(); + done() + }, (error)=>{ + console.info('SensorJsTest_116 success'); + expect(true).assertTrue(); + done() + }); + }) }) -- Gitee From b159e9aee14337d94388e0430a31e3e0efb0b825 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 18 Jan 2022 22:03:22 +0800 Subject: [PATCH 2/2] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_js.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index d58ceb06..6f8ed84d 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -466,6 +466,8 @@ static napi_value GetDirection(napi_env env, napi_callback_info info) HiLog::Error(LABEL, "%{public}s argument should be napi_function type!", __func__); napi_value result; napi_get_undefined(env, &result); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; return result; } napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); -- Gitee