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.



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/remotedroid.rb', line 428

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)

end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



425
426
427
# File 'lib/remotedroid.rb', line 425

def control
  @control
end

#macrosObject

Returns the value of attribute macros.



426
427
428
# File 'lib/remotedroid.rb', line 426

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



425
426
427
# File 'lib/remotedroid.rb', line 425

def model
  @model
end

#storeObject

Returns the value of attribute store.



426
427
428
# File 'lib/remotedroid.rb', line 426

def store
  @store
end

#titleObject

Returns the value of attribute title.



426
427
428
# File 'lib/remotedroid.rb', line 426

def title
  @title
end

Instance Method Details

#export(s) ⇒ Object



445
446
447
# File 'lib/remotedroid.rb', line 445

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

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



449
450
451
452
453
454
455
456
# File 'lib/remotedroid.rb', line 449

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’



462
463
464
# File 'lib/remotedroid.rb', line 462

def op()
  @model.op
end

#query(id = nil) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/remotedroid.rb', line 466

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}        
  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 + 5
  
  return {warning: 'HTTP response timeout'} if Time.now > t+5
  
  return @store[id]

  
end

#request(s) ⇒ Object



509
510
511
# File 'lib/remotedroid.rb', line 509

def request(s)
  @model.request s
end

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



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/remotedroid.rb', line 514

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

#update(id, val) ⇒ Object



543
544
545
546
# File 'lib/remotedroid.rb', line 543

def update(id, val)
  key  = id == :location ? id : val.keys.first.to_sym
  @store[key] = val      
end