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 < MessageEvent > > = 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 ( {
@@ -78,29 +79,29 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
7879 return ;
7980 }
8081
81- if ( ! currentInstrument ) {
82- return ;
83- }
84-
8582 if (
83+ currentInstrument &&
8684 updatedPVName == `${ currentInstrument . prefix } ${ CONFIG_DETAILS } ` &&
8785 updatedPVbytes != null
8886 ) {
8987 // config change, reset instrument groups
90- if ( updatedPVbytes == lastUpdate ) {
88+ if ( updatedPVbytes == lastUpdate || ! currentInstrument ) {
9189 // config hasnt actually changed so do nothing
9290 return ;
9391 }
94- setLastUpdate ( updatedPVbytes ) ;
95- currentInstrument . groups = getGroupsWithBlocksFromConfigOutput (
96- currentInstrument . prefix ,
92+ let newInstrument = currentInstrument . clone ( ) ;
93+ newInstrument . groups = getGroupsWithBlocksFromConfigOutput (
94+ newInstrument . prefix ,
9795 JSON . parse ( dehex_and_decompress ( atob ( updatedPVbytes ) ) ) ,
9896 ) ;
9997
10098 sendJsonMessage ( {
10199 type : PVWSRequestType . subscribe ,
102- pvs : currentInstrument . getAllBlockPVs ( ) ,
100+ pvs : newInstrument . getAllBlockPVs ( ) ,
103101 } ) ;
102+
103+ setLastUpdate ( updatedPVbytes ) ;
104+ setCurrentInstrument ( newInstrument ) ;
104105 } else {
105106 messageQueue . current . push ( m ) ;
106107 }
@@ -118,9 +119,17 @@ 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 ( ) ;
132+ const m = messageQueue . current . shift ( ) ;
124133 if ( m === undefined ) {
125134 break ;
126135 }
@@ -130,15 +139,18 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
130139 const pvVal = getPvValue ( updatedPV ) ;
131140
132141 if ( pvVal == undefined ) {
133- console . debug ( `initial/blank message from ${ updatedPVName } ` ) ;
134- return ;
142+ console . debug (
143+ `initial/blank message from ${ updatedPVName } : ${ m . data } ` ,
144+ ) ;
145+ continue ;
135146 }
136147
137148 // Check if this is a dashboard, run info, or block PV update.
138149 const pv =
139- currentInstrument . dashboard . get ( updatedPVName ) ||
140- currentInstrument . runInfoPVs . get ( updatedPVName ) ||
141- findPVInGroups ( currentInstrument . groups , updatedPVName ) ;
150+ newInstrument . dashboard . get ( updatedPVName ) ||
151+ newInstrument . runInfoPVs . get ( updatedPVName ) ||
152+ findPVInGroups ( newInstrument . groups , updatedPVName ) ;
153+
142154 if ( pv ) {
143155 storePrecision ( updatedPV , pv ) ;
144156 pv . value = toPrecision ( pv , pvVal ) ;
@@ -150,21 +162,21 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
150162 // its object such as its run control status or SP:RBV
151163 if ( updatedPVName . endsWith ( RC_INRANGE ) ) {
152164 const underlyingBlock = findPVInGroups (
153- currentInstrument . groups ,
165+ newInstrument . groups ,
154166 updatedPVName . replace ( RC_INRANGE , "" ) ,
155167 ) ;
156168 if ( underlyingBlock )
157169 underlyingBlock . runcontrol_inrange = yesToBoolean ( pvVal ) ;
158170 } else if ( updatedPVName . endsWith ( RC_ENABLE ) ) {
159171 const underlyingBlock = findPVInGroups (
160- currentInstrument . groups ,
172+ newInstrument . groups ,
161173 updatedPVName . replace ( RC_ENABLE , "" ) ,
162174 ) ;
163175 if ( underlyingBlock )
164176 underlyingBlock . runcontrol_enabled = yesToBoolean ( pvVal ) ;
165177 } else if ( updatedPVName . endsWith ( SP_RBV ) ) {
166178 const underlyingBlock = findPVInGroups (
167- currentInstrument . groups ,
179+ newInstrument . groups ,
168180 updatedPVName . replace ( SP_RBV , "" ) ,
169181 ) ;
170182 if ( underlyingBlock )
@@ -176,7 +188,8 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
176188 }
177189 }
178190 }
179- } , 5000 ) ;
191+ setCurrentInstrument ( newInstrument ) ;
192+ } , 1000 ) ;
180193 return ( ) => clearInterval ( interval ) ;
181194 } , [ currentInstrument ] ) ;
182195
0 commit comments