import 'ota_platform_interface.dart'; import 'dart:async'; import 'dart:typed_data'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; class OtaEvent { static final int NOT_STARTED = -1; // 未开始 static final int VALIDATION_FILE = 0; // 验证文件 static final int ONUPGRADE = 1; // 升级中 static final int START_UPGRADING = 2; // 开始升级 static final int UPGRADE_SUCCESS = 3; // 升级成功 static final int UPGRADE_FAILED = 4; // 升级失败 static final int CONNECTION_DISCONNECTION = 5; // 连接断开 static final int CONNECTION_SUCCESS = 6; // 连接成功 /// 事件类型 final String event; /// 输出升级状态 final int? type; /// 输出升级进度 final double? progress; /// 错误码 (仅在发生错误时有效) final int? code; /// 错误信息 (仅在发生错误时有效) final String? message; /// 构造函数 OtaEvent({ required this.event, this.type, this.progress, this.code, this.message, }); /// 从Map构造对象 factory OtaEvent.fromMap(Map map) { return OtaEvent( event: map['event'], type: map['type'], progress: map['progress'], code: map['code'], message: map['message'], ); } } class Ota { // 单例实例 static final Ota _instance = Ota._internal(); // 私有构造函数 Ota._internal() { // 初始化时设置事件监听 _setupEventListener(); } // 公共访问点 static Ota get instance => _instance; static const EventChannel _eventChannel = EventChannel( 'com.example.ota/events', ); final RxDouble progress = 0.0.obs; final RxInt type = OtaEvent.NOT_STARTED.obs; final RxBool isUpgrade = false.obs; /// 事件流 Stream? _eventStream; bool _isEventListenerSet = false; // 防止重复监听 /// 获取事件流 Stream get eventStream { _eventStream ??= _eventChannel.receiveBroadcastStream().map((event) { if (event is Map) { return OtaEvent.fromMap(event); } throw PlatformException(code: 'INVALID_EVENT', message: '无效的事件数据'); }); return _eventStream!; } /// 释放资源 void onClose() { onOtaClose(); // 停止事件监听 _eventStream = null; _isEventListenerSet = false; // 清理 Rx 对象 progress.close(); type.close(); isUpgrade.value = false; print("Ota 对象资源已释放"); } /// 内部设置事件监听(只执行一次) void _setupEventListener() { if (_isEventListenerSet) return; eventStream.listen((event) { if (event.event == 'onProgress' && event.type != null && event.progress != null) { print("flutter 收到数据${event.progress}type=${type.value}"); progress.value = event.progress!; type.value = event.type!; } if (event.event == 'onStartOTA') { type.value = OtaEvent.START_UPGRADING; isUpgrade.value = true; // 开始升级 } if (event.event == 'onStopOTA') { type.value = OtaEvent.UPGRADE_SUCCESS; isUpgrade.value = false; } if (event.event == 'onError' && event.code != null && event.message != null) { print("flutter onError${event.code}type=${event.message}"); type.value = OtaEvent.UPGRADE_FAILED; isUpgrade.value = false; } if (event.event == 'NOCONNECTION') { type.value = OtaEvent.CONNECTION_DISCONNECTION; isUpgrade.value = false; } if (event.event == 'CONNECTION_SUCCESS') { type.value = OtaEvent.CONNECTION_SUCCESS; } }); _isEventListenerSet = true; } Future searchDevices(String? filePath) { return OtaPlatform.instance.searchDevices(filePath); } Future onOtaClose() { return OtaPlatform.instance.onOtaClose(); } }