ruby-macrodroid 0.9.2 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d378de53e069fe32854e621ab9b72b08a0785f99957f57cf4933d650287be069
4
- data.tar.gz: 856870eab31f6fffd85f55f1a61315dbec075f9bcbe89b8111e15eca5a064b54
3
+ metadata.gz: 3784001df302ddb9f8389ea019f860925803b96913f4531839df140f48634857
4
+ data.tar.gz: 686838c12129f1cd329d72745f37a7cb81ed91d98cdea059a5b59938d6d8b9e3
5
5
  SHA512:
6
- metadata.gz: 42ec4a29e2a2976767cd62eaa3630388acd4777a07ea72ba95549d2ba0f184d8e8e8fb433385d1610c796c4bcecd9dc613372f702fe9c15e626c0717d26a13ab
7
- data.tar.gz: 7fec30fda5d214a892b703ba5cf9ea3e1c9d0b6a5d7bf72221c6fa286558807d7f629c7fc0f6c0b858161873dc2e1a3323c7598fc3c61788848f72e848cabea8
6
+ metadata.gz: 948ceb9a759bf97fd4e4786fe35f95aee1a76d971ced0553c3984a885bae87155a18747a77c27d2daf7e928847cd503e19820e648956b0a93a62ef056a5992a9
7
+ data.tar.gz: d9ae51714a522375bd2aa92410f4a9412d83903d29ca33892cdd55a61dfab08faf7d02da292bdda2506715ca6948cdd308e0be4a03eb16b8010f5b56dd291ce0
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -38,6 +38,7 @@ require 'rxfhelper'
38
38
  require 'chronic_cron'
39
39
 
40
40
 
41
+ # PASTE_START
41
42
 
42
43
  MODEL =<<EOF
43
44
  device
@@ -145,8 +146,9 @@ class MacroDroid
145
146
  puts 'before RowX.new' if @debug
146
147
 
147
148
  s2 = s.gsub(/^g:/,'geofence:').gsub(/^m:/,'macro:')\
