61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
/* EquipmentManager.ts simplified without mutex */
|
|
import { _decorator, Component } from 'cc';
|
|
import { apiSensors } from './apiSensors';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('EquipmentManager')
|
|
export class EquipmentManager extends Component {
|
|
public static instance: EquipmentManager;
|
|
|
|
|
|
public apiUrl: string = 'http://192.168.0.219:3030/api/v2/sensors';
|
|
|
|
private _dataList: apiSensors[] = [];
|
|
public totalCount: number = 0;
|
|
private _intervalId: number | null = null;
|
|
|
|
// Используем оператор "!" для отложенной инициализации
|
|
public initialDataPromise!: Promise<void>;
|
|
|
|
onLoad() {
|
|
EquipmentManager.instance = this;
|
|
// Промис будет разрешён после первой загрузки данных
|
|
this.initialDataPromise = this.loadItems();
|
|
this._intervalId = window.setInterval(() => {
|
|
this.loadItems();
|
|
}, 10000);
|
|
}
|
|
|
|
get dataList(): apiSensors[] {
|
|
return this._dataList;
|
|
}
|
|
|
|
async loadItems(): Promise<void> {
|
|
try {
|
|
const response = await fetch(this.apiUrl);
|
|
const data = await response.json();
|
|
if (data && data.items && Array.isArray(data.items)) {
|
|
this._dataList = data.items;
|
|
this.totalCount = this._dataList.length;
|
|
console.log('Данные оборудования обновлены:', this._dataList);
|
|
this.node.emit('data-updated');
|
|
} else {
|
|
console.error('Некорректный формат данных', data);
|
|
}
|
|
} catch (error) {
|
|
console.error('Ошибка при загрузке данных:', error);
|
|
}
|
|
}
|
|
|
|
async getDataList(): Promise<apiSensors[]> {
|
|
return this._dataList;
|
|
}
|
|
|
|
onDestroy() {
|
|
if (this._intervalId !== null) {
|
|
window.clearInterval(this._intervalId);
|
|
this._intervalId = null;
|
|
}
|
|
}
|
|
}
|