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.



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/remotedroid.rb', line 490

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.



487
488
489
# File 'lib/remotedroid.rb', line 487

def control
  @control
end

#macrosObject

Returns the value of attribute macros.



488
489
490
# File 'lib/remotedroid.rb', line 488

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



487
488
489
# File 'lib/remotedroid.rb', line 487

def model
  @model
end

#storeObject

Returns the value of attribute store.



488
489
490
# File 'lib/remotedroid.rb', line 488

def store
  @store
end

#titleObject

Returns the value of attribute title.



488
489
490
# File 'lib/remotedroid.rb', line 488

def title
  @title
end

Instance Method Details

#export(s) ⇒ Object



507
508
509
# File 'lib/remotedroid.rb', line 507

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

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



511
512
513
514
515
516
517
518
# File 'lib/remotedroid.rb', line 511

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’



524
525
526
# File 'lib/remotedroid.rb', line 524

def op()
  @model.op
end

#query(id = nil) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/remotedroid.rb', line 528

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



571
572
573
# File 'lib/remotedroid.rb', line 571

def request(s)
  @model.request s
end

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



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/remotedroid.rb', line 576

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



605
606
607
608
# File 'lib/remotedroid.rb', line 605

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