Class: Macro

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-macrodroid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, debug: false) ⇒ Macro

Returns a new instance of Macro.



198
199
200
201
202
203
204
205
206
207
# File 'lib/ruby-macrodroid.rb', line 198

def initialize(name=nil, debug: false)

  @title, @debug = name, debug
  
  puts 'inside Macro#initialize' if @debug    
        
  @local_variables, @triggers, @actions, @constraints = [], [], [], []
  @h = {}
  
end

Instance Attribute Details

#actions ⇒ Object (readonly)

Returns the value of attribute actions.



195
196
197
# File 'lib/ruby-macrodroid.rb', line 195

def actions
  @actions
end

#constraints ⇒ Object (readonly)

Returns the value of attribute constraints.



195
196
197
# File 'lib/ruby-macrodroid.rb', line 195

def constraints
  @constraints
end

#description ⇒ Object

Returns the value of attribute description.



196
197
198
# File 'lib/ruby-macrodroid.rb', line 196

def description
  @description
end

#local_variables ⇒ Object (readonly)

Returns the value of attribute local_variables.



195
196
197
# File 'lib/ruby-macrodroid.rb', line 195

def local_variables
  @local_variables
end

#title ⇒ Object

Returns the value of attribute title.



196
197
198
# File 'lib/ruby-macrodroid.rb', line 196

def title
  @title
end

#triggers ⇒ Object (readonly)

Returns the value of attribute triggers.



195
196
197
# File 'lib/ruby-macrodroid.rb', line 195

def triggers
  @triggers
end

Instance Method Details

#add(obj) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ruby-macrodroid.rb', line 209

def add(obj)

  if obj.kind_of? Trigger then
    
    puts 'trigger found' if @debug
    @triggers << obj
    
  elsif obj.kind_of? Action
    
    puts 'action found' if @debug
    @actions << obj
    
  elsif obj.kind_of? Constraint
    
    puts 'constraint found' if @debug
    @constraints << obj
    
  end
  
end

#import_h(h) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ruby-macrodroid.rb', line 252

def import_h(h)

  if @debug then
    puts 'inside import_h'
    puts 'h:' + h.inspect
  end
  
  @title = h[:name]
  @description = h[:description]
  
  # fetch the local variables
  @local_variables = h['local_variables']
  
  # fetch the triggers
  @triggers = h[:trigger_list].map do |trigger|
    
    object(trigger.to_snake_case)

  end

  @actions = h[:action_list].map do |action|
    object(action.to_snake_case)
  end

  # fetch the constraints                               
  @constraints = h[:constraint_list].map do |constraint|
    object(constraint.to_snake_case)
  end                               
  
  @h = h

  %i(local_variables m_trigger_list m_action_list m_constraint_list)\
    .each {|x| @h[x] = [] }

  @h

end

#import_xml(node) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/ruby-macrodroid.rb', line 290

def import_xml(node)
  
  if @debug then
    puts 'inside Macro#import_xml'
    puts 'node: ' + node.xml.inspect
  end
  
  @title = node.attributes[:name]
  @description = node.attributes[:description]

  if node.element('triggers') then
    
    # level 2
    
    # get all the triggers
    @triggers = node.xpath('triggers/*').map do |e|
      
      puts 'e.name: ' + e.name.inspect if @debug
      {timer: TimerTrigger}[e.name.to_sym].new(e.attributes.to_h)
      
    end

    # get all the actions
    @actions = node.xpath('actions/*').map do |e|
      
      if e.name == 'notification' then
        
        case e.attributes[:type].to_sym
        when :popup          
          e.attributes.delete :type
          ToastAction.new e.attributes.to_h
        end
        
      end

    end    
                             
    # get all the constraints
    @constraints = node.xpath('constraints/*').map do |e|
      
      puts 'e.name: ' + e.name.inspect if @debug
      {airplanemode: AirplaneModeConstraint}[e.name.to_sym].new(e.attributes.to_h)

    end                                  
    
  else
    
    # Level 1
    
    tp = TriggersNlp.new      
    
    @triggers = node.xpath('trigger').map do |e|
      
      r = tp.find_trigger e.text
      
      puts 'found trigger ' + r.inspect if @debug
      
      if r then
        r[0].new(r[1])
      end
      
    end
    
    ap = ActionsNlp.new      
    
    @actions = node.xpath('action').map do |e|
      
      r = ap.find_action e.text
      puts 'found action ' + r.inspect if @debug
      
      if r then
        r[0].new(r[1])
      end
      
    end      
                             
    cp = ConstraintsNlp.new      
    
    @constraints = node.xpath('constraint').map do |e|
      
      r = cp.find_constraint e.text
      puts 'found constraint ' + r.inspect if @debug
      
      if r then
        r[0].new(r[1])
      end
      
    end                                   
    
  end
  
  self
  
end

#match?(triggerx, detail = {time: $env[:time]}, model = nil) ⇒ Boolean

Returns:

  • (Boolean)


385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/ruby-macrodroid.rb', line 385

def match?(triggerx, detail={time: $env[:time]}, model=nil )
              
  if @triggers.any? {|x| x.type == triggerx and x.match?(detail, model) } then
    
    if @debug then
      puts 'checking constraints ...' 
      puts '@constraints: ' + @constraints.inspect
    end
    
    if @constraints.all? {|x| x.match?($env.merge(detail), model) } then
    
      true
      
    else

      return false
      
    end
    
  end
  
end

#run ⇒ Object



408
409
410
# File 'lib/ruby-macrodroid.rb', line 408

def run()
  @actions.map(&:invoke)
end

#to_h ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ruby-macrodroid.rb', line 230

def to_h()

  h = {
    local_variables: @local_variables,
    m_trigger_list: @triggers.map(&:to_h),
    m_action_list: @actions.map(&:to_h),
    m_constraint_list: @constraints.map(&:to_h),
    m_description: '',
    m_name: title(),
    m_excludeLog: false,
    m_GUID: guid(),
    m_isOrCondition: false,
    m_enabled: false,
    m_descriptionOpen: false,
    m_headingColor: 0
  }
  
  puts 'h: ' + h.inspect if @debug

  @h.merge(h)
end

#to_pc ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/ruby-macrodroid.rb', line 412

def to_pc()
  
  heading = '# ' + @title + "\n"
  heading += '# ' + @description if @description
  condition = @triggers.first.to_pc
  actions = @actions.map(&:to_pc).join("\n")
  
<<EOF
#{heading}

if #{condition} then
#{actions}
end
EOF
end

#to_s ⇒ Object



428
429
430
431
432
433
434
435
436
437
# File 'lib/ruby-macrodroid.rb', line 428

def to_s()
  a = [
    'm: ' + @title,
    @triggers.map {|x| "t: %s" % x}.join("\n"),
    @actions.map {|x| "a: %s" % x}.join("\n"),
    @constraints.map {|x| "a: %s" % x}.join("\n")
  ]
  a.insert(1, 'd: ' + @description) if @description
  a.join("\n")
end