motion-master-client 0.0.3 → 0.0.6

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.
Files changed (118) hide show
  1. package/README.md +123 -49
  2. package/package.json +21 -6
  3. package/src/{index.ts → index.d.ts} +23 -22
  4. package/src/index.js +27 -0
  5. package/src/index.js.map +1 -0
  6. package/src/lib/cia402.d.ts +30 -0
  7. package/src/lib/cia402.js +112 -0
  8. package/src/lib/cia402.js.map +1 -0
  9. package/src/lib/device-log-line.d.ts +5 -0
  10. package/src/lib/device-log-line.js +3 -0
  11. package/src/lib/device-log-line.js.map +1 -0
  12. package/src/lib/{device-parameter.ts → device-parameter.d.ts} +56 -69
  13. package/src/lib/device-parameter.js +39 -0
  14. package/src/lib/device-parameter.js.map +1 -0
  15. package/src/lib/device.d.ts +8 -0
  16. package/src/lib/device.js +3 -0
  17. package/src/lib/device.js.map +1 -0
  18. package/src/lib/examples/get-firmware-version-for-all-devices.d.ts +1 -0
  19. package/src/lib/examples/get-firmware-version-for-all-devices.js +15 -0
  20. package/src/lib/examples/get-firmware-version-for-all-devices.js.map +1 -0
  21. package/src/lib/hardware-description.d.ts +37 -0
  22. package/src/lib/hardware-description.js +48 -0
  23. package/src/lib/hardware-description.js.map +1 -0
  24. package/src/lib/logger.d.ts +1 -0
  25. package/src/lib/logger.js +8 -0
  26. package/src/lib/logger.js.map +1 -0
  27. package/src/lib/monitoring-config.d.ts +6 -0
  28. package/src/lib/monitoring-config.js +3 -0
  29. package/src/lib/monitoring-config.js.map +1 -0
  30. package/src/lib/monitoring-entry.d.ts +9 -0
  31. package/src/lib/monitoring-entry.js +3 -0
  32. package/src/lib/monitoring-entry.js.map +1 -0
  33. package/src/lib/motion-master-client.d.ts +18 -0
  34. package/src/lib/motion-master-client.js +73 -0
  35. package/src/lib/motion-master-client.js.map +1 -0
  36. package/src/lib/motion-master-pub-sub-client.d.ts +16 -0
  37. package/src/lib/motion-master-pub-sub-client.js +68 -0
  38. package/src/lib/motion-master-pub-sub-client.js.map +1 -0
  39. package/src/lib/motion-master-pub-sub-socket.d.ts +28 -0
  40. package/src/lib/motion-master-pub-sub-socket.js +3 -0
  41. package/src/lib/motion-master-pub-sub-socket.js.map +1 -0
  42. package/src/lib/motion-master-pub-sub-web-socket.d.ts +13 -0
  43. package/src/lib/motion-master-pub-sub-web-socket.js +61 -0
  44. package/src/lib/motion-master-pub-sub-web-socket.js.map +1 -0
  45. package/src/lib/motion-master-pub-sub-worker-socket.d.ts +13 -0
  46. package/src/lib/motion-master-pub-sub-worker-socket.js +37 -0
  47. package/src/lib/motion-master-pub-sub-worker-socket.js.map +1 -0
  48. package/src/lib/motion-master-req-res-client.d.ts +956 -0
  49. package/src/lib/motion-master-req-res-client.js +1521 -0
  50. package/src/lib/motion-master-req-res-client.js.map +1 -0
  51. package/src/lib/motion-master-req-res-socket.d.ts +38 -0
  52. package/src/lib/motion-master-req-res-socket.js +3 -0
  53. package/src/lib/motion-master-req-res-socket.js.map +1 -0
  54. package/src/lib/motion-master-req-res-web-socket.d.ts +20 -0
  55. package/src/lib/motion-master-req-res-web-socket.js +100 -0
  56. package/src/lib/motion-master-req-res-web-socket.js.map +1 -0
  57. package/src/lib/motion-master-req-res-worker-socket.d.ts +15 -0
  58. package/src/lib/motion-master-req-res-worker-socket.js +53 -0
  59. package/src/lib/motion-master-req-res-worker-socket.js.map +1 -0
  60. package/src/lib/motion-master.proto.js +5 -3
  61. package/src/lib/operators.d.ts +21 -0
  62. package/src/lib/operators.js +71 -0
  63. package/src/lib/operators.js.map +1 -0
  64. package/src/lib/options.d.ts +34 -0
  65. package/src/lib/options.js +38 -0
  66. package/src/lib/options.js.map +1 -0
  67. package/src/lib/parameter.d.ts +65 -0
  68. package/src/lib/parameter.js +70 -0
  69. package/src/lib/parameter.js.map +1 -0
  70. package/src/lib/request-status-resolver.d.ts +4 -0
  71. package/src/lib/request-status-resolver.js +345 -0
  72. package/src/lib/request-status-resolver.js.map +1 -0
  73. package/src/lib/system-log-line.d.ts +8 -0
  74. package/src/lib/system-log-line.js +3 -0
  75. package/src/lib/system-log-line.js.map +1 -0
  76. package/src/lib/types.d.ts +50 -0
  77. package/src/lib/types.js +23 -0
  78. package/src/lib/types.js.map +1 -0
  79. package/src/lib/urls.d.ts +3 -0
  80. package/src/lib/urls.js +10 -0
  81. package/src/lib/urls.js.map +1 -0
  82. package/src/lib/util.d.ts +30 -0
  83. package/src/lib/util.js +279 -0
  84. package/src/lib/util.js.map +1 -0
  85. package/.babelrc +0 -3
  86. package/.eslintrc.json +0 -18
  87. package/jest.config.ts +0 -15
  88. package/motion-master.proto +0 -1822
  89. package/src/lib/device-log-line.ts +0 -5
  90. package/src/lib/device-parameter.spec.ts +0 -85
  91. package/src/lib/hardware-description.spec.ts +0 -223
  92. package/src/lib/hardware-description.ts +0 -79
  93. package/src/lib/logger.ts +0 -5
  94. package/src/lib/monitoring-config.ts +0 -6
  95. package/src/lib/monitoring-entry.ts +0 -11
  96. package/src/lib/motion-master-client.ts +0 -94
  97. package/src/lib/motion-master-pub-sub-client.ts +0 -94
  98. package/src/lib/motion-master-pub-sub-socket.ts +0 -33
  99. package/src/lib/motion-master-pub-sub-web-socket.ts +0 -71
  100. package/src/lib/motion-master-pub-sub-worker-socket.ts +0 -58
  101. package/src/lib/motion-master-req-res-client.spec.ts +0 -126
  102. package/src/lib/motion-master-req-res-client.ts +0 -1990
  103. package/src/lib/motion-master-req-res-socket.ts +0 -45
  104. package/src/lib/motion-master-req-res-web-socket.ts +0 -119
  105. package/src/lib/motion-master-req-res-worker-socket.ts +0 -63
  106. package/src/lib/operators.ts +0 -93
  107. package/src/lib/options.ts +0 -12
  108. package/src/lib/parameter.spec.ts +0 -81
  109. package/src/lib/parameter.ts +0 -119
  110. package/src/lib/request-status-resolver.ts +0 -380
  111. package/src/lib/system-log-line.ts +0 -8
  112. package/src/lib/types.ts +0 -58
  113. package/src/lib/urls.ts +0 -6
  114. package/src/lib/util.ts +0 -261
  115. package/tsconfig.json +0 -22
  116. package/tsconfig.lib.json +0 -10
  117. package/tsconfig.spec.json +0 -20
  118. package/typedoc.json +0 -10
