@adcops/autocore-react 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/LICENSE +59 -0
- package/additional-docs/ButtonApiSpecs.md +48 -0
- package/additional-docs/GlobalEventEmitter.md +244 -0
- package/additional-docs/general_recommendations.md +22 -0
- package/dist/components/DPad.css +522 -0
- package/dist/components/DPad.d.ts +34 -0
- package/dist/components/DPad.js +1 -0
- package/dist/components/Indicator.d.ts +81 -0
- package/dist/components/Indicator.js +1 -0
- package/dist/components/IndicatorButton.d.ts +137 -0
- package/dist/components/IndicatorButton.js +1 -0
- package/dist/components/IndicatorColor.d.ts +26 -0
- package/dist/components/IndicatorColor.js +1 -0
- package/dist/components/Osk.d.ts +14 -0
- package/dist/components/Osk.js +1 -0
- package/dist/components/OskDialog.d.ts +33 -0
- package/dist/components/OskDialog.js +1 -0
- package/dist/components/ValueDisplay.d.ts +99 -0
- package/dist/components/ValueDisplay.js +1 -0
- package/dist/components/osk.css +123 -0
- package/dist/core/EventEmitterContext.d.ts +223 -0
- package/dist/core/EventEmitterContext.js +1 -0
- package/dist/core/MaskPatterns.d.ts +71 -0
- package/dist/core/MaskPatterns.js +1 -0
- package/dist/core/NumerableTypes.d.ts +58 -0
- package/dist/core/NumerableTypes.js +1 -0
- package/dist/core/PositionContext.d.ts +38 -0
- package/dist/core/PositionContext.js +1 -0
- package/dist/core/UniqueId.d.ts +16 -0
- package/dist/core/UniqueId.js +1 -0
- package/dist/core/ValueSimulator.d.ts +71 -0
- package/dist/core/ValueSimulator.js +1 -0
- package/dist/core/hoc.d.ts +31 -0
- package/dist/core/hoc.js +1 -0
- package/dist/hub/HubBase.d.ts +169 -0
- package/dist/hub/HubBase.js +1 -0
- package/dist/hub/HubSimulate.d.ts +20 -0
- package/dist/hub/HubSimulate.js +1 -0
- package/dist/hub/HubSocketIo.d.ts +101 -0
- package/dist/hub/HubSocketIo.js +1 -0
- package/dist/hub/HubTauri.d.ts +86 -0
- package/dist/hub/HubTauri.js +1 -0
- package/dist/hub/index.d.ts +13 -0
- package/dist/hub/index.js +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +106 -0
- package/docs/assets/icons.js +15 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1414 -0
- package/docs/classes/components_Indicator.Indicator.html +105 -0
- package/docs/classes/components_IndicatorButton.IndicatorButton.html +119 -0
- package/docs/classes/components_OskDialog.OskDialog.html +117 -0
- package/docs/classes/components_ValueDisplay.ValueDisplay.html +104 -0
- package/docs/classes/core_ValueSimulator.ValueSimulator.html +41 -0
- package/docs/classes/hub_HubBase.HubBase.html +67 -0
- package/docs/classes/hub_HubSimulate.HubSimulate.html +72 -0
- package/docs/classes/hub_HubSocketIo.HubSocketIo.html +80 -0
- package/docs/classes/hub_HubTauri.HubTauri.html +80 -0
- package/docs/enums/components_DPad.VcJoyPadAction.html +10 -0
- package/docs/enums/components_DPad.VcJoyPadButtonId.html +7 -0
- package/docs/enums/components_IndicatorButton.ActionMode.html +6 -0
- package/docs/enums/components_IndicatorColor.IndicatorColor.html +13 -0
- package/docs/functions/components_DPad.VcDPad.html +5 -0
- package/docs/functions/components_Osk.Osk.html +5 -0
- package/docs/functions/core_EventEmitterContext.EventEmitterProvider.html +8 -0
- package/docs/functions/core_UniqueId.UniqueId.html +4 -0
- package/docs/functions/core_hoc.hocAddSubscription.html +6 -0
- package/docs/functions/hub.createHub.html +3 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +69 -0
- package/docs/interfaces/components_IndicatorButton.IndicatorButtonProps.html +635 -0
- package/docs/interfaces/components_IndicatorButton.IndicatorButtonState.html +10 -0
- package/docs/interfaces/core_DimensionsContext.IDimensionsContext.html +4 -0
- package/docs/interfaces/core_EventEmitterContext.Action.html +5 -0
- package/docs/interfaces/core_EventEmitterContext.EventEmitterContextType.html +18 -0
- package/docs/interfaces/core_EventEmitterContext.State.html +8 -0
- package/docs/interfaces/core_EventEmitterContext.Subscription.html +6 -0
- package/docs/modules/components_DPad.html +5 -0
- package/docs/modules/components_Indicator.html +4 -0
- package/docs/modules/components_IndicatorButton.html +7 -0
- package/docs/modules/components_IndicatorColor.html +2 -0
- package/docs/modules/components_Osk.html +3 -0
- package/docs/modules/components_OskDialog.html +3 -0
- package/docs/modules/components_ValueDisplay.html +3 -0
- package/docs/modules/core_DimensionsContext.html +4 -0
- package/docs/modules/core_EventEmitterContext.html +11 -0
- package/docs/modules/core_InputPatterns.html +2 -0
- package/docs/modules/core_NumerableTypes.html +2 -0
- package/docs/modules/core_UniqueId.html +2 -0
- package/docs/modules/core_ValueSimulator.html +3 -0
- package/docs/modules/core_hoc.html +3 -0
- package/docs/modules/hub.html +6 -0
- package/docs/modules/hub_HubBase.html +3 -0
- package/docs/modules/hub_HubSimulate.html +3 -0
- package/docs/modules/hub_HubSocketIo.html +3 -0
- package/docs/modules/hub_HubTauri.html +2 -0
- package/docs/types/core_EventEmitterContext.EmitterDispatchFunction.html +1 -0
- package/docs/types/core_EventEmitterContext.EmitterSubscribeFunction.html +1 -0
- package/docs/types/core_EventEmitterContext.EmitterUnsubscribeFunction.html +1 -0
- package/docs/types/core_NumerableTypes.NumerableFormatOptions.html +1 -0
- package/docs/types/core_hoc.HocAddSubscriptionProps.html +4 -0
- package/docs/variables/core_DimensionsContext.DimensionsContext.html +1 -0
- package/docs/variables/core_EventEmitterContext.EventEmitterContext.html +5 -0
- package/docs/variables/core_InputPatterns.InputPatterns.html +2 -0
- package/package.json +79 -0
- package/readme.md +140 -0
- package/src/components/DPad.css +522 -0
- package/src/components/DPad.tsx +94 -0
- package/src/components/Indicator.tsx +243 -0
- package/src/components/IndicatorButton.tsx +306 -0
- package/src/components/IndicatorColor.ts +36 -0
- package/src/components/Osk.tsx +193 -0
- package/src/components/OskDialog.tsx +165 -0
- package/src/components/ValueDisplay.tsx +181 -0
- package/src/components/osk.css +123 -0
- package/src/core/EventEmitterContext.tsx +394 -0
- package/src/core/MaskPatterns.ts +82 -0
- package/src/core/NumerableTypes.ts +81 -0
- package/src/core/PositionContext.ts +60 -0
- package/src/core/UniqueId.ts +41 -0
- package/src/core/ValueSimulator.ts +167 -0
- package/src/core/hoc.tsx +65 -0
- package/src/hub/HubBase.ts +293 -0
- package/src/hub/HubSimulate.ts +47 -0
- package/src/hub/HubSocketIo.ts +166 -0
- package/src/hub/HubTauri.ts +145 -0
- package/src/hub/index.ts +41 -0
- package/terser.config.cjs +25 -0
- package/todo.md +18 -0
- package/tools/copy-distribution-files.cjs +73 -0
- package/tools/minify.cjs +56 -0
- package/tsconfig.json +34 -0
- package/typedoc.json +13 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2024-01-18 12:49:58
|
|
4
|
+
* Author: Thomas C. Bitsky Jr.
|
|
5
|
+
* -----
|
|
6
|
+
* Last Modified: 2024-03-08 09:33:45
|
|
7
|
+
* Modified By: ADC
|
|
8
|
+
* -----
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { EmitterDispatchFunction } from "./EventEmitterContext";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Generates values for simulating data into an HMI.
|
|
17
|
+
* Used for demonstration and development testing.
|
|
18
|
+
*/
|
|
19
|
+
export class ValueSimulator {
|
|
20
|
+
|
|
21
|
+
protected bBit1: boolean = false;
|
|
22
|
+
protected bBit2: boolean = true;
|
|
23
|
+
|
|
24
|
+
protected iRamp1: number = 0;
|
|
25
|
+
protected iRamp2: number = 0;
|
|
26
|
+
protected fRamp1: number = 0;
|
|
27
|
+
protected fRamp2: number = 0;
|
|
28
|
+
|
|
29
|
+
protected iSin1: number = 0;
|
|
30
|
+
protected iSin2: number = 0;
|
|
31
|
+
protected fSin1: number = 0;
|
|
32
|
+
protected fSin2: number = 0;
|
|
33
|
+
|
|
34
|
+
protected iRamp1DirectionUp : boolean = true;
|
|
35
|
+
protected iRamp2DirectionUp : boolean = true;
|
|
36
|
+
protected fRamp1DirectionUp : boolean = true;
|
|
37
|
+
protected fRamp2DirectionUp : boolean = true;
|
|
38
|
+
|
|
39
|
+
protected iSin1Angle : number = Math.random() * Math.PI * 2;
|
|
40
|
+
protected iSin2Angle : number = Math.random() * Math.PI * 2;
|
|
41
|
+
protected fSin1Angle : number = (Math.random() * Math.PI * 2) / 10;
|
|
42
|
+
protected fSin2Angle : number = (Math.random() * Math.PI * 2) / 10;
|
|
43
|
+
|
|
44
|
+
protected intervalId: number = 0;
|
|
45
|
+
|
|
46
|
+
protected dispatch: EmitterDispatchFunction;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Constructor
|
|
50
|
+
* @param dispatch EmitterDispatchFunction Dispatch function from the application's EventEmiterContext.
|
|
51
|
+
*/
|
|
52
|
+
constructor(dispatch: EmitterDispatchFunction) {
|
|
53
|
+
this.dispatch = dispatch;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Start simulating values on a tick
|
|
58
|
+
*/
|
|
59
|
+
start() {
|
|
60
|
+
|
|
61
|
+
if (this.intervalId === 0) {
|
|
62
|
+
this.intervalId = setInterval(
|
|
63
|
+
() => this.update(),
|
|
64
|
+
1000
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Stop simulating values.
|
|
72
|
+
*/
|
|
73
|
+
stop() {
|
|
74
|
+
if (this.intervalId !== 0) {
|
|
75
|
+
clearInterval(this.intervalId);
|
|
76
|
+
this.intervalId = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Update and dispatch the values.
|
|
83
|
+
*/
|
|
84
|
+
protected update() {
|
|
85
|
+
|
|
86
|
+
this.bBit1 = !this.bBit1;
|
|
87
|
+
this.bBit2 = !this.bBit2;
|
|
88
|
+
|
|
89
|
+
[this.iRamp1, this.iRamp1DirectionUp] = this.updateRamp(this.iRamp1, this.iRamp1DirectionUp, 32767);
|
|
90
|
+
[this.iRamp2, this.iRamp2DirectionUp] = this.updateRamp(this.iRamp2, this.iRamp2DirectionUp, 32767);
|
|
91
|
+
[this.fRamp1, this.fRamp1DirectionUp] = this.updateRamp(this.fRamp1, this.fRamp1DirectionUp, 3276.7);
|
|
92
|
+
[this.fRamp2, this.fRamp2DirectionUp] = this.updateRamp(this.fRamp2, this.fRamp2DirectionUp, 3276.7);
|
|
93
|
+
|
|
94
|
+
[this.iSin1, this.iSin1Angle] = this.updateSin(this.iSin1, this.iSin1Angle, Math.PI / 180, 32767);
|
|
95
|
+
[this.iSin2, this.iSin2Angle] = this.updateSin(this.iSin2, this.iSin2Angle, Math.PI / 180, 32767);
|
|
96
|
+
[this.fSin1, this.fSin1Angle] = this.updateSin(this.fSin1, this.fSin1Angle, Math.PI / 180, 3276.7);
|
|
97
|
+
[this.fSin2, this.fSin2Angle] = this.updateSin(this.fSin2, this.fSin2Angle, Math.PI / 180, 3276.7);
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
this.updateTopic("value-simulator-bBit1", this.bBit1);
|
|
101
|
+
this.updateTopic("value-simulator-bBit2", this.bBit2);
|
|
102
|
+
this.updateTopic("value-simulator-iRamp1", this.iRamp1);
|
|
103
|
+
this.updateTopic("value-simulator-iRamp2", this.iRamp2);
|
|
104
|
+
this.updateTopic("value-simulator-fRamp1", this.fRamp1);
|
|
105
|
+
this.updateTopic("value-simulator-fRamp2", this.fRamp2);
|
|
106
|
+
|
|
107
|
+
this.updateTopic("value-simulator-iSin1", this.iSin1);
|
|
108
|
+
this.updateTopic("value-simulator-iSin2", this.iSin2);
|
|
109
|
+
this.updateTopic("value-simulator-fSin1", this.fSin1);
|
|
110
|
+
this.updateTopic("value-simulator-fSin2", this.fSin2);
|
|
111
|
+
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Updates the value of a ramp variable, changing its direction when limits are reached.
|
|
117
|
+
* @param {number} value The current value of the ramp variable.
|
|
118
|
+
* @param {boolean} directionUp Indicates if the ramp variable is currently increasing.
|
|
119
|
+
* @param {number} max The maximum value for the ramp.
|
|
120
|
+
* @returns {[number, boolean]} The updated value and direction.
|
|
121
|
+
*/
|
|
122
|
+
protected updateRamp(value: number, directionUp: boolean, max: number): [number, boolean] {
|
|
123
|
+
if (directionUp) {
|
|
124
|
+
value++;
|
|
125
|
+
if (value >= max) return [value, false];
|
|
126
|
+
} else {
|
|
127
|
+
value--;
|
|
128
|
+
if (value <= 0) return [value, true];
|
|
129
|
+
}
|
|
130
|
+
return [value, directionUp];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Updates a sine wave value based on the given angle and increments, then calculates the new value and angle.
|
|
136
|
+
* This method is useful for scenarios where a sine wave value needs to be progressively updated, such as in animations
|
|
137
|
+
* or simulations. The sine of the updated angle is calculated, multiplied by the maximum value to scale it, and both
|
|
138
|
+
* the new value and the updated angle are returned.
|
|
139
|
+
*
|
|
140
|
+
* @param {number} value The current value of the sine wave. This parameter is not directly used in the calculation of the new value,
|
|
141
|
+
* but it can be utilized if the method is extended or modified.
|
|
142
|
+
* @param {number} angle The current angle in radians. This is the phase of the sine wave.
|
|
143
|
+
* @param {number} increment The value by which the angle is to be incremented, in radians. This controls the progression of the wave.
|
|
144
|
+
* @param {number} max The maximum value (amplitude) of the sine wave. The calculated sine value will be scaled by this factor.
|
|
145
|
+
* @returns {[number,number]} A tuple where the first element is the new sine wave value and the second element is the updated angle.
|
|
146
|
+
*/
|
|
147
|
+
protected updateSin(value: number, angle: number, increment: number, max: number): [number, number] {
|
|
148
|
+
angle += increment;
|
|
149
|
+
value = Math.sin(angle) * max;
|
|
150
|
+
return [value, angle];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Dispatch the update for a particular topic.
|
|
156
|
+
*/
|
|
157
|
+
protected updateTopic(topic: string, value: any) {
|
|
158
|
+
|
|
159
|
+
this.dispatch({
|
|
160
|
+
topic: topic,
|
|
161
|
+
payload: value
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default ValueSimulator;
|
package/src/core/hoc.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2024 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2024-01-18 16:14:20
|
|
4
|
+
* -----
|
|
5
|
+
* Last Modified: 2024-03-08 09:27:12
|
|
6
|
+
* Modified By: ADC
|
|
7
|
+
* -----
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @fileinfo
|
|
12
|
+
* Explanation:
|
|
13
|
+
* React.ComponentType<P>: This is a generic type that can accept any React component (functional or class) with props of type P.
|
|
14
|
+
* P extends WithSubscriptionProps: Here, P is a generic type parameter that extends WithSubscriptionProps,
|
|
15
|
+
* meaning it can be any type that includes the properties of WithSubscriptionProps.
|
|
16
|
+
* Omit<P, keyof WithSubscriptionProps>: This utility type creates a new type by omitting properties from
|
|
17
|
+
* P that are also in WithSubscriptionProps. This is useful to prevent type clashes.
|
|
18
|
+
* useState<any>(): The state is typed as any since the value received from the subscription
|
|
19
|
+
* can be of any type. You may choose to make this more specific based on your application's needs.
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
import React, { useState, useEffect, useContext } from 'react';
|
|
25
|
+
import { EventEmitterContext } from './EventEmitterContext';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* High Order Component Subscription properties.
|
|
29
|
+
*/
|
|
30
|
+
export type HocAddSubscriptionProps = {
|
|
31
|
+
/** The topic to subscribe to. */
|
|
32
|
+
topic: string;
|
|
33
|
+
/** The name of the prop through which the subscription data is passed to the WrappedComponent. */
|
|
34
|
+
propName?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a higher-order component that subscribes to a specified topic and passes the data to the wrapped component.
|
|
40
|
+
*
|
|
41
|
+
* @param WrappedComponent The component to be wrapped and enhanced with subscription data.
|
|
42
|
+
* @param topic The topic to subscribe to.
|
|
43
|
+
* @param propName The name of the prop through which the subscription data is passed to the WrappedComponent.
|
|
44
|
+
* @returns A component that subscribes to the given topic and updates its specified prop with the received data.
|
|
45
|
+
*/
|
|
46
|
+
export function hocAddSubscription<P extends HocAddSubscriptionProps>(
|
|
47
|
+
WrappedComponent: React.ComponentType<P>,
|
|
48
|
+
topic: string,
|
|
49
|
+
propName: keyof P
|
|
50
|
+
) {
|
|
51
|
+
return function(props: Omit<P, keyof HocAddSubscriptionProps>) {
|
|
52
|
+
const [value, setValue] = useState<any>();
|
|
53
|
+
const { subscribe, unsubscribe } = useContext(EventEmitterContext);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const unsubscribeToken = subscribe(topic, setValue);
|
|
57
|
+
return () => unsubscribe(unsubscribeToken);
|
|
58
|
+
}, [topic, subscribe, unsubscribe]);
|
|
59
|
+
|
|
60
|
+
// Use propName to pass the updated value
|
|
61
|
+
const additionalProps = { [propName]: value } as Pick<P, keyof P>;
|
|
62
|
+
|
|
63
|
+
return <WrappedComponent {...props as P} {...additionalProps} />;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2023 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2023-12-15 14:21:33
|
|
4
|
+
* Author: Thomas C. Bitsky Jr.
|
|
5
|
+
* -----
|
|
6
|
+
* Last Modified: 2024-03-08 07:09:11
|
|
7
|
+
* Modified By: ADC
|
|
8
|
+
* -----
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { EventEmitterContextType} from '../core/EventEmitterContext';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Base class for the interface for the IPC to the backend,
|
|
18
|
+
* which may be websockets, MQTT, the Tauri API,
|
|
19
|
+
* or something else.
|
|
20
|
+
*
|
|
21
|
+
* The EventEmitterContext should generate the hub automatically.
|
|
22
|
+
*
|
|
23
|
+
* # Usage
|
|
24
|
+
* The hub can be used to publish and subscribe to
|
|
25
|
+
* topics in the front-end, regardless of being connected to any backend.
|
|
26
|
+
*
|
|
27
|
+
* ```
|
|
28
|
+
* const {dispatch, subscribe, unsubscribe} = useContext(EventEmitterContext);
|
|
29
|
+
* const [controlPower, setControlPower] = useState(false);
|
|
30
|
+
* useEffect(() => {
|
|
31
|
+
* const unsubscribeControlPower = subscribe('value-simulator-bBit1', (value) => {
|
|
32
|
+
* setControlPower(value);
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
*
|
|
36
|
+
* return () => {
|
|
37
|
+
* unsubscribe(unsubscribeControlPower);
|
|
38
|
+
* }
|
|
39
|
+
* }, [] );
|
|
40
|
+
*
|
|
41
|
+
* const onPbPressed = () => {
|
|
42
|
+
* let count = 1;
|
|
43
|
+
* dispatch({
|
|
44
|
+
* topic: "my-awesome-topic",
|
|
45
|
+
* payload: count
|
|
46
|
+
* });
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* ```
|
|
50
|
+
* The hub should also be used for invoking events in the backend.
|
|
51
|
+
* This example will call the function "update_count" in the backend, passing
|
|
52
|
+
* the expected argument "count". Details of the interaction between the Hub and
|
|
53
|
+
* the backend will be handled by the appropriate HubBase sub-class, and should
|
|
54
|
+
* be transparent to the front end.
|
|
55
|
+
*
|
|
56
|
+
* ```
|
|
57
|
+
* const {invoke} = useContext(EventEmitterContext);
|
|
58
|
+
* const incrementCount = () => {
|
|
59
|
+
* count += 1;
|
|
60
|
+
* invoke('update_count', {"count": count});
|
|
61
|
+
* };
|
|
62
|
+
*
|
|
63
|
+
* Subscribing to a topic is simple. The type of value received is specific
|
|
64
|
+
* to the topic.
|
|
65
|
+
*
|
|
66
|
+
* Example: Listen to an event 'xarm-position':
|
|
67
|
+
* ```
|
|
68
|
+
* const {subscribe, unsubscribe} = useContext(EventEmitterContext);
|
|
69
|
+
* useEffect(() => {
|
|
70
|
+
* const unsubscripeMp = subscribe('xarm-position', (value) => {
|
|
71
|
+
* // The publisher sent a JSON object of 3D position values.
|
|
72
|
+
* setX(value.x);
|
|
73
|
+
* setY(value.y);
|
|
74
|
+
* setZ(value.z);
|
|
75
|
+
* setA(value.roll);
|
|
76
|
+
* setB(value.yaw);
|
|
77
|
+
* setC(value.pitch);
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* return () => {
|
|
81
|
+
* unsubscribe(unsubscripeMp);
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* }, [] );
|
|
85
|
+
*
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
export abstract class HubBase {
|
|
90
|
+
|
|
91
|
+
protected localTopicToBackendTopicMap : Map<string,string> = new Map<string,string>();
|
|
92
|
+
protected backendTopicToLocalMap : Map<string,string> = new Map<string,string>();
|
|
93
|
+
protected localFNameToBackendFNameMap : Map<string,string> = new Map<string,string>();
|
|
94
|
+
protected backendFNameToLocalFNameMap : Map<string,string> = new Map<string,string>();
|
|
95
|
+
|
|
96
|
+
protected context : EventEmitterContextType | null = null;
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Constructor
|
|
101
|
+
*/
|
|
102
|
+
constructor() {
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Set the EventEmitterContext for this instance of the Hub. The EventEmitterContext
|
|
108
|
+
* should handle this as part of the initialization sequence. Because React can
|
|
109
|
+
* sometimes make (and destroy) multiple copies of the EventEmitterContext, it's important
|
|
110
|
+
* that the context be stored in the instance this way so that the HubBase can properly target
|
|
111
|
+
* right context.
|
|
112
|
+
*
|
|
113
|
+
* If this context is not sent, no events will be emitted from the backend to the front end,
|
|
114
|
+
* and global events in the interface may not work.
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
setContext(context : EventEmitterContextType | null ) {
|
|
118
|
+
this.context = context;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Invoke/send a message to the back end.
|
|
123
|
+
* This does NOT get published to the front end.
|
|
124
|
+
*/
|
|
125
|
+
abstract invoke(fname: string, payload? : object) : Promise<object>;
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Convenience function to invoke a command with an optional topic and optional value.
|
|
130
|
+
* This will invoke the specified command in the backend. It does not broadcast the
|
|
131
|
+
* value within the local frontend.
|
|
132
|
+
* @param fname string Function name
|
|
133
|
+
* @param topic string Topic
|
|
134
|
+
* @param value any data payload
|
|
135
|
+
*/
|
|
136
|
+
invokeTopic( fname : string, topic : string | null, value? : any ) : Promise<object> {
|
|
137
|
+
const msg = {
|
|
138
|
+
topic : topic,
|
|
139
|
+
data : value
|
|
140
|
+
};
|
|
141
|
+
return this.invoke(fname, msg);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Pubish a topic throughout the web app using the
|
|
146
|
+
* Global EventEmitterContext. This will broadcast within the local frontend.
|
|
147
|
+
* This does NOT get relayed to the backend.
|
|
148
|
+
*/
|
|
149
|
+
publish(topic : string, data : any | undefined) : void {
|
|
150
|
+
const convertedTopic = this.toLocalTopic(topic);
|
|
151
|
+
this.context?.dispatch({
|
|
152
|
+
topic: convertedTopic,
|
|
153
|
+
payload: data
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Map a local topic to a topic in the backend.
|
|
160
|
+
* @param localTopic string
|
|
161
|
+
* @param backendTopic string
|
|
162
|
+
*/
|
|
163
|
+
mapTopic(localTopic : string, backendTopic: string ) : void {
|
|
164
|
+
this.localTopicToBackendTopicMap.set(localTopic, backendTopic);
|
|
165
|
+
this.backendTopicToLocalMap.set(backendTopic, localTopic);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Unmap a local topic.
|
|
170
|
+
* @param localTopic string
|
|
171
|
+
*/
|
|
172
|
+
unmapTopic(localTopic: string) : void {
|
|
173
|
+
if (this.localTopicToBackendTopicMap.has(localTopic) ) {
|
|
174
|
+
const be = this.localTopicToBackendTopicMap.get(localTopic);
|
|
175
|
+
this.localTopicToBackendTopicMap.delete(localTopic);
|
|
176
|
+
|
|
177
|
+
if (be !== null && be !== undefined && this.backendTopicToLocalMap.has(be)) {
|
|
178
|
+
this.backendTopicToLocalMap.delete(be);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Map a local function name, as would be sent to the invoke method, to a
|
|
186
|
+
* function name in the backend.
|
|
187
|
+
* @param localFName string
|
|
188
|
+
* @param backendFName string
|
|
189
|
+
*/
|
|
190
|
+
mapFName(localFName : string, backendFName: string ) : void {
|
|
191
|
+
this.localFNameToBackendFNameMap.set(localFName, backendFName);
|
|
192
|
+
this.backendFNameToLocalFNameMap.set(backendFName, localFName);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Unmap a local function name.
|
|
197
|
+
* @param localFName string
|
|
198
|
+
*/
|
|
199
|
+
unmapFName(localFName: string) : void {
|
|
200
|
+
if (this.localFNameToBackendFNameMap.has(localFName) ) {
|
|
201
|
+
const be = this.localFNameToBackendFNameMap.get(localFName);
|
|
202
|
+
this.localFNameToBackendFNameMap.delete(localFName);
|
|
203
|
+
|
|
204
|
+
if (be !== null && be !== undefined && this.backendFNameToLocalFNameMap.has(be)) {
|
|
205
|
+
this.backendFNameToLocalFNameMap.delete(be);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Convert a local topic to a backend topic. If a mapping isn't
|
|
213
|
+
* registered, the local topic is returned.
|
|
214
|
+
* @param localTopic local topic name
|
|
215
|
+
* @returns string
|
|
216
|
+
*/
|
|
217
|
+
toBackendTopic(localTopic : string) : string {
|
|
218
|
+
if (this.localTopicToBackendTopicMap.has(localTopic) ) {
|
|
219
|
+
const ret = this.localTopicToBackendTopicMap.get(localTopic);
|
|
220
|
+
if (ret !== null && ret !== undefined)
|
|
221
|
+
return ret;
|
|
222
|
+
else
|
|
223
|
+
return localTopic;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
return localTopic;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Convert a local function name to a backend function name. If a mapping isn't
|
|
232
|
+
* registered, the local FName is returned.
|
|
233
|
+
* @param localTopic local function name
|
|
234
|
+
* @returns string
|
|
235
|
+
*/
|
|
236
|
+
toBackendFName(localFName : string) : string {
|
|
237
|
+
if (this.localFNameToBackendFNameMap.has(localFName) ) {
|
|
238
|
+
const ret = this.localFNameToBackendFNameMap.get(localFName);
|
|
239
|
+
if (ret !== null && ret !== undefined)
|
|
240
|
+
return ret;
|
|
241
|
+
else
|
|
242
|
+
return localFName;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
return localFName;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Convert a backend topic to a local topic. If a mapping isn't
|
|
252
|
+
* registered, the backend topic is returned.
|
|
253
|
+
* @param backendTopic backend topic name
|
|
254
|
+
* @returns string
|
|
255
|
+
*/
|
|
256
|
+
toLocalTopic(backendTopic : string) : string {
|
|
257
|
+
if (this.backendTopicToLocalMap.has(backendTopic) ) {
|
|
258
|
+
const ret = this.backendTopicToLocalMap.get(backendTopic);
|
|
259
|
+
if (ret !== null && ret !== undefined)
|
|
260
|
+
return ret;
|
|
261
|
+
else
|
|
262
|
+
return backendTopic;
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
return backendTopic;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Convert a backend function name to a local function name. If a mapping isn't
|
|
272
|
+
* registered, the backend function name is returned.
|
|
273
|
+
* @param backendTopic backend function name
|
|
274
|
+
* @returns string
|
|
275
|
+
*/
|
|
276
|
+
toLocalFName(backendTopic : string) : string {
|
|
277
|
+
if (this.backendFNameToLocalFNameMap.has(backendTopic) ) {
|
|
278
|
+
const ret = this.backendFNameToLocalFNameMap.get(backendTopic);
|
|
279
|
+
if (ret !== null && ret !== undefined)
|
|
280
|
+
return ret;
|
|
281
|
+
else
|
|
282
|
+
return backendTopic;
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
return backendTopic;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
export default HubBase;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2023 Automated Design Corp. All Rights Reserved.
|
|
3
|
+
* Created Date: 2023-12-17 10:38:21
|
|
4
|
+
* -----
|
|
5
|
+
* Last Modified: 2024-03-08 09:35:51
|
|
6
|
+
* Modified By: ADC
|
|
7
|
+
* -----
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import { HubBase } from './HubBase';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hub for simulating functionality when no backend is present.
|
|
16
|
+
*/
|
|
17
|
+
export class HubSimulate extends HubBase {
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
*/
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Invoke a method in the Socket.IO backend and wait for acknowledgment.
|
|
28
|
+
*
|
|
29
|
+
* @param fname method name
|
|
30
|
+
* @param payload Optional data payload
|
|
31
|
+
* @param timeout Timeout in milliseconds after which the promise is rejected if no response is received.
|
|
32
|
+
* @returns A Promise that resolves to the response from the backend or rejects if a timeout occurs.
|
|
33
|
+
*/
|
|
34
|
+
invoke(fname: string, payload?: object, timeout: number = 20): Promise<object> {
|
|
35
|
+
fname; // Not using for simulator
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
reject; // not using for simulator
|
|
38
|
+
// Set a timeout to echo a response
|
|
39
|
+
setTimeout(() => {
|
|
40
|
+
resolve({data: payload});
|
|
41
|
+
}, timeout);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default HubSimulate;
|