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.



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

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 = {}

end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



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

def control
  @control
end

#macrosObject

Returns the value of attribute macros.



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

def macros
  @macros
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#export(s) ⇒ Object



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

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

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



348
349
350
# File 'lib/remotedroid.rb', line 348

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’



356
357
358
# File 'lib/remotedroid.rb', line 356

def op()
  @model.op
end

#query(id) ⇒ Object



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

def query(id)
  
  @store[id] = nil
  
  # send http request via macrodroid.com API
  @control.http_exec id
  
  # 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 @store[id]

  
end

#request(s) ⇒ Object



381
382
383
# File 'lib/remotedroid.rb', line 381

def request(s)
  @model.request s
end

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



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

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(key, val) ⇒ Object



415
416
417
# File 'lib/remotedroid.rb', line 415

def update(key, val)
  @store[key.to_sym] = val      
end