index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. import { Badge, Button, Card, Divider, Dropdown, Input, Menu, Modal } from 'antd';
  2. import { useDomFullHeight } from '@/hooks';
  3. import '../index.less';
  4. import SearchComponent from '@/components/SearchComponent';
  5. import ProTable, { ActionType, ProColumns } from '@jetlinks/pro-table';
  6. import PermissionButton from '@/components/PermissionButton';
  7. import {
  8. DeleteOutlined,
  9. EditOutlined,
  10. ExportOutlined,
  11. ImportOutlined,
  12. PlayCircleOutlined,
  13. PlusOutlined,
  14. SearchOutlined,
  15. StopOutlined,
  16. } from '@ant-design/icons';
  17. import { useEffect, useRef, useState } from 'react';
  18. import { useIntl } from 'umi';
  19. import ChannelCard from '../channelCard';
  20. import { PageContainer } from '@ant-design/pro-layout';
  21. import Service from './service';
  22. import SaveChannel from './saveChannel';
  23. import SavePoint from './savePoint';
  24. import Import from './import';
  25. import { onlyMessage } from '@/utils/util';
  26. import Export from './Export';
  27. import useSendWebsocketMessage from '@/hooks/websocket/useSendWebsocketMessage';
  28. import { map } from 'rxjs/operators';
  29. import Ellipsis from '@/components/Ellipsis';
  30. export const service = new Service('');
  31. const NewModbus = () => {
  32. const { minHeight } = useDomFullHeight(`.modbus`);
  33. const intl = useIntl();
  34. const actionRef = useRef<ActionType>();
  35. const { permission } = PermissionButton.usePermission('link/Channel/Modbus');
  36. const [param, setParam] = useState({});
  37. const [activeKey, setActiveKey] = useState<any>('');
  38. const [visible, setVisible] = useState<boolean>(false);
  39. const [visiblePoint, setVisiblePoint] = useState<boolean>(false);
  40. const [exportVisible, setExportVisible] = useState<boolean>(false);
  41. const [current, setCurrent] = useState<any>({});
  42. const [pointDetail, setPointDetail] = useState<any>({});
  43. const [importVisible, setImportVisible] = useState<boolean>(false);
  44. const [masterList, setMasterList] = useState<any>([]);
  45. const [filterList, setFilterList] = useState([]);
  46. const masterId = useRef<string>('');
  47. const [subscribeTopic] = useSendWebsocketMessage();
  48. const wsRef = useRef<any>();
  49. const [pointList, setPointList] = useState<any>([]);
  50. const [currentData, setCurrentData] = useState<any>({});
  51. const collectMap = new Map();
  52. collectMap.set('running', 'success');
  53. collectMap.set('error', 'error');
  54. collectMap.set('stopped', 'warning');
  55. const menu = (
  56. <Menu>
  57. <Menu.Item key="1">
  58. <PermissionButton
  59. isPermission={permission.export || true}
  60. icon={<ExportOutlined />}
  61. type="default"
  62. onClick={() => {
  63. setExportVisible(true);
  64. }}
  65. >
  66. 批量导出点位
  67. </PermissionButton>
  68. </Menu.Item>
  69. <Menu.Item key="2">
  70. <PermissionButton
  71. isPermission={permission.import || true}
  72. icon={<ImportOutlined />}
  73. onClick={() => {
  74. setImportVisible(true);
  75. }}
  76. >
  77. 批量导入点位
  78. </PermissionButton>
  79. </Menu.Item>
  80. </Menu>
  81. );
  82. const columns: ProColumns<any>[] = [
  83. {
  84. title: '名称',
  85. dataIndex: 'name',
  86. ellipsis: true,
  87. width: 200,
  88. fixed: 'left',
  89. },
  90. {
  91. title: '功能码',
  92. render: (record: any) => <>{record.function?.text}</>,
  93. },
  94. {
  95. title: '从站ID',
  96. dataIndex: 'unitId',
  97. search: false,
  98. },
  99. {
  100. title: '寄存器数量',
  101. search: false,
  102. render: (record: any) => <>{record.parameter?.quantity}</>,
  103. },
  104. {
  105. title: '地址',
  106. dataIndex: 'address',
  107. search: false,
  108. },
  109. {
  110. title: '当前数据',
  111. search: false,
  112. // ellipsis: true,
  113. width: 150,
  114. render: (record: any) => (
  115. <a
  116. onClick={() => {
  117. if (currentData[record?.id]) {
  118. const arr = currentData[record?.id].match(/[\S]{1,4}/g);
  119. Modal.info({
  120. title: '当前数据',
  121. content: (
  122. <div style={{ display: 'flex', flexDirection: 'column' }}>
  123. {arr.map((item: any, index: number) => (
  124. <div>
  125. 寄存器{index + 1}: {item}
  126. </div>
  127. ))}
  128. </div>
  129. ),
  130. });
  131. }
  132. }}
  133. >
  134. <Ellipsis title={currentData[record?.id] || ''} />
  135. </a>
  136. ),
  137. },
  138. {
  139. title: '采集状态',
  140. search: false,
  141. render: (record: any) => (
  142. <>
  143. {record.state.value === 'disabled' ? (
  144. ''
  145. ) : (
  146. <>
  147. <Badge
  148. status={collectMap.get(record.collectState?.value)}
  149. text={record.collectState?.text}
  150. />
  151. {record.collectState?.value === 'error' && (
  152. <SearchOutlined
  153. style={{ color: '#1d39c4', marginLeft: 3 }}
  154. onClick={() => {
  155. Modal.error({
  156. title: '失败原因',
  157. content: <div>{record.errorReason}</div>,
  158. });
  159. }}
  160. />
  161. )}
  162. </>
  163. )}
  164. </>
  165. ),
  166. },
  167. {
  168. title: '状态',
  169. dataIndex: 'state',
  170. renderText: (state) => (
  171. <Badge text={state?.text} status={state?.value === 'disabled' ? 'error' : 'success'} />
  172. ),
  173. valueType: 'select',
  174. valueEnum: {
  175. disabled: {
  176. text: intl.formatMessage({
  177. id: 'pages.data.option.disabled',
  178. defaultMessage: '禁用',
  179. }),
  180. status: 'disabled',
  181. },
  182. enabled: {
  183. text: '正常',
  184. status: 'enabled',
  185. },
  186. },
  187. filterMultiple: false,
  188. },
  189. {
  190. title: '操作',
  191. valueType: 'option',
  192. align: 'center',
  193. width: 120,
  194. fixed: 'right',
  195. render: (text, record) => [
  196. <PermissionButton
  197. isPermission={permission.update}
  198. key="edit"
  199. onClick={() => {
  200. setPointDetail(record);
  201. setVisiblePoint(true);
  202. }}
  203. type={'link'}
  204. style={{ padding: 0 }}
  205. tooltip={{
  206. title: intl.formatMessage({
  207. id: 'pages.data.option.edit',
  208. defaultMessage: '编辑',
  209. }),
  210. }}
  211. >
  212. <EditOutlined />
  213. </PermissionButton>,
  214. <PermissionButton
  215. type="link"
  216. key={'action'}
  217. style={{ padding: 0 }}
  218. popConfirm={{
  219. title: intl.formatMessage({
  220. id: `pages.data.option.${
  221. record.state.value !== 'disabled' ? 'disabled' : 'enabled'
  222. }.tips`,
  223. defaultMessage: '确认禁用?',
  224. }),
  225. onConfirm: async () => {
  226. if (record.state.value === 'disabled') {
  227. await service.editPoint(record.id, {
  228. ...record,
  229. state: 'enabled',
  230. });
  231. } else {
  232. await service.editPoint(record.id, {
  233. ...record,
  234. state: 'disabled',
  235. });
  236. }
  237. onlyMessage(
  238. intl.formatMessage({
  239. id: 'pages.data.option.success',
  240. defaultMessage: '操作成功!',
  241. }),
  242. );
  243. actionRef.current?.reload();
  244. },
  245. }}
  246. isPermission={permission.action}
  247. tooltip={{
  248. title: intl.formatMessage({
  249. id: `pages.data.option.${record.state.value !== 'disabled' ? 'disabled' : 'enabled'}`,
  250. defaultMessage: record.state.value !== 'disabled' ? '禁用' : '启用',
  251. }),
  252. }}
  253. >
  254. {record.state.value !== 'disabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  255. </PermissionButton>,
  256. <PermissionButton
  257. isPermission={permission.delete}
  258. style={{ padding: 0 }}
  259. disabled={record.state.value === 'enabled'}
  260. popConfirm={{
  261. title: '确认删除',
  262. disabled: record.state.value === 'enabled',
  263. onConfirm: async () => {
  264. const resp: any = await service.deletePoint(record.id);
  265. if (resp.status === 200) {
  266. onlyMessage(
  267. intl.formatMessage({
  268. id: 'pages.data.option.success',
  269. defaultMessage: '操作成功!',
  270. }),
  271. );
  272. actionRef.current?.reload();
  273. }
  274. },
  275. }}
  276. key="delete"
  277. type="link"
  278. >
  279. <DeleteOutlined />
  280. </PermissionButton>,
  281. ],
  282. },
  283. ];
  284. const getMaster = () => {
  285. service
  286. .queryMaster({
  287. paging: false,
  288. sorts: [
  289. {
  290. name: 'createTime',
  291. order: 'desc',
  292. },
  293. ],
  294. })
  295. .then((res: any) => {
  296. if (res.status === 200) {
  297. setMasterList(res.result);
  298. setFilterList(res.result);
  299. setActiveKey(res.result?.[0]?.id);
  300. masterId.current = res.result?.[0]?.id;
  301. }
  302. });
  303. };
  304. //启用禁用
  305. const actions = (id: string, data: any) => {
  306. service.editMaster(id, data).then((res) => {
  307. if (res.status === 200) {
  308. onlyMessage('操作成功');
  309. getMaster();
  310. }
  311. });
  312. };
  313. const deteleMaster = (id: string) => {
  314. service.deleteMaster(id).then((res) => {
  315. if (res.status === 200) {
  316. onlyMessage('删除成功');
  317. getMaster();
  318. }
  319. });
  320. };
  321. useEffect(() => {
  322. masterId.current = activeKey;
  323. actionRef.current?.reload();
  324. }, [activeKey]);
  325. useEffect(() => {
  326. getMaster();
  327. }, []);
  328. useEffect(() => {
  329. const id = `collector-data-modbus`;
  330. const topic = `/collector/MODBUS_TCP/${activeKey}/data`;
  331. wsRef.current = subscribeTopic?.(id, topic, {
  332. pointId: pointList.map((item: any) => item.id),
  333. })
  334. ?.pipe(map((res: any) => res.payload))
  335. .subscribe((payload: any) => {
  336. const { pointId, hex } = payload;
  337. current[pointId] = hex;
  338. setCurrentData({ ...current });
  339. });
  340. return () => wsRef.current && wsRef.current?.unsubscribe();
  341. }, [pointList]);
  342. return (
  343. <PageContainer>
  344. <Card className="modbus" style={{ minHeight }}>
  345. <div className="item">
  346. <div className="item-left">
  347. <div style={{ width: 220 }}>
  348. <Input.Search
  349. placeholder="请输入名称"
  350. allowClear
  351. onSearch={(value) => {
  352. const items = masterList.filter((item: any) => item.name.match(value));
  353. if (value) {
  354. setFilterList(items);
  355. setActiveKey(items?.[0].id);
  356. } else {
  357. setFilterList(masterList);
  358. setActiveKey(masterList?.[0].id);
  359. }
  360. }}
  361. />
  362. <PermissionButton
  363. onClick={() => {
  364. setVisible(true);
  365. setCurrent({});
  366. }}
  367. isPermission={permission.add}
  368. key="add"
  369. icon={<PlusOutlined />}
  370. type="primary"
  371. style={{ width: '100%', marginTop: 16, marginBottom: 16 }}
  372. >
  373. 新增
  374. </PermissionButton>
  375. <div className="item-left-list">
  376. {filterList.map((item: any) => (
  377. <ChannelCard
  378. active={activeKey === item.id}
  379. data={item}
  380. onClick={() => {
  381. setActiveKey(item.id);
  382. }}
  383. actions={
  384. <>
  385. <PermissionButton
  386. isPermission={permission.update}
  387. key="edit"
  388. onClick={() => {
  389. setVisible(true);
  390. setCurrent(item);
  391. }}
  392. type={'link'}
  393. style={{ padding: 0 }}
  394. >
  395. <EditOutlined />
  396. 编辑
  397. </PermissionButton>
  398. <Divider type="vertical" />
  399. <PermissionButton
  400. isPermission={permission.update}
  401. key="enbale"
  402. type={'link'}
  403. style={{ padding: 0 }}
  404. popConfirm={{
  405. title: intl.formatMessage({
  406. id: `pages.data.option.${
  407. item.state.value !== 'disabled' ? 'disabled' : 'enabled'
  408. }.tips`,
  409. defaultMessage: '确认禁用?',
  410. }),
  411. onConfirm: async () => {
  412. if (item.state.value === 'disabled') {
  413. actions(item.id, { state: 'enabled' });
  414. } else {
  415. actions(item.id, { state: 'disabled' });
  416. }
  417. },
  418. }}
  419. >
  420. {item.state.value === 'enabled' ? (
  421. <StopOutlined />
  422. ) : (
  423. <PlayCircleOutlined />
  424. )}
  425. {item.state.value === 'enabled' ? '禁用' : '启用'}
  426. </PermissionButton>
  427. <Divider type="vertical" />
  428. <PermissionButton
  429. isPermission={permission.delete}
  430. style={{ padding: 0 }}
  431. disabled={item.state.value === 'enabled'}
  432. tooltip={{
  433. title: item.state.value === 'enabled' ? '请先禁用该通道,再删除。' : '',
  434. }}
  435. popConfirm={{
  436. title: '确认删除',
  437. disabled: item.state.value === 'enabled',
  438. onConfirm: async () => {
  439. deteleMaster(item.id);
  440. },
  441. }}
  442. key="delete"
  443. type="link"
  444. >
  445. <DeleteOutlined />
  446. </PermissionButton>
  447. </>
  448. }
  449. />
  450. ))}
  451. </div>
  452. </div>
  453. </div>
  454. <div className="item-right">
  455. <div style={{ width: '100%' }}>
  456. <SearchComponent<any>
  457. field={columns}
  458. target="modbus"
  459. onSearch={(value) => {
  460. actionRef.current?.reset?.();
  461. setParam(value);
  462. }}
  463. />
  464. <ProTable
  465. actionRef={actionRef}
  466. params={param}
  467. columns={columns}
  468. rowKey="id"
  469. scroll={{ x: 1000 }}
  470. search={false}
  471. headerTitle={
  472. <>
  473. <PermissionButton
  474. onClick={() => {
  475. setPointDetail({});
  476. setVisiblePoint(true);
  477. }}
  478. isPermission={permission.add}
  479. key="add"
  480. icon={<PlusOutlined />}
  481. type="primary"
  482. style={{ marginRight: 10 }}
  483. >
  484. {intl.formatMessage({
  485. id: 'pages.data.option.add',
  486. defaultMessage: '新增',
  487. })}
  488. </PermissionButton>
  489. <Dropdown key={'more'} overlay={menu} placement="bottom">
  490. <Button>批量操作</Button>
  491. </Dropdown>
  492. </>
  493. }
  494. request={async (params) => {
  495. if (masterId.current) {
  496. const res = await service.queryPoint(masterId.current, {
  497. ...params,
  498. sorts: [{ name: 'createTime', order: 'desc' }],
  499. });
  500. setPointList(res.result.data);
  501. return {
  502. code: res.message,
  503. result: {
  504. data: res.result.data,
  505. pageIndex: res.result.pageIndex,
  506. pageSize: res.result.pageSize,
  507. total: res.result.total,
  508. },
  509. status: res.status,
  510. };
  511. } else {
  512. return {
  513. code: 200,
  514. result: {
  515. data: [],
  516. pageIndex: 0,
  517. pageSize: 0,
  518. total: 0,
  519. },
  520. status: 200,
  521. };
  522. }
  523. }}
  524. />
  525. </div>
  526. </div>
  527. </div>
  528. </Card>
  529. {visible && (
  530. <SaveChannel
  531. data={current}
  532. close={() => {
  533. setVisible(false);
  534. getMaster();
  535. // actionRef.current?.reload();
  536. }}
  537. />
  538. )}
  539. {visiblePoint && (
  540. <SavePoint
  541. data={pointDetail}
  542. masterId={activeKey}
  543. close={() => {
  544. setVisiblePoint(false);
  545. actionRef.current?.reload();
  546. }}
  547. />
  548. )}
  549. <Import
  550. masterId={activeKey}
  551. close={() => {
  552. setImportVisible(false);
  553. actionRef.current?.reload();
  554. }}
  555. visible={importVisible}
  556. />
  557. <Export
  558. data={masterList}
  559. close={() => {
  560. setExportVisible(false);
  561. actionRef.current?.reload();
  562. }}
  563. visible={exportVisible}
  564. />
  565. </PageContainer>
  566. );
  567. };
  568. export default NewModbus;