import 'package:vnote_device_plugin/consts/status.dart';
import 'package:vnote_device_plugin/consts/types.dart';
import 'package:vnote_device_plugin/devices/base.dart';
import 'package:vnote_device_plugin/events/event_type.dart';
import 'package:vnote_device_plugin/models/exams/weight_height.dart';

class WeightHeightDeviceWorker extends DeviceWorkerBase {
  WeightHeightDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.WEIGHTHEIGHT);

  ///测量成功事件
  final successEvent = FEventHandler<WeightHeightExamData>();

  ///测量失败事件
  final errorEvent = FEventHandler<String>();

  @override
  void init() {
    super.init();
    injectStatusCallback(
        VDeviceEventStatus.EXAM_WEIGHT_HEIGHT_SUCCESS, _onSuccess);
    injectStatusCallback(VDeviceEventStatus.EXAM_WEIGHT_HEIGHT_ERROR, _onError);
  }

  void _onSuccess(Map<String, dynamic>? data) {
    final WeightHeightExamData examData =
        WeightHeightExamData.fromEventData(data!);
    log("Result; weight: ${examData.weight}, height: ${examData.height}");
    successEvent.emit(this, examData);
  }

  void _onError(Map<String, dynamic>? data) {
    final String msg = data!["MSG"];
    log("Error:$msg");
    errorEvent.emit(this, msg);
  }
}
