From 8133f839be5468d978b9f177be3e4f4975d9c10f Mon Sep 17 00:00:00 2001 From: NoahAndrews <10224994+NoahAndrews@users.noreply.github.com.> Date: Thu, 7 Nov 2024 19:23:02 -0600 Subject: [PATCH] Replace previous function with getLatestMessageOfEveryReceivedArbId() --- lib/binding.ts | 6 +++--- src/addon.cc | 4 ++-- src/canWrapper.cc | 18 ++++++++++++++---- src/canWrapper.h | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/binding.ts b/lib/binding.ts index dd7e912..1910844 100644 --- a/lib/binding.ts +++ b/lib/binding.ts @@ -75,9 +75,9 @@ export class CanBridge { stopHeartbeats: (descriptor: string, sendDisabledHeartbeatsFirst: boolean) => void; ackHeartbeats: () => void; /** - * @return Object that maps arbitration IDs to the timestamp a message with that ID was last received at + * @return Object that maps arbitration IDs to the last-received message with that ID */ - getTimestampsForAllReceivedMessages: (descriptor: string) => Record; + getLatestMessageOfEveryReceivedArbId: (descriptor: string) => Record; constructor() { try { @@ -107,7 +107,7 @@ export class CanBridge { this.startRevCommonHeartbeat = addon.startRevCommonHeartbeat; this.ackHeartbeats = addon.ackHeartbeats; this.stopHeartbeats = addon.stopHeartbeats; - this.getTimestampsForAllReceivedMessages = addon.getTimestampsForAllReceivedMessages; + this.getLatestMessageOfEveryReceivedArbId = addon.getLatestMessageOfEveryReceivedArbId; } catch (e: any) { throw new CanBridgeInitializationError(e); } diff --git a/src/addon.cc b/src/addon.cc index 6ecc3e0..296db51 100644 --- a/src/addon.cc +++ b/src/addon.cc @@ -50,8 +50,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) { Napi::Function::New(env, stopHeartbeats)); exports.Set(Napi::String::New(env, "ackHeartbeats"), Napi::Function::New(env, ackHeartbeats)); - exports.Set(Napi::String::New(env, "getTimestampsForAllReceivedMessages"), - Napi::Function::New(env, getTimestampsForAllReceivedMessages)); + exports.Set(Napi::String::New(env, "getLatestMessageOfEveryReceivedArbId"), + Napi::Function::New(env, getLatestMessageOfEveryReceivedArbId)); return exports; } diff --git a/src/canWrapper.cc b/src/canWrapper.cc index 6c75d50..f7cc7a9 100644 --- a/src/canWrapper.cc +++ b/src/canWrapper.cc @@ -724,7 +724,7 @@ Napi::Array getImageElements(const Napi::CallbackInfo& info) { return elements; } -Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info) { +Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); std::string descriptor = info[0].As().Utf8Value(); @@ -751,9 +751,19 @@ Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info) Napi::Object result = Napi::Object::New(env); for (auto& m: messages) { uint32_t arbId = m.first; - // GetTimestampUs() actually returns timestamps in milliseconds - uint32_t timestampMs = m.second->GetTimestampUs(); - result.Set(arbId, timestampMs); + auto message = m.second; + + size_t messageSize = message->GetSize(); + const uint8_t* messageData = message->GetData(); + Napi::Array napiMessage = Napi::Array::New(env, messageSize); + for (int i = 0; i < messageSize; i++) { + napiMessage[i] = messageData[i]; + } + Napi::Object messageInfo = Napi::Object::New(env); + messageInfo.Set("messageID", message->GetMessageId()); + messageInfo.Set("timeStamp", message->GetTimestampUs()); + messageInfo.Set("data", napiMessage); + result.Set(arbId, messageInfo); } return result; diff --git a/src/canWrapper.h b/src/canWrapper.h index 6de1b79..ced4460 100644 --- a/src/canWrapper.h +++ b/src/canWrapper.h @@ -28,5 +28,5 @@ void setSparkMaxHeartbeatData(const Napi::CallbackInfo& info); void startRevCommonHeartbeat(const Napi::CallbackInfo& info); void stopHeartbeats(const Napi::CallbackInfo& info); void ackHeartbeats(const Napi::CallbackInfo& info); -Napi::Object getTimestampsForAllReceivedMessages(const Napi::CallbackInfo& info); +Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info); #endif