148
- .gsub(/^v:/,'variable:').gsub(/^t:/,'trigger:')\
149
- .gsub(/^a:/,'action:').gsub(/^c:/,'constraint:').gsub(/^#.*/,'')
149
+ .gsub(/^d:/,'description:').gsub(/^v:/,'variable:')\
150
+ .gsub(/^t:/,'trigger:').gsub(/^a:/,'action:')\
151
+ .gsub(/^c:/,'constraint:').gsub(/^#.*/,'')
150
152
 
151
153
  a = s2.split(/(?=^macro:)/)
152
154
 
@@ -577,6 +579,7 @@ class DroidSim
577
579
 
578
580
  end
579
581
 
582
+ # PASTE_END
580
583
 
581
584
  require 'ruby-macrodroid/base'
582
585
  require 'ruby-macrodroid/triggers'
@@ -33,7 +33,9 @@
33
33
 
34
34
 
35
35
  class Action < MacroObject
36
+ using ColouredText
36
37
  using Params
38
+ include ObjectX
37
39
 
38
40
  attr_reader :constraints
39
41
 
@@ -141,25 +143,39 @@ end
141
143
 
142
144
  # Category: Applications
143
145
  #
146
+
147
+
144
148
  class OpenWebPageAction < ApplicationAction
149
+ using ColouredText
145
150
 
146
151
  def initialize(obj={}, macro=nil)
147
-
148
- # puts 'obj: ' + obj[0].xml.inspect
152
+
153
+ $debug = true
154
+ puts ('obj: ' + obj.inspect).debug if $debug
149
155
 
150
156
  h = if obj.is_a? Hash then
151
157
 
152
- obj
158
+ obj.merge({macro: macro})
153
159
 
154
160
  elsif obj.is_a? Array
155
161
 
156
- puts 'obj: ' + obj.inspect if $debug
162
+ puts ('obj: ' + obj.inspect).debug if $debug
157
163
  e, macro = obj
158
164
 
159
165
  a = e.xpath('item/*')
160
166
 
161
167
  h2 = if a.any? then
162
- a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h
168
+
169
+ a.map do |node|
170
+
171
+ if node.name == 'description' and node.text.to_s =~ /: / then
172
+ node.text.to_s.split(/: +/,2).map(&:strip)
173
+ else
174
+ [node.name.to_sym, node.text.to_s.strip]
175
+ end
176
+
177
+ end.to_h
178
+
163
179
  else
164
180
  txt = e.text('item/description')
165
181
  {url: (txt || e.text)}
@@ -169,7 +185,7 @@ class OpenWebPageAction < ApplicationAction
169
185
 
170
186
  end
171
187
 
172
- puts 'h:' + h.inspect if $debug
188
+ puts ('h:' + h.inspect).debug if $debug
173
189
 
174
190
  #h[:url_to_open] = h[:url] if h[:url] and h[:url].length > 1
175
191
 
@@ -393,6 +409,7 @@ class IfConditionAction < Action
393
409
  a: true,
394
410
  constraint_list: []
395
411
  }
412
+ puts 'obj: ' + obj.inspect if $debug
396
413
 
397
414
  if obj.is_a? Hash then
398
415
 
@@ -402,18 +419,23 @@ class IfConditionAction < Action
402
419
  super(h2)
403
420
 
404
421
  elsif obj.is_a? Array
422
+
405
423
  e, macro = obj
406
- super()
407
- raw_txt = e.text('item/description') || e.text.to_s
424
+ super()
425
+ puts 'e.xml: ' + e.xml if $debug
426
+ puts 'e.text: ' + e.text.to_s.strip if $debug
427
+ raw_txt = e.text.to_s.strip[/^if [^$]+/i] || e.text('item/description')
408
428
  puts 'raw_txt: ' + raw_txt.inspect if $debug
409
429
 
410
- clause = raw_txt[/^if (.*)/i,1]
430
+ clause = raw_txt[/^If (.*)/i,1]
431
+ puts 'clause: ' + clause.inspect if $debug
411
432
  conditions = clause.split(/\s+\b(?:AND|OR)\b\s+/i)
433
+ puts 'conditions: ' + conditions.inspect if $debug
412
434
 
413
435
  cp = ConstraintsNlp.new
414
436
 
415
437
  @constraints = conditions.map do |c|
416
- puts 'c: ' + c.inspect
438
+ puts 'c: ' + c.inspect if $debug
417
439
  r = cp.find_constraint c
418
440
  puts 'found constraint ' + r.inspect if $debug
419
441
 
@@ -421,7 +443,24 @@ class IfConditionAction < Action
421
443
 
422
444
  end
423
445
  puts '@constraints: ' + @constraints.inspect if $debug
424
- {}
446
+
447
+ # find any nested actions
448
+ item = e.element('item')
449
+
450
+ if item then
451
+
452
+ ap = ActionsNlp.new
453
+ obj2 = action_to_object(ap, item, item, macro)
454
+ puts 'obj2: ' + obj2.inspect if $debug
455
+ #macro.add obj2
456
+
457
+ end
458
+
459
+ h = {
460
+ constraint_list: @constraints.map(&:to_h)
461
+ }
462
+ super(h) {}
463
+
425
464
  else
426
465
  # get the constraints
427
466
 
@@ -450,21 +489,47 @@ class IfConditionAction < Action
450
489
  out << s + constraints
451
490
  out.join("\n")
452
491
 
453
- end
492
+ end
493
+
454
494
  end
455
495
 
456
496
  class ElseAction < Action
457
497
 
458
- def initialize(h={})
498
+ def initialize(obj=[])
459
499
 
460
500
  options = {
461
- constraint_list: ''
501
+ constraint_list: []
462
502
  }
503
+
504
+ if obj.is_a? Hash then
505
+
506
+ h = obj
463
507
 
464
- super(options.merge h)
508
+ super(options.merge h)
509
+
510
+ elsif obj.is_a? Array
511
+
512
+ e, macro = obj
513
+
514
+ # find any nested actions
515
+ item = e.element('item')
516
+
517
+ if item then
518
+
519
+ ap = ActionsNlp.new
520
+ obj2 = action_to_object(ap, item, item, macro)
521
+ puts 'obj2: ' + obj2.inspect if $debug
522
+ #macro.add obj2
523
+
524
+ end
525
+
526
+ super(options)
527
+ end
528
+
465
529
 
466
530
 
467
- end
531
+
532
+ end
468
533
 
469
534
  def to_s(colour: false, indent: 0)
470
535
  'Else'
@@ -644,6 +709,9 @@ class SetHotspotAction < ConnectivityAction
644
709
 
645
710
  def initialize(h={})
646
711
 
712
+ # to-do: check when *disable hotspot*, is the
713
+ # *enable wifi* option selected?
714
+
647
715
  options = {
648
716
  device_name: "", state: 0, turn_wifi_on: true, use_legacy_mechanism: false, mechanism: 0
649
717
 
@@ -654,8 +722,15 @@ class SetHotspotAction < ConnectivityAction
654
722
  end
655
723
 
656
724
  def to_s(colour: false, indent: 0)
657
- action = @h[:turn_wifi_on] ? 'Enable' : 'Disable'
658
- action + ' HotSpot'
725
+
726
+ @s = "%s HotSpot" % [@h[:state] == 0 ? 'Enable' : 'Disable']
727
+
728
+ if @h[:state] == 1 then
729
+ @s += "\n" + (@h[:turn_wifi_on] ? 'Enable WiFi' : 'Don\'t Enable Wifi')
730
+ end
731
+
732
+ super()
733
+
659
734
  end
660
735
  end
661
736
 
@@ -1066,7 +1141,17 @@ end
1066
1141
  class VibrateAction < DeviceSettingsAction
1067
1142
 
1068
1143
  def initialize(h={})
1069
-
1144
+
1145
+ pattern = [
1146
+ 'Blip', 'Short Buzz', 'Long Buzz', 'Rapid', 'Slow', 'Increasing',
1147
+ 'Constant', 'Decreasing', 'Final Fantasy', 'Game Over', 'Star Wars',
1148
+ 'Mini Blip', 'Micro Blip'
1149
+ ]
1150
+
1151
+ if h[:pattern] then
1152
+ h[:vibrate_pattern] = pattern.map(&:downcase).index h[:pattern]
1153
+ end
1154
+
1070
1155
  options = {
1071
1156
  vibrate_pattern: 1
1072
1157
  }
@@ -1596,6 +1681,7 @@ end
1596
1681
  # MacroDroid Specific
1597
1682
  #
1598
1683
  class SetVariableAction < Action
1684
+ using ColouredText
1599
1685
 
1600
1686
  def initialize(obj=nil)
1601
1687
 
@@ -1604,7 +1690,14 @@ class SetVariableAction < Action
1604
1690
  elsif obj.is_a? Array
1605
1691
  e, macro = obj
1606
1692
  node = e.element('item/*')
1607
- macro.set_var node.name, node.value.to_s
1693
+ #puts ("node.name: %s node.value: %s" % [node.name, node.value]).debug
1694
+ r = macro.set_var node.name, node.value.to_s
1695
+ puts ('r: ' + r.inspect).debug if $debug
1696
+ r
1697
+ if r[:type] == 2 then
1698
+ { variable: {name: r[:name], type: r[:type]}, new_string_value: r[:string_value]
1699
+ }
1700
+ end
1608
1701
  end
1609
1702
 
1610
1703
  options = {
@@ -2324,8 +2417,31 @@ end
2324
2417
  # Category: Screen
2325
2418
  #
2326
2419
  class ScreenOnAction < ScreenAction
2420
+ using ColouredText
2327
2421
 
2328
- def initialize(h={})
2422
+ def initialize(obj=nil)
2423
+
2424
+ debug = false
2425
+
2426
+ h = if obj.is_a? Hash then
2427
+
2428
+ obj
2429
+
2430
+ elsif obj.is_a? Array
2431
+ =begin
2432
+ puts 'obj: ' + obj.inspect if debug
2433
+ e, macro = obj
2434
+ puts ('e: ' + e.xml.inspect).debug if debug
2435
+ a = e.xpath('item/*')
2436
+
2437
+ txt = e.text.to_s
2438
+ puts ('txt: ' + txt.inspect).debug if debug
2439
+ state = txt[/Screen (On|Off)/i,1]
2440
+
2441
+ {screen_off: state.downcase == 'off'}
2442
+ =end
2443
+ {}
2444
+ end
2329
2445
 
2330
2446
  options = {
2331
2447
  pie_lock_screen: false,
@@ -2381,8 +2497,46 @@ end
2381
2497
  # disable keep awake => enabled: false
2382
2498
  #
2383
2499
  class KeepAwakeAction < ScreenAction
2500
+ using ColouredText
2384
2501
 
2385
- def initialize(h={})
2502
+ def initialize(obj=nil)
2503
+
2504
+
2505
+ h = if obj.is_a? Hash then
2506
+
2507
+ obj
2508
+
2509
+ elsif obj.is_a? Array
2510
+
2511
+ puts 'obj: ' + obj.inspect if $debug
2512
+ e, macro = obj
2513
+
2514
+ a = e.xpath('item/*')
2515
+
2516
+ txt = e.text('item/description')
2517
+
2518
+ h2 = if txt then
2519
+
2520
+ raw_duration = (txt || e.text).to_s
2521
+ puts 'raw_duration: ' + raw_duration.inspect if $debug
2522
+ duration = raw_duration[/Screen On - ([^$]+)/i]
2523
+ {duration: duration}
2524
+
2525
+ elsif a.any? then
2526
+ a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h
2527
+ end
2528
+
2529
+ h2.merge(macro: macro)
2530
+
2531
+ end
2532
+
2533
+ puts ('h: ' + h.inspect).debug if $debug
2534
+
2535
+ if h[:duration] then
2536
+
2537
+ h[:seconds_to_stay_awake_for] = Subunit.hms_to_seconds(h[:duration])
2538
+
2539
+ end
2386
2540
 
2387
2541
  options = {
2388
2542
  enabled: true,
@@ -7,7 +7,104 @@
7
7
  # MacroObject
8
8
  #
9
9
 
10
+ module ObjectX
11
+
12
+
13
+ def action_to_object(ap, e, item, macro)
14
+
15
+ debug = false
16
+
17
+ puts 'inside action_to_object: item.xml: ' + item.xml if debug
18
+
19
+ if item.element('description') then
20
+
21
+ item.xpath('description').map do |description|
22
+
23
+ inner_lines = description.text.to_s.strip.lines
24
+ puts 'inner_lines: ' + inner_lines.inspect if debug
25
+
26
+ action = if e.text.to_s.strip.empty? then
27
+ inner_lines.shift.strip
28
+ else
29
+ e.text.strip
30
+ end
31
+
32
+ puts 'action: ' + action.inspect if debug
33
+
34
+ r = ap.find_action action
35
+ puts 'r: ' + r.inspect if debug
36
+
37
+ if r[1].any? then
38
+
39
+ macro.add r[0].new(r[1])
40
+
41
+ else
42
+
43
+ puts 'description: ' + description.xml.inspect if debug
44
+ #o = r[0].new([description, self]) if r
45
+ index = macro.actions.length
46
+ macro.add Action.new
47
+ o = object_create(r[0],[description, macro]) if r
48
+ macro.actions[index] = o
49
+ puts 'after o' if debug
50
+ o
51
+
52
+ end
53
+ end
54
+
55
+ else
56
+
57
+ action = e.text.strip
58
+ puts 'action: ' + action.inspect if $debug
59
+ r = ap.find_action action
60
+
61
+ a = e.xpath('item/*')
62
+
63
+ h = if a.any? then
64
+ a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h
65
+ else
66
+ {}
67
+ end
68
+ puts 'h: ' + h.inspect if $debug
69
+
70
+ #r = ap.find_action action
71
+ #r[0].new(h.merge(macro: self)) if r
72
+ o = object_create(r[0], h.merge(macro: macro)) if r
73
+ macro.add o
74
+ o
75
+
76
+ end
77
+
78
+
79
+ end
10
80
 
81
+ def object_create(klass, *args)
82
+
83
+ begin
84
+ klass.new(*args)
85
+ rescue
86
+ raise MacroError, klass.to_s + ': ' + ($!).to_s
87
+ end
88
+ end
89
+
90
+ def varify(label, value='')
91
+
92
+ type = VAR_TYPES[value.class.to_s.to_sym]
93
+
94
+ h = {
95
+ boolean_value: false,
96
+ decimal_value: 0.0,
97
+ int_value: 0,
98
+ name: label,
99
+ string_value: '',
100
+ type: type[0]
101
+ }
102
+ h[type[1]] = value
103
+ h
104
+
105
+ end
106
+
107
+ end
11
108
 
12
109
  class MacroObject
13
110
  using ColouredText
@@ -97,11 +194,11 @@ class MacroObject
97
194
  private
98
195
 
99
196
  def hashify(h)
100
-
197
+ puts 'h: ' + h.inspect
101
198
  h2 = h.inject({}) do |r,x|
102
- puts 'x: ' + x.inspect if $debug
199
+ puts 'x: ' + x.inspect #if $debug
103
200
  key, value = x
104
- puts 'key: ' + key.inspect if $debug
201
+ puts 'key: ' + key.inspect #if $debug
105
202
  new_key = key.to_s.gsub(/\w_\w/){|x| x[0] + x[-1].upcase}
106
203
  new_key = new_key.prepend 'm_' unless @list.include? new_key
107
204
  new_key = 'm_SIGUID' if new_key == 'm_siguid'
@@ -794,6 +794,24 @@ class MacroDroidVariableConstraint < Constraint
794
794
 
795
795
  def initialize(h={})
796
796
 
797
+ if h[:loperand] then
798
+ h[:variable] = {}
799
+ h[:variable][:name] = h[:loperand]
800
+ h[:variable][:type] = 2
801
+
802
+ if h[:roperand] =~ /true|false/i then
803
+
804
+ val = h[:roperand].downcase == 'true'
805
+ h[:boolean_value] = val
806
+ h[:variable][:type] = 0
807
+ h[:variable][:boolean_value] = val
808
+ h[:string_value] = h[:roperand].capitalize
809
+ else
810
+ h[:string_value] = h[:roperand]
811
+ end
812
+
813
+ end
814
+
797
815
  options = {
798
816
 
799
817
  :enable_regex=>false,
@@ -825,11 +843,24 @@ class MacroDroidVariableConstraint < Constraint
825
843
 
826
844
  def to_s(colour: false, indent: 0)
827
845
 
828
- a = [:int_greater_than, :int_less_than, :int_not_equal,
829
- :string_equal].zip(['>','<','!=', '='])
830
- operator = a.find {|label,_| @h[label]}.last
846
+ a = [:int_greater_than, :int_less_than, :int_not_equal,
847
+ :string_equal].zip(['>','<','!=', '='])
848
+ operator = a.find {|label,_| @h[label]}.last
849
+
850
+ var = @h[:variable]
851
+
852
+ type = case var[:type]
853
+ when 0 # boolean
854
+ :boolean_value
855
+ when 1 # integer
856
+ :int_value
857
+ when 2 # string
858
+ :string_value
859
+ when 3 # decimal
860
+ :decimal_Value
861
+ end
831
862
 
832
- @s = "%s %s %s" % [@h[:variable][:name], operator, @h[:int_value]]
863
+ @s = "%s %s %s" % [@h[:variable][:name], operator, @h[type]]
833
864
  super()
834
865
  end
835
866
 
@@ -888,7 +919,7 @@ class TriggerThatInvokedConstraint < Constraint
888
919
 
889
920
  def initialize(h={})
890
921
 
891
- puts ('h: ' + h.inspect).green
922
+ puts ('h: ' + h.inspect).green if $debug
892
923
  @trigger = h[:macro].triggers.find {|x| x.siguid == h[:si_guid_that_invoked] }
893
924
 
894
925
  options = {
@@ -21,8 +21,11 @@ VAR_TYPES = {
21
21
  Float: [3, :decimal_value]
22
22
  }
23
23
 
24
+
25
+
24
26
  class TriggersNlp
25
27
  include AppRoutes
28
+ using ColouredText
26
29
 
27
30
  def initialize(macro=nil)
28
31
 
@@ -32,8 +35,40 @@ class TriggersNlp
32
35
 
33
36
  end
34
37
 
35
- def triggers(params)
38
+ def triggers(params)
39
+
40
+ # -- Battery/Power ---------------------------------------------
36
41
 
42
+ get /^Power Connected: (Wired \([^\)]+\))/i do |s|
43
+
44
+ h = {
45
+ power_connected_options: [true, true, true],
46
+ has_set_usb_option: true,
47
+ power_connected: true
48
+ }
49
+
50
+ a = ['Wired (Fast Charge)', 'Wireless', 'Wired (Slow Charge)']
51
+
52
+ puts ('s: ' + s.inspect).debug
53
+
54
+ options = s.downcase.split(/ \+ /)
55
+ puts ('options: ' + options.inspect).debug
56
+
57
+ h[:power_connected_options] = a.map {|x| options.include? x.downcase }
58
+
59
+ [ExternalPowerTrigger, h]
60
+ end
61
+
62
+ get /^Power Connected: Any/i do |s|
63
+
64
+ h = {
65
+ power_connected_options: [true, true, true],
66
+ has_set_usb_option: true,
67
+ power_connected: true
68
+ }
69
+
70
+ [ExternalPowerTrigger, h]
71
+ end
37
72
  # e.g. at 7:30pm daily
38
73
  get /^(?:at )?(\d+:\d+(?:[ap]m)?) daily/i do |time, days|
39
74
  [TimerTrigger, {time: time,
@@ -133,13 +168,25 @@ class ActionsNlp
133
168
  def initialize(macro=nil)
134
169
 
135
170
  super()
171
+
136
172
  params = {macro: macro}
137
173
  actions(params)
138
174
 
139
175
  end
140
176
 
141
177
  def actions(params)
142
-
178
+
179
+ # -- Connectivity ------------------------------------------------------
180
+
181
+ get /^(Enable|Disable) HotSpot/i do |state|
182
+ enable, state = if state.downcase == 'enable' then
183
+ [true, 0]
184
+ else
185
+ [false, 1]
186
+ end
187
+ [SetHotspotAction, {turn_wifi_on: enable, state: state }]
188
+ end
189
+
143
190
  # e.g. message popup: hello world!
144
191
  get /^message popup: (.*)/i do |msg|
145
192
  [ToastAction, {msg: msg}]
@@ -192,6 +239,14 @@ class ActionsNlp
192
239
  [SpeakTextAction, {}]
193
240
  end
194
241
 
242
+ get /^Vibrate \(([^\)]+)/i do |pattern|
243
+ [VibrateAction, {pattern: pattern}]
244
+ end
245
+
246
+ get /^Vibrate$/i do |pattern|
247
+ [VibrateAction, {pattern: 'short buzz'}]
248
+ end
249
+
195
250
  # e.g. Display Notification: Hi there: This is the body of the message
196
251
  get /^Display Notification: ([^:]+): [^$]+$/i do |subject, text|
197
252
  [NotificationAction, {subject: subject, text: text}]
@@ -246,13 +301,13 @@ class ActionsNlp
246
301
 
247
302
  #
248
303
  get /^WebHook \(Url\)/i do
249
- [OpenWebPageAction, params]
304
+ [OpenWebPageAction, {}]
250
305
  end
251
306
 
252
307
  # e.g. webhook entered_kitchen
253
308
  #
254
309
  get /^webhook$/i do
255
- [OpenWebPageAction, params]
310
+ [OpenWebPageAction, {}, params[:macro]]
256
311
  end
257
312
 
258
313
  # -- Location ---------------------------------------------------------
@@ -262,7 +317,7 @@ class ActionsNlp
262
317
  end
263
318
 
264
319
  get /^Share Location$/i do
265
- [ShareLocationAction, params]
320
+ [ShareLocationAction, {}]
266
321
  end
267
322
 
268
323
  #a: Keep Device Awake Screen On Until Disabled
@@ -285,6 +340,10 @@ class ActionsNlp
285
340
  [KeepAwakeAction, h]
286
341
  end
287
342
 
343
+ get /Keep Device Awake$/i do
344
+ [KeepAwakeAction, {}]
345
+ end
346
+
288
347
  #a: Disable Keep Awake
289
348
  #
290
349
  get /Disable Keep Awake/i do
@@ -297,6 +356,10 @@ class ActionsNlp
297
356
  [IfConditionAction, {}]
298
357
  end
299
358
 
359
+ get /else/i do
360
+ [ElseAction, {}]
361
+ end
362
+
300
363
  get /End If/i do
301
364
  [EndIfAction, {}]
302
365
  end
@@ -306,6 +369,12 @@ class ActionsNlp
306
369
  get /^Set Variable$/i do
307
370
  [SetVariableAction, {}]
308
371
  end
372
+
373
+ # -- Screen ------------------------------------------------
374
+ #
375
+ get /^Screen (On|Off)$/i do |state|
376
+ [ScreenOnAction, {screen_off: state.downcase == 'off'}]
377
+ end
309
378
 
310
379
  end
311
380
 
@@ -326,10 +395,58 @@ class ConstraintsNlp
326
395
  end
327
396
 
328
397
  def constraints(params)
398
+
399
+ # Device State
400
+
401
+ get /^Device (locked|unlocked)/i do |state|
402
+ [DeviceLockedConstraint, {locked: state.downcase == 'locked'}]
403
+ end
329
404
 
330
405
  get /^airplane mode (.*)/i do |state|
331
406
  [AirplaneModeConstraint, {enabled: (state =~ /^enabled|on$/i) == 0}]
332
407
  end
408
+
409
+ #
410
+
411
+ # -- MacroDroid specific -----------------------------------------------------------------------
412
+
413
+ get /^(\w+) (=) (\[?\w+\]?)/i do |loperand, operator, roperand|
414
+
415
+ h = {
416
+ loperand: loperand,
417
+ operator: operator,
418
+ roperand: roperand
419
+ }
420
+
421
+ [MacroDroidVariableConstraint, h]
422
+
423
+ end
424
+
425
+ # -- Sensors -----------------------------------
426
+ #
427
+ get /^Light Sensor (Less|Greater) than (50.0)lx/i do |operator, val|
428
+
429
+ level, option = operator.downcase == 'less' ? [-1,0] : [1,1]
430
+
431
+ h = {
432
+ light_level: level,
433
+ light_level_float: val,
434
+ option: option
435
+ }
436
+
437
+ [LightLevelConstraint, h]
438
+ end
439
+
440
+ get /^Proximity Sensor: (Near|Far)/i do |distance|
441
+ [ProximitySensorConstraint, {near: distance.downcase == 'near'}]
442
+ end
443
+
444
+
445
+ # -- Screen and Speaker ---------------------------
446
+ #
447
+ get /^Screen (On|Off)/i do |state|
448
+ [ScreenOnOffConstraint, {screen_on: state.downcase == 'on'}]
449
+ end
333
450
 
334
451
  end
335
452
 
@@ -344,6 +461,7 @@ end
344
461
  class Macro
345
462
  using ColouredText
346
463
  using Params
464
+ include ObjectX
347
465
 
348
466
  attr_reader :local_variables, :triggers, :actions, :constraints,
349
467
  :guid, :deviceid
@@ -363,6 +481,8 @@ class Macro
363
481
  end
364
482
 
365
483
  def add(obj)
484
+ puts 'inside add; ' + obj.inspect if @debug
485
+ puts '@actions: ' + @actions.inspect if @debug
366
486
 
367
487
  if obj.kind_of? Trigger then
368
488
 
@@ -523,6 +643,21 @@ class Macro
523
643
 
524
644
  @title = node.text('macro') || node.attributes[:name]
525
645
 
646
+ d = node.element('description')
647
+
648
+ if d then
649
+
650
+ desc = []
651
+ desc << d.text.strip
652
+
653
+ if d.element('item/description') then
654
+ desc << d.text('item/description').strip
655
+ end
656
+
657
+ @description = desc.join("\n")
658
+
659
+ end
660
+
526
661
  node.xpath('variable').each {|e| set_var(*e.text.to_s.split(/: */,2)) }
527
662
 
528
663
  #@description = node.attributes[:description]
@@ -606,68 +741,25 @@ class Macro
606
741
 
607
742
  ap = ActionsNlp.new self
608
743
 
609
- @actions = node.xpath('action').flat_map do |e|
744
+ node.xpath('action').each do |e|
610
745
 
611
746
  puts 'action e: ' + e.xml.inspect if @debug
612
747
  puts 'e.text ' + e.text if @debug
613
748
 
614
749
  item = e.element('item')
750
+
615
751
  if item then
616
752
 
617
- if item.element('description') then
618
-
619
- item.xpath('description').map do |description|
620
-
621
- inner_lines = description.text.to_s.strip.lines
622
- puts 'inner_lines: ' + inner_lines.inspect if @debug
623
-
624
- action = if e.text.to_s.strip.empty? then
625
- inner_lines.shift.strip
626
- else
627
- e.text.strip
628
- end
629
-
630
- puts 'action: ' + action.inspect if @debug
631
-
632
- r = ap.find_action action
633
- puts 'r: ' + r.inspect if @debug
634
- puts 'description: ' + description.xml.inspect if @debug
635
- #o = r[0].new([description, self]) if r
636
- o = object_create(r[0],[description, self]) if r
637
- puts 'after o' if @debug
638
- o
639
-
640
- end
641
-
642
- else
643
-
644
- action = e.text.strip
645
- puts 'action: ' + action.inspect if @debug
646
- r = ap.find_action action
647
-
648
- a = e.xpath('item/*')
649
-
650
- h = if a.any? then
651
- a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h
652
- else
653
- {}
654
- end
655
- puts 'h: ' + h.inspect if @debug
656
-
657
- #r = ap.find_action action
658
- #r[0].new(h.merge(macro: self)) if r
659
- object_create(r[0], h.merge(macro: self)) if r
660
-
661
- end
753
+ action_to_object(ap, e, item, self)
662
754
 
663
755
  else
664
756
 
665
757
  action = e.text.strip
666
758
  r = ap.find_action action
667
759
  #r[0].new(r[1]) if r
668
- object_create(r[0],r[1]) if r
760
+ self.add object_create(r[0],*r[1..-1]) if r
669
761
 
670
- end
762
+ end
671
763
 
672
764
  end
673
765
 
@@ -741,7 +833,10 @@ class Macro
741
833
  if not @local_variables.has_key? label.to_sym then
742
834
  @local_variables.merge!({label.to_sym => value})
743
835
  end
744
-
836
+
837
+ if @debug then
838
+ puts ("before varify; label: %s value: %s" % [label, value]).debug
839
+ end
745
840
  varify(label, value)
746
841
  end
747
842
 
@@ -955,31 +1050,7 @@ EOF
955
1050
 
956
1051
  end
957
1052
 
958
- def object_create(klass, *args)
959
-
960
- begin
961
- klass.new(*args)
962
- rescue
963
- raise MacroError, klass.to_s + ': ' + ($!).to_s
964
- end
965
- end
966
-
967
- def varify(label, value='')
968
-
969
1053
 
970
- type = VAR_TYPES[value.class.to_s.to_sym]
971
1054
 
972
- h = {
973
- boolean_value: false,
974
- decimal_value: 0.0,
975
- int_value: 0,
976
- name: label,
977
- string_value: '',
978
- type: type[0]
979
- }
980
- h[type[1]] = value
981
- h
982
-
983
- end
984
1055
 
985
1056
  end
@@ -244,7 +244,7 @@ class ExternalPowerTrigger < Trigger
244
244
 
245
245
  return 'Power Disconnected' unless @h[:power_connected]
246
246
 
247
- status = 'Power Connectd'
247
+ status = 'Power Connected'
248
248
  options = if @h[:power_connected_options].all? then
249
249
  'Any'
250
250
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-macrodroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  NZ2kdBIUDnAM24e0/wXdVxg4HnsZbdymxyzMQ4P5pKYcpI6oisBxI37p/Xy+wAg3
36
36
  SBHno3GEuuD8ZWj24IMJpfbp
37
37
  -----END CERTIFICATE-----
38
- date: 2020-10-05 00:00:00.000000000 Z
38
+ date: 2020-10-14 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: glw
metadata.gz.sig CHANGED
Binary file