Class: RemoteDroid::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/remotedroid/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mcs, model = MODEL, devices: {}, debug: false) ⇒ Controller

Returns a new instance of Controller.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/remotedroid/controller.rb', line 8

def initialize(mcs, model=MODEL, devices: {}, debug: false)
  
  @debug = debug
  @syslog = []
   
  @devices = devices
  
  @devices.each do |key, deviceid|
    devices[key] = Control.new(deviceid)
  end
  
  @macros = mcs.macros
  
  if model then
    @model = Model.new(model)
  end
  
  @store = {}
  #@query = Query.new(self)
  
  # enable the required triggers on the Android device
  #
  names = @macros.map {|x| x.triggers.first.type}.uniq
  #@control.enable names.first.to_s.gsub('_',' ')
  puts 'Enabling ' + names.join(',')
=begin      
  Thread.new do
    names.each do |title|
      @control.enable title.to_s.gsub('_',' ')
      sleep 0.8
    end
  end
=end
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



5
6
7
# File 'lib/remotedroid/controller.rb', line 5

def devices
  @devices
end

#macrosObject

Returns the value of attribute macros.



6
7
8
# File 'lib/remotedroid/controller.rb', line 6

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/remotedroid/controller.rb', line 5

def model
  @model
end

#storeObject

Returns the value of attribute store.



6
7
8
# File 'lib/remotedroid/controller.rb', line 6

def store
  @store
end

#syslogObject (readonly)

Returns the value of attribute syslog.



5
6
7
# File 'lib/remotedroid/controller.rb', line 5

def syslog
  @syslog
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/remotedroid/controller.rb', line 6

def title
  @title
end

Instance Method Details

#control(device) ⇒ Object



43
44
45
# File 'lib/remotedroid/controller.rb', line 43

def control(device)
  @devices[device]
end

#delete_allObject



47
48
49
# File 'lib/remotedroid/controller.rb', line 47

def delete_all()
  @macros = []
end

#export(s, replace: false) ⇒ Object



51
52
53
54
55
56
# File 'lib/remotedroid/controller.rb', line 51

def export(s, replace: false)
  
  macros = MacroDroid.new(s).macros
  replace ? @macros = macros : @macros.concat(macros)
  
end

#invoke(device, name, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/remotedroid/controller.rb', line 58

def invoke(device, name, options={})      
  
  if control(device).respond_to? name.to_sym then
    control(device).method(name.to_sym).call(options)
  else
    control(device).http_exec name.to_sym, options
  end
end

#local(action, options = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/remotedroid/controller.rb', line 67

def local(action, options={})
  
  case action.to_sym
  when :open_web_page
    open(options[:url_to_open]).read
  end
end

#opObject

Object Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = ‘off’



79
80
81
# File 'lib/remotedroid/controller.rb', line 79

def op()
  @model.op
end

#query(device, id = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/remotedroid/controller.rb', line 83

def query(device, id=nil)
  
  return Query.new(device, self) unless id
  
  @store[id] = nil

  sys = %i(accelerometer_rotation)      
  
  global = [:airplane_mode_on, :bluetooth_on, :cell_on, :device_name, \
            :usb_mass_storage_enabled, :wifi_on]       
  
  secure = %i(bluetooth_name flashlight_enabled)

  
  # send http request via macrodroid.com API
  
  identifier, options = if id.downcase.to_sym == :location then
    id
  elsif sys.include? id
    [:'query-setting-system', {qvar: id}]
  elsif global.include? id
    [:'query-setting-global', {qvar: id}]
  elsif secure.include? id
    [:'query-setting-secure', {qvar: id}]
  elsif id.downcase.to_sym == :'take-picture'
    id
  else
    [:query, {qvar: id}]
  end
  
  control(device).http_exec identifier, options
  
  # wait for the local variable to be updated
  # timeout after 5 seoncds
  t = Time.now
  
  begin
    sleep 1
  end until @store[id] or Time.now > t + 10
  
  return {warning: 'HTTP response timeout'} if Time.now > t+5
  
  return @store[id]

  
end

#request(s) ⇒ Object



130
131
132
# File 'lib/remotedroid/controller.rb', line 130

def request(s)
  @model.request s
end

#run_macro(macro_name: '') ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/remotedroid/controller.rb', line 134

def run_macro(macro_name: '')
  
  found = @macros.find do |macro|
    macro.title.downcase == macro_name.downcase
  end
  
  found.run if found
  
end

#trigger(name, detail = {}) ⇒ Object Also known as: trigger_fired



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/remotedroid/controller.rb', line 144

def trigger(name, detail={})
  
  macros = @macros.select do |macro|
    
    puts 'macro: '  + macro.inspect if @debug

    # fetch the associated properties from the model if possible and 
    # merge them into the detail.
    #
    valid_trigger = macro.match?(name, detail, @model.op)
    
    #puts 'valid_trigger: ' + valid_trigger.inspect if @debug
    
    #if valid_trigger then
    #  @syslog << [Time.now, :trigger, name] 
    #  @syslog << [Time.now, :macro, macro.title]
    #end
    
    @syslog << [Time.now, name, detail]
                 
    valid_trigger
    
  end
  
  puts 'macros: ' + macros.inspect if @debug
  
  macros.flat_map(&:run)
end

#update(id, val) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/remotedroid/controller.rb', line 175

def update(id, val)
  
  if @debug then
    puts 'inside update' 
    puts [id, val].inspect
  end
  
  key  = if %i(location take-picture).include? id
    id
  else
    val.keys.first.to_sym
  end
  
  @syslog << [id, val]      
  @store[key] = val   
  
end