@@ -5,12 +5,25 @@ import { EOL, isEOL, MonitorSettings, PluggableMonitor } from "./utils";
55
66export default function App ( ) {
77 const [ config , setConfig ] = useState < Partial < MonitorSettings | null > > ( null ) ;
8+
9+ const [ webSocketPort , setWebsocketPort ] = useState < number > ( ) ;
10+ const [ serialPort , setSerialPort ] = useState < string > ( ) ;
11+
812 const [ websocketIsConnected , setWebsocketIsConnected ] = useState ( false ) ;
913
1014 const websocket = useRef < WebSocket | null > ( null ) ;
1115
1216 const chartRef = useRef < any > ( ) ;
1317
18+ const wsSend = useCallback (
19+ ( clientCommand : PluggableMonitor . Protocol . ClientCommandMessage ) => {
20+ if ( websocket . current ?. readyState === WebSocket . OPEN ) {
21+ websocket . current . send ( JSON . stringify ( clientCommand ) ) ;
22+ }
23+ } ,
24+ [ ]
25+ ) ;
26+
1427 const onMiddlewareMessage = useCallback (
1528 ( message : PluggableMonitor . Protocol . Message ) => {
1629 // if there is no command
@@ -20,13 +33,22 @@ export default function App() {
2033 }
2134
2235 if ( PluggableMonitor . Protocol . isMiddlewareCommandMessage ( message ) ) {
23- const { darkTheme, serialPort, connected } =
24- message . data . monitorUISettings || { } ;
36+ const {
37+ autoscroll,
38+ timestamp,
39+ lineEnding,
40+ interpolate,
41+ darkTheme,
42+ wsPort,
43+ serialPort : serialPortExtracted ,
44+ connected,
45+ generate,
46+ } = message . data . monitorUISettings || { } ;
2547
2648 let updateTitle = false ;
27- let serialNameTitle = config ?. monitorUISettings ?. serialPort ;
28- if ( typeof serialPort !== "undefined" ) {
29- serialNameTitle = serialPort ;
49+ let serialNameTitle = serialPort ;
50+ if ( typeof serialPortExtracted !== "undefined" ) {
51+ serialNameTitle = serialPortExtracted ;
3052 updateTitle = true ;
3153 }
3254
@@ -45,32 +67,99 @@ export default function App() {
4567 ? document . body . classList . add ( "dark" )
4668 : document . body . classList . remove ( "dark" ) ;
4769 }
48- setConfig ( ( c ) => ( { ...c , ...message . data } ) ) ;
70+
71+ // ** we should not set a "setting" as undefined FROM THE IDE,
72+ // ** more specifically we will not overwrite a given "setting" if we receive a message from the IDE that doesn't include it,
73+ // ** we only overwrite a given "setting" if we recieve a value for it (that's not undefined) from the IDE
74+
75+ const { id, label, type, values, selectedValue } =
76+ message . data . pluggableMonitorSettings ?. baudrate || { } ;
77+
78+ setConfig ( ( prevConfig ) => ( {
79+ pluggableMonitorSettings : {
80+ baudrate : {
81+ id :
82+ typeof id === "undefined"
83+ ? prevConfig ?. pluggableMonitorSettings ?. baudrate ?. id
84+ : id ,
85+ label :
86+ typeof label === "undefined"
87+ ? prevConfig ?. pluggableMonitorSettings ?. baudrate ?. label
88+ : label ,
89+ type :
90+ typeof type === "undefined"
91+ ? prevConfig ?. pluggableMonitorSettings ?. baudrate ?. type
92+ : type ,
93+ values :
94+ typeof values === "undefined"
95+ ? prevConfig ?. pluggableMonitorSettings ?. baudrate ?. values
96+ : values ,
97+ selectedValue :
98+ typeof selectedValue === "undefined"
99+ ? prevConfig ?. pluggableMonitorSettings ?. baudrate
100+ ?. selectedValue || "9600"
101+ : selectedValue ,
102+ } ,
103+ } ,
104+ monitorUISettings : {
105+ autoscroll :
106+ typeof autoscroll === "undefined"
107+ ? prevConfig ?. monitorUISettings ?. autoscroll
108+ : autoscroll ,
109+ timestamp :
110+ typeof timestamp === "undefined"
111+ ? prevConfig ?. monitorUISettings ?. timestamp
112+ : timestamp ,
113+ lineEnding :
114+ typeof lineEnding === "undefined"
115+ ? prevConfig ?. monitorUISettings ?. lineEnding
116+ : lineEnding ,
117+ interpolate :
118+ typeof interpolate === "undefined"
119+ ? prevConfig ?. monitorUISettings ?. interpolate
120+ : interpolate ,
121+ darkTheme :
122+ typeof darkTheme === "undefined"
123+ ? prevConfig ?. monitorUISettings ?. darkTheme
124+ : darkTheme ,
125+ connected :
126+ typeof connected === "undefined"
127+ ? prevConfig ?. monitorUISettings ?. connected
128+ : connected ,
129+ generate :
130+ typeof generate === "undefined"
131+ ? prevConfig ?. monitorUISettings ?. generate
132+ : generate ,
133+ } ,
134+ } ) ) ;
135+
136+ if ( typeof serialPortExtracted !== "undefined" ) {
137+ setSerialPort ( serialPortExtracted ) ;
138+ }
139+ if ( typeof wsPort !== "undefined" ) {
140+ setWebsocketPort ( wsPort ) ;
141+ }
49142 }
50143 } ,
51- [ config ?. monitorUISettings ?. serialPort ]
144+ [ serialPort ]
52145 ) ;
53146
54147 // as soon as the wsPort is set, create a websocket connection
55148 useEffect ( ( ) => {
56- if ( ! config ?. monitorUISettings ?. wsPort ) {
149+ if ( ! webSocketPort ) {
57150 return ;
58151 }
59152
60- console . log (
61- `opening ws connection on localhost:${ config ?. monitorUISettings ?. wsPort } `
62- ) ;
63- websocket . current = new WebSocket (
64- `ws://localhost:${ config ?. monitorUISettings ?. wsPort } `
65- ) ;
153+ console . log ( `opening ws connection on localhost:${ webSocketPort } ` ) ;
154+ websocket . current = new WebSocket ( `ws://localhost:${ webSocketPort } ` ) ;
66155 setWebsocketIsConnected ( true ) ;
67156
68157 const wsCurrent = websocket . current ;
69158 return ( ) => {
70159 console . log ( "closing ws connection" ) ;
71160 wsCurrent . close ( ) ;
72161 } ;
73- } , [ config ?. monitorUISettings ?. wsPort ] ) ;
162+ } , [ webSocketPort ] ) ;
74163
75164 useEffect ( ( ) => {
76165 if ( websocketIsConnected && websocket . current ) {
@@ -93,7 +182,7 @@ export default function App() {
93182 label : "Baudrate" ,
94183 type : "enum" ,
95184 values : ( urlParams . get ( "baudrates" ) || "" ) . split ( "," ) ,
96- selectedValue : urlParams . get ( "baudrate " ) || "9600" ,
185+ selectedValue : urlParams . get ( "currentBaudrate " ) || "9600" ,
97186 } ,
98187 } ,
99188 monitorUISettings : {
@@ -132,7 +221,7 @@ export default function App() {
132221
133222 return (
134223 ( config && (
135- < ChartPlotter config = { config } ref = { chartRef } websocket = { websocket } />
224+ < ChartPlotter config = { config } wsSend = { wsSend } ref = { chartRef } />
136225 ) ) ||
137226 null
138227 ) ;
0 commit comments