|
| 1 | +import { Button, Grid, makeStyles, TextField } from '@material-ui/core'; |
| 2 | +import React, { FunctionComponent, useEffect, useState } from 'react'; |
| 3 | +import Omnibox, { Option } from '../Omnibox'; |
| 4 | +import { |
| 5 | + SerialPortInformation, |
| 6 | + useAvailableDevicesListQuery, |
| 7 | +} from '../../gql/generated/types'; |
| 8 | +import Loader from '../Loader'; |
| 9 | +import ShowAlerts from '../ShowAlerts'; |
| 10 | + |
| 11 | +const useStyles = makeStyles((theme) => ({ |
| 12 | + root: { |
| 13 | + marginTop: theme.spacing(2), |
| 14 | + marginBottom: theme.spacing(2), |
| 15 | + }, |
| 16 | + loader: { |
| 17 | + marginTop: theme.spacing(2), |
| 18 | + marginBottom: theme.spacing(2), |
| 19 | + }, |
| 20 | + button: { |
| 21 | + paddingTop: '15px !important', |
| 22 | + paddingBottom: '15px !important', |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +interface SerialConnectionFormProps { |
| 27 | + serialDevice: string | null; |
| 28 | + baudRate: number; |
| 29 | + onConnect: (serialDevice: string | null, baudRate: number) => void; |
| 30 | +} |
| 31 | + |
| 32 | +const SerialConnectionForm: FunctionComponent<SerialConnectionFormProps> = ( |
| 33 | + props |
| 34 | +) => { |
| 35 | + const { onConnect, serialDevice, baudRate } = props; |
| 36 | + const styles = useStyles(); |
| 37 | + |
| 38 | + const { loading, data, error, previousData } = useAvailableDevicesListQuery({ |
| 39 | + pollInterval: 1000, |
| 40 | + }); |
| 41 | + const options: Option[] = |
| 42 | + data?.availableDevicesList?.map((target) => ({ |
| 43 | + label: `${target.path} ${target.manufacturer}`, |
| 44 | + value: target.path, |
| 45 | + })) ?? []; |
| 46 | + |
| 47 | + const [currentBaud, setCurrentBaud] = useState<number>(baudRate); |
| 48 | + const [currentValue, setCurrentValue] = useState<Option | null>( |
| 49 | + serialDevice |
| 50 | + ? { |
| 51 | + label: serialDevice, |
| 52 | + value: serialDevice, |
| 53 | + } |
| 54 | + : null |
| 55 | + ); |
| 56 | + const onSerialDeviceChange = (value: string | null) => { |
| 57 | + if (value === null) { |
| 58 | + setCurrentValue(null); |
| 59 | + } else { |
| 60 | + setCurrentValue({ |
| 61 | + label: value, |
| 62 | + value, |
| 63 | + }); |
| 64 | + } |
| 65 | + }; |
| 66 | + const onBaudChange = ( |
| 67 | + event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement> |
| 68 | + ) => { |
| 69 | + if (event.target.validity.valid) { |
| 70 | + try { |
| 71 | + const value = parseInt(event.target.value, 10); |
| 72 | + setCurrentBaud(value); |
| 73 | + } catch (e) { |
| 74 | + console.error('failed to parse number', e); |
| 75 | + } |
| 76 | + } else { |
| 77 | + console.log('only numbers'); |
| 78 | + } |
| 79 | + }; |
| 80 | + useEffect(() => { |
| 81 | + const difference = ( |
| 82 | + a: readonly Pick<SerialPortInformation, 'path' | 'manufacturer'>[], |
| 83 | + b: readonly Pick<SerialPortInformation, 'path' | 'manufacturer'>[] |
| 84 | + ): Pick<SerialPortInformation, 'path' | 'manufacturer'>[] => { |
| 85 | + return a.filter((item) => { |
| 86 | + return b.find((item2) => item2.path === item.path) === undefined; |
| 87 | + }); |
| 88 | + }; |
| 89 | + if (currentValue === null) { |
| 90 | + const added = difference( |
| 91 | + data?.availableDevicesList ?? [], |
| 92 | + previousData?.availableDevicesList ?? [] |
| 93 | + ); |
| 94 | + if (added.length > 0) { |
| 95 | + onSerialDeviceChange(added[0].path); |
| 96 | + } |
| 97 | + } else { |
| 98 | + const added = difference( |
| 99 | + data?.availableDevicesList ?? [], |
| 100 | + previousData?.availableDevicesList ?? [] |
| 101 | + ); |
| 102 | + if (added.length > 0) { |
| 103 | + onSerialDeviceChange(added[0].path); |
| 104 | + return; |
| 105 | + } |
| 106 | + const removed = difference( |
| 107 | + previousData?.availableDevicesList ?? [], |
| 108 | + data?.availableDevicesList ?? [] |
| 109 | + ); |
| 110 | + if ( |
| 111 | + removed.find((item) => item.path === currentValue.value) !== |
| 112 | + undefined && |
| 113 | + data?.availableDevicesList && |
| 114 | + data?.availableDevicesList.length > 0 |
| 115 | + ) { |
| 116 | + onSerialDeviceChange(data?.availableDevicesList[0].path); |
| 117 | + } |
| 118 | + } |
| 119 | + }, [data, previousData]); |
| 120 | + const onSubmit = () => { |
| 121 | + if (currentValue !== null) { |
| 122 | + console.log('currentValue', currentValue); |
| 123 | + onConnect(currentValue?.value, baudRate); |
| 124 | + } |
| 125 | + }; |
| 126 | + return ( |
| 127 | + <div className={styles.root}> |
| 128 | + <Grid container spacing={2}> |
| 129 | + <Grid item xs={3}> |
| 130 | + <Omnibox |
| 131 | + title="Serial device" |
| 132 | + currentValue={currentValue} |
| 133 | + onChange={onSerialDeviceChange} |
| 134 | + options={options} |
| 135 | + loading={loading} |
| 136 | + /> |
| 137 | + <Loader className={styles.loader} loading={loading} /> |
| 138 | + <ShowAlerts severity="error" messages={error} /> |
| 139 | + </Grid> |
| 140 | + <Grid item> |
| 141 | + <TextField |
| 142 | + size="medium" |
| 143 | + onBlur={onBaudChange} |
| 144 | + defaultValue={currentBaud} |
| 145 | + fullWidth |
| 146 | + inputProps={{ |
| 147 | + min: 0, |
| 148 | + type: 'number', |
| 149 | + step: '1', |
| 150 | + }} |
| 151 | + label="Baud rate" |
| 152 | + /> |
| 153 | + </Grid> |
| 154 | + <Grid item> |
| 155 | + <Button |
| 156 | + onClick={onSubmit} |
| 157 | + size="large" |
| 158 | + variant="contained" |
| 159 | + className={styles.button} |
| 160 | + > |
| 161 | + Connect |
| 162 | + </Button> |
| 163 | + </Grid> |
| 164 | + </Grid> |
| 165 | + </div> |
| 166 | + ); |
| 167 | +}; |
| 168 | + |
| 169 | +export default SerialConnectionForm; |
0 commit comments