Class: OpenHAB::Core::Types::PointType

Inherits:
Object
  • Object
show all
Includes:
Command, State
Defined in:
lib/openhab/core/types/point_type.rb

Overview

PointType can be used for items that are dealing with GPS or location awareness functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latitude, longitude, altitude) ⇒ PointType

Returns a new instance of PointType.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openhab/core/types/point_type.rb', line 18

def initialize(*args)
  if (2..3).cover?(args.length)
    args = args.each_with_index.map do |value, index|
      if value.is_a?(DecimalType) || value.is_a?(StringType)
        value
      elsif value.is_a?(QuantityType)
        unit = (index == 2) ? DSL.unit(SIUnits::METRE.dimension) || SIUnits::METRE : Units::DEGREE_ANGLE
        DecimalType.new(value.to_unit(unit).to_big_decimal)
      elsif value.respond_to?(:to_str)
        StringType.new(value.to_str)
      elsif value.respond_to?(:to_d)
        DecimalType.new(value)
      end
    end
  end

  super(*args)
end

Instance Attribute Details

#altitudeQuantityType (readonly)

Returns:



94
95
96
# File 'lib/openhab/core/types/point_type.rb', line 94

def altitude
  QuantityType.new(raw_altitude.to_big_decimal, SIUnits::METRE)
end

#latitudeQuantityType (readonly)

Returns:



82
83
84
# File 'lib/openhab/core/types/point_type.rb', line 82

def latitude
  QuantityType.new(raw_latitude.to_big_decimal, Units::DEGREE_ANGLE)
end

#longitudeQuantityType (readonly)

Returns:



88
89
90
# File 'lib/openhab/core/types/point_type.rb', line 88

def longitude
  QuantityType.new(raw_longitude.to_big_decimal, Units::DEGREE_ANGLE)
end

Instance Method Details

#==(other) ⇒ true, false

Check equality with type conversion

Parameters:

  • other (PointType, String)

    object to compare to

Returns:

  • (true, false)


56
57
58
59
60
61
62
63
64
65
# File 'lib/openhab/core/types/point_type.rb', line 56

def ==(other)
  logger.trace { "(#{self.class}) #{self} == #{other} (#{other.class})" }
  if other.instance_of?(self.class)
    equals(other)
  elsif other.respond_to?(:coerce)
    return false unless (lhs, rhs = other.coerce(self))

    lhs == rhs
  end
end

#distance_from(other) ⇒ QuantityType Also known as: -

Calculate the distance in meters from other, ignoring altitude.

This algorithm also ignores the oblate spheroid shape of Earth and assumes a perfect sphere, so results are inexact.

Returns:

Raises:

  • (TypeError)


105
106
107
108
109
110
# File 'lib/openhab/core/types/point_type.rb', line 105

def distance_from(other)
  logger.trace("(#{self}).distance_from(#{other} (#{other.class})")
  raise TypeError, "#{other.class} can't be coerced into #{self.class}" unless other.is_a?(PointType)

  QuantityType.new(raw_distance_from(other), SIUnits::METRE)
end

#eql?(other) ⇒ true, false

Check equality without type conversion

Returns:

  • (true, false)

    if the same value is represented, without type conversion



42
43
44
45
46
# File 'lib/openhab/core/types/point_type.rb', line 42

def eql?(other)
  return false unless other.instance_of?(self.class)

  equals(other)
end