Class: Motel::Callbacks::Movement

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

Overview

Defines a Omega::Server::Callback to only invoke callback if a location moves a specified minimum distance.

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

This callback will be invoked with the current location and the old x,y,z coordinates individually

note all minimum conditions will need to be met to trigger handler! so if minimum_distance and min_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) ⇒ Movement

Motel::Callbacks::Movement initializer

Options Hash (args):

  • :min_distance,'min_distance' (Integer)

    minium distance location needs to move before handler in invoked

  • :min_x,'min_x' (Integer)

    minium distance location needs to move along x axis before handler in invoked

  • :min_y,'min_y' (Integer)

    minium distance location needs to move along y axis before handler in invoked

  • :min_z,'min_z' (Integer)

    minium distance location needs to move along z axis before handler in invoked


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/motel/callbacks/movement.rb', line 67

def initialize(args = {}, &block)
  attr_from_args args, :min_distance => 0,
                       :min_x => 0, :min_y => 0, :min_z => 0

  # store original coordinates internally,
  # until minimum distances are satified
  # and callback is invoked, then clear
  @orig_x = @orig_y = @orig_z = nil

  # only run this handler if minimums are specified
  @only_if = proc { |*args| self.check_distance(*args) }

  super(args)
end

Instance Attribute Details

#min_distanceObject

minimum distance the location needs to move to trigger event.


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

def min_distance
  @min_distance
end

#min_xObject

minimum x,y,z distance the location needs to move to trigger the event


29
30
31
# File 'lib/motel/callbacks/movement.rb', line 29

def min_x
  @min_x
end

#min_yObject

minimum x,y,z distance the location needs to move to trigger the event


29
30
31
# File 'lib/motel/callbacks/movement.rb', line 29

def min_y
  @min_y
end

#min_zObject

minimum x,y,z distance the location needs to move to trigger the event


29
30
31
# File 'lib/motel/callbacks/movement.rb', line 29

def min_z
  @min_z
end

Class Method Details

.json_create(o) ⇒ Object

Create new callback from json representation


110
111
112
113
# File 'lib/motel/callbacks/movement.rb', line 110

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

Instance Method Details

#invoke(loc, old_x, old_y, old_z) ⇒ Object

Override Omega::Server::Callback#invoke, call original then reset local coordinates


88
89
90
91
92
# File 'lib/motel/callbacks/movement.rb', line 88

def invoke(loc, old_x, old_y, old_z)
  d, dx, dy, dz = get_distance(loc)
  super(loc, d, dx, dy, dz)
  @orig_x = @orig_y = @orig_z = nil
end

#to_json(*a) ⇒ Object

Convert callback to json representation and return it


100
101
102
103
104
105
106
107
# File 'lib/motel/callbacks/movement.rb', line 100

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      { :endpoint_id => @endpoint_id, :min_distance => @min_distance,
        :min_x => @min_x, :min_y => @min_y, :min_z => @min_z }
  }.to_json(*a)
end

#to_sObject

Convert callback to human readable string and return it


95
96
97
# File 'lib/motel/callbacks/movement.rb', line 95

def to_s
  "(#{@min_distance},#{@min_x},#{@min_y},#{@min_z})"
end