— так компоненты не упадут, даже если кнопка или её node к моменту onDestroy уже не существует сделал ноды менеджеров списка оборудовани, деталей, и аварий Persistent Node, чтобы при смене сцены не сбрасывалось их состояние.
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
// ErrorManager.ts
|
|
import { _decorator, Component, Node, AudioSource, director } from 'cc';
|
|
import { EquipmentManager } from './EquipmentManager';
|
|
import { apiSensors } from './apiSensors';
|
|
import { ErrorsStore, ApiErrorsResponse } from './ErrorsStore';
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('ErrorManager')
|
|
export class ErrorManager extends Component {
|
|
@property(Node)
|
|
public errorListNode: Node = null!;
|
|
|
|
@property(AudioSource)
|
|
public audioSource: AudioSource | null = null;
|
|
|
|
private soundEnabled = true;
|
|
private currentErrors: string[] = [];
|
|
private errorItems: apiSensors[] = [];
|
|
private disabledAlerts: string[] = [];
|
|
private errorsStore = ErrorsStore.instance;
|
|
|
|
static instance: ErrorManager;
|
|
|
|
onLoad() {
|
|
ErrorManager.instance = this;
|
|
// Делаем этот узел персистентным
|
|
director.addPersistRootNode(this.node);
|
|
this.node?.on('touchstart', this.disableSoundAlert, this);
|
|
}
|
|
|
|
start() {
|
|
EquipmentManager.instance?.node?.on('data-updated', this.checkErrors, this);
|
|
this.schedule(this.checkErrors, 5);
|
|
this.schedule(this.fetchApiErrors, 10);
|
|
}
|
|
|
|
onDestroy() {
|
|
this.node?.off('touchstart', this.disableSoundAlert, this);
|
|
EquipmentManager.instance?.node?.off('data-updated', this.checkErrors, this);
|
|
this.unscheduleAllCallbacks();
|
|
}
|
|
|
|
public getErrorList(): { equipmentErrors: apiSensors[]; apiErrors: ApiErrorsResponse } {
|
|
return { equipmentErrors: this.errorItems, apiErrors: this.errorsStore.getApiErrors() };
|
|
}
|
|
|
|
checkErrors() {
|
|
const dataList = EquipmentManager.instance?.dataList ?? [];
|
|
let updated = false;
|
|
|
|
const prevErrors = [...this.currentErrors];
|
|
this.currentErrors = this.currentErrors.filter(tag =>
|
|
dataList.some(item => item.tag === tag && item.value === 2)
|
|
);
|
|
if (prevErrors.length !== this.currentErrors.length) {
|
|
this.errorItems = this.errorItems.filter(item =>
|
|
dataList.some(it => it.tag === item.tag && it.value === 2)
|
|
);
|
|
updated = true;
|
|
}
|
|
|
|
dataList.forEach(item => {
|
|
if (item.value === 2 && this.currentErrors.indexOf(item.tag) === -1) {
|
|
this.currentErrors.push(item.tag);
|
|
this.errorItems.push(item);
|
|
this.addErrorToUI(item);
|
|
updated = true;
|
|
if (this.soundEnabled && this.disabledAlerts.indexOf(item.tag) === -1) {
|
|
this.audioSource?.play();
|
|
}
|
|
}
|
|
});
|
|
|
|
if (updated) {
|
|
this.node.emit('errors-updated');
|
|
}
|
|
this.fetchApiErrors();
|
|
}
|
|
|
|
async fetchApiErrors() {
|
|
try {
|
|
await this.errorsStore.fetchApiErrors();
|
|
this.node.emit('errors-updated');
|
|
} catch (e) {
|
|
console.error('ErrorManager.fetchApiErrors:', e);
|
|
}
|
|
}
|
|
|
|
addErrorToUI(item: apiSensors) {
|
|
// Ваша логика добавления элемента в UI
|
|
}
|
|
|
|
disableSoundAlert() {
|
|
this.soundEnabled = false;
|
|
}
|
|
|
|
public disableAlertForError(tag: string): void {
|
|
if (this.disabledAlerts.indexOf(tag) === -1) {
|
|
this.disabledAlerts.push(tag);
|
|
}
|
|
}
|
|
|
|
confirmError(tag: string) {
|
|
this.currentErrors = this.currentErrors.filter(e => e !== tag);
|
|
this.errorItems = this.errorItems.filter(item => item.tag !== tag);
|
|
this.node.emit('errors-updated');
|
|
}
|
|
}
|