yantar/assets/Scripts/stores/ErrorManager.ts

108 lines
3.2 KiB
TypeScript

// ErrorManager.ts
import { _decorator, Component, Node, AudioSource } 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)
private soundEnabled: boolean = true;
private currentErrors: string[] = [];
private errorItems: apiSensors[] = [];
private disabledAlerts: string[] = [];
// Используем единый экземпляр ErrorsStore (Singleton)
private errorsStore: ErrorsStore = ErrorsStore.instance;
static instance: ErrorManager;
onLoad() {
ErrorManager.instance = this;
this.node.on('touchstart', this.disableSoundAlert, this);
}
start() {
if (EquipmentManager.instance) {
EquipmentManager.instance.node.on('data-updated', this.checkErrors, this);
this.schedule(this.checkErrors, 5);
} else {
}
this.schedule(() => { this.fetchApiErrors(); }, 10);
}
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) {
}
}
});
if (updated) {
this.node.emit('errors-updated');
}
this.fetchApiErrors();
}
async fetchApiErrors() {
await this.errorsStore.fetchApiErrors();
this.node.emit('errors-updated');
}
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');
}
}