|
| 1 | +import React, { Fragment } from 'react'; |
| 2 | +import { ValidatorForm } from 'react-material-ui-form-validator'; |
| 3 | + |
| 4 | +import { Table, TableBody, TableCell, TableHead, TableFooter, TableRow, withWidth, WithWidthProps, isWidthDown } from '@material-ui/core'; |
| 5 | +import { Box, Button, Typography, } from '@material-ui/core'; |
| 6 | + |
| 7 | +import EditIcon from '@material-ui/icons/Edit'; |
| 8 | +import DeleteIcon from '@material-ui/icons/Delete'; |
| 9 | +import CloseIcon from '@material-ui/icons/Close'; |
| 10 | +import CheckIcon from '@material-ui/icons/Check'; |
| 11 | +import IconButton from '@material-ui/core/IconButton'; |
| 12 | +import SaveIcon from '@material-ui/icons/Save'; |
| 13 | +import CompassCalibration from '@material-ui/icons/CompassCalibration'; |
| 14 | + |
| 15 | +import { withAuthenticatedContext, AuthenticatedContextProps } from '../authentication'; |
| 16 | +import { RestFormProps, FormActions, FormButton, extractEventValue } from '../components'; |
| 17 | + |
| 18 | +import SensorForm from './SensorForm'; |
| 19 | +import { SensorsSettings, Sensor } from './types'; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +function compareSensors(a: Sensor, b: Sensor) { |
| 25 | + if (a.name < b.name) { |
| 26 | + return -1; |
| 27 | + } |
| 28 | + if (a.name > b.name) { |
| 29 | + return 1; |
| 30 | + } |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +type ManageSensorsFormProps = RestFormProps<SensorsSettings> & AuthenticatedContextProps & WithWidthProps; |
| 35 | + |
| 36 | +type ManageSensorsFormState = { |
| 37 | + creating: boolean; |
| 38 | + sensor?: Sensor; |
| 39 | + driverlist?: string[]; |
| 40 | +} |
| 41 | + |
| 42 | +class ManageSensorsForm extends React.Component<ManageSensorsFormProps, ManageSensorsFormState> { |
| 43 | + |
| 44 | + state: ManageSensorsFormState = { |
| 45 | + creating: false |
| 46 | + }; |
| 47 | + |
| 48 | + createSensor = () => { |
| 49 | + this.setState({ |
| 50 | + creating: true, |
| 51 | + sensor: new Sensor() |
| 52 | + }); |
| 53 | + }; |
| 54 | + componentDidMount = ()=> { |
| 55 | + console.log("manage sensor mounted"); |
| 56 | + console.log(this.props.data.sensors); |
| 57 | + }; |
| 58 | + uniqueSensorname = (name: string) => { |
| 59 | + |
| 60 | + return this.props.data.sensors==undefined || !this.props.data.sensors.find(u => u.name === name); |
| 61 | + } |
| 62 | + |
| 63 | + // noAdminConfigured = () => { |
| 64 | + // return !this.props.data.sensors.find(u => u.enabled); |
| 65 | + // } |
| 66 | + |
| 67 | + removeSensor = (sensor: Sensor) => { |
| 68 | + const { data } = this.props; |
| 69 | + const sensors = data.sensors.filter(u => u.name !== sensor.name); |
| 70 | + this.props.setData({ ...data, sensors }); |
| 71 | + } |
| 72 | + |
| 73 | + startEditingSensor = (sensor: Sensor) => { |
| 74 | + this.setState({ |
| 75 | + creating: false, |
| 76 | + sensor: sensor |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + cancelEditingSensor = () => { |
| 81 | + this.setState({ |
| 82 | + sensor: undefined |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + doneEditingSensor = () => { |
| 87 | + const { sensor } = this.state; |
| 88 | + console.log("props before", this.props.data); |
| 89 | + |
| 90 | + if (sensor) { |
| 91 | + const { data } = this.props; |
| 92 | + const sensors = data.sensors.filter(u => u.name !== sensor.name); |
| 93 | + // sensors.push(sensor); |
| 94 | + sensors.push(JSON.parse(JSON.stringify(sensor)))//need a deep clone here |
| 95 | + this.props.setData({ ...data, sensors }); |
| 96 | + this.setState({ |
| 97 | + sensor: undefined |
| 98 | + }); |
| 99 | + } |
| 100 | + console.log("props before", this.props.data); |
| 101 | + }; |
| 102 | + |
| 103 | + handleSensorParamChange = (key: string) =>(event: React.ChangeEvent<HTMLInputElement>) => { |
| 104 | + let sensor ={ ...this.state.sensor!} |
| 105 | + let param = sensor.driver.config as Record<string, any> |
| 106 | + param[key]=extractEventValue(event) as string |
| 107 | + // param.set(key, extractEventValue(event) as string) |
| 108 | + this.setState( {sensor}) |
| 109 | + }; |
| 110 | + |
| 111 | + handleSensorDriverChange = () => (event: React.ChangeEvent<HTMLInputElement>) => { |
| 112 | + console.log("Now state is ",this.state) |
| 113 | + // const csensor = { ...this.state.sensor} |
| 114 | + |
| 115 | + // csensor["driver"] = this.props.data.drivers[+event.target.value] |
| 116 | + // this.setState({ csensor }) |
| 117 | + this.setState({ sensor: { ...this.state.sensor!, driver: this.props.data.drivers[+event.target.value] } }); |
| 118 | + console.log("Now state is ",this.state) |
| 119 | + }; |
| 120 | + |
| 121 | + |
| 122 | + handleSensorValueChange = (name: keyof Sensor) => (event: React.ChangeEvent<HTMLInputElement>) => { |
| 123 | + this.setState({ sensor: { ...this.state.sensor!, [name]: extractEventValue(event) } }); |
| 124 | + }; |
| 125 | + |
| 126 | + onSubmit = () => { |
| 127 | + this.props.saveData(); |
| 128 | + this.props.authenticatedContext.refresh(); |
| 129 | + } |
| 130 | + secondsToHms = (d:number) => { |
| 131 | + d = Number(d); |
| 132 | + var h = Math.floor(d / 3600); |
| 133 | + var m = Math.floor(d % 3600 / 60); |
| 134 | + var s = Math.floor(d % 3600 % 60); |
| 135 | + |
| 136 | + var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""; |
| 137 | + var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""; |
| 138 | + var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""; |
| 139 | + return hDisplay + mDisplay + sDisplay; |
| 140 | + } |
| 141 | + |
| 142 | + render() { |
| 143 | + const { width, data } = this.props; |
| 144 | + if (data.sensors == undefined) { |
| 145 | + data.sensors = []; |
| 146 | + } |
| 147 | + const { sensor, creating } = this.state; |
| 148 | + |
| 149 | + let table = <TableRow><TableCell colSpan={6}>No sensor defined</TableCell></TableRow>; |
| 150 | + |
| 151 | + return ( |
| 152 | + <Fragment> |
| 153 | + <ValidatorForm onSubmit={this.onSubmit}> |
| 154 | + <Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}> |
| 155 | + <TableHead> |
| 156 | + <TableRow> |
| 157 | + <TableCell>Name</TableCell> |
| 158 | + <TableCell align="center">Enabled</TableCell> |
| 159 | + <TableCell align="center">Interval</TableCell> |
| 160 | + <TableCell align="center">Driver</TableCell> |
| 161 | + <TableCell align="center">Params</TableCell> |
| 162 | + <TableCell /> |
| 163 | + </TableRow> |
| 164 | + </TableHead> |
| 165 | + <TableBody> |
| 166 | + |
| 167 | + |
| 168 | + {(data.sensors != null && data.sensors.length >0) ? |
| 169 | + data.sensors.sort(compareSensors).map(csensor => ( |
| 170 | + <TableRow key={csensor.name}> |
| 171 | + <TableCell component="th" scope="row"> |
| 172 | + {csensor.name} |
| 173 | + </TableCell> |
| 174 | + <TableCell align="center"> |
| 175 | + { |
| 176 | + csensor.enabled? <CheckIcon /> : <CloseIcon /> |
| 177 | + } |
| 178 | + </TableCell> |
| 179 | + <TableCell component="th" align="center" scope="row"> |
| 180 | + {this.secondsToHms(csensor.interval)} |
| 181 | + </TableCell> |
| 182 | + <TableCell component="th" scope="row"> |
| 183 | + {csensor.driver?.name} |
| 184 | + </TableCell> |
| 185 | + <TableCell component="th" scope="row"> |
| 186 | + <Table size="small" > |
| 187 | + <TableBody> |
| 188 | + <TableRow> |
| 189 | + |
| 190 | + {csensor.driver.config && Object.entries(csensor.driver.config as Object).map(([key ,value]) => { |
| 191 | + return <TableCell key={csensor.name + key}>{key}: {value}</TableCell> |
| 192 | + })} |
| 193 | + |
| 194 | + </TableRow> |
| 195 | + </TableBody> |
| 196 | + </Table> |
| 197 | + </TableCell> |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + <TableCell align="center"> |
| 202 | + <IconButton size="small" aria-label="Delete" onClick={() => this.removeSensor(csensor)}> |
| 203 | + <DeleteIcon /> |
| 204 | + </IconButton> |
| 205 | + <IconButton size="small" aria-label="Edit" onClick={() => this.startEditingSensor(csensor)}> |
| 206 | + <EditIcon /> |
| 207 | + </IconButton> |
| 208 | + </TableCell> |
| 209 | + </TableRow> |
| 210 | + )) |
| 211 | + : table |
| 212 | + } |
| 213 | + </TableBody> |
| 214 | + <TableFooter > |
| 215 | + <TableRow> |
| 216 | + <TableCell colSpan={5} /> |
| 217 | + <TableCell align="center" padding="default"> |
| 218 | + <Button startIcon={<CompassCalibration />} variant="contained" color="secondary" onClick={this.createSensor}> |
| 219 | + Add Sensor |
| 220 | + </Button> |
| 221 | + </TableCell> |
| 222 | + </TableRow> |
| 223 | + </TableFooter> |
| 224 | + </Table> |
| 225 | + { |
| 226 | + // this.noAdminConfigured() && |
| 227 | + // ( |
| 228 | + // <Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}> |
| 229 | + // <Typography variant="body1"> |
| 230 | + // You must have at least one admin sensor configured. |
| 231 | + // </Typography> |
| 232 | + // </Box> |
| 233 | + // ) |
| 234 | + } |
| 235 | + <FormActions> |
| 236 | + {/* <FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit" disabled={this.noAdminConfigured()}> */} |
| 237 | + <FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit" > |
| 238 | + Save |
| 239 | + </FormButton> |
| 240 | + </FormActions> |
| 241 | + </ValidatorForm> |
| 242 | + { |
| 243 | + sensor && |
| 244 | + <SensorForm |
| 245 | + sensor={sensor} |
| 246 | + driverlist={data.drivers} |
| 247 | + creating={creating} |
| 248 | + onDoneEditing={this.doneEditingSensor} |
| 249 | + onCancelEditing={this.cancelEditingSensor} |
| 250 | + handleDriverChange={this.handleSensorDriverChange} |
| 251 | + handleParamChange={this.handleSensorParamChange} |
| 252 | + handleValueChange={this.handleSensorValueChange} |
| 253 | + uniqueSensorname={this.uniqueSensorname} |
| 254 | + /> |
| 255 | + } |
| 256 | + </Fragment> |
| 257 | + ); |
| 258 | + } |
| 259 | + |
| 260 | +} |
| 261 | + |
| 262 | +export default withAuthenticatedContext(withWidth()(ManageSensorsForm)); |
0 commit comments