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';

/// 体重
class WeightDeviceWorker extends DeviceWorkerBase {
  WeightDeviceWorker({
    required super.model,
    required super.mac,
  }) : super(type: DeviceTypes.WEIGHT);

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

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

  @override
  void init() {
    super.init();

    injectStatusCallback(VDeviceEventStatus.EXAM_WEIGHT_SUCCESS, _onSuccess);
    injectStatusCallback(VDeviceEventStatus.EXAM_WEIGHT_ERROR, _onError);
  }

  void _onSuccess(Map<String, dynamic>? data) {
    final double value = data!["WEIGHT"].toDouble();
    log("Result: $value");
    successEvent.emit(this, value);
  }

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