import { useState, useEffect } from 'react'; import { Button, Cascader, Input, message, Form } from 'antd'; import { LoadingOutlined, } from '@ant-design/icons'; import { getOrgListService } from '../../services/ktj/org'; import { OrgItem } from '../../services/ktjTypes'; import {bleItem, child, GwdItem, orgCascaderType, systemItem} from './types'; import { sysSet } from '../../services/user/system'; import { useAppDispatch, useAppSelector } from "../../utils/hooks"; import { updateSystemAccountState, updateSystemOrgState } from '../../store/system/systemSlice'; import {createWebSocket, sharedWsUrl} from "../../services/socket"; import {start, stop, disconnect, connect} from "../../services/ble/ble"; export default function Setting(){ useEffect(()=>{ queryRailData() // onBleStart() querySettingData() return ()=>{ onBleStop() } },[]) const dispatch = useAppDispatch(); const deviceInfo = useAppSelector(store => store.context.device); const systemState = useAppSelector((store) => store.systemState); const [systemList, setSystemList] = useState([]) const [accountInfo, setAccountInfo] = useState({}) const [orgInfo, setOrgInfo] = useState({}) const [orgValues, setOrgValues] = useState([]) const [bleList, setBleList] = useState([]) // 创建 websocket 客户端 const wsClient = createWebSocket(sharedWsUrl); function querySettingData(){ let systemInfo = systemState.systemInfo setSystemList(systemInfo) } useEffect(() => { const subscription = wsClient.dataOb.subscribe(data => { // @ts-ignore if (data.messageType === "STATE" && data.path === "/api/ble/ble-list") { // @ts-ignore setBleList(data.data.map(item => { return { ...item, loading: item.address === connecting, } })) } }); wsClient.connect(); return () => subscription.unsubscribe(); }); useEffect(()=>{ if(systemState.orgInfo){ const cloneOrgItem = systemState.orgInfo let orgItem:systemItem = { id: cloneOrgItem.id, name: cloneOrgItem.name, code: cloneOrgItem.code, value: cloneOrgItem.value } setOrgInfo(orgItem) } }, [systemState.orgInfo]) useEffect(()=>{ if(systemState.accountInfo){ const cloneAccountItem = systemState.accountInfo let accountInfo:systemItem = { id: cloneAccountItem.id, name: cloneAccountItem.name, code: cloneAccountItem.code, value: cloneAccountItem.value } setAccountInfo(accountInfo) } }, [systemState.accountInfo]) useEffect(()=>{ if(orgInfo && orgInfo.value){ let orgIds:string[] = [] const values:child[] = JSON.parse(orgInfo.value) values.map(item => { orgIds.push(item.value) }) setOrgValues(orgIds) } }, [orgInfo]) //获取铁路局数据 const [KTJOrgList, setKTJOrgList] = useState([]); function queryRailData(){ getOrgListService().then((res) => { if (res && res.data) { let resData: OrgItem[] = res.data; let data = convertToCascaderData(resData) setKTJOrgList(data) } }).catch((e) => {}); } function onOrgChange(value:string[], selectedOptions: orgCascaderType[]){ if(selectedOptions && selectedOptions.length){ let orgList = selectedOptions.map(item => { return { value:item.value, label:item.label } }) const orgParams = { ...orgInfo, value:JSON.stringify(orgList) } sysSet(orgParams, "PUT").then(res=>{ if(res.status === 0){ dispatch(updateSystemOrgState(orgParams)) message.success("修改成功") } }) } } let newAccountInfo = { name:'', value: '' } function onSaveAccount(){ const accountParams = { code:"UPLOAD_USERNAME", name:accountInfo.name, value:newAccountInfo.value, id:accountInfo.id } sysSet(accountParams, "PUT").then(res=>{ if(res.status === 0){ dispatch(updateSystemAccountState(accountParams)) message.success("修改成功") } }) } function onAccountChange(accountValue:string){ newAccountInfo.name = accountInfo.name || ''; newAccountInfo.value = accountValue; } function convertToCascaderData(data:OrgItem[]) { return data.map(item => { const newItem:orgCascaderType = { value: item.key, label: item.value, }; if (item.gwdDicList && item.gwdDicList.length > 0) { newItem.children = item.gwdDicList.map(gwdItem => { const newGwdItem:GwdItem = { value: gwdItem.key, label: gwdItem.value, }; if (gwdItem.railDicList && gwdItem.railDicList.length > 0) { newGwdItem.children = gwdItem.railDicList.map(railItem => ({ value: railItem.key, label: railItem.value || railItem.input })); } return newGwdItem; }); } return newItem; }); } const [loading, setLoading] = useState(false); function onBleStart() { start().then(() => { setLoading(true); message.success('蓝牙开始扫描') }) } function onBleStop() { stop().then(() => { }) } let connecting = '' function bleConnect(address: string) { setBleList(bleList.map(item => { if (item.address === address) { item.loading = true } return item })) connecting = address; connect(address).then((res) => { if (res.status !== 0) { // @ts-ignore message.error(res.data?.info || res.message) return } message.success('蓝牙连接成功') }).finally(() => { setBleList(bleList.map(item => { if (item.address === address) { item.loading = false } return item })) connecting = ''; }) } function bleDisconnect() { disconnect().then(() => { message.success('蓝牙断开连接') }) } const connectionStatus = () => { if (!deviceInfo.isConnected) { if (loading) { return (
附近设备
{bleList.map(item => { return

{item.name}

{item.address}

{ item.connect ?

已连接

: }
})}
); }else { return ( ) } } else { if(deviceInfo.connectedType === 'UART_CHANNEL') { return (
已通过usb连接
sn码:{deviceInfo.sn}
) }else if(deviceInfo.connectedType === 'BLE_CHANNEL') { return (
已通过蓝牙连接
sn码:{deviceInfo.sn}
) } } }; const saveSet = (item:systemItem, value:string) => { const params = { id:item.id, value } sysSet(params, "PUT").then(res=>{ }) } const form = () =>{ return
{ systemList.map((item, index) => { return {item.code === 'ORG' ? : saveSet(item, e.target.value)}/>} }) }
} // @ts-ignore return <>

系统配置

{form()} {/*
*/} {/* 铁路局:*/} {/*
*/} {/*
*/} {/* {accountInfo.name}:*/} {/* {onAccountChange(e.target.value)}} className='w-[300px]'>*/} {/* */} {/*
*/}

设备配置

{connectionStatus()}
}