typedef EventListenUnitCallback = void Function( int status, Map e, ); class EventListenUnit { final String type; final String? model; final List? acceptStatus; final int? maxStatus; final int? minStatus; final EventListenUnitCallback callback; EventListenUnit( this.type, { required this.callback, this.model, this.acceptStatus, this.minStatus, this.maxStatus, }); @override String toString() { if (model == null || model?.isEmpty == true) { return type; } return "$type|$model"; } } class EventEntrepot { static EventEntrepot? _instance; final _listenMap = {}; EventEntrepot._(); static EventEntrepot get instance { _instance ??= EventEntrepot._(); return _instance!; } void register(EventListenUnit unit) { final key = unit.toString(); _listenMap[key] = unit; } void unregister(EventListenUnit unit) { final key = unit.toString(); _listenMap.remove(key); } void receiveEvent(Map event) { final int status = event["STATUS"]; for (final unit in _listenMap.values) { unit.callback.call(status, event); } return; // TODO: EventListenUnit? unit; Map? data; if (event.containsKey("DATAS")) { data = event["DATAS"]; unit = _findUnit(event); } else { data = event["DATA"]; unit = _findUnit(data); } if (data == null) { unit = _listenMap['temp']; unit?.callback.call(status, event); return; // TODO: } if (unit != null) { bool isAccept = true; if (unit.acceptStatus != null) { isAccept = unit.acceptStatus!.contains(status); } else { if (unit.minStatus != null && unit.minStatus! > status) { isAccept = false; } if (unit.maxStatus != null && unit.maxStatus! < status) { isAccept = false; } } if (isAccept) { unit.callback.call(status, data); } } } EventListenUnit? _findUnit(Map? data) { if (data == null) return null; if (data.containsKey("TYPE")) { final type = data["TYPE"]; if (data.containsKey("MODEL")) { final model = data["MODEL"]; try { final unit = _listenMap["$type|$model"]; return unit ?? _listenMap[type]; } catch (e) { print(e); } } else { return _listenMap[type]; } } return null; } }