import { useCallback, useEffect, useState } from 'react'; import { Button, Card, CardActions, CardContent, CardHeader, Divider, Stack, TextField, InputLabel, MenuItem, Select, FormControl, SvgIcon, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Box, IconButton, Grid } from '@mui/material'; import XMarkIcon from '@heroicons/react/24/outline/XMarkIcon'; import PaperAirplane from '@heroicons/react/24/solid/PaperAirplaneIcon'; import CircularProgress from '@mui/material/CircularProgress'; import Backdrop from '@mui/material/Backdrop'; import { useRouter } from 'next/router'; import { useBackendContext } from 'src/contexts/backend-context'; import DocumentArrowDown from '@heroicons/react/24/outline/DocumentArrowDownIcon'; import TrashIcon from '@heroicons/react/24/outline/TrashIcon'; import PlusCircleIcon from '@heroicons/react/24/outline/PlusCircleIcon'; import EnvelopeIcon from '@heroicons/react/24/outline/EnvelopeIcon'; import CheckIcon from '@heroicons/react/24/outline/CheckIcon'; export const DevicesRPC = () => { const router = useRouter() let { httpRequest } = useBackendContext() var prettifyXml = function(sourceXml) { var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml'); var xsltDoc = new DOMParser().parseFromString([ // describes how we want to modify the XML - indent everything '', ' ', ' ', // change to just text() to strip space in text nodes ' ', ' ', ' ', ' ', ' ', ' ', '', ].join('\n'), 'application/xml'); var xsltProcessor = new XSLTProcessor(); xsltProcessor.importStylesheet(xsltDoc); var resultDoc = xsltProcessor.transformToDocument(xmlDoc); var resultXml = new XMLSerializer().serializeToString(resultDoc); return resultXml; }; const [open, setOpen] = useState(false); const [scroll, setScroll] = useState('paper'); const [answer, setAnswer] = useState(false) const [content, setContent] = useState('') const [age, setAge] = useState(6); const [newMessage, setNewMessage] = useState(false) const [message, setMessage] = useState(null) const [currentMsg, setCurrentMsg] = useState(0) const [newMsgName, setNewMsgName] = useState("") const [value, setValue] = useState() const [saveChanges, setSaveChanges] = useState(false) const [loadingSaveMsg, setLoadingSaveMsg] = useState(false) const possibleMsgs = [ ` InternetGatewayDevice.TraceRouteDiagnostics.Host 192.168.60.4 `, ` InternetGatewayDevice.LANDevice.1.WLANConfiguration.2. `, ` InternetGatewayDevice.LANDevice.1.WLANConfiguration. `, ` 007 `, ` InternetGatewayDevice.LANDevice.1.WLANConfiguration.1. `,` InternetGatewayDevice. 1 `, ` InternetGatewayDevice.LANDevice.1.WLANConfiguration. `] const [newMsgValue, setNewMsgValue] = useState(possibleMsgs[age-1]) const [loading, setLoading] = useState(false); const handleNewMessageValue = (event) => { setNewMsgValue(event.target.value) } const handleClose = () => { setOpen(false); }; const handleCancelNewMsgTemplate = () => { setNewMessage(false) setNewMsgName("") setNewMsgValue(possibleMsgs[age-1]) // setValue(possibleMsgs[age-1]) } const saveMsg = async () => { let {status} = await httpRequest( `/api/device/message?name=`+message[currentMsg].name, "PUT", value, null, ) if ( status === 204){ setSaveChanges(false) setMessage(message.map((msg, index) => { if (index === currentMsg) { return {...msg, value: value} }else{ return msg } })) } } const createNewMsg = async () => { setLoading(true) let {status} = await httpRequest( `/api/device/message/cwmp?name=`+newMsgName, "POST", newMsgValue, null, ) if ( status === 204){ setNewMessage(false) setNewMsgName("") let result = await fetchMessages() if (result) { setCurrentMsg(result.length-1) } setValue(newMsgValue) setNewMsgValue(possibleMsgs[age-1]) } setLoading(false) } const handleChangeMessage = (event) => { setSaveChanges(false) setCurrentMsg(event.target.value) setValue(message[event.target.value].value) } const handleDeleteMessage = async () => { let {status} = await httpRequest( `/api/device/message?name=`+message[currentMsg].name.replace(" ", '+'), "DELETE", ) if ( status === 204){ fetchMessages() setCurrentMsg(0) setValue("") } } const handleOpen = async () => { setOpen(true); let {result, status} = await httpRequest( `/api/device/cwmp/${router.query.id[0]}/generic`, "PUT", value, null, "text", ) if (status === 200){ setAnswer(true) console.log("result:",result) let answer = prettifyXml(result) if (answer == "null"){ answer = result } console.log(answer) setContent(answer) } setOpen(false) } const fetchMessages = async () => { let {result, status} = await httpRequest( `/api/device/message?type=cwmp`, "GET", null, null, ) if ( status === 200){ setMessage(result) setValue(result ? result[0].value : "") return result } } const handleChangeRPC = (event) => { setAge(event.target.value); setNewMsgValue(possibleMsgs[event.target.value-1]) }; const handleEditMessage = (event) => { setSaveChanges(true) setValue(event.target.value) } const handleSubmit = useCallback( (event) => { event.preventDefault(); }, [] ); useEffect(() => { fetchMessages(); },[]); return (
< EnvelopeIcon/>} title="Custom Message" action={ } > Message {!loadingSaveMsg ? :} {/* */} {!loadingSaveMsg ? : } theme.zIndex.drawer + 1 }} open={open} onClick={handleClose} > Response { setAnswer(false); handleClose; //setContent(""); }} >
            {content}
          
{setNewMsgName(event.target.value)}} label="Name" sx={{maxWidth: "30%", justifyContent:"center"}} /> Template {/* */} {!loading ? :}
); };