Skip to content

Commit

Permalink
fix: Only fetch Reolink media for cameras that support it (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored Dec 13, 2024
1 parent 1992db2 commit e0335c5
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 72 deletions.
5 changes: 2 additions & 3 deletions src/camera-manager/browse-media/engine-browse-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
PartialEventQuery,
QueryType,
} from '../types';
import { BrowseMediaMetadata } from './types';

/**
* A base class for cameras that read events from HA BrowseMedia interface.
Expand All @@ -29,15 +28,15 @@ export class BrowseMediaCameraManagerEngine
extends GenericCameraManagerEngine
implements CameraManagerEngine
{
protected _browseMediaManager: BrowseMediaManager<BrowseMediaMetadata>;
protected _browseMediaManager: BrowseMediaManager;
protected _entityRegistryManager: EntityRegistryManager;
protected _resolvedMediaCache: ResolvedMediaCache;
protected _requestCache: RequestCache;

public constructor(
entityRegistryManager: EntityRegistryManager,
stateWatcher: StateWatcherSubscriptionInterface,
browseMediaManager: BrowseMediaManager<BrowseMediaMetadata>,
browseMediaManager: BrowseMediaManager,
resolvedMediaCache: ResolvedMediaCache,
requestCache: RequestCache,
eventCallback?: CameraEventCallback,
Expand Down
53 changes: 47 additions & 6 deletions src/camera-manager/reolink/engine-reolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
import { getPTZCapabilitiesFromCameraConfig } from '../utils/ptz';
import reolinkLogo from './assets/reolink.svg';
import { ReolinkCamera } from './camera';
import { ReolinkEventQueryResults } from './types';
import { BrowseMediaReolinkCameraMetadata, ReolinkEventQueryResults } from './types';

export class ReolinkQueryResultsClassifier {
public static isReolinkEventQueryResults(
Expand All @@ -51,8 +51,7 @@ export class ReolinkQueryResultsClassifier {
}

export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
protected _directoryCache = new MemoryRequestCache<string, BrowseMedia>();
protected _fileCache = new MemoryRequestCache<string, BrowseMedia>();
protected _cache = new MemoryRequestCache<string, BrowseMedia>();

public getEngineType(): Engine {
return Engine.Reolink;
Expand Down Expand Up @@ -115,6 +114,21 @@ export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
: null;
}

protected _reolinkCameraMetadataGenerator(
media: BrowseMedia,
): BrowseMediaReolinkCameraMetadata | null {
// Example: "media-source://reolink/CAM|01J8XHYTNH77WE3C654K03KX1F|0"
const result = media.media_content_id.match(
/^media-source:\/\/reolink\/CAM\|(?<configEntryID>.+)\|(?<channel>\d+)$/,
);
return result?.groups
? {
configEntryID: result.groups.configEntryID,
channel: Number(result.groups.channel),
}
: null;
}

public async createCamera(
hass: HomeAssistant,
cameraConfig: CameraConfig,
Expand Down Expand Up @@ -164,6 +178,34 @@ export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
return null;
}

// First fetch all the Reolink cameras that show up under the media root,
// that match the expected camera. Some Reolink cameras will not show up
// here causing errors.
// https://github.com/dermotduffy/frigate-hass-card/issues/1723
const camerasWithMedia = await this._browseMediaManager.walkBrowseMedias(
hass,
[
{
targets: [`media-source://reolink`],
metadataGenerator: (
media: BrowseMedia,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_parent?: RichBrowseMedia<BrowseMediaReolinkCameraMetadata>,
) => this._reolinkCameraMetadataGenerator(media),
matcher: (media: RichBrowseMedia<BrowseMediaReolinkCameraMetadata>): boolean =>
media._metadata?.channel === camera.getChannel() &&
media._metadata?.configEntryID === configID,
},
],
{
...(engineOptions?.useCache !== false && { cache: this._cache }),
},
);

if (!camerasWithMedia?.length) {
return null;
}

return await this._browseMediaManager.walkBrowseMedias(
hass,
[
Expand All @@ -172,7 +214,6 @@ export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
`media-source://reolink/RES|${configID}|${camera.getChannel()}|` +
`${cameraConfig.reolink?.media_resolution === 'low' ? 'sub' : 'main'}`,
],
concurrency: Infinity,
metadataGenerator: (
media: BrowseMedia,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -186,7 +227,7 @@ export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
},
],
{
...(engineOptions?.useCache !== false && { cache: this._directoryCache }),
...(engineOptions?.useCache !== false && { cache: this._cache }),
},
);
}
Expand Down Expand Up @@ -251,7 +292,7 @@ export class ReolinkCameraManagerEngine extends BrowseMediaCameraManagerEngine {
},
],
{
...(engineOptions?.useCache !== false && { cache: this._fileCache }),
...(engineOptions?.useCache !== false && { cache: this._cache }),
},
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/camera-manager/reolink/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { RichBrowseMedia } from '../../utils/ha/browse-media/types';
import { BrowseMediaMetadata } from '../browse-media/types';
import { Engine, EventQueryResults } from '../types';

export interface BrowseMediaReolinkCameraMetadata {
configEntryID: string;
channel: number;
}

// ==============================
// Reolink concrete query results
// ==============================
Expand Down
10 changes: 4 additions & 6 deletions src/utils/ha/browse-media/browse-media-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export interface BrowseMediaStep<M> {

type BrowseMediaStepAdvancer<M> = (media: RichBrowseMedia<M>[]) => BrowseMediaStep<M>[];

export class BrowseMediaManager<M> {
export class BrowseMediaManager {
// Walk down a browse media tree according to instructions included in `steps`.
public async walkBrowseMedias(
public async walkBrowseMedias<M>(
hass: HomeAssistant,
steps: BrowseMediaStep<M>[] | null,
options?: {
Expand All @@ -77,7 +77,7 @@ export class BrowseMediaManager<M> {
).flat();
}

protected async _walkBrowseMedia(
protected async _walkBrowseMedia<M>(
hass: HomeAssistant,
step: BrowseMediaStep<M>,
options?: {
Expand All @@ -92,7 +92,6 @@ export class BrowseMediaManager<M> {
async (target) =>
await this._browseMedia(hass, target, {
cache: options?.cache,
matcher: step.matcher,
metadataGenerator: step.metadataGenerator,
}),
);
Expand Down Expand Up @@ -121,12 +120,11 @@ export class BrowseMediaManager<M> {
return await this.walkBrowseMedias(hass, nextSteps, options);
}

protected async _browseMedia(
protected async _browseMedia<M>(
hass: HomeAssistant,
target: string | RichBrowseMedia<M>,
options?: {
cache?: BrowseMediaCache<M>;
matcher?: RichBrowseMediaPredicate<M>;
metadataGenerator?: RichMetadataGenerator<M>;
},
): Promise<RichBrowseMedia<M>> {
Expand Down
Loading

0 comments on commit e0335c5

Please sign in to comment.