@@ -1,45 +0,0 @@
1
- import { BehaviorSubject, Observable } from "rxjs";
2
- import { MotionMasterMessage } from "./types";
3
-
4
- export interface MotionMasterReqResSocket {
5
- /**
6
- * Emits a boolean value when socket gets opened or closed.
7
- */
8
- readonly opened$: BehaviorSubject<boolean>;
9
-
10
- /**
11
- * Motion Master is considered alive when it sends messages in regular time interval.
12
- */
13
- readonly alive$: BehaviorSubject<boolean>;
14
-
15
- /**
16
- * Decoded message instances coming from Motion Master.
17
- */
18
- readonly message$: Observable<MotionMasterMessage>;
19
-
20
- /**
21
- * URL set in call to open.
22
- */
23
- get url(): string | undefined;
24
-
25
- /**
26
- * Open socket.
27
- *
28
- * The connected$ and eventually alive$ observables will emit true values.
29
- */
30
- open(url: string, pingSystemInterval?: number, systemAliveTimeout?: number): void;
31
-
32
- /**
33
- * Close socket.
34
- *
35
- * The opened$ and alive$ observables will emit false values.
36
- */
37
- close(): void;
38
-
39
- /**
40
- * Send an instance of a message.
41
- *
42
- * Subscribe to the message$ observable in order to receive the response messages.
43
- */
44
- send(message: MotionMasterMessage): void;
45
- }
@@ -1,119 +0,0 @@
1
- import { BehaviorSubject, filter, interval, map, Subject, Subscription, tap } from "rxjs";
2
- import { webSocket, WebSocketSubject, WebSocketSubjectConfig } from "rxjs/webSocket";
3
- import { logger } from "./logger";
4
- import { MotionMasterReqResSocket } from "./motion-master-req-res-socket";
5
- import { MotionMasterMessage } from "./types";
6
-
7
- export class MotionMasterReqResWebSocket implements MotionMasterReqResSocket {
8
-
9
- readonly opened$ = new BehaviorSubject<boolean>(false);
10
-
11
- readonly alive$ = new BehaviorSubject<boolean>(false);
12
-
13
- readonly message$ = new Subject<MotionMasterMessage>();
14
-
15
- private aliveTimeoutId = 0;
16
-
17
- private pingSystem = MotionMasterMessage.create({
18
- request: { pingSystem: MotionMasterMessage.Request.PingSystem.create() }
19
- });
20
-
21
- private pingSystemObserver = { next: () => this.send(this.pingSystem) };
22
-
23
- private pingSystemSubscription = new Subscription();
24
-
25
- private webSocket$?: WebSocketSubject<Uint8Array>;
26
-
27
- private _url?: string;
28
-
29
- get url(): string | undefined {
30
- return this._url;
31
- }
32
-
33
- open(url: string, pingSystemInterval = 250, systemAliveTimeout = 1000): void {
34
- if (this.opened$.getValue() === true) {
35
- throw new Error('MotionMasterReqResWebSocket is already opened!');
36
- }
37
-
38
- this._url = url;
39
-
40
- this.webSocket$ = this.createWebSocket(url, pingSystemInterval);
41
-
42
- this.webSocket$?.pipe(
43
- map((data: Uint8Array) => MotionMasterMessage.decode(data)),
44
- tap(() => this.keepalive(systemAliveTimeout)),
45
- filter((message) => !(message.status?.systemPong)),
46
- tap((message) => {
47
- if (message.status) {
48
- const keys = Object.keys(message.status).join();
49
- const motionMasterMessage = message.toJSON ? message.toJSON() : message;
50
- logger.info({
51
- namespace: 'reqResMessage',
52
- motionMasterMessage,
53
- type: message.type,
54
- }, `Received ${keys}`);
55
- }
56
- }),
57
- ).subscribe({
58
- next: (message) => this.message$.next(message),
59
- error: () => {
60
- // ignore
61
- },
62
- });
63
- }
64
-
65
- close(): void {
66
- clearTimeout(this.aliveTimeoutId);
67
- this.pingSystemSubscription.unsubscribe();
68
- this.webSocket$?.unsubscribe();
69
- }
70
-
71
- send(message: MotionMasterMessage): void {
72
- if (!(message.request?.pingSystem)) {
73
- if (message.request) {
74
- const keys = Object.keys(message.request).join();
75
- const motionMasterMessage = message.toJSON ? message.toJSON() : message;
76
- logger.info({
77
- namespace: 'reqResMessage',
78
- motionMasterMessage,
79
- type: message.type,
80
- }, `Send ${keys}`);
81
- }
82
- }
83
- this.webSocket$?.next(MotionMasterMessage.encode(message).finish());
84
- }
85
-
86
- private createWebSocket(url: string, pingSystemInterval = 250) {
87
- const webSocketConfig: WebSocketSubjectConfig<Uint8Array> = {
88
- binaryType: 'arraybuffer',
89
- closeObserver: {
90
- next: () => {
91
- this.opened$.next(false);
92
- logger.info(`Connection closed ${url}`);
93
- },
94
- },
95
- deserializer: (e: MessageEvent) => new Uint8Array(e.data as unknown as Iterable<number>),
96
- openObserver: {
97
- next: () => {
98
- this.pingSystemSubscription = interval(pingSystemInterval)
99
- .subscribe(this.pingSystemObserver);
100
- this.opened$.next(true);
101
- logger.info(`Connection opened ${url}`);
102
- },
103
- },
104
- serializer: (value: Uint8Array) => value,
105
- url,
106
- };
107
-
108
- return webSocket(webSocketConfig);
109
- }
110
-
111
- private keepalive(systemAliveTimeout = 1000) {
112
- if (this.alive$.getValue() === false) {
113
- this.alive$.next(true);
114
- }
115
- self.clearTimeout(this.aliveTimeoutId);
116
- this.aliveTimeoutId = self.setTimeout(() => this.alive$.next(false), systemAliveTimeout);
117
- }
118
-
119
- }
@@ -1,63 +0,0 @@
1
- import { BehaviorSubject, Subject } from "rxjs";
2
- import { logger } from "./logger";
3
- import { MotionMasterReqResSocket } from "./motion-master-req-res-socket";
4
- import { MotionMasterMessage } from "./types";
5
- import { createPlainObjectFromMotionMasterMessage } from "./util";
6
-
7
- export class MotionMasterReqResWorkerSocket implements MotionMasterReqResSocket {
8
-
9
- readonly opened$ = new BehaviorSubject<boolean>(false);
10
-
11
- readonly alive$ = new BehaviorSubject<boolean>(false);
12
-
13
- readonly message$ = new Subject<MotionMasterMessage>();
14
-
15
- private _url?: string;
16
-
17
- constructor(
18
- public readonly worker: Worker,
19
- ) { }
20
-
21
- get url(): string | undefined {
22
- return this._url;
23
- }
24
-
25
- open(url: string, pingSystemInterval = 250, systemAliveTimeout = 1000): void {
26
- if (this.opened$.getValue() === true) {
27
- throw new Error('MotionMasterReqResWorkerSocket is already opened!');
28
- }
29
-
30
- this._url = url;
31
-
32
- this.worker.onmessage = ({ data }) => {
33
- if ('opened' in data) {
34
- const { opened } = data;
35
- this.opened$.next(opened);
36
- logger.info(opened ? `Opened req/res 🔌 ${url}` : `Closed req/res 🔌 ${url}`);
37
- } else if ('alive' in data) {
38
- const { alive } = data;
39
- this.alive$.next(alive);
40
- logger.info(alive ? `System is alive 💓` : 'System is as dead as 🦤');
41
- } else if ('message' in data) {
42
- const { message: motionMasterMessage } = data;
43
- this.message$.next(motionMasterMessage);
44
- logger.info({ namespace: 'reqResMessage', motionMasterMessage, type: motionMasterMessage.type }, `Received ${motionMasterMessage.status.type}`);
45
- }
46
- }
47
-
48
- this.worker.postMessage({
49
- open: { url, pingSystemInterval, systemAliveTimeout },
50
- });
51
- }
52
-
53
- close(): void {
54
- this.worker.postMessage({ close: true });
55
- }
56
-
57
- send(message: MotionMasterMessage): void {
58
- const motionMasterMessage = createPlainObjectFromMotionMasterMessage(message);
59
- logger.info({ namespace: 'reqResMessage', motionMasterMessage, type: motionMasterMessage.type }, `Send ${motionMasterMessage.request.type}`);
60
- this.worker.postMessage({ message });
61
- }
62
-
63
- }
@@ -1,93 +0,0 @@
1
- import { pipe, filter, map, timeout, takeWhile, retryWhen, concat, take, Observable, delay } from 'rxjs';
2
- import { getParameterValue } from './util';
3
- import { requestStatusResolver } from './request-status-resolver';
4
- import { MotionMasterMessage, ParameterValueType, RequestStatus, StatusKey } from './types';
5
-
6
- export function selectMotionMasterMessageStatusByMessageId<T>(key: StatusKey, id?: string) {
7
- return pipe(
8
- filter((message: MotionMasterMessage) => message.id === id),
9
- map((message) => {
10
- // Handling the special case when Motion Master sends SystemEvent instead of e.g. DeviceParameterInfo status for
11
- // GetDeviceParameterInfo request message. This typically happens when request contains an invalid device address.
12
- if (message.status?.systemEvent && key !== 'systemEvent') {
13
- if (message.status.systemEvent.error) {
14
- throw new Error(`Error selecting message: ${message.status.systemEvent.error.message ?? ''}`);
15
- } else {
16
- throw new Error(`Error selecting message. Received System Event instead of "${key.toString()}": ${JSON.stringify(message)}`);
17
- }
18
- }
19
- return message.status?.[key] as T;
20
- }),
21
- );
22
- }
23
-
24
- export function extendStatus<T>(data: { statusKey: StatusKey, messageId: string }) {
25
- return pipe(
26
- map((status: T) => {
27
- const { statusKey, messageId } = data;
28
- const request = requestStatusResolver[statusKey]?.<T>(status);
29
- return { ...status, messageId, request };
30
- }),
31
- );
32
- }
33
-
34
- export function transformMotionMasterMessageToStatus<T>(statusKey: StatusKey, requestTimeout: number, messageId: string) {
35
- if (typeof requestTimeout !== 'number') {
36
- throw new Error(`Invalid requestTimeout=${requestTimeout} provided for ${statusKey}`);
37
- }
38
-
39
- return pipe(
40
- selectMotionMasterMessageStatusByMessageId<T>(statusKey, messageId),
41
- timeout(requestTimeout),
42
- extendStatus({ statusKey, messageId }),
43
- takeWhile((status) => status.request !== 'succeeded' && status.request !== 'failed', true),
44
- );
45
- }
46
-
47
- export function selectMotionMasterMessageByTopic<T>(topic: string) {
48
- return pipe(
49
- filter(([t]: [string, T]) => t === topic),
50
- map(([, data]) => data),
51
- );
52
- }
53
-
54
- export function selectMotionMasterMessageStatusByKey<T>(key: keyof MotionMasterMessage.IStatus) {
55
- return pipe(
56
- filter((message: MotionMasterMessage) => !!(message.status && message.status[key])),
57
- map((message) => message.status?.[key] as T),
58
- );
59
- }
60
-
61
- /**
62
- * Retry (resubscribe) to the source observable a number of attempts with delay timeout.
63
- */
64
- export function retryWithDelay<T>(delayTimeout = 1000, attempts = 3) {
65
- return pipe(
66
- retryWhen<T>((errors: Observable<Error>) => concat(
67
- errors.pipe(
68
- delay(delayTimeout),
69
- take(attempts),
70
- ),
71
- errors.pipe(
72
- map(error => {
73
- throw error; // rethrow the source observable error if all attempts fail
74
- }),
75
- ),
76
- )),
77
- );
78
- }
79
-
80
- export function mapStatusMessageToParameterValues() {
81
- return pipe(
82
- map((message: MotionMasterMessage) => {
83
- if (message.status?.monitoringParameterValues) {
84
- const { timestamp, deviceParameterValues } = message.status.monitoringParameterValues;
85
- return [
86
- timestamp,
87
- ...(deviceParameterValues?.parameterValues?.map((parameterValue) => getParameterValue(parameterValue)) ?? []),
88
- ] as ParameterValueType[];
89
- }
90
- return [];
91
- }),
92
- );
93
- }
@@ -1,12 +0,0 @@
1
- import { MotionMasterMessage } from './types';
2
-
3
- export const parameterValueTypeOptions = {
4
- 'intValue': 'intValue',
5
- 'uintValue': 'uintValue',
6
- 'floatValue': 'floatValue',
7
- 'stringValue': 'stringValue',
8
- 'rawValue': 'rawValue',
9
- };
10
-
11
- export const ethercatNetworkStateOptions = MotionMasterMessage.Status.EthercatNetworkState.State;
12
- export const positionControllerTypeOptions = MotionMasterMessage.Request.ComputeAutoTuningGains.PositionParameters.ControllerType;
@@ -1,81 +0,0 @@
1
- import { makeParameterId, splitParameterId } from "./parameter";
2
-
3
- describe('parameter', () => {
4
-
5
- describe('makeParameterId', () => {
6
-
7
- test.each<[[(number | null | undefined), (number | null | undefined)], string]>([
8
- [[0x1234, 123], '0x1234:7B'],
9
- [[23, 23], '0x0017:17'],
10
- [[0, 0], '0x0000:00'],
11
- [[0x2000, undefined], '0x2000:00'],
12
- [[0x2003, null], '0x2003:00'],
13
- [[0x10000, 3], '0x00010000:03'],
14
- [[0x100F0, undefined], '0x000100F0:00'],
15
- ])('should for tuple %j return "%s"', (tuple, expected) => {
16
- const result = makeParameterId(tuple);
17
-
18
- expect(result).toBe(expected);
19
- });
20
-
21
- test.each([
22
- [{}, '0x0000:00'],
23
- [{ index: 0x2004, subindex: 4 }, '0x2004:04'],
24
- [{ index: 0x1024 }, '0x1024:00'],
25
- ])('should for parameter %j return "%s"', (parameter, expected) => {
26
- const result = makeParameterId(parameter);
27
-
28
- expect(result).toBe(expected);
29
- });
30
-
31
- test.each([
32
- [0x1234, 123, '0x1234:7B'],
33
- [23, 23, '0x0017:17'],
34
- [0, 0, '0x0000:00'],
35
- [0x2000, undefined, '0x2000:00'],
36
- [0x2003, null, '0x2003:00'],
37
- [0x10000, 3, '0x00010000:03'],
38
- [0x100F0, undefined, '0x000100F0:00'],
39
- ])('should for index %d and subindex %d return "%s"', (index, subindex, expected) => {
40
- const result = makeParameterId(index, subindex);
41
-
42
- expect(result).toBe(expected);
43
- });
44
-
45
- it('should throw if index is less than 0', () => {
46
- expect(() => {
47
- makeParameterId(-123, 0);
48
- }).toThrow();
49
- });
50
-
51
- it('should throw if subindex is less than 0', () => {
52
- expect(() => {
53
- makeParameterId(0x1024, -3);
54
- }).toThrow();
55
- });
56
-
57
- });
58
-
59
- describe('splitParameterId', () => {
60
-
61
- test.each([
62
- ['0x1234:7B', [4660, 123]],
63
- ['0x000100F0:00', [65776, 0]],
64
- ])('should for id "%s" return tuple %j', (id, expected) => {
65
- const result = splitParameterId(id);
66
-
67
- expect(result).toEqual(expected);
68
- });
69
-
70
- test.each([
71
- ['0x12:7B'],
72
- ['0x1234:F'],
73
- ['0x1234:FG'],
74
- ['0x1H34:FF'],
75
- ])('should throw error for id "%s"', (id) => {
76
- expect(() => splitParameterId(id)).toThrow();
77
- });
78
-
79
- });
80
-
81
- });
@@ -1,119 +0,0 @@
1
- import { MotionMasterMessage, ParameterTypeValueKey, ParameterValueType } from "./types";
2
-
3
- export interface Parameter extends MotionMasterMessage.Status.DeviceParameterInfo.IParameter, MotionMasterMessage.Status.DeviceParameterValues.IParameterValue {
4
- index: number;
5
- subindex: number;
6
- name: string;
7
- value: ParameterValueType;
8
- typeValue: ParameterTypeValueKey;
9
- }
10
-
11
- export type ParameterIndexSubindex = Pick<Parameter, 'index' | 'subindex'>;
12
-
13
- /**
14
- * Make parameter id by providing a tuple of index and subindex.
15
- *
16
- * @param tuple - an array of index and subindex
17
- * @throws {Error} if index or subindex are less than 0
18
- * @returns combined index and subindex in uppercase hexadecimal format, e.g. "0x60FE:02"
19
- */
20
- export function makeParameterId(tuple: [(number | null | undefined), (number | null | undefined)]): string;
21
-
22
- /**
23
- * Make parameter id by providing an object which has index and subindex properties.
24
- *
25
- * @param parameter - an object with optional index and subindex properties
26
- * @throws {Error} if index or subindex are less than 0
27
- * @returns combined index and subindex in uppercase hexadecimal format, e.g. "0x60FE:02"
28
- */
29
- export function makeParameterId(parameter: { index?: (number | null), subindex?: (number | null) }): string;
30
-
31
- /**
32
- * Make parameter id by providing index and subindex.
33
- *
34
- * @param index - from 0x0000 to 0xFFFF for objects defined in ESI and >0xFFFF for custom object
35
- * @param subindex - defaults to 0 if not provided
36
- * @throws {Error} if index or subindex are less than 0
37
- * @returns combined index and subindex in uppercase hexadecimal format, e.g. "0x60FE:02"
38
- */
39
- export function makeParameterId(index?: (number | null), subindex?: (number | null)): string;
40
-
41
- /**
42
- * Make parameter id by providing different arguments.
43
- *
44
- * The purpose of this declaration is to expose the signature of the implementation for {@link makeDeviceParameterId} function.
45
- *
46
- * @param a - number or an object with index and subindex or tuple of index and subindex
47
- * @param b - optional subindex, defaults to 0 if not provided
48
- * @throws {Error} if index or subindex are less than 0
49
- * @returns combined index and subindex in uppercase hexadecimal format, e.g. "0x60FE:02"
50
- */
51
- export function makeParameterId(a?: (number | null) | { index?: (number | null), subindex?: (number | null) } | [(number | null | undefined), (number | null | undefined)], b?: (number | null)): string;
52
-
53
- export function makeParameterId(a?: (number | null) | { index?: (number | null), subindex?: (number | null) } | [(number | null | undefined), (number | null | undefined)], b?: (number | null)): string {
54
- let index = 0;
55
- let subindex = 0;
56
-
57
- if (Array.isArray(a)) {
58
- if (a[0]) {
59
- index = a[0];
60
- }
61
- if (a[1]) {
62
- subindex = a[1];
63
- }
64
- } else if (typeof a === 'object') {
65
- if (a?.index) {
66
- index = a.index;
67
- }
68
- if (a?.subindex) {
69
- subindex = a.subindex;
70
- }
71
- } else {
72
- if (typeof a === 'number') {
73
- index = a;
74
- }
75
- if (typeof b === 'number') {
76
- subindex = b;
77
- }
78
- }
79
-
80
- if (index < 0) {
81
- throw new Error(`The provided index to makeParameterId must be greater than 0: ${index}`);
82
- }
83
-
84
- if (subindex < 0) {
85
- throw new Error(`The provided subindex to makeParameterId must be greater than 0: ${subindex}`);
86
- }
87
-
88
- const indexMaxLength = index > 0xFFFF ? 8 : 4;
89
- const x = index.toString(16).toUpperCase().padStart(indexMaxLength, '0');
90
- const y = subindex.toString(16).toUpperCase().padStart(2, '0');
91
-
92
- return `0x${x}:${y}`;
93
- }
94
-
95
- /**
96
- * Example of parameter id is "0x2110:1A" where:
97
- * - 0x2110 is object index in hexadecimal format
98
- * - 1A is subindex in hexadecimal format
99
- */
100
- export const parameterIdRegExp = /^0x([0-9a-fA-F]{4,}):([0-9a-fA-F]{2})$/;
101
-
102
- /**
103
- * Split device parameter id into parts.
104
- *
105
- * @param id - parameter id like "0x2110:1A"
106
- * @returns tuple of index and subindex
107
- */
108
- export function splitParameterId(id: string): [number, number] {
109
- const match = id.match(parameterIdRegExp);
110
-
111
- if (!match) {
112
- throw new Error(`Parameter id "${id}" doesn't match the regular expression: ${parameterIdRegExp}`);
113
- }
114
-
115
- return [
116
- parseInt(match[1], 16), // index
117
- parseInt(match[2], 16), // subindex
118
- ];
119
- }