<template>
  <Icon
    name="stree"
    :size="20"
    :color="show && rtcConnected ? '#00CED1' : 'rgba(255, 255, 255, 0.5)'"
    style="margin-left: 7px;"
  />
</template>
<script setup lang="ts">
import { invoke } from '@tauri-apps/api/core'
import { listen } from '@tauri-apps/api/event'
import { computed, ref } from 'vue'
import { useMessage } from 'naive-ui'
import useStore from '@/store'

const emit = defineEmits<{(evt: 'callBack', value: Any): void }>()
const message = useMessage()
const store = useStore()
const rtcConnected = computed(() => store.rtcConnected)
const show = ref(false)

function parseG923Data(data) {
  // 假设数据是一个数组，例如 [128, 255, 0, 0, ...]
  const buffer = new Uint8Array(data)

  // 解析转向值（假设存储在 buffer[0] 和 buffer[1] 中，小端序）
  const steeringValue = (buffer[1] << 8) | buffer[0]

  // 解析刹车值（假设存储在 buffer[2] 中）
  const brakeValue = buffer[2]

  return {
    steering: steeringValue,
    brake: brakeValue
  }
}

invoke('start_reading_data')
listen('g923-data', (event) => {
  const data = event.payload
  const parsedData = parseG923Data(data)
  console.log('解析后的 G923 数据:', data, parsedData)
})

</script>
