From 35d553dab2b6f467ef049b8210ef2b27484480a3 Mon Sep 17 00:00:00 2001 From: leandrofars Date: Sun, 29 Oct 2023 23:55:03 -0300 Subject: [PATCH] feat(frontend): device obj add --- .../src/sections/devices/devices-discovery.js | 142 ++++++++++++++++-- 1 file changed, 127 insertions(+), 15 deletions(-) diff --git a/frontend/src/sections/devices/devices-discovery.js b/frontend/src/sections/devices/devices-discovery.js index cd93e42..721999c 100644 --- a/frontend/src/sections/devices/devices-discovery.js +++ b/frontend/src/sections/devices/devices-discovery.js @@ -17,6 +17,7 @@ import { Button, Backdrop, } from '@mui/material'; +import ArrowRightIcon from '@heroicons/react/24/solid/ArrowRightIcon'; import CircularProgress from '@mui/material/CircularProgress'; import PlusCircleIcon from '@heroicons/react/24/outline/PlusCircleIcon'; import Pencil from "@heroicons/react/24/outline/PencilIcon" @@ -24,8 +25,25 @@ import ArrowUturnLeftIcon from '@heroicons/react/24/outline/ArrowUturnLeftIcon' import XMarkIcon from '@heroicons/react/24/outline/XMarkIcon'; import { useRouter } from 'next/router'; +import TrashIcon from '@heroicons/react/24/outline/TrashIcon'; -const AccessType = { +/* + OBJ_READ_ONLY (0) + OBJ_ADD_DELETE (1) + OBJ_ADD_ONLY (2) + OBJ_DELETE_ONLY (3) + +*/ + +const ObjAccessType = { + ReadOnly: 0, + AddDelete: 1, + AddOnly: 2, + DeleteOnly:3, +} + + +const ParamAccessType = { ReadOnly: 0, ReadWrite: 1, WriteOnly: 2, @@ -44,6 +62,110 @@ const ParamValueType = { UnisgnedInt: 9, UnsignedLong: 10, } +//TODO: refact all of this mess +const addDeviceObj = async(obj, setShowLoading, router, updateDeviceParameters) => { + console.log("AddDeviceObj => obj = ", obj) + let raw = JSON.stringify( + { + "allow_partial": true, + "create_objs": [ + { + "obj_path": obj, + //TODO: create queue and create new obj with parameters + // "param_settings": [ + // { + // "param": "Alias", + // "value": "test", + // "required": true + // } + // ] + } + ] + } + ) + 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 + }; + setShowLoading(true) + let result = await (await fetch(`${process.env.NEXT_PUBLIC_REST_ENPOINT}/device/${router.query.id[0]}/add`, requestOptions)) + if (result.status != 200) { + setShowLoading(false) + throw new Error('Please check your email and password'); + }else{ + setShowLoading(false) + console.log(result.json()) + updateDeviceParameters(obj+"*.") + } +} + +function ShowPath({x,updateDeviceParameters,setShowLoading, router}) { + console.log(x) + console.log("x.supported_obj_path:", x.supported_obj_path) + console.log("x.access:", x.access) + if(x.supported_obj_path != "Device."){ + if (x.access === ObjAccessType.ReadOnly || x.access === undefined){ + return ( + { + console.log("x.supported_obj_path:",x.supported_obj_path) + let supported_obj_path = x.supported_obj_path.replaceAll("{i}.","*.") + updateDeviceParameters(supported_obj_path) + }}> + + + + ) + }else if (x.access === ObjAccessType.AddDelete){ + console.log("addDelete") + return ( +
+ + addDeviceObj( + x.supported_obj_path.replace("{i}.",""), + setShowLoading, router, updateDeviceParameters)}> + + + + + + + + + { + console.log("x.supported_obj_path:",x.supported_obj_path) + let supported_obj_path = x.supported_obj_path.replaceAll("{i}.","*.") + updateDeviceParameters(supported_obj_path) + }}> + + + + +
+ ) + }else if (x.access === ObjAccessType.AddOnly){ + return + addDeviceObj(x.supported_obj_path.replace("{i}.",""), + setShowLoading, router, updateDeviceParameters)}> + + + + }else if (x.access === ObjAccessType.DeleteOnly){ + return + + + + + } + } + return <> +} function ShowParamsWithValues({x, deviceParametersValue, setOpen, setParameter, setParameterValue}) { console.log("HEY jow:", deviceParametersValue) @@ -95,7 +217,7 @@ function ShowParamsWithValues({x, deviceParametersValue, setOpen, setParameter, secondaryAction={
{Object.values(param)[0].value} - {Object.values(param)[0].access > AccessType.ReadOnly && + {Object.values(param)[0].access > ParamAccessType.ReadOnly && { showDialog( @@ -140,7 +262,7 @@ function ShowParamsWithValues({x, deviceParametersValue, setOpen, setParameter, secondaryAction={
{deviceParametersValue[y.param_name].value} - {deviceParametersValue[y.param_name].access > AccessType.ReadOnly && + {deviceParametersValue[y.param_name].access > ParamAccessType.ReadOnly && { showDialog( @@ -347,6 +469,7 @@ const getDeviceParameterInstances = async (raw) =>{ // setDeviceParametersValue(values) } + const updateDeviceParameters = async (param) => { console.log("UpdateDeviceParameters => param = ", param) let raw = JSON.stringify({ @@ -544,18 +667,7 @@ const getDeviceParameterInstances = async (raw) =>{ : - { - console.log("x.supported_obj_path:",x.supported_obj_path) - let supported_obj_path = x.supported_obj_path.replaceAll("{i}.","*.") - updateDeviceParameters(supported_obj_path) - }}> - - { - x.supported_obj_path != "Device." && - - } - - + } sx={{ boxShadow: 'rgba(149, 157, 165, 0.2) 0px 0px 5px;'