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>(
cacheKey: string,
cacheTime: number,
func: (
hass: HomeAssistant,
entityId: string,
...args: unknown[]
) => Promise<T>,
func: (hass: HomeAssistant, entityId: string, ...args: any[]) => Promise<T>,
hass: HomeAssistant,
entityId: string,
...args: unknown[]
...args: any[]
): Promise<T> => {
let cache: ResultCache<T> | undefined = (hass as any)[cacheKey];

View File

@ -38,22 +38,28 @@ export const computeMJPEGStreamUrl = (entity: CameraEntity) =>
export const fetchThumbnailUrlWithCache = (
hass: HomeAssistant,
entityId: string
entityId: string,
width: number,
height: number
) =>
timeCachePromiseFunc(
"_cameraTmbUrl",
9000,
fetchThumbnailUrl,
hass,
entityId
entityId,
width,
height
);
export const fetchThumbnailUrl = async (
hass: HomeAssistant,
entityId: string
entityId: string,
width: number,
height: number
) => {
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 (

View File

@ -19,6 +19,10 @@ import { HomeAssistant } from "../../../types";
const UPDATE_INTERVAL = 10000;
const DEFAULT_FILTER = "grayscale(100%)";
const MAX_IMAGE_WIDTH = 640;
const ASPECT_RATIO_DEFAULT = 9 / 16;
const SCALING_FACTOR = 2;
export interface StateSpecificConfig {
[state: string]: string;
}
@ -228,9 +232,25 @@ export class HuiImage extends LitElement {
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.hass,
this.cameraImage
this.cameraImage,
width,
height
);
}