camstreamerlib 4.0.0-beta.1 → 4.0.0-beta.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/README.md +3 -1
- package/cjs/CamOverlayAPI.js +176 -0
- package/cjs/CamOverlayDrawingAPI.js +232 -0
- package/cjs/CamOverlayPainter/Frame.js +301 -0
- package/cjs/CamOverlayPainter/Painter.js +168 -0
- package/cjs/CamOverlayPainter/ResourceManager.js +50 -0
- package/cjs/CamScripterAPI.js +70 -0
- package/cjs/CamScripterAPICameraEventsGenerator.js +159 -0
- package/cjs/CamStreamerAPI.js +65 -0
- package/{CamSwitcherAPI.d.ts → cjs/CamSwitcherAPI.d.ts} +3 -3
- package/cjs/CamSwitcherAPI.js +354 -0
- package/{CamSwitcherEvents.d.ts → cjs/CamSwitcherEvents.d.ts} +1 -1
- package/cjs/CamSwitcherEvents.js +67 -0
- package/cjs/CreatePackage.js +106 -0
- package/cjs/VapixAPI.js +458 -0
- package/cjs/VapixEvents.js +88 -0
- package/cjs/errors/errors.js +79 -0
- package/cjs/events/AxisCameraStationEvents.js +48 -0
- package/cjs/events/GenetecAgent.js +123 -0
- package/cjs/index.d.ts +11 -0
- package/cjs/index.js +31 -0
- package/cjs/internal/Digest.js +42 -0
- package/cjs/internal/ProxyClient.js +44 -0
- package/{internal → cjs/internal}/common.d.ts +0 -1
- package/cjs/internal/common.js +27 -0
- package/cjs/internal/constants.js +4 -0
- package/cjs/internal/transformers.js +32 -0
- package/cjs/internal/utils.js +46 -0
- package/cjs/internal/versionCompare.js +53 -0
- package/cjs/node/DefaultClient.js +54 -0
- package/cjs/node/HttpRequestSender.js +89 -0
- package/cjs/node/HttpServer.js +96 -0
- package/cjs/node/WsClient.js +149 -0
- package/cjs/node/WsEventClient.js +22 -0
- package/cjs/node/index.d.ts +2 -0
- package/cjs/node/index.js +7 -0
- package/cjs/types/CamOverlayAPI.js +47 -0
- package/cjs/types/CamScripterAPI.js +20 -0
- package/cjs/types/CamStreamerAPI.js +28 -0
- package/cjs/types/CamSwitcherAPI.js +137 -0
- package/{types/CamswitcherEvents.d.ts → cjs/types/CamSwitcherEvents.d.ts} +33 -0
- package/cjs/types/CamSwitcherEvents.js +62 -0
- package/{types → cjs/types}/VapixAPI.d.ts +7 -7
- package/cjs/types/VapixAPI.js +132 -0
- package/cjs/types/common.js +14 -0
- package/cjs/web/DefaultClient.js +20 -0
- package/cjs/web/WsClient.js +62 -0
- package/cjs/web/index.d.ts +2 -0
- package/cjs/web/index.js +7 -0
- package/esm/CamOverlayAPI.d.ts +31 -0
- package/esm/CamOverlayDrawingAPI.d.ts +86 -0
- package/esm/CamOverlayPainter/Frame.d.ts +96 -0
- package/esm/CamOverlayPainter/Painter.d.ts +37 -0
- package/esm/CamOverlayPainter/ResourceManager.d.ts +14 -0
- package/esm/CamScripterAPI.d.ts +19 -0
- package/esm/CamScripterAPICameraEventsGenerator.d.ts +74 -0
- package/esm/CamStreamerAPI.d.ts +16 -0
- package/esm/CamSwitcherAPI.d.ts +48 -0
- package/{CamSwitcherAPI.js → esm/CamSwitcherAPI.js} +3 -3
- package/esm/CamSwitcherEvents.d.ts +18 -0
- package/{CamSwitcherEvents.js → esm/CamSwitcherEvents.js} +1 -1
- package/esm/CreatePackage.d.ts +1 -0
- package/esm/VapixAPI.d.ts +66 -0
- package/{VapixAPI.js → esm/VapixAPI.js} +2 -3
- package/esm/VapixEvents.d.ts +43 -0
- package/esm/errors/errors.d.ts +34 -0
- package/esm/events/AxisCameraStationEvents.d.ts +9 -0
- package/esm/events/GenetecAgent.d.ts +174 -0
- package/esm/index.d.ts +11 -0
- package/esm/index.js +11 -0
- package/esm/internal/Digest.d.ts +4 -0
- package/esm/internal/ProxyClient.d.ts +11 -0
- package/esm/internal/common.d.ts +39 -0
- package/{internal → esm/internal}/common.js +0 -3
- package/esm/internal/constants.d.ts +1 -0
- package/esm/internal/transformers.d.ts +5 -0
- package/esm/internal/utils.d.ts +11 -0
- package/{internal → esm/internal}/utils.js +1 -1
- package/esm/internal/versionCompare.d.ts +6 -0
- package/esm/node/DefaultClient.d.ts +15 -0
- package/esm/node/HttpRequestSender.d.ts +28 -0
- package/esm/node/HttpServer.d.ts +21 -0
- package/esm/node/WsClient.d.ts +39 -0
- package/esm/node/WsEventClient.d.ts +13 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/types/CamOverlayAPI.d.ts +188 -0
- package/esm/types/CamScripterAPI.d.ts +67 -0
- package/esm/types/CamStreamerAPI.d.ts +139 -0
- package/esm/types/CamSwitcherAPI.d.ts +814 -0
- package/esm/types/CamSwitcherEvents.d.ts +491 -0
- package/{types/CamswitcherEvents.js → esm/types/CamSwitcherEvents.js} +4 -0
- package/esm/types/VapixAPI.d.ts +1704 -0
- package/{types → esm/types}/VapixAPI.js +1 -1
- package/esm/types/common.d.ts +37 -0
- package/esm/web/DefaultClient.d.ts +6 -0
- package/esm/web/WsClient.d.ts +13 -0
- package/esm/web/index.d.ts +2 -0
- package/esm/web/index.js +2 -0
- package/package.json +4 -4
- /package/{CamOverlayAPI.d.ts → cjs/CamOverlayAPI.d.ts} +0 -0
- /package/{CamOverlayDrawingAPI.d.ts → cjs/CamOverlayDrawingAPI.d.ts} +0 -0
- /package/{CamOverlayPainter → cjs/CamOverlayPainter}/Frame.d.ts +0 -0
- /package/{CamOverlayPainter → cjs/CamOverlayPainter}/Painter.d.ts +0 -0
- /package/{CamOverlayPainter → cjs/CamOverlayPainter}/ResourceManager.d.ts +0 -0
- /package/{CamScripterAPI.d.ts → cjs/CamScripterAPI.d.ts} +0 -0
- /package/{CamScripterAPICameraEventsGenerator.d.ts → cjs/CamScripterAPICameraEventsGenerator.d.ts} +0 -0
- /package/{CamStreamerAPI.d.ts → cjs/CamStreamerAPI.d.ts} +0 -0
- /package/{CreatePackage.d.ts → cjs/CreatePackage.d.ts} +0 -0
- /package/{VapixAPI.d.ts → cjs/VapixAPI.d.ts} +0 -0
- /package/{VapixEvents.d.ts → cjs/VapixEvents.d.ts} +0 -0
- /package/{errors → cjs/errors}/errors.d.ts +0 -0
- /package/{events → cjs/events}/AxisCameraStationEvents.d.ts +0 -0
- /package/{events → cjs/events}/GenetecAgent.d.ts +0 -0
- /package/{internal → cjs/internal}/Digest.d.ts +0 -0
- /package/{internal → cjs/internal}/ProxyClient.d.ts +0 -0
- /package/{internal → cjs/internal}/constants.d.ts +0 -0
- /package/{internal → cjs/internal}/transformers.d.ts +0 -0
- /package/{internal → cjs/internal}/utils.d.ts +0 -0
- /package/{internal → cjs/internal}/versionCompare.d.ts +0 -0
- /package/{node → cjs/node}/DefaultClient.d.ts +0 -0
- /package/{node → cjs/node}/HttpRequestSender.d.ts +0 -0
- /package/{node → cjs/node}/HttpServer.d.ts +0 -0
- /package/{node → cjs/node}/WsClient.d.ts +0 -0
- /package/{node → cjs/node}/WsEventClient.d.ts +0 -0
- /package/{types → cjs/types}/CamOverlayAPI.d.ts +0 -0
- /package/{types → cjs/types}/CamScripterAPI.d.ts +0 -0
- /package/{types → cjs/types}/CamStreamerAPI.d.ts +0 -0
- /package/{types → cjs/types}/CamSwitcherAPI.d.ts +0 -0
- /package/{types → cjs/types}/common.d.ts +0 -0
- /package/{web → cjs/web}/DefaultClient.d.ts +0 -0
- /package/{web → cjs/web}/WsClient.d.ts +0 -0
- /package/{CamOverlayAPI.js → esm/CamOverlayAPI.js} +0 -0
- /package/{CamOverlayDrawingAPI.js → esm/CamOverlayDrawingAPI.js} +0 -0
- /package/{CamOverlayPainter → esm/CamOverlayPainter}/Frame.js +0 -0
- /package/{CamOverlayPainter → esm/CamOverlayPainter}/Painter.js +0 -0
- /package/{CamOverlayPainter → esm/CamOverlayPainter}/ResourceManager.js +0 -0
- /package/{CamScripterAPI.js → esm/CamScripterAPI.js} +0 -0
- /package/{CamScripterAPICameraEventsGenerator.js → esm/CamScripterAPICameraEventsGenerator.js} +0 -0
- /package/{CamStreamerAPI.js → esm/CamStreamerAPI.js} +0 -0
- /package/{CreatePackage.js → esm/CreatePackage.js} +0 -0
- /package/{VapixEvents.js → esm/VapixEvents.js} +0 -0
- /package/{errors → esm/errors}/errors.js +0 -0
- /package/{events → esm/events}/AxisCameraStationEvents.js +0 -0
- /package/{events → esm/events}/GenetecAgent.js +0 -0
- /package/{internal → esm/internal}/Digest.js +0 -0
- /package/{internal → esm/internal}/ProxyClient.js +0 -0
- /package/{internal → esm/internal}/constants.js +0 -0
- /package/{internal → esm/internal}/transformers.js +0 -0
- /package/{internal → esm/internal}/versionCompare.js +0 -0
- /package/{node → esm/node}/DefaultClient.js +0 -0
- /package/{node → esm/node}/HttpRequestSender.js +0 -0
- /package/{node → esm/node}/HttpServer.js +0 -0
- /package/{node → esm/node}/WsClient.js +0 -0
- /package/{node → esm/node}/WsEventClient.js +0 -0
- /package/{types → esm/types}/CamOverlayAPI.js +0 -0
- /package/{types → esm/types}/CamScripterAPI.js +0 -0
- /package/{types → esm/types}/CamStreamerAPI.js +0 -0
- /package/{types → esm/types}/CamSwitcherAPI.js +0 -0
- /package/{types → esm/types}/common.js +0 -0
- /package/{web → esm/web}/DefaultClient.js +0 -0
- /package/{web → esm/web}/WsClient.js +0 -0
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
**This is beta version of CamStreamerLib v4, dont use it on production. Is going to be changed significantly.**
|
|
4
4
|
|
|
5
|
+
**The documentation is in progress**
|
|
6
|
+
|
|
5
7
|
Node.js helper library for CamStreamer ACAP applications.
|
|
6
8
|
|
|
7
9
|
The library is primarily developed for the CamScripter ACAP application running directly in Axis cameras.
|
|
@@ -17,7 +19,7 @@ npm install camstreamerlib
|
|
|
17
19
|
|
|
18
20
|
- [HttpServer](doc/HttpServer.md) is a module for processing HTTP requests in your scripts. It also automatically serves up the content from html directory or you can register paths which you can process by your own (e.g. http://$CAMERA_IP/local/camscripter/proxy/$MY_PACKAGE_NAME/control.cgi).
|
|
19
21
|
|
|
20
|
-
- [
|
|
22
|
+
- [VapixAPI](doc/VapixAPI.md) is a module to access Axis camera VAPIX interface.
|
|
21
23
|
|
|
22
24
|
- [CamStreamerAPI](doc/CamStreamerAPI.md) is a module for easy control of video streaming in the CamStreamer ACAP application (RTMP, HLS, SRT and MPEG-TS protocols).
|
|
23
25
|
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CamOverlayAPI = void 0;
|
|
4
|
+
const common_1 = require("./internal/common");
|
|
5
|
+
const DefaultClient_1 = require("./node/DefaultClient");
|
|
6
|
+
const CamOverlayAPI_1 = require("./types/CamOverlayAPI");
|
|
7
|
+
const errors_1 = require("./errors/errors");
|
|
8
|
+
const common_2 = require("./types/common");
|
|
9
|
+
class CamOverlayAPI {
|
|
10
|
+
client;
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
if ((0, common_1.isClient)(options)) {
|
|
13
|
+
this.client = options;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this.client = new DefaultClient_1.DefaultClient(options);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async checkCameraTime() {
|
|
20
|
+
const cameraTime = await this.get('/local/camoverlay/api/camera_time.cgi');
|
|
21
|
+
return cameraTime.state;
|
|
22
|
+
}
|
|
23
|
+
async getNetworkCameraList() {
|
|
24
|
+
const response = await this.get('/local/camoverlay/api/network_camera_list.cgi');
|
|
25
|
+
return common_2.networkCameraListSchema.parse(response.camera_list);
|
|
26
|
+
}
|
|
27
|
+
async wsAutoratization() {
|
|
28
|
+
const response = await this.get(`/local/camoverlay/api/ws_authorization.cgi`);
|
|
29
|
+
return response.data;
|
|
30
|
+
}
|
|
31
|
+
async getMjpegStreamImage(mjpegUrl) {
|
|
32
|
+
const res = await this.get(`/local/camoverlay/api/fetch_mjpeg_image.cgi?mjpeg_url=${encodeURIComponent(decodeURIComponent(mjpegUrl))}`);
|
|
33
|
+
return await this.parseBlobResponse(res);
|
|
34
|
+
}
|
|
35
|
+
async listFiles(fileType) {
|
|
36
|
+
const files = await this.get(`/local/camoverlay/api/upload_${fileType}.cgi?action=list`);
|
|
37
|
+
return CamOverlayAPI_1.fileListSchema.parse(files.list);
|
|
38
|
+
}
|
|
39
|
+
async uploadFile(fileType, file, fileName) {
|
|
40
|
+
const formData = new FormData();
|
|
41
|
+
formData.append('target', 'SD0');
|
|
42
|
+
formData.append('uploadedFile[]', file, fileName);
|
|
43
|
+
const path = `/local/camoverlay/api/upload_${fileType}.cgi?action=upload`;
|
|
44
|
+
await this.post(path, formData);
|
|
45
|
+
}
|
|
46
|
+
async removeFile(fileType, file) {
|
|
47
|
+
const path = `/local/camoverlay/api/upload_${fileType}.cgi?action=remove`;
|
|
48
|
+
await this.post(path, JSON.stringify(file));
|
|
49
|
+
}
|
|
50
|
+
async getFileStorage(fileType) {
|
|
51
|
+
const data = await this.get(`/local/camoverlay/api/upload_${fileType}.cgi?action=get_storage`);
|
|
52
|
+
return CamOverlayAPI_1.storageSchema.parse(data);
|
|
53
|
+
}
|
|
54
|
+
async updateInfoticker(serviceID, text) {
|
|
55
|
+
await this.get(`/local/camoverlay/api/infoticker.cgi?service_id=${serviceID}&text=${text}`);
|
|
56
|
+
}
|
|
57
|
+
async setEnabled(serviceID, enabled) {
|
|
58
|
+
await this.post(`/local/camoverlay/api/enabled.cgi?id_${serviceID}=${enabled ? 1 : 0}`, '');
|
|
59
|
+
}
|
|
60
|
+
async isEnabled(serviceID) {
|
|
61
|
+
const res = await this.client.get('/local/camoverlay/api/services.cgi?action=get');
|
|
62
|
+
if (res.ok) {
|
|
63
|
+
const data = JSON.parse(await res.text());
|
|
64
|
+
for (const service of data.services) {
|
|
65
|
+
if (service.id === serviceID) {
|
|
66
|
+
return service.enabled === 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new errors_1.ServiceNotFoundError();
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw new Error(await (0, common_1.responseStringify)(res));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async getSingleService(serviceId) {
|
|
76
|
+
const data = await this.get('/local/camoverlay/api/services.cgi', {
|
|
77
|
+
action: 'get',
|
|
78
|
+
service_id: serviceId.toString(),
|
|
79
|
+
});
|
|
80
|
+
return CamOverlayAPI_1.serviceSchema.parse(data);
|
|
81
|
+
}
|
|
82
|
+
async getServices() {
|
|
83
|
+
const serviceList = await this.get('/local/camoverlay/api/services.cgi?action=get');
|
|
84
|
+
return CamOverlayAPI_1.serviceSchema.parse(serviceList).services;
|
|
85
|
+
}
|
|
86
|
+
async updateSingleService(serviceId, serviceJson) {
|
|
87
|
+
const path = '/local/camoverlay/api/services.cgi';
|
|
88
|
+
await this.post(path, JSON.stringify(serviceJson), {
|
|
89
|
+
action: 'set',
|
|
90
|
+
service_id: serviceId.toString(),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async updateServices(servicesJson) {
|
|
94
|
+
const path = '/local/camoverlay/api/services.cgi?action=set';
|
|
95
|
+
await this.post(path, JSON.stringify(servicesJson));
|
|
96
|
+
}
|
|
97
|
+
updateCGText(serviceID, fields) {
|
|
98
|
+
const params = {};
|
|
99
|
+
for (const field of fields) {
|
|
100
|
+
const name = field.field_name;
|
|
101
|
+
params[name] = field.text;
|
|
102
|
+
if (field.color !== undefined) {
|
|
103
|
+
params[`${name}_color`] = field.color;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return this.promiseCGUpdate(serviceID, 'update_text', params);
|
|
107
|
+
}
|
|
108
|
+
updateCGImagePos(serviceID, coordinates = '', x = 0, y = 0) {
|
|
109
|
+
const params = {
|
|
110
|
+
coord_system: coordinates,
|
|
111
|
+
pos_x: x,
|
|
112
|
+
pos_y: y,
|
|
113
|
+
};
|
|
114
|
+
return this.promiseCGUpdate(serviceID, 'update_image', params);
|
|
115
|
+
}
|
|
116
|
+
updateCGImage(serviceID, path, coordinates = '', x = 0, y = 0) {
|
|
117
|
+
const params = {
|
|
118
|
+
coord_system: coordinates,
|
|
119
|
+
pos_x: x,
|
|
120
|
+
pos_y: y,
|
|
121
|
+
image: path,
|
|
122
|
+
};
|
|
123
|
+
return this.promiseCGUpdate(serviceID, 'update_image', params);
|
|
124
|
+
}
|
|
125
|
+
updateCGImageFromData(serviceID, imageType, imageData, coordinates = '', x = 0, y = 0) {
|
|
126
|
+
const contentType = imageType === CamOverlayAPI_1.ImageType.PNG ? 'image/png' : 'image/jpeg';
|
|
127
|
+
const params = {
|
|
128
|
+
coord_system: coordinates,
|
|
129
|
+
pos_x: x,
|
|
130
|
+
pos_y: y,
|
|
131
|
+
};
|
|
132
|
+
return this.promiseCGUpdate(serviceID, 'update_image', params, contentType, imageData);
|
|
133
|
+
}
|
|
134
|
+
async promiseCGUpdate(serviceID, action, params = {}, contentType, data) {
|
|
135
|
+
const path = `/local/camoverlay/api/customGraphics.cgi`;
|
|
136
|
+
let headers = {};
|
|
137
|
+
if (contentType !== undefined && data) {
|
|
138
|
+
headers = { 'Content-Type': contentType };
|
|
139
|
+
}
|
|
140
|
+
const res = await this.client.post(path, data ?? '', {
|
|
141
|
+
action: action,
|
|
142
|
+
service_id: serviceID.toString(),
|
|
143
|
+
...params,
|
|
144
|
+
}, headers);
|
|
145
|
+
if (!res.ok) {
|
|
146
|
+
throw new Error(await (0, common_1.responseStringify)(res));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async get(path, params) {
|
|
150
|
+
const res = await this.client.get(path, params);
|
|
151
|
+
if (res.ok) {
|
|
152
|
+
return await res.json();
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
throw new Error(await (0, common_1.responseStringify)(res));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async post(path, data, params) {
|
|
159
|
+
const res = await this.client.post(path, data, params);
|
|
160
|
+
if (res.ok) {
|
|
161
|
+
return await res.json();
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
throw new Error(await (0, common_1.responseStringify)(res));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async parseBlobResponse(response) {
|
|
168
|
+
try {
|
|
169
|
+
return await response.blob();
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
throw new errors_1.ParsingBlobError(err);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.CamOverlayAPI = CamOverlayAPI;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CamOverlayDrawingAPI = void 0;
|
|
4
|
+
const EventEmitter = require("events");
|
|
5
|
+
const WsClient_1 = require("./node/WsClient");
|
|
6
|
+
class CamOverlayDrawingAPI extends EventEmitter {
|
|
7
|
+
tls;
|
|
8
|
+
tlsInsecure;
|
|
9
|
+
ip;
|
|
10
|
+
port;
|
|
11
|
+
user;
|
|
12
|
+
pass;
|
|
13
|
+
cameraList;
|
|
14
|
+
zIndex;
|
|
15
|
+
callId;
|
|
16
|
+
sendMessages;
|
|
17
|
+
timeoutCheckTimer;
|
|
18
|
+
wsConnected;
|
|
19
|
+
ws;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super();
|
|
22
|
+
this.tls = options?.tls ?? false;
|
|
23
|
+
this.tlsInsecure = options?.tlsInsecure ?? false;
|
|
24
|
+
this.ip = options?.ip ?? '127.0.0.1';
|
|
25
|
+
this.port = options?.port ?? (this.tls ? 443 : 80);
|
|
26
|
+
this.user = options?.user ?? '';
|
|
27
|
+
this.pass = options?.pass ?? '';
|
|
28
|
+
this.zIndex = options?.zIndex ?? 0;
|
|
29
|
+
this.cameraList = [0];
|
|
30
|
+
if (options && Array.isArray(options.camera)) {
|
|
31
|
+
this.cameraList = options.camera;
|
|
32
|
+
}
|
|
33
|
+
else if (typeof options?.camera === 'number') {
|
|
34
|
+
this.cameraList = [options.camera];
|
|
35
|
+
}
|
|
36
|
+
this.callId = 0;
|
|
37
|
+
this.sendMessages = {};
|
|
38
|
+
this.wsConnected = false;
|
|
39
|
+
this.createWsClient();
|
|
40
|
+
EventEmitter.call(this);
|
|
41
|
+
}
|
|
42
|
+
connect() {
|
|
43
|
+
this.ws.open();
|
|
44
|
+
this.startMsgsTimeoutCheck();
|
|
45
|
+
}
|
|
46
|
+
disconnect() {
|
|
47
|
+
this.ws.close();
|
|
48
|
+
this.stopMsgsTimeoutCheck();
|
|
49
|
+
}
|
|
50
|
+
isConnected() {
|
|
51
|
+
return this.wsConnected;
|
|
52
|
+
}
|
|
53
|
+
cairo(command, ...params) {
|
|
54
|
+
return this.sendMessage({ command: command, params: params });
|
|
55
|
+
}
|
|
56
|
+
writeText(...params) {
|
|
57
|
+
return this.sendMessage({ command: 'write_text', params: params });
|
|
58
|
+
}
|
|
59
|
+
uploadImageData(imgBuffer) {
|
|
60
|
+
return this.sendBinaryMessage({
|
|
61
|
+
command: 'upload_image_data',
|
|
62
|
+
params: [],
|
|
63
|
+
}, imgBuffer);
|
|
64
|
+
}
|
|
65
|
+
uploadFontData(fontBuffer) {
|
|
66
|
+
return this.sendBinaryMessage({
|
|
67
|
+
command: 'upload_font_data',
|
|
68
|
+
params: [fontBuffer.toString('base64')],
|
|
69
|
+
}, fontBuffer);
|
|
70
|
+
}
|
|
71
|
+
showCairoImage(cairoImage, posX, posY) {
|
|
72
|
+
return this.sendMessage({
|
|
73
|
+
command: 'show_cairo_image_v2',
|
|
74
|
+
params: [cairoImage, posX, posY, this.cameraList, this.zIndex],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
showCairoImageAbsolute(cairoImage, posX, posY, width, height) {
|
|
78
|
+
return this.sendMessage({
|
|
79
|
+
command: 'show_cairo_image_v2',
|
|
80
|
+
params: [
|
|
81
|
+
cairoImage,
|
|
82
|
+
-1.0 + (2.0 / width) * posX,
|
|
83
|
+
-1.0 + (2.0 / height) * posY,
|
|
84
|
+
this.cameraList,
|
|
85
|
+
this.zIndex,
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
removeImage() {
|
|
90
|
+
return this.sendMessage({ command: 'remove_image_v2', params: [] });
|
|
91
|
+
}
|
|
92
|
+
createWsClient() {
|
|
93
|
+
const options = {
|
|
94
|
+
ip: this.ip,
|
|
95
|
+
port: this.port,
|
|
96
|
+
address: '/local/camoverlay/ws',
|
|
97
|
+
protocol: 'cairo-api',
|
|
98
|
+
user: this.user,
|
|
99
|
+
pass: this.pass,
|
|
100
|
+
tls: this.tls,
|
|
101
|
+
tlsInsecure: this.tlsInsecure,
|
|
102
|
+
};
|
|
103
|
+
this.ws = new WsClient_1.WsClient(options);
|
|
104
|
+
this.ws.on('open', () => {
|
|
105
|
+
console.log('CamOverlay connection opened');
|
|
106
|
+
this.wsConnected = true;
|
|
107
|
+
this.emit('open');
|
|
108
|
+
});
|
|
109
|
+
this.ws.on('message', (msgData) => this.incomingWsMessageHandler(msgData));
|
|
110
|
+
this.ws.on('error', (error) => {
|
|
111
|
+
this.reportError(error);
|
|
112
|
+
});
|
|
113
|
+
this.ws.on('close', () => {
|
|
114
|
+
console.log('CamOverlay connection closed');
|
|
115
|
+
this.wsConnected = false;
|
|
116
|
+
this.reportClose();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
incomingWsMessageHandler(msgData) {
|
|
120
|
+
const dataJSON = JSON.parse(msgData.toString());
|
|
121
|
+
let errorResponse;
|
|
122
|
+
if ('error' in dataJSON) {
|
|
123
|
+
errorResponse = dataJSON;
|
|
124
|
+
}
|
|
125
|
+
if (dataJSON.call_id !== undefined) {
|
|
126
|
+
if (errorResponse !== undefined) {
|
|
127
|
+
this.sendMessages[dataJSON.call_id].reject(new Error(errorResponse.error));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
this.sendMessages[dataJSON.call_id].resolve(dataJSON);
|
|
131
|
+
}
|
|
132
|
+
delete this.sendMessages[dataJSON.call_id];
|
|
133
|
+
}
|
|
134
|
+
if (errorResponse !== undefined) {
|
|
135
|
+
this.reconnectWithError(new Error(errorResponse.error));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
this.reportMessage(msgData.toString());
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
sendMessage(msgJson) {
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
try {
|
|
144
|
+
if (!this.wsConnected) {
|
|
145
|
+
throw new Error('No CamOverlay connection');
|
|
146
|
+
}
|
|
147
|
+
msgJson.call_id = ++this.callId;
|
|
148
|
+
this.ws.send(JSON.stringify(msgJson));
|
|
149
|
+
this.sendMessages[this.callId] = { resolve, reject, sentTimestamp: Date.now() };
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
|
153
|
+
const error = new Error(`Send message error: ${errorMessage}`);
|
|
154
|
+
reject(error);
|
|
155
|
+
if (!this.wsConnected) {
|
|
156
|
+
this.reportError(error);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.reconnectWithError(error);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
sendBinaryMessage(msgJson, data) {
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
try {
|
|
167
|
+
if (!this.wsConnected) {
|
|
168
|
+
throw new Error('No CamOverlay connection');
|
|
169
|
+
}
|
|
170
|
+
msgJson.call_id = ++this.callId;
|
|
171
|
+
const jsonBuffer = Buffer.from(JSON.stringify(msgJson));
|
|
172
|
+
const header = new ArrayBuffer(5);
|
|
173
|
+
const headerView = new DataView(header);
|
|
174
|
+
headerView.setInt8(0, 1);
|
|
175
|
+
headerView.setInt32(1, jsonBuffer.byteLength);
|
|
176
|
+
const msgBuffer = Buffer.concat([Buffer.from(header), jsonBuffer, data]);
|
|
177
|
+
this.ws.send(msgBuffer);
|
|
178
|
+
this.sendMessages[this.callId] = { resolve, reject, sentTimestamp: Date.now() };
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
|
182
|
+
const error = new Error(`Send binary message error: ${errorMessage}`);
|
|
183
|
+
reject(error);
|
|
184
|
+
if (!this.wsConnected) {
|
|
185
|
+
this.reportError(error);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
this.reconnectWithError(error);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
startMsgsTimeoutCheck() {
|
|
194
|
+
clearInterval(this.timeoutCheckTimer);
|
|
195
|
+
this.timeoutCheckTimer = setInterval(() => {
|
|
196
|
+
let reconnect = false;
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
for (const callId in this.sendMessages) {
|
|
199
|
+
const msg = this.sendMessages[callId];
|
|
200
|
+
if (now - msg.sentTimestamp > 10000) {
|
|
201
|
+
reconnect = true;
|
|
202
|
+
msg.reject(new Error('Message timeout'));
|
|
203
|
+
delete this.sendMessages[callId];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (reconnect) {
|
|
207
|
+
this.reconnectWithError(new Error('Message timeout'));
|
|
208
|
+
}
|
|
209
|
+
}, 5000);
|
|
210
|
+
}
|
|
211
|
+
stopMsgsTimeoutCheck() {
|
|
212
|
+
clearInterval(this.timeoutCheckTimer);
|
|
213
|
+
}
|
|
214
|
+
reconnectWithError(err) {
|
|
215
|
+
this.reportError(err);
|
|
216
|
+
this.ws.reconnect();
|
|
217
|
+
}
|
|
218
|
+
reportMessage(msg) {
|
|
219
|
+
this.emit('message', msg);
|
|
220
|
+
}
|
|
221
|
+
reportError(err) {
|
|
222
|
+
this.emit('error', err);
|
|
223
|
+
}
|
|
224
|
+
reportClose() {
|
|
225
|
+
for (const callId in this.sendMessages) {
|
|
226
|
+
this.sendMessages[callId].reject(new Error('Connection lost'));
|
|
227
|
+
}
|
|
228
|
+
this.sendMessages = {};
|
|
229
|
+
this.emit('close');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
exports.CamOverlayDrawingAPI = CamOverlayDrawingAPI;
|