@blueyerobotics/blueye-ts 2.0.0 → 3.0.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 CHANGED
@@ -15,19 +15,23 @@ import { BlueyeClient } from "@blueyerobotics/blueye-ts";
15
15
 
16
16
  const client = new BlueyeClient();
17
17
 
18
- // request battery information
19
- const batteryRep = await client.sendRequest("GetBatteryReq");
20
- console.log("batteryRep:", batteryRep);
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 turn on the lights
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
- // request battery information
7
- const batteryRep = await client.sendRequest("GetBatteryReq");
8
- console.log("batteryRep:", batteryRep);
9
- // get latest battery telemetry
10
- const batteryTel = await client.getTelemetry("BatteryTel");
11
- console.log("batteryTel:", batteryTel);
12
- // send a control message to turn on the lights
13
- await client.sendControl("LightsCtrl", { lights: { value: 1 } });
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
17
  client.on("BatteryTel", data => {
16
18
  console.log("received BatteryTel:", data);
17
19
  });
20
+ client.connect();
18
21
  };
19
22
  main();
@@ -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?: number, logLevel?: LogLevel);
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 {};
@@ -7,30 +7,33 @@ 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 SUB_URL = "ws://192.168.1.101:9985";
11
- const RPC_URL = "ws://192.168.1.101:9986";
12
- const PUB_URL = "ws://192.168.1.101:9987";
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
30
  this.logger = (0, consola_1.createConsola)({ level: logLevel, formatOptions: { colors: true, compact: false } });
31
+ this.subUrl = subUrl;
32
+ this.rpcUrl = rpcUrl;
33
+ this.pubUrl = pubUrl;
27
34
  this.sub = new jszmq_1.Sub();
28
35
  this.rpc = new jszmq_1.Req();
29
36
  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
37
  // @ts-ignore
35
38
  this.sub.on("message", (topic, msg) => {
36
39
  const { key, data } = schema_1.responseSchema.parse({ key: topic, data: msg });
@@ -43,6 +46,45 @@ class BlueyeClient extends strict_event_emitter_1.Emitter {
43
46
  this.logger.verbose("[sub] message:", key, message);
44
47
  this.emit(key, message);
45
48
  });
49
+ if (autoConnect) {
50
+ this.connect();
51
+ }
52
+ }
53
+ updateState(newState) {
54
+ this.state = newState;
55
+ this.logger.info(`[client] ${newState}`);
56
+ this.emit(newState);
57
+ }
58
+ connect() {
59
+ if (this.state === "connected") {
60
+ this.logger.warn("[client] already connected");
61
+ return;
62
+ }
63
+ if (this.state === "connecting") {
64
+ this.logger.warn("[client] already connecting");
65
+ return;
66
+ }
67
+ this.updateState("connecting");
68
+ this.sub.subscribe("");
69
+ this.sub.connect(this.subUrl);
70
+ this.rpc.connect(this.rpcUrl);
71
+ this.pub.connect(this.pubUrl);
72
+ this.updateState("connected");
73
+ }
74
+ disconnect() {
75
+ if (this.state === "disconnected") {
76
+ this.logger.warn("[client] already disconnected");
77
+ return;
78
+ }
79
+ if (this.state === "connecting") {
80
+ this.logger.warn("[client] cannot disconnect while connecting");
81
+ return;
82
+ }
83
+ this.sub.unsubscribe("");
84
+ this.sub.disconnect(this.subUrl);
85
+ this.rpc.disconnect(this.rpcUrl);
86
+ this.pub.disconnect(this.pubUrl);
87
+ this.updateState("disconnected");
46
88
  }
47
89
  async sendRequest(req, opts = {}) {
48
90
  if (!(0, exports.isInProtocol)(req) || !req.endsWith("Req")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueyerobotics/blueye-ts",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "A TypeScript client for interacting with Blueye underwater drones.",
5
5
  "repository": {
6
6
  "type": "git",