import IconFont from '@/components/IconFont'; import { StatisticCard } from '@ant-design/pro-components'; import { Divider, Flex, GlobalToken, Grid, theme, Tooltip, Typography, } from 'antd'; const { Text } = Typography; type BinarySensorsProps = { nodeConfigs: MasterModel.NodeConfig[]; }; export const getBinaryEntities = ( nodeConfigs: MasterModel.NodeConfig[] = [], ): MasterModel.Entity[] => nodeConfigs.flatMap((nodeConfig) => nodeConfig.type === 'din' ? nodeConfig.entities.filter( (entity) => entity.type === 'bin' && entity.active === 1, ) : [], ); interface IconTypeResult { iconType: string; color: string; name?: string; } const getIconTypeByEntity = ( entity: MasterModel.Entity, token: GlobalToken, ): IconTypeResult => { if (!entity.config || entity.config.length === 0) { return { iconType: 'icon-not-found', color: token.colorPrimary, }; } switch (entity.config[0].subType) { case 'smoke': return { iconType: 'icon-smoke1', color: entity.value === 0 ? token.colorSuccess : token.colorWarning, name: entity.value === 0 ? 'Bình thường' : 'Phát hiện', }; case 'heat': return { iconType: 'icon-fire', color: entity.value === 0 ? token.colorSuccess : token.colorWarning, name: entity.value === 0 ? 'Bình thường' : 'Phát hiện', }; case 'motion': return { iconType: 'icon-motion', color: entity.value === 0 ? token.colorTextBase : token.colorInfoActive, name: entity.value === 0 ? 'Không' : 'Phát hiện', }; case 'flood': return { iconType: 'icon-water-ingress', color: entity.value === 0 ? token.colorSuccess : token.colorWarning, name: entity.value === 0 ? 'Không' : 'Phát hiện', }; case 'door': return { iconType: entity.value === 0 ? 'icon-door' : 'icon-door-open1', color: entity.value === 0 ? token.colorText : token.colorWarning, name: entity.value === 0 ? 'Đóng' : 'Mở', }; case 'button': return { iconType: 'icon-alarm-button', color: entity.value === 0 ? token.colorText : token.colorSuccess, name: entity.value === 0 ? 'Tắt' : 'Bật', }; default: return { iconType: 'icon-not-found', color: token.colorPrimary, }; } }; const StatisticCardItem = (entity: MasterModel.Entity, token: GlobalToken) => { const { iconType, color, name } = getIconTypeByEntity(entity, token); return ( { const el = e.currentTarget as HTMLElement; el.style.boxShadow = `0 4px 12px ${token.colorPrimary}20`; el.style.transform = 'translateY(-2px)'; }} onMouseLeave={(e) => { const el = e.currentTarget as HTMLElement; el.style.boxShadow = 'none'; el.style.transform = 'translateY(0)'; }} statistic={{ icon: (
), title: ( {entity.name} ), value: name, valueStyle: { fontSize: 12, color, fontWeight: 600, marginTop: 8 }, }} /> ); }; const BinarySensors = ({ nodeConfigs }: BinarySensorsProps) => { const binarySensors = getBinaryEntities(nodeConfigs); console.log('BinarySensor: ', binarySensors); const { token } = theme.useToken(); const { useBreakpoint } = Grid; const screens = useBreakpoint(); return ( Cảm biến {binarySensors.map((entity) => (
{StatisticCardItem(entity, token)}
))}
); }; export default BinarySensors;