DeviceWorkStatus.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. if (route.query.deviceType && route.query.deviceId) {
  42. const deviceType = Number(route.query.deviceType);
  43. const isDeviceTypeExisted = deviceTypes.value.find((item) => item.deviceType === deviceType);
  44. if (isDeviceTypeExisted) {
  45. activeDeviceId.value = Number(route.query.deviceId);
  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(`/device-manage/device-list/equipment-details/${devId}`);
  171. }
  172. };
  173. </script>
  174. <template>
  175. <div class="device-work-container">
  176. <div class="device-work-header">
  177. <ATabs
  178. class="button-tabs-card device-type-tab"
  179. v-model:active-key="activeDeviceType"
  180. type="card"
  181. @tab-click="handleTabClick"
  182. >
  183. <ATabPane v-for="item in deviceTypes" :key="item.deviceType" :tab="`${item.deviceTypeName}(${item.count})`" />
  184. </ATabs>
  185. <ATabs class="device-status-tab" v-model:active-key="activeDeviceStatus" @tab-click="handleTabClick">
  186. <ATabPane :key="DeviceStatusQuery.All" :tab="$t('common.entire')" />
  187. <ATabPane :key="DeviceStatusQuery.Online" :tab="$t('common.online')" />
  188. <ATabPane :key="DeviceStatusQuery.Offline" :tab="$t('common.offline')" />
  189. </ATabs>
  190. </div>
  191. <div ref="deviceListEl" class="device-card-list">
  192. <ARow :gutter="[22, 24]">
  193. <ACol v-for="item in deviceList" :key="item.id" :xs="24" :sm="24" :md="24" :lg="24" :xl="12" :xxl="8">
  194. <div class="device-card-container" @click="handleDevCardClick(item.id, $event)">
  195. <div class="device-card-header">
  196. <span
  197. :class="[
  198. 'status-dot',
  199. {
  200. 'device-status-offline': item.runningStatus === DeviceRunningStatus.Offline,
  201. },
  202. ]"
  203. ></span>
  204. <div class="ellipsis-text device-card-header-title">{{ item.deviceName }}</div>
  205. <div
  206. v-if="
  207. activeDeviceType === DeviceType.控制柜 &&
  208. deviceRealTimeData[item.id]?.[DevParamCtrlCabinet.系统控制模式设定]
  209. "
  210. class="ctrl-cabinet-mode"
  211. >
  212. {{ deviceRealTimeData[item.id][DevParamCtrlCabinet.系统控制模式设定] }}
  213. </div>
  214. <template
  215. v-if="
  216. activeDeviceType === DeviceType.冷水主机 &&
  217. deviceRealTimeData[item.id]?.[DevParamChillerUnit.COP] !== undefined
  218. "
  219. >
  220. <div class="device-cop-value" :data-param-code="DevParamChillerUnit.COP">
  221. {{ deviceRealTimeData[item.id][DevParamChillerUnit.COP] }}
  222. </div>
  223. <!-- <div class="device-cop-level">中</div> -->
  224. </template>
  225. <span class="device-card-header-time">{{ deviceRealTimeData[item.id]?.time }}</span>
  226. <SvgIcon class="device-card-header-button" name="adjustment" @click="viewDevParam(item.id, $event)" />
  227. </div>
  228. <component
  229. :is="deviceCardData[activeDeviceType]?.component"
  230. :real-time-data="deviceRealTimeData[item.id]"
  231. :device-detail="JSON.parse(item.deviceDetail || '{}')"
  232. />
  233. </div>
  234. </ACol>
  235. </ARow>
  236. </div>
  237. <DeviceWorkParams ref="deviceWorkParams" :dev-id="currentDevId" @view-history-data="viewHistoryData" />
  238. <DevWorkParamData ref="devWorkParamData" :dev-id="currentDevId" :param-codes="currentDevParamCodes" />
  239. </div>
  240. </template>
  241. <style lang="scss" scoped>
  242. .device-work-container {
  243. display: flex;
  244. flex-direction: column;
  245. height: 100%;
  246. overflow: hidden;
  247. }
  248. .device-work-header {
  249. display: flex;
  250. align-items: center;
  251. justify-content: space-between;
  252. }
  253. .device-type-tab {
  254. width: calc(100% - 200px);
  255. }
  256. :deep(.device-status-tab) {
  257. font-weight: 500;
  258. line-height: 22px;
  259. &.ant-tabs-top > .ant-tabs-nav {
  260. .ant-tabs-tab {
  261. padding: 5px 0;
  262. + .ant-tabs-tab {
  263. margin-left: 24px;
  264. }
  265. }
  266. &::before {
  267. border: none;
  268. }
  269. }
  270. }
  271. .device-card-list {
  272. flex: 1;
  273. overflow: hidden auto;
  274. }
  275. .device-card-container {
  276. position: relative;
  277. height: 328px;
  278. padding: 16px 24px;
  279. background-color: #fff;
  280. border-radius: 12px;
  281. }
  282. .device-card-header {
  283. display: flex;
  284. align-items: center;
  285. margin-bottom: 24px;
  286. }
  287. .device-status-offline {
  288. --status-dot-rgb: 191, 191, 191; // RGB 颜色分量 (灰色)
  289. }
  290. .device-card-header-title {
  291. max-width: 150px;
  292. margin-right: 16px;
  293. margin-left: 8px;
  294. font-size: 16px;
  295. font-weight: 600;
  296. line-height: 28px;
  297. color: var(--antd-color-text);
  298. }
  299. .ctrl-cabinet-mode {
  300. width: 86px;
  301. height: 24px;
  302. font-size: 14px;
  303. font-weight: 500;
  304. line-height: 22px;
  305. color: var(--antd-color-primary);
  306. text-align: center;
  307. background: var(--antd-color-primary-opacity-10);
  308. border: 1px solid var(--antd-color-primary);
  309. border-radius: 4px;
  310. }
  311. .device-cop-value {
  312. font-family: DINAlternate;
  313. font-size: 24px;
  314. font-weight: bold;
  315. line-height: 32px;
  316. color: #e6a23c;
  317. cursor: pointer;
  318. }
  319. .device-cop-level {
  320. width: 16px;
  321. height: 16px;
  322. margin-left: 8px;
  323. font-size: 12px;
  324. font-weight: 600;
  325. line-height: 16px;
  326. color: #e6a23c;
  327. text-align: center;
  328. cursor: pointer;
  329. background: rgb(234 178 94 / 40%);
  330. border-radius: 2px;
  331. }
  332. .device-card-header-time {
  333. flex: 1;
  334. font-size: 12px;
  335. font-weight: 500;
  336. line-height: 20px;
  337. color: #999;
  338. text-align: right;
  339. }
  340. .device-card-header-button {
  341. padding: 4px;
  342. margin-left: 16px;
  343. font-size: 16px;
  344. color: var(--antd-color-primary);
  345. cursor: pointer;
  346. background: var(--antd-color-primary-opacity-15);
  347. border-radius: 4px;
  348. }
  349. :deep(.device-card-label) {
  350. height: 20px;
  351. font-size: 12px;
  352. font-weight: 500;
  353. line-height: 20px;
  354. color: #999;
  355. + .progress-text-bar {
  356. margin-top: 4px;
  357. cursor: pointer;
  358. }
  359. }
  360. :deep(.device-card-value) {
  361. height: 24px;
  362. font-family: DINAlternate;
  363. font-size: 16px;
  364. font-weight: bold;
  365. line-height: 24px;
  366. color: #333;
  367. cursor: pointer;
  368. &.device-card-no-history {
  369. cursor: default;
  370. }
  371. }
  372. </style>