diff --git a/frontend/src/sections/devices/devices-discovery.js b/frontend/src/sections/devices/devices-discovery.js index e175a58..88b9c9b 100644 --- a/frontend/src/sections/devices/devices-discovery.js +++ b/frontend/src/sections/devices/devices-discovery.js @@ -7,16 +7,11 @@ import { List, ListItem, ListItemText, - Collapse, Box, - Tabs, - Tab, - ListItemIcon, - ListItemAvatar, - Avatar } from '@mui/material'; import CircularProgress from '@mui/material/CircularProgress'; import PlusCircleIcon from '@heroicons/react/24/outline/PlusCircleIcon'; +import ArrowUturnLeftIcon from '@heroicons/react/24/outline/ArrowUturnLeftIcon' import { useRouter } from 'next/router'; @@ -25,6 +20,7 @@ export const DevicesDiscovery = () => { const router = useRouter() const [deviceParameters, setDeviceParameters] = useState(null) +const [deviceParametersValue, setDeviceParametersValue] = useState([]) const initialize = async (raw) => { let content = await getDeviceParameters(raw) @@ -57,8 +53,8 @@ const getDeviceParameters = async (raw) =>{ JSON.stringify({ "obj_paths": ["Device."], "first_level_only" : true, - "return_commands" : false, - "return_events" : false, + "return_commands" : true, + "return_events" : true, "return_params" : true }) ); @@ -99,13 +95,65 @@ const getDeviceParameters = async (raw) =>{ let content = await getDeviceParameters(raw) - console.log(content) + console.log("content:",content) setDeviceParameters(content) + + let supportedParams = content.req_obj_results[0].supported_objs[0].supported_params + let parametersToFetch = () => { + let paramsToFetch = [] + for (let i =0; i < supportedParams.length ;i++){ + paramsToFetch.push(content.req_obj_results[0].supported_objs[0].supported_obj_path+supportedParams[i].param_name) + } + return paramsToFetch + } + + if (supportedParams !== undefined) { + const fetchparameters = parametersToFetch() + console.log("parameters to fetch: ", fetchparameters) + + raw = JSON.stringify({ + "param_paths": fetchparameters, + "max_depth": 1 + }) + + let result = await getDeviceParametersValue(raw) + console.log("result:", result) + + let values = [] + let setValues = result.req_path_results.map((x)=>{ + let path = x.requested_path.split(".") + let param = path[path.length -1] + return values.push(x.resolved_path_results[0].result_params[param]) + }) + console.log(values) + setDeviceParametersValue(values) + } + } + + const getDeviceParametersValue = 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]}/get`, requestOptions)) + if (result.status != 200) { + throw new Error('Please check your email and password'); + }else{ + return result.json() + } + } const showParameters = () => { - console.log(deviceParameters) return deviceParameters.req_obj_results[0].supported_objs.map((x,i)=> { return ( @@ -113,9 +161,25 @@ const getDeviceParameters = async (raw) =>{ key={x.supported_obj_path} divider={true} secondaryAction={ + i == 0 && x.supported_obj_path != "Device." ? + + { + let paths = x.supported_obj_path.split(".") + console.log(paths) + updateDeviceParameters(paths[paths.length -3]+".") + } + }> + + + + + : updateDeviceParameters(x.supported_obj_path)}> + { + x.supported_obj_path != "Device." && + } } @@ -129,7 +193,7 @@ const getDeviceParameters = async (raw) =>{ /> { x.supported_params && - x.supported_params.map((y)=>{ + x.supported_params.map((y, index)=>{ return { boxShadow: 'rgba(149, 157, 165, 0.2) 0px 0px 5px;', pl: 4 }} + secondaryAction={ +
{deviceParametersValue[index]}
+ } >