Pass the width and height when requesting camera images (#9683)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2021-08-10 23:42:37 -05:00 committed by GitHub
parent f686816c86
commit b36e342f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 11 deletions

View File

@ -7,14 +7,10 @@ interface ResultCache<T> {
export const timeCachePromiseFunc = async <T>( export const timeCachePromiseFunc = async <T>(
cacheKey: string, cacheKey: string,
cacheTime: number, cacheTime: number,
func: ( func: (hass: HomeAssistant, entityId: string, ...args: any[]) => Promise<T>,
hass: HomeAssistant, hass: HomeAssistant,
entityId: string, entityId: string,
...args: unknown[] ...args: any[]
) => Promise<T>,
hass: HomeAssistant,
entityId: string,
...args: unknown[]
): Promise<T> => { ): Promise<T> => {
let cache: ResultCache<T> | undefined = (hass as any)[cacheKey]; let cache: ResultCache<T> | undefined = (hass as any)[cacheKey];

View File

@ -38,22 +38,28 @@ export const computeMJPEGStreamUrl = (entity: CameraEntity) =>
export const fetchThumbnailUrlWithCache = ( export const fetchThumbnailUrlWithCache = (
hass: HomeAssistant, hass: HomeAssistant,
entityId: string entityId: string,
width: number,
height: number
) => ) =>
timeCachePromiseFunc( timeCachePromiseFunc(
"_cameraTmbUrl", "_cameraTmbUrl",
9000, 9000,
fetchThumbnailUrl, fetchThumbnailUrl,
hass, hass,
entityId entityId,
width,
height
); );
export const fetchThumbnailUrl = async ( export const fetchThumbnailUrl = async (
hass: HomeAssistant, hass: HomeAssistant,
entityId: string entityId: string,
width: number,
height: number
) => { ) => {
const path = await getSignedPath(hass, `/api/camera_proxy/${entityId}`); const path = await getSignedPath(hass, `/api/camera_proxy/${entityId}`);
return hass.hassUrl(path.path); return hass.hassUrl(`${path.path}&width=${width}&height=${height}`);
}; };
export const fetchStreamUrl = async ( export const fetchStreamUrl = async (

View File

@ -19,6 +19,10 @@ import { HomeAssistant } from "../../../types";
const UPDATE_INTERVAL = 10000; const UPDATE_INTERVAL = 10000;
const DEFAULT_FILTER = "grayscale(100%)"; const DEFAULT_FILTER = "grayscale(100%)";
const MAX_IMAGE_WIDTH = 640;
const ASPECT_RATIO_DEFAULT = 9 / 16;
const SCALING_FACTOR = 2;
export interface StateSpecificConfig { export interface StateSpecificConfig {
[state: string]: string; [state: string]: string;
} }
@ -228,9 +232,25 @@ export class HuiImage extends LitElement {
return; return;
} }
// One the first render we will not know the width
const element_width = this._image.offsetWidth
? this._image.offsetWidth
: MAX_IMAGE_WIDTH;
// Because the aspect ratio might result in a smaller image,
// we ask for 200% of what we need to make sure the image is
// still clear. In practice, for 4k sources, this is still
// an order of magnitude smaller.
const width = Math.ceil(element_width * SCALING_FACTOR);
// If the image has not rendered yet we may have a zero height
const height = this._image.offsetHeight
? this._image.offsetHeight * SCALING_FACTOR
: Math.ceil(element_width * SCALING_FACTOR * ASPECT_RATIO_DEFAULT);
this._cameraImageSrc = await fetchThumbnailUrlWithCache( this._cameraImageSrc = await fetchThumbnailUrlWithCache(
this.hass, this.hass,
this.cameraImage this.cameraImage,
width,
height
); );
} }