Class: Macro
- Inherits:
-
Object
- Object
- Macro
- Defined in:
- lib/ruby-macrodroid.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#description ⇒ Object
Returns the value of attribute description.
-
#deviceid ⇒ Object
readonly
Returns the value of attribute deviceid.
-
#local_variables ⇒ Object
readonly
Returns the value of attribute local_variables.
-
#title ⇒ Object
Returns the value of attribute title.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
- #add(obj) ⇒ Object
- #import_h(h) ⇒ Object
- #import_xml(node) ⇒ Object
-
#initialize(name = nil, geofences: nil, deviceid: nil, debug: false) ⇒ Macro
constructor
A new instance of Macro.
- #match?(triggerx, detail = {time: $env[:time]}, model = nil) ⇒ Boolean
-
#run ⇒ Object
invokes the actions.
-
#set_env ⇒ Object
prepares the environment in order for triggers to test fire successfully Used for testing.
- #to_h ⇒ Object
- #to_pc ⇒ Object
- #to_s(colour: false) ⇒ Object
- #to_summary(colour: false) ⇒ Object
Constructor Details
#initialize(name = nil, geofences: nil, deviceid: nil, debug: false) ⇒ Macro
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ruby-macrodroid.rb', line 294 def initialize(name=nil, geofences: nil, deviceid: nil, debug: false) @title, @geofences, @deviceid, @debug = name, geofences, deviceid, 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.
290 291 292 |
# File 'lib/ruby-macrodroid.rb', line 290 def actions @actions end |
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
290 291 292 |
# File 'lib/ruby-macrodroid.rb', line 290 def constraints @constraints end |
#description ⇒ Object
Returns the value of attribute description.
292 293 294 |
# File 'lib/ruby-macrodroid.rb', line 292 def description @description end |
#deviceid ⇒ Object (readonly)
Returns the value of attribute deviceid.
290 291 292 |
# File 'lib/ruby-macrodroid.rb', line 290 def deviceid @deviceid end |
#local_variables ⇒ Object (readonly)
Returns the value of attribute local_variables.
290 291 292 |
# File 'lib/ruby-macrodroid.rb', line 290 def local_variables @local_variables end |
#title ⇒ Object
Returns the value of attribute title.
292 293 294 |
# File 'lib/ruby-macrodroid.rb', line 292 def title @title end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
290 291 292 |
# File 'lib/ruby-macrodroid.rb', line 290 def triggers @triggers end |
Instance Method Details
#add(obj) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/ruby-macrodroid.rb', line 305 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
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 384 385 |
# File 'lib/ruby-macrodroid.rb', line 348 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| puts 'trigger: ' + trigger.inspect #exit 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
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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 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 |
# File 'lib/ruby-macrodroid.rb', line 387 def import_xml(node) if @debug then puts 'inside Macro#import_xml' puts 'node: ' + node.xml.inspect end if node.element('triggers') then # level 2 @title = node.attributes[:name] @description = node.attributes[:description] # 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 puts 'import_xml: inside level 1' if @debug @title = node.text('macro') || node.attributes[:name] #@description = node.attributes[:description] 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 if r[0] == GeofenceTrigger then GeofenceTrigger.new(r[1], geofences: @geofences) else r[0].new(r[1]) end end end ap = ActionsNlp.new @actions = node.xpath('action').map do |e| puts 'action e: ' + e.xml.inspect if @debug r = ap.find_action e.text puts 'found action ' + r.inspect if @debug if r then a = e.xpath('item/*') h = if a.any? then a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h else {} end r[0].new(r[1].merge(h)) 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
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/ruby-macrodroid.rb', line 503 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
invokes the actions
528 529 530 |
# File 'lib/ruby-macrodroid.rb', line 528 def run() @actions.map(&:invoke) end |
#set_env ⇒ Object
prepares the environment in order for triggers to test fire successfully Used for testing
535 536 537 |
# File 'lib/ruby-macrodroid.rb', line 535 def set_env() @triggers.each(&:set_env) end |
#to_h ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/ruby-macrodroid.rb', line 326 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
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/ruby-macrodroid.rb', line 539 def to_pc() heading = '# ' + @title heading += '\n# ' + @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(colour: false) ⇒ Object
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/ruby-macrodroid.rb', line 555 def to_s(colour: false) indent = 0 actions = @actions.map do |x| s = x.to_s(colour: colour) if s.lines.length > 1 then lines = s.lines s = lines[0] + lines[1..-1].map {|x| x.prepend (' ' * indent) }.join end r = if indent <= 0 then if colour then "a".bg_blue.gray.bold + ": %s" % s else "a: %s" % s end elsif indent > 0 if s =~ /^Else/ then (' ' * (indent-1)) + "%s" % s elsif s =~ /^End/ indent -= 1 (' ' * indent) + "%s" % s else (' ' * indent) + "%s" % s end end if s =~ /^If/i then if indent < 1 then r = if colour then "a".bg_blue.gray.bold + ":\n %s" % s else "a:\n %s" % s end indent += 1 else r = (' ' * indent) + "%s" % s end indent += 1 end r end.join("\n") a = [ (colour ? "m".bg_cyan.gray.bold : 'm') + ': ' + @title, @triggers.map {|x| (colour ? "t".bg_red.gray.bold : 't') \ + ": %s" % x}.join("\n"), actions ] if @constraints.any? then a << @constraints.map do |x| (colour ? "c".bg_green.gray.bold : 'c') + ": %s" % x end.join("\n") end if @description and @description.length >= 1 then a.insert(1, (colour ? "d".bg_gray.gray.bold : 'd') + ': ' \ + @description.gsub(/\n/,"\n ")) end a.join("\n") + "\n" end |
#to_summary(colour: false) ⇒ Object
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/ruby-macrodroid.rb', line 631 def to_summary(colour: false) if colour then a = [ 'm'.bg_cyan.gray.bold + ': ' + @title, 't'.bg_red.gray.bold + ': ' + @triggers.map \ {|x| x.to_summary(colour: false)}.join(", "), 'a'.bg_blue.gray.bold + ': ' + @actions.map \ {|x| x.to_summary(colour: false)}.join(", ") ] if @constraints.any? then a << 'c'.bg_green.gray.bold + ': ' + @constraints.map \ {|x| x.to_summary(colour: false)}.join(", ") end else a = [ 'm: ' + @title, 't: ' + @triggers.map {|x| x.to_summary(colour: false)}.join(", "), 'a: ' + @actions.map {|x| x.to_summary(colour: false)}.join(", ") ] if @constraints.any? then a << 'c: ' + @constraints.map \ {|x| x.to_summary(colour: false)}.join(", ") end end a.join("\n") + "\n" end |