Home-AssistantConfig/config/www/community/frigate-hass-card/engine-reolink-5d642010.js

9 lines
5.0 KiB
JavaScript

import{l as e,e4 as t,ec as a,e7 as s,eh as n,ed as i,dQ as r,d4 as o,e6 as c,e5 as l,ei as u,e9 as h,ea as d,ee as g,ej as m,dc as p,de as _}from"./card-45855f1f.js";import{B as y,a as f,i as C,g as w}from"./within-dates-a663657f.js";import{C as M}from"./engine-86b0096c.js";import{p as D}from"./parse-114e8d53.js";import{e as x}from"./endOfDay-24d35e3f.js";import"./engine-generic-31c14ad9.js";import"./media-c9012082.js";class k extends t{}class b extends y{constructor(){super(...arguments),this._channel=null}async initialize(e){return await super.initialize(e),this._initializeChannel(),this}_initializeChannel(){const t=this._entity?.unique_id,a=t?String(t).match(/(.*)_(?<channel>\d+)/):null,s=a&&a.groups?.channel?Number(a.groups.channel):null;if(null===s)throw new k(e("error.camera_initialization_reolink"),this.getConfig());this._channel=s}getChannel(){return this._channel}getProxyConfig(){return{...super.getProxyConfig(),media:"auto"===this._config.proxy.media||this._config.proxy.media,ssl_verification:"auto"!==this._config.proxy.ssl_verification&&this._config.proxy.ssl_verification,ssl_ciphers:"auto"===this._config.proxy.ssl_ciphers?"intermediate":this._config.proxy.ssl_ciphers}}}class v{static isReolinkEventQueryResults(e){return e.engine===s.Reolink&&e.type===d.Event}}class E extends f{constructor(){super(...arguments),this._directoryCache=new a,this._fileCache=new a}getEngineType(){return s.Reolink}_reolinkFileMetadataGenerator(e,t,a){
/* istanbul ignore next: This situation cannot happen as the directory would
be rejected by _reolinkDirectoryMetadataGenerator if there was no start date
-- @preserve */
if(!a?._metadata?.startDate||t.media_class!==n)return null;const s=t.title.split(/ +/);if(2!==s.length)return null;const o=D(s[0],"HH:mm:ss",a._metadata.startDate);if(!i(o))return null;const c=s[1].match(/(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+)/),l=c?.groups?{hours:Number(c.groups.hours),minutes:Number(c.groups.minutes),seconds:Number(c.groups.seconds)}:null;return{cameraID:e,startDate:o,endDate:l?r(o,l):o}}_reolinkDirectoryMetadataGenerator(e,t){const a=D(t.title,"yyyy/M/d",new Date);return i(a)?{cameraID:e,startDate:o(a),endDate:x(a)}:null}async createCamera(e,t){const a=new b(t,this,{capabilities:new c({"favorite-events":!1,"favorite-recordings":!1,clips:!0,live:!0,menu:!0,recordings:!1,seek:!1,snapshots:!1,substream:!0,ptz:l(t)??void 0},{disable:t.capabilities?.disable,disableExcept:t.capabilities?.disable_except}),eventCallback:this._eventCallback});return await a.initialize({entityRegistryManager:this._entityRegistryManager,hass:e,stateWatcher:this._stateWatcher})}async _getMatchingDirectories(e,t,a,s){const n=t.getConfig(),i=t.getEntity(),r=i?.config_entry_id;return null!==t.getChannel()&&r?await this._browseMediaManager.walkBrowseMedias(e,[{targets:[`media-source://reolink/RES|${r}|${t.getChannel()}|`+("low"===n.reolink?.media_resolution?"sub":"main")],concurrency:1/0,metadataGenerator:(e,a)=>this._reolinkDirectoryMetadataGenerator(t.getID(),e),matcher:e=>e.can_expand&&C(e,a?.start,a?.end),sorter:e=>u(e)}],{...!1!==s?.useCache&&{cache:this._directoryCache}}):null}async getEvents(e,t,a,n){if(a.favorite||a.tags?.size||a.what?.size||a.where?.size||a.hasSnapshot)return null;const i=new Map,r=async r=>{const o={...a,cameraIDs:new Set([r])},c=n?.useCache??1?this._requestCache.get(o):null;if(c)return void i.set(o,c);const l=t.getCamera(r),h=l&&l instanceof b?await this._getMatchingDirectories(e,l,o,n):null,g=o.limit??M;let m=[];h?.length&&(m=await this._browseMediaManager.walkBrowseMedias(e,[{targets:h,concurrency:1,metadataGenerator:(e,t)=>this._reolinkFileMetadataGenerator(r,e,t),earlyExit:e=>e.length>=g,matcher:e=>!e.can_expand&&C(e,o.start,o.end),sorter:e=>u(e)}],{...!1!==n?.useCache&&{cache:this._fileCache}}));const _=p(m,(e=>e._metadata?.startDate),"desc").slice(0,g),y={type:d.Event,engine:s.Reolink,browseMedia:_};(n?.useCache??1)&&this._requestCache.set(o,{...y,cached:!0},y.expiry),i.set(o,y)};return await h(a.cameraIDs,(e=>r(e))),i}generateMediaFromEvents(e,t,a,s){return v.isReolinkEventQueryResults(s)?w(s.browseMedia):null}async getMediaMetadata(e,t,a,n){const i=new Map,o=n?.useCache??1?this._requestCache.get(a):null;if(o)return i.set(a,o),i;const c=new Set,l=async a=>{const s=t.getCamera(a);if(!(s&&s instanceof b))return;const i=await this._getMatchingDirectories(e,s,null,n);for(const e of i??[])
/* istanbul ignore next: This situation cannot happen as the directory
will not match without metadata -- @preserve */
e._metadata&&c.add(_(e._metadata?.startDate))};await h(a.cameraIDs,(e=>l(e)));const u={type:d.MediaMetadata,engine:s.Reolink,metadata:{...c.size&&{days:c}},expiry:r(new Date,{seconds:g}),cached:!1};return(n?.useCache??1)&&this._requestCache.set(a,{...u,cached:!0},u.expiry),i.set(a,u),i}getCameraMetadata(e,t){return{...super.getCameraMetadata(e,t),engineLogo:m}}getCameraEndpoints(e,t){const a=e.reolink?.url?{endpoint:e.reolink.url}:null;return{...super.getCameraEndpoints(e,t),...a&&{ui:a}}}}export{E as ReolinkCameraManagerEngine,v as ReolinkQueryResultsClassifier};