connected the api to the button to update the controller state
This commit is contained in:
parent
a9f5b9594b
commit
b992b6785c
2 changed files with 44 additions and 21 deletions
|
|
@ -4,7 +4,7 @@ import { makeStyles } from "@mui/styles";
|
||||||
import { Box, Button, DialogActions, DialogContent, DialogTitle, Typography } from "@mui/material";
|
import { Box, Button, DialogActions, DialogContent, DialogTitle, Typography } from "@mui/material";
|
||||||
import AerationFanIcon from "products/AgIcons/AerationFanIcon";
|
import AerationFanIcon from "products/AgIcons/AerationFanIcon";
|
||||||
import { quack } from "protobuf-ts/quack";
|
import { quack } from "protobuf-ts/quack";
|
||||||
import { useComponentAPI, useMobile } from "hooks";
|
import { useComponentAPI, useMobile, useSnackbar } from "hooks";
|
||||||
import ButtonGroup from "common/ButtonGroup";
|
import ButtonGroup from "common/ButtonGroup";
|
||||||
import ObjectHeaterIcon from "products/Construction/ObjectHeaterIcon";
|
import ObjectHeaterIcon from "products/Construction/ObjectHeaterIcon";
|
||||||
import ResponsiveDialog from "common/ResponsiveDialog";
|
import ResponsiveDialog from "common/ResponsiveDialog";
|
||||||
|
|
@ -13,6 +13,7 @@ import ResponsiveDialog from "common/ResponsiveDialog";
|
||||||
interface Props {
|
interface Props {
|
||||||
deviceID: number
|
deviceID: number
|
||||||
component: Component
|
component: Component
|
||||||
|
currentState?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const useStyles = makeStyles(() => {
|
const useStyles = makeStyles(() => {
|
||||||
|
|
@ -27,10 +28,11 @@ const useStyles = makeStyles(() => {
|
||||||
})
|
})
|
||||||
|
|
||||||
export default function BinControllerDisplay(props: Props) {
|
export default function BinControllerDisplay(props: Props) {
|
||||||
const {component, deviceID} = props
|
const {component, deviceID, currentState} = props
|
||||||
const [controllerState, setControllerState] = useState(0)
|
const [controllerState, setControllerState] = useState(0)
|
||||||
const [openDialog, setOpenDialog] = useState(false)
|
const [openDialog, setOpenDialog] = useState(false)
|
||||||
const [newOutputState, setNewOutputState] = useState(0)
|
const [newOutputState, setNewOutputState] = useState(0)
|
||||||
|
const {openSnack} = useSnackbar()
|
||||||
const componentAPI = useComponentAPI()
|
const componentAPI = useComponentAPI()
|
||||||
const isMobile = useMobile()
|
const isMobile = useMobile()
|
||||||
const classes = useStyles()
|
const classes = useStyles()
|
||||||
|
|
@ -51,6 +53,14 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
const setController = () => {
|
const setController = () => {
|
||||||
let newSettings = component.settings
|
let newSettings = component.settings
|
||||||
newSettings.defaultOutputState = newOutputState
|
newSettings.defaultOutputState = newOutputState
|
||||||
|
componentAPI.update(deviceID, newSettings).then(resp => {
|
||||||
|
openSnack("Updated controllers output state")
|
||||||
|
setControllerState(newOutputState)
|
||||||
|
}).catch(err => {
|
||||||
|
openSnack("There was a problem updating the controller")
|
||||||
|
}).finally(() => {
|
||||||
|
setOpenDialog(false)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const controllerDialog = () => {
|
const controllerDialog = () => {
|
||||||
|
|
@ -62,15 +72,17 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
<Button onClick={()=>{setOpenDialog(false)}}>Cancel</Button>
|
<Button onClick={()=>{setOpenDialog(false)}}>Cancel</Button>
|
||||||
<Button onClick={()=>{setController()}}>Confirm</Button>
|
<Button onClick={()=>{setController()}} variant="contained" color="primary">Confirm</Button>
|
||||||
</DialogActions>
|
</DialogActions>
|
||||||
</ResponsiveDialog>
|
</ResponsiveDialog>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const componentOn = () => {
|
const componentOn = () => {
|
||||||
|
if(currentState){
|
||||||
|
return currentState
|
||||||
|
}else{
|
||||||
let state = false
|
let state = false
|
||||||
console.log(component.status)
|
|
||||||
component.status.lastGoodMeasurement.forEach(um => {
|
component.status.lastGoodMeasurement.forEach(um => {
|
||||||
if(um.type === quack.MeasurementType.MEASUREMENT_TYPE_BOOLEAN){
|
if(um.type === quack.MeasurementType.MEASUREMENT_TYPE_BOOLEAN){
|
||||||
if(um.values.length > 0){
|
if(um.values.length > 0){
|
||||||
|
|
@ -80,21 +92,27 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
if (nodes[nodes.length -1] === 1){
|
if (nodes[nodes.length -1] === 1){
|
||||||
state = true
|
state = true
|
||||||
}
|
}
|
||||||
//um.values[um.values.length-1].values[um.values[um.values.length-1].values.length-1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className={classes.controllerDisplay} key={component.key()}>
|
<React.Fragment>
|
||||||
|
{controllerDialog()}
|
||||||
|
<Box className={classes.controllerDisplay} key={component.key()} marginTop={1} marginBottom={1}>
|
||||||
<Box display="flex" marginY="auto">
|
<Box display="flex" marginY="auto">
|
||||||
|
<Box margin="auto">
|
||||||
{controllerIcon()}
|
{controllerIcon()}
|
||||||
|
</Box>
|
||||||
<Typography
|
<Typography
|
||||||
align="center"
|
align="center"
|
||||||
style={{
|
style={{
|
||||||
|
marginBottom: "auto",
|
||||||
|
marginTop: "auto",
|
||||||
paddingLeft: 5,
|
paddingLeft: 5,
|
||||||
fontSize: isMobile ? "0.85rem" : "1.0rem",
|
fontSize: isMobile ? "0.85rem" : "1.0rem",
|
||||||
fontWeight: 650
|
fontWeight: 650
|
||||||
|
|
@ -102,6 +120,7 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
{component.name()}
|
{component.name()}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
<Box display="flex">
|
||||||
<ButtonGroup
|
<ButtonGroup
|
||||||
buttons={[
|
buttons={[
|
||||||
{
|
{
|
||||||
|
|
@ -131,6 +150,8 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
/>
|
/>
|
||||||
<Typography
|
<Typography
|
||||||
style={{
|
style={{
|
||||||
|
minWidth: 30,
|
||||||
|
marginLeft: "10px",
|
||||||
marginBottom: "auto",
|
marginBottom: "auto",
|
||||||
marginTop: "auto",
|
marginTop: "auto",
|
||||||
fontSize: isMobile ? "0.85rem" : "1.0rem",
|
fontSize: isMobile ? "0.85rem" : "1.0rem",
|
||||||
|
|
@ -140,6 +161,8 @@ export default function BinControllerDisplay(props: Props) {
|
||||||
{componentOn() ? "ON" : "OFF"}
|
{componentOn() ? "ON" : "OFF"}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
</Box>
|
||||||
|
</React.Fragment>
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1556,7 +1556,7 @@ export default function BinVisualizer(props: Props) {
|
||||||
let device = componentDevices.get(fan.key)
|
let device = componentDevices.get(fan.key)
|
||||||
if(fanComp && device){
|
if(fanComp && device){
|
||||||
return (
|
return (
|
||||||
<BinControllerDisplay component={fanComp} deviceID={device}/>
|
<BinControllerDisplay component={fanComp} deviceID={device} currentState/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
|
|
@ -1565,7 +1565,7 @@ export default function BinVisualizer(props: Props) {
|
||||||
let device = componentDevices.get(heater.key)
|
let device = componentDevices.get(heater.key)
|
||||||
if(heaterComp && device){
|
if(heaterComp && device){
|
||||||
return (
|
return (
|
||||||
<BinControllerDisplay component={heaterComp} deviceID={device}/>
|
<BinControllerDisplay component={heaterComp} deviceID={device} currentState={heater.state}/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue