You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.8 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import { useState, useEffect } from 'react';
  2. import { Button, Cascader, Input, message, List, Typography } from 'antd';
  3. import { getOrgListService } from '../../services/ktj/org';
  4. import { OrgItem } from '../../services/ktjTypes';
  5. import {bleItem, child, GwdItem, orgCascaderType, systemItem} from './types';
  6. import { sysSet } from '../../services/user/system';
  7. import { useAppDispatch, useAppSelector } from "../../utils/hooks";
  8. import { updateSystemAccountState, updateSystemOrgState } from '../../store/system/systemSlice';
  9. import {createWebSocket, sharedWsUrl} from "../../services/socket";
  10. import {start, stop, disconnect, connect} from "../../services/ble/ble";
  11. export default function Setting(){
  12. useEffect(()=>{
  13. queryRailData()
  14. onBleStart()
  15. querySettingData()
  16. return ()=>{
  17. onBleStop()
  18. }
  19. },[])
  20. const dispatch = useAppDispatch();
  21. const deviceInfo = useAppSelector(store => store.context.device);
  22. const systemState = useAppSelector((store) => store.systemState);
  23. const [systemList, setSystemList] = useState<systemItem[]>([])
  24. const [accountInfo, setAccountInfo] = useState<systemItem>({})
  25. const [orgInfo, setOrgInfo] = useState<systemItem>({})
  26. const [orgValues, setOrgValues] = useState<string[]>([])
  27. const [bleList, setBleList] = useState<bleItem[]>([])
  28. // 创建 websocket 客户端
  29. const wsClient = createWebSocket(sharedWsUrl);
  30. function querySettingData(){
  31. let systemInfo = systemState.systemInfo
  32. setSystemList(systemInfo)
  33. }
  34. useEffect(() => {
  35. const subscription = wsClient.dataOb.subscribe(data => {
  36. // @ts-ignore
  37. if (data.messageType === "STATE" && data.path === "/api/ble/ble-list") {
  38. // @ts-ignore
  39. setBleList(data.data)
  40. }
  41. });
  42. wsClient.connect();
  43. return () => subscription.unsubscribe();
  44. });
  45. useEffect(()=>{
  46. if(systemState.orgInfo){
  47. const cloneOrgItem = systemState.orgInfo
  48. let orgItem:systemItem = {
  49. id: cloneOrgItem.id,
  50. name: cloneOrgItem.name,
  51. code: cloneOrgItem.code,
  52. value: cloneOrgItem.value
  53. }
  54. setOrgInfo(orgItem)
  55. }
  56. }, [systemState.orgInfo])
  57. useEffect(()=>{
  58. if(systemState.accountInfo){
  59. const cloneAccountItem = systemState.accountInfo
  60. let accountInfo:systemItem = {
  61. id: cloneAccountItem.id,
  62. name: cloneAccountItem.name,
  63. code: cloneAccountItem.code,
  64. value: cloneAccountItem.value
  65. }
  66. setAccountInfo(accountInfo)
  67. }
  68. }, [systemState.accountInfo])
  69. useEffect(()=>{
  70. if(orgInfo && orgInfo.value){
  71. let orgIds:string[] = []
  72. const values:child[] = JSON.parse(orgInfo.value)
  73. values.map(item => {
  74. orgIds.push(item.value)
  75. })
  76. setOrgValues(orgIds)
  77. }
  78. }, [orgInfo])
  79. //获取铁路局数据
  80. const [KTJOrgList, setKTJOrgList] = useState<orgCascaderType[]>([]);
  81. function queryRailData(){
  82. getOrgListService().then((res) => {
  83. if (res && res.data) {
  84. let resData: OrgItem[] = res.data;
  85. let data = convertToCascaderData(resData)
  86. setKTJOrgList(data)
  87. }
  88. }).catch((e) => {});
  89. }
  90. function onOrgChange(value:string[], selectedOptions: orgCascaderType[]){
  91. if(selectedOptions && selectedOptions.length){
  92. let orgList = selectedOptions.map(item => {
  93. return {
  94. value:item.value,
  95. label:item.label
  96. }
  97. })
  98. const orgParams = {
  99. ...orgInfo,
  100. value:JSON.stringify(orgList)
  101. }
  102. sysSet(orgParams, "PUT").then(res=>{
  103. if(res.status === 0){
  104. dispatch(updateSystemOrgState(orgParams))
  105. message.success("修改成功")
  106. }
  107. })
  108. }
  109. }
  110. let newAccountInfo = {
  111. name:'',
  112. value: ''
  113. }
  114. function onSaveAccount(){
  115. const accountParams = {
  116. code:"UPLOAD_USERNAME",
  117. name:accountInfo.name,
  118. value:newAccountInfo.value,
  119. id:accountInfo.id
  120. }
  121. sysSet(accountParams, "PUT").then(res=>{
  122. if(res.status === 0){
  123. dispatch(updateSystemAccountState(accountParams))
  124. message.success("修改成功")
  125. }
  126. })
  127. }
  128. function onAccountChange(accountValue:string){
  129. newAccountInfo.name = accountInfo.name || '';
  130. newAccountInfo.value = accountValue;
  131. }
  132. function convertToCascaderData(data:OrgItem[]) {
  133. return data.map(item => {
  134. const newItem:orgCascaderType = {
  135. value: item.key,
  136. label: item.value,
  137. };
  138. if (item.gwdDicList && item.gwdDicList.length > 0) {
  139. newItem.children = item.gwdDicList.map(gwdItem => {
  140. const newGwdItem:GwdItem = {
  141. value: gwdItem.key,
  142. label: gwdItem.value,
  143. };
  144. if (gwdItem.railDicList && gwdItem.railDicList.length > 0) {
  145. newGwdItem.children = gwdItem.railDicList.map(railItem => ({
  146. value: railItem.key,
  147. label: railItem.value || railItem.input
  148. }));
  149. }
  150. return newGwdItem;
  151. });
  152. }
  153. return newItem;
  154. });
  155. }
  156. function onBleStart() {
  157. start().then(() => {
  158. message.success('蓝牙开始扫描')
  159. })
  160. }
  161. function onBleStop() {
  162. stop().then(() => {
  163. })
  164. }
  165. function bleConnect(address: string) {
  166. connect(address).then((res) => {
  167. if (res.status !== 0) {
  168. message.error(res.data.info);
  169. return
  170. }
  171. message.success('蓝牙连接成功')
  172. })
  173. }
  174. function bleDisconnect() {
  175. disconnect().then(() => {
  176. message.success('蓝牙断开连接')
  177. })
  178. }
  179. const connectionStatus = () => {
  180. if (!deviceInfo.isConnected) {
  181. return (
  182. <section className='p-[20px]'>
  183. <div></div>
  184. {bleList.map(item => {
  185. return <div key={item.address}>
  186. <div className={'flex justify-between w-1/4 pt-2.5'}>
  187. <p >
  188. <p className={'text-[16px] text-gray-400'}>{item.name}</p>
  189. <p className={'text-[12px] text-gray-400'}>{item.address}</p>
  190. </p>
  191. {
  192. item.connect ? <p className={'text-[12px] text-gray-400'}></p> : <Button type={'primary'} onClick={() =>bleConnect(item.address)}></Button>
  193. }
  194. </div>
  195. </div>
  196. })}
  197. </section>
  198. );
  199. } else {
  200. if(deviceInfo.connectedType === 'UART_CHANNEL') {
  201. return (
  202. <div className='p-[20px]'>
  203. usb连接
  204. <div className="mt-[1rem] ml-[1.6rem]">sn码{deviceInfo.sn}</div>
  205. </div>
  206. )
  207. }else if(deviceInfo.connectedType === 'BLE_CHANNEL') {
  208. return (
  209. <div className='p-[20px]'>
  210. <Button className={'ml-[10px]'} onClick={bleDisconnect}></Button>
  211. <div className="mt-[1rem] ml-[1.6rem]">sn码{deviceInfo.sn}</div>
  212. </div>
  213. )
  214. }
  215. }
  216. };
  217. // @ts-ignore
  218. return <><div className={'pr-2.5 pl-2.5 w-full h-full'}>
  219. <div className={'h-full w-full bg-white p-10 rounded-2xl'}>
  220. <h1 className='text-[20px]'></h1>
  221. <section className='p-[20px]'>
  222. <div>
  223. <span></span>
  224. <Cascader className='w-[300px]' key={orgValues.length} defaultValue={orgValues} options={KTJOrgList} onChange={onOrgChange} placeholder="请选择局段线" /></div>
  225. <div className='mt-[10px]'>
  226. <span> {accountInfo.name}</span>
  227. <Input key={accountInfo.value} defaultValue={accountInfo.value} onChange={(e)=>{onAccountChange(e.target.value)}} className='w-[300px]'></Input>
  228. <Button className='ml-[10px]' size='small' type="primary" onClick={onSaveAccount}></Button>
  229. </div>
  230. </section>
  231. <h1 className='text-[20px]'></h1>
  232. {connectionStatus()}
  233. </div>
  234. </div>
  235. </>
  236. }