Skip to content

Commit

Permalink
Expose maximum/minimum/step values of a numeric characteristics
Browse files Browse the repository at this point in the history
Closes #123
  • Loading branch information
karlentwistle committed Jul 17, 2023
1 parent f50b9ae commit 107ffa9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/ruby_home/characteristic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ def valid_values
constraints.fetch("ValidValues", {}).keys.map(&:to_i)
end

def minimum_value
return unless numeric_constraints?

constraints["MinimumValue"]
end

def maximum_value
return unless numeric_constraints?

constraints["MaximumValue"]
end

def step_value
return unless numeric_constraints?

constraints["StepValue"]
end

def numeric_constraints?
format == "uint32" || format == "float"
end

def method_missing(method_name, *args, &block)
value.send(method_name, *args, &block)
end
Expand Down
11 changes: 11 additions & 0 deletions lib/ruby_home/http/serializers/characteristic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def record_hash(characteristic)
}
.merge(value_hash(characteristic))
.merge(valid_values_hash(characteristic))
.merge(numeric_constraints(characteristic))
end

private
Expand All @@ -42,6 +43,16 @@ def valid_values_hash(characteristic)
{"valid-values" => characteristic.valid_values}
end
end

def numeric_constraints(characteristic)
return {} unless characteristic.numeric_constraints?

{
"minValue" => characteristic.minimum_value,
"maxValue" => characteristic.maximum_value,
"minStep" => characteristic.step_value,
}.compact
end
end
end
end

0 comments on commit 107ffa9

Please sign in to comment.