@7365admin1/layer-common 3.2.0 → 3.2.1-staging.59
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/.changeset/camera-wall-gated-endpoint.md +47 -0
- package/components/CameraWall.vue +625 -0
- package/components/CameraWallTile.vue +333 -0
- package/components/EntryPassInformation.vue +4 -4
- package/components/HidAccessLogDashboard.vue +53 -8
- package/components/HidIntercomManagement.vue +842 -201
- package/components/HidQrCodeConfiguration.vue +907 -95
- package/components/HidServiceSettingsPanel.vue +20 -1
- package/components/HidUserEnrollment.vue +96 -2
- package/components/Nfc/NFCPatrolRouteMain.vue +52 -7
- package/components/OnlineFormConfigurationForm.vue +620 -224
- package/components/OnlineFormFill.vue +36 -8
- package/components/OnlineFormsConfiguration.vue +6 -2
- package/components/QrTemplate/CreditCardLandscape.vue +19 -12
- package/components/QrTemplate/PrintDialog.vue +209 -196
- package/components/SiteSettings.vue +4 -2
- package/components/VisitorForm.vue +201 -78
- package/components/VisitorManagement.vue +0 -1
- package/composables/useHidAmico.ts +127 -0
- package/composables/useHidNavigation.ts +0 -7
- package/composables/useOnlineFormPages.ts +2 -0
- package/composables/useSipWebPhone.ts +332 -0
- package/composables/useSiteSettings.ts +28 -0
- package/composables/useWebUsb.ts +127 -37
- package/package.json +2 -1
- package/pages/[org]/[site]/access-mgmt/intercom/index.vue +1 -1
- package/plugins/secure-member.client.ts +21 -56
- package/types/site.d.ts +71 -0
- package/utils/camera-wall.test.ts +149 -0
- package/utils/camera-wall.ts +299 -0
- package/components/HidIdentityMapping.vue +0 -975
- package/middleware/member.ts +0 -4
- package/pages/[org]/[site]/access-mgmt/identity-mapping/index.vue +0 -23
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { computed, onScopeDispose, readonly, ref, shallowRef } from "vue";
|
|
2
|
+
import type { SimpleUser, SimpleUserDelegate, SimpleUserOptions } from "sip.js/lib/platform/web";
|
|
3
|
+
|
|
4
|
+
export type SipWebPhoneConfig = {
|
|
5
|
+
webSocketServer: string;
|
|
6
|
+
aor: string;
|
|
7
|
+
authorizationUsername: string;
|
|
8
|
+
authorizationPassword: string;
|
|
9
|
+
displayName?: string;
|
|
10
|
+
video?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type SipWebPhoneMedia = {
|
|
14
|
+
localVideo?: HTMLVideoElement | null;
|
|
15
|
+
remoteAudio?: HTMLAudioElement | null;
|
|
16
|
+
remoteVideo?: HTMLVideoElement | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type ConnectionState = "disconnected" | "connecting" | "registered" | "error";
|
|
20
|
+
type CallState = "idle" | "calling" | "incoming" | "active" | "ending";
|
|
21
|
+
|
|
22
|
+
export default function useSipWebPhone() {
|
|
23
|
+
const simpleUser = shallowRef<SimpleUser>();
|
|
24
|
+
const connectionState = ref<ConnectionState>("disconnected");
|
|
25
|
+
const callState = ref<CallState>("idle");
|
|
26
|
+
const errorMessage = ref("");
|
|
27
|
+
const muted = ref(false);
|
|
28
|
+
const cameraEnabled = ref(false);
|
|
29
|
+
const localVideoAvailable = ref(false);
|
|
30
|
+
const remoteVideoAvailable = ref(false);
|
|
31
|
+
const currentTarget = ref("");
|
|
32
|
+
const activeConfig = shallowRef<SipWebPhoneConfig>();
|
|
33
|
+
|
|
34
|
+
const isRegistered = computed(() => connectionState.value === "registered");
|
|
35
|
+
const hasCall = computed(() => callState.value !== "idle");
|
|
36
|
+
|
|
37
|
+
async function connect(config: SipWebPhoneConfig, media: SipWebPhoneMedia) {
|
|
38
|
+
validateConfig(config);
|
|
39
|
+
await disconnect();
|
|
40
|
+
|
|
41
|
+
connectionState.value = "connecting";
|
|
42
|
+
errorMessage.value = "";
|
|
43
|
+
activeConfig.value = { ...config };
|
|
44
|
+
let clearRegistrationTimer = () => {};
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const { SimpleUser: SimpleUserConstructor } = await import("sip.js/lib/platform/web");
|
|
48
|
+
let registrationSettled = false;
|
|
49
|
+
let resolveRegistration: (() => void) | undefined;
|
|
50
|
+
let rejectRegistration: ((error: Error) => void) | undefined;
|
|
51
|
+
const registrationPromise = new Promise<void>((resolve, reject) => {
|
|
52
|
+
resolveRegistration = resolve;
|
|
53
|
+
rejectRegistration = reject;
|
|
54
|
+
});
|
|
55
|
+
const registrationTimeout = globalThis.setTimeout(() => {
|
|
56
|
+
if (registrationSettled) return;
|
|
57
|
+
registrationSettled = true;
|
|
58
|
+
rejectRegistration?.(new Error("SIP registration timed out. Check the WebSocket server and account credentials."));
|
|
59
|
+
}, 15000);
|
|
60
|
+
clearRegistrationTimer = () => globalThis.clearTimeout(registrationTimeout);
|
|
61
|
+
const settleRegistration = (error?: Error) => {
|
|
62
|
+
if (registrationSettled) return;
|
|
63
|
+
registrationSettled = true;
|
|
64
|
+
clearRegistrationTimer();
|
|
65
|
+
if (error) rejectRegistration?.(error);
|
|
66
|
+
else resolveRegistration?.();
|
|
67
|
+
};
|
|
68
|
+
const delegate: SimpleUserDelegate = {
|
|
69
|
+
onCallCreated: () => {
|
|
70
|
+
if (callState.value !== "incoming") callState.value = "calling";
|
|
71
|
+
},
|
|
72
|
+
onCallReceived: () => {
|
|
73
|
+
callState.value = "incoming";
|
|
74
|
+
currentTarget.value = "Incoming call";
|
|
75
|
+
},
|
|
76
|
+
onCallAnswered: () => {
|
|
77
|
+
callState.value = "active";
|
|
78
|
+
syncVideoState();
|
|
79
|
+
},
|
|
80
|
+
onCallHangup: () => {
|
|
81
|
+
const previousState = callState.value;
|
|
82
|
+
resetCall();
|
|
83
|
+
if (previousState === "calling") {
|
|
84
|
+
errorMessage.value = "The call was not answered or was rejected by the destination.";
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
onRegistered: () => {
|
|
88
|
+
connectionState.value = "registered";
|
|
89
|
+
settleRegistration();
|
|
90
|
+
},
|
|
91
|
+
onUnregistered: () => {
|
|
92
|
+
if (connectionState.value === "connecting") {
|
|
93
|
+
settleRegistration(new Error("SIP registration was rejected. Check the username and password."));
|
|
94
|
+
}
|
|
95
|
+
connectionState.value = "disconnected";
|
|
96
|
+
},
|
|
97
|
+
onServerDisconnect: () => {
|
|
98
|
+
const error = new Error("The SIP WebSocket server disconnected during registration.");
|
|
99
|
+
connectionState.value = "error";
|
|
100
|
+
errorMessage.value = error.message;
|
|
101
|
+
settleRegistration(error);
|
|
102
|
+
resetCall();
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
const options: SimpleUserOptions = {
|
|
106
|
+
aor: normalizeSipUri(config.aor),
|
|
107
|
+
delegate,
|
|
108
|
+
media: {
|
|
109
|
+
constraints: { audio: true, video: Boolean(config.video) },
|
|
110
|
+
local: { video: media.localVideo || undefined },
|
|
111
|
+
remote: {
|
|
112
|
+
audio: media.remoteAudio || undefined,
|
|
113
|
+
video: media.remoteVideo || undefined,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
userAgentOptions: {
|
|
117
|
+
authorizationUsername: config.authorizationUsername,
|
|
118
|
+
authorizationPassword: config.authorizationPassword,
|
|
119
|
+
displayName: config.displayName || config.authorizationUsername,
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const user = new SimpleUserConstructor(config.webSocketServer.trim(), options);
|
|
124
|
+
simpleUser.value = user;
|
|
125
|
+
await user.connect();
|
|
126
|
+
await user.register();
|
|
127
|
+
await registrationPromise;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
clearRegistrationTimer();
|
|
130
|
+
await disconnect(false);
|
|
131
|
+
connectionState.value = "error";
|
|
132
|
+
errorMessage.value = getErrorMessage(error);
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function disconnect(clearError = true) {
|
|
138
|
+
const user = simpleUser.value;
|
|
139
|
+
simpleUser.value = undefined;
|
|
140
|
+
|
|
141
|
+
if (user) {
|
|
142
|
+
try {
|
|
143
|
+
if (hasCall.value) await user.hangup();
|
|
144
|
+
} catch {
|
|
145
|
+
// The session may already have ended remotely.
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
await user.unregister();
|
|
149
|
+
} catch {
|
|
150
|
+
// Registration may not have completed.
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
await user.disconnect();
|
|
154
|
+
} catch {
|
|
155
|
+
// The WebSocket may already be closed.
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
connectionState.value = "disconnected";
|
|
160
|
+
activeConfig.value = undefined;
|
|
161
|
+
resetCall();
|
|
162
|
+
if (clearError) errorMessage.value = "";
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function call(target: string) {
|
|
166
|
+
const user = requireRegisteredUser();
|
|
167
|
+
const destination = normalizeDestination(target, activeConfig.value?.aor || "");
|
|
168
|
+
errorMessage.value = "";
|
|
169
|
+
currentTarget.value = destination;
|
|
170
|
+
callState.value = "calling";
|
|
171
|
+
try {
|
|
172
|
+
await user.call(destination);
|
|
173
|
+
} catch (error) {
|
|
174
|
+
resetCall();
|
|
175
|
+
errorMessage.value = getErrorMessage(error);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function answer() {
|
|
181
|
+
const user = requireRegisteredUser();
|
|
182
|
+
await user.answer();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function decline() {
|
|
186
|
+
const user = requireUser();
|
|
187
|
+
callState.value = "ending";
|
|
188
|
+
await user.decline();
|
|
189
|
+
resetCall();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function hangup() {
|
|
193
|
+
const user = requireUser();
|
|
194
|
+
callState.value = "ending";
|
|
195
|
+
await user.hangup();
|
|
196
|
+
resetCall();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function toggleMute() {
|
|
200
|
+
const user = requireUser();
|
|
201
|
+
const tracks = user.localMediaStream?.getAudioTracks() || [];
|
|
202
|
+
if (!tracks.length) throw new Error("No microphone track is available for this call.");
|
|
203
|
+
const enable = muted.value;
|
|
204
|
+
tracks.forEach((track) => {
|
|
205
|
+
track.enabled = enable;
|
|
206
|
+
});
|
|
207
|
+
muted.value = !enable;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function toggleCamera() {
|
|
211
|
+
const user = requireUser();
|
|
212
|
+
const tracks = user.localMediaStream?.getVideoTracks() || [];
|
|
213
|
+
if (!tracks.length) throw new Error("No camera track is available. Reconnect the web phone with video enabled.");
|
|
214
|
+
const enable = !cameraEnabled.value;
|
|
215
|
+
tracks.forEach((track) => {
|
|
216
|
+
track.enabled = enable;
|
|
217
|
+
});
|
|
218
|
+
cameraEnabled.value = enable;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function syncVideoState() {
|
|
222
|
+
const user = simpleUser.value;
|
|
223
|
+
const localStream = user?.localMediaStream;
|
|
224
|
+
const remoteStream = user?.remoteMediaStream;
|
|
225
|
+
const localTrack = localStream?.getVideoTracks()[0];
|
|
226
|
+
const remoteTrack = remoteStream?.getVideoTracks()[0];
|
|
227
|
+
|
|
228
|
+
const updateState = () => {
|
|
229
|
+
const currentLocalTrack = localStream?.getVideoTracks()[0];
|
|
230
|
+
const currentRemoteTrack = remoteStream?.getVideoTracks()[0];
|
|
231
|
+
localVideoAvailable.value = Boolean(currentLocalTrack && currentLocalTrack.readyState === "live");
|
|
232
|
+
remoteVideoAvailable.value = Boolean(currentRemoteTrack && currentRemoteTrack.readyState === "live");
|
|
233
|
+
cameraEnabled.value = Boolean(currentLocalTrack?.enabled);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
updateState();
|
|
237
|
+
if (localStream) localStream.onaddtrack = updateState;
|
|
238
|
+
if (remoteStream) remoteStream.onaddtrack = updateState;
|
|
239
|
+
|
|
240
|
+
if (localTrack) {
|
|
241
|
+
localTrack.onended = () => {
|
|
242
|
+
localVideoAvailable.value = false;
|
|
243
|
+
cameraEnabled.value = false;
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (remoteTrack) {
|
|
247
|
+
remoteTrack.onended = () => {
|
|
248
|
+
remoteVideoAvailable.value = false;
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function resetCall() {
|
|
254
|
+
callState.value = "idle";
|
|
255
|
+
currentTarget.value = "";
|
|
256
|
+
muted.value = false;
|
|
257
|
+
cameraEnabled.value = false;
|
|
258
|
+
localVideoAvailable.value = false;
|
|
259
|
+
remoteVideoAvailable.value = false;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function requireRegisteredUser() {
|
|
263
|
+
const user = requireUser();
|
|
264
|
+
if (!isRegistered.value) throw new Error("Connect the web phone before making a call.");
|
|
265
|
+
return user;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function requireUser() {
|
|
269
|
+
if (!simpleUser.value) throw new Error("Web phone is not connected.");
|
|
270
|
+
return simpleUser.value;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
onScopeDispose(() => {
|
|
274
|
+
void disconnect();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
connectionState: readonly(connectionState),
|
|
279
|
+
callState: readonly(callState),
|
|
280
|
+
errorMessage: readonly(errorMessage),
|
|
281
|
+
muted: readonly(muted),
|
|
282
|
+
cameraEnabled: readonly(cameraEnabled),
|
|
283
|
+
localVideoAvailable: readonly(localVideoAvailable),
|
|
284
|
+
remoteVideoAvailable: readonly(remoteVideoAvailable),
|
|
285
|
+
currentTarget: readonly(currentTarget),
|
|
286
|
+
isRegistered,
|
|
287
|
+
hasCall,
|
|
288
|
+
connect,
|
|
289
|
+
disconnect,
|
|
290
|
+
call,
|
|
291
|
+
answer,
|
|
292
|
+
decline,
|
|
293
|
+
hangup,
|
|
294
|
+
toggleMute,
|
|
295
|
+
toggleCamera,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function validateConfig(config: SipWebPhoneConfig) {
|
|
300
|
+
if (!/^wss:\/\//i.test(config.webSocketServer.trim())) {
|
|
301
|
+
throw new Error("SIP WebSocket URL must use WSS.");
|
|
302
|
+
}
|
|
303
|
+
if (!config.aor.trim() || !config.authorizationUsername.trim() || !config.authorizationPassword) {
|
|
304
|
+
throw new Error("SIP address, username, and password are required.");
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function normalizeSipUri(value: string) {
|
|
309
|
+
const trimmed = value.trim();
|
|
310
|
+
return trimmed.toLowerCase().startsWith("sip:") ? trimmed : `sip:${trimmed}`;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function normalizeDestination(target: string, aor: string) {
|
|
314
|
+
const trimmed = target.trim();
|
|
315
|
+
if (!trimmed) throw new Error("Enter an extension or SIP address to call.");
|
|
316
|
+
if (trimmed.toLowerCase().startsWith("sip:")) return trimmed;
|
|
317
|
+
if (trimmed.includes("@")) return `sip:${trimmed}`;
|
|
318
|
+
|
|
319
|
+
const domain = normalizeSipUri(aor).split("@")[1];
|
|
320
|
+
if (!domain) throw new Error("Enter a full SIP address for the destination.");
|
|
321
|
+
return `sip:${trimmed}@${domain}`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function getErrorMessage(error: unknown) {
|
|
325
|
+
if (error instanceof DOMException && error.name === "NotAllowedError") {
|
|
326
|
+
return "Camera or microphone permission was denied. Allow media access in the browser and reconnect the web phone.";
|
|
327
|
+
}
|
|
328
|
+
if (error instanceof DOMException && error.name === "NotFoundError") {
|
|
329
|
+
return "No camera or microphone was found for this video call.";
|
|
330
|
+
}
|
|
331
|
+
return error instanceof Error ? error.message : "SIP connection failed.";
|
|
332
|
+
}
|
|
@@ -99,6 +99,33 @@ export default function () {
|
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* The monitoring wall for one site.
|
|
104
|
+
*
|
|
105
|
+
* A different endpoint from `getAllSiteCameras`, and the difference is the
|
|
106
|
+
* point. That one is the Settings panel's paginated CRUD list; this one is
|
|
107
|
+
* purpose-built for a wall and is what the React Native app already uses:
|
|
108
|
+
*
|
|
109
|
+
* - it is **site-scoped and authorised server-side** — the caller must be a
|
|
110
|
+
* member of the site, of its owning organisation, or work for an
|
|
111
|
+
* organisation engaged to serve it;
|
|
112
|
+
* - it returns `type: "ip"` cameras only, so an ANPR unit can never land on a
|
|
113
|
+
* monitoring wall;
|
|
114
|
+
* - it returns each camera's **capability descriptor** and the server's own
|
|
115
|
+
* `unavailableReason`, so a tile explains itself instead of showing a black
|
|
116
|
+
* rectangle;
|
|
117
|
+
* - it never returns a credential, and it contacts no device.
|
|
118
|
+
*
|
|
119
|
+
* Unpaginated by design: a wall shows a site's cameras, and paging them was
|
|
120
|
+
* an artefact of borrowing the CRUD list.
|
|
121
|
+
*/
|
|
122
|
+
async function getSiteWall(siteId: string) {
|
|
123
|
+
return await useNuxtApp().$api<Record<string, any>>(
|
|
124
|
+
`/api/site-cameras/site/${siteId}/wall`,
|
|
125
|
+
{ method: "GET" }
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
102
129
|
async function updateSiteInformation(
|
|
103
130
|
siteId: string,
|
|
104
131
|
payload: { bgImage: string; description: string; docs: { id: string; name: string }[] }
|
|
@@ -175,6 +202,7 @@ export default function () {
|
|
|
175
202
|
updateSite,
|
|
176
203
|
addCamera,
|
|
177
204
|
getAllSiteCameras,
|
|
205
|
+
getSiteWall,
|
|
178
206
|
setSiteGuardPosts,
|
|
179
207
|
updateSiteCamera,
|
|
180
208
|
deleteSiteCameraById,
|
package/composables/useWebUsb.ts
CHANGED
|
@@ -11,6 +11,10 @@ interface USBDevice {
|
|
|
11
11
|
close(): Promise<void>;
|
|
12
12
|
selectConfiguration(configurationValue: number): Promise<void>;
|
|
13
13
|
claimInterface(interfaceNumber: number): Promise<void>;
|
|
14
|
+
selectAlternateInterface(
|
|
15
|
+
interfaceNumber: number,
|
|
16
|
+
alternateSetting: number,
|
|
17
|
+
): Promise<void>;
|
|
14
18
|
releaseInterface(interfaceNumber: number): Promise<void>;
|
|
15
19
|
transferOut(
|
|
16
20
|
endpointNumber: number,
|
|
@@ -19,14 +23,27 @@ interface USBDevice {
|
|
|
19
23
|
configuration: {
|
|
20
24
|
interfaces: Array<{
|
|
21
25
|
interfaceNumber: number;
|
|
26
|
+
alternate: USBAlternateInterface;
|
|
22
27
|
alternates: Array<{
|
|
28
|
+
alternateSetting: number;
|
|
29
|
+
interfaceClass: number;
|
|
23
30
|
endpoints: Array<{
|
|
24
31
|
endpointNumber: number;
|
|
25
32
|
direction: "in" | "out";
|
|
26
33
|
}>;
|
|
27
34
|
}>;
|
|
28
35
|
}>;
|
|
29
|
-
};
|
|
36
|
+
} | null;
|
|
37
|
+
configurations: Array<{ configurationValue: number }>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface USBAlternateInterface {
|
|
41
|
+
alternateSetting: number;
|
|
42
|
+
interfaceClass: number;
|
|
43
|
+
endpoints: Array<{
|
|
44
|
+
endpointNumber: number;
|
|
45
|
+
direction: "in" | "out";
|
|
46
|
+
}>;
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
interface USBOutTransferResult {
|
|
@@ -39,13 +56,86 @@ declare global {
|
|
|
39
56
|
usb: {
|
|
40
57
|
getDevices(): Promise<USBDevice[]>;
|
|
41
58
|
requestDevice(options: {
|
|
42
|
-
filters: Array<{
|
|
59
|
+
filters: Array<{
|
|
60
|
+
vendorId?: number;
|
|
61
|
+
productId?: number;
|
|
62
|
+
classCode?: number;
|
|
63
|
+
}>;
|
|
43
64
|
}): Promise<USBDevice>;
|
|
44
65
|
};
|
|
45
66
|
}
|
|
46
67
|
}
|
|
47
68
|
|
|
48
69
|
export default function useWebUsb() {
|
|
70
|
+
const printerConnections = new WeakMap<
|
|
71
|
+
USBDevice,
|
|
72
|
+
{ interfaceNumber: number; endpointNumber: number }
|
|
73
|
+
>();
|
|
74
|
+
|
|
75
|
+
const protectedInterfaceClasses = new Set([0x01, 0x03, 0x08, 0x0b, 0x0e, 0x10, 0xe0]);
|
|
76
|
+
|
|
77
|
+
function getErrorMessage(error: unknown, fallback: string) {
|
|
78
|
+
return error instanceof Error && error.message ? error.message : fallback;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function findPrinterInterface(device: USBDevice) {
|
|
82
|
+
const candidates = (device.configuration?.interfaces || []).flatMap((usbInterface) =>
|
|
83
|
+
usbInterface.alternates.flatMap((alternate) => {
|
|
84
|
+
const outputEndpoint = alternate.endpoints.find((endpoint) => endpoint.direction === "out");
|
|
85
|
+
if (!outputEndpoint || protectedInterfaceClasses.has(alternate.interfaceClass)) return [];
|
|
86
|
+
|
|
87
|
+
return [{
|
|
88
|
+
interfaceNumber: usbInterface.interfaceNumber,
|
|
89
|
+
alternateSetting: alternate.alternateSetting,
|
|
90
|
+
activeAlternateSetting: usbInterface.alternate.alternateSetting,
|
|
91
|
+
interfaceClass: alternate.interfaceClass,
|
|
92
|
+
endpointNumber: outputEndpoint.endpointNumber,
|
|
93
|
+
}];
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
candidates.sort((left, right) => {
|
|
98
|
+
const priority = (interfaceClass: number) => interfaceClass === 0x07 ? 0 : interfaceClass === 0xff ? 1 : 2;
|
|
99
|
+
return priority(left.interfaceClass) - priority(right.interfaceClass);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return candidates[0] || null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function preparePrinterDevice(device: USBDevice) {
|
|
106
|
+
await device.open();
|
|
107
|
+
if (!device.configuration) {
|
|
108
|
+
await device.selectConfiguration(device.configurations[0]?.configurationValue || 1);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const printerInterface = findPrinterInterface(device);
|
|
112
|
+
if (!printerInterface) {
|
|
113
|
+
throw new Error(
|
|
114
|
+
"The selected USB device is not a compatible printer. Select the receipt printer instead of a Bluetooth, camera, keyboard, or other system device.",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
await device.claimInterface(printerInterface.interfaceNumber);
|
|
119
|
+
if (printerInterface.alternateSetting !== printerInterface.activeAlternateSetting) {
|
|
120
|
+
await device.selectAlternateInterface(
|
|
121
|
+
printerInterface.interfaceNumber,
|
|
122
|
+
printerInterface.alternateSetting,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
printerConnections.set(device, {
|
|
127
|
+
interfaceNumber: printerInterface.interfaceNumber,
|
|
128
|
+
endpointNumber: printerInterface.endpointNumber,
|
|
129
|
+
});
|
|
130
|
+
return device;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getPrinterConnection(device: USBDevice) {
|
|
134
|
+
const connection = printerConnections.get(device);
|
|
135
|
+
if (!connection) throw new Error("Printer interface is not connected");
|
|
136
|
+
return connection;
|
|
137
|
+
}
|
|
138
|
+
|
|
49
139
|
const isWebUsbSupported = computed(() => {
|
|
50
140
|
return typeof navigator !== "undefined" && "usb" in navigator;
|
|
51
141
|
});
|
|
@@ -75,7 +165,9 @@ export default function useWebUsb() {
|
|
|
75
165
|
throw new Error("Web USB is not supported in this browser");
|
|
76
166
|
}
|
|
77
167
|
try {
|
|
78
|
-
const device = await navigator.usb.requestDevice({
|
|
168
|
+
const device = await navigator.usb.requestDevice({
|
|
169
|
+
filters: [{ classCode: 0x07 }, { classCode: 0xff }],
|
|
170
|
+
});
|
|
79
171
|
return {
|
|
80
172
|
vendorId: device.vendorId,
|
|
81
173
|
productId: device.productId,
|
|
@@ -84,12 +176,12 @@ export default function useWebUsb() {
|
|
|
84
176
|
serialNumber: device.serialNumber || "Unknown",
|
|
85
177
|
deviceId: `${device.vendorId}:${device.productId}`,
|
|
86
178
|
};
|
|
87
|
-
} catch (error:
|
|
88
|
-
if (error.name === "NotFoundError") {
|
|
179
|
+
} catch (error: unknown) {
|
|
180
|
+
if (error instanceof DOMException && error.name === "NotFoundError") {
|
|
89
181
|
throw new Error("No device selected");
|
|
90
182
|
}
|
|
91
183
|
console.error("Error requesting USB device:", error);
|
|
92
|
-
throw new Error("Failed to access USB
|
|
184
|
+
throw new Error(getErrorMessage(error, "Failed to access USB printer"));
|
|
93
185
|
}
|
|
94
186
|
};
|
|
95
187
|
|
|
@@ -110,14 +202,17 @@ export default function useWebUsb() {
|
|
|
110
202
|
});
|
|
111
203
|
}
|
|
112
204
|
console.log("[WebUSB] Opening device:", device.vendorId, device.productId);
|
|
113
|
-
await device
|
|
114
|
-
await device.selectConfiguration(1);
|
|
115
|
-
await device.claimInterface(0);
|
|
205
|
+
await preparePrinterDevice(device);
|
|
116
206
|
console.log("[WebUSB] Device opened and interface claimed.");
|
|
117
207
|
return device;
|
|
118
|
-
} catch (error) {
|
|
208
|
+
} catch (error: unknown) {
|
|
119
209
|
console.error("Error connecting to USB device:", error);
|
|
120
|
-
|
|
210
|
+
try {
|
|
211
|
+
await device?.close();
|
|
212
|
+
} catch (closeError) {
|
|
213
|
+
console.error("Error closing rejected USB device:", closeError);
|
|
214
|
+
}
|
|
215
|
+
throw new Error(getErrorMessage(error, "Failed to connect to USB printer"));
|
|
121
216
|
}
|
|
122
217
|
};
|
|
123
218
|
|
|
@@ -173,15 +268,20 @@ export default function useWebUsb() {
|
|
|
173
268
|
? "Connection and print test successful"
|
|
174
269
|
: "Connection successful but print test failed",
|
|
175
270
|
};
|
|
176
|
-
} catch (error:
|
|
271
|
+
} catch (error: unknown) {
|
|
177
272
|
return {
|
|
178
273
|
success: false,
|
|
179
|
-
error: error
|
|
274
|
+
error: getErrorMessage(error, "Failed to connect to USB printer"),
|
|
180
275
|
message: "Connection failed",
|
|
181
276
|
};
|
|
182
277
|
} finally {
|
|
183
278
|
if (device) {
|
|
184
279
|
try {
|
|
280
|
+
const connection = printerConnections.get(device);
|
|
281
|
+
if (connection) {
|
|
282
|
+
await device.releaseInterface(connection.interfaceNumber);
|
|
283
|
+
printerConnections.delete(device);
|
|
284
|
+
}
|
|
185
285
|
await device.close();
|
|
186
286
|
} catch (closeError) {
|
|
187
287
|
console.error("Error closing device:", closeError);
|
|
@@ -205,21 +305,16 @@ export default function useWebUsb() {
|
|
|
205
305
|
];
|
|
206
306
|
|
|
207
307
|
const testData = new Uint8Array(testCommands);
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
const outputEndpoint = alternate.endpoints.find(
|
|
211
|
-
(ep: any) => ep.direction === "out",
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
if (!outputEndpoint) {
|
|
215
|
-
throw new Error("No output endpoint found");
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
await device.transferOut(outputEndpoint.endpointNumber, testData);
|
|
308
|
+
const { endpointNumber } = getPrinterConnection(device);
|
|
309
|
+
await device.transferOut(endpointNumber, testData);
|
|
219
310
|
return { success: true, message: "Test print sent successfully" };
|
|
220
|
-
} catch (error:
|
|
311
|
+
} catch (error: unknown) {
|
|
221
312
|
console.error("Test print error:", error);
|
|
222
|
-
return {
|
|
313
|
+
return {
|
|
314
|
+
success: false,
|
|
315
|
+
error: getErrorMessage(error, "Test print failed"),
|
|
316
|
+
message: "Test print failed",
|
|
317
|
+
};
|
|
223
318
|
}
|
|
224
319
|
};
|
|
225
320
|
|
|
@@ -354,13 +449,8 @@ export default function useWebUsb() {
|
|
|
354
449
|
) => {
|
|
355
450
|
try {
|
|
356
451
|
console.log("[WebUSB] printQrCode start — doorLevel:", doorLevel, "liftLevel:", liftLevel, "company:", companyName, "address:", address);
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
const outputEndpoint = alternate.endpoints.find(
|
|
360
|
-
(ep: any) => ep.direction === "out",
|
|
361
|
-
);
|
|
362
|
-
if (!outputEndpoint) throw new Error("No output endpoint found");
|
|
363
|
-
console.log("[WebUSB] Using endpoint:", outputEndpoint.endpointNumber);
|
|
452
|
+
const { endpointNumber } = getPrinterConnection(device);
|
|
453
|
+
console.log("[WebUSB] Using endpoint:", endpointNumber);
|
|
364
454
|
|
|
365
455
|
const canvas = await createReceiptLayout(
|
|
366
456
|
urlImage,
|
|
@@ -375,17 +465,17 @@ export default function useWebUsb() {
|
|
|
375
465
|
const rasterData = canvasToRaster(canvas);
|
|
376
466
|
console.log("[WebUSB] Raster data size:", rasterData.byteLength, "bytes");
|
|
377
467
|
|
|
378
|
-
await device.transferOut(
|
|
468
|
+
await device.transferOut(endpointNumber, rasterData.buffer);
|
|
379
469
|
console.log("[WebUSB] Raster data sent.");
|
|
380
470
|
|
|
381
471
|
const CUT = new Uint8Array([0x1d, 0x56, 0x41, 0x10]);
|
|
382
|
-
await device.transferOut(
|
|
472
|
+
await device.transferOut(endpointNumber, CUT.buffer);
|
|
383
473
|
console.log("[WebUSB] Cut command sent.");
|
|
384
474
|
|
|
385
475
|
return { success: true, message: "QR print sent successfully" };
|
|
386
|
-
} catch (error:
|
|
476
|
+
} catch (error: unknown) {
|
|
387
477
|
console.error("[WebUSB] printQrCode error:", error);
|
|
388
|
-
return { success: false, error: error
|
|
478
|
+
return { success: false, error: getErrorMessage(error, "Unable to print QR code") };
|
|
389
479
|
}
|
|
390
480
|
};
|
|
391
481
|
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@7365admin1/layer-common",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "3.2.
|
|
5
|
+
"version": "3.2.1-staging.59",
|
|
6
6
|
"author": "7365admin1",
|
|
7
7
|
"main": "./nuxt.config.ts",
|
|
8
8
|
"publishConfig": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"qrcode": "^1.5.4",
|
|
41
41
|
"qrcode.vue": "^3.4.1",
|
|
42
42
|
"sass": "^1.80.6",
|
|
43
|
+
"sip.js": "0.21.2",
|
|
43
44
|
"socket.io-client": "^4.8.3",
|
|
44
45
|
"vue-draggable-next": "^2.3.0",
|
|
45
46
|
"vue3-signature": "^0.2.4",
|