@@ -5,12 +5,25 @@ import { EOL, isEOL, MonitorSettings, PluggableMonitor } from "./utils";
5
5
6
6
export default function App ( ) {
7
7
const [ config , setConfig ] = useState < Partial < MonitorSettings | null > > ( null ) ;
8
+
9
+ const [ webSocketPort , setWebsocketPort ] = useState < number > ( ) ;
10
+ const [ serialPort , setSerialPort ] = useState < string > ( ) ;
11
+
8
12
const [ websocketIsConnected , setWebsocketIsConnected ] = useState ( false ) ;
9
13
10
14
const websocket = useRef < WebSocket | null > ( null ) ;
11
15
12
16
const chartRef = useRef < any > ( ) ;
13
17
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
+
14
27
const onMiddlewareMessage = useCallback (
15
28
( message : PluggableMonitor . Protocol . Message ) => {
16
29
// if there is no command
@@ -20,13 +33,22 @@ export default function App() {
20
33
}
21
34
22
35
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 || { } ;
25
47
26
48
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 ;
30
52
updateTitle = true ;
31
53
}
32
54
@@ -45,32 +67,99 @@ export default function App() {
45
67
? document . body . classList . add ( "dark" )
46
68
: document . body . classList . remove ( "dark" ) ;
47
69
}
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
+ }
49
142
}
50
143
} ,
51
- [ config ?. monitorUISettings ?. serialPort ]
144
+ [ serialPort ]
52
145
) ;
53
146
54
147
// as soon as the wsPort is set, create a websocket connection
55
148
useEffect ( ( ) => {
56
- if ( ! config ?. monitorUISettings ?. wsPort ) {
149
+ if ( ! webSocketPort ) {
57
150
return ;
58
151
}
59
152
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 } ` ) ;
66
155
setWebsocketIsConnected ( true ) ;
67
156
68
157
const wsCurrent = websocket . current ;
69
158
return ( ) => {
70
159
console . log ( "closing ws connection" ) ;
71
160
wsCurrent . close ( ) ;
72
161
} ;
73
- } , [ config ?. monitorUISettings ?. wsPort ] ) ;
162
+ } , [ webSocketPort ] ) ;
74
163
75
164
useEffect ( ( ) => {
76
165
if ( websocketIsConnected && websocket . current ) {
@@ -93,7 +182,7 @@ export default function App() {
93
182
label : "Baudrate" ,
94
183
type : "enum" ,
95
184
values : ( urlParams . get ( "baudrates" ) || "" ) . split ( "," ) ,
96
- selectedValue : urlParams . get ( "baudrate " ) || "9600" ,
185
+ selectedValue : urlParams . get ( "currentBaudrate " ) || "9600" ,
97
186
} ,
98
187
} ,
99
188
monitorUISettings : {
@@ -132,7 +221,7 @@ export default function App() {
132
221
133
222
return (
134
223
( config && (
135
- < ChartPlotter config = { config } ref = { chartRef } websocket = { websocket } />
224
+ < ChartPlotter config = { config } wsSend = { wsSend } ref = { chartRef } />
136
225
) ) ||
137
226
null
138
227
) ;
0 commit comments