@busy-app/busy-lib 0.0.2 → 0.0.3
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 +54 -0
- package/dist/index.js +204 -74
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var w=(e,t,n)=>t in e?d(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var a=(e,t,n)=>w(e,typeof t!="symbol"?t+"":t,n);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 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}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 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 o=null;function S(e){o=y({baseUrl:e,bodySerializer:g})}async function E(e){const{appId:t,fileName:n,file:r}=e;if(!o)throw new Error("API client is not initialized");const{data:i,error:s}=await o.POST("/v0/assets/upload",{params:{query:{app_id:t,file:n}},headers:{"Content-Type":"application/octet-stream"},body:r});if(s)throw s;return i}async function A(e){const{appId:t}=e;if(!o)throw new Error("API client is not initialized");const{data:n,error:r}=await o.DELETE("/v0/assets/upload",{params:{query:{app_id:t}}});if(r)throw r;return n}const B={timeout:5,x:0,y:0,display:"front"};function k(e){return{...B,...e}}async function T(e){const{appId:t,elements:n}=e;if(!o)throw new Error("API client is not initialized");const r=n.map(k),{data:i,error:s}=await o.POST("/v0/display/draw",{body:{app_id:t,elements:r}});if(s)throw s;return i}async function D(){if(!o)throw new Error("API client is not initialized");const{data:e,error:t}=await o.DELETE("/v0/display/draw");if(t)throw t;return e}async function L(e){const{appId:t,path:n}=e;if(!o)throw new Error("API client is not initialized");const{data:r,error:i}=await o.POST("/v0/audio/play",{params:{query:{app_id:t,path:n}}});if(i)throw i;return r}async function v(){if(!o)throw new Error("API client is not initialized");const{data:e,error:t}=await o.DELETE("/v0/audio/play");if(t)throw t;return e}class P{constructor(t="10.0.4.20"){a(this,"ip");if(!m(t))throw new Error(`Incorrect IPv4: ${t}`);this.ip=t,console.log(this.ip),S(`http://${this.ip}/api/`)}async uploadAsset(t){return await E(t)}async deleteAssets(t){return await A(t)}async drawDisplay(t){return await T(t)}async clearDisplay(){return await D()}async playSound(t){return await L(t)}async stopSound(){return await v()}}var l=(e=>(e[e.FRONT=0]="FRONT",e[e.BACK=1]="BACK",e))(l||{});const C=3e3,I=new Set([1001,1006,1012,1013,1014,3008]);function h(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 O(e,t){let n=0;const r=e.length,i=[];for(;n<r;){const s=h(e,n);if(n+=1,(s&128)!==0){const c=s&127;for(let f=0;f<c*t;f++)i.push(e[n+f]);n+=c*t}else{const c=s,f=e.slice(n,n+t);for(let p=0;p<c;p++)for(let u=0;u<t;u++)i.push(f[u]);n+=t}}return new Uint8Array(i)}function U(e){const t=new Uint8Array(e.length*2);let n=0,r=0;for(;n<e.length;){const i=h(e,n),s=i&15,c=i>>4&15;t[r]=s,t[r+1]=c,n+=1,r+=2}return t}class N{constructor(t){a(this,"connected",!1);a(this,"dataListeners",[]);a(this,"stopListeners",[]);a(this,"errorListeners",[]);a(this,"socket",null);a(this,"deviceScreen");if(this.config=t,!(()=>typeof window<"u"&&typeof window.document<"u"))throw new Error("not browser");this.deviceScreen=l[this.config.deviceScreen.toUpperCase()]}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();let t="";this.config.mode==="cloud"?t=`${this.config.domain}/bars/${this.config.idDevice}/ws`:this.config.mode==="local"&&(t=`${this.config.barUrl}/api/v0/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.deviceScreen})):this.config.mode==="local"&&this.socket.send(JSON.stringify({display:this.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);if(this.config.mode==="cloud")this.emitData(r);else if(this.config.mode==="local"){let i;const s=this.deviceScreen===l.FRONT?3:2;try{const c=O(r,s);this.deviceScreen===l.BACK?i=U(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===C||I.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()})}}exports.BusyBar=P;exports.ScreenStream=N;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ declare interface AudioParams {
|
|
|
3
3
|
path: paths["/v0/audio/play"]["post"]["parameters"]["query"]["path"];
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
declare interface BaseConfig {
|
|
7
|
+
deviceScreen: Lowercase<keyof typeof DeviceScreen>;
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
/**
|
|
7
11
|
* Main library class for interacting with the Busy Bar API.
|
|
8
12
|
*
|
|
@@ -258,17 +262,39 @@ declare type CustomImageElement = MakeOptional<components["schemas"]["ImageEleme
|
|
|
258
262
|
|
|
259
263
|
declare type CustomTextElement = MakeOptional<components["schemas"]["TextElement"], OptionalFields>;
|
|
260
264
|
|
|
265
|
+
declare type DataListener = (data: Uint8Array) => void;
|
|
266
|
+
|
|
261
267
|
declare interface DeleteParams {
|
|
262
268
|
appId: paths["/v0/assets/upload"]["delete"]["parameters"]["query"]["app_id"];
|
|
263
269
|
}
|
|
264
270
|
|
|
271
|
+
declare type DeviceConfig = LocalConfig | SiteConfig;
|
|
272
|
+
|
|
273
|
+
declare enum DeviceScreen {
|
|
274
|
+
FRONT = 0,
|
|
275
|
+
BACK = 1
|
|
276
|
+
}
|
|
277
|
+
|
|
265
278
|
declare interface DrawParams {
|
|
266
279
|
appId: paths["/v0/display/draw"]["post"]["requestBody"]["content"]["application/json"]["app_id"];
|
|
267
280
|
elements: CustomElement[];
|
|
268
281
|
}
|
|
269
282
|
|
|
283
|
+
declare type ErrorListener = (payload: ErrorPayload) => void;
|
|
284
|
+
|
|
285
|
+
declare interface ErrorPayload {
|
|
286
|
+
code: number;
|
|
287
|
+
message: string;
|
|
288
|
+
raw: Error | CloseEvent | Event;
|
|
289
|
+
}
|
|
290
|
+
|
|
270
291
|
declare type IPv4 = string;
|
|
271
292
|
|
|
293
|
+
declare interface LocalConfig extends BaseConfig {
|
|
294
|
+
mode: "local";
|
|
295
|
+
barUrl: string;
|
|
296
|
+
}
|
|
297
|
+
|
|
272
298
|
declare type MakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
273
299
|
|
|
274
300
|
declare interface operations {
|
|
@@ -796,6 +822,34 @@ declare interface paths {
|
|
|
796
822
|
};
|
|
797
823
|
}
|
|
798
824
|
|
|
825
|
+
export declare class ScreenStream {
|
|
826
|
+
private config;
|
|
827
|
+
connected: boolean;
|
|
828
|
+
private dataListeners;
|
|
829
|
+
private stopListeners;
|
|
830
|
+
private errorListeners;
|
|
831
|
+
private socket;
|
|
832
|
+
private deviceScreen;
|
|
833
|
+
constructor(config: DeviceConfig);
|
|
834
|
+
onData(listener: DataListener): void;
|
|
835
|
+
onStop(listener: StopListener): void;
|
|
836
|
+
onError(listener: ErrorListener): void;
|
|
837
|
+
protected emitData(data: Uint8Array): void;
|
|
838
|
+
protected emitStop(): void;
|
|
839
|
+
protected emitError(payload: ErrorPayload): void;
|
|
840
|
+
openWebsocket(): Promise<void>;
|
|
841
|
+
closeWebsocket(): Promise<void>;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
declare interface SiteConfig extends BaseConfig {
|
|
845
|
+
mode: "cloud";
|
|
846
|
+
domain: string;
|
|
847
|
+
token: string;
|
|
848
|
+
idDevice: string;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
declare type StopListener = () => void;
|
|
852
|
+
|
|
799
853
|
declare interface UploadParams {
|
|
800
854
|
appId: paths["/v0/assets/upload"]["post"]["parameters"]["query"]["app_id"];
|
|
801
855
|
fileName: paths["/v0/assets/upload"]["post"]["parameters"]["query"]["file"];
|
package/dist/index.js
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import
|
|
5
|
-
function
|
|
6
|
-
const t =
|
|
1
|
+
var d = Object.defineProperty;
|
|
2
|
+
var w = (e, t, n) => t in e ? d(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
+
var a = (e, t, n) => w(e, typeof t != "symbol" ? t + "" : t, n);
|
|
4
|
+
import y from "openapi-fetch";
|
|
5
|
+
function m(e) {
|
|
6
|
+
const t = e.split(".");
|
|
7
7
|
if (t.length !== 4)
|
|
8
8
|
return !1;
|
|
9
|
-
for (const
|
|
10
|
-
if (
|
|
9
|
+
for (const n of t) {
|
|
10
|
+
if (n.length === 0 || n.length > 1 && n[0] === "0" || !/^\d+$/.test(n))
|
|
11
11
|
return !1;
|
|
12
|
-
const
|
|
13
|
-
if (
|
|
12
|
+
const r = Number(n);
|
|
13
|
+
if (r < 0 || r > 255)
|
|
14
14
|
return !1;
|
|
15
15
|
}
|
|
16
16
|
return !0;
|
|
17
17
|
}
|
|
18
|
-
const
|
|
19
|
-
if (typeof FormData < "u" &&
|
|
20
|
-
return
|
|
21
|
-
let
|
|
22
|
-
return t && (t instanceof Headers ?
|
|
18
|
+
const g = (e, t) => {
|
|
19
|
+
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))
|
|
20
|
+
return e;
|
|
21
|
+
let n;
|
|
22
|
+
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);
|
|
23
23
|
};
|
|
24
|
-
let
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
baseUrl:
|
|
28
|
-
bodySerializer:
|
|
24
|
+
let o = null;
|
|
25
|
+
function E(e) {
|
|
26
|
+
o = y({
|
|
27
|
+
baseUrl: e,
|
|
28
|
+
bodySerializer: g
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
-
async function
|
|
32
|
-
const { appId: t, fileName:
|
|
33
|
-
if (!
|
|
31
|
+
async function S(e) {
|
|
32
|
+
const { appId: t, fileName: n, file: r } = e;
|
|
33
|
+
if (!o)
|
|
34
34
|
throw new Error("API client is not initialized");
|
|
35
|
-
const { data:
|
|
35
|
+
const { data: i, error: s } = await o.POST("/v0/assets/upload", {
|
|
36
36
|
params: {
|
|
37
37
|
query: {
|
|
38
38
|
app_id: t,
|
|
39
|
-
file:
|
|
39
|
+
file: n
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
headers: {
|
|
43
43
|
"Content-Type": "application/octet-stream"
|
|
44
44
|
},
|
|
45
|
-
body:
|
|
45
|
+
body: r
|
|
46
46
|
});
|
|
47
|
-
if (
|
|
48
|
-
throw
|
|
49
|
-
return
|
|
47
|
+
if (s)
|
|
48
|
+
throw s;
|
|
49
|
+
return i;
|
|
50
50
|
}
|
|
51
|
-
async function
|
|
52
|
-
const { appId: t } =
|
|
53
|
-
if (!
|
|
51
|
+
async function A(e) {
|
|
52
|
+
const { appId: t } = e;
|
|
53
|
+
if (!o)
|
|
54
54
|
throw new Error("API client is not initialized");
|
|
55
|
-
const { data:
|
|
55
|
+
const { data: n, error: r } = await o.DELETE("/v0/assets/upload", {
|
|
56
56
|
params: {
|
|
57
57
|
query: {
|
|
58
58
|
app_id: t
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
|
-
if (
|
|
63
|
-
throw
|
|
64
|
-
return
|
|
62
|
+
if (r)
|
|
63
|
+
throw r;
|
|
64
|
+
return n;
|
|
65
65
|
}
|
|
66
|
-
const
|
|
67
|
-
function
|
|
68
|
-
return { ...
|
|
66
|
+
const B = { timeout: 5, x: 0, y: 0, display: "front" };
|
|
67
|
+
function k(e) {
|
|
68
|
+
return { ...B, ...e };
|
|
69
69
|
}
|
|
70
|
-
async function
|
|
71
|
-
const { appId: t, elements:
|
|
72
|
-
if (!
|
|
70
|
+
async function D(e) {
|
|
71
|
+
const { appId: t, elements: n } = e;
|
|
72
|
+
if (!o)
|
|
73
73
|
throw new Error("API client is not initialized");
|
|
74
|
-
const
|
|
74
|
+
const r = n.map(k), { data: i, error: s } = await o.POST("/v0/display/draw", {
|
|
75
75
|
body: {
|
|
76
76
|
app_id: t,
|
|
77
|
-
elements:
|
|
77
|
+
elements: r
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
|
-
if (
|
|
81
|
-
throw
|
|
82
|
-
return
|
|
80
|
+
if (s)
|
|
81
|
+
throw s;
|
|
82
|
+
return i;
|
|
83
83
|
}
|
|
84
|
-
async function
|
|
85
|
-
if (!
|
|
84
|
+
async function L() {
|
|
85
|
+
if (!o)
|
|
86
86
|
throw new Error("API client is not initialized");
|
|
87
|
-
const { data:
|
|
87
|
+
const { data: e, error: t } = await o.DELETE("/v0/display/draw");
|
|
88
88
|
if (t)
|
|
89
89
|
throw t;
|
|
90
|
-
return
|
|
90
|
+
return e;
|
|
91
91
|
}
|
|
92
|
-
async function
|
|
93
|
-
const { appId: t, path:
|
|
94
|
-
if (!
|
|
92
|
+
async function T(e) {
|
|
93
|
+
const { appId: t, path: n } = e;
|
|
94
|
+
if (!o)
|
|
95
95
|
throw new Error("API client is not initialized");
|
|
96
|
-
const { data:
|
|
96
|
+
const { data: r, error: i } = await o.POST("/v0/audio/play", {
|
|
97
97
|
params: {
|
|
98
98
|
query: {
|
|
99
99
|
app_id: t,
|
|
100
|
-
path:
|
|
100
|
+
path: n
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
});
|
|
104
|
-
if (
|
|
105
|
-
throw
|
|
106
|
-
return
|
|
104
|
+
if (i)
|
|
105
|
+
throw i;
|
|
106
|
+
return r;
|
|
107
107
|
}
|
|
108
|
-
async function
|
|
109
|
-
if (!
|
|
108
|
+
async function v() {
|
|
109
|
+
if (!o)
|
|
110
110
|
throw new Error("API client is not initialized");
|
|
111
|
-
const { data:
|
|
111
|
+
const { data: e, error: t } = await o.DELETE("/v0/audio/play");
|
|
112
112
|
if (t)
|
|
113
113
|
throw t;
|
|
114
|
-
return
|
|
114
|
+
return e;
|
|
115
115
|
}
|
|
116
|
-
class
|
|
116
|
+
class $ {
|
|
117
117
|
/**
|
|
118
118
|
* Creates an instance of BUSY Bar.
|
|
119
119
|
* Initializes the API client with the provided IPv4 address.
|
|
@@ -127,10 +127,10 @@ class T {
|
|
|
127
127
|
* @type {IPv4}
|
|
128
128
|
* @readonly
|
|
129
129
|
*/
|
|
130
|
-
|
|
131
|
-
if (!
|
|
130
|
+
a(this, "ip");
|
|
131
|
+
if (!m(t))
|
|
132
132
|
throw new Error(`Incorrect IPv4: ${t}`);
|
|
133
|
-
this.ip = t, console.log(this.ip),
|
|
133
|
+
this.ip = t, console.log(this.ip), E(`http://${this.ip}/api/`);
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
136
|
* Uploads an asset to the device.
|
|
@@ -142,7 +142,7 @@ class T {
|
|
|
142
142
|
* @returns {Promise<{ result: string }>} Result of the upload operation.
|
|
143
143
|
*/
|
|
144
144
|
async uploadAsset(t) {
|
|
145
|
-
return await
|
|
145
|
+
return await S(t);
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Deletes all assets for a specific application from the device.
|
|
@@ -152,7 +152,7 @@ class T {
|
|
|
152
152
|
* @returns {Promise<{ result: string }>} Result of the delete operation.
|
|
153
153
|
*/
|
|
154
154
|
async deleteAssets(t) {
|
|
155
|
-
return await
|
|
155
|
+
return await A(t);
|
|
156
156
|
}
|
|
157
157
|
/**
|
|
158
158
|
* Draws elements on the device display.
|
|
@@ -163,7 +163,7 @@ class T {
|
|
|
163
163
|
* @returns {Promise<{ result: string }>} Result of the draw operation.
|
|
164
164
|
*/
|
|
165
165
|
async drawDisplay(t) {
|
|
166
|
-
return await
|
|
166
|
+
return await D(t);
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* Clears the device display and stops the Canvas application if running.
|
|
@@ -171,7 +171,7 @@ class T {
|
|
|
171
171
|
* @returns {Promise<{ result: string }>} Result of the clear operation.
|
|
172
172
|
*/
|
|
173
173
|
async clearDisplay() {
|
|
174
|
-
return await
|
|
174
|
+
return await L();
|
|
175
175
|
}
|
|
176
176
|
/**
|
|
177
177
|
* Plays an audio file from the assets directory.
|
|
@@ -182,7 +182,7 @@ class T {
|
|
|
182
182
|
* @returns {Promise<{ result: string }>} Result of the play operation.
|
|
183
183
|
*/
|
|
184
184
|
async playSound(t) {
|
|
185
|
-
return await
|
|
185
|
+
return await T(t);
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
188
188
|
* Stops any currently playing audio on the device.
|
|
@@ -190,9 +190,139 @@ class T {
|
|
|
190
190
|
* @returns {Promise<{ result: string }>} Result of the stop operation.
|
|
191
191
|
*/
|
|
192
192
|
async stopSound() {
|
|
193
|
-
return await
|
|
193
|
+
return await v();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
var l = /* @__PURE__ */ ((e) => (e[e.FRONT = 0] = "FRONT", e[e.BACK = 1] = "BACK", e))(l || {});
|
|
197
|
+
const C = 3e3, I = /* @__PURE__ */ new Set([1001, 1006, 1012, 1013, 1014, 3008]);
|
|
198
|
+
function h(e, t) {
|
|
199
|
+
if (t < 0 || t >= e.length)
|
|
200
|
+
throw new Error(`Index ${t} is out of bounds (0…${e.length - 1})`);
|
|
201
|
+
const n = e[t];
|
|
202
|
+
if (n === void 0)
|
|
203
|
+
throw new Error(`Unexpected undefined at index ${t}`);
|
|
204
|
+
return n;
|
|
205
|
+
}
|
|
206
|
+
function P(e, t) {
|
|
207
|
+
let n = 0;
|
|
208
|
+
const r = e.length, i = [];
|
|
209
|
+
for (; n < r; ) {
|
|
210
|
+
const s = h(e, n);
|
|
211
|
+
if (n += 1, (s & 128) !== 0) {
|
|
212
|
+
const c = s & 127;
|
|
213
|
+
for (let f = 0; f < c * t; f++)
|
|
214
|
+
i.push(e[n + f]);
|
|
215
|
+
n += c * t;
|
|
216
|
+
} else {
|
|
217
|
+
const c = s, f = e.slice(n, n + t);
|
|
218
|
+
for (let p = 0; p < c; p++)
|
|
219
|
+
for (let u = 0; u < t; u++)
|
|
220
|
+
i.push(f[u]);
|
|
221
|
+
n += t;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return new Uint8Array(i);
|
|
225
|
+
}
|
|
226
|
+
function U(e) {
|
|
227
|
+
const t = new Uint8Array(e.length * 2);
|
|
228
|
+
let n = 0, r = 0;
|
|
229
|
+
for (; n < e.length; ) {
|
|
230
|
+
const i = h(e, n), s = i & 15, c = i >> 4 & 15;
|
|
231
|
+
t[r] = s, t[r + 1] = c, n += 1, r += 2;
|
|
232
|
+
}
|
|
233
|
+
return t;
|
|
234
|
+
}
|
|
235
|
+
class F {
|
|
236
|
+
constructor(t) {
|
|
237
|
+
a(this, "connected", !1);
|
|
238
|
+
a(this, "dataListeners", []);
|
|
239
|
+
a(this, "stopListeners", []);
|
|
240
|
+
a(this, "errorListeners", []);
|
|
241
|
+
a(this, "socket", null);
|
|
242
|
+
a(this, "deviceScreen");
|
|
243
|
+
if (this.config = t, !(() => typeof window < "u" && typeof window.document < "u"))
|
|
244
|
+
throw new Error("not browser");
|
|
245
|
+
this.deviceScreen = l[this.config.deviceScreen.toUpperCase()];
|
|
246
|
+
}
|
|
247
|
+
onData(t) {
|
|
248
|
+
this.dataListeners.push(t);
|
|
249
|
+
}
|
|
250
|
+
onStop(t) {
|
|
251
|
+
this.stopListeners.push(t);
|
|
252
|
+
}
|
|
253
|
+
onError(t) {
|
|
254
|
+
this.errorListeners.push(t);
|
|
255
|
+
}
|
|
256
|
+
emitData(t) {
|
|
257
|
+
for (const n of this.dataListeners)
|
|
258
|
+
n(t);
|
|
259
|
+
}
|
|
260
|
+
emitStop() {
|
|
261
|
+
for (const t of this.stopListeners)
|
|
262
|
+
t();
|
|
263
|
+
}
|
|
264
|
+
emitError(t) {
|
|
265
|
+
for (const n of this.errorListeners)
|
|
266
|
+
n(t);
|
|
267
|
+
}
|
|
268
|
+
async openWebsocket() {
|
|
269
|
+
this.socket && await this.closeWebsocket();
|
|
270
|
+
let t = "";
|
|
271
|
+
this.config.mode === "cloud" ? t = `${this.config.domain}/bars/${this.config.idDevice}/ws` : this.config.mode === "local" && (t = `${this.config.barUrl}/api/v0/screen/ws`), this.socket = new WebSocket(t), this.socket.onopen = () => {
|
|
272
|
+
this.socket && (this.config.mode === "cloud" ? this.socket.send(
|
|
273
|
+
JSON.stringify({
|
|
274
|
+
token: this.config.token,
|
|
275
|
+
display: this.deviceScreen
|
|
276
|
+
})
|
|
277
|
+
) : this.config.mode === "local" && this.socket.send(JSON.stringify({ display: this.deviceScreen })), this.connected = !0);
|
|
278
|
+
}, this.socket.binaryType = "arraybuffer", this.socket.onmessage = (n) => {
|
|
279
|
+
try {
|
|
280
|
+
if (typeof n.data == "string")
|
|
281
|
+
return;
|
|
282
|
+
const r = new Uint8Array(n.data);
|
|
283
|
+
if (this.config.mode === "cloud")
|
|
284
|
+
this.emitData(r);
|
|
285
|
+
else if (this.config.mode === "local") {
|
|
286
|
+
let i;
|
|
287
|
+
const s = this.deviceScreen === l.FRONT ? 3 : 2;
|
|
288
|
+
try {
|
|
289
|
+
const c = P(r, s);
|
|
290
|
+
this.deviceScreen === l.BACK ? i = U(c) : i = c, this.emitData(i);
|
|
291
|
+
} catch {
|
|
292
|
+
this.emitData(r);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
} catch {
|
|
296
|
+
this.connected = !1, this.emitStop();
|
|
297
|
+
}
|
|
298
|
+
}, this.socket.onerror = (n) => {
|
|
299
|
+
this.connected = !1, this.emitError({
|
|
300
|
+
code: 1006,
|
|
301
|
+
// Standard «abnormal closure» code per RFC-6455
|
|
302
|
+
message: "WebSocket error occurred",
|
|
303
|
+
raw: n
|
|
304
|
+
}), this.emitStop();
|
|
305
|
+
}, this.socket.onclose = async (n) => {
|
|
306
|
+
if (this.socket = null, this.connected = !1, n.code === C || I.has(n.code)) {
|
|
307
|
+
this.emitError({
|
|
308
|
+
code: n.code,
|
|
309
|
+
message: n.reason,
|
|
310
|
+
raw: n
|
|
311
|
+
});
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
this.emitStop();
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
closeWebsocket() {
|
|
318
|
+
return this.connected = !1, new Promise((t) => {
|
|
319
|
+
this.socket ? (this.socket.onclose = () => {
|
|
320
|
+
t();
|
|
321
|
+
}, this.socket.close(), this.socket = null) : t(), this.emitStop();
|
|
322
|
+
});
|
|
194
323
|
}
|
|
195
324
|
}
|
|
196
325
|
export {
|
|
197
|
-
|
|
326
|
+
$ as BusyBar,
|
|
327
|
+
F as ScreenStream
|
|
198
328
|
};
|