@busy-app/busy-lib 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +33 -5
- package/dist/index.js +240 -177
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var O=Object.defineProperty;var B=(e,t,n)=>t in e?O(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var u=(e,t,n)=>B(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("openapi-fetch"),U=(e,t)=>{if(typeof FormData<"u"&&e instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(e)||typeof File<"u"&&e instanceof File||typeof Blob<"u"&&e instanceof Blob||typeof ArrayBuffer<"u"&&e instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(e))return e;let n;return t&&(t instanceof Headers?n=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(n=t["Content-Type"]??t["content-type"]),n==="application/x-www-form-urlencoded")?e&&typeof e=="object"&&!(e instanceof URLSearchParams)?new URLSearchParams(e).toString():String(e):JSON.stringify(e)};let T,h,y=null;async function G(){if(!h){if(!T)throw new Error("getApiVersionFn is not set");y||(y=(async()=>{const e=await T();if(!e.api_semver)throw new Error("Empty API version");h=e.api_semver})().finally(()=>{y=null})),await y}}async function m(e){const i=(e.headers.get("content-type")||"").includes("application/json")?await e.clone().json():await e.clone().text(),r=typeof i=="object"&&i!==null?i.error||i.message:typeof i=="string"?i:void 0;return Object.assign(new Error(r||`HTTP ${e.status} ${e.statusText}`),{status:e.status,statusText:e.statusText,body:i})}let w;const I={async onRequest({request:e,schemaPath:t}){return w&&e.headers.set("Authorization",`Bearer ${w}`),t!=="/version"&&(await G(),h&&e.headers.set("X-API-Sem-Ver",h)),e},async onResponse({request:e,response:t,options:n,schemaPath:i}){if(t.ok)return t;if(i==="/version")throw await m(t);if(t.status!==405)throw await m(t);h=void 0,await G(),h&&e.headers.set("X-API-Sem-Ver",h),w&&e.headers.set("Authorization",`Bearer ${w}`);const r=await(n.fetch??fetch)(e);if(r.ok)return r;throw await m(r)}};let S=null;function C(e,t,n){T=t,w=n??void 0,S=b({baseUrl:e,bodySerializer:U}),S.use(I)}function o(){if(!S)throw new Error("API client is not initialized");return S}async function s(e,t=3e3){if(t<=0)return await e();const n=new AbortController,i=setTimeout(()=>n.abort(),t);try{return await e(n.signal)}catch(r){throw r instanceof DOMException&&r.name==="AbortError"?new Error(`Request timed out after ${t}ms`):r}finally{clearTimeout(i)}}async function $(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/version",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function R(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function W(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status/system",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function N(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status/power",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class M{async SystemVersionGet(t){const n=await $(t);return this.apiSemver=n.api_semver,n}async SystemVersion(t){return this.SystemVersionGet(t)}async SystemStatusGet(t){return await R(t)}async SystemStatus(t){return this.SystemStatusGet(t)}async SystemInfoGet(t){return await W(t)}async SystemInfo(t){return this.SystemInfoGet(t)}async SystemStatusPowerGet(t){return await N(t)}async SystemStatusPower(t){return this.SystemStatusPowerGet(t)}}async function V(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/update/check",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function K(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/update/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function _(e){const t=o(),{version:n}=e,{data:i,error:r}=await s(c=>t.GET("/update/changelog",{params:{query:{version:n}},signal:c}),e.timeout);if(r)throw r;return i}async function q(e){const t=o(),{version:n}=e,{data:i,error:r}=await s(c=>t.POST("/update/install",{params:{query:{version:n}},signal:c}),e.timeout);if(r)throw r;return i}async function x(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/update/abort_download",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class F{async UpdateCheck(t){return await V(t)}async SystemUpdateCheck(t){return this.UpdateCheck(t)}async UpdateStatusGet(t){return await K(t)}async SystemUpdateStatus(t){return this.UpdateStatusGet(t)}async UpdateChangelogGet(t){return await _(t)}async SystemUpdateChangelog(t){return this.UpdateChangelogGet(t)}async UpdateInstall(t){return await q(t)}async UpdateAbort(t){return await x(t)}}async function j(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/time",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function z(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/time/timestamp",{params:{query:{...e,timeout:void 0}},signal:r}),e.timeout);if(i)throw i;return n}async function H(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/time/timezone",{params:{query:{...e,timeout:void 0}},signal:r}),e.timeout);if(i)throw i;return n}async function J(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/time/tzlist",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class X{async TimeGet(t){return await j(t)}async SystemTime(t){return await this.TimeGet(t)}async TimeTimestampSet(t){return await z(t)}async SystemTimeTimestamp(t){return await this.TimeTimestampSet(t)}async TimeTimezoneSet(t){return await H(t)}async SystemTimeTimezone(t){return await this.TimeTimezoneSet(t)}async TimeTzListGet(t){return await J(t)}async SystemTimeTzList(t){return await this.TimeTzListGet(t)}}async function Q(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Y(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/info",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Z(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/profile",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function tt(e){const t=o(),{profile:n}=e,{data:i,error:r}=await s(c=>t.POST("/account/profile",{params:{query:{profile:n}},signal:c}),e.timeout);if(r)throw r;return i}async function et(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/account",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function nt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/account/link",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class it{async AccountInfoGet(t){return await Y(t)}async Account(t){return this.AccountInfoGet(t)}async AccountStateGet(t){return await Q(t)}async AccountProfileGet(t){return await Z(t)}async AccountProfileSet(t){return await tt(t)}async AccountUnlink(t){return await et(t)}async AccountLink(t){return await nt(t)}}async function rt(e){const t=o(),{appId:n,elements:i}=e,{data:r,error:c}=await s(a=>t.POST("/display/draw",{body:{app_id:n,elements:i},signal:a}),e.timeout);if(c)throw c;return r}async function ot(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/display/draw",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function st(e){const t=o(),{display:n}=e,{data:i,error:r}=await s(c=>t.GET("/screen",{params:{query:{display:n}},parseAs:"blob",signal:c}),e.timeout);if(r)throw r;return i}async function ct(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/display/brightness",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function at(e){const t=o(),{front:n,back:i}=e,r=d=>{if(typeof d=="number"){if(d<0||d>100)throw new Error("Brightness value must be between 0 and 100 or 'auto'");return String(d)}if(d==="auto")return"auto"},c=r(n),a=r(i),{data:f,error:l}=await s(d=>t.POST("/display/brightness",{params:{query:{front:c,back:a}},signal:d}),e.timeout);if(l)throw l;return f}class ut{async DisplayDraw(t){return await rt(t)}async DisplayClear(t){return await ot(t)}async DisplayScreenFrameGet(t){return await st(t)}async DisplayBrightnessGet(t){return await ct(t)}async DisplayBrightness(t){return this.DisplayBrightnessGet(t)}async DisplayBrightnessSet(t){return await at(t)}}async function dt(e){const t=o(),{appId:n,path:i}=e,{data:r,error:c}=await s(a=>t.POST("/audio/play",{params:{query:{app_id:n,path:i}},signal:a}),e.timeout);if(c)throw c;return r}async function ft(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/audio/play",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function ht(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/audio/volume",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function lt(e){const t=o(),{volume:n}=e;if(typeof n!="number"||n<0||n>100)throw new Error("Volume must be a number between 0 and 100");const{data:i,error:r}=await s(c=>t.POST("/audio/volume",{params:{query:{volume:n}},signal:c}),e.timeout);if(r)throw r;return i}class wt{async AudioPlay(t){return await dt(t)}async Audio(t){return this.AudioPlay(t)}async AudioStop(t){return await ft(t)}async AudioVolumeGet(t){return await ht(t)}async AudioVolume(t){return this.AudioVolumeGet(t)}async AudioVolumeSet(t){return await lt(t)}}async function yt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/wifi/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function St(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/wifi/connect",{body:{ssid:e.ssid,password:e.password,security:e.security,ip_config:{ip_method:e.ipConfig.ipMethod,address:e.ipConfig.address,mask:e.ipConfig.mask,gateway:e.ipConfig.gateway}},signal:r}),e.timeout);if(i)throw i;return n}async function pt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/wifi/disconnect",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function mt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/wifi/networks",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class Tt{async WifiStatusGet(t){return await yt(t)}async WifiStatus(t){return this.WifiStatusGet(t)}async WifiConnect(t){return await St(t)}async WifiDisconnect(t){return await pt(t)}async WifiNetworksGet(t){return await mt(t)}async WifiNetworks(t){return this.WifiNetworksGet(t)}}async function Et(e){const t=o(),{path:n,file:i}=e,{data:r,error:c}=await s(a=>t.POST("/storage/write",{params:{query:{path:n}},headers:{"Content-Type":"application/octet-stream"},body:i,signal:a}),e.timeout);if(c)throw c;return r}async function gt(e){const t=o(),{path:n,asArrayBuffer:i}=e,{data:r,error:c}=await s(a=>t.GET("/storage/read",{params:{query:{path:n}},parseAs:i?"arrayBuffer":"blob",signal:a}),e.timeout);if(c)throw c;return r}async function kt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.GET("/storage/list",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function Gt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.DELETE("/storage/remove",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function bt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.POST("/storage/mkdir",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function At(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/storage/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class vt{async StorageWrite(t){return await Et(t)}async StorageRead(t){return await gt(t)}async StorageListGet(t){return await kt(t)}async StorageList(t){return this.StorageListGet(t)}async StorageRemove(t){return await Gt(t)}async StorageMkdir(t){return await bt(t)}async StorageStatusGet(t){return await At(t)}async StorageStatus(t){return this.StorageStatusGet(t)}}async function Pt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/access",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Lt(e){const t=o();let{mode:n,key:i}=e;if(i=i??"",String(i).trim()&&!/^\d{4,10}$/.test(String(i)))throw new Error("Key must be a string of 4 to 10 digits");const{data:r,error:c}=await s(a=>t.POST("/access",{params:{query:{mode:n,key:i}},signal:a}),e.timeout);if(c)throw c;return r}async function Dt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/name",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Ot(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/name",{body:e,signal:r}),e.timeout);if(i)throw i;return n}class Bt{async SettingsAccessGet(t){return await Pt(t)}async SettingsAccess(t){return this.SettingsAccessGet(t)}async SettingsAccessSet(t){return await Lt(t)}async SettingsNameGet(t){return await Dt(t)}async SettingsName(t){return this.SettingsNameGet(t)}async SettingsNameSet(t){return await Ot(t)}}async function Ut(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/ble/enable",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function It(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/ble/disable",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Ct(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/ble/pairing",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function $t(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/ble/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class Rt{async BleEnable(t){return await Ut(t)}async BleDisable(t){return await It(t)}async BleUnpair(t){return await Ct(t)}async BleStatusGet(t){return await $t(t)}}async function Wt(e){const t=o(),{keyName:n}=e,{data:i,error:r}=await s(c=>t.POST("/input",{params:{query:{key:n}},signal:c}),e.timeout);if(r)throw r;return i}class Nt{async InputSend(t){return await Wt(t)}}async function Mt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Vt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Kt(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class _t{async MatterStatusGet(t){return await Mt(t)}async MatterPair(t){return await Vt(t)}async MatterErase(t){return await Kt(t)}}const E="http://10.0.4.20",qt="https://proxy.busy.app",xt=/^https?:\/\/proxy(?:\.(?:dev|test|stage))?\.busy\.app$/i;function g(e){const t=e.split(".");if(t.length!==4)return!1;for(const n of t){if(n.length===0||n.length>1&&n[0]==="0"||!/^\d+$/.test(n))return!1;const i=Number(n);if(i<0||i>255)return!1}return!0}function k(e){return/\.local$/i.test(e)}class A{constructor(t){u(this,"addr");u(this,"apiSemver");u(this,"connectionType","unknown");if(!t||!t.addr&&!t.token)this.addr=E;else if(!t.addr)this.addr=qt;else{let n=t.addr.trim();if(/^https?:\/\//i.test(n)||(n=`http://${n}`),xt.test(n)&&!t.token)throw new Error("Token is required. Please provide it.");this.addr=n}this.apiSemver="",C(`${this.addr}/api/`,this.SystemVersionGet.bind(this),t==null?void 0:t.token),this.detectConnectionType()}async detectConnectionType(){const t=new URL(this.addr).hostname;if(!g(t)&&!k(t)){this.connectionType="wifi";return}const n=b({baseUrl:`${this.addr}/api/`});try{const{response:i}=await n.GET("/name");if(i.status===401||i.status===403)this.connectionType="wifi";else if(i.ok)this.connectionType="usb";else throw new Error(`Failed to detect connection type. Status: ${i.status}`)}catch(i){throw i}}}function Ft(e,t){t.forEach(n=>{Object.getOwnPropertyNames(n.prototype).forEach(i=>{Object.defineProperty(e.prototype,i,Object.getOwnPropertyDescriptor(n.prototype,i)||Object.create(null))})})}Ft(A,[M,F,X,it,ut,wt,Tt,vt,Bt,Rt,Nt,_t]);var p=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(p||{});const v=3e3,P=new Set([1001,1006,1012,1013,1014,3008]);function L(e,t){if(t<0||t>=e.length)throw new Error(`Index ${t} is out of bounds (0…${e.length-1})`);const n=e[t];if(n===void 0)throw new Error(`Unexpected undefined at index ${t}`);return n}function jt(e,t){let n=0;const i=e.length,r=[];for(;n<i;){const c=L(e,n);if(n+=1,(c&128)!==0){const a=c&127;for(let f=0;f<a*t;f++)r.push(e[n+f]);n+=a*t}else{const a=c,f=e.slice(n,n+t);for(let l=0;l<a;l++)for(let d=0;d<t;d++)r.push(f[d]);n+=t}}return new Uint8Array(r)}function zt(e){const t=new Uint8Array(e.length*2);let n=0,i=0;for(;n<e.length;){const r=L(e,n),c=r&15,a=r>>4&15;t[i]=c,t[i+1]=a,n+=1,i+=2}return t}const D=()=>typeof window<"u"&&typeof window.document<"u";class Ht{constructor(t){u(this,"addr");u(this,"connected",!1);u(this,"apiKey");u(this,"apiSemver");u(this,"dataListeners",[]);u(this,"stopListeners",[]);u(this,"errorListeners",[]);u(this,"socket",null);if(this.config=t,!D())throw new Error("not browser");if(t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=E;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const r=new URL(n).hostname;if(!g(r)&&!k(r))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(i){throw i instanceof Error&&i.message.startsWith("Invalid address")?i:new Error(`Invalid URL format: "${t.addr}"`)}this.addr=n}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const n of this.dataListeners)n(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const n of this.errorListeners)n(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();const t=new URL(`${this.addr}/api/screen/ws`);if(this.apiKey&&t.searchParams.append("x-api-token",this.apiKey),this.apiSemver&&t.searchParams.append("x-api-sem-ver",this.apiSemver),!t)throw new Error("The WebSocket URL is not specified");this.socket=new WebSocket(t),this.socket.onopen=()=>{this.socket&&(this.socket.send(JSON.stringify({display:this.config.deviceScreen})),this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=n=>{try{if(typeof n.data=="string")return;const i=new Uint8Array(n.data);let r;const c=this.config.deviceScreen===p.FRONT?3:2;try{const a=jt(i,c);this.config.deviceScreen===p.BACK?r=zt(a):r=a,this.emitData(r)}catch{this.emitData(i)}}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=n=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:n}),this.emitStop()},this.socket.onclose=async n=>{if(this.socket=null,this.connected=!1,n.code===v||P.has(n.code)){this.emitError({code:n.code,message:n.reason,raw:n});return}this.emitStop()}}closeWebsocket(){return this.connected=!1,new Promise(t=>{this.socket?(this.socket.onclose=()=>{t()},this.socket.close(),this.socket=null):t(),this.emitStop()})}}class Jt{constructor(t){u(this,"addr");u(this,"connected",!1);u(this,"apiKey");u(this,"apiSemver");u(this,"inputEvent");u(this,"dataListeners",[]);u(this,"stopListeners",[]);u(this,"errorListeners",[]);u(this,"socket",null);if(!D())throw new Error("not browser");if(t!=null&&t.apiKey&&(this.apiKey=t.apiKey),t!=null&&t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=E;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const r=new URL(n).hostname;if(!g(r)&&!k(r))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(i){throw i instanceof Error&&i.message.startsWith("Invalid address")?i:new Error(`Invalid URL format: "${t.addr}"`)}this.addr=n}this.inputEvent={}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const n of this.dataListeners)n(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const n of this.errorListeners)n(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();const t=new URL(`${this.addr}/api/input`);if(this.apiKey&&t.searchParams.append("x-api-token",this.apiKey),this.apiSemver&&t.searchParams.append("x-api-sem-ver",this.apiSemver),!t)throw new Error("The WebSocket URL is not specified");this.socket=new WebSocket(t),this.socket.onopen=()=>{this.socket&&(this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=n=>{try{if(typeof n.data=="string")return;const i=new Uint8Array(n.data);this.emitData(i)}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=n=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:n}),this.emitStop()},this.socket.onclose=async n=>{if(this.socket=null,this.connected=!1,n.code===v||P.has(n.code)){this.emitError({code:n.code,message:n.reason,raw:n});return}this.emitStop()}}sendInput({keyName:t,value:n}){if(!this.socket||!this.connected)throw new Error("WebSocket: Not connected");this.inputEvent[t]=n,this.socket.send(JSON.stringify(this.inputEvent)),n===0&&delete this.inputEvent[t]}closeWebsocket(){return this.connected=!1,new Promise(t=>{this.socket?(this.socket.onclose=()=>{t()},this.socket.close(),this.socket=null):t(),this.emitStop()})}}exports.BusyBar=A;exports.DeviceScreen=p;exports.Input=Jt;exports.ScreenStream=Ht;
|
|
1
|
+
"use strict";var U=Object.defineProperty;var B=(e,t,n)=>t in e?U(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var u=(e,t,n)=>B(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("openapi-fetch"),I=(e,t)=>{if(typeof FormData<"u"&&e instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(e)||typeof File<"u"&&e instanceof File||typeof Blob<"u"&&e instanceof Blob||typeof ArrayBuffer<"u"&&e instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(e))return e;let n;return t&&(t instanceof Headers?n=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(n=t["Content-Type"]??t["content-type"]),n==="application/x-www-form-urlencoded")?e&&typeof e=="object"&&!(e instanceof URLSearchParams)?new URLSearchParams(e).toString():String(e):JSON.stringify(e)};let T,h,w=null;async function A(){if(!h){if(!T)throw new Error("getApiVersionFn is not set");w||(w=(async()=>{const e=await T();if(!e.api_semver)throw new Error("Empty API version");h=e.api_semver})().finally(()=>{w=null})),await w}}async function m(e){const i=(e.headers.get("content-type")||"").includes("application/json")?await e.clone().json():await e.clone().text(),r=typeof i=="object"&&i!==null?i.error||i.message:typeof i=="string"?i:void 0;return Object.assign(new Error(r||`HTTP ${e.status} ${e.statusText}`),{status:e.status,statusText:e.statusText,body:i})}let E;function C(e){E=e}let l;const $={async onRequest({request:e,schemaPath:t}){return l&&e.headers.set("Authorization",`Bearer ${l}`),t!=="/version"&&(await A(),h&&e.headers.set("X-API-Sem-Ver",h),E&&e.headers.set("X-API-Token",E)),e},async onResponse({request:e,response:t,options:n,schemaPath:i}){if(t.ok)return t;if(i==="/version")throw await m(t);if(t.status!==405)throw await m(t);h=void 0,await A(),h&&e.headers.set("X-API-Sem-Ver",h),l&&e.headers.set("Authorization",`Bearer ${l}`);const r=await(n.fetch??fetch)(e);if(r.ok)return r;throw await m(r)}};let S=null;function R(e,t,n){T=t,l=n??void 0,S=b({baseUrl:e,bodySerializer:I}),S.use($)}function o(){if(!S)throw new Error("API client is not initialized");return S}async function s(e,t=3e3){if(t<=0)return await e();const n=new AbortController,i=setTimeout(()=>n.abort(),t);try{return await e(n.signal)}catch(r){throw r instanceof DOMException&&r.name==="AbortError"?new Error(`Request timed out after ${t}ms`):r}finally{clearTimeout(i)}}async function W(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/version",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function N(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function K(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status/system",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function M(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/status/power",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class F{async SystemVersionGet(t){const n=await W(t);return this.apiSemver=n.api_semver,n}async SystemVersion(t){return this.SystemVersionGet(t)}async SystemStatusGet(t){return await N(t)}async SystemStatus(t){return this.SystemStatusGet(t)}async SystemInfoGet(t){return await K(t)}async SystemInfo(t){return this.SystemInfoGet(t)}async SystemStatusPowerGet(t){return await M(t)}async SystemStatusPower(t){return this.SystemStatusPowerGet(t)}}async function V(e){const t=o(),{file:n}=e,{data:i,error:r}=await s(c=>t.POST("/update",{headers:{"Content-Type":"application/octet-stream"},body:n,signal:c}),e.timeout);if(r)throw r;return i}async function _(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/update/check",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function x(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/update/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function z(e){const t=o(),{version:n}=e,{data:i,error:r}=await s(c=>t.GET("/update/changelog",{params:{query:{version:n}},signal:c}),e.timeout);if(r)throw r;return i}async function q(e){const t=o(),{version:n}=e,{data:i,error:r}=await s(c=>t.POST("/update/install",{params:{query:{version:n}},signal:c}),e.timeout);if(r)throw r;return i}async function j(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/update/abort_download",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class H{async UpdateFromFile(t){return await V(t)}async SystemUpdate(t){return await this.UpdateFromFile(t)}async UpdateCheck(t){return await _(t)}async SystemUpdateCheck(t){return this.UpdateCheck(t)}async UpdateStatusGet(t){return await x(t)}async SystemUpdateStatus(t){return this.UpdateStatusGet(t)}async UpdateChangelogGet(t){return await z(t)}async SystemUpdateChangelog(t){return this.UpdateChangelogGet(t)}async UpdateInstall(t){return await q(t)}async UpdateAbort(t){return await j(t)}}async function J(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/time",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function X(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/time/timestamp",{params:{query:{...e,timeout:void 0}},signal:r}),e.timeout);if(i)throw i;return n}async function Q(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/time/timezone",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Y(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/time/timezone",{params:{query:{...e,timeout:void 0}},signal:r}),e.timeout);if(i)throw i;return n}async function Z(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/time/tzlist",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class tt{async TimeGet(t){return await J(t)}async SystemTime(t){return await this.TimeGet(t)}async TimeTimestampSet(t){return await X(t)}async SystemTimeTimestamp(t){return await this.TimeTimestampSet(t)}async TimeTimezoneGet(t){return await Q(t)}async TimeTimezoneSet(t){return await Y(t)}async SystemTimeTimezone(t){return await this.TimeTimezoneSet(t)}async TimeTzListGet(t){return await Z(t)}async SystemTimeTzList(t){return await this.TimeTzListGet(t)}}async function et(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function nt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/info",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function it(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/account/profile",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function rt(e){const t=o(),{profile:n}=e,{data:i,error:r}=await s(c=>t.POST("/account/profile",{params:{query:{profile:n}},signal:c}),e.timeout);if(r)throw r;return i}async function ot(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/account",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function st(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/account/link",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class ct{async AccountInfoGet(t){return await nt(t)}async Account(t){return this.AccountInfoGet(t)}async AccountStateGet(t){return await et(t)}async AccountProfileGet(t){return await it(t)}async AccountProfileSet(t){return await rt(t)}async AccountUnlink(t){return await ot(t)}async AccountLink(t){return await st(t)}}async function at(e){const t=o(),{appId:n,elements:i}=e,{data:r,error:c}=await s(a=>t.POST("/display/draw",{body:{app_id:n,elements:i},signal:a}),e.timeout);if(c)throw c;return r}async function ut(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/display/draw",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function dt(e){const t=o(),{display:n}=e,{data:i,error:r}=await s(c=>t.GET("/screen",{params:{query:{display:n}},parseAs:"blob",signal:c}),e.timeout);if(r)throw r;return i}async function ft(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/display/brightness",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function ht(e){const t=o(),{front:n,back:i}=e,r=d=>{if(typeof d=="number"){if(d<0||d>100)throw new Error("Brightness value must be between 0 and 100 or 'auto'");return String(d)}if(d==="auto")return"auto"},c=r(n),a=r(i),{data:f,error:y}=await s(d=>t.POST("/display/brightness",{params:{query:{front:c,back:a}},signal:d}),e.timeout);if(y)throw y;return f}class yt{async DisplayDraw(t){return await at(t)}async DisplayClear(t){return await ut(t)}async DisplayScreenFrameGet(t){return await dt(t)}async DisplayBrightnessGet(t){return await ft(t)}async DisplayBrightness(t){return this.DisplayBrightnessGet(t)}async DisplayBrightnessSet(t){return await ht(t)}}async function lt(e){const t=o(),{appId:n,path:i}=e,{data:r,error:c}=await s(a=>t.POST("/audio/play",{params:{query:{app_id:n,path:i}},signal:a}),e.timeout);if(c)throw c;return r}async function wt(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/audio/play",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function St(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/audio/volume",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function pt(e){const t=o(),{volume:n}=e;if(typeof n!="number"||n<0||n>100)throw new Error("Volume must be a number between 0 and 100");const{data:i,error:r}=await s(c=>t.POST("/audio/volume",{params:{query:{volume:n}},signal:c}),e.timeout);if(r)throw r;return i}class mt{async AudioPlay(t){return await lt(t)}async Audio(t){return this.AudioPlay(t)}async AudioStop(t){return await wt(t)}async AudioVolumeGet(t){return await St(t)}async AudioVolume(t){return this.AudioVolumeGet(t)}async AudioVolumeSet(t){return await pt(t)}}async function Tt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/wifi/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Et(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/wifi/connect",{body:{ssid:e.ssid,password:e.password,security:e.security,ip_config:{ip_method:e.ipConfig.ipMethod,address:e.ipConfig.address,mask:e.ipConfig.mask,gateway:e.ipConfig.gateway}},signal:r}),e.timeout);if(i)throw i;return n}async function gt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/wifi/disconnect",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function kt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/wifi/networks",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class Gt{async WifiStatusGet(t){return await Tt(t)}async WifiStatus(t){return this.WifiStatusGet(t)}async WifiConnect(t){return await Et(t)}async WifiDisconnect(t){return await gt(t)}async WifiNetworksGet(t){return await kt(t)}async WifiNetworks(t){return this.WifiNetworksGet(t)}}async function At(e){const t=o(),{path:n,file:i}=e,{data:r,error:c}=await s(a=>t.POST("/storage/write",{params:{query:{path:n}},headers:{"Content-Type":"application/octet-stream"},body:i,signal:a}),e.timeout);if(c)throw c;return r}async function bt(e){const t=o(),{path:n,asArrayBuffer:i}=e,{data:r,error:c}=await s(a=>t.GET("/storage/read",{params:{query:{path:n}},parseAs:i?"arrayBuffer":"blob",signal:a}),e.timeout);if(c)throw c;return r}async function vt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.GET("/storage/list",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function Pt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.DELETE("/storage/remove",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function Lt(e){const t=o(),{path:n}=e,{data:i,error:r}=await s(c=>t.POST("/storage/mkdir",{params:{query:{path:n}},signal:c}),e.timeout);if(r)throw r;return i}async function Ot(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/storage/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class Dt{async StorageWrite(t){return await At(t)}async StorageRead(t){return await bt(t)}async StorageListGet(t){return await vt(t)}async StorageList(t){return this.StorageListGet(t)}async StorageRemove(t){return await Pt(t)}async StorageMkdir(t){return await Lt(t)}async StorageStatusGet(t){return await Ot(t)}async StorageStatus(t){return this.StorageStatusGet(t)}}async function Ut(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/access",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Bt(e){const t=o();let{mode:n,key:i}=e;if(i=i??"",String(i).trim()&&!/^\d{4,10}$/.test(String(i)))throw new Error("Key must be a string of 4 to 10 digits");const{data:r,error:c}=await s(a=>t.POST("/access",{params:{query:{mode:n,key:i}},signal:a}),e.timeout);if(c)throw c;return r}async function It(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/name",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Ct(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/name",{body:e,signal:r}),e.timeout);if(i)throw i;return n}class $t{async SettingsAccessGet(t){return await Ut(t)}async SettingsAccess(t){return this.SettingsAccessGet(t)}async SettingsAccessSet(t){const n=await Bt(t);return t.mode==="key"&&t.key&&this.setApiKey(t.key),n}async SettingsNameGet(t){return await It(t)}async SettingsName(t){return this.SettingsNameGet(t)}async SettingsNameSet(t){return await Ct(t)}}async function Rt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/ble/enable",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Wt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/ble/disable",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Nt(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/ble/pairing",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function Kt(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/ble/status",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class Mt{async BleEnable(t){return await Rt(t)}async BleDisable(t){return await Wt(t)}async BleUnpair(t){return await Nt(t)}async BleStatusGet(t){return await Kt(t)}}async function Ft(e){const t=o(),{keyName:n}=e,{data:i,error:r}=await s(c=>t.POST("/input",{params:{query:{key:n}},signal:c}),e.timeout);if(r)throw r;return i}class Vt{async InputSend(t){return await Ft(t)}}async function _t(e){const t=o(),{data:n,error:i}=await s(r=>t.GET("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function xt(e){const t=o(),{data:n,error:i}=await s(r=>t.POST("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}async function zt(e){const t=o(),{data:n,error:i}=await s(r=>t.DELETE("/matter/commissioning",{signal:r}),e==null?void 0:e.timeout);if(i)throw i;return n}class qt{async MatterStatusGet(t){return await _t(t)}async MatterPair(t){return await xt(t)}async MatterErase(t){return await zt(t)}}const g="http://10.0.4.20",jt="https://proxy.busy.app",Ht=/^https?:\/\/proxy(?:\.(?:dev|test|stage))?\.busy\.app$/i;function k(e){const t=e.split(".");if(t.length!==4)return!1;for(const n of t){if(n.length===0||n.length>1&&n[0]==="0"||!/^\d+$/.test(n))return!1;const i=Number(n);if(i<0||i>255)return!1}return!0}function G(e){return/\.local$/i.test(e)}class v{constructor(t){u(this,"addr");u(this,"apiSemver");u(this,"connectionType","unknown");if(!t||!t.addr&&!t.token)this.addr=g;else if(!t.addr)this.addr=jt;else{let n=t.addr.trim();if(/^https?:\/\//i.test(n)||(n=`http://${n}`),Ht.test(n)&&!t.token)throw new Error("Token is required. Please provide it.");this.addr=n}this.apiSemver="",R(`${this.addr}/api/`,this.SystemVersionGet.bind(this),t==null?void 0:t.token),this.detectConnectionType()}async detectConnectionType(){const t=new URL(this.addr).hostname;if(!k(t)&&!G(t)){this.connectionType="wifi";return}const n=b({baseUrl:`${this.addr}/api/`});try{const{response:i}=await n.GET("/name");if(i.status===401||i.status===403)this.connectionType="wifi";else if(i.ok)this.connectionType="usb";else throw new Error(`Failed to detect connection type. Status: ${i.status}`)}catch(i){throw i}}setApiKey(t){C(t)}}function Jt(e,t){t.forEach(n=>{Object.getOwnPropertyNames(n.prototype).forEach(i=>{Object.defineProperty(e.prototype,i,Object.getOwnPropertyDescriptor(n.prototype,i)||Object.create(null))})})}Jt(v,[F,H,tt,ct,yt,mt,Gt,Dt,$t,Mt,Vt,qt]);var p=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(p||{});const P=3e3,L=new Set([1001,1006,1012,1013,1014,3008]);function O(e,t){if(t<0||t>=e.length)throw new Error(`Index ${t} is out of bounds (0…${e.length-1})`);const n=e[t];if(n===void 0)throw new Error(`Unexpected undefined at index ${t}`);return n}function Xt(e,t){let n=0;const i=e.length,r=[];for(;n<i;){const c=O(e,n);if(n+=1,(c&128)!==0){const a=c&127;for(let f=0;f<a*t;f++)r.push(e[n+f]);n+=a*t}else{const a=c,f=e.slice(n,n+t);for(let y=0;y<a;y++)for(let d=0;d<t;d++)r.push(f[d]);n+=t}}return new Uint8Array(r)}function Qt(e){const t=new Uint8Array(e.length*2);let n=0,i=0;for(;n<e.length;){const r=O(e,n),c=r&15,a=r>>4&15;t[i]=c,t[i+1]=a,n+=1,i+=2}return t}const D=()=>typeof window<"u"&&typeof window.document<"u";class Yt{constructor(t){u(this,"addr");u(this,"connected",!1);u(this,"apiKey");u(this,"apiSemver");u(this,"dataListeners",[]);u(this,"stopListeners",[]);u(this,"errorListeners",[]);u(this,"socket",null);if(this.config=t,!D())throw new Error("not browser");if(t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=g;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const r=new URL(n).hostname;if(!k(r)&&!G(r))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(i){throw i instanceof Error&&i.message.startsWith("Invalid address")?i:new Error(`Invalid URL format: "${t.addr}"`)}this.addr=n}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const n of this.dataListeners)n(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const n of this.errorListeners)n(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();const t=new URL(`${this.addr}/api/screen/ws`);if(this.apiKey&&t.searchParams.append("x-api-token",this.apiKey),this.apiSemver&&t.searchParams.append("x-api-sem-ver",this.apiSemver),!t)throw new Error("The WebSocket URL is not specified");this.socket=new WebSocket(t),this.socket.onopen=()=>{this.socket&&(this.socket.send(JSON.stringify({display:this.config.deviceScreen})),this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=n=>{try{if(typeof n.data=="string")return;const i=new Uint8Array(n.data);let r;const c=this.config.deviceScreen===p.FRONT?3:2;try{const a=Xt(i,c);this.config.deviceScreen===p.BACK?r=Qt(a):r=a,this.emitData(r)}catch{this.emitData(i)}}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=n=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:n}),this.emitStop()},this.socket.onclose=async n=>{if(this.socket=null,this.connected=!1,n.code===P||L.has(n.code)){this.emitError({code:n.code,message:n.reason,raw:n});return}this.emitStop()}}closeWebsocket(){return this.connected=!1,new Promise(t=>{this.socket?(this.socket.onclose=()=>{t()},this.socket.close(),this.socket=null):t(),this.emitStop()})}}class Zt{constructor(t){u(this,"addr");u(this,"connected",!1);u(this,"apiKey");u(this,"apiSemver");u(this,"inputEvent");u(this,"dataListeners",[]);u(this,"stopListeners",[]);u(this,"errorListeners",[]);u(this,"socket",null);if(!D())throw new Error("not browser");if(t!=null&&t.apiKey&&(this.apiKey=t.apiKey),t!=null&&t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=g;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const r=new URL(n).hostname;if(!k(r)&&!G(r))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(i){throw i instanceof Error&&i.message.startsWith("Invalid address")?i:new Error(`Invalid URL format: "${t.addr}"`)}this.addr=n}this.inputEvent={}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const n of this.dataListeners)n(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const n of this.errorListeners)n(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();const t=new URL(`${this.addr}/api/input`);if(this.apiKey&&t.searchParams.append("x-api-token",this.apiKey),this.apiSemver&&t.searchParams.append("x-api-sem-ver",this.apiSemver),!t)throw new Error("The WebSocket URL is not specified");this.socket=new WebSocket(t),this.socket.onopen=()=>{this.socket&&(this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=n=>{try{if(typeof n.data=="string")return;const i=new Uint8Array(n.data);this.emitData(i)}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=n=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:n}),this.emitStop()},this.socket.onclose=async n=>{if(this.socket=null,this.connected=!1,n.code===P||L.has(n.code)){this.emitError({code:n.code,message:n.reason,raw:n});return}this.emitStop()}}sendInput({keyName:t,value:n}){if(!this.socket||!this.connected)throw new Error("WebSocket: Not connected");this.inputEvent[t]=n,this.socket.send(JSON.stringify(this.inputEvent)),n===0&&delete this.inputEvent[t]}closeWebsocket(){return this.connected=!1,new Promise(t=>{this.socket?(this.socket.onclose=()=>{t()},this.socket.close(),this.socket=null):t(),this.emitStop()})}}exports.BusyBar=v;exports.DeviceScreen=p;exports.Input=Zt;exports.ScreenStream=Yt;
|
package/dist/index.d.ts
CHANGED
|
@@ -231,6 +231,11 @@ export declare class BusyBar {
|
|
|
231
231
|
* Sends a request without authentication credentials.
|
|
232
232
|
*/
|
|
233
233
|
private detectConnectionType;
|
|
234
|
+
/**
|
|
235
|
+
* Sets API key for all subsequent requests.
|
|
236
|
+
* @param {string} key - API key to use in "X-API-Token" header.
|
|
237
|
+
*/
|
|
238
|
+
setApiKey(key: string): void;
|
|
234
239
|
}
|
|
235
240
|
|
|
236
241
|
export declare type BusyBarConfig = {
|
|
@@ -338,7 +343,7 @@ declare interface components {
|
|
|
338
343
|
* @description Check result status
|
|
339
344
|
* @enum {string}
|
|
340
345
|
*/
|
|
341
|
-
|
|
346
|
+
status?: "available" | "not_available" | "failure" | "none";
|
|
342
347
|
};
|
|
343
348
|
};
|
|
344
349
|
/**
|
|
@@ -4113,10 +4118,6 @@ declare class SystemMethods {
|
|
|
4113
4118
|
SystemStatusPower(this: BusyBar, params?: TimeoutOptions): Promise<StatusPower>;
|
|
4114
4119
|
}
|
|
4115
4120
|
|
|
4116
|
-
export declare interface SystemUpdateParams extends TimeoutOptions {
|
|
4117
|
-
file: BusyFile;
|
|
4118
|
-
}
|
|
4119
|
-
|
|
4120
4121
|
declare class TimeMethods {
|
|
4121
4122
|
/**
|
|
4122
4123
|
* Get current timestamp info.
|
|
@@ -4143,6 +4144,14 @@ declare class TimeMethods {
|
|
|
4143
4144
|
* @deprecated Use `TimeTimestampSet` instead. will be removed in the next release.
|
|
4144
4145
|
*/
|
|
4145
4146
|
SystemTimeTimestamp(this: BusyBar, params: TimeTimestampParams): Promise<SuccessResponse>;
|
|
4147
|
+
/**
|
|
4148
|
+
* Get current timezone.
|
|
4149
|
+
*
|
|
4150
|
+
* @param {TimeoutOptions} [params] - Optional parameters.
|
|
4151
|
+
* @param {TimeoutOptions['timeout']} [params.timeout] - Request timeout in milliseconds.
|
|
4152
|
+
* @returns {Promise<TimezoneInfo>} A promise that resolves to the timezone information.
|
|
4153
|
+
*/
|
|
4154
|
+
TimeTimezoneGet(this: BusyBar, params?: TimeoutOptions): Promise<TimezoneInfo>;
|
|
4146
4155
|
/**
|
|
4147
4156
|
* Set system timezone.
|
|
4148
4157
|
*
|
|
@@ -4196,11 +4205,30 @@ export declare interface UpdateChangelogParams extends TimeoutOptions {
|
|
|
4196
4205
|
version: string;
|
|
4197
4206
|
}
|
|
4198
4207
|
|
|
4208
|
+
export declare interface UpdateFromFileParams extends TimeoutOptions {
|
|
4209
|
+
file: BusyFile;
|
|
4210
|
+
}
|
|
4211
|
+
|
|
4199
4212
|
export declare interface UpdateInstallParams extends TimeoutOptions {
|
|
4200
4213
|
version: string;
|
|
4201
4214
|
}
|
|
4202
4215
|
|
|
4203
4216
|
declare class UpdateMethods {
|
|
4217
|
+
/**
|
|
4218
|
+
* Upload firmware update package.
|
|
4219
|
+
*
|
|
4220
|
+
* @param {UpdateParams} params - Update parameters.
|
|
4221
|
+
* @param {BusyFile} params.file - Firmware TAR file to upload.
|
|
4222
|
+
* @param {TimeoutOptions['timeout']} [params.timeout] - Request timeout in milliseconds.
|
|
4223
|
+
* @returns {Promise<SuccessResponse>} A promise that resolves when upload is complete.
|
|
4224
|
+
*/
|
|
4225
|
+
UpdateFromFile(this: BusyBar, params: UpdateFromFileParams): Promise<SuccessResponse>;
|
|
4226
|
+
/**
|
|
4227
|
+
* @deprecated Use `UpdateUpload` instead. will be removed in the next release.
|
|
4228
|
+
*/
|
|
4229
|
+
SystemUpdate(this: BusyBar, params: UpdateFromFileParams): Promise<{
|
|
4230
|
+
result: string;
|
|
4231
|
+
}>;
|
|
4204
4232
|
/**
|
|
4205
4233
|
* Start firmware update check.
|
|
4206
4234
|
*
|