@@ -3,23 +3,28 @@ import React, { useEffect, useState } from "react";
33import TopBar from "./TopBar" ;
44import Groups from "./Groups" ;
55import useWebSocket from "react-use-websocket" ;
6- import { dehex_and_decompress } from "./dehex_and_decompress" ;
6+ import {
7+ dehex_and_decompress ,
8+ instListFromBytes ,
9+ } from "./dehex_and_decompress" ;
710import { findPVInDashboard , Instrument } from "./Instrument" ;
811import { useSearchParams } from "next/navigation" ;
912import {
1013 ConfigOutput ,
1114 ConfigOutputBlock ,
1215 IfcBlock ,
1316 IfcGroup ,
14- IfcPV ,
1517 IfcPVWSMessage ,
1618 IfcPVWSRequest ,
19+ instList ,
20+ PVWSRequestType ,
1721} from "@/app/types" ;
1822import {
19- findPVByAddress ,
2023 ExponentialOnThresholdFormat ,
24+ findPVByAddress ,
2125} from "@/app/components/PVutils" ;
2226import CheckToggle from "@/app/components/CheckToggle" ;
27+ import { instListPV , instListSubscription , socketURL } from "@/app/commonVars" ;
2328
2429let lastUpdate : string = "" ;
2530
@@ -46,7 +51,7 @@ export function subscribeToBlockPVs(
4651 * Subscribes to a block and its associated run control PVs
4752 */
4853 sendJsonMessage ( {
49- type : " subscribe" ,
54+ type : PVWSRequestType . subscribe ,
5055 pvs : [
5156 block_address ,
5257 block_address + RC_ENABLE ,
@@ -101,15 +106,12 @@ export function toPrecision(
101106
102107function InstrumentData ( { instrumentName } : { instrumentName : string } ) {
103108 const [ showHiddenBlocks , setShowHiddenBlocks ] = useState ( false ) ;
104- const [ showSetpoints , setShowSetpoints ] = useState ( false ) ;
105- const [ showTimestamps , setShowTimestamps ] = useState ( false ) ;
106109 const CONFIG_DETAILS = "CS:BLOCKSERVER:GET_CURR_CONFIG_DETAILS" ;
107- const [ instlist , setInstlist ] = useState < Array < any > | null > ( null ) ;
110+ const [ instlist , setInstlist ] = useState < instList | null > ( null ) ;
108111 const [ currentInstrument , setCurrentInstrument ] = useState < Instrument | null > (
109112 null ,
110113 ) ;
111- const socketURL =
112- process . env . NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv" ;
114+
113115 const instName = instrumentName ;
114116
115117 useEffect ( ( ) => {
@@ -130,10 +132,7 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
130132
131133 useEffect ( ( ) => {
132134 // This is an initial useEffect to subscribe to lots of PVs including the instlist.
133- sendJsonMessage ( {
134- type : "subscribe" ,
135- pvs : [ "CS:INSTLIST" ] ,
136- } ) ;
135+ sendJsonMessage ( instListSubscription ) ;
137136
138137 if ( instName == "" || instName == null || instlist == null ) {
139138 return ;
@@ -142,8 +141,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
142141 let prefix = "" ;
143142
144143 for ( const item of instlist ) {
145- if ( item [ " name" ] == instName . toUpperCase ( ) ) {
146- prefix = item [ " pvPrefix" ] ;
144+ if ( item . name == instName . toUpperCase ( ) ) {
145+ prefix = item . pvPrefix ;
147146 }
148147 }
149148 if ( ! prefix ) {
@@ -156,15 +155,18 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
156155 setCurrentInstrument ( instrument ) ;
157156
158157 sendJsonMessage ( {
159- type : " subscribe" ,
158+ type : PVWSRequestType . subscribe ,
160159 pvs : [ `${ prefix } ${ CONFIG_DETAILS } ` ] ,
161160 } ) ;
162161
163162 // subscribe to dashboard and run info PVs
164163 for ( const pv of instrument . runInfoPVs . concat (
165164 instrument . dashboard . flat ( 3 ) ,
166165 ) ) {
167- sendJsonMessage ( { type : "subscribe" , pvs : [ pv . pvaddress ] } ) ;
166+ sendJsonMessage ( {
167+ type : PVWSRequestType . subscribe ,
168+ pvs : [ pv . pvaddress ] ,
169+ } ) ;
168170 }
169171 }
170172 } , [ instlist , instName , sendJsonMessage , currentInstrument ] ) ;
@@ -178,11 +180,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
178180 const updatedPVName : string = updatedPV . pv ;
179181 const updatedPVbytes : string | null | undefined = updatedPV . b64byt ;
180182
181- if ( updatedPVName == "CS:INSTLIST" && updatedPVbytes != null ) {
182- const dehexedInstList = dehex_and_decompress ( atob ( updatedPVbytes ) ) ;
183- if ( dehexedInstList != null && typeof dehexedInstList == "string" ) {
184- setInstlist ( JSON . parse ( dehexedInstList ) ) ;
185- }
183+ if ( updatedPVName == instListPV && updatedPVbytes != null ) {
184+ setInstlist ( instListFromBytes ( updatedPVbytes ) ) ;
186185 }
187186
188187 if ( ! currentInstrument ) {
@@ -200,9 +199,6 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
200199 }
201200 lastUpdate = updatedPVbytes ;
202201 const res = dehex_and_decompress ( atob ( updatedPVbytes ) ) ;
203- if ( res == null || typeof res != "string" ) {
204- return ;
205- }
206202 currentInstrument . groups = getGroupsWithBlocksFromConfigOutput (
207203 JSON . parse ( res ) ,
208204 sendJsonMessage ,
0 commit comments