@busy-app/busy-lib 0.8.0 → 0.9.0

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 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 G=Object.defineProperty;var L=(n,t,e)=>t in n?G(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var c=(n,t,e)=>L(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("openapi-fetch"),O=(n,t)=>{if(typeof FormData<"u"&&n instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(n)||typeof File<"u"&&n instanceof File||typeof Blob<"u"&&n instanceof Blob||typeof ArrayBuffer<"u"&&n instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(n))return n;let e;return t&&(t instanceof Headers?e=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(e=t["Content-Type"]??t["content-type"]),e==="application/x-www-form-urlencoded")?n&&typeof n=="object"&&!(n instanceof URLSearchParams)?new URLSearchParams(n).toString():String(n):JSON.stringify(n)};async function p(n){const i=(n.headers.get("content-type")||"").includes("application/json")?await n.clone().json():await n.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 ${n.status} ${n.statusText}`),{status:n.status,statusText:n.statusText,body:i})}async function s(n,t=3e3){if(t<=0)return await n();const e=new AbortController,i=setTimeout(()=>e.abort(),t);try{return await n(e.signal)}catch(r){throw r instanceof DOMException&&r.name==="AbortError"?new Error(`Request timed out after ${t}ms`):r}finally{clearTimeout(i)}}function D(n,t,e){let i,r=e??void 0,o,a=null;const d=async()=>{i||(a||(a=(async()=>{const u=await t();if(!u.api_semver)throw new Error("Empty API version");i=u.api_semver})().finally(()=>{a=null})),await a)},f={async onRequest({request:u,schemaPath:l}){return r&&u.headers.set("Authorization",`Bearer ${r}`),l!=="/version"&&(await d(),i&&u.headers.set("X-API-Sem-Ver",i),o&&u.headers.set("X-API-Token",o)),u},async onResponse({request:u,response:l,options:v,schemaPath:P}){if(l.ok)return l;if(P==="/version")throw await p(l);if(l.status!==405)throw await p(l);i=void 0,await d(),i&&u.headers.set("X-API-Sem-Ver",i),r&&u.headers.set("Authorization",`Bearer ${r}`);const y=await(v.fetch??fetch)(u);if(y.ok)return y;throw await p(y)}},h=m({baseUrl:n,bodySerializer:O});return h.use(f),{client:h,setApiKey:u=>{o=u},setToken:u=>{r=u}}}async function B(n,t){const{data:e,error:i}=await s(r=>n.GET("/version",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function U(n,t){const{data:e,error:i}=await s(r=>n.GET("/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function $(n,t){const{data:e,error:i}=await s(r=>n.GET("/status/system",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function I(n,t){const{data:e,error:i}=await s(r=>n.GET("/status/power",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class K{async SystemVersionGet(t){const e=await B(this.apiClient,t);return this.apiSemver=e.api_semver,e}async SystemStatusGet(t){return await U(this.apiClient,t)}async SystemInfoGet(t){return await $(this.apiClient,t)}async SystemStatusPowerGet(t){return await I(this.apiClient,t)}}async function R(n,t){const{file:e}=t,{data:i,error:r}=await s(o=>n.POST("/update",{headers:{"Content-Type":"application/octet-stream"},body:e,signal:o}),t.timeout);if(r)throw r;return i}async function F(n,t){const{data:e,error:i}=await s(r=>n.POST("/update/check",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function W(n,t){const{data:e,error:i}=await s(r=>n.GET("/update/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function M(n,t){const{version:e}=t,{data:i,error:r}=await s(o=>n.GET("/update/changelog",{params:{query:{version:e}},signal:o}),t.timeout);if(r)throw r;return i}async function N(n,t){const{version:e}=t,{data:i,error:r}=await s(o=>n.POST("/update/install",{params:{query:{version:e}},signal:o}),t.timeout);if(r)throw r;return i}async function _(n,t){const{data:e,error:i}=await s(r=>n.POST("/update/abort_download",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class x{async UpdateFromFile(t){return await R(this.apiClient,t)}async UpdateCheck(t){return await F(this.apiClient,t)}async UpdateStatusGet(t){return await W(this.apiClient,t)}async UpdateChangelogGet(t){return await M(this.apiClient,t)}async UpdateInstall(t){return await N(this.apiClient,t)}async UpdateAbort(t){return await _(this.apiClient,t)}}async function q(n,t){const{data:e,error:i}=await s(r=>n.GET("/time",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function j(n,t){const{data:e,error:i}=await s(r=>n.POST("/time/timestamp",{params:{query:{...t,timeout:void 0}},signal:r}),t.timeout);if(i)throw i;return e}async function V(n,t){const{data:e,error:i}=await s(r=>n.GET("/time/timezone",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function z(n,t){const{data:e,error:i}=await s(r=>n.POST("/time/timezone",{params:{query:{...t,timeout:void 0}},signal:r}),t.timeout);if(i)throw i;return e}async function H(n,t){const{data:e,error:i}=await s(r=>n.GET("/time/tzlist",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class J{async TimeGet(t){return await q(this.apiClient,t)}async TimeTimestampSet(t){return await j(this.apiClient,t)}async TimeTimezoneGet(t){return await V(this.apiClient,t)}async TimeTimezoneSet(t){return await z(this.apiClient,t)}async TimeTzListGet(t){return await H(this.apiClient,t)}}async function X(n,t){const{data:e,error:i}=await s(r=>n.GET("/account/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Q(n,t){const{data:e,error:i}=await s(r=>n.GET("/account/info",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Y(n,t){const{data:e,error:i}=await s(r=>n.GET("/account/profile",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Z(n,t){const{profile:e}=t,{data:i,error:r}=await s(o=>n.POST("/account/profile",{params:{query:{profile:e}},signal:o}),t.timeout);if(r)throw r;return i}async function tt(n,t){const{data:e,error:i}=await s(r=>n.DELETE("/account",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function et(n,t){const{data:e,error:i}=await s(r=>n.POST("/account/link",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class it{async AccountInfoGet(t){return await Q(this.apiClient,t)}async AccountStateGet(t){return await X(this.apiClient,t)}async AccountProfileGet(t){return await Y(this.apiClient,t)}async AccountProfileSet(t){return await Z(this.apiClient,t)}async AccountUnlink(t){return await tt(this.apiClient,t)}async AccountLink(t){return await et(this.apiClient,t)}}async function nt(n,t){const{appId:e,elements:i}=t,{data:r,error:o}=await s(a=>n.POST("/display/draw",{body:{app_id:e,elements:i},signal:a}),t.timeout);if(o)throw o;return r}async function rt(n,t){const{data:e,error:i}=await s(r=>n.DELETE("/display/draw",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function ot(n,t){const{display:e}=t,{data:i,error:r}=await s(o=>n.GET("/screen",{params:{query:{display:e}},parseAs:"blob",signal:o}),t.timeout);if(r)throw r;return i}async function st(n,t){const{data:e,error:i}=await s(r=>n.GET("/display/brightness",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function at(n,t){const{front:e,back:i}=t,r=h=>{if(typeof h=="number"){if(h<0||h>100)throw new Error("Brightness value must be between 0 and 100 or 'auto'");return String(h)}if(h==="auto")return"auto"},o=r(e),a=r(i),{data:d,error:f}=await s(h=>n.POST("/display/brightness",{params:{query:{front:o,back:a}},signal:h}),t.timeout);if(f)throw f;return d}class ct{async DisplayDraw(t){return await nt(this.apiClient,t)}async DisplayClear(t){return await rt(this.apiClient,t)}async DisplayScreenFrameGet(t){return await ot(this.apiClient,t)}async DisplayBrightnessGet(t){return await st(this.apiClient,t)}async DisplayBrightnessSet(t){return await at(this.apiClient,t)}}async function ut(n,t){const{appId:e,path:i}=t,{data:r,error:o}=await s(a=>n.POST("/audio/play",{params:{query:{app_id:e,path:i}},signal:a}),t.timeout);if(o)throw o;return r}async function ht(n,t){const{data:e,error:i}=await s(r=>n.DELETE("/audio/play",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function dt(n,t){const{data:e,error:i}=await s(r=>n.GET("/audio/volume",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function ft(n,t){const{volume:e}=t;if(typeof e!="number"||e<0||e>100)throw new Error("Volume must be a number between 0 and 100");const{data:i,error:r}=await s(o=>n.POST("/audio/volume",{params:{query:{volume:e}},signal:o}),t.timeout);if(r)throw r;return i}class lt{async AudioPlay(t){return await ut(this.apiClient,t)}async AudioStop(t){return await ht(this.apiClient,t)}async AudioVolumeGet(t){return await dt(this.apiClient,t)}async AudioVolumeSet(t){return await ft(this.apiClient,t)}}async function wt(n,t){const{data:e,error:i}=await s(r=>n.GET("/wifi/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function yt(n,t){const{data:e,error:i}=await s(r=>n.POST("/wifi/connect",{body:{ssid:t.ssid,password:t.password,security:t.security,ip_config:{ip_method:t.ipConfig.ipMethod,address:t.ipConfig.address,mask:t.ipConfig.mask,gateway:t.ipConfig.gateway}},signal:r}),t.timeout);if(i)throw i;return e}async function pt(n,t){const{data:e,error:i}=await s(r=>n.POST("/wifi/disconnect",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function St(n,t){const{data:e,error:i}=await s(r=>n.GET("/wifi/networks",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class Tt{async WifiStatusGet(t){return await wt(this.apiClient,t)}async WifiConnect(t){return await yt(this.apiClient,t)}async WifiDisconnect(t){return await pt(this.apiClient,t)}async WifiNetworksGet(t){return await St(this.apiClient,t)}}async function Et(n,t){const{path:e,file:i}=t,{data:r,error:o}=await s(a=>n.POST("/storage/write",{params:{query:{path:e}},headers:{"Content-Type":"application/octet-stream"},body:i,signal:a}),t.timeout);if(o)throw o;return r}async function mt(n,t){const{path:e,asArrayBuffer:i}=t,{data:r,error:o}=await s(a=>n.GET("/storage/read",{params:{query:{path:e}},parseAs:i?"arrayBuffer":"blob",signal:a}),t.timeout);if(o)throw o;return r}async function kt(n,t){const{path:e}=t,{data:i,error:r}=await s(o=>n.GET("/storage/list",{params:{query:{path:e}},signal:o}),t.timeout);if(r)throw r;return i}async function Ct(n,t){const{path:e}=t,{data:i,error:r}=await s(o=>n.DELETE("/storage/remove",{params:{query:{path:e}},signal:o}),t.timeout);if(r)throw r;return i}async function gt(n,t){const{path:e}=t,{data:i,error:r}=await s(o=>n.POST("/storage/mkdir",{params:{query:{path:e}},signal:o}),t.timeout);if(r)throw r;return i}async function bt(n,t){const{data:e,error:i}=await s(r=>n.GET("/storage/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class At{async StorageWrite(t){return await Et(this.apiClient,t)}async StorageRead(t){return await mt(this.apiClient,t)}async StorageListGet(t){return await kt(this.apiClient,t)}async StorageRemove(t){return await Ct(this.apiClient,t)}async StorageMkdir(t){return await gt(this.apiClient,t)}async StorageStatusGet(t){return await bt(this.apiClient,t)}}async function vt(n,t){const{data:e,error:i}=await s(r=>n.GET("/access",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Pt(n,t){let{mode:e,key:i}=t;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:o}=await s(a=>n.POST("/access",{params:{query:{mode:e,key:i}},signal:a}),t.timeout);if(o)throw o;return r}async function Gt(n,t){const{data:e,error:i}=await s(r=>n.GET("/name",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Lt(n,t){const{data:e,error:i}=await s(r=>n.POST("/name",{body:t,signal:r}),t.timeout);if(i)throw i;return e}class Ot{async SettingsAccessGet(t){return await vt(this.apiClient,t)}async SettingsAccessSet(t){const e=await Pt(this.apiClient,t);return t.mode==="key"&&t.key&&this.setApiKey(t.key),e}async SettingsNameGet(t){return await Gt(this.apiClient,t)}async SettingsNameSet(t){return await Lt(this.apiClient,t)}}async function Dt(n,t){const{data:e,error:i}=await s(r=>n.POST("/ble/enable",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Bt(n,t){const{data:e,error:i}=await s(r=>n.POST("/ble/disable",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Ut(n,t){const{data:e,error:i}=await s(r=>n.DELETE("/ble/pairing",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function $t(n,t){const{data:e,error:i}=await s(r=>n.GET("/ble/status",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class It{async BleEnable(t){return await Dt(this.apiClient,t)}async BleDisable(t){return await Bt(this.apiClient,t)}async BleUnpair(t){return await Ut(this.apiClient,t)}async BleStatusGet(t){return await $t(this.apiClient,t)}}async function Kt(n,t){const{keyName:e}=t,{data:i,error:r}=await s(o=>n.POST("/input",{params:{query:{key:e}},signal:o}),t.timeout);if(r)throw r;return i}class Rt{async InputSend(t){return await Kt(this.apiClient,t)}}async function Ft(n,t){const{data:e,error:i}=await s(r=>n.GET("/matter/commissioning",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Wt(n,t){const{data:e,error:i}=await s(r=>n.POST("/matter/commissioning",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}async function Mt(n,t){const{data:e,error:i}=await s(r=>n.DELETE("/matter/commissioning",{signal:r}),t==null?void 0:t.timeout);if(i)throw i;return e}class Nt{async MatterStatusGet(t){return await Ft(this.apiClient,t)}async MatterPair(t){return await Wt(this.apiClient,t)}async MatterErase(t){return await Mt(this.apiClient,t)}}const S="http://10.0.4.20",_t="https://proxy.busy.app",xt=/^https?:\/\/proxy(?:\.(?:dev|test|stage))?\.busy\.app$/i;function T(n){const t=n.split(".");if(t.length!==4)return!1;for(const e of t){if(e.length===0||e.length>1&&e[0]==="0"||!/^\d+$/.test(e))return!1;const i=Number(e);if(i<0||i>255)return!1}return!0}function E(n){return/\.local$/i.test(n)}class k{constructor(t){c(this,"addr");c(this,"apiSemver");c(this,"apiClient");c(this,"setApiKeyFn");c(this,"setTokenFn");c(this,"connectionType","unknown");if(!t||!t.addr&&!t.token)this.addr=S;else if(!t.addr)this.addr=_t;else{let o=t.addr.trim();if(/^https?:\/\//i.test(o)||(o=`http://${o}`),xt.test(o)&&!t.token)throw new Error("Token is required. Please provide it.");this.addr=o}this.apiSemver="";const{client:e,setApiKey:i,setToken:r}=D(`${this.addr}/api/`,this.SystemVersionGet.bind(this),t==null?void 0:t.token);this.apiClient=e,this.setApiKeyFn=i,this.setTokenFn=r,this.detectConnectionType()}async detectConnectionType(){const t=new URL(this.addr).hostname;if(!T(t)&&!E(t)){this.connectionType="wifi";return}const e=m({baseUrl:`${this.addr}/api/`});try{const{response:i}=await e.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){this.setApiKeyFn(t)}setToken(t){this.setTokenFn(t)}}function qt(n,t){t.forEach(e=>{Object.getOwnPropertyNames(e.prototype).forEach(i=>{Object.defineProperty(n.prototype,i,Object.getOwnPropertyDescriptor(e.prototype,i)||Object.create(null))})})}qt(k,[K,x,J,it,ct,lt,Tt,At,Ot,It,Rt,Nt]);var w=(n=>(n[n.FRONT=0]="FRONT",n[n.BACK=1]="BACK",n))(w||{});const C=3e3,g=new Set([1001,1006,1012,1013,1014,3008]);function b(n,t){if(t<0||t>=n.length)throw new Error(`Index ${t} is out of bounds (0…${n.length-1})`);const e=n[t];if(e===void 0)throw new Error(`Unexpected undefined at index ${t}`);return e}function jt(n,t){let e=0;const i=n.length,r=[];for(;e<i;){const o=b(n,e);if(e+=1,(o&128)!==0){const a=o&127;for(let d=0;d<a*t;d++)r.push(n[e+d]);e+=a*t}else{const a=o,d=n.slice(e,e+t);for(let f=0;f<a;f++)for(let h=0;h<t;h++)r.push(d[h]);e+=t}}return new Uint8Array(r)}function Vt(n){const t=new Uint8Array(n.length*2);let e=0,i=0;for(;e<n.length;){const r=b(n,e),o=r&15,a=r>>4&15;t[i]=o,t[i+1]=a,e+=1,i+=2}return t}const A=()=>typeof window<"u"&&typeof window.document<"u";class zt{constructor(t){c(this,"addr");c(this,"connected",!1);c(this,"apiKey");c(this,"apiSemver");c(this,"dataListeners",[]);c(this,"stopListeners",[]);c(this,"errorListeners",[]);c(this,"socket",null);if(this.config=t,!A())throw new Error("not browser");if(t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=S;else{let e=t.addr.trim();/^https?:\/\//i.test(e)||(e=`http://${e}`);try{const r=new URL(e).hostname;if(!T(r)&&!E(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=e}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const e of this.dataListeners)e(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const e of this.errorListeners)e(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=e=>{try{if(typeof e.data=="string")return;const i=new Uint8Array(e.data);let r;const o=this.config.deviceScreen===w.FRONT?3:2;try{const a=jt(i,o);this.config.deviceScreen===w.BACK?r=Vt(a):r=a,this.emitData(r)}catch{this.emitData(i)}}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=e=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:e}),this.emitStop()},this.socket.onclose=async e=>{if(this.socket=null,this.connected=!1,e.code===C||g.has(e.code)){this.emitError({code:e.code,message:e.reason,raw:e});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 Ht{constructor(t){c(this,"addr");c(this,"connected",!1);c(this,"apiKey");c(this,"apiSemver");c(this,"inputEvent");c(this,"dataListeners",[]);c(this,"stopListeners",[]);c(this,"errorListeners",[]);c(this,"socket",null);if(!A())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=S;else{let e=t.addr.trim();/^https?:\/\//i.test(e)||(e=`http://${e}`);try{const r=new URL(e).hostname;if(!T(r)&&!E(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=e}this.inputEvent={}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const e of this.dataListeners)e(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const e of this.errorListeners)e(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=e=>{try{if(typeof e.data=="string")return;const i=new Uint8Array(e.data);this.emitData(i)}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=e=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:e}),this.emitStop()},this.socket.onclose=async e=>{if(this.socket=null,this.connected=!1,e.code===C||g.has(e.code)){this.emitError({code:e.code,message:e.reason,raw:e});return}this.emitStop()}}sendInput({keyName:t,value:e}){if(!this.socket||!this.connected)throw new Error("WebSocket: Not connected");this.inputEvent[t]=e,this.socket.send(JSON.stringify(this.inputEvent)),e===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=k;exports.DeviceScreen=w;exports.Input=Ht;exports.ScreenStream=zt;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Client } from 'openapi-fetch';
2
+
1
3
  export declare type AccountInfo = components["schemas"]["AccountInfo"];
2
4
 
3
5
  export declare type AccountLink = components["schemas"]["AccountLink"];
@@ -11,10 +13,6 @@ declare class AccountMethods {
11
13
  * @returns {Promise<AccountInfo>} A promise that resolves to the account information.
12
14
  */
13
15
  AccountInfoGet(this: BusyBar, params?: TimeoutOptions): Promise<AccountInfo>;
14
- /**
15
- * @deprecated Use `AccountInfoGet` instead. will be removed in the next release.
16
- */
17
- Account(this: BusyBar, params?: TimeoutOptions): Promise<AccountInfo>;
18
16
  /**
19
17
  * Get account state.
20
18
  *
@@ -91,10 +89,6 @@ declare class AudioMethods {
91
89
  * @returns {Promise<SuccessResponse>} A promise that resolves on successful play command.
92
90
  */
93
91
  AudioPlay(this: BusyBar, params: AudioPlayParams): Promise<SuccessResponse>;
94
- /**
95
- * @deprecated Use `AudioPlay` instead. will be removed in the next release.
96
- */
97
- Audio(this: BusyBar, params: AudioPlayParams): Promise<SuccessResponse>;
98
92
  /**
99
93
  * Stop audio playback. Stops any currently playing audio.
100
94
  *
@@ -111,10 +105,6 @@ declare class AudioMethods {
111
105
  * @returns {Promise<AudioVolumeInfo>} A promise that resolves to the audio volume information.
112
106
  */
113
107
  AudioVolumeGet(this: BusyBar, params?: TimeoutOptions): Promise<AudioVolumeInfo>;
114
- /**
115
- * @deprecated Use `AudioVolumeGet` instead. will be removed in the next release.
116
- */
117
- AudioVolume(this: BusyBar, params?: TimeoutOptions): Promise<AudioVolumeInfo>;
118
108
  /**
119
109
  * Set audio volume.
120
110
  *
@@ -196,6 +186,12 @@ export declare class BusyBar {
196
186
  * @type {ApiSemver}
197
187
  */
198
188
  apiSemver: ApiSemver;
189
+ /**
190
+ * API Client instance.
191
+ */
192
+ readonly apiClient: BusyBarClient;
193
+ private setApiKeyFn;
194
+ private setTokenFn;
199
195
  /**
200
196
  * Detected connection type based on auth requirements.
201
197
  * - "wifi": Device requires authentication (returned 401/403).
@@ -231,8 +227,20 @@ export declare class BusyBar {
231
227
  * Sends a request without authentication credentials.
232
228
  */
233
229
  private detectConnectionType;
230
+ /**
231
+ * Sets API key for all subsequent requests.
232
+ * @param {string} key - API key to use in "X-API-Token" header.
233
+ */
234
+ setApiKey(key: string): void;
235
+ /**
236
+ * Sets Bearer token for all subsequent requests.
237
+ * @param {string} token - Bearer token to use in "Authorization" header.
238
+ */
239
+ setToken(token: string): void;
234
240
  }
235
241
 
242
+ declare type BusyBarClient = Client<paths, `${string}/${string}`>;
243
+
236
244
  export declare type BusyBarConfig = {
237
245
  addr?: string;
238
246
  token?: string;
@@ -338,7 +346,7 @@ declare interface components {
338
346
  * @description Check result status
339
347
  * @enum {string}
340
348
  */
341
- result?: "available" | "not_available" | "failure" | "none";
349
+ status?: "available" | "not_available" | "failure" | "none";
342
350
  };
343
351
  };
344
352
  /**
@@ -1017,10 +1025,6 @@ declare class DisplayMethods {
1017
1025
  * @returns {Promise<DisplayBrightnessInfo>} A promise that resolves to the brightness information.
1018
1026
  */
1019
1027
  DisplayBrightnessGet(this: BusyBar, params?: TimeoutOptions): Promise<DisplayBrightnessInfo>;
1020
- /**
1021
- * @deprecated Use `DisplayBrightnessGet` instead. will be removed in the next release.
1022
- */
1023
- DisplayBrightness(this: BusyBar, params?: TimeoutOptions): Promise<DisplayBrightnessInfo>;
1024
1028
  /**
1025
1029
  * Set display brightness.
1026
1030
  *
@@ -3913,10 +3917,6 @@ declare class SettingsMethods {
3913
3917
  * @returns {Promise<HttpAccessInfo>} A promise that resolves to the access configuration.
3914
3918
  */
3915
3919
  SettingsAccessGet(this: BusyBar, params?: TimeoutOptions): Promise<HttpAccessInfo>;
3916
- /**
3917
- * @deprecated Use `SettingsAccessGet` instead. will be removed in the next release.
3918
- */
3919
- SettingsAccess(this: BusyBar, params?: TimeoutOptions): Promise<HttpAccessInfo>;
3920
3920
  /**
3921
3921
  * Set HTTP API access over Wi-Fi configuration.
3922
3922
  *
@@ -3935,10 +3935,6 @@ declare class SettingsMethods {
3935
3935
  * @returns {Promise<NameInfo>} A promise that resolves to the device name.
3936
3936
  */
3937
3937
  SettingsNameGet(this: BusyBar, params?: TimeoutOptions): Promise<NameInfo>;
3938
- /**
3939
- * @deprecated Use `SettingsNameGet` instead. will be removed in the next release.
3940
- */
3941
- SettingsName(this: BusyBar, params?: TimeoutOptions): Promise<NameInfo>;
3942
3938
  /**
3943
3939
  * Set device name.
3944
3940
  *
@@ -4007,10 +4003,6 @@ declare class StorageMethods {
4007
4003
  * @returns {Promise<StorageList>} A promise that resolves to a list of files and directories.
4008
4004
  */
4009
4005
  StorageListGet(this: BusyBar, params: StorageReadDirectoryParams): Promise<StorageList>;
4010
- /**
4011
- * @deprecated Use `StorageListGet` instead. will be removed in the next release.
4012
- */
4013
- StorageList(this: BusyBar, params: StorageReadDirectoryParams): Promise<StorageList>;
4014
4006
  /**
4015
4007
  * Remove a file on internal storage. Removes a file with a specified path.
4016
4008
  *
@@ -4037,10 +4029,6 @@ declare class StorageMethods {
4037
4029
  * @returns {Promise<StorageStatus>} A promise that resolves to the storage status.
4038
4030
  */
4039
4031
  StorageStatusGet(this: BusyBar, params?: TimeoutOptions): Promise<StorageStatus>;
4040
- /**
4041
- * @deprecated Use `StorageStatusGet` instead. will be removed in the next release.
4042
- */
4043
- StorageStatus(this: BusyBar, params?: TimeoutOptions): Promise<StorageStatus>;
4044
4032
  }
4045
4033
 
4046
4034
  export declare interface StorageReadDirectoryParams extends TimeoutOptions {
@@ -4071,10 +4059,6 @@ declare class SystemMethods {
4071
4059
  * @returns {Promise<VersionInfo>} A promise that resolves to an object containing the `api_semver` string.
4072
4060
  */
4073
4061
  SystemVersionGet(this: BusyBar, params?: TimeoutOptions): Promise<VersionInfo>;
4074
- /**
4075
- * @deprecated Use `SystemVersionGet` instead. will be removed in the next release.
4076
- */
4077
- SystemVersion(this: BusyBar, params?: TimeoutOptions): Promise<VersionInfo>;
4078
4062
  /**
4079
4063
  * Get device status.
4080
4064
  *
@@ -4083,10 +4067,6 @@ declare class SystemMethods {
4083
4067
  * @returns {Promise<Status>} Current status of the device.
4084
4068
  */
4085
4069
  SystemStatusGet(this: BusyBar, params?: TimeoutOptions): Promise<Status>;
4086
- /**
4087
- * @deprecated Use `SystemStatusGet` instead. will be removed in the next release.
4088
- */
4089
- SystemStatus(this: BusyBar, params?: TimeoutOptions): Promise<Status>;
4090
4070
  /**
4091
4071
  * Get system status.
4092
4072
  *
@@ -4095,10 +4075,6 @@ declare class SystemMethods {
4095
4075
  * @returns {Promise<StatusSystem>} Current system status.
4096
4076
  */
4097
4077
  SystemInfoGet(this: BusyBar, params?: TimeoutOptions): Promise<StatusSystem>;
4098
- /**
4099
- * @deprecated Use `SystemInfoGet` instead. will be removed in the next release.
4100
- */
4101
- SystemInfo(this: BusyBar, params?: TimeoutOptions): Promise<StatusSystem>;
4102
4078
  /**
4103
4079
  * Get power status.
4104
4080
  *
@@ -4107,14 +4083,6 @@ declare class SystemMethods {
4107
4083
  * @returns {Promise<StatusPower>} Current power status.
4108
4084
  */
4109
4085
  SystemStatusPowerGet(this: BusyBar, params?: TimeoutOptions): Promise<StatusPower>;
4110
- /**
4111
- * @deprecated Use `SystemStatusPowerGet` instead. will be removed in the next release.
4112
- */
4113
- SystemStatusPower(this: BusyBar, params?: TimeoutOptions): Promise<StatusPower>;
4114
- }
4115
-
4116
- export declare interface SystemUpdateParams extends TimeoutOptions {
4117
- file: BusyFile;
4118
4086
  }
4119
4087
 
4120
4088
  declare class TimeMethods {
@@ -4126,10 +4094,6 @@ declare class TimeMethods {
4126
4094
  * @returns {Promise<TimestampInfo>} A promise that resolves to the timestamp information.
4127
4095
  */
4128
4096
  TimeGet(this: BusyBar, params?: TimeoutOptions): Promise<TimestampInfo>;
4129
- /**
4130
- * @deprecated Use `TimeGet` instead. will be removed in the next release.
4131
- */
4132
- SystemTime(this: BusyBar, params?: TimeoutOptions): Promise<TimestampInfo>;
4133
4097
  /**
4134
4098
  * Set system timestamp.
4135
4099
  *
@@ -4140,9 +4104,13 @@ declare class TimeMethods {
4140
4104
  */
4141
4105
  TimeTimestampSet(this: BusyBar, params: TimeTimestampParams): Promise<SuccessResponse>;
4142
4106
  /**
4143
- * @deprecated Use `TimeTimestampSet` instead. will be removed in the next release.
4107
+ * Get current timezone.
4108
+ *
4109
+ * @param {TimeoutOptions} [params] - Optional parameters.
4110
+ * @param {TimeoutOptions['timeout']} [params.timeout] - Request timeout in milliseconds.
4111
+ * @returns {Promise<TimezoneInfo>} A promise that resolves to the timezone information.
4144
4112
  */
4145
- SystemTimeTimestamp(this: BusyBar, params: TimeTimestampParams): Promise<SuccessResponse>;
4113
+ TimeTimezoneGet(this: BusyBar, params?: TimeoutOptions): Promise<TimezoneInfo>;
4146
4114
  /**
4147
4115
  * Set system timezone.
4148
4116
  *
@@ -4152,10 +4120,6 @@ declare class TimeMethods {
4152
4120
  * @returns {Promise<SuccessResponse>} A promise that resolves on success.
4153
4121
  */
4154
4122
  TimeTimezoneSet(this: BusyBar, params: TimeTimezoneParams): Promise<SuccessResponse>;
4155
- /**
4156
- * @deprecated Use `TimeTimezoneSet` instead. will be removed in the next release.
4157
- */
4158
- SystemTimeTimezone(this: BusyBar, params: TimeTimezoneParams): Promise<SuccessResponse>;
4159
4123
  /**
4160
4124
  * Get list of supported timezones.
4161
4125
  *
@@ -4164,10 +4128,6 @@ declare class TimeMethods {
4164
4128
  * @returns {Promise<TimezoneList>} A promise that resolves to a list of timezone items.
4165
4129
  */
4166
4130
  TimeTzListGet(this: BusyBar, params?: TimeoutOptions): Promise<TimezoneList>;
4167
- /**
4168
- * @deprecated Use `TimeTzListGet` instead. will be removed in the next release.
4169
- */
4170
- SystemTimeTzList(this: BusyBar, params?: TimeoutOptions): Promise<TimezoneList>;
4171
4131
  }
4172
4132
 
4173
4133
  export declare interface TimeoutOptions {
@@ -4196,11 +4156,24 @@ export declare interface UpdateChangelogParams extends TimeoutOptions {
4196
4156
  version: string;
4197
4157
  }
4198
4158
 
4159
+ export declare interface UpdateFromFileParams extends TimeoutOptions {
4160
+ file: BusyFile;
4161
+ }
4162
+
4199
4163
  export declare interface UpdateInstallParams extends TimeoutOptions {
4200
4164
  version: string;
4201
4165
  }
4202
4166
 
4203
4167
  declare class UpdateMethods {
4168
+ /**
4169
+ * Upload firmware update package.
4170
+ *
4171
+ * @param {UpdateParams} params - Update parameters.
4172
+ * @param {BusyFile} params.file - Firmware TAR file to upload.
4173
+ * @param {TimeoutOptions['timeout']} [params.timeout] - Request timeout in milliseconds.
4174
+ * @returns {Promise<SuccessResponse>} A promise that resolves when upload is complete.
4175
+ */
4176
+ UpdateFromFile(this: BusyBar, params: UpdateFromFileParams): Promise<SuccessResponse>;
4204
4177
  /**
4205
4178
  * Start firmware update check.
4206
4179
  *
@@ -4209,10 +4182,6 @@ declare class UpdateMethods {
4209
4182
  * @returns {Promise<SuccessResponse>} A promise that resolves to the update check result.
4210
4183
  */
4211
4184
  UpdateCheck(this: BusyBar, params?: TimeoutOptions): Promise<SuccessResponse>;
4212
- /**
4213
- * @deprecated Use `UpdateCheck` instead. will be removed in the next release.
4214
- */
4215
- SystemUpdateCheck(this: BusyBar, params?: TimeoutOptions): Promise<SuccessResponse>;
4216
4185
  /**
4217
4186
  * Get firmware update status.
4218
4187
  *
@@ -4221,10 +4190,6 @@ declare class UpdateMethods {
4221
4190
  * @returns {Promise<UpdateStatus>} A promise that resolves to the update status.
4222
4191
  */
4223
4192
  UpdateStatusGet(this: BusyBar, params?: TimeoutOptions): Promise<UpdateStatus>;
4224
- /**
4225
- * @deprecated Use `UpdateStatusGet` instead. will be removed in the next release.
4226
- */
4227
- SystemUpdateStatus(this: BusyBar, params?: TimeoutOptions): Promise<UpdateStatus>;
4228
4193
  /**
4229
4194
  * Get firmware update changelog.
4230
4195
  *
@@ -4234,10 +4199,6 @@ declare class UpdateMethods {
4234
4199
  * @returns {Promise<UpdateChangelog>} A promise that resolves to the changelog content.
4235
4200
  */
4236
4201
  UpdateChangelogGet(this: BusyBar, params: UpdateChangelogParams): Promise<UpdateChangelog>;
4237
- /**
4238
- * @deprecated Use `UpdateChangelogGet` instead. will be removed in the next release.
4239
- */
4240
- SystemUpdateChangelog(this: BusyBar, params: UpdateChangelogParams): Promise<UpdateChangelog>;
4241
4202
  /**
4242
4203
  * Start firmware update installation.
4243
4204
  *
@@ -4280,10 +4241,6 @@ declare class WifiMethods {
4280
4241
  * @returns {Promise<WifiStatusResponse>} A promise that resolves to the Wi-Fi status.
4281
4242
  */
4282
4243
  WifiStatusGet(this: BusyBar, params?: TimeoutOptions): Promise<WifiStatusResponse>;
4283
- /**
4284
- * @deprecated Use `WifiStatusGet` instead. will be removed in the next release.
4285
- */
4286
- WifiStatus(this: BusyBar, params?: TimeoutOptions): Promise<WifiStatusResponse>;
4287
4244
  /**
4288
4245
  * Connects to Wi-Fi network.
4289
4246
  *
@@ -4312,10 +4269,6 @@ declare class WifiMethods {
4312
4269
  * @returns {Promise<WifiNetworkResponse>} A promise that resolves to a list of available networks.
4313
4270
  */
4314
4271
  WifiNetworksGet(this: BusyBar, params?: TimeoutOptions): Promise<WifiNetworkResponse>;
4315
- /**
4316
- * @deprecated Use `WifiNetworksGet` instead. will be removed in the next release.
4317
- */
4318
- WifiNetworks(this: BusyBar, params?: TimeoutOptions): Promise<WifiNetworkResponse>;
4319
4272
  }
4320
4273
 
4321
4274
  export declare type WifiNetwork = components["schemas"]["Network"];