64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import { _decorator, Component, Node, Sprite, Label, Button, Color } from 'cc';
|
||
import { ErrorManager } from '../stores/ErrorManager';
|
||
import { ErrorItemData } from '../stores/ErrorItemData';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
// Если у вас ранее не был определён тип ApiError, можно его определить так:
|
||
interface ApiError {
|
||
alarmCode: number;
|
||
description: string;
|
||
timestamp: string;
|
||
}
|
||
|
||
@ccclass('ErrorButtonStatus')
|
||
export class ErrorButtonStatus extends Component {
|
||
@property(Sprite)
|
||
public sprite: Sprite = null!;
|
||
|
||
@property(Button)
|
||
public button: Button = null!;
|
||
|
||
@property(Label)
|
||
public label: Label = null!;
|
||
|
||
@property(Node)
|
||
public equipmentList: Node = null!;
|
||
|
||
start() {
|
||
// Подписываемся на событие обновления ошибок из ErrorManager
|
||
ErrorManager.instance.node.on('errors-updated', this.updateStatus, this);
|
||
// Первоначальное обновление состояния
|
||
this.updateStatus();
|
||
}
|
||
|
||
updateStatus() {
|
||
// Получаем объект с ошибками из ErrorManager.
|
||
// Предполагается, что getErrorList возвращает объект вида:
|
||
// { equipmentErrors: apiSensors[], apiErrors: { [sensorName: string]: ApiError[] } }
|
||
const errors = ErrorManager.instance.getErrorList();
|
||
|
||
// Перебираем ключи (имена оборудования) и преобразуем каждую группу ошибок в ErrorItemData
|
||
const countError = Object.keys(errors.apiErrors).reduce((sum: number, key: string) => {
|
||
const arr: ErrorItemData[] = (errors.apiErrors[key] as ApiError[]).map((err: ApiError) => {
|
||
const e = new ErrorItemData();
|
||
e.sensorName = key; // добавляем имя оборудования
|
||
e.alarmCode = err.alarmCode;
|
||
e.description = err.description;
|
||
e.timestamp = err.timestamp;
|
||
return e;
|
||
});
|
||
return sum + arr.length;
|
||
}, 0);
|
||
|
||
if (countError > 0) {
|
||
this.sprite.color = new Color(255, 0, 0);
|
||
this.button.interactable = true;
|
||
this.label.string = `Аварий: ${countError}`;
|
||
} else {
|
||
this.sprite.color = new Color(128, 128, 128,0);
|
||
this.button.interactable = false;
|
||
this.label.string = "";
|
||
this.equipmentList.active = false;
|
||
}
|
||
}
|
||
} |