index.vue 2.45 KB
<script lang="ts" setup>
  import { ComponentPropsConfigType } from '/@/views/visual/packages/index.type';
  import { option } from './config';
  import { useComponentScale } from '/@/views/visual/packages/hook/useComponentScale';
  import { ref } from 'vue';
  import { UpdateTime } from '/@/views/visual/commonComponents/UpdateTime';
  import { SvgIcon } from '/@/components/Icon';
  import { computed } from 'vue';
  import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
  import { useDataFetch } from '../../../hook/socket/useSocket';
  import { DataFetchUpdateFn } from '../../../hook/socket/useSocket.type';

  const props = defineProps<{
    config: ComponentPropsConfigType<typeof option>;
  }>();

  const currentValue = ref<string | number>(123);

  const time = ref<Nullable<number>>(null);

  const getDesign = computed(() => {
    const { persetOption = {}, option } = props.config;

    const {
      iconColor: persetIconColor,
      unit: perseUnit,
      icon: persetIcon,
      fontColor: persetFontColor,
    } = persetOption;

    const { componentInfo, attribute, attributeRename, attributeName } = option;

    const { icon, iconColor, fontColor, unit } = componentInfo || {};
    return {
      iconColor: iconColor || persetIconColor,
      unit: unit ?? perseUnit,
      icon: icon || persetIcon,
      fontColor: fontColor || persetFontColor,
      attribute: attributeRename || attributeName || attribute,
    };
  });

  const updateFn: DataFetchUpdateFn = (message, attribute) => {
    const { data = {} } = message;
    const [latest] = data[attribute] || [];
    const [timespan, value] = latest;
    currentValue.value = value;
    time.value = timespan;
  };

  useDataFetch(props, updateFn);

  const { getScale } = useComponentScale(props);
</script>

<template>
  <main :style="getScale" class="w-full h-full flex flex-col justify-center items-center">
    <DeviceName :config="config" />
    <div class="flex-1 flex justify-center items-center flex-col">
      <SvgIcon
        :name="getDesign.icon!"
        prefix="iconfont"
        :size="60"
        :style="{ color: getDesign.iconColor }"
      />
      <h1 class="font-bold text-xl m-2 truncate" :style="{ color: getDesign.fontColor }">
        <span> {{ currentValue || 0 }}</span>
        <span>{{ getDesign.unit }} </span>
      </h1>
      <div class="text-gray-500 text-sm truncate">{{ getDesign.attribute || '温度' }}</div>
    </div>
    <UpdateTime :time="time" />
  </main>
</template>