Skip to content

Commit fb4ed65

Browse files
Fix commands matching by name and returning deleted commands (#211)
1 parent 7eb909b commit fb4ed65

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

packages/data-sdk/src/devices/Device.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
import { RtcClient, IRtcPeer } from "@formant/realtime-sdk";
1+
import { IRtcPeer, RtcClient } from "@formant/realtime-sdk";
2+
import { defined } from "../../../common/defined";
3+
import { delay } from "../../../common/delay";
4+
import { createDevice } from "../api/createDevice";
5+
import { createShareLink } from "../api/createShareLink";
6+
import { disableDevice } from "../api/disableDevice";
7+
import { eventsCounter } from "../api/eventsCounter";
8+
import { getAnnotationCount } from "../api/getAnnotationCount";
9+
import { getAnnotationCountByIntervals } from "../api/getAnnotationCountByIntervals";
10+
import { getDevicesData } from "../api/getDevicesData";
11+
import { getPeers } from "../api/getPeers";
12+
import { getRealtimeSessions } from "../api/getRealtimeSessions";
13+
import { getTelemetry } from "../api/getTelemetry";
14+
import { patchDevice } from "../api/patchDevice";
15+
import { queryDevicesData } from "../api/queryDevicesData";
16+
import { queryEvents } from "../api/queryEvents";
217
import { getRtcClientPool } from "../AppRtcClientPools";
318
import { Authentication } from "../Authentication";
419
import { CaptureStream } from "../CaptureStream";
520
import { FORMANT_API_URL } from "../config";
6-
import { delay } from "../../../common/delay";
7-
import { defined } from "../../../common/defined";
8-
import { InterventionType } from "../model/InterventionType";
9-
import { IInterventionTypeMap } from "../model/IInterventionTypeMap";
10-
import { IInterventionResponse } from "../model/IInterventionResponse";
11-
import { IEventQuery } from "../model/IEventQuery";
1221
import { AggregateLevel } from "../model/AggregateLevel";
1322
import { EventType } from "../model/EventType";
23+
import { IEvent } from "../model/IEvent";
24+
import { IEventQuery } from "../model/IEventQuery";
25+
import { IInterventionResponse } from "../model/IInterventionResponse";
26+
import { IInterventionTypeMap } from "../model/IInterventionTypeMap";
27+
import { InterventionType } from "../model/InterventionType";
1428
import { IShare } from "../model/IShare";
29+
import { ITags } from "../model/ITags";
1530
import { SessionType } from "../model/SessionType";
31+
import { TelemetryResult } from "../model/TelemetryResult";
1632
import { isRtcPeer } from "../utils/isRtcPeer";
33+
import { BaseDevice } from "./BaseDevice";
1734
import {
1835
Command,
1936
ConfigurationDocument,
2037
IStartRealtimeConnectionOptions,
2138
TelemetryStream,
2239
} from "./device.types";
23-
import { BaseDevice } from "./BaseDevice";
24-
import { ITags } from "../model/ITags";
25-
import { createShareLink } from "../api/createShareLink";
26-
import { eventsCounter } from "../api/eventsCounter";
27-
import { getAnnotationCount } from "../api/getAnnotationCount";
28-
import { getAnnotationCountByIntervals } from "../api/getAnnotationCountByIntervals";
29-
import { getTelemetry } from "../api/getTelemetry";
30-
import { getRealtimeSessions } from "../api/getRealtimeSessions";
31-
import { getPeers } from "../api/getPeers";
32-
import { createDevice } from "../api/createDevice";
33-
import { patchDevice } from "../api/patchDevice";
34-
import { getDevicesData } from "../api/getDevicesData";
35-
import { queryDevicesData } from "../api/queryDevicesData";
36-
import { disableDevice } from "../api/disableDevice";
37-
import { TelemetryResult } from "../model/TelemetryResult";
38-
import { IEvent } from "../model/IEvent";
39-
import { queryEvents } from "../api/queryEvents";
4040
export class Device extends BaseDevice {
4141
constructor(
4242
public id: string,
@@ -362,7 +362,7 @@ export class Device extends BaseDevice {
362362
return false;
363363
}
364364

365-
async getAvailableCommands(): Promise<Command[]> {
365+
async getAvailableCommands(includeDisabled=true): Promise<Command[]> {
366366
const result = await fetch(
367367
`${FORMANT_API_URL}/v1/admin/command-templates/`,
368368
{
@@ -384,17 +384,28 @@ export class Device extends BaseDevice {
384384
parameterMeta: i.parameterMeta,
385385
enabled: i.enabled,
386386
tags: i.tags,
387-
}));
387+
})).filter((i: any) => {
388+
if (includeDisabled) {
389+
return true;
390+
}
391+
return i.enabled;
392+
});
388393
}
389394

390395
async sendCommand(
391396
name: string,
392397
data?: string,
393398
time?: Date,
394-
metadata?: {}
399+
metadata?: {},
400+
id?: string
395401
): Promise<Response> {
396-
const commands = await this.getAvailableCommands();
397-
const command = commands.find((_) => _.name === name);
402+
const commands = await this.getAvailableCommands(false);
403+
const command = commands.find((_) => {
404+
if (id) {
405+
return _.id === id
406+
}
407+
return _.name === name
408+
});
398409
if (!command) {
399410
throw new Error(`Could not find command with name "${name}"`);
400411
}

0 commit comments

Comments
 (0)