You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.8 KiB
145 lines
3.8 KiB
import 'ota_platform_interface.dart';
|
|
import 'dart:async';
|
|
import 'dart:typed_data';
|
|
import 'package:flutter/services.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<dynamic, dynamic> 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',
|
|
);
|
|
// double progress = 0.0;
|
|
// late int type = OtaEvent.NOT_STARTED;
|
|
// bool isUpgrade = false;
|
|
|
|
/// 事件流
|
|
Stream<OtaEvent>? _eventStream;
|
|
// bool _isEventListenerSet = false; // 防止重复监听
|
|
|
|
/// 获取事件流
|
|
Stream<OtaEvent> 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;
|
|
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}");
|
|
// progress = event.progress!;
|
|
// type = event.type!;
|
|
// }
|
|
// if (event.event == 'onStartOTA') {
|
|
// type = OtaEvent.START_UPGRADING;
|
|
// isUpgrade = true; // 开始升级
|
|
// }
|
|
// if (event.event == 'onStopOTA') {
|
|
// type = OtaEvent.UPGRADE_SUCCESS;
|
|
// isUpgrade = false;
|
|
// }
|
|
// if (event.event == 'onError' &&
|
|
// event.code != null &&
|
|
// event.message != null) {
|
|
// print("flutter onError${event.code}type=${event.message}");
|
|
// type = OtaEvent.UPGRADE_FAILED;
|
|
// isUpgrade = false;
|
|
// }
|
|
// if (event.event == 'NOCONNECTION') {
|
|
// type = OtaEvent.CONNECTION_DISCONNECTION;
|
|
// isUpgrade = false;
|
|
// }
|
|
// if (event.event == 'CONNECTION_SUCCESS') {
|
|
// type = OtaEvent.CONNECTION_SUCCESS;
|
|
// }
|
|
// });
|
|
|
|
// _isEventListenerSet = true;
|
|
// }
|
|
|
|
Future<void> startUpgrading({String? address, String? filePath}) {
|
|
return OtaPlatform.instance.startUpgrading(
|
|
address: address,
|
|
filePath: filePath,
|
|
);
|
|
}
|
|
|
|
Future<void> onOtaClose() {
|
|
return OtaPlatform.instance.onOtaClose();
|
|
}
|
|
}
|
|
|