@busy-app/busy-lib 0.0.6 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var h=Object.defineProperty;var p=(e,t,i)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var c=(e,t,i)=>p(e,typeof t!="symbol"?t+"":t,i);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("openapi-fetch");function m(e){const t=e.split(".");if(t.length!==4)return!1;for(const i of t){if(i.length===0||i.length>1&&i[0]==="0"||!/^\d+$/.test(i))return!1;const n=Number(i);if(n<0||n>255)return!1}return!0}const g=(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 r=null;function E(e){r=y({baseUrl:e,bodySerializer:g})}async function A(e){const{appId:t,fileName:i,file:n}=e;if(!r)throw new Error("API client is not initialized");const{data:o,error:s}=await r.POST("/v0/assets/upload",{params:{query:{app_id:t,file:i}},headers:{"Content-Type":"application/octet-stream"},body:n});if(s)throw s;return o}async function S(e){const{appId:t}=e;if(!r)throw new Error("API client is not initialized");const{data:i,error:n}=await r.DELETE("/v0/assets/upload",{params:{query:{app_id:t}}});if(n)throw n;return i}const P={timeout:5,x:0,y:0,display:"front"};function T(e){return{...P,...e}}async function k(e){const{appId:t,elements:i}=e;if(!r)throw new Error("API client is not initialized");const n=i.map(T),{data:o,error:s}=await r.POST("/v0/display/draw",{body:{app_id:t,elements:n}});if(s)throw s;return o}async function v(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/v0/display/draw");if(t)throw t;return e}async function B(e){const{appId:t,path:i}=e;if(!r)throw new Error("API client is not initialized");const{data:n,error:o}=await r.POST("/v0/audio/play",{params:{query:{app_id:t,path:i}}});if(o)throw o;return n}async function I(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.DELETE("/v0/audio/play");if(t)throw t;return e}async function D(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/v0/wifi/enable");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("/v0/wifi/disable");if(t)throw t;return e}async function C(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/v0/wifi/status");if(t)throw t;return e}async function L(e){if(!r)throw new Error("API client is not initialized");const{data:t,error:i}=await r.POST("/v0/wifi/connect",{body:{ssid:e.ssid,password:e.password,security:e.security,ip_config:{ip_method:e.ipConfig.ipMethod,ip_type:e.ipConfig.ipType,address:e.ipConfig.address,mask:e.ipConfig.mask,gateway:e.ipConfig.gateway}}});if(i)throw i;return t}async function z(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/v0/wifi/disconnect");if(t)throw t;return e}async function b(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.GET("/v0/wifi/networks");if(t)throw t;return e}async function U(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/v0/wifi/forget");if(t)throw t;return e}async function W(e){if(!r)throw new Error("API client is not initialized");const{path:t,file:i}=e,{data:n,error:o}=await r.POST("/v0/storage/write",{params:{query:{path:t}},headers:{"Content-Type":"application/octet-stream"},body:i});if(o)throw o;return n}async function F(e){if(!r)throw new Error("API client is not initialized");const{path:t,asArrayBuffer:i}=e,{data:n,error:o}=await r.GET("/v0/storage/read",{params:{query:{path:t}},parseAs:i?"arrayBuffer":"blob"});if(o)throw o;return n}async function N(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.GET("/v0/storage/list",{params:{query:{path:t}}});if(n)throw n;return i}async function _(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.DELETE("/v0/storage/remove",{params:{query:{path:t}}});if(n)throw n;return i}async function q(e){if(!r)throw new Error("API client is not initialized");const{path:t}=e,{data:i,error:n}=await r.POST("/v0/storage/mkdir",{params:{query:{path:t}}});if(n)throw n;return i}class ${constructor(t="10.0.4.20"){c(this,"ip");if(!m(t))throw new Error(`Incorrect IPv4: ${t}`);this.ip=t,console.log(this.ip),E(`http://${this.ip}/api/`)}async uploadAsset(t){return await A(t)}async deleteAssets(t){return await S(t)}async drawDisplay(t){return await k(t)}async clearDisplay(){return await v()}async playSound(t){return await B(t)}async stopSound(){return await I()}async enableWifi(){return await D()}async disableWifi(){return await O()}async statusWifi(){return await C()}async connectWifi(t){return await L(t)}async disconnectWifi(){return await z()}async networksWifi(){return await b()}async forgetWifi(){return await U()}async uploadFile(t){return await W(t)}async downloadFile(t){return await F(t)}async readDirectory(t){return await N(t)}async removeResource(t){return await _(t)}async createDirectory(t){return await q(t)}}var u=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(u||{});const R=3e3,j=new Set([1001,1006,1012,1013,1014,3008]);function d(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 x(e,t){let i=0;const n=e.length,o=[];for(;i<n;){const s=d(e,i);if(i+=1,(s&128)!==0){const a=s&127;for(let f=0;f<a*t;f++)o.push(e[i+f]);i+=a*t}else{const a=s,f=e.slice(i,i+t);for(let w=0;w<a;w++)for(let l=0;l<t;l++)o.push(f[l]);i+=t}}return new Uint8Array(o)}function G(e){const t=new Uint8Array(e.length*2);let i=0,n=0;for(;i<e.length;){const o=d(e,i),s=o&15,a=o>>4&15;t[n]=s,t[n+1]=a,i+=1,n+=2}return t}class J{constructor(t){c(this,"connected",!1);c(this,"dataListeners",[]);c(this,"stopListeners",[]);c(this,"errorListeners",[]);c(this,"socket",null);if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser")}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="";this.config.mode==="cloud"?t=`${this.config.domain}/bars/${this.config.idDevice}/ws`:this.config.mode==="local"&&(t=`${this.config.barUrl}/api/screen/ws`),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 o;const s=this.config.deviceScreen===u.FRONT?3:2;try{const a=x(n,s);this.config.deviceScreen===u.BACK?o=G(a):o=a,this.emitData(o)}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===R||j.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()})}}exports.BusyBar=$;exports.DeviceScreen=u;exports.ScreenStream=J;
1
+ "use strict";var A=Object.defineProperty;var P=(e,t,i)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i;var u=(e,t,i)=>P(e,typeof t!="symbol"?t+"":t,i);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("openapi-fetch");function T(e){const t=e.split(".");if(t.length!==4)return!1;for(const i of t){if(i.length===0||i.length>1&&i[0]==="0"||!/^\d+$/.test(i))return!1;const n=Number(i);if(n<0||n>255)return!1}return!0}const I=(e,t)=>{if(typeof FormData<"u"&&e instanceof FormData||typeof Buffer<"u"&&typeof Buffer.isBuffer=="function"&&Buffer.isBuffer(e)||typeof File<"u"&&e instanceof File||typeof Blob<"u"&&e instanceof Blob||typeof ArrayBuffer<"u"&&e instanceof ArrayBuffer||typeof ArrayBuffer<"u"&&ArrayBuffer.isView&&ArrayBuffer.isView(e))return e;let 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 p,w,l=null;async function g(){if(!w){if(!p)throw new Error("getApiVersionFn is not set");l||(l=(async()=>{const e=await p();if(!e.api_semver)throw new Error("Empty API version");w=e.api_semver})().finally(()=>{l=null})),await l}}async function y(e){const n=(e.headers.get("content-type")||"").includes("application/json")?await e.clone().json():await e.clone().text(),o=typeof n=="object"&&n!==null?n.error||n.message:typeof n=="string"?n:void 0;return Object.assign(new Error(o||`HTTP ${e.status} ${e.statusText}`),{status:e.status,statusText:e.statusText,body:n})}let m;function b(e){m=e}const k={async onRequest({request:e,schemaPath:t}){if(t!=="/version"){if(await g(),w)return e.headers.set("X-API-Sem-Ver",w);m&&e.headers.set("X-API-Token",m)}return 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 g(),w&&e.headers.set("X-API-Sem-Ver",w);const o=await(i.fetch??fetch)(e);if(o.ok)return o;throw await y(o)}};let r=null;function B(e,t){p=t,r=S({baseUrl:e,bodySerializer:I}),r.use(k)}async function z(e){const{appId:t,fileName:i,file:n}=e;if(!r)throw new Error("API client is not initialized");const{data:o,error:s}=await r.POST("/assets/upload",{params:{query:{app_id:t,file:i}},headers:{"Content-Type":"application/octet-stream"},body:n});if(s)throw s;return o}async function v(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}const O={timeout:5,x:0,y:0,display:"front"};function D(e){return{...O,...e}}async function C(e){const{appId:t,elements:i}=e;if(!r)throw new Error("API client is not initialized");const n=i.map(D),{data:o,error:s}=await r.POST("/display/draw",{body:{app_id:t,elements:n}});if(s)throw s;return o}async function L(){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 F(e){const{appId:t,path:i}=e;if(!r)throw new Error("API client is not initialized");const{data:n,error:o}=await r.POST("/audio/play",{params:{query:{app_id:t,path:i}}});if(o)throw o;return n}async function V(){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.POST("/wifi/enable");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.POST("/wifi/disable");if(t)throw t;return e}async function U(){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 W(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,ip_type:e.ipConfig.ipType,address:e.ipConfig.address,mask:e.ipConfig.mask,gateway:e.ipConfig.gateway}}});if(i)throw i;return t}async function q(){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 G(){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(){if(!r)throw new Error("API client is not initialized");const{data:e,error:t}=await r.POST("/wifi/forget");if(t)throw t;return e}async function j(e){if(!r)throw new Error("API client is not initialized");const{path:t,file:i}=e,{data:n,error:o}=await r.POST("/storage/write",{params:{query:{path:t}},headers:{"Content-Type":"application/octet-stream"},body:i});if(o)throw o;return n}async function R(e){if(!r)throw new Error("API client is not initialized");const{path:t,asArrayBuffer:i}=e,{data:n,error:o}=await r.GET("/storage/read",{params:{query:{path:t}},parseAs:i?"arrayBuffer":"blob"});if(o)throw o;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 K(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("/version");if(t)throw t;return e}async function X(e){if(!r)throw new Error("API client is not initialized");const{name:t,file:i}=e,{data:n,error:o}=await r.POST("/update",{params:{query:{name:t}},headers:{"Content-Type":"application/octet-stream"},body:i});if(o)throw o;return n}async function M(){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 Q(){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 Y(){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 Z(){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 tt(e){if(!r)throw new Error("API client is not initialized");const{front:t,back:i}=e,n=f=>{if(typeof f=="number"){if(f<0||f>100)throw new Error("Brightness value must be between 0 and 100 or 'auto'");return String(f)}if(f==="auto")return"auto"},o=n(t),s=n(i),{data:a,error:c}=await r.POST("/display/brightness",{params:{query:{front:o,back:s}}});if(c)throw c;return a}async function et(){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 it(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 rt(){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 nt(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:o}=await r.POST("/access",{params:{query:{mode:t,key:i}}});if(o)throw o;return n}async function ot(){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 st(){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}class at{constructor(t="10.0.4.20"){u(this,"ip");u(this,"apiSemver");if(!T(t))throw new Error(`Incorrect IPv4: ${t}`);this.ip=t,this.apiSemver="",B(`http://${this.ip}/api/`,this.getApiVersion.bind(this))}async getApiVersion(){const t=await J();return this.apiSemver=t.api_semver,t}async updateFirmware(t){return await X(t)}async deviceStatus(){return await M()}async systemStatus(){return await Q()}async powerStatus(){return await Y()}async uploadAsset(t){return await z(t)}async deleteAssets(t){return await v(t)}async drawDisplay(t){return await C(t)}async clearDisplay(){return await L()}async playSound(t){return await F(t)}async stopSound(){return await V()}async enableWifi(){return await $()}async disableWifi(){return await _()}async statusWifi(){return await U()}async connectWifi(t){return await W(t)}async disconnectWifi(){return await q()}async networksWifi(){return await G()}async forgetWifi(){return await N()}async uploadFile(t){return await j(t)}async downloadFile(t){return await R(t)}async readDirectory(t){return await x(t)}async removeResource(t){return await K(t)}async createDirectory(t){return await H(t)}async getDisplayBrightness(){return await Z()}async setDisplayBrightness(t){return await tt(t)}async getAudioVolume(){return await et()}async setAudioVolume(t){return await it(t)}async getHttpAccess(){return await rt()}async setHttpAccess(t){const i=await nt(t);return t.mode==="key"&&t.key&&this.setApiKey(t.key),i}setApiKey(t){b(t)}async enableBle(){return await ot()}async disableBle(){return await st()}}var d=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(d||{});const ct=3e3,ft=new Set([1001,1006,1012,1013,1014,3008]);function E(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 ut(e,t){let i=0;const n=e.length,o=[];for(;i<n;){const s=E(e,i);if(i+=1,(s&128)!==0){const a=s&127;for(let c=0;c<a*t;c++)o.push(e[i+c]);i+=a*t}else{const a=s,c=e.slice(i,i+t);for(let f=0;f<a;f++)for(let h=0;h<t;h++)o.push(c[h]);i+=t}}return new Uint8Array(o)}function wt(e){const t=new Uint8Array(e.length*2);let i=0,n=0;for(;i<e.length;){const o=E(e,i),s=o&15,a=o>>4&15;t[n]=s,t[n+1]=a,i+=1,n+=2}return t}class lt{constructor(t){u(this,"connected",!1);u(this,"dataListeners",[]);u(this,"stopListeners",[]);u(this,"errorListeners",[]);u(this,"socket",null);if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser")}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="";this.config.mode==="cloud"?t=`${this.config.domain}/bars/${this.config.idDevice}/ws`:this.config.mode==="local"&&(t=`${this.config.barUrl}/api/screen/ws`),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 o;const s=this.config.deviceScreen===d.FRONT?3:2;try{const a=ut(n,s);this.config.deviceScreen===d.BACK?o=wt(a):o=a,this.emitData(o)}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===ct||ft.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()})}}exports.BusyBar=at;exports.DeviceScreen=d;exports.ScreenStream=lt;