123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import 'dart:async';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'index.dart';
- class EcgViewController extends GetxController {
- EcgViewController();
- /// 每秒的数据量
- static int get dataPerSecond => 125;
- /// 更新帧率
- static int updateFrameRate = 25;
- /// 更新周期
- static int updatePeriod = 1000 ~/ updateFrameRate;
- /// 画布时间跨度(秒)
- int get timeSpan => 3;
- /// 横坐标数据量
- int get xDataCount => dataPerSecond * timeSpan;
- /// 所有心电数据
- List<int> allPoints = [];
- /// 需绘制的新数据
- List<int> newPointsToDraw = [];
- /// 需绘制的历史数据
- List<int> oldPointsToDraw = [];
- /// 启动时时间戳
- int startTime = DateTime.now().millisecondsSinceEpoch;
- /// 当前数据位(根据时间戳计算)
- int currentDataIndex = 0;
- /// 数据刷新定时器
- Timer timer = Timer(Duration.zero, () {});
- /// 发生错误时暂停了
- bool isPaused = false;
- /// 读取到数据的回调
- void addData(List<int> data) {
- if (allPoints.isEmpty) {
- startTime = DateTime.now().millisecondsSinceEpoch;
- _startTimer();
- }
- allPoints.addAll(data);
- if (isPaused) {
- isPaused = false;
- _startTimer();
- }
- }
- /// 开启定时器,每隔一定时间添加一次数据,并且更新UI
- void _startTimer() {
- timer = Timer.periodic(
- Duration(milliseconds: updatePeriod),
- (timer) {
- // print("timer: ${timer.tick}");
- _updateData();
- },
- );
- }
- /// 每帧更新数据
- void _updateData() {
- // 计算当前数据位
- currentDataIndex = (DateTime.now().millisecondsSinceEpoch - startTime) ~/
- (1000 ~/ dataPerSecond);
- /// 需显示的数据量
- int needDataCount = currentDataIndex % xDataCount;
- /// 当前周期数
- int currentPeriod = currentDataIndex ~/ xDataCount;
- // 计算新数据
- try {
- newPointsToDraw = allPoints.sublist(
- currentDataIndex - needDataCount,
- currentDataIndex,
- );
- if (currentPeriod > 0) {
- oldPointsToDraw = allPoints.sublist(
- (currentPeriod - 1) * xDataCount,
- currentPeriod * xDataCount,
- );
- }
- // print(
- // "update newPointsToDraw: ${currentDataIndex} ${needDataCount} ${newPointsToDraw.length} --currentPeriod ${currentPeriod}");
- } catch (e) {
- print(e);
- timer.cancel();
- isPaused = true;
- }
- update(['ecg_view']);
- }
- void openFullScreenDialog() {
- print("当前点总数为:${allPoints.length}");
- if (allPoints.length < dataPerSecond * 30) {
- PromptBox.toast("未完成检测,数据量不足");
- return;
- }
- Get.dialog(
- const FullScreenEcgDataDialog(),
- );
- }
- void reset() {
- allPoints.clear();
- newPointsToDraw.clear();
- oldPointsToDraw.clear();
- startTime = DateTime.now().millisecondsSinceEpoch;
- currentDataIndex = 0;
- timer.cancel();
- isPaused = false;
- update(['ecg_view']);
- }
- // @override
- // void onInit() {
- // super.onInit();
- // }
- @override
- void onReady() {
- super.onReady();
- print("onReady");
- }
- @override
- void onClose() {
- super.onClose();
- timer.cancel();
- }
- }
|