index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { InstanceModel } from '@/pages/device/Instance';
  3. import { history, useParams } from 'umi';
  4. import { Badge, Card, Descriptions, Divider, message, Space, Tooltip } from 'antd';
  5. import type { ReactNode } from 'react';
  6. import { useEffect, useState } from 'react';
  7. import { observer } from '@formily/react';
  8. import Log from '@/pages/device/Instance/Detail/Log';
  9. // import Alarm from '@/pages/device/components/Alarm';
  10. import Info from '@/pages/device/Instance/Detail/Info';
  11. import Functions from '@/pages/device/Instance/Detail/Functions';
  12. import Running from '@/pages/device/Instance/Detail/Running';
  13. import ChildDevice from '@/pages/device/Instance/Detail/ChildDevice';
  14. import Diagnose from '@/pages/device/Instance/Detail/Diagnose';
  15. import MetadataMap from '@/pages/device/Instance/Detail/MetadataMap';
  16. import Opcua from '@/pages/device/Instance/Detail/Opcua';
  17. import Modbus from '@/pages/device/Instance/Detail/Modbus';
  18. import { useIntl } from '@@/plugin-locale/localeExports';
  19. import Metadata from '../../components/Metadata';
  20. import type { DeviceMetadata } from '@/pages/device/Product/typings';
  21. import MetadataAction from '@/pages/device/components/Metadata/DataBaseAction';
  22. import { Store } from 'jetlinks-store';
  23. import SystemConst from '@/utils/const';
  24. import { getMenuPathByCode, getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  25. import useSendWebsocketMessage from '@/hooks/websocket/useSendWebsocketMessage';
  26. import { Ellipsis, PermissionButton } from '@/components';
  27. import { QuestionCircleOutlined } from '@ant-design/icons';
  28. import Service from '@/pages/device/Instance/service';
  29. import useLocation from '@/hooks/route/useLocation';
  30. import { onlyMessage } from '@/utils/util';
  31. import Parsing from './Parsing';
  32. export const deviceStatus = new Map();
  33. deviceStatus.set('online', <Badge status="success" text={'在线'} />);
  34. deviceStatus.set('offline', <Badge status="error" text={'离线'} />);
  35. deviceStatus.set('notActive', <Badge status="processing" text={'禁用'} />);
  36. const InstanceDetail = observer(() => {
  37. const intl = useIntl();
  38. // const [tab, setTab] = useState<string>('detail');
  39. const params = useParams<{ id: string }>();
  40. const service = new Service('device-instance');
  41. const { permission } = PermissionButton.usePermission('device/Instance');
  42. const location = useLocation();
  43. // const resetMetadata = async () => {
  44. // const resp = await service.deleteMetadata(params.id);
  45. // if (resp.status === 200) {
  46. // message.success('操作成功');
  47. // Store.set(SystemConst.REFRESH_DEVICE, true);
  48. // setTimeout(() => {
  49. // Store.set(SystemConst.REFRESH_METADATA_TABLE, true);
  50. // }, 400);
  51. // }
  52. // };
  53. const baseList = [
  54. {
  55. key: 'detail',
  56. tab: intl.formatMessage({
  57. id: 'pages.device.instanceDetail.detail',
  58. defaultMessage: '实例信息',
  59. }),
  60. component: <Info />,
  61. },
  62. {
  63. key: 'running',
  64. tab: intl.formatMessage({
  65. id: 'pages.device.instanceDetail.running',
  66. defaultMessage: '运行状态',
  67. }),
  68. component: <Running />,
  69. },
  70. {
  71. key: 'metadata',
  72. tab: (
  73. <>
  74. {intl.formatMessage({
  75. id: 'pages.device.instanceDetail.metadata',
  76. defaultMessage: '物模型',
  77. })}
  78. <Tooltip
  79. title={
  80. <>
  81. 属性:
  82. <br />
  83. 用于描述设备运行时具体信息和状态。
  84. <br />
  85. 功能:
  86. <br />
  87. 指设备可供外部调用的指令或方法。
  88. <br />
  89. 事件:
  90. <br />
  91. 设备运行时,主动上报给云端的信息。
  92. <br />
  93. 标签:
  94. <br />
  95. 统一为设备添加拓展字段,添加后将在设备信息页显示。
  96. </>
  97. }
  98. >
  99. <QuestionCircleOutlined style={{ marginLeft: 5 }} />
  100. </Tooltip>
  101. </>
  102. ),
  103. component: (
  104. <Card>
  105. <Metadata
  106. type="device"
  107. // tabAction={
  108. // <PermissionButton
  109. // isPermission={permission.update}
  110. // popConfirm={{
  111. // title: '确认重置?',
  112. // onConfirm: resetMetadata,
  113. // }}
  114. // tooltip={{
  115. // title: '重置后将使用产品的物模型配置',
  116. // }}
  117. // key={'reload'}
  118. // >
  119. // 重置操作1
  120. // </PermissionButton>
  121. // }
  122. />
  123. </Card>
  124. ),
  125. },
  126. {
  127. // 物模型中有事件信息,且设备状态是在线的情况下才显示此模块
  128. key: 'functions',
  129. tab: intl.formatMessage({
  130. id: 'pages.device.instanceDetail.functions',
  131. defaultMessage: '设备功能',
  132. }),
  133. component: <Functions />,
  134. },
  135. {
  136. key: 'log',
  137. tab: intl.formatMessage({
  138. id: 'pages.device.instanceDetail.log',
  139. defaultMessage: '日志管理',
  140. }),
  141. component: <Log />,
  142. },
  143. {
  144. key: 'diagnose',
  145. tab: '设备诊断',
  146. component: <Diagnose />,
  147. },
  148. ];
  149. const pList = [
  150. 'websocket-server',
  151. 'http-server-gateway',
  152. 'udp-device-gateway',
  153. 'coap-server-gateway',
  154. 'mqtt-client-gateway',
  155. 'mqtt-server-gateway',
  156. 'tcp-server-gateway',
  157. ];
  158. const [list, setList] =
  159. useState<{ key: string; tab: string | ReactNode; component: ReactNode }[]>(baseList);
  160. const getDetail = async (id: string) => {
  161. const response = await service.detail(id);
  162. if (response.status === 200) {
  163. InstanceModel.detail = response?.result;
  164. const datalist = [...baseList];
  165. if (
  166. InstanceModel.detail?.accessProvider &&
  167. pList.includes(InstanceModel.detail?.accessProvider)
  168. ) {
  169. datalist.push({
  170. key: 'metadata-map',
  171. tab: '物模型映射',
  172. component: <MetadataMap type="device" />,
  173. });
  174. }
  175. const paring = response.result?.features?.find((item: any) => item.id === 'transparentCodec');
  176. if (paring) {
  177. datalist.push({
  178. key: 'parsing',
  179. tab: intl.formatMessage({
  180. id: 'pages.device.instanceDetail.parsing',
  181. defaultMessage: '数据解析',
  182. }),
  183. component: <Parsing tag="device" data={InstanceModel.detail} />,
  184. });
  185. }
  186. if (response.result.protocol === 'modbus-tcp') {
  187. datalist.push({
  188. key: 'modbus',
  189. tab: 'Modbus',
  190. component: <Modbus />,
  191. });
  192. }
  193. if (response.result.protocol === 'opc-ua') {
  194. datalist.push({
  195. key: 'opcua',
  196. tab: 'OPC UA',
  197. component: <Opcua />,
  198. });
  199. }
  200. if (response.result.deviceType?.value === 'gateway') {
  201. // 产品类型为网关的情况下才显示此模块
  202. datalist.push({
  203. key: 'child-device',
  204. tab: '子设备',
  205. component: <ChildDevice />,
  206. });
  207. }
  208. setList(datalist);
  209. // 写入物模型数据
  210. const metadata: DeviceMetadata = JSON.parse(response.result?.metadata || '{}');
  211. MetadataAction.insert(metadata);
  212. }
  213. };
  214. const [subscribeTopic] = useSendWebsocketMessage();
  215. useEffect(() => {
  216. if (subscribeTopic) {
  217. subscribeTopic(
  218. `instance-editor-info-status-${params.id}`,
  219. `/dashboard/device/status/change/realTime`,
  220. {
  221. deviceId: params.id,
  222. },
  223. // @ts-ignore
  224. ).subscribe((data: any) => {
  225. const payload = data.payload;
  226. const state = payload.value.type;
  227. InstanceModel.detail.state = {
  228. value: state,
  229. text: '',
  230. };
  231. });
  232. }
  233. }, []);
  234. useEffect(() => {
  235. Store.subscribe(SystemConst.REFRESH_DEVICE, () => {
  236. MetadataAction.clean();
  237. setTimeout(() => {
  238. getDetail(params.id);
  239. }, 200);
  240. });
  241. // return subscription.unsubscribe();
  242. }, []);
  243. useEffect(() => {
  244. // console.log(InstanceModel.current)
  245. if (!InstanceModel.current && !params.id) {
  246. history.goBack();
  247. } else {
  248. // setTab('detail');
  249. InstanceModel.active = 'detail';
  250. getDetail(params?.id || InstanceModel.current?.id || '');
  251. }
  252. return () => {
  253. MetadataAction.clean();
  254. };
  255. }, [params.id]);
  256. useEffect(() => {
  257. if ((location as any).query?.key) {
  258. // setTab((location as any).query?.key || 'detail');
  259. InstanceModel.active = (location as any).query?.key || 'detail';
  260. }
  261. const subscription = Store.subscribe(SystemConst.BASE_UPDATE_DATA, (data) => {
  262. if ((window as any).onTabSaveSuccess) {
  263. (window as any).onTabSaveSuccess(data);
  264. setTimeout(() => window.close(), 300);
  265. }
  266. });
  267. return () => subscription.unsubscribe();
  268. }, []);
  269. useEffect(() => {
  270. const { state } = location;
  271. if (state && state?.tab) {
  272. // setTab(state?.tab);
  273. InstanceModel.active = state?.tab;
  274. }
  275. }, [location]);
  276. return (
  277. <PageContainer
  278. className={'page-title-show'}
  279. onBack={history.goBack}
  280. onTabChange={(e) => {
  281. InstanceModel.active = e;
  282. }}
  283. tabList={list}
  284. tabActiveKey={InstanceModel.active}
  285. content={
  286. <Descriptions size="small" column={4}>
  287. <Descriptions.Item label={'ID'}>
  288. <Ellipsis
  289. title={InstanceModel.detail?.id}
  290. tooltip={{ placement: 'topLeft' }}
  291. style={{ maxWidth: 250 }}
  292. limitWidth={250}
  293. />
  294. {/*<Tooltip placement="topLeft" title={InstanceModel.detail?.id}>*/}
  295. {/* <div className="ellipsis" style={{ maxWidth: 250 }}>*/}
  296. {/* {InstanceModel.detail?.id}*/}
  297. {/* </div>*/}
  298. {/*</Tooltip>*/}
  299. </Descriptions.Item>
  300. <Descriptions.Item label={'所属产品'}>
  301. <PermissionButton
  302. type={'link'}
  303. size={'small'}
  304. tooltip={{
  305. title: InstanceModel.detail?.productName,
  306. placement: 'topLeft',
  307. }}
  308. isPermission={!!getMenuPathByCode(MENUS_CODE['device/Product'])}
  309. onClick={() => {
  310. const url = getMenuPathByParams(
  311. MENUS_CODE['device/Product/Detail'],
  312. InstanceModel.detail?.productId,
  313. );
  314. if (url) {
  315. history.replace(url);
  316. }
  317. }}
  318. >
  319. <Ellipsis
  320. title={InstanceModel.detail?.productName}
  321. tooltip={{ placement: 'topLeft' }}
  322. style={{ maxWidth: 250 }}
  323. limitWidth={250}
  324. />
  325. {/*<div className="ellipsis" style={{ width: 250 }}>*/}
  326. {/* {InstanceModel.detail?.productName}*/}
  327. {/*</div>*/}
  328. </PermissionButton>
  329. </Descriptions.Item>
  330. </Descriptions>
  331. }
  332. title={
  333. <div style={{ display: 'flex', alignItems: 'center' }}>
  334. {/*<Tooltip placement="topLeft" title={InstanceModel.detail?.name}>*/}
  335. {/* <div className="ellipsis" style={{ maxWidth: 250 }}>*/}
  336. {/* {InstanceModel.detail?.name}*/}
  337. {/* </div>*/}
  338. {/*</Tooltip>*/}
  339. <Ellipsis
  340. title={InstanceModel.detail?.name}
  341. tooltip={{ placement: 'topLeft' }}
  342. style={{ maxWidth: 250 }}
  343. limitWidth={250}
  344. />
  345. <Divider type="vertical" />
  346. <Space>
  347. {deviceStatus.get(InstanceModel.detail?.state?.value)}
  348. {InstanceModel.detail?.state?.value === 'notActive' && (
  349. <PermissionButton
  350. type={'link'}
  351. key={'state'}
  352. popConfirm={{
  353. title: '确认启用设备',
  354. onConfirm: async () => {
  355. const resp = await service.deployDevice(params.id);
  356. if (resp.status === 200) {
  357. onlyMessage(
  358. intl.formatMessage({
  359. id: 'pages.data.option.success',
  360. defaultMessage: '操作成功!',
  361. }),
  362. );
  363. getDetail(params.id);
  364. }
  365. },
  366. }}
  367. isPermission={permission.action}
  368. tooltip={{
  369. title: '启用设备',
  370. }}
  371. >
  372. 启用设备
  373. </PermissionButton>
  374. )}
  375. {InstanceModel.detail?.state?.value === 'online' && (
  376. <PermissionButton
  377. type={'link'}
  378. key={'state'}
  379. popConfirm={{
  380. title: '确认断开连接',
  381. onConfirm: async () => {
  382. const resp = await service.disconnectDevice(params.id);
  383. if (resp.status === 200) {
  384. onlyMessage(
  385. intl.formatMessage({
  386. id: 'pages.data.option.success',
  387. defaultMessage: '操作成功!',
  388. }),
  389. );
  390. getDetail(params.id);
  391. }
  392. },
  393. }}
  394. isPermission={permission.action}
  395. tooltip={{
  396. title: '断开连接',
  397. }}
  398. >
  399. 断开连接
  400. </PermissionButton>
  401. )}
  402. {InstanceModel.detail?.accessProvider === 'child-device' &&
  403. InstanceModel.detail?.state?.value === 'offline' ? (
  404. <div>
  405. <Tooltip
  406. placement="bottom"
  407. title={
  408. InstanceModel.detail?.features?.find((item) => item.id === 'selfManageState')
  409. ? '该设备的在线状态与父设备(网关设备)保持一致'
  410. : '该设备在线状态由设备自身运行状态决定,不继承父设备(网关设备)的在线状态'
  411. }
  412. >
  413. <QuestionCircleOutlined style={{ fontSize: 14, marginRight: 5 }} />
  414. </Tooltip>
  415. </div>
  416. ) : (
  417. ''
  418. )}
  419. </Space>
  420. </div>
  421. }
  422. extra={[
  423. // <Button key="2">
  424. // {intl.formatMessage({
  425. // id: 'pages.device.productDetail.disable',
  426. // defaultMessage: '停用',
  427. // })}
  428. // </Button>,
  429. // <Button key="1" type="primary">
  430. // {intl.formatMessage({
  431. // id: 'pages.device.productDetail.setting',
  432. // defaultMessage: '应用配置',
  433. // })}
  434. // </Button>,
  435. <img
  436. style={{ marginRight: 20, cursor: 'pointer' }}
  437. src={require('/public/images/device/button.png')}
  438. onClick={() => {
  439. getDetail(params.id);
  440. service.getConfigMetadata(params.id).then((config) => {
  441. if (config.status === 200) {
  442. InstanceModel.config = config?.result || [];
  443. message.success('操作成功!');
  444. }
  445. });
  446. }}
  447. />,
  448. // <SyncOutlined
  449. // onClick={() => {
  450. // getDetail(params.id);
  451. // service.getConfigMetadata(params.id).then((config) => {
  452. // if (config.status === 200) {
  453. // InstanceModel.config = config?.result || [];
  454. // message.success('操作成功!');
  455. // }
  456. // });
  457. // }}
  458. // style={{ fontSize: 20, marginRight: 20 }}
  459. // key="1"
  460. // />,
  461. ]}
  462. >
  463. {list.find((k) => k.key === InstanceModel.active)?.component}
  464. </PageContainer>
  465. );
  466. });
  467. export default InstanceDetail;