Class: RemoteDroid::Controller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mcs, model = MODEL, deviceid: nil, debug: false) ⇒ Controller

Returns a new instance of Controller.



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/remotedroid.rb', line 327

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

end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



324
325
326
# File 'lib/remotedroid.rb', line 324

def control
  @control
end

#macrosObject

Returns the value of attribute macros.



325
326
327
# File 'lib/remotedroid.rb', line 325

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



324
325
326
# File 'lib/remotedroid.rb', line 324

def model
  @model
end

#titleObject

Returns the value of attribute title.



325
326
327
# File 'lib/remotedroid.rb', line 325

def title
  @title
end

Instance Method Details

#export(s) ⇒ Object



341
342
343
# File 'lib/remotedroid.rb', line 341

def export(s)
  @macros = MacroDroid.new(s).macros
end

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



345
346
347
# File 'lib/remotedroid.rb', line 345

def invoke(name, options={})      
  @control.method(name.to_sym).call(options)
end

#opObject

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



353
354
355
# File 'lib/remotedroid.rb', line 353

def op()
  @model.op
end

#query(trigger) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/remotedroid.rb', line 357

def query(trigger)
  
  @h[trigger] = nil
  
  # send http request via macrodroid.com API
  @control.http_exec trigger
  
  # wait for the local variable to be updated
  # timeout after 5 seoncds
  t = Time.now
  
  begin
    sleep 1
  end until @h[trigger] or Time.now > t + 5
  
  if @h[trigger] then
    yield(@h[trigger])
  end
  
end

#request(s) ⇒ Object



378
379
380
# File 'lib/remotedroid.rb', line 378

def request(s)
  @model.request s
end

#storeObject



412
413
414
# File 'lib/remotedroid.rb', line 412

def store()
  @h
end

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



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/remotedroid.rb', line 383

def trigger(name, detail={time: Time.now})
  
  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
                 
    valid_trigger
    
  end
  
  puts 'macros: ' + macros.inspect if @debug
  
  macros.flat_map(&:run)
end