index.vue
4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<script lang="ts" setup>
  import { option } from './config';
  // import { useComponentScale } from '/@/views/visual/packages/hook/useComponentScale';
  import { ComponentPropsConfigType, MultipleDataFetchUpdateFn } from '../../../index.type';
  import { useMultipleDataFetch } from '/@/views/visual/packages/hook/useSocket';
  import { computed } from 'vue';
  import { ref } from 'vue';
  import { Progress } from 'ant-design-vue';
  import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
  import { unref } from 'vue';
  import { onMounted } from 'vue';
  import { UpdateTime } from '/@/views/visual/commonComponents/UpdateTime';
  import { useReceiveMessage } from '../../../hook/useReceiveMessage';
  import { buildUUID } from '/@/utils/uuid';
  import { useReceiveValue } from '../../../hook/useReceiveValue';
  const props = defineProps<{
    config: ComponentPropsConfigType<typeof option>;
  }>();
  const time = ref<Nullable<number>>(null);
  interface PercentType {
    value?: number;
    fontColor: string;
    backgroundColor: string;
    unit: string;
    id: string;
    attribute?: string;
    attributeRename?: string;
    deviceId?: string;
    deviceName?: string;
    attributeName?: string;
    deviceRename?: string;
  }
  const defaultValue: PercentType[] = [
    {
      value: 20,
      fontColor: '#377dff',
      backgroundColor: '#377dff',
      unit: '℃',
      deviceName: '温度',
      attribute: '1',
      id: buildUUID(),
    },
    {
      value: 66,
      fontColor: '#1E8667',
      backgroundColor: '#1E8667',
      unit: '℃',
      deviceName: '温度',
      attribute: '3',
      id: buildUUID(),
    },
  ];
  const getDesign = computed(() => {
    const { persetOption = {}, option } = props.config;
    const { dataSource = [] } = option || {};
    const {
      unit: presetUnit,
      fontColor: presetFontColor,
      backgroundColor: persetBackgroundColor,
    } = persetOption || {};
    return {
      dataSource: dataSource.map((item) => {
        const { unit, fontColor, backgroundColor } = item.componentInfo || {};
        const { attribute, attributeRename, deviceName, deviceRename, deviceId, attributeName } =
          item;
        return {
          unit: unit ?? presetUnit,
          fontColor: fontColor ?? presetFontColor,
          backgroundColor: backgroundColor ?? persetBackgroundColor,
          deviceName,
          deviceRename,
          attribute,
          attributeRename,
          attributeName,
          id: deviceId,
        } as PercentType;
      }),
    };
  });
  const percentList = ref(
    props.config.option.dataSource ? unref(getDesign).dataSource : defaultValue
  );
  const { forEachGroupMessage } = useReceiveMessage();
  const { getNumberValue } = useReceiveValue();
  const updateFn: MultipleDataFetchUpdateFn = (message, deviceId, attribute) => {
    forEachGroupMessage(message, deviceId, attribute, (attribute, value, timespan) => {
      percentList.value.forEach((item) => {
        if (item.id === deviceId && item.attribute === attribute) {
          item.value = getNumberValue(value);
          time.value = timespan;
        }
      });
    });
  };
  onMounted(() => {});
  useMultipleDataFetch(props, updateFn);
</script>
<template>
  <main class="w-full h-full flex flex-col justify-center">
    <DeviceName :config="config" />
    <div
      v-for="item in percentList"
      :key="item.id"
      class="mt-2 flex flex-col ml-3 mr-3 items-stretch"
    >
      <div class="flex justify-between">
        <div class="text-gray-500 text-sm truncate" :style="{ color: item.fontColor }">
          {{
            `${item.deviceRename || item.deviceName}
            -
            ${item.attributeRename || item.attributeName || item.attribute || '温度'}`
          }}
        </div>
        <span class="text-lg" :style="{ color: item.fontColor }"
          >{{ item.value }} {{ item.unit }}</span
        >
      </div>
      <div>
        <Progress :strokeColor="item.backgroundColor" :percent="item.value" :showInfo="false" />
      </div>
    </div>
    <UpdateTime :time="time" />
  </main>
</template>
<style lang="less" scoped>
  // :deep(.ant-progress-success-bg, .ant-progress-bg) {
  //   background: '#2196F3' !important;
  // }
  // :deep(.ant-slider-handle) {
  //   all: unset;
  // }
  // :deep(.ant-slider-track) {
  //   background: var(--slider-color) !important;
  //   height: 8px;
  // }
  // :deep(.ant-slider-rail) {
  //   height: 8px;
  // }
</style>