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, deviceid: nil, debug: false) ⇒ 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
# File 'lib/remotedroid/controller.rb', line 8

def initialize(mcs, model=MODEL, deviceid: nil, debug: false)
  
  @debug = debug
  @syslog = []
        
  @control = Control.new(deviceid)
  @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

#controlObject (readonly)

Returns the value of attribute control.



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

def control
  @control
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

#delete_allObject



38
39
40
# File 'lib/remotedroid/controller.rb', line 38

def delete_all()
  @macros = []
end

#export(s, replace: false) ⇒ Object



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

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

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



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

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

#opObject

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



62
63
64
# File 'lib/remotedroid/controller.rb', line 62

def op()
  @model.op
end

#query(id = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/remotedroid/controller.rb', line 66

def query(id=nil)
  
  return @query 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
  
  if id.downcase.to_sym == :location then
    @control.http_exec id
  elsif sys.include? id
    @control.http_exec :'query-setting-system', {qvar: id}        
  elsif global.include? id
    @control.http_exec :'query-setting-global', {qvar: id}
  elsif secure.include? id
    @control.http_exec :'query-setting-secure', {qvar: id}        
  elsif id.downcase.to_sym == :'take-picture'
    @control.http_exec id
  else
    @control.http_exec :query, {qvar: id}
  end
  
  # 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



111
112
113
# File 'lib/remotedroid/controller.rb', line 111

def request(s)
  @model.request s
end

#run_macro(macro_name: '') ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/remotedroid/controller.rb', line 115

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/remotedroid/controller.rb', line 125

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



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/remotedroid/controller.rb', line 156

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