@blueyerobotics/blueye-ts 2.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -8
- package/dist/example.js +12 -9
- package/dist/src/client.d.ts +20 -1
- package/dist/src/client.js +67 -14
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -15,19 +15,23 @@ import { BlueyeClient } from "@blueyerobotics/blueye-ts";
|
|
|
15
15
|
|
|
16
16
|
const client = new BlueyeClient();
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
client.on("connected", async () => {
|
|
19
|
+
// request battery information
|
|
20
|
+
const batteryRep = await client.sendRequest("GetBatteryReq");
|
|
21
|
+
console.log("batteryRep:", batteryRep);
|
|
21
22
|
|
|
22
|
-
// get latest battery telemetry
|
|
23
|
-
const batteryTel = await client.getTelemetry("BatteryTel");
|
|
24
|
-
console.log("batteryTel:", batteryTel);
|
|
23
|
+
// get latest battery telemetry
|
|
24
|
+
const batteryTel = await client.getTelemetry("BatteryTel");
|
|
25
|
+
console.log("batteryTel:", batteryTel);
|
|
25
26
|
|
|
26
|
-
// send a control message to
|
|
27
|
-
await client.sendControl("LightsCtrl", { lights: { value: 1 } });
|
|
27
|
+
// send a control message to change the light intensity to 1
|
|
28
|
+
await client.sendControl("LightsCtrl", { lights: { value: 1 } });
|
|
29
|
+
});
|
|
28
30
|
|
|
29
31
|
// subscribe to battery telemetry updates
|
|
30
32
|
client.on("BatteryTel", data => {
|
|
31
33
|
console.log("received BatteryTel:", data);
|
|
32
34
|
});
|
|
35
|
+
|
|
36
|
+
client.connect();
|
|
33
37
|
```
|
package/dist/example.js
CHANGED
|
@@ -3,17 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const index_1 = require("./index");
|
|
4
4
|
const main = async () => {
|
|
5
5
|
const client = new index_1.BlueyeClient();
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
client.on("connected", async () => {
|
|
7
|
+
// request battery information
|
|
8
|
+
const batteryRep = await client.sendRequest("GetBatteryReq");
|
|
9
|
+
console.log("batteryRep:", batteryRep);
|
|
10
|
+
// get latest battery telemetry
|
|
11
|
+
const batteryTel = await client.getTelemetry("BatteryTel");
|
|
12
|
+
console.log("batteryTel:", batteryTel);
|
|
13
|
+
// send a control message to change the light intensity to 1
|
|
14
|
+
await client.sendControl("LightsCtrl", { lights: { value: 1 } });
|
|
15
|
+
});
|
|
14
16
|
// subscribe to battery telemetry updates
|
|
15
|
-
client.on("BatteryTel", data => {
|
|
17
|
+
client.on("BatteryTel", (data) => {
|
|
16
18
|
console.log("received BatteryTel:", data);
|
|
17
19
|
});
|
|
20
|
+
client.connect();
|
|
18
21
|
};
|
|
19
22
|
main();
|
package/dist/src/client.d.ts
CHANGED
|
@@ -13,18 +13,37 @@ export type MsgHandler<T extends Req | Ctrl> = Protocol[T];
|
|
|
13
13
|
export type CreateArgs<T extends Req | Ctrl> = Parameters<MsgHandler<T>["create"]>[0];
|
|
14
14
|
export type DecodedOutput<T extends Req> = ReturnType<ReqToRep<T>["decode"]>;
|
|
15
15
|
export type DecodedTelOutput<T extends Tel> = ReturnType<Protocol[T]["decode"]>;
|
|
16
|
+
type State = "connecting" | "connected" | "disconnected";
|
|
16
17
|
export type Events = {
|
|
18
|
+
[K in State]: [];
|
|
19
|
+
} & {
|
|
17
20
|
[K in Tel]: [DecodedTelOutput<K>];
|
|
18
21
|
};
|
|
19
22
|
export declare const isInProtocol: (key: string) => key is keyof typeof blueye.protocol;
|
|
23
|
+
type Options = Partial<{
|
|
24
|
+
subUrl: string;
|
|
25
|
+
rpcUrl: string;
|
|
26
|
+
pubUrl: string;
|
|
27
|
+
timeout: number;
|
|
28
|
+
logLevel: LogLevel;
|
|
29
|
+
autoConnect: boolean;
|
|
30
|
+
}>;
|
|
20
31
|
export declare class BlueyeClient extends Emitter<Events> {
|
|
32
|
+
state: State;
|
|
21
33
|
timeout: number;
|
|
34
|
+
private subUrl;
|
|
35
|
+
private rpcUrl;
|
|
36
|
+
private pubUrl;
|
|
22
37
|
private sub;
|
|
23
38
|
private rpc;
|
|
24
39
|
private pub;
|
|
25
40
|
private logger;
|
|
26
|
-
constructor(timeout
|
|
41
|
+
constructor({ subUrl, rpcUrl, pubUrl, timeout, logLevel, autoConnect, }?: Options);
|
|
42
|
+
private updateState;
|
|
43
|
+
connect(): void;
|
|
44
|
+
disconnect(): void;
|
|
27
45
|
sendRequest<T extends Req>(req: T, opts?: CreateArgs<T>): Promise<DecodedOutput<T> | null>;
|
|
28
46
|
getTelemetry<T extends Tel>(type: T): Promise<DecodedTelOutput<T>>;
|
|
29
47
|
sendControl<T extends Ctrl>(ctrl: T, opts?: CreateArgs<T>): Promise<void>;
|
|
30
48
|
}
|
|
49
|
+
export {};
|
package/dist/src/client.js
CHANGED
|
@@ -7,30 +7,36 @@ const consola_1 = require("consola");
|
|
|
7
7
|
const jszmq_1 = require("jszmq");
|
|
8
8
|
const strict_event_emitter_1 = require("strict-event-emitter");
|
|
9
9
|
const schema_1 = require("./schema");
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
10
|
+
const DEFAULT_SUB_URL = "ws://192.168.1.101:9985";
|
|
11
|
+
const DEFAULT_RPC_URL = "ws://192.168.1.101:9986";
|
|
12
|
+
const DEFAULT_PUB_URL = "ws://192.168.1.101:9987";
|
|
13
13
|
const isInProtocol = (key) => {
|
|
14
14
|
return key in protocol_definitions_1.blueye.protocol;
|
|
15
15
|
};
|
|
16
16
|
exports.isInProtocol = isInProtocol;
|
|
17
17
|
class BlueyeClient extends strict_event_emitter_1.Emitter {
|
|
18
|
+
state = "disconnected";
|
|
18
19
|
timeout;
|
|
20
|
+
subUrl;
|
|
21
|
+
rpcUrl;
|
|
22
|
+
pubUrl;
|
|
19
23
|
sub;
|
|
20
24
|
rpc;
|
|
21
25
|
pub;
|
|
22
26
|
logger;
|
|
23
|
-
constructor(timeout = 2000, logLevel = consola_1.LogLevels.info) {
|
|
27
|
+
constructor({ subUrl = DEFAULT_SUB_URL, rpcUrl = DEFAULT_RPC_URL, pubUrl = DEFAULT_PUB_URL, timeout = 2000, logLevel = consola_1.LogLevels.info, autoConnect = false, } = {}) {
|
|
24
28
|
super();
|
|
25
29
|
this.timeout = timeout;
|
|
26
|
-
this.logger = (0, consola_1.createConsola)({
|
|
30
|
+
this.logger = (0, consola_1.createConsola)({
|
|
31
|
+
level: logLevel,
|
|
32
|
+
formatOptions: { colors: true, compact: false },
|
|
33
|
+
});
|
|
34
|
+
this.subUrl = subUrl;
|
|
35
|
+
this.rpcUrl = rpcUrl;
|
|
36
|
+
this.pubUrl = pubUrl;
|
|
27
37
|
this.sub = new jszmq_1.Sub();
|
|
28
38
|
this.rpc = new jszmq_1.Req();
|
|
29
39
|
this.pub = new jszmq_1.Pub();
|
|
30
|
-
this.sub.subscribe("");
|
|
31
|
-
this.sub.connect(SUB_URL);
|
|
32
|
-
this.rpc.connect(RPC_URL);
|
|
33
|
-
this.pub.connect(PUB_URL);
|
|
34
40
|
// @ts-ignore
|
|
35
41
|
this.sub.on("message", (topic, msg) => {
|
|
36
42
|
const { key, data } = schema_1.responseSchema.parse({ key: topic, data: msg });
|
|
@@ -43,6 +49,45 @@ class BlueyeClient extends strict_event_emitter_1.Emitter {
|
|
|
43
49
|
this.logger.verbose("[sub] message:", key, message);
|
|
44
50
|
this.emit(key, message);
|
|
45
51
|
});
|
|
52
|
+
if (autoConnect) {
|
|
53
|
+
this.connect();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
updateState(newState) {
|
|
57
|
+
this.state = newState;
|
|
58
|
+
this.logger.info(`[client] ${newState}`);
|
|
59
|
+
this.emit(newState);
|
|
60
|
+
}
|
|
61
|
+
connect() {
|
|
62
|
+
if (this.state === "connected") {
|
|
63
|
+
this.logger.warn("[client] already connected");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (this.state === "connecting") {
|
|
67
|
+
this.logger.warn("[client] already connecting");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.updateState("connecting");
|
|
71
|
+
this.sub.subscribe("");
|
|
72
|
+
this.sub.connect(this.subUrl);
|
|
73
|
+
this.rpc.connect(this.rpcUrl);
|
|
74
|
+
this.pub.connect(this.pubUrl);
|
|
75
|
+
this.updateState("connected");
|
|
76
|
+
}
|
|
77
|
+
disconnect() {
|
|
78
|
+
if (this.state === "disconnected") {
|
|
79
|
+
this.logger.warn("[client] already disconnected");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (this.state === "connecting") {
|
|
83
|
+
this.logger.warn("[client] cannot disconnect while connecting");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.sub.unsubscribe("");
|
|
87
|
+
this.sub.disconnect(this.subUrl);
|
|
88
|
+
this.rpc.disconnect(this.rpcUrl);
|
|
89
|
+
this.pub.disconnect(this.pubUrl);
|
|
90
|
+
this.updateState("disconnected");
|
|
46
91
|
}
|
|
47
92
|
async sendRequest(req, opts = {}) {
|
|
48
93
|
if (!(0, exports.isInProtocol)(req) || !req.endsWith("Req")) {
|
|
@@ -53,13 +98,16 @@ class BlueyeClient extends strict_event_emitter_1.Emitter {
|
|
|
53
98
|
const encoded = protocol.encode(message).finish();
|
|
54
99
|
const { key, data } = await Promise.race([
|
|
55
100
|
new Promise((_, reject) => setTimeout(() => reject(new Error("[rpc] request timed out")), this.timeout)),
|
|
56
|
-
new Promise(resolve => {
|
|
101
|
+
new Promise((resolve) => {
|
|
57
102
|
// @ts-ignore
|
|
58
103
|
this.rpc.once("message", (topic, msg) => {
|
|
59
104
|
resolve({ key: topic.toString().split(".").at(-1), data: msg });
|
|
60
105
|
});
|
|
61
|
-
this.rpc.send([
|
|
62
|
-
|
|
106
|
+
this.rpc.send([
|
|
107
|
+
buffer_1.Buffer.from(`blueye.protocol.${req}`),
|
|
108
|
+
buffer_1.Buffer.from(encoded),
|
|
109
|
+
]);
|
|
110
|
+
}),
|
|
63
111
|
]);
|
|
64
112
|
if (key === "Empty") {
|
|
65
113
|
return null;
|
|
@@ -73,7 +121,9 @@ class BlueyeClient extends strict_event_emitter_1.Emitter {
|
|
|
73
121
|
return result;
|
|
74
122
|
}
|
|
75
123
|
async getTelemetry(type) {
|
|
76
|
-
const response = await this.sendRequest("GetTelemetryReq", {
|
|
124
|
+
const response = await this.sendRequest("GetTelemetryReq", {
|
|
125
|
+
messageType: type,
|
|
126
|
+
});
|
|
77
127
|
const { payload } = schema_1.telemetrySchema.parse(response);
|
|
78
128
|
const { typeUrl, value } = payload;
|
|
79
129
|
if (!(0, exports.isInProtocol)(typeUrl) || !typeUrl.endsWith("Tel")) {
|
|
@@ -91,7 +141,10 @@ class BlueyeClient extends strict_event_emitter_1.Emitter {
|
|
|
91
141
|
const message = protocol.create(opts);
|
|
92
142
|
const encoded = protocol.encode(message).finish();
|
|
93
143
|
this.logger.debug("[pub] sending control:", ctrl, message);
|
|
94
|
-
this.pub.send([
|
|
144
|
+
this.pub.send([
|
|
145
|
+
buffer_1.Buffer.from(`blueye.protocol.${ctrl}`),
|
|
146
|
+
buffer_1.Buffer.from(encoded),
|
|
147
|
+
]);
|
|
95
148
|
}
|
|
96
149
|
}
|
|
97
150
|
exports.BlueyeClient = BlueyeClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blueyerobotics/blueye-ts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "A TypeScript client for interacting with Blueye underwater drones.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"author": "Blueye <contact@blueye.no> (https://blueye.no/)",
|
|
20
20
|
"license": "LGPL-3.0-only",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@blueyerobotics/protocol-definitions": "3.2.0-
|
|
22
|
+
"@blueyerobotics/protocol-definitions": "3.2.0-b032513b",
|
|
23
23
|
"buffer": "^6.0.3",
|
|
24
24
|
"consola": "^3.4.2",
|
|
25
25
|
"jszmq": "^0.1.2",
|
|
@@ -27,6 +27,6 @@
|
|
|
27
27
|
"zod": "^3.25.67"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"typescript": "^5.
|
|
30
|
+
"typescript": "^5.9.2"
|
|
31
31
|
}
|
|
32
32
|
}
|