11import { Button , NumberInput , TextInput } from "@mantine/core" ;
2- import { Connect , Disconnect } from "../../wailsjs/go/main/App" ;
2+ import { SetUnitId , Connect , Disconnect } from "../../wailsjs/go/main/App" ;
33import { useState } from "react" ;
44import { notifications } from "@mantine/notifications" ;
55
66export default function ConnectForm ( props : { connected : boolean } ) {
77 const [ IP , setIP ] = useState < string > ( "" ) ;
88 const [ port , setPort ] = useState < string | number > ( 502 ) ;
9- const [ unitID , setUnitID ] = useState < number > ( 1 ) ;
9+ const [ unitID , setUnitID ] = useState < string | number > ( 1 ) ;
1010 function ConnectModbus ( ) {
1111 console . log ( "Connecting to" , IP , port , unitID ) ;
1212 let portNum = port as number ;
13- Connect ( IP , portNum , unitID )
14- . then ( ( ) => notifications . show ( {
15- title : "Connected" ,
16- message : ""
17- } ) )
18- . catch ( ( err ) => notifications . show ( {
19- title : "Failed to Connect" ,
20- message : err ,
21- autoClose : 20000
22- } ) )
13+ let numId = unitID as number ;
14+ Connect ( IP , portNum , numId )
15+ . then ( ( ) =>
16+ notifications . show ( {
17+ title : "Connected" ,
18+ message : "" ,
19+ } )
20+ )
21+ . catch ( ( err ) =>
22+ notifications . show ( {
23+ title : "Failed to Connect" ,
24+ message : err ,
25+ autoClose : 20000 ,
26+ } )
27+ ) ;
2328 }
2429 return (
2530 < >
26- < TextInput label = "IP Address" placeholder = "192.168.1.100" onChange = { ( event ) => setIP ( event . currentTarget . value ) } />
27- < NumberInput label = "Port" value = { 502 } onChange = { setPort } />
28- { /* <NumberInput label="Unit ID" value={1} /> */ }
29- {
30- ! props . connected ?
31- < Button onClick = { ConnectModbus } > Connect</ Button > : < Button onClick = { Disconnect } > Disconnect</ Button >
32- }
31+ < TextInput
32+ label = "IP Address"
33+ placeholder = "192.168.1.100"
34+ onChange = { ( event ) => setIP ( event . currentTarget . value ) }
35+ disabled = { props . connected }
36+ />
37+ < NumberInput
38+ label = "Port"
39+ value = { port }
40+ onChange = { setPort }
41+ disabled = { props . connected }
42+ />
43+ < NumberInput
44+ label = "Unit ID"
45+ value = { unitID }
46+ onChange = { ( v ) => {
47+ setUnitID ( v ) ;
48+ SetUnitId ( v as number )
49+ . then ( ( ) =>
50+ notifications . show ( {
51+ title : "Set Unit ID" ,
52+ message : "" ,
53+ } )
54+ )
55+ . catch ( ( err ) =>
56+ notifications . show ( {
57+ title : "Failed to Set ID" ,
58+ message : err ,
59+ } )
60+ ) ;
61+ } }
62+ />
63+ { ! props . connected ? (
64+ < Button onClick = { ConnectModbus } > Connect</ Button >
65+ ) : (
66+ < Button onClick = { Disconnect } > Disconnect</ Button >
67+ ) }
3368 </ >
34- )
35- }
69+ ) ;
70+ }
0 commit comments