11import React , { useEffect , useState , useCallback , useRef } from "react" ;
22import { ChartPlotter } from "./ChartPlotter" ;
33import { namedVariablesMulti } from "./fakeMessagsGenerators" ;
4- import { SerialPlotter } from "./utils" ;
4+ import { EOL , isEOL , MonitorSettings , PluggableMonitor } from "./utils" ;
55
66export default function App ( ) {
7- const [ config , setConfig ] = useState < SerialPlotter . Config | null > ( null ) ;
7+ const [ config , setConfig ] = useState < Partial < MonitorSettings | null > > ( null ) ;
88
99 const websocket = useRef < WebSocket | null > ( null ) ;
1010
1111 const chartRef = useRef < any > ( ) ;
1212
1313 const onMiddlewareMessage = useCallback (
14- (
15- message :
16- | SerialPlotter . Protocol . StreamMessage
17- | SerialPlotter . Protocol . CommandMessage
18- ) => {
14+ ( message : PluggableMonitor . Protocol . Message ) => {
1915 // if there is no command
20- if ( ! SerialPlotter . Protocol . isCommandMessage ( message ) ) {
16+ if ( PluggableMonitor . Protocol . isDataMessage ( message ) ) {
2117 chartRef && chartRef . current && chartRef . current . addNewData ( message ) ;
2218 return ;
2319 }
2420
25- if (
26- message . command ===
27- SerialPlotter . Protocol . Command . MIDDLEWARE_CONFIG_CHANGED
28- ) {
21+ if ( PluggableMonitor . Protocol . isMiddlewareCommandMessage ( message ) ) {
2922 const { darkTheme, serialPort, connected } =
30- message . data as SerialPlotter . Config ;
23+ message . data . monitorUISettings || { } ;
3124
3225 let updateTitle = false ;
33- let serialNameTitle = config ?. serialPort ;
26+ let serialNameTitle = config ?. monitorUISettings ?. serialPort ;
3427 if ( typeof serialPort !== "undefined" ) {
3528 serialNameTitle = serialPort ;
3629 updateTitle = true ;
@@ -54,19 +47,23 @@ export default function App() {
5447 setConfig ( ( c ) => ( { ...c , ...message . data } ) ) ;
5548 }
5649 } ,
57- [ config ?. serialPort ]
50+ [ config ?. monitorUISettings ?. serialPort ]
5851 ) ;
5952
6053 // as soon as the wsPort is set, create a websocket connection
6154 React . useEffect ( ( ) => {
62- if ( ! config ?. wsPort ) {
55+ if ( ! config ?. monitorUISettings ?. wsPort ) {
6356 return ;
6457 }
6558
66- console . log ( `opening ws connection on localhost:${ config ?. wsPort } ` ) ;
67- websocket . current = new WebSocket ( `ws://localhost:${ config ?. wsPort } ` ) ;
59+ console . log (
60+ `opening ws connection on localhost:${ config ?. monitorUISettings ?. wsPort } `
61+ ) ;
62+ websocket . current = new WebSocket (
63+ `ws://localhost:${ config ?. monitorUISettings ?. wsPort } `
64+ ) ;
6865 websocket . current . onmessage = ( res : any ) => {
69- const message : SerialPlotter . Protocol . Message = JSON . parse ( res . data ) ;
66+ const message : PluggableMonitor . Protocol . Message = JSON . parse ( res . data ) ;
7067 onMiddlewareMessage ( message ) ;
7168 } ;
7269 const wsCurrent = websocket . current ;
@@ -76,40 +73,53 @@ export default function App() {
7673 wsCurrent . close ( ) ;
7774 } ;
7875 // eslint-disable-next-line react-hooks/exhaustive-deps
79- } , [ config ?. wsPort ] ) ;
76+ } , [ config ?. monitorUISettings ?. wsPort ] ) ;
8077
8178 // at bootstrap read params from the URL
8279 useEffect ( ( ) => {
8380 const urlParams = new URLSearchParams ( window . location . search ) ;
8481
85- const urlSettings : SerialPlotter . Config = {
86- currentBaudrate : parseInt ( urlParams . get ( "currentBaudrate" ) || "9600" ) ,
87- currentLineEnding : urlParams . get ( "lineEnding" ) || "\n" ,
88- baudrates : ( urlParams . get ( "baudrates" ) || "" )
89- . split ( "," )
90- . map ( ( baud : string ) => parseInt ( baud ) ) ,
91- darkTheme : urlParams . get ( "darkTheme" ) === "true" ,
92- wsPort : parseInt ( urlParams . get ( "wsPort" ) || "3030" ) ,
93- interpolate : urlParams . get ( "interpolate" ) === "true" ,
94- serialPort : urlParams . get ( "serialPort" ) || "/serial/port/address" ,
95- connected : urlParams . get ( "connected" ) === "true" ,
96- generate : urlParams . get ( "generate" ) === "true" ,
82+ const urlSettings : MonitorSettings = {
83+ pluggableMonitorSettings : {
84+ baudrate : {
85+ id : "baudrate" ,
86+ label : "Baudrate" ,
87+ type : "enum" ,
88+ values : ( urlParams . get ( "baudrates" ) || "" ) . split ( "," ) ,
89+ selectedValue : urlParams . get ( "baudrate" ) || "9600" ,
90+ } ,
91+ } ,
92+ monitorUISettings : {
93+ lineEnding : isEOL ( urlParams . get ( "lineEnding" ) )
94+ ? ( urlParams . get ( "lineEnding" ) as EOL )
95+ : "\r\n" ,
96+ darkTheme : urlParams . get ( "darkTheme" ) === "true" ,
97+ wsPort : parseInt ( urlParams . get ( "wsPort" ) || "3030" ) ,
98+ interpolate : urlParams . get ( "interpolate" ) === "true" ,
99+ serialPort : urlParams . get ( "serialPort" ) || "/serial/port/address" ,
100+ connected : urlParams . get ( "connected" ) === "true" ,
101+ generate : urlParams . get ( "generate" ) === "true" ,
102+ } ,
97103 } ;
98104
99105 if ( config === null ) {
100106 onMiddlewareMessage ( {
101- command : SerialPlotter . Protocol . Command . MIDDLEWARE_CONFIG_CHANGED ,
107+ command :
108+ PluggableMonitor . Protocol . MiddlewareCommand . ON_SETTINGS_DID_CHANGE ,
102109 data : urlSettings ,
103110 } ) ;
104111 }
105112 } , [ config , onMiddlewareMessage ] ) ;
106113
107114 // If in "generate" mode, create fake data
108115 useEffect ( ( ) => {
109- if ( config ?. generate ) {
116+ if ( config ?. monitorUISettings ?. generate ) {
110117 const randomValuesInterval = setInterval ( ( ) => {
111118 const messages = namedVariablesMulti ( ) ;
112- onMiddlewareMessage ( messages ) ;
119+ onMiddlewareMessage ( {
120+ command : null ,
121+ data : messages ,
122+ } ) ;
113123 } , 32 ) ;
114124 return ( ) => {
115125 clearInterval ( randomValuesInterval ) ;
0 commit comments