Skip to content

Commit

Permalink
Add maxAgeMs parameter to getLatestMessageOfEveryReceivedArbId()
Browse files Browse the repository at this point in the history
This was necessary to achieve acceptable performance
  • Loading branch information
NoahAndrews committed Nov 11, 2024
1 parent f98f277 commit 11ed247
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class CanBridge {
/**
* @return Object that maps arbitration IDs to the last-received message with that ID
*/
getLatestMessageOfEveryReceivedArbId: (descriptor: string) => Record<number, CanMessage>;
getLatestMessageOfEveryReceivedArbId: (descriptor: string, maxAgeMs: number) => Record<number, CanMessage>;

constructor() {
try {
Expand Down
11 changes: 10 additions & 1 deletion src/canWrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ Napi::Array getImageElements(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();
uint32_t maxAgeMs = info[1].As<Napi::Number>().Uint32Value();

std::shared_ptr<rev::usb::CANDevice> device;

Expand All @@ -748,10 +749,18 @@ Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info
return Napi::Object::New(env);
}

// TODO(Harper): Use HAL clock
const auto nowMs = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()).time_since_epoch().count();

Napi::Object result = Napi::Object::New(env);
for (auto& m: messages) {
uint32_t arbId = m.first;
auto message = m.second;
uint32_t timestampMs = message->GetTimestampUs();

if (nowMs - timestampMs > maxAgeMs) {
continue;
}

size_t messageSize = message->GetSize();
const uint8_t* messageData = message->GetData();
Expand All @@ -761,7 +770,7 @@ Napi::Object getLatestMessageOfEveryReceivedArbId(const Napi::CallbackInfo& info
}
Napi::Object messageInfo = Napi::Object::New(env);
messageInfo.Set("messageID", message->GetMessageId());
messageInfo.Set("timeStamp", message->GetTimestampUs());
messageInfo.Set("timeStamp", timestampMs);
messageInfo.Set("data", napiMessage);
result.Set(arbId, messageInfo);
}
Expand Down

0 comments on commit 11ed247

Please sign in to comment.