// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { I18n, Logger } from '@aws-amplify/core'; import { useState } from 'react'; import Alert from 'react-bootstrap/Alert'; import Breadcrumb from 'react-bootstrap/Breadcrumb'; import Button from 'react-bootstrap/Button'; import ButtonGroup from 'react-bootstrap/ButtonGroup'; import Card from 'react-bootstrap/Card'; import Col from 'react-bootstrap/Col'; import Container from 'react-bootstrap/Container'; import Dropdown from 'react-bootstrap/Dropdown'; import DropdownButton from 'react-bootstrap/DropdownButton'; import Jumbotron from 'react-bootstrap/Jumbotron'; import Row from 'react-bootstrap/Row'; import Table from 'react-bootstrap/Table'; import { useNavigate } from 'react-router-dom'; import ConnectionLogsModal from './ConnectionLogsModal'; import MessageModal from '../../components/MessageModal'; import EmptyRow from '../../components/EmptyRow'; import EmptyCol from '../../components/EmptyCol'; import { LoadingProgressBar } from '../../components/Loading'; import { ConnectionsHook } from '../../hooks/ConnectionHook'; import { requestApi } from '../../util/apis'; import { ControlConnectionResponse, ConnectionControl, ListConnectionsItem, MachineProtocol, MessageModalType, PaginationType } from '../../util/types'; import { buildConnectionDefinition, getErrorMessage } from '../../util/utils'; type ConnectionRowRequest = { connectionName: string; machineName: string; protocol: MachineProtocol; status: ConnectionControl; }; const logger = new Logger('Dashboard'); /** * Renders the dashboard. * @param props The properties for the dashboard * @param props.region The AWS region * @returns The dashboard */ export default function Dashboard(props: { region: string }): JSX.Element { const navigate = useNavigate(); const [showDeleteConfirmMessageModal, setShowDeleteConfirmMessageModal] = useState(false); const [showMessageModal, setShowMessageModal] = useState(false); const [messageModalMessage, setMessageModalMessage] = useState(''); const [connectionName, setConnectionName] = useState(); const [showConnectionLogsModal, setShowConnectionLogsModal] = useState(false); const [deleteConnectionProtocol, setDeleteConnectionProtocol] = useState(); const { connections, getConnections, loading, pageIndex, pageToken } = ConnectionsHook({ setDeleteConnectionProtocol, setMessageModalMessage, setShowMessageModal }); /** * Renders the empty connection message. * @returns Empty connection component */ function EmptyConnection(): JSX.Element { return (

{I18n.get('info.message.no.connection')}

); } /** * Renders a connection table row. * @param connection Connection * @returns Connection row */ function Connection(connection: ConnectionRowRequest): JSX.Element { return ( {connection.connectionName} {connection.machineName} {connection.protocol === MachineProtocol.OPCDA && 'OPC DA'} {connection.protocol === MachineProtocol.OPCUA && 'OPC UA'} {connection.protocol === MachineProtocol.OSIPI && 'OSI PI'} {connection.protocol === MachineProtocol.MODBUSTCP && 'Modbus TCP'} {I18n.get(`status.${connection.status}`)} }> navigate(`/connection/${connection.connectionName}`)}> {I18n.get('update.connection')} controlConnection(connection.connectionName, ConnectionControl.PUSH, connection.protocol) }> {I18n.get('check.connectivity')} controlConnection(connection.connectionName, ConnectionControl.PULL, connection.protocol) }> {I18n.get('get.connection.configuration')} handleConnectionLogs(connection.connectionName)}> {I18n.get('view.connection.logs')} handleDeleteConnection(connection.connectionName, connection.protocol)} className="red-text"> {I18n.get('delete.connection')} ); } /** * Controls a connection. * @param paramConnectionName The connection name * @param control The connection control * @param protocol The connection protocol */ async function controlConnection(paramConnectionName: string, control: ConnectionControl, protocol: MachineProtocol) { try { const connectionDefinition = buildConnectionDefinition({ connectionName: paramConnectionName, control, protocol }); const response = (await requestApi({ method: 'post', path: '/connections', options: { body: connectionDefinition } })) as ControlConnectionResponse; switch (control) { case ConnectionControl.START: setMessageModalMessage(I18n.get('info.message.start.connection').replace('{}', response.connectionName)); break; case ConnectionControl.STOP: setMessageModalMessage(I18n.get('info.message.stop.connection').replace('{}', response.connectionName)); break; case ConnectionControl.PUSH: setMessageModalMessage(I18n.get('info.message.check.connectivity').replace('{}', response.connectionName)); break; case ConnectionControl.PULL: setMessageModalMessage( I18n.get('info.message.get.connection.configuration').replace('{}', response.connectionName) ); break; case ConnectionControl.DELETE: setMessageModalMessage(I18n.get('info.message.delete.connection').replace('{}', response.connectionName)); break; default: break; } } catch (error) { const errorMessage = getErrorMessage(error); logger.error(errorMessage); setMessageModalMessage( {I18n.get('error.message.control.connection')}
{I18n.get('error')}: {JSON.stringify(errorMessage)}
); } setShowMessageModal(true); if ([ConnectionControl.START, ConnectionControl.STOP].includes(control)) getConnections(); } /** * Shows the connection logs modal. * @param paramConnectionName The connection name */ function handleConnectionLogs(paramConnectionName: string) { setConnectionName(paramConnectionName); setShowConnectionLogsModal(true); } /** * Shows the connection deletion confirm modal. * @param paramConnectionName The connection name * @param protocol The machine protocol */ function handleDeleteConnection(paramConnectionName: string, protocol: MachineProtocol) { setConnectionName(paramConnectionName); setDeleteConnectionProtocol(protocol); setShowDeleteConfirmMessageModal(true); } return ( {I18n.get('manage.connections')} {I18n.get('manage.connections')} {!loading && connections.length === 0 && } {!loading && connections.length > 0 && ( <> {connections.map((connection: ListConnectionsItem) => ( ))}
{I18n.get('connection.name')} {I18n.get('machine.name')} {I18n.get('protocol')} {I18n.get('status')} {I18n.get('connection.actions')} {I18n.get('manage.connection')}
{I18n.get('info.message.create.opc.ua')}:{' '} {I18n.get('iot.sitewise.console.link')} )}
setShowMessageModal(false)} message={messageModalMessage} modalType={MessageModalType.MESSAGE} /> setShowDeleteConfirmMessageModal(false)} message={I18n.get('warning.message.delete.connection')} modalType={MessageModalType.CONFIRM} confirmAction={() => controlConnection( connectionName as string, ConnectionControl.DELETE, deleteConnectionProtocol as MachineProtocol ) } /> setShowConnectionLogsModal(false)} connectionName={connectionName as string} />
); }