DeviceWorkStatus.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <script setup lang="ts">
  2. import { onMounted, ref, useTemplateRef } from 'vue';
  3. import { useRoute, useRouter } from 'vue-router';
  4. import { useInfiniteScroll } from '@vueuse/core';
  5. import SvgIcon from '@/components/SvgIcon.vue';
  6. import { useRequest } from '@/hooks/request';
  7. import { getDevWorkRealTimeData, getDevWorkTypeCount, queryDevicesList } from '@/api';
  8. import { DeviceRunningStatus, DeviceStatusQuery } from '@/constants';
  9. import { DevParamChillerUnit, DevParamCtrlCabinet } from '@/constants/device-params';
  10. import { deviceCardData, DeviceType } from './device-card';
  11. import DeviceWorkParams from './DeviceWorkParams.vue';
  12. import DevWorkParamData from './DevWorkParamData.vue';
  13. import type { DevGroupTabCompProps, DevicesListItem, DeviceTypeCount, PageParams } from '@/types';
  14. const props = defineProps<DevGroupTabCompProps>();
  15. const router = useRouter();
  16. const route = useRoute();
  17. const { isLoading, handleRequest } = useRequest();
  18. const deviceTypes = ref<DeviceTypeCount[]>([]);
  19. const activeDeviceId = ref<number>();
  20. const activeDeviceType = ref<DeviceType>(DeviceType.空);
  21. const activeDeviceStatus = ref<DeviceStatusQuery>(DeviceStatusQuery.All);
  22. const deviceTypeOrder = [
  23. DeviceType.冷水主机,
  24. DeviceType.控制柜,
  25. DeviceType.冷却塔,
  26. DeviceType.冷却泵,
  27. DeviceType.冷冻泵,
  28. ];
  29. onMounted(() => {
  30. handleRequest(async () => {
  31. deviceTypes.value = await getDevWorkTypeCount(props.deviceGroupId, deviceTypeOrder);
  32. deviceTypes.value.sort((a, b) => {
  33. const indexA = deviceTypeOrder.indexOf(a.deviceType);
  34. const indexB = deviceTypeOrder.indexOf(b.deviceType);
  35. // 如果某个设备类型不在指定顺序中,将其放在末尾
  36. if (indexA === -1) return 1;
  37. if (indexB === -1) return -1;
  38. return indexA - indexB;
  39. });
  40. if (deviceTypes.value.length) {
  41. activeDeviceId.value = route.query.deviceId ? Number(route.query.deviceId) : undefined;
  42. if (route.query.deviceType) {
  43. const deviceType = Number(route.query.deviceType);
  44. const isDeviceTypeExisted = deviceTypes.value.find((item) => item.deviceType === deviceType);
  45. if (isDeviceTypeExisted) {
  46. activeDeviceType.value = deviceType;
  47. router.replace({
  48. path: route.path,
  49. query: {
  50. tab: 'deviceWorkStatus',
  51. r: route.query.r,
  52. },
  53. });
  54. }
  55. } else {
  56. activeDeviceType.value = deviceTypes.value[0].deviceType;
  57. }
  58. getDeviceList();
  59. }
  60. });
  61. });
  62. /**
  63. * 设备实时数据映射对象
  64. * - 以设备 id 作为 key
  65. * - 时间与设备参数的值构成的对象作为 value
  66. */
  67. type DeviceRTDMap = Record<
  68. number,
  69. {
  70. [key: string]: number | string | undefined;
  71. time?: string;
  72. }
  73. >;
  74. const deviceList = ref<DevicesListItem[]>([]);
  75. const deviceTotal = ref(0);
  76. const deviceRealTimeData = ref<DeviceRTDMap>({});
  77. const pageParams = ref<PageParams>({
  78. pageIndex: 1,
  79. pageSize: 12,
  80. pageSorts: [
  81. {
  82. column: 'id',
  83. asc: true,
  84. },
  85. ],
  86. });
  87. const deviceListEl = useTemplateRef('deviceListEl');
  88. useInfiniteScroll(
  89. deviceListEl,
  90. () => {
  91. getDeviceList();
  92. },
  93. {
  94. distance: 10,
  95. canLoadMore: () => {
  96. if (pageParams.value.pageIndex === 1 || isLoading.value) {
  97. return false;
  98. }
  99. return deviceList.value.length < deviceTotal.value;
  100. },
  101. },
  102. );
  103. const getDeviceList = () => {
  104. handleRequest(async () => {
  105. let runningStatusList = null;
  106. if (activeDeviceStatus.value === DeviceStatusQuery.Offline) {
  107. runningStatusList = [DeviceRunningStatus.Offline];
  108. } else if (activeDeviceStatus.value === DeviceStatusQuery.Online) {
  109. runningStatusList = [DeviceRunningStatus.Stop, DeviceRunningStatus.Run];
  110. }
  111. const { records, total } = await queryDevicesList({
  112. ...pageParams.value,
  113. deviceType: activeDeviceType.value,
  114. groupId: props.deviceGroupId,
  115. runningStatusList,
  116. });
  117. const deviceIds = records.map((item) => item.id);
  118. const deviceParamCode = deviceCardData[activeDeviceType.value]?.paramCodes || [];
  119. const data = await getDevWorkRealTimeData(activeDeviceType.value, deviceIds, deviceParamCode);
  120. const isDeviceChillerUnit = activeDeviceType.value === DeviceType.冷水主机;
  121. data.forEach((item) => {
  122. const { deviceId, deviceParamMapList, ...chillerUnitExtraParams } = item;
  123. if (!deviceRealTimeData.value[deviceId]) {
  124. deviceRealTimeData.value[deviceId] = {};
  125. }
  126. deviceParamMapList.forEach((paramItem) => {
  127. Object.assign(deviceRealTimeData.value[item.deviceId], paramItem);
  128. });
  129. if (isDeviceChillerUnit) {
  130. Object.assign(deviceRealTimeData.value[item.deviceId], chillerUnitExtraParams);
  131. }
  132. });
  133. deviceList.value.push(...records);
  134. deviceTotal.value = total;
  135. pageParams.value.pageIndex++;
  136. });
  137. };
  138. const handleTabClick = () => {
  139. deviceList.value = [];
  140. deviceTotal.value = 0;
  141. deviceRealTimeData.value = {};
  142. setTimeout(() => {
  143. pageParams.value.pageIndex = 1;
  144. getDeviceList();
  145. }, 0);
  146. };
  147. const deviceWorkParamsRef = useTemplateRef('deviceWorkParams');
  148. const currentDevId = ref<number>(0);
  149. const viewDevParam = (id: number, e: MouseEvent) => {
  150. e.stopPropagation();
  151. currentDevId.value = id;
  152. deviceWorkParamsRef.value?.showView();
  153. };
  154. const devWorkParamDataRef = useTemplateRef('devWorkParamData');
  155. const currentDevParamCodes = ref<string[]>([]);
  156. const viewHistoryData = (paramCode: string) => {
  157. currentDevParamCodes.value = [paramCode];
  158. devWorkParamDataRef.value?.showView();
  159. };
  160. /**
  161. * 使用事件委托处理设置了参数编码属性 data-param-code 的元素的点击事件,用来查看该参数的历史数据
  162. */
  163. const handleDevCardClick = (devId: number, e: Event) => {
  164. const target = e.target as HTMLElement;
  165. const { paramCode } = target.dataset;
  166. if (paramCode) {
  167. currentDevId.value = devId;
  168. viewHistoryData(paramCode);
  169. } else {
  170. router.push({
  171. path: `/device-manage/device-list/equipment-details/${devId}`,
  172. query: {
  173. from: 'deviceWorkStatus',
  174. groupId: props.deviceGroupId,
  175. deviceType: activeDeviceType.value,
  176. },
  177. });
  178. }
  179. };
  180. </script>
  181. <template>
  182. <div class="device-work-container">
  183. <div class="device-work-header">
  184. <ATabs
  185. class="button-tabs-card device-type-tab"
  186. v-model:active-key="activeDeviceType"
  187. type="card"
  188. @tab-click="handleTabClick"
  189. >
  190. <ATabPane v-for="item in deviceTypes" :key="item.deviceType" :tab="`${item.deviceTypeName}(${item.count})`" />
  191. </ATabs>
  192. <ATabs class="device-status-tab" v-model:active-key="activeDeviceStatus" @tab-click="handleTabClick">
  193. <ATabPane :key="DeviceStatusQuery.All" :tab="$t('common.entire')" />
  194. <ATabPane :key="DeviceStatusQuery.Online" :tab="$t('common.online')" />
  195. <ATabPane :key="DeviceStatusQuery.Offline" :tab="$t('common.offline')" />
  196. </ATabs>
  197. </div>
  198. <div ref="deviceListEl" class="device-card-list">
  199. <ARow :gutter="[22, 24]">
  200. <ACol v-for="item in deviceList" :key="item.id" :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="8">
  201. <div class="device-card-container" @click="handleDevCardClick(item.id, $event)">
  202. <div class="device-card-header">
  203. <span
  204. :class="[
  205. 'status-dot',
  206. {
  207. 'device-status-offline': item.runningStatus === DeviceRunningStatus.Offline,
  208. },
  209. ]"
  210. ></span>
  211. <div class="ellipsis-text device-card-header-title">{{ item.deviceName }}</div>
  212. <div
  213. v-if="
  214. activeDeviceType === DeviceType.控制柜 &&
  215. deviceRealTimeData[item.id]?.[DevParamCtrlCabinet.系统控制模式设定]
  216. "
  217. class="ctrl-cabinet-mode"
  218. >
  219. {{ deviceRealTimeData[item.id][DevParamCtrlCabinet.系统控制模式设定] }}
  220. </div>
  221. <template
  222. v-if="
  223. item.enableCopSet &&
  224. activeDeviceType === DeviceType.冷水主机 &&
  225. deviceRealTimeData[item.id]?.[DevParamChillerUnit.COP] !== undefined
  226. "
  227. >
  228. <div class="device-cop-value" :data-param-code="DevParamChillerUnit.COP">
  229. {{ deviceRealTimeData[item.id][DevParamChillerUnit.COP] }}
  230. </div>
  231. <!-- <div class="device-cop-level">中</div> -->
  232. </template>
  233. <span class="device-card-header-time">{{ deviceRealTimeData[item.id]?.time }}</span>
  234. <SvgIcon class="device-card-header-button" name="adjustment" @click="viewDevParam(item.id, $event)" />
  235. </div>
  236. <component
  237. :is="deviceCardData[activeDeviceType]?.component"
  238. :real-time-data="deviceRealTimeData[item.id]"
  239. :device-detail="JSON.parse(item.deviceDetail || '{}')"
  240. :enable-cop-set="item.enableCopSet"
  241. />
  242. </div>
  243. </ACol>
  244. </ARow>
  245. </div>
  246. <DeviceWorkParams ref="deviceWorkParams" :dev-id="currentDevId" @view-history-data="viewHistoryData" />
  247. <DevWorkParamData ref="devWorkParamData" :dev-id="currentDevId" :param-codes="currentDevParamCodes" />
  248. </div>
  249. </template>
  250. <style lang="scss" scoped>
  251. .device-work-container {
  252. display: flex;
  253. flex-direction: column;
  254. height: 100%;
  255. overflow: hidden;
  256. }
  257. .device-work-header {
  258. display: flex;
  259. align-items: center;
  260. justify-content: space-between;
  261. }
  262. .device-type-tab {
  263. width: calc(100% - 200px);
  264. }
  265. :deep(.device-status-tab) {
  266. font-weight: 500;
  267. line-height: 22px;
  268. &.ant-tabs-top > .ant-tabs-nav {
  269. .ant-tabs-tab {
  270. padding: 5px 0;
  271. + .ant-tabs-tab {
  272. margin-left: 24px;
  273. }
  274. }
  275. &::before {
  276. border: none;
  277. }
  278. }
  279. }
  280. .device-card-list {
  281. flex: 1;
  282. overflow: hidden auto;
  283. }
  284. .device-card-container {
  285. position: relative;
  286. height: 328px;
  287. padding: 16px 24px;
  288. background-color: #fff;
  289. border-radius: 12px;
  290. }
  291. .device-card-header {
  292. display: flex;
  293. align-items: center;
  294. margin-bottom: 24px;
  295. }
  296. .device-status-offline {
  297. --status-dot-rgb: 191, 191, 191; // RGB 颜色分量 (灰色)
  298. }
  299. .device-card-header-title {
  300. max-width: 150px;
  301. margin-right: 16px;
  302. margin-left: 8px;
  303. font-size: 16px;
  304. font-weight: 600;
  305. line-height: 28px;
  306. color: var(--antd-color-text);
  307. }
  308. .ctrl-cabinet-mode {
  309. width: 86px;
  310. height: 24px;
  311. font-size: 14px;
  312. font-weight: 500;
  313. line-height: 22px;
  314. color: var(--antd-color-primary);
  315. text-align: center;
  316. background: var(--antd-color-primary-opacity-10);
  317. border: 1px solid var(--antd-color-primary);
  318. border-radius: 4px;
  319. }
  320. .device-cop-value {
  321. font-family: DINAlternate;
  322. font-size: 24px;
  323. font-weight: bold;
  324. line-height: 32px;
  325. color: #e6a23c;
  326. cursor: pointer;
  327. }
  328. .device-cop-level {
  329. width: 16px;
  330. height: 16px;
  331. margin-left: 8px;
  332. font-size: 12px;
  333. font-weight: 600;
  334. line-height: 16px;
  335. color: #e6a23c;
  336. text-align: center;
  337. cursor: pointer;
  338. background: rgb(234 178 94 / 40%);
  339. border-radius: 2px;
  340. }
  341. .device-card-header-time {
  342. flex: 1;
  343. font-size: 12px;
  344. font-weight: 500;
  345. line-height: 20px;
  346. color: #999;
  347. text-align: right;
  348. }
  349. .device-card-header-button {
  350. padding: 4px;
  351. margin-left: 16px;
  352. font-size: 16px;
  353. color: var(--antd-color-primary);
  354. cursor: pointer;
  355. background: var(--antd-color-primary-opacity-15);
  356. border-radius: 4px;
  357. }
  358. :deep(.device-card-label) {
  359. height: 20px;
  360. font-size: 12px;
  361. font-weight: 500;
  362. line-height: 20px;
  363. color: #999;
  364. + .progress-text-bar {
  365. margin-top: 4px;
  366. cursor: pointer;
  367. }
  368. }
  369. :deep(.device-card-value) {
  370. height: 24px;
  371. font-family: DINAlternate;
  372. font-size: 16px;
  373. font-weight: bold;
  374. line-height: 24px;
  375. color: #333;
  376. cursor: pointer;
  377. &.device-card-no-history {
  378. cursor: default;
  379. }
  380. }
  381. </style>