70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { HttpService } from '@nestjs/axios';
|
|
import { firstValueFrom } from 'rxjs';
|
|
|
|
@Injectable()
|
|
export class RangeService {
|
|
constructor(private readonly httpService: HttpService) { }
|
|
|
|
async getRanges(): Promise<Record<string, Array<{ min: number; max: number; status: number }>>> {
|
|
try {
|
|
const response = await firstValueFrom(
|
|
this.httpService.request({
|
|
method: 'OPTIONS',
|
|
url: 'http://192.168.2.39:9999/api/ranges/9999',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
})
|
|
);
|
|
|
|
// Проверяем, что ответ содержит данные в ожидаемом формате
|
|
if (!response.data || !Array.isArray(response.data)) {
|
|
console.error('Invalid response format from ranges API', response.data);
|
|
return {};
|
|
}
|
|
|
|
const rangesMap: Record<string, Array<{ min: number; max: number; status: number }>> = {};
|
|
|
|
response.data.forEach(item => {
|
|
if (item.name && Array.isArray(item.ranges)) {
|
|
rangesMap[item.name] = item.ranges;
|
|
}
|
|
});
|
|
|
|
return rangesMap;
|
|
} catch (error) {
|
|
console.error('Failed to fetch ranges:', error);
|
|
|
|
// Детальное логирование ошибки
|
|
if (error.response) {
|
|
console.error('Server responded with:', {
|
|
status: error.response.status,
|
|
data: error.response.data
|
|
});
|
|
} else if (error.request) {
|
|
console.error('No response received:', error.request);
|
|
} else {
|
|
console.error('Request setup error:', error.message);
|
|
}
|
|
|
|
return {};
|
|
}
|
|
}
|
|
|
|
async updateRanges(data: Array<{ name: string; ranges: { min: number; max: number; status: number }[] }>) {
|
|
try {
|
|
const response = await firstValueFrom(
|
|
this.httpService.post('http://192.168.2.39:9999/api/ranges/9999', data, {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Failed to update ranges:', error);
|
|
throw new Error('Failed to update ranges');
|
|
}
|
|
}
|
|
|
|
|
|
} |