diff --git a/interfaces/plugin/include/sensor_napi_utils.h b/interfaces/plugin/include/sensor_napi_utils.h index 22b06da0b3194f6f0306798b7eac980203e16adb..b7a377d5c0e4817cc625051a178204315dd8350a 100755 --- a/interfaces/plugin/include/sensor_napi_utils.h +++ b/interfaces/plugin/include/sensor_napi_utils.h @@ -21,14 +21,20 @@ #define EVENT_INVALID_PARAMETER (-1); #define EVENT_OK 0; + +struct SensorData { + int32_t sensorTypeId; + int32_t dataLength; + float data[16]; + int64_t timestamp; +}; + struct AsyncCallbackInfo { napi_env env; napi_async_work asyncWork; napi_deferred deferred; napi_ref callback[1] = { 0 }; - int32_t sensorTypeId; - int32_t sensorDataLength; - float sensorData[16]; + SensorData sensorData; int32_t status; }; diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c60fc7b24fd39634b615d300031283d46ac0bf94..a3537a8b411770d500e36708731c6d289e031d78 100755 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -49,9 +49,10 @@ static void DataCallbackImpl(SensorEvent *event) float *data = (float *)(event->data); for (auto &onCallbackInfo: g_onCallbackInfos) { if ((int32_t)onCallbackInfo.first == sensorTypeId) { - onCallbackInfo.second->sensorTypeId = sensorTypeId; - onCallbackInfo.second->sensorDataLength = event->dataLen; - if (memcpy_s(onCallbackInfo.second->sensorData, event->dataLen, data, event->dataLen) != EOK) { + onCallbackInfo.second->sensorData.sensorTypeId = sensorTypeId; + onCallbackInfo.second->sensorData.dataLength = event->dataLen; + onCallbackInfo.second->sensorData.timestamp = event->timestamp; + if (memcpy_s(onCallbackInfo.second->sensorData.data, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } @@ -64,9 +65,10 @@ static void DataCallbackImpl(SensorEvent *event) return; } struct AsyncCallbackInfo *onceCallbackInfo = g_onceCallbackInfos[sensorTypeId]; - onceCallbackInfo->sensorTypeId = sensorTypeId; - onceCallbackInfo->sensorDataLength = event->dataLen; - if (memcpy_s(onceCallbackInfo->sensorData, event->dataLen, data, event->dataLen) != EOK) { + onceCallbackInfo->sensorData.sensorTypeId = sensorTypeId; + onceCallbackInfo->sensorData.dataLength = event->dataLen; + onceCallbackInfo->sensorData.timestamp = event->timestamp; + if (memcpy_s(onceCallbackInfo->sensorData.data, event->dataLen, data, event->dataLen) != EOK) { HiLog::Error(LABEL, "%{public}s copy data failed", __func__); return; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index fc88427bcde2ed153e70d159df25b872e3a9b587..2d66a145fd1167a8938edab655d5c0cabb992465 100755 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -191,20 +191,22 @@ void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo) napi_create_error(env, code, message, &result[0]); napi_get_undefined(env, &result[1]); } else { - int32_t sensorTypeId = asyncCallbackInfo->sensorTypeId; - if (g_sensorAttributeList.count(sensorTypeId) == 0) { + int32_t sensorTypeId = asyncCallbackInfo->sensorData.sensorTypeId; + if ((g_sensorAttributeList.count(sensorTypeId)) == 0 || (g_sensorAttributeList[sensorTypeId].size() + != (asyncCallbackInfo->sensorData.dataLength / sizeof(float)))) { HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__); return; } std::vector sensorAttribute = g_sensorAttributeList[sensorTypeId]; napi_create_object(env, &result[1]); + napi_value message = nullptr; for (size_t i = 0; i < sensorAttribute.size(); i++) { - napi_value message = nullptr; - double a = asyncCallbackInfo->sensorData[i]; - HiLog::Info(LABEL, "%{public}s data id %{public}f", __func__, a); - napi_create_double(env, a, &message); + napi_create_double(env, asyncCallbackInfo->sensorData.data[i], &message); napi_set_named_property(env, result[1], sensorAttribute[i].c_str(), message); + message = nullptr; } + napi_create_int64(env, asyncCallbackInfo->sensorData.timestamp, &message); + napi_set_named_property(env, result[1], "timestamp", message); napi_get_undefined(env, &result[0]); } napi_call_function(env, undefined, callback, 2, result, &callResult);