Skip to content

Commit

Permalink
Replace previous function with getLatestMessageOfEveryReceivedArbId()
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahAndrews committed Nov 10, 2024
1 parent 1f79afe commit f98f277
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, number>;
getLatestMessageOfEveryReceivedArbId: (descriptor: string) => Record<number, CanMessage>;

constructor() {
try {
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 14 additions & 4 deletions src/canWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Napi::String>().Utf8Value();

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/canWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f98f277

Please sign in to comment.