@dronedeploy/rocos-js-sdk 4.5.0 → 4.5.2
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/cjs/constants/api.d.ts +2 -0
- package/cjs/constants/api.js +4 -2
- package/cjs/models/maps/MapContent.d.ts +9 -0
- package/cjs/services/BaseServiceAbstract.d.ts +5 -0
- package/cjs/services/BaseServiceAbstract.js +9 -0
- package/cjs/services/MapService.d.ts +28 -2
- package/cjs/services/MapService.js +37 -0
- package/cjs/services/RobotStatusService.d.ts +22 -3
- package/cjs/services/RobotStatusService.js +79 -13
- package/cjs/services/index.d.ts +1 -0
- package/cjs/services/robot-status/connectivity.d.ts +19 -30
- package/cjs/services/robot-status/connectivity.js +16 -58
- package/cjs/services/robot-status/reportedStatus.d.ts +11 -4
- package/cjs/services/robot-status/reportedStatus.js +14 -9
- package/cjs/services/robot-status/sharedStreams.d.ts +28 -0
- package/cjs/services/robot-status/sharedStreams.js +52 -0
- package/esm/constants/api.d.ts +2 -0
- package/esm/constants/api.js +2 -0
- package/esm/models/maps/MapContent.d.ts +9 -0
- package/esm/services/BaseServiceAbstract.d.ts +5 -0
- package/esm/services/BaseServiceAbstract.js +9 -0
- package/esm/services/MapService.d.ts +28 -2
- package/esm/services/MapService.js +39 -2
- package/esm/services/RobotStatusService.d.ts +22 -3
- package/esm/services/RobotStatusService.js +82 -16
- package/esm/services/index.d.ts +1 -0
- package/esm/services/robot-status/connectivity.d.ts +19 -30
- package/esm/services/robot-status/connectivity.js +14 -56
- package/esm/services/robot-status/reportedStatus.d.ts +11 -4
- package/esm/services/robot-status/reportedStatus.js +12 -7
- package/esm/services/robot-status/sharedStreams.d.ts +28 -0
- package/esm/services/robot-status/sharedStreams.js +48 -0
- package/package.json +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { API_SERVICE_CONNECTIONS_URL, API_SERVICE_CONNECTION_URL } from '../../constants/api';
|
|
2
2
|
import { ConnectionStatus, RobotConnectivity, ServiceConnection, } from '../../models';
|
|
3
|
-
import { catchError, combineLatest, distinctUntilChanged, from, map, of, startWith, switchMap, timer, } from 'rxjs';
|
|
4
3
|
import { formatServiceUrl } from '../../helpers/formatServiceUrl';
|
|
5
4
|
/**
|
|
6
5
|
* Service id → public path prefix, for services that expose the standard
|
|
@@ -15,8 +14,7 @@ export const CONNECTION_SERVICE_PREFIXES = {
|
|
|
15
14
|
};
|
|
16
15
|
/**
|
|
17
16
|
* The telemetry-liveness signal is derived from heartbeat freshness rather than
|
|
18
|
-
* an HTTP endpoint, so it is handled specially
|
|
19
|
-
* the caller). See `getTelemetryLiveness`.
|
|
17
|
+
* an HTTP endpoint, so it is handled specially. See `getTelemetryLiveness`.
|
|
20
18
|
*/
|
|
21
19
|
export const TELEMETRY_LIVENESS_SERVICE = 'telemetry-liveness';
|
|
22
20
|
/** The services evaluated by default. */
|
|
@@ -26,7 +24,8 @@ export const DEFAULT_EXPECTED_SERVICES = [
|
|
|
26
24
|
'robot-services',
|
|
27
25
|
'robot-telemetry',
|
|
28
26
|
];
|
|
29
|
-
|
|
27
|
+
/** How often the bulk `connections` endpoints are polled by default (ms). */
|
|
28
|
+
export const DEFAULT_CONNECTIVITY_POLL_MS = 5000;
|
|
30
29
|
/**
|
|
31
30
|
* Floor for the connections-endpoint poll interval. Guards against a caller
|
|
32
31
|
* passing `0` (or any tiny value), which would otherwise poll the API on every
|
|
@@ -83,20 +82,19 @@ export const aggregateConnectivity = (services) => {
|
|
|
83
82
|
return RobotConnectivity.OFFLINE;
|
|
84
83
|
};
|
|
85
84
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* degraded flash on a transient error.
|
|
90
|
-
*
|
|
91
|
-
* `pollMs` is floored at {@link MIN_CONNECTIVITY_POLL_MS} so the endpoint can't
|
|
92
|
-
* be polled unreasonably fast.
|
|
85
|
+
* One callsign's {@link ServiceConnection} from a bulk poll. A failed poll is `UNKNOWN` (held
|
|
86
|
+
* transitional) rather than `DISCONNECTED`, to avoid a spurious degraded flash on a transient error;
|
|
87
|
+
* a callsign missing from a successful poll is `DISCONNECTED`.
|
|
93
88
|
*/
|
|
94
|
-
export const
|
|
95
|
-
|
|
96
|
-
return
|
|
97
|
-
|
|
89
|
+
export const getCallsignConnection = (poll, callsign) => {
|
|
90
|
+
if (poll === undefined)
|
|
91
|
+
return ServiceConnection.UNKNOWN;
|
|
92
|
+
if (!Object.prototype.hasOwnProperty.call(poll, callsign))
|
|
93
|
+
return ServiceConnection.DISCONNECTED;
|
|
94
|
+
return mapConnectionStatus(poll[callsign]);
|
|
98
95
|
};
|
|
99
|
-
|
|
96
|
+
/** Whether two connectivity statuses carry the same verdict and per-service breakdown. */
|
|
97
|
+
export const connectivityEquals = (a, b) => {
|
|
100
98
|
if (a.overall !== b.overall)
|
|
101
99
|
return false;
|
|
102
100
|
const keys = Object.keys(a.services);
|
|
@@ -104,43 +102,3 @@ const connectivityEquals = (a, b) => {
|
|
|
104
102
|
return false;
|
|
105
103
|
return keys.every((key) => a.services[key] === b.services[key]);
|
|
106
104
|
};
|
|
107
|
-
/**
|
|
108
|
-
* Emits the robot's {@link RobotConnectivityStatus} — the per-service breakdown
|
|
109
|
-
* plus the aggregate verdict — for the configured `expectedServices`, plus any
|
|
110
|
-
* caller-injected `extraServices` (folded in on equal footing). HTTP services
|
|
111
|
-
* are polled; `telemetry-liveness` and injected services use their supplied
|
|
112
|
-
* observable; any unrecognised service id resolves to `UNKNOWN`.
|
|
113
|
-
*/
|
|
114
|
-
export const getConnectivityChanges = (deps, projectId, callsign, expectedServices = DEFAULT_EXPECTED_SERVICES, pollMs = DEFAULT_CONNECTIVITY_POLL_MS) => {
|
|
115
|
-
const extraServices = deps.extraServices ?? {};
|
|
116
|
-
const serviceIds = [...expectedServices, ...Object.keys(extraServices).filter((id) => !expectedServices.includes(id))];
|
|
117
|
-
// combineLatest([]) completes without emitting; emit an explicit empty/offline status instead.
|
|
118
|
-
if (serviceIds.length === 0) {
|
|
119
|
-
return of({ overall: RobotConnectivity.OFFLINE, services: {} });
|
|
120
|
-
}
|
|
121
|
-
const perService = serviceIds.map((serviceId) => {
|
|
122
|
-
let source$;
|
|
123
|
-
if (serviceId === TELEMETRY_LIVENESS_SERVICE) {
|
|
124
|
-
source$ = deps.telemetryLiveness$;
|
|
125
|
-
}
|
|
126
|
-
else if (Object.prototype.hasOwnProperty.call(CONNECTION_SERVICE_PREFIXES, serviceId)) {
|
|
127
|
-
const url = buildConnectionsUrl(serviceId, deps.baseUrl, projectId, callsign, deps.insecure);
|
|
128
|
-
source$ = getServiceConnectionChanges(deps.httpGet, url, pollMs);
|
|
129
|
-
}
|
|
130
|
-
else if (Object.prototype.hasOwnProperty.call(extraServices, serviceId)) {
|
|
131
|
-
// Contain an injected observable's errors as UNKNOWN, mirroring the built-in sources, so a
|
|
132
|
-
// faulty caller signal can't error out combineLatest and tear down the whole aggregate.
|
|
133
|
-
source$ = extraServices[serviceId].pipe(catchError(() => of(ServiceConnection.UNKNOWN)));
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
source$ = of(ServiceConnection.UNKNOWN);
|
|
137
|
-
}
|
|
138
|
-
return source$.pipe(startWith(ServiceConnection.UNKNOWN), map((connection) => ({ serviceId, connection })));
|
|
139
|
-
});
|
|
140
|
-
return combineLatest(perService).pipe(map((entries) => {
|
|
141
|
-
const services = {};
|
|
142
|
-
for (const { serviceId, connection } of entries)
|
|
143
|
-
services[serviceId] = connection;
|
|
144
|
-
return { overall: aggregateConnectivity(services), services };
|
|
145
|
-
}), distinctUntilChanged(connectivityEquals));
|
|
146
|
-
};
|
|
@@ -7,6 +7,13 @@ export declare const ROBOT_STATUS_SOURCE = "/diagnostics/robot/status";
|
|
|
7
7
|
export declare const LEGACY_HEARTBEAT_SOURCE = "/rocos/agent/telemetry/heartbeat";
|
|
8
8
|
/** The per-contributor breakdown behind the reported status (health + readiness). */
|
|
9
9
|
export declare const ROBOT_STATUS_CONTRIBUTORS_SOURCE = "/diagnostics/robot/status/contributors";
|
|
10
|
+
/** How long without a liveness message before `telemetry-liveness` reports `DISCONNECTED` (ms). */
|
|
11
|
+
export declare const DEFAULT_HEARTBEAT_TIMEOUT_MS = 5000;
|
|
12
|
+
/**
|
|
13
|
+
* The telemetry these streams subscribe through. `RobotStatusService` passes a pooled implementation,
|
|
14
|
+
* so every status stream for a robot shares one subscription per topic.
|
|
15
|
+
*/
|
|
16
|
+
export type StatusTelemetry = Pick<TelemetryService, 'subscribe'>;
|
|
10
17
|
export interface ReportedRobotStatus {
|
|
11
18
|
health: RobotHealth;
|
|
12
19
|
readiness: RobotReadiness;
|
|
@@ -29,7 +36,7 @@ export declare const parseRobotReadiness: (value: unknown) => RobotReadiness;
|
|
|
29
36
|
* preserves today's "online ⇒ green" behaviour for agents not yet emitting the
|
|
30
37
|
* new topic.
|
|
31
38
|
*/
|
|
32
|
-
export declare const getReportedStatusChanges: (telemetry:
|
|
39
|
+
export declare const getReportedStatusChanges: (telemetry: StatusTelemetry, projectId: string, callsign: string) => Observable<ReportedRobotStatus>;
|
|
33
40
|
/**
|
|
34
41
|
* Emits the per-contributor breakdown behind the robot's reported health and
|
|
35
42
|
* readiness, from the agent's `robot/status/contributors` topic. Each
|
|
@@ -40,7 +47,7 @@ export declare const getReportedStatusChanges: (telemetry: TelemetryService, pro
|
|
|
40
47
|
* emit immediately, and — like {@link getReportedStatusChanges} — a telemetry
|
|
41
48
|
* error falls back to an empty breakdown rather than terminating the stream.
|
|
42
49
|
*/
|
|
43
|
-
export declare const getStatusContributorsChanges: (telemetry:
|
|
50
|
+
export declare const getStatusContributorsChanges: (telemetry: StatusTelemetry, projectId: string, callsign: string) => Observable<RobotStatusContributors>;
|
|
44
51
|
/**
|
|
45
52
|
* Emits the robot's telemetry latency in milliseconds, once per liveness message (from either the
|
|
46
53
|
* new `robot/status` topic or the legacy heartbeat — the same messages {@link getTelemetryLiveness}
|
|
@@ -52,7 +59,7 @@ export declare const getStatusContributorsChanges: (telemetry: TelemetryService,
|
|
|
52
59
|
* {@link getTelemetryLiveness} to tell a stale value from a live one. A telemetry error completes
|
|
53
60
|
* the stream rather than erroring it.
|
|
54
61
|
*/
|
|
55
|
-
export declare const getTelemetryLatencyChanges: (telemetry:
|
|
62
|
+
export declare const getTelemetryLatencyChanges: (telemetry: StatusTelemetry, projectId: string, callsign: string, now: () => number) => Observable<number>;
|
|
56
63
|
/**
|
|
57
64
|
* Emits the `telemetry-liveness` connectivity signal: whether a heartbeat (from
|
|
58
65
|
* either the new `robot/status` topic or the legacy heartbeat) has been received
|
|
@@ -60,4 +67,4 @@ export declare const getTelemetryLatencyChanges: (telemetry: TelemetryService, p
|
|
|
60
67
|
* but spans both topics and maps onto {@link ServiceConnection} so it can be
|
|
61
68
|
* folded into connectivity aggregation as just another service entry.
|
|
62
69
|
*/
|
|
63
|
-
export declare const getTelemetryLiveness: (telemetry:
|
|
70
|
+
export declare const getTelemetryLiveness: (telemetry: StatusTelemetry, projectId: string, callsign: string, heartbeatTimeoutMs?: number, intervalMs?: number) => Observable<ServiceConnection>;
|
|
@@ -6,8 +6,17 @@ export const ROBOT_STATUS_SOURCE = '/diagnostics/robot/status';
|
|
|
6
6
|
export const LEGACY_HEARTBEAT_SOURCE = '/rocos/agent/telemetry/heartbeat';
|
|
7
7
|
/** The per-contributor breakdown behind the reported status (health + readiness). */
|
|
8
8
|
export const ROBOT_STATUS_CONTRIBUTORS_SOURCE = `${ROBOT_STATUS_SOURCE}/contributors`;
|
|
9
|
-
|
|
9
|
+
/** How long without a liveness message before `telemetry-liveness` reports `DISCONNECTED` (ms). */
|
|
10
|
+
export const DEFAULT_HEARTBEAT_TIMEOUT_MS = 5000;
|
|
10
11
|
const DEFAULT_INTERVAL_MS = 2000;
|
|
12
|
+
/**
|
|
13
|
+
* Every liveness message — the new `robot/status` topic or the legacy heartbeat — as one stream. The
|
|
14
|
+
* topics are subscribed separately, so the subscriptions are the same ones
|
|
15
|
+
* {@link getReportedStatusChanges} makes and can be shared with it.
|
|
16
|
+
*/
|
|
17
|
+
const getLivenessMessages = (telemetry, projectId, callsign) => {
|
|
18
|
+
return merge(telemetry.subscribe({ projectId, callsigns: [callsign], sources: [ROBOT_STATUS_SOURCE] }), telemetry.subscribe({ projectId, callsigns: [callsign], sources: [LEGACY_HEARTBEAT_SOURCE] }));
|
|
19
|
+
};
|
|
11
20
|
/**
|
|
12
21
|
* Maps a `robot/status` wire value (integer, or its string equivalent) to a
|
|
13
22
|
* {@link RobotHealth}. Anything unrecognised — including `0` / `'unknown'` —
|
|
@@ -124,9 +133,7 @@ export const getStatusContributorsChanges = (telemetry, projectId, callsign) =>
|
|
|
124
133
|
* the stream rather than erroring it.
|
|
125
134
|
*/
|
|
126
135
|
export const getTelemetryLatencyChanges = (telemetry, projectId, callsign, now) => {
|
|
127
|
-
return telemetry
|
|
128
|
-
.subscribe({ projectId, callsigns: [callsign], sources: [ROBOT_STATUS_SOURCE, LEGACY_HEARTBEAT_SOURCE] })
|
|
129
|
-
.pipe(map((message) => now() - message.createdAt.getTime()), catchError(() => EMPTY));
|
|
136
|
+
return getLivenessMessages(telemetry, projectId, callsign).pipe(map((message) => now() - message.createdAt.getTime()), catchError(() => EMPTY));
|
|
130
137
|
};
|
|
131
138
|
/**
|
|
132
139
|
* Emits the `telemetry-liveness` connectivity signal: whether a heartbeat (from
|
|
@@ -137,9 +144,7 @@ export const getTelemetryLatencyChanges = (telemetry, projectId, callsign, now)
|
|
|
137
144
|
*/
|
|
138
145
|
export const getTelemetryLiveness = (telemetry, projectId, callsign, heartbeatTimeoutMs = DEFAULT_HEARTBEAT_TIMEOUT_MS, intervalMs = DEFAULT_INTERVAL_MS) => {
|
|
139
146
|
const startedAt = Date.now();
|
|
140
|
-
const lastMessageAt$ = telemetry
|
|
141
|
-
.subscribe({ projectId, callsigns: [callsign], sources: [ROBOT_STATUS_SOURCE, LEGACY_HEARTBEAT_SOURCE] })
|
|
142
|
-
.pipe(map(() => Date.now()));
|
|
147
|
+
const lastMessageAt$ = getLivenessMessages(telemetry, projectId, callsign).pipe(map(() => Date.now()));
|
|
143
148
|
return combineLatest([lastMessageAt$.pipe(startWith(startedAt)), interval(intervalMs)]).pipe(map(([lastMessageAt]) => {
|
|
144
149
|
const now = Date.now();
|
|
145
150
|
// No message yet, but still within the initial grace period.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
export interface SharedStreamOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Replay the latest value to a subscriber that joins late. Use it for state (a status, a poll
|
|
5
|
+
* result); leave it off for events (raw telemetry messages), where a replayed message would be
|
|
6
|
+
* mistaken for a fresh one.
|
|
7
|
+
*/
|
|
8
|
+
replay: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hands out one shared, reference-counted observable per key, so equivalent subscribers (e.g. every
|
|
12
|
+
* status dot for robots in one project) share a single upstream subscription or poll.
|
|
13
|
+
*
|
|
14
|
+
* The upstream is subscribed on the first subscriber and torn down after the last one leaves, at
|
|
15
|
+
* which point the entry is dropped; a later subscriber starts it afresh. An upstream that errors or
|
|
16
|
+
* completes is reset the same way, so one failure never leaves a dead stream in the pool.
|
|
17
|
+
*/
|
|
18
|
+
export declare class SharedStreamPool {
|
|
19
|
+
private readonly streams;
|
|
20
|
+
get<T>(key: unknown[], create: () => Observable<T>, options: SharedStreamOptions): Observable<T>;
|
|
21
|
+
/** How many streams are currently pooled (subscribed and live). */
|
|
22
|
+
get size(): number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The process-wide pool, shared across `RobotStatusService` instances. Keys include the SDK's unique
|
|
26
|
+
* config key, so only equivalent SDK configurations share streams.
|
|
27
|
+
*/
|
|
28
|
+
export declare const sharedStreamPool: SharedStreamPool;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ReplaySubject, Subject, defer, finalize, share } from 'rxjs';
|
|
2
|
+
/**
|
|
3
|
+
* Hands out one shared, reference-counted observable per key, so equivalent subscribers (e.g. every
|
|
4
|
+
* status dot for robots in one project) share a single upstream subscription or poll.
|
|
5
|
+
*
|
|
6
|
+
* The upstream is subscribed on the first subscriber and torn down after the last one leaves, at
|
|
7
|
+
* which point the entry is dropped; a later subscriber starts it afresh. An upstream that errors or
|
|
8
|
+
* completes is reset the same way, so one failure never leaves a dead stream in the pool.
|
|
9
|
+
*/
|
|
10
|
+
export class SharedStreamPool {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.streams = new Map();
|
|
13
|
+
}
|
|
14
|
+
get(key, create, options) {
|
|
15
|
+
const id = JSON.stringify(key);
|
|
16
|
+
const existing = this.streams.get(id);
|
|
17
|
+
if (existing)
|
|
18
|
+
return existing;
|
|
19
|
+
// Registered only once subscribed, so a stream handed out but never subscribed leaves no entry
|
|
20
|
+
// behind. Until then (or after the pool dropped it, for a caller that held on to it and
|
|
21
|
+
// resubscribes) another stream may have taken the key: join that rather than run a second upstream.
|
|
22
|
+
const shared$ = defer(() => {
|
|
23
|
+
const current = this.streams.get(id);
|
|
24
|
+
if (current && current !== shared$)
|
|
25
|
+
return current;
|
|
26
|
+
this.streams.set(id, shared$);
|
|
27
|
+
return create();
|
|
28
|
+
}).pipe(finalize(() => {
|
|
29
|
+
if (this.streams.get(id) === shared$)
|
|
30
|
+
this.streams.delete(id);
|
|
31
|
+
}), share({
|
|
32
|
+
connector: () => (options.replay ? new ReplaySubject(1) : new Subject()),
|
|
33
|
+
resetOnError: true,
|
|
34
|
+
resetOnComplete: true,
|
|
35
|
+
resetOnRefCountZero: true,
|
|
36
|
+
}));
|
|
37
|
+
return shared$;
|
|
38
|
+
}
|
|
39
|
+
/** How many streams are currently pooled (subscribed and live). */
|
|
40
|
+
get size() {
|
|
41
|
+
return this.streams.size;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The process-wide pool, shared across `RobotStatusService` instances. Keys include the SDK's unique
|
|
46
|
+
* config key, so only equivalent SDK configurations share streams.
|
|
47
|
+
*/
|
|
48
|
+
export const sharedStreamPool = new SharedStreamPool();
|