123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- typedef EventListenUnitCallback = void Function(
- int status,
- Map<String, dynamic> e,
- );
- class EventListenUnit {
- final String type;
- final String? model;
- final List<int>? 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 = <String, EventListenUnit>{};
- 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<String, dynamic> event) {
- final int status = event["STATUS"];
- for (final unit in _listenMap.values) {
- unit.callback.call(status, event);
- }
- return;
- // TODO:
- EventListenUnit? unit;
- Map<String, dynamic>? 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<String, dynamic>? 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;
- }
- }
|