import React, { useEffect, useState } from 'react'; import { useHistory, useParams } from 'react-router-dom'; import { CRow, CCol, CCard, CCardBody, CNav, CNavLink, CTabPane, CTabContent } from '@coreui/react'; import DeviceHealth from 'components/DeviceHealth'; import CommandHistory from 'components/CommandHistory'; import DeviceLogs from 'components/DeviceLogs'; import DeviceStatisticsCard from 'components/InterfaceStatistics'; import DeviceActionCard from 'components/DeviceActionCard'; import axiosInstance from 'utils/axiosInstance'; import { DeviceProvider, useAuth, useToast } from 'ucentral-libs'; import { useTranslation } from 'react-i18next'; import ConfigurationDisplay from 'components/ConfigurationDisplay'; import WifiAnalysis from 'components/WifiAnalysis'; import CapabilitiesDisplay from 'components/CapabilitiesDisplay'; import { useGlobalWebSocket } from 'contexts/WebSocketProvider'; import NotesTab from './NotesTab'; import DeviceDetails from './Details'; import DeviceStatusCard from './DeviceStatusCard'; const DevicePage = () => { const { t } = useTranslation(); const { deviceId } = useParams(); const [index, setIndex] = useState(0); const { currentToken, endpoints } = useAuth(); const [lastStats, setLastStats] = useState(null); const { addToast } = useToast(); const [status, setStatus] = useState(null); const history = useHistory(); const [deviceConfig, setDeviceConfig] = useState(null); const [error, setError] = useState(false); const [loading, setLoading] = useState(false); const { addDeviceListener, removeDeviceListener } = useGlobalWebSocket(); const updateNav = (target) => { sessionStorage.setItem('devicePageIndex', target); setIndex(target); }; const getDevice = () => { const options = { headers: { Accept: 'application/json', Authorization: `Bearer ${currentToken}`, }, }; let deviceInfo = null; axiosInstance .get(`${endpoints.owgw}/api/v1/device/${encodeURIComponent(deviceId)}`, options) .then((response) => { deviceInfo = response.data; if (response.data.venue !== '' || (response.data.owner !== '' && endpoints.owprov)) { return axiosInstance.get( `${endpoints.owprov}/api/v1/inventory/${encodeURIComponent( deviceId, )}?withExtendedInfo=true`, options, ); } setDeviceConfig({ ...deviceInfo }); return null; }) .then((response) => { if (response) setDeviceConfig({ ...deviceInfo, extendedInfo: response.data.extendedInfo }); }) .catch((e) => { if (e.response?.status === 404 || e.response?.status === 400) { addToast({ title: t('common.error'), body: t('device.mac_not_found'), color: 'danger', autohide: true, }); history.push('/devices'); } setDeviceConfig(deviceInfo); }); }; const getData = () => { setLoading(true); const options = { headers: { Accept: 'application/json', Authorization: `Bearer ${currentToken}`, }, }; const lastStatsRequest = axiosInstance.get( `${endpoints.owgw}/api/v1/device/${encodeURIComponent(deviceId)}/statistics?lastOnly=true`, options, ); const statusRequest = axiosInstance.get( `${endpoints.owgw}/api/v1/device/${encodeURIComponent(deviceId)}/status`, options, ); Promise.all([lastStatsRequest, statusRequest]) .then(([newStats, newStatus]) => { setLastStats({ ...newStats.data }); setStatus({ ...newStatus.data }); setError(false); }) .catch(() => { setLastStats(null); setStatus(null); setError(true); }) .finally(() => { setLoading(false); }); }; const refresh = () => { getData(); getDevice(); }; useEffect(() => { const target = sessionStorage.getItem('devicePageIndex'); if (target !== null) setIndex(parseInt(target, 10)); }, []); useEffect(() => { setError(false); if (deviceId) { addDeviceListener({ serialNumber: deviceId, types: [ 'device_connection', 'device_disconnection', 'device_firmware_upgrade', 'device_statistics', ], onTrigger: () => refresh(), }); refresh(); } return () => { if (deviceId) { removeDeviceListener({ serialNumber: deviceId, }); } }; }, [deviceId]); return (