From 71cc19b807edf257f31329a58eef568cc935ab14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20Ant=C3=B4nio=20Farias=20Machado?= Date: Sun, 25 Jun 2023 23:35:52 -0300 Subject: [PATCH] feat(frontend): new components for new device remote calls via front --- .../src/sections/devices/devices-discovery.js | 169 ++++++++++++++++++ frontend/src/sections/devices/devices-rpc.js | 2 +- .../overview/overview-latest-orders.js | 2 +- 3 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 frontend/src/sections/devices/devices-discovery.js diff --git a/frontend/src/sections/devices/devices-discovery.js b/frontend/src/sections/devices/devices-discovery.js new file mode 100644 index 0000000..d1822bc --- /dev/null +++ b/frontend/src/sections/devices/devices-discovery.js @@ -0,0 +1,169 @@ +import { useEffect, useState } from 'react'; +import { + Card, + CardContent, + SvgIcon, + IconButton, + List, + ListItem, + ListItemText, + Collapse, + Box, + Tabs, + Tab +} from '@mui/material'; +import CircularProgress from '@mui/material/CircularProgress'; +import { useRouter } from 'next/router'; + + +export const DevicesDiscovery = () => { + +const router = useRouter() + +const [deviceParameters, setDeviceParameters] = useState(null) + +const initialize = async (raw) => { + let content = await getDeviceParameters(raw) + setDeviceParameters(content) +} + +const getDeviceParameters = async (raw) =>{ + var myHeaders = new Headers(); + myHeaders.append("Content-Type", "application/json"); + myHeaders.append("Authorization", localStorage.getItem("token")); + + var requestOptions = { + method: 'PUT', + headers: myHeaders, + redirect: 'follow', + body: raw + }; + + let result = await (await fetch(`${process.env.NEXT_PUBLIC_REST_ENPOINT}/device/${router.query.id[0]}/parameters`, requestOptions)) + if (result.status != 200) { + throw new Error('Please check your email and password'); + }else{ + return result.json() + } +} + + useEffect(()=> { + + initialize( + JSON.stringify({ + "obj_paths": ["Device."], + "first_level_only" : false, + "return_commands" : false, + "return_events" : false, + "return_params" : true + }) + ); + },[]) + +//Together with showParameters, this function renders all the device parameters the device supports +//but you must set req with first_level_only property to false +// const showPathParameters = (pathParamsList) => { +// return pathParamsList.map((x,i)=>{ +// return( +// +// +// +// +// +// ) +// }) +// } + +// const updateDeviceParameters = async (param,) => { + +// let raw = JSON.stringify({ +// "obj_paths": [param], +// "first_level_only" : true, +// "return_commands" : false, +// "return_events" : false, +// "return_params" : true +// }) + +// let content = await getDeviceParameters(raw) + +// console.log(content) + +// setDeviceParameters(prevState => { +// return {...prevState, req_obj_results: [ +// { +// supported_objs:[ ...prevState.req_obj_results[0]. +// supported_objs][index] +// .supported_params:[] +// } +// ] +// } +// }) +// } + + const showParameters = () => { + console.log(deviceParameters) + return deviceParameters.req_obj_results[0].supported_objs.map((x,i)=> { + return ( + + updateDeviceParameters(x.supported_obj_path, index)}*/> + // + // + // + // + // } + sx={{ + boxShadow: 'rgba(149, 157, 165, 0.2) 0px 0px 5px;' + }} + > + {x.supported_obj_path}} + sx={{fontWeight:'bold'}} + /> + + { x.supported_params && + x.supported_params.map((y)=>{ + return + + + + + }) + } + ) + }) + } + + return ( deviceParameters ? + + + {showParameters()} + + + : + + + + ) +}; diff --git a/frontend/src/sections/devices/devices-rpc.js b/frontend/src/sections/devices/devices-rpc.js index 48d02f4..f1275b0 100644 --- a/frontend/src/sections/devices/devices-rpc.js +++ b/frontend/src/sections/devices/devices-rpc.js @@ -84,7 +84,7 @@ const handleOpen = () => { } - fetch(`${process.env.NEXT_PUBLIC_REST_ENPOINT}/device/${router.query.id}/${method}`, requestOptions) + fetch(`${process.env.NEXT_PUBLIC_REST_ENPOINT}/device/${router.query.id[0]}/${method}`, requestOptions) .then(response => response.text()) .then(result => { setOpen(false) diff --git a/frontend/src/sections/overview/overview-latest-orders.js b/frontend/src/sections/overview/overview-latest-orders.js index f16c235..cb33731 100644 --- a/frontend/src/sections/overview/overview-latest-orders.js +++ b/frontend/src/sections/overview/overview-latest-orders.js @@ -102,7 +102,7 @@ export const OverviewLatestOrders = (props) => { sx={{cursor:'pointer'}} onClick={()=>{ if (order.Status !== 2){ - router.push("devices/"+order.SN) + router.push("devices/"+order.SN+"/discovery") } } }