|
|
@ -2,7 +2,6 @@ import 'ota_platform_interface.dart'; |
|
|
import 'dart:async'; |
|
|
import 'dart:async'; |
|
|
import 'dart:typed_data'; |
|
|
import 'dart:typed_data'; |
|
|
import 'package:flutter/services.dart'; |
|
|
import 'package:flutter/services.dart'; |
|
|
import 'package:get/get.dart'; |
|
|
|
|
|
|
|
|
|
|
|
class OtaEvent { |
|
|
class OtaEvent { |
|
|
static final int NOT_STARTED = -1; // 未开始 |
|
|
static final int NOT_STARTED = -1; // 未开始 |
|
|
@ -57,7 +56,7 @@ class Ota { |
|
|
// 私有构造函数 |
|
|
// 私有构造函数 |
|
|
Ota._internal() { |
|
|
Ota._internal() { |
|
|
// 初始化时设置事件监听 |
|
|
// 初始化时设置事件监听 |
|
|
_setupEventListener(); |
|
|
// _setupEventListener(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 公共访问点 |
|
|
// 公共访问点 |
|
|
@ -66,13 +65,13 @@ class Ota { |
|
|
static const EventChannel _eventChannel = EventChannel( |
|
|
static const EventChannel _eventChannel = EventChannel( |
|
|
'com.example.ota/events', |
|
|
'com.example.ota/events', |
|
|
); |
|
|
); |
|
|
final RxDouble progress = 0.0.obs; |
|
|
// double progress = 0.0; |
|
|
final RxInt type = OtaEvent.NOT_STARTED.obs; |
|
|
// late int type = OtaEvent.NOT_STARTED; |
|
|
final RxBool isUpgrade = false.obs; |
|
|
// bool isUpgrade = false; |
|
|
|
|
|
|
|
|
/// 事件流 |
|
|
/// 事件流 |
|
|
Stream<OtaEvent>? _eventStream; |
|
|
Stream<OtaEvent>? _eventStream; |
|
|
bool _isEventListenerSet = false; // 防止重复监听 |
|
|
// bool _isEventListenerSet = false; // 防止重复监听 |
|
|
|
|
|
|
|
|
/// 获取事件流 |
|
|
/// 获取事件流 |
|
|
Stream<OtaEvent> get eventStream { |
|
|
Stream<OtaEvent> get eventStream { |
|
|
@ -90,53 +89,48 @@ class Ota { |
|
|
onOtaClose(); |
|
|
onOtaClose(); |
|
|
// 停止事件监听 |
|
|
// 停止事件监听 |
|
|
_eventStream = null; |
|
|
_eventStream = null; |
|
|
_isEventListenerSet = false; |
|
|
//_isEventListenerSet = false; |
|
|
|
|
|
|
|
|
// 清理 Rx 对象 |
|
|
|
|
|
progress.close(); |
|
|
|
|
|
type.close(); |
|
|
|
|
|
isUpgrade.value = false; |
|
|
|
|
|
print("Ota 对象资源已释放"); |
|
|
print("Ota 对象资源已释放"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 内部设置事件监听(只执行一次) |
|
|
// /// 内部设置事件监听(只执行一次) |
|
|
void _setupEventListener() { |
|
|
// void _setupEventListener() { |
|
|
if (_isEventListenerSet) return; |
|
|
// if (_isEventListenerSet) return; |
|
|
|
|
|
|
|
|
eventStream.listen((event) { |
|
|
// eventStream.listen((event) { |
|
|
if (event.event == 'onProgress' && |
|
|
// if (event.event == 'onProgress' && |
|
|
event.type != null && |
|
|
// event.type != null && |
|
|
event.progress != null) { |
|
|
// event.progress != null) { |
|
|
print("flutter 收到数据${event.progress}type=${type.value}"); |
|
|
// print("flutter 收到数据${event.progress}type=${type}"); |
|
|
progress.value = event.progress!; |
|
|
// progress = event.progress!; |
|
|
type.value = event.type!; |
|
|
// type = event.type!; |
|
|
} |
|
|
// } |
|
|
if (event.event == 'onStartOTA') { |
|
|
// if (event.event == 'onStartOTA') { |
|
|
type.value = OtaEvent.START_UPGRADING; |
|
|
// type = OtaEvent.START_UPGRADING; |
|
|
isUpgrade.value = true; // 开始升级 |
|
|
// isUpgrade = true; // 开始升级 |
|
|
} |
|
|
// } |
|
|
if (event.event == 'onStopOTA') { |
|
|
// if (event.event == 'onStopOTA') { |
|
|
type.value = OtaEvent.UPGRADE_SUCCESS; |
|
|
// type = OtaEvent.UPGRADE_SUCCESS; |
|
|
isUpgrade.value = false; |
|
|
// isUpgrade = false; |
|
|
} |
|
|
// } |
|
|
if (event.event == 'onError' && |
|
|
// if (event.event == 'onError' && |
|
|
event.code != null && |
|
|
// event.code != null && |
|
|
event.message != null) { |
|
|
// event.message != null) { |
|
|
print("flutter onError${event.code}type=${event.message}"); |
|
|
// print("flutter onError${event.code}type=${event.message}"); |
|
|
type.value = OtaEvent.UPGRADE_FAILED; |
|
|
// type = OtaEvent.UPGRADE_FAILED; |
|
|
isUpgrade.value = false; |
|
|
// isUpgrade = false; |
|
|
} |
|
|
// } |
|
|
if (event.event == 'NOCONNECTION') { |
|
|
// if (event.event == 'NOCONNECTION') { |
|
|
type.value = OtaEvent.CONNECTION_DISCONNECTION; |
|
|
// type = OtaEvent.CONNECTION_DISCONNECTION; |
|
|
isUpgrade.value = false; |
|
|
// isUpgrade = false; |
|
|
} |
|
|
// } |
|
|
if (event.event == 'CONNECTION_SUCCESS') { |
|
|
// if (event.event == 'CONNECTION_SUCCESS') { |
|
|
type.value = OtaEvent.CONNECTION_SUCCESS; |
|
|
// type = OtaEvent.CONNECTION_SUCCESS; |
|
|
} |
|
|
// } |
|
|
}); |
|
|
// }); |
|
|
|
|
|
|
|
|
_isEventListenerSet = true; |
|
|
// _isEventListenerSet = true; |
|
|
} |
|
|
// } |
|
|
|
|
|
|
|
|
Future<String?> startUpgrading(String? filePath) { |
|
|
Future<String?> startUpgrading(String? filePath) { |
|
|
return OtaPlatform.instance.startUpgrading(filePath); |
|
|
return OtaPlatform.instance.startUpgrading(filePath); |
|
|
|