@adcops/autocore-react 3.0.32 → 3.0.33
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.
|
@@ -65,7 +65,7 @@ export declare const useRegisterSymbols: (domain: string, setters: Record<string
|
|
|
65
65
|
* );
|
|
66
66
|
* };
|
|
67
67
|
*/
|
|
68
|
-
export declare function
|
|
68
|
+
export declare function useWriteValue(domain: string, symbolName: string, fname?: string): (value: object | boolean | number | string) => Promise<void>;
|
|
69
69
|
/**
|
|
70
70
|
* useWriteScaledValue is a custom React hook that enables writing scaled numerical values to the autocore backend system.
|
|
71
71
|
* It applies a specified scale and offset to a numeric value before sending it over the websocket connection.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useContext,useRef,useEffect,useCallback}from"react";import{EventEmitterContext}from"../core/EventEmitterContext";function sleep(e){return new Promise((t=>setTimeout(t,e)))}export const useRegisterSymbols=(e,t,n,r={})=>{const{subscribe:
|
|
1
|
+
import{useContext,useRef,useEffect,useCallback}from"react";import{EventEmitterContext}from"../core/EventEmitterContext";function sleep(e){return new Promise((t=>setTimeout(t,e)))}export const useRegisterSymbols=(e,t,n,r={})=>{const{subscribe:o,unsubscribe:u,isConnected:s,invoke:c}=useContext(EventEmitterContext),a=useRef([]),i=useRef(!0);return useEffect((()=>{i.current=!0;const r=async()=>{for(const[r,u]of Object.entries(n))try{const n=o(`${e}/${u}`,(e=>{if(i.current){const n=t[r];n&&n(e.value)}}));a.current.push(n),await c(e,"refresh",{topic:`${u}`})}catch(e){}};if(s())r();else{let e=o("HUB/connected",(()=>{setTimeout((()=>{r(),u(e)}),500)}));a.current.push(e)}return()=>{i.current=!1,a.current.forEach((e=>u(e))),a.current=[]}}),[]),null};export function useWriteValue(e,t,n="write_value"){const{invoke:r}=useContext(EventEmitterContext);return async o=>{try{await r(e,n,{topic:t,value:o})}catch(e){}}}export function useWriteScaledValue(e,t,n,r,o="write_value"){const{invoke:u}=useContext(EventEmitterContext);return useCallback((async s=>{const c=(s-r)/n;try{await u(e,o,{symbol_name:t,value:c})}catch(e){}}),[t,n,r,u])}export function useTapValue(e,t,n="write_value"){const{invoke:r}=useContext(EventEmitterContext);return async()=>{try{await r(e,n,{symbol_name:t,value:!0}),await sleep(300),await r(e,n,{symbol_name:t,value:!1})}catch(e){}}}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Copyright (C) 2025 Automated Design Corp. All Rights Reserved.
|
|
3
3
|
* Created Date: 2025-03-31 06:38:50
|
|
4
4
|
* -----
|
|
5
|
-
* Last Modified: 2025-
|
|
5
|
+
* Last Modified: 2025-04-28 10:37:20
|
|
6
6
|
* -----
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
@@ -64,7 +64,7 @@ export const useRegisterSymbols = (
|
|
|
64
64
|
symbols: Record<string, string>,
|
|
65
65
|
options: Record<string, {}> = {},
|
|
66
66
|
) => {
|
|
67
|
-
const { subscribe, unsubscribe, isConnected } = useContext(EventEmitterContext);
|
|
67
|
+
const { subscribe, unsubscribe, isConnected, invoke } = useContext(EventEmitterContext);
|
|
68
68
|
const subscriptions = useRef<number[]>([]);
|
|
69
69
|
const isMounted = useRef(true);
|
|
70
70
|
|
|
@@ -80,9 +80,7 @@ export const useRegisterSymbols = (
|
|
|
80
80
|
//console.log(`Subscribe... ${domain}/${symbolName}`);
|
|
81
81
|
const subscriptionId = subscribe(`${domain}/${symbolName}`, (data) => {
|
|
82
82
|
|
|
83
|
-
//console.log(`Value received for ${key} to ${data.value}`);
|
|
84
83
|
if (isMounted.current) {
|
|
85
|
-
//console.log(`Setting ${key} to ${data.value}`);
|
|
86
84
|
const setter = setters[key];
|
|
87
85
|
if (setter) {
|
|
88
86
|
setter(data.value);
|
|
@@ -90,13 +88,18 @@ export const useRegisterSymbols = (
|
|
|
90
88
|
}
|
|
91
89
|
});
|
|
92
90
|
subscriptions.current.push(subscriptionId);
|
|
91
|
+
|
|
92
|
+
// Refresh on a topic is the same as a read_value, except the response is broadcasted.
|
|
93
|
+
// Because there currently isn't a centralized tag system, the topic name should not
|
|
94
|
+
// contain the domain name.
|
|
95
|
+
await invoke(domain, "refresh", {'topic' : `${symbolName}`});
|
|
96
|
+
|
|
93
97
|
} catch (err) {
|
|
94
98
|
console.error(`Failed to register symbol ${symbolName}: ${err}`);
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
|
|
98
|
-
|
|
99
|
-
// await invoke(domain, "refresh", {});
|
|
102
|
+
|
|
100
103
|
};
|
|
101
104
|
|
|
102
105
|
if (!isConnected() ) {
|
|
@@ -162,13 +165,13 @@ export const useRegisterSymbols = (
|
|
|
162
165
|
* );
|
|
163
166
|
* };
|
|
164
167
|
*/
|
|
165
|
-
export function
|
|
168
|
+
export function useWriteValue(domain : string, symbolName: string, fname : string = "write_value") {
|
|
166
169
|
const { invoke } = useContext(EventEmitterContext);
|
|
167
170
|
|
|
168
171
|
// Return a function that takes a value and sends it to the backend
|
|
169
172
|
return async (value: object | boolean | number | string ) => {
|
|
170
173
|
try {
|
|
171
|
-
await invoke(domain, fname, {
|
|
174
|
+
await invoke(domain, fname, { "topic" : symbolName, "value" : value });
|
|
172
175
|
} catch (err) {
|
|
173
176
|
console.error(`Error writing tag ${symbolName}: ${err}`);
|
|
174
177
|
}
|