11"use client" ;
2- import { useEffect , useRef , useState } from "react" ;
2+ import { RefObject , useEffect , useRef , useState } from "react" ;
33import { IfcPVWSMessage , IfcPVWSRequest , PVWSRequestType } from "@/app/types" ;
44import {
55 findPVInGroups ,
@@ -46,7 +46,7 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
4646 }
4747 } , [ instName ] ) ;
4848
49- let messageQueue = useRef ( [ ] ) ;
49+ let messageQueue : RefObject < Array < IfcPVWSMessage > > = useRef ( [ ] ) ;
5050
5151 const {
5252 sendJsonMessage,
@@ -67,6 +67,7 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
6767 const prefix = getPrefix ( instListFromBytes ( updatedPVbytes ) , instName ) ;
6868 const instrument = new Instrument ( prefix ) ;
6969 setCurrentInstrument ( instrument ) ;
70+ messageQueue . current = [ ] ;
7071
7172 // subscribe to dashboard and run info PVs
7273 sendJsonMessage ( {
@@ -75,14 +76,10 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
7576 . concat ( Array . from ( instrument . dashboard . keys ( ) ) )
7677 . concat ( [ `${ prefix } ${ CONFIG_DETAILS } ` ] ) ,
7778 } ) ;
78- return ;
79- }
80-
81- if ( ! currentInstrument ) {
82- return ;
8379 }
8480
8581 if (
82+ currentInstrument &&
8683 updatedPVName == `${ currentInstrument . prefix } ${ CONFIG_DETAILS } ` &&
8784 updatedPVbytes != null
8885 ) {
@@ -91,18 +88,22 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
9188 // config hasnt actually changed so do nothing
9289 return ;
9390 }
94- setLastUpdate ( updatedPVbytes ) ;
95- currentInstrument . groups = getGroupsWithBlocksFromConfigOutput (
96- currentInstrument . prefix ,
91+ let newInstrument = currentInstrument . clone ( ) ;
92+ newInstrument . groups = getGroupsWithBlocksFromConfigOutput (
93+ newInstrument . prefix ,
9794 JSON . parse ( dehex_and_decompress ( atob ( updatedPVbytes ) ) ) ,
9895 ) ;
9996
10097 sendJsonMessage ( {
10198 type : PVWSRequestType . subscribe ,
102- pvs : currentInstrument . getAllBlockPVs ( ) ,
99+ pvs : newInstrument . getAllBlockPVs ( ) ,
103100 } ) ;
101+
102+ setLastUpdate ( updatedPVbytes ) ;
103+ setCurrentInstrument ( newInstrument ) ;
104104 } else {
105- messageQueue . current . push ( m ) ;
105+ console . log ( updatedPV . b64byt ) ;
106+ messageQueue . current . push ( updatedPV ) ;
106107 }
107108 } ,
108109 onError : ( ) => {
@@ -118,27 +119,37 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
118119
119120 useEffect ( ( ) => {
120121 const interval = setInterval ( ( ) => {
121- console . log ( "Process " + messageQueue . current . length + " queued messages" ) ;
122+ if ( currentInstrument == null ) {
123+ return ;
124+ }
125+ console . log (
126+ "Process " + messageQueue . current . length + " queued messages" ,
127+ ) ;
128+
129+ let newInstrument = currentInstrument . clone ( ) ;
130+
122131 while ( true ) {
123- const m = messageQueue . current . pop ( ) ;
124- if ( m === undefined ) {
132+ const updatedPV = messageQueue . current . shift ( ) ;
133+ if ( updatedPV === undefined ) {
125134 break ;
126135 }
127- const updatedPV : IfcPVWSMessage = JSON . parse ( m . data ) ;
128136 const updatedPVName : string = updatedPV . pv ;
129137
130138 const pvVal = getPvValue ( updatedPV ) ;
131139
132140 if ( pvVal == undefined ) {
133- console . debug ( `initial/blank message from ${ updatedPVName } ` ) ;
134- return ;
141+ console . debug (
142+ `initial/blank message from ${ updatedPVName } : ${ updatedPV . b64byt } }` ,
143+ ) ;
144+ continue ;
135145 }
136146
137147 // Check if this is a dashboard, run info, or block PV update.
138148 const pv =
139- currentInstrument . dashboard . get ( updatedPVName ) ||
140- currentInstrument . runInfoPVs . get ( updatedPVName ) ||
141- findPVInGroups ( currentInstrument . groups , updatedPVName ) ;
149+ newInstrument . dashboard . get ( updatedPVName ) ||
150+ newInstrument . runInfoPVs . get ( updatedPVName ) ||
151+ findPVInGroups ( newInstrument . groups , updatedPVName ) ;
152+
142153 if ( pv ) {
143154 storePrecision ( updatedPV , pv ) ;
144155 pv . value = toPrecision ( pv , pvVal ) ;
@@ -150,21 +161,21 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
150161 // its object such as its run control status or SP:RBV
151162 if ( updatedPVName . endsWith ( RC_INRANGE ) ) {
152163 const underlyingBlock = findPVInGroups (
153- currentInstrument . groups ,
164+ newInstrument . groups ,
154165 updatedPVName . replace ( RC_INRANGE , "" ) ,
155166 ) ;
156167 if ( underlyingBlock )
157168 underlyingBlock . runcontrol_inrange = yesToBoolean ( pvVal ) ;
158169 } else if ( updatedPVName . endsWith ( RC_ENABLE ) ) {
159170 const underlyingBlock = findPVInGroups (
160- currentInstrument . groups ,
171+ newInstrument . groups ,
161172 updatedPVName . replace ( RC_ENABLE , "" ) ,
162173 ) ;
163174 if ( underlyingBlock )
164175 underlyingBlock . runcontrol_enabled = yesToBoolean ( pvVal ) ;
165176 } else if ( updatedPVName . endsWith ( SP_RBV ) ) {
166177 const underlyingBlock = findPVInGroups (
167- currentInstrument . groups ,
178+ newInstrument . groups ,
168179 updatedPVName . replace ( SP_RBV , "" ) ,
169180 ) ;
170181 if ( underlyingBlock )
@@ -176,7 +187,8 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
176187 }
177188 }
178189 }
179- } , 5000 ) ;
190+ setCurrentInstrument ( newInstrument ) ;
191+ } , 1000 ) ;
180192 return ( ) => clearInterval ( interval ) ;
181193 } , [ currentInstrument ] ) ;
182194
0 commit comments