import { getLatestWsData } from '../pipe_client';
|
|
// const pipeName = 'tauri-pipe-server';
|
|
// const dataSource = 'ws'; // pipe|ws
|
|
export async function fetchForceData() {
|
try {
|
let response: string | null = null;
|
|
// if (dataSource === 'pipe') {
|
// if (!pipeName) throw new Error('Pipe name is required for pipe data source');
|
// response = await sendToPipe(pipeName, 'GET_FORCE_DATA');
|
// } else if (dataSource === 'ws') {
|
response = await getLatestWsData();
|
if (!response) return null;
|
// }
|
|
if (!response) return null;
|
|
const jsonData = JSON.parse(response);
|
const forceData = jsonData[0];
|
if (Array.isArray(forceData) && forceData.length === 6) {
|
// 将数组中的每个数分别*4000/4194303
|
for (let i = 0; i < forceData.length; i++) {
|
forceData[i] = forceData[i] * 4000 / 4194303;
|
}
|
return forceData;
|
}
|
|
return null;
|
} catch (error) {
|
console.error('Error fetching force data:', error);
|
return null;
|
}
|
}
|
|
export function createDataReceiver( callback: (data: number[]) => void) {
|
let errorCount = 0;
|
const maxConsecutiveErrors = 5;
|
let showingError = false;
|
|
return async () => {
|
try {
|
const forceData = await fetchForceData();
|
console.log('forceData111', forceData);
|
if (forceData) {
|
// 重置错误计数
|
errorCount = 0;
|
if (showingError) {
|
showingError = false;
|
console.log('数据通信已恢复');
|
}
|
console.log('forceData222', forceData);
|
callback(forceData);
|
}
|
} catch (err) {
|
// 增加错误计数
|
errorCount++;
|
|
// 只在连续错误达到阈值时显示错误信息,避免日志刷屏
|
if (errorCount >= maxConsecutiveErrors && !showingError) {
|
showingError = true;
|
console.error('数据通信持续失败,请检查服务端状态:', err);
|
}
|
}
|
};
|
}
|