From b9cef866c9dc902e00160ee0e01e361e1263ed43 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Sun, 6 Mar 2022 19:52:26 -0800 Subject: [PATCH] Use the newly embedded Frigate event metadata. --- src/browse-media-util.ts | 30 ++++++++---------------------- src/card.ts | 2 +- src/components/viewer.ts | 6 +++--- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/browse-media-util.ts b/src/browse-media-util.ts index c6069b1d..9d6966c6 100644 --- a/src/browse-media-util.ts +++ b/src/browse-media-util.ts @@ -22,34 +22,20 @@ dayjs.extend(dayjs_custom_parse_format); export class BrowseMediaUtil { /** * Return the Frigate event_id given a FrigateBrowseMediaSource object. - * @param media The event to extract the id from. + * @param media The event to get the id from. * @returns The `event_id` or `null` if not successfully parsed. */ - static extractEventID(media: FrigateBrowseMediaSource): string | null { - const result = media.media_content_id.match( - /^media-source:\/\/frigate\/.*\/(?[.0-9]+-[a-zA-Z0-9]+)$/, - ); - return result && result.groups ? result.groups['id'] : null; + static getEventID(media: FrigateBrowseMediaSource): string | null { + return media.frigate?.event.id ?? null; } /** * Return the event start time given a FrigateBrowseMediaSource object. - * @param browseMedia The media object to extract the start time from. + * @param browseMedia The media object to get the start time from. * @returns The start time in unix/epoch time, or null if it cannot be determined. */ - static extractEventStartTime(browseMedia: FrigateBrowseMediaSource): number | null { - // Example: 2021-08-27 20:57:22 [10s, Person 76%] - const result = browseMedia.title.match(/^(?.+) \[/); - if (result && result.groups) { - const iso_datetime_str = result.groups['iso_datetime']; - if (iso_datetime_str) { - const iso_datetime = dayjs(iso_datetime_str, 'YYYY-MM-DD HH:mm:ss', true); - if (iso_datetime.isValid()) { - return iso_datetime.unix(); - } - } - } - return null; + static getEventStartTime(media: FrigateBrowseMediaSource): number | null { + return media.frigate?.event.start_time ?? null; } /** @@ -116,8 +102,8 @@ export class BrowseMediaUtil { 'event-search', params.mediaType, '', // Name/Title to render (not necessary here) - params.after ? String(params.after) : '', - params.before ? String(params.before) : '', + params.after ? String(Math.floor(params.after)) : '', + params.before ? String(Math.ceil(params.before)) : '', params.cameraName, params.label, params.zone, diff --git a/src/card.ts b/src/card.ts index 7c758015..2aee36c5 100644 --- a/src/card.ts +++ b/src/card.ts @@ -746,7 +746,7 @@ export class FrigateCard extends LitElement { }); return; } - const event_id = BrowseMediaUtil.extractEventID(this._view.media); + const event_id = BrowseMediaUtil.getEventID(this._view.media); if (!event_id) { this._setMessageAndUpdate({ message: localize('error.download_no_event_id'), diff --git a/src/components/viewer.ts b/src/components/viewer.ts index 7a713e99..a5aa231f 100644 --- a/src/components/viewer.ts +++ b/src/components/viewer.ts @@ -414,7 +414,7 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel { return null; } - const snapshotStartTime = BrowseMediaUtil.extractEventStartTime(snapshot); + const snapshotStartTime = BrowseMediaUtil.getEventStartTime(snapshot); if (!snapshotStartTime) { return null; } @@ -435,7 +435,7 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel { if (!BrowseMediaUtil.isTrueMedia(child)) { continue; } - const startTime = BrowseMediaUtil.extractEventStartTime(child); + const startTime = BrowseMediaUtil.getEventStartTime(child); if (startTime && (earliest === null || startTime < earliest)) { earliest = startTime; @@ -471,7 +471,7 @@ export class FrigateCardViewerCarousel extends FrigateCardMediaCarousel { if (!BrowseMediaUtil.isTrueMedia(child)) { continue; } - const clipStartTime = BrowseMediaUtil.extractEventStartTime(child); + const clipStartTime = BrowseMediaUtil.getEventStartTime(child); if (clipStartTime && clipStartTime === snapshotStartTime) { return new View({ view: 'clip',