Class: Motel::Callbacks::Proximity

Inherits:
Omega::Server::Callback show all
Defined in:
lib/motel/callbacks/proximity.rb

Overview

Defines a Omega::Server::Callback to only invoke callback if two locations are within the specified maximum distance of each other.

TODO make to_location optional such that if its omitted any location may trigger the proximity event should the conditions be satisfied.

The client may specify the maximum overall distance and/or the maximum distance along any axis (x,y,z).

note all maximum conditions will need to be met to trigger handler! So if maximum_distance and max_x are specified, the location will have need to have moved both the minimum overall distance and the minimum distance along the x axis.

Instance Attribute Summary collapse

Attributes inherited from Omega::Server::Callback

#endpoint_id, #event_type, #handler, #only_if, #rjr_event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Omega::Server::Callback

#should_invoke?

Constructor Details

#initialize(args = {}, &block) ⇒ Proximity

Motel::Callbacks::Movement initializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize callback with

Options Hash (args):

  • :event,'event' (String)

    proximity event on which to trigger hander see #event

  • :max_distance,'max_distance' (Integer)

    maximum distance locations can be apart to trigger handler

  • :max_x,'max_x' (Integer)

    maximum distance locations can be apart to trigger handler

  • :max_y,'max_y' (Integer)

    maximum distance locations can be apart to trigger handler

  • :max_z,'max_z' (Integer)

    maximum distance locations can be apart to trigger handler

  • :to_location,'to_location' (Motel::Location)

    location which to compare that specified to Omega::Server::Callback#invoke to to determine proximity


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/motel/callbacks/proximity.rb', line 78

def initialize(args = {}, &block)
  attr_from_args args,
                 :max_distance => 0,
                 :max_x => 0, :max_y => 0, :max_z => 0,
                 :to_location => nil, :event => :proximity

  self.event = event.intern if event.is_a?(String)

  # keep track of proximity state internally for different event types
  @locations_in_proximity = false

  # only run handler if conditions are met
  @only_if = proc { |*args| self.check_proximity(*args) }

  super(args, &block)
end

Instance Attribute Details

#eventObject

:proximity,:entered_proximity,:left_proximity

Proximity event which to trigger on.

May correspond to:

  • :proximity - trigger callback handler every time when locations are in proximity

  • :entered_proximity - trigger callback handler only once after every time locations enter proximity of each other

  • :left_proximityy - trigger callback handler only once after every time locations leave proximity of each other


34
35
36
# File 'lib/motel/callbacks/proximity.rb', line 34

def event
  @event
end

#max_distanceObject

Max distance the locations needs to be apart to trigger event


37
38
39
# File 'lib/motel/callbacks/proximity.rb', line 37

def max_distance
  @max_distance
end

#max_xObject

Max x,y,z distance the locations need to be to trigger the event


40
41
42
# File 'lib/motel/callbacks/proximity.rb', line 40

def max_x
  @max_x
end

#max_yObject

Max x,y,z distance the locations need to be to trigger the event


40
41
42
# File 'lib/motel/callbacks/proximity.rb', line 40

def max_y
  @max_y
end

#max_zObject

Max x,y,z distance the locations need to be to trigger the event


40
41
42
# File 'lib/motel/callbacks/proximity.rb', line 40

def max_z
  @max_z
end

#to_locationObject

Motel::Location

which to compare proximity of other location to


26
27
28
# File 'lib/motel/callbacks/proximity.rb', line 26

def to_location
  @to_location
end

Class Method Details

.json_create(o) ⇒ Object

Create new callback from json representation


119
120
121
122
# File 'lib/motel/callbacks/proximity.rb', line 119

def self.json_create(o)
  callback = new(o['data'])
  return callback
end

Instance Method Details

#invoke(loc) ⇒ Object

Override Omega::Server::Callback#invoke, set locations_in_proximity

Parameters:


97
98
99
100
# File 'lib/motel/callbacks/proximity.rb', line 97

def invoke(loc)
  super(loc, to_location)
  @locations_in_proximity = currently_in_proximity(loc)
end

#to_json(*a) ⇒ Object

Convert callback to json representation and return it


108
109
110
111
112
113
114
115
116
# File 'lib/motel/callbacks/proximity.rb', line 108

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      { :endpoint_id => @endpoint_id, :max_distance => @max_distance,
        :max_x => @max_x, :max_y => @max_y, :max_z => @max_z,
        :to_location => @to_location, :event => @event}
  }.to_json(*a)
end

#to_sObject

Convert callback to human readable string and return it


103
104
105
# File 'lib/motel/callbacks/proximity.rb', line 103

def to_s
  "(#{@max_distance},#{@max_x},#{@max_y},#{@max_z})"
end