@busy-app/busy-lib 0.6.0 → 0.7.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 +1 -1
- package/dist/index.d.ts +369 -193
- package/dist/index.js +1112 -702
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var k=Object.defineProperty;var P=(e,t,i)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var o=(e,t,i)=>P(e,typeof t!="symbol"?t+"":t,i);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("openapi-fetch"),b=(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 i;return t&&(t instanceof Headers?i=t.get("Content-Type")??t.get("content-type")??void 0:typeof t=="object"&&(i=t["Content-Type"]??t["content-type"]),i==="application/x-www-form-urlencoded")?e&&typeof e=="object"&&!(e instanceof URLSearchParams)?new URLSearchParams(e).toString():String(e):JSON.stringify(e)};let m,w,h=null;async function A(){if(!w){if(!m)throw new Error("getApiVersionFn is not set");h||(h=(async()=>{const e=await m();if(!e.api_semver)throw new Error("Empty API version");w=e.api_semver})().finally(()=>{h=null})),await h}}async function y(e){const n=(e.headers.get("content-type")||"").includes("application/json")?await e.clone().json():await e.clone().text(),s=typeof n=="object"&&n!==null?n.error||n.message:typeof n=="string"?n:void 0;return Object.assign(new Error(s||`HTTP ${e.status} ${e.statusText}`),{status:e.status,statusText:e.statusText,body:n})}let E;function I(e){E=e}let d;const B={async onRequest({request:e,schemaPath:t}){return d&&e.headers.set("Authorization",`Bearer ${d}`),t!=="/version"&&(await A(),w&&e.headers.set("X-API-Sem-Ver",w),E&&e.headers.set("X-API-Token",E)),e},async onResponse({request:e,response:t,options:i,schemaPath:n}){if(t.ok)return t;if(n==="/version")throw await y(t);if(t.status!==405)throw await y(t);w=void 0,await A(),w&&e.headers.set("X-API-Sem-Ver",w),d&&e.headers.set("Authorization",`Bearer ${d}`);const s=await(i.fetch??fetch)(e);if(s.ok)return s;throw await y(s)}};let r=null;function z(e,t,i){m=t,d=i??void 0,r=v({baseUrl:e,bodySerializer:b}),r.use(B)}async function D(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/account");if(t)throw t;return e}async function L(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/account");if(t)throw t;return e}async function O(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/account/link");if(t)throw t;return e}async function W(e){const{appId:t,fileName:i,file:n}=e;if(!r)throw new Error("API client is not initialized");const{data:s,error:a}=await r.POST("/assets/upload",{params:{query:{app_id:t,file:i}},headers:{"Content-Type":"application/octet-stream"},body:n});if(a)throw a;return s}async function C(e){const{appId:t}=e;if(!r)throw new Error("API client is not initialized");const{data:i,error:n}=await r.DELETE("/assets/upload",{params:{query:{app_id:t}}});if(n)throw n;return i}async function R(e){if(!r)throw new Error("API client is not initialized");const{appId:t,elements:i}=e,{data:n,error:s}=await r.POST("/display/draw",{body:{app_id:t,elements:i}});if(s)throw s;return n}async function U(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/display/draw");if(t)throw t;return e}async function K(e){const{appId:t,path:i}=e;if(!r)throw new Error("API client is not initialized");const{data:n,error:s}=await r.POST("/audio/play",{params:{query:{app_id:t,path:i}}});if(s)throw s;return n}async function $(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/audio/play");if(t)throw t;return e}async function _(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/wifi/status");if(t)throw t;return e}async function F(e){if(!r)throw new Error("API client is not initialized");const{data:t,error:i}=await r.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}}});if(i)throw i;return t}async function G(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/wifi/disconnect");if(t)throw t;return e}async function q(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/wifi/networks");if(t)throw t;return e}async function N(e){if(!r)throw new Error("API client is not initialized");const{path:t,file:i}=e,{data:n,error:s}=await r.POST("/storage/write",{params:{query:{path:t}},headers:{"Content-Type":"application/octet-stream"},body:i});if(s)throw s;return n}async function V(e){if(!r)throw new Error("API client is not initialized");const{path:t,asArrayBuffer:i}=e,{data:n,error:s}=await r.GET("/storage/read",{params:{query:{path:t}},parseAs:i?"arrayBuffer":"blob"});if(s)throw s;return n}async function x(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.GET("/storage/list",{params:{query:{path:t}}});if(n)throw n;return i}async function j(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.DELETE("/storage/remove",{params:{query:{path:t}}});if(n)throw n;return i}async function H(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.POST("/storage/mkdir",{params:{query:{path:t}}});if(n)throw n;return i}async function J(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/storage/status");if(t)throw t;return e}async function X(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/version");if(t)throw t;return e}async function M(e){if(!r)throw new Error("API client is not initialized");const{name:t,file:i}=e,{data:n,error:s}=await r.POST("/update",{params:{query:{name:t}},headers:{"Content-Type":"application/octet-stream"},body:i});if(s)throw s;return n}async function Q(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/status");if(t)throw t;return e}async function Y(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/status/system");if(t)throw t;return e}async function Z(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/status/power");if(t)throw t;return e}async function tt(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/time");if(t)throw t;return e}async function et(e){if(!r)throw new Error("API client is not initialized");const{data:t,error:i}=await r.POST("/time/timestamp",{params:{query:e}});if(i)throw i;return t}async function it(e){if(!r)throw new Error("API client is not initialized");const{data:t,error:i}=await r.POST("/time/timezone",{params:{query:e}});if(i)throw i;return t}async function rt(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/display/brightness");if(t)throw t;return e}async function nt(e){if(!r)throw new Error("API client is not initialized");const{front:t,back:i}=e,n=u=>{if(typeof u=="number"){if(u<0||u>100)throw new Error("Brightness value must be between 0 and 100 or 'auto'");return String(u)}if(u==="auto")return"auto"},s=n(t),a=n(i),{data:c,error:f}=await r.POST("/display/brightness",{params:{query:{front:s,back:a}}});if(f)throw f;return c}async function st(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/audio/volume");if(t)throw t;return e}async function ot(e){if(!r)throw new Error("API client is not initialized");const{volume:t}=e;if(typeof t!="number"||t<0||t>100)throw new Error("Volume must be a number between 0 and 100");const{data:i,error:n}=await r.POST("/audio/volume",{params:{query:{volume:t}}});if(n)throw n;return i}async function at(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/access");if(t)throw t;return e}async function ct(e){if(!r)throw new Error("API client is not initialized");const{mode:t,key:i}=e;if(!/^\d{4,10}$/.test(String(i)))throw new Error("Key must be a string of 4 to 10 digits");const{data:n,error:s}=await r.POST("/access",{params:{query:{mode:t,key:i}}});if(s)throw s;return n}async function ft(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/name");if(t)throw t;return e}async function ut(e){if(!r)throw new Error("API client is not initialized");const{data:t,error:i}=await r.POST("/name",{body:e});if(i)throw i;return t}async function wt(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/ble/enable");if(t)throw t;return e}async function dt(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/ble/disable");if(t)throw t;return e}async function ht(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/ble/pairing");if(t)throw t;return e}async function lt(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/ble/status");if(t)throw t;return e}async function pt(e){if(!r)throw new Error("API client is not initialized");const{keyName:t}=e,{data:i,error:n}=await r.POST("/input",{params:{query:{key:t}}});if(n)throw n;return i}const yt="http://10.0.4.20",mt="https://proxy.busy.app",Et=/^https?:\/\/proxy(?:\.(?:dev|test|stage))?\.busy\.app$/i;class At{constructor(t){o(this,"addr");o(this,"apiSemver");if(!t||!t.addr&&!t.token)this.addr=yt;else if(!t.addr)this.addr=mt;else{let i=t.addr.trim();if(/^https?:\/\//i.test(i)||(i=`http://${i}`),Et.test(i)&&!t.token)throw new Error("Token is required. Please provide it.");this.addr=i}this.apiSemver="",z(`${this.addr}/api/`,this.getApiVersion.bind(this),t==null?void 0:t.token)}async getApiVersion(){const t=await X();return this.apiSemver=t.api_semver,t}async updateFirmware(t){return await M(t)}async deviceStatus(){return await Q()}async systemStatus(){return await Y()}async powerStatus(){return await Z()}async getTime(){return await tt()}async setTimestamp(t){return await et(t)}async setTimezone(t){return await it(t)}async getMqttStatus(){return await D()}async unlinkAccount(){return await L()}async linkAccount(){return await O()}async uploadAsset(t){return await W(t)}async deleteAssets(t){return await C(t)}async drawDisplay(t){return await R(t)}async clearDisplay(){return await U()}async playSound(t){return await K(t)}async stopSound(){return await $()}async enableWifi(){throw new Error("[DEPRECATED] BusyBar.enableWifi: This method is deprecated since v0.5.0 and will be removed in v0.7.0. It is no longer supported and does nothing. Works only with BusyLib v0.5.0 and device firmware v0.3.0.")}async disableWifi(){throw new Error("[DEPRECATED] BusyBar.disableWifi: This method is deprecated since v0.5.0 and will be removed in v0.7.0. It is no longer supported and does nothing. Works only with BusyLib v0.5.0 and device firmware v0.3.0.")}async statusWifi(){return await _()}async connectWifi(t){return await F(t)}async disconnectWifi(){return await G()}async networksWifi(){return await q()}async forgetWifi(){throw new Error("[DEPRECATED] BusyBar.forgetWifi: This method is deprecated since v0.5.0 and will be removed in v0.7.0. It is no longer supported and does nothing. Works only with BusyLib v0.5.0 and device firmware v0.3.0.")}async uploadFile(t){return await N(t)}async downloadFile(t){return await V(t)}async readDirectory(t){return await x(t)}async removeResource(t){return await j(t)}async createDirectory(t){return await H(t)}async statusStorage(){return await J()}async getDisplayBrightness(){return await rt()}async setDisplayBrightness(t){return await nt(t)}async getAudioVolume(){return await st()}async setAudioVolume(t){return await ot(t)}async getHttpAccess(){return await at()}async setHttpAccess(t){const i=await ct(t);return t.mode==="key"&&t.key&&this.setApiKey(t.key),i}async getName(){return await ft()}async setName(t){return await ut(t)}setApiKey(t){I(t)}async enableBle(){return await wt()}async disableBle(){return await dt()}async pairingBle(){return await ht()}async statusBle(){return await lt()}async pressButton(t){return await pt(t)}}var l=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(l||{});const g=3e3,S=new Set([1001,1006,1012,1013,1014,3008]);function T(e,t){if(t<0||t>=e.length)throw new Error(`Index ${t} is out of bounds (0…${e.length-1})`);const i=e[t];if(i===void 0)throw new Error(`Unexpected undefined at index ${t}`);return i}function gt(e,t){let i=0;const n=e.length,s=[];for(;i<n;){const a=T(e,i);if(i+=1,(a&128)!==0){const c=a&127;for(let f=0;f<c*t;f++)s.push(e[i+f]);i+=c*t}else{const c=a,f=e.slice(i,i+t);for(let u=0;u<c;u++)for(let p=0;p<t;p++)s.push(f[p]);i+=t}}return new Uint8Array(s)}function St(e){const t=new Uint8Array(e.length*2);let i=0,n=0;for(;i<e.length;){const s=T(e,i),a=s&15,c=s>>4&15;t[n]=a,t[n+1]=c,i+=1,n+=2}return t}class Tt{constructor(t){o(this,"connected",!1);o(this,"apiKey");o(this,"apiSemver");o(this,"dataListeners",[]);o(this,"stopListeners",[]);o(this,"errorListeners",[]);o(this,"socket",null);if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser");t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver)}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const i of this.dataListeners)i(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const i of this.errorListeners)i(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();let t;if(this.config.mode==="cloud"?t=new URL(`${this.config.domain}/bars/${this.config.idDevice}/ws`):this.config.mode==="local"&&(t=new URL(`${this.config.barUrl}/api/screen/ws`),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.config.mode==="cloud"?this.socket.send(JSON.stringify({token:this.config.token,display:this.config.deviceScreen})):this.config.mode==="local"&&this.socket.send(JSON.stringify({display:this.config.deviceScreen})),this.connected=!0)},this.socket.binaryType="arraybuffer",this.socket.onmessage=i=>{try{if(typeof i.data=="string")return;const n=new Uint8Array(i.data);if(this.config.mode==="cloud")this.emitData(n);else if(this.config.mode==="local"){let s;const a=this.config.deviceScreen===l.FRONT?3:2;try{const c=gt(n,a);this.config.deviceScreen===l.BACK?s=St(c):s=c,this.emitData(s)}catch{this.emitData(n)}}}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=i=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:i}),this.emitStop()},this.socket.onclose=async i=>{if(this.socket=null,this.connected=!1,i.code===g||S.has(i.code)){this.emitError({code:i.code,message:i.reason,raw:i});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 kt{constructor(t){o(this,"connected",!1);o(this,"apiKey");o(this,"apiSemver");o(this,"inputEvent");o(this,"dataListeners",[]);o(this,"stopListeners",[]);o(this,"errorListeners",[]);o(this,"socket",null);if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser");t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver),this.inputEvent={}}onData(t){this.dataListeners.push(t)}onStop(t){this.stopListeners.push(t)}onError(t){this.errorListeners.push(t)}emitData(t){for(const i of this.dataListeners)i(t)}emitStop(){for(const t of this.stopListeners)t()}emitError(t){for(const i of this.errorListeners)i(t)}async openWebsocket(){this.socket&&await this.closeWebsocket();let t;if(this.config.mode==="cloud"?t=new URL(`${this.config.domain}/bars/${this.config.idDevice}/ws`):this.config.mode==="local"&&(t=new URL(`${this.config.barUrl}/api/input`),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=i=>{try{if(typeof i.data=="string")return;const n=new Uint8Array(i.data);this.emitData(n)}catch{this.connected=!1,this.emitStop()}},this.socket.onerror=i=>{this.connected=!1,this.emitError({code:1006,message:"WebSocket error occurred",raw:i}),this.emitStop()},this.socket.onclose=async i=>{if(this.socket=null,this.connected=!1,i.code===g||S.has(i.code)){this.emitError({code:i.code,message:i.reason,raw:i});return}this.emitStop()}}sendInput({keyName:t,value:i}){if(!this.socket||!this.connected)throw new Error("WebSocket: Not connected");this.inputEvent[t]=i,this.socket.send(JSON.stringify(this.inputEvent)),i===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=At;exports.DeviceScreen=l;exports.Input=kt;exports.ScreenStream=Tt;
|
|
1
|
+
"use strict";var O=Object.defineProperty;var U=(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)=>U(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("openapi-fetch"),k="http://10.0.4.20",C="https://proxy.busy.app",I=/^https?:\/\/proxy(?:\.(?:dev|test|stage))?\.busy\.app$/i,W=(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,y,w=null;async function b(){if(!y){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");y=e.api_semver})().finally(()=>{w=null})),await w}}async function m(e){const r=(e.headers.get("content-type")||"").includes("application/json")?await e.clone().json():await e.clone().text(),i=typeof r=="object"&&r!==null?r.error||r.message:typeof r=="string"?r:void 0;return Object.assign(new Error(i||`HTTP ${e.status} ${e.statusText}`),{status:e.status,statusText:e.statusText,body:r})}let E;function R(e){E=e}let f;const $={async onRequest({request:e,schemaPath:t}){return f&&e.headers.set("Authorization",`Bearer ${f}`),t!=="/version"&&(await b(),y&&e.headers.set("X-API-Sem-Ver",y),E&&e.headers.set("X-API-Token",E)),e},async onResponse({request:e,response:t,options:n,schemaPath:r}){if(t.ok)return t;if(r==="/version")throw await m(t);if(t.status!==405)throw await m(t);y=void 0,await b(),y&&e.headers.set("X-API-Sem-Ver",y),f&&e.headers.set("Authorization",`Bearer ${f}`);const i=await(n.fetch??fetch)(e);if(i.ok)return i;throw await m(i)}};let p=null;function N(e,t,n){T=t,f=n??void 0,p=v({baseUrl:e,bodySerializer:W}),p.use($)}function o(){if(!p)throw new Error("API client is not initialized");return p}async function a(e,t=3e3){if(t<=0)return await e();const n=new AbortController,r=setTimeout(()=>n.abort(),t);try{return await e(n.signal)}catch(i){throw i instanceof DOMException&&i.name==="AbortError"?new Error(`Request timed out after ${t}ms`):i}finally{clearTimeout(r)}}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 r=Number(n);if(r<0||r>255)return!1}return!0}function A(e){return/\.local$/i.test(e)}async function K(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/account",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function V(e){const t=o(),{data:n,error:r}=await a(i=>t.DELETE("/account",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function G(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/account/link",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function _(e){const{appId:t,fileName:n,file:r}=e,i=o(),{data:s,error:c}=await a(h=>i.POST("/assets/upload",{params:{query:{app_id:t,file:n}},headers:{"Content-Type":"application/octet-stream"},body:r,signal:h}),e.timeout);if(c)throw c;return s}async function F(e){const{appId:t}=e,n=o(),{data:r,error:i}=await a(s=>n.DELETE("/assets/upload",{params:{query:{app_id:t}},signal:s}),e.timeout);if(i)throw i;return r}async function q(e){const t=o(),{appId:n,elements:r}=e,{data:i,error:s}=await a(c=>t.POST("/display/draw",{body:{app_id:n,elements:r},signal:c}),e.timeout);if(s)throw s;return i}async function x(e){const t=o(),{data:n,error:r}=await a(i=>t.DELETE("/display/draw",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function j(e){const t=o(),{appId:n,path:r}=e,{data:i,error:s}=await a(c=>t.POST("/audio/play",{params:{query:{app_id:n,path:r}},signal:c}),e.timeout);if(s)throw s;return i}async function H(e){const t=o(),{data:n,error:r}=await a(i=>t.DELETE("/audio/play",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function z(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/wifi/status",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function M(e){const t=o(),{data:n,error:r}=await a(i=>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:i}),e.timeout);if(r)throw r;return n}async function J(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/wifi/disconnect",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function X(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/wifi/networks",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function Q(e){const t=o(),{path:n,file:r}=e,{data:i,error:s}=await a(c=>t.POST("/storage/write",{params:{query:{path:n}},headers:{"Content-Type":"application/octet-stream"},body:r,signal:c}),e.timeout);if(s)throw s;return i}async function Y(e){const t=o(),{path:n,asArrayBuffer:r}=e,{data:i,error:s}=await a(c=>t.GET("/storage/read",{params:{query:{path:n}},parseAs:r?"arrayBuffer":"blob",signal:c}),e.timeout);if(s)throw s;return i}async function Z(e){const t=o(),{path:n}=e,{data:r,error:i}=await a(s=>t.GET("/storage/list",{params:{query:{path:n}},signal:s}),e.timeout);if(i)throw i;return r}async function tt(e){const t=o(),{path:n}=e,{data:r,error:i}=await a(s=>t.DELETE("/storage/remove",{params:{query:{path:n}},signal:s}),e.timeout);if(i)throw i;return r}async function et(e){const t=o(),{path:n}=e,{data:r,error:i}=await a(s=>t.POST("/storage/mkdir",{params:{query:{path:n}},signal:s}),e.timeout);if(i)throw i;return r}async function nt(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/storage/status",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function rt(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/version",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function it(e){const t=o(),{name:n,file:r}=e,{data:i,error:s}=await a(c=>t.POST("/update",{params:{query:{name:n}},headers:{"Content-Type":"application/octet-stream"},body:r,signal:c}),e.timeout);if(s)throw s;return i}async function st(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/status",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function ot(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/status/system",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function at(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/status/power",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function ct(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/time",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function ut(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/time/timestamp",{params:{query:{...e,timeout:void 0}},signal:i}),e.timeout);if(r)throw r;return n}async function dt(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/time/timezone",{params:{query:{...e,timeout:void 0}},signal:i}),e.timeout);if(r)throw r;return n}async function ht(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/display/brightness",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function yt(e){const t=o(),{front:n,back:r}=e,i=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"},s=i(n),c=i(r),{data:h,error:l}=await a(d=>t.POST("/display/brightness",{params:{query:{front:s,back:c}},signal:d}),e.timeout);if(l)throw l;return h}async function lt(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/audio/volume",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function ft(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:r,error:i}=await a(s=>t.POST("/audio/volume",{params:{query:{volume:n}},signal:s}),e.timeout);if(i)throw i;return r}async function wt(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/access",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function pt(e){const t=o();let{mode:n,key:r}=e;if(r=r??"",String(r).trim()&&!/^\d{4,10}$/.test(String(r)))throw new Error("Key must be a string of 4 to 10 digits");const{data:i,error:s}=await a(c=>t.POST("/access",{params:{query:{mode:n,key:r}},signal:c}),e.timeout);if(s)throw s;return i}async function St(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/name",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function mt(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/name",{body:e,signal:i}),e.timeout);if(r)throw r;return n}async function Tt(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/ble/enable",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function Et(e){const t=o(),{data:n,error:r}=await a(i=>t.POST("/ble/disable",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function kt(e){const t=o(),{data:n,error:r}=await a(i=>t.DELETE("/ble/pairing",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function gt(e){const t=o(),{data:n,error:r}=await a(i=>t.GET("/ble/status",{signal:i}),e==null?void 0:e.timeout);if(r)throw r;return n}async function At(e){const t=o(),{keyName:n}=e,{data:r,error:i}=await a(s=>t.POST("/input",{params:{query:{key:n}},signal:s}),e.timeout);if(i)throw i;return r}class bt{constructor(t){u(this,"addr");u(this,"apiSemver");u(this,"connectionType","unknown");if(!t||!t.addr&&!t.token)this.addr=k;else if(!t.addr)this.addr=C;else{let n=t.addr.trim();if(/^https?:\/\//i.test(n)||(n=`http://${n}`),I.test(n)&&!t.token)throw new Error("Token is required. Please provide it.");this.addr=n}this.apiSemver="",N(`${this.addr}/api/`,this.SystemVersion.bind(this),t==null?void 0:t.token),this.detectConnectionType()}async detectConnectionType(){const t=new URL(this.addr).hostname;if(!g(t)&&!A(t)){this.connectionType="wifi";return}const n=v({baseUrl:`${this.addr}/api/`});try{const{response:r}=await n.GET("/name");if(r.status===401||r.status===403)this.connectionType="wifi";else if(r.ok)this.connectionType="usb";else throw new Error(`Failed to detect connection type. Status: ${r.status}`)}catch(r){throw r}}async SystemVersion(t){const n=await rt(t);return this.apiSemver=n.api_semver,n}async getApiVersion(t){return this.SystemVersion(t)}async SystemUpdate(t){return await it(t)}async updateFirmware(t){return this.SystemUpdate(t)}async SystemStatus(t){return await st(t)}async deviceStatus(t){return this.SystemStatus(t)}async SystemInfo(t){return await ot(t)}async systemStatus(t){return this.SystemInfo(t)}async SystemStatusPower(t){return await at(t)}async powerStatus(t){return this.SystemStatusPower(t)}async SystemTime(t){return await ct(t)}async getTime(t){return this.SystemTime(t)}async SystemTimeTimestamp(t){return await ut(t)}async setTimestamp(t){return this.SystemTimeTimestamp(t)}async SystemTimeTimezone(t){return await dt(t)}async setTimezone(t){return this.SystemTimeTimezone(t)}async Account(t){return await K(t)}async getMqttStatus(t){return this.Account(t)}async AccountUnlink(t){return await V(t)}async unlinkAccount(t){return this.AccountUnlink(t)}async AccountLink(t){return await G(t)}async linkAccount(t){return this.AccountLink(t)}async AssetsUpload(t){return await _(t)}async uploadAsset(t){return this.AssetsUpload(t)}async AssetsDelete(t){return await F(t)}async deleteAssets(t){return this.AssetsDelete(t)}async DisplayDraw(t){return await q(t)}async drawDisplay(t){return this.DisplayDraw(t)}async DisplayClear(t){return await x(t)}async clearDisplay(t){return this.DisplayClear(t)}async AudioPlay(t){return await j(t)}async playSound(t){return this.AudioPlay(t)}async AudioStop(t){return await H(t)}async stopSound(t){return this.AudioStop(t)}async WifiStatus(t){return await z(t)}async statusWifi(t){return this.WifiStatus(t)}async WifiConnect(t){return await M(t)}async connectWifi(t){return this.WifiConnect(t)}async WifiDisconnect(t){return await J(t)}async disconnectWifi(t){return this.WifiDisconnect(t)}async WifiNetworks(t){return await X(t)}async networksWifi(t){return this.WifiNetworks(t)}async StorageWrite(t){return await Q(t)}async uploadFile(t){return this.StorageWrite(t)}async StorageRead(t){return await Y(t)}async downloadFile(t){return this.StorageRead(t)}async StorageList(t){return await Z(t)}async readDirectory(t){return this.StorageList(t)}async StorageRemove(t){return await tt(t)}async removeResource(t){return this.StorageRemove(t)}async StorageMkdir(t){return await et(t)}async createDirectory(t){return this.StorageMkdir(t)}async StorageStatus(t){return await nt(t)}async statusStorage(t){return this.StorageStatus(t)}async DisplayBrightness(t){return await ht(t)}async getDisplayBrightness(t){return this.DisplayBrightness(t)}async DisplayBrightnessSet(t){return await yt(t)}async setDisplayBrightness(t){return this.DisplayBrightnessSet(t)}async AudioVolume(t){return await lt(t)}async getAudioVolume(t){return this.AudioVolume(t)}async AudioVolumeSet(t){return await ft(t)}async setAudioVolume(t){return this.AudioVolumeSet(t)}async SettingsAccess(t){return await wt(t)}async getHttpAccess(t){return this.SettingsAccess(t)}async SettingsAccessSet(t){const n=await pt(t);return t.mode==="key"&&t.key&&this.setApiKey(t.key),n}async setHttpAccess(t){return this.SettingsAccessSet(t)}async SettingsName(t){return await St(t)}async getName(t){return this.SettingsName(t)}async SettingsNameSet(t){return await mt(t)}async setName(t){return this.SettingsNameSet(t)}setApiKey(t){R(t)}async BleEnable(t){return await Tt(t)}async enableBle(t){return this.BleEnable(t)}async BleDisable(t){return await Et(t)}async disableBle(t){return this.BleDisable(t)}async BleUnpair(t){return await kt(t)}async pairingBle(t){return this.BleUnpair(t)}async BleStatus(t){return await gt(t)}async statusBle(t){return this.BleStatus(t)}async InputSend(t){return await At(t)}async pressButton(t){return this.InputSend(t)}}var S=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(S||{});const D=3e3,B=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 vt(e,t){let n=0;const r=e.length,i=[];for(;n<r;){const s=L(e,n);if(n+=1,(s&128)!==0){const c=s&127;for(let h=0;h<c*t;h++)i.push(e[n+h]);n+=c*t}else{const c=s,h=e.slice(n,n+t);for(let l=0;l<c;l++)for(let d=0;d<t;d++)i.push(h[d]);n+=t}}return new Uint8Array(i)}function Dt(e){const t=new Uint8Array(e.length*2);let n=0,r=0;for(;n<e.length;){const i=L(e,n),s=i&15,c=i>>4&15;t[r]=s,t[r+1]=c,n+=1,r+=2}return t}const P=()=>typeof window<"u"&&typeof window.document<"u";class Bt{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,!P())throw new Error("not browser");if(t.apiKey&&(this.apiKey=t.apiKey),t.apiSemver&&(this.apiSemver=t.apiSemver),!t||!t.addr)this.addr=k;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const i=new URL(n).hostname;if(!g(i)&&!A(i))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(r){throw r instanceof Error&&r.message.startsWith("Invalid address")?r: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 r=new Uint8Array(n.data);let i;const s=this.config.deviceScreen===S.FRONT?3:2;try{const c=vt(r,s);this.config.deviceScreen===S.BACK?i=Dt(c):i=c,this.emitData(i)}catch{this.emitData(r)}}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===D||B.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 Lt{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(!P())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=k;else{let n=t.addr.trim();/^https?:\/\//i.test(n)||(n=`http://${n}`);try{const i=new URL(n).hostname;if(!g(i)&&!A(i))throw new Error(`Invalid address: "${t.addr}". Only IP addresses and mDNS names (ending in .local) are supported.`)}catch(r){throw r instanceof Error&&r.message.startsWith("Invalid address")?r: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 r=new Uint8Array(n.data);this.emitData(r)}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===D||B.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=bt;exports.DeviceScreen=S;exports.Input=Lt;exports.ScreenStream=Bt;
|