values.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  2. import 'package:vid/us/vid_us_unit.dart';
  3. /// 测量结果
  4. abstract class ValueBase {
  5. /// 格式元
  6. final ItemOutputMeta meta;
  7. String? _name;
  8. /// 数据单位
  9. VidUsUnit unit;
  10. ValueBase(this.meta, this.unit);
  11. /// 计算结果名称
  12. String get name => _name ?? meta.name;
  13. set name(String? val) {
  14. if (val != _name) {
  15. _name = val;
  16. }
  17. }
  18. /// 目标单位
  19. VidUsUnit get metaUnit => meta.unit;
  20. double? pickFloat() {
  21. if (this is FloatValue) {
  22. final f = (this as FloatValue).value;
  23. return f;
  24. }
  25. return null;
  26. }
  27. }
  28. abstract class TypedValueBase<T> extends ValueBase {
  29. T? _value;
  30. TypedValueBase(
  31. ItemOutputMeta meta,
  32. T? val,
  33. VidUsUnit unit,
  34. ) : super(meta, unit) {
  35. _value = val;
  36. }
  37. /// 测量结果值
  38. T? get value => _value;
  39. set value(T? val) {
  40. if (val != _value) {
  41. _value = val;
  42. }
  43. }
  44. }
  45. /// 浮点值
  46. class FloatValue extends TypedValueBase<double> {
  47. int _fractionalDigits = 2;
  48. FloatValue(
  49. ItemOutputMeta meta,
  50. double? val,
  51. VidUsUnit unit,
  52. ) : super(meta, val, unit);
  53. /// 小数精度
  54. int get fractionalDigits => _fractionalDigits;
  55. set fractionalDigits(int value) {
  56. if (value != _fractionalDigits) {
  57. _fractionalDigits = value;
  58. }
  59. }
  60. UnitFloatValue? toUnitFloatValue() {
  61. if (value != null) {
  62. return UnitFloatValue(value!, unit);
  63. }
  64. return null;
  65. }
  66. }
  67. /// 字符串值
  68. class StringValue extends TypedValueBase<String> {
  69. StringValue(
  70. ItemOutputMeta meta,
  71. String? val,
  72. VidUsUnit unit,
  73. ) : super(meta, val, unit);
  74. }
  75. class UnitFloatValue extends UnitValue<double> {
  76. UnitFloatValue(super.value, super.unit);
  77. }
  78. class UnitValue<T> {
  79. final VidUsUnit unit;
  80. final T value;
  81. UnitValue(this.value, this.unit);
  82. }