ruby-macrodroid 0.9.9 → 0.9.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,6 +61,7 @@ class Trigger < MacroObject
61
61
  {|x| 'c: ' + x.to_summary(colour: colour, indent: 1)}.join(" %s " % operator)
62
62
 
63
63
  @constraints.any? ? @s + "\n" + constraints : @s
64
+ super()
64
65
 
65
66
  end
66
67
 
@@ -85,7 +86,7 @@ end
85
86
  # ["Any Network"]
86
87
  # ["some Wifi SSID"] - 1 or more SSID can be supplied
87
88
 
88
- class WifiConnectionTrigger < Trigger
89
+ class WifiConnectionTrigger2 < Trigger
89
90
 
90
91
  def initialize(h={})
91
92
 
@@ -216,7 +217,7 @@ class PowerButtonToggleTrigger < Trigger
216
217
  end
217
218
 
218
219
  def to_s(colour: false)
219
- 'PowerButtonToggleTrigger ' + @h.inspect
220
+ "Power Button Toggle (%s)" % @h[:num_toggles] #+ @h.inspect
220
221
  end
221
222
 
222
223
  alias to_summary to_s
@@ -457,7 +458,7 @@ class WebHookTrigger < Trigger
457
458
 
458
459
  url = "https://trigger.macrodroid.com/%s/%s" % \
459
460
  [@deviceid, @h[:identifier]]
460
- @s = 'WebHook (Url)' + "\n " + url
461
+ @s = 'WebHook (Url)' + "\n" + url
461
462
  super()
462
463
 
463
464
  end
@@ -469,8 +470,15 @@ end
469
470
  #
470
471
  class WifiConnectionTrigger < Trigger
471
472
 
472
- def initialize(h={})
473
+ def initialize(obj=nil)
473
474
 
475
+ h = if obj.is_a? Hash then
476
+ obj
477
+ elsif obj.is_a? Array
478
+ e, macro = obj
479
+ {ssid_list: [e.text('item/description').to_s], wifi_state: 2}
480
+ end
481
+
474
482
  options = {
475
483
  ssid_list: [],
476
484
  wifi_state: 0
@@ -482,10 +490,15 @@ class WifiConnectionTrigger < Trigger
482
490
 
483
491
  def to_s(colour: false)
484
492
  access_point = @h[:ssid_list].first
485
- 'Connected to network ' + access_point
493
+ @s = 'Connected to network'
494
+ @s += "\n" + access_point
495
+ super()
486
496
  end
487
497
 
488
- alias to_summary to_s
498
+ def to_summary()
499
+ access_point = @h[:ssid_list].first
500
+ 'Connected to network' + access_point
501
+ end
489
502
  end
490
503
 
491
504
  # Category: Connectivity
@@ -1010,7 +1023,7 @@ class MusicPlayingTrigger < DeviceEventsTrigger
1010
1023
  def to_s(colour: false)
1011
1024
 
1012
1025
  event = @h[:option] == 0 ? 'Started' : 'Stopped'
1013
- @s = 'Music/Sound Playing' + "\n %s" % event #+ @h.inspect
1026
+ @s = 'Music/Sound Playing' + "\n%s" % event #+ @h.inspect
1014
1027
  super()
1015
1028
 
1016
1029
  end
@@ -1026,8 +1039,15 @@ end
1026
1039
  #
1027
1040
  class NFCTrigger < DeviceEventsTrigger
1028
1041
 
1029
- def initialize(h={})
1030
-
1042
+ def initialize(obj=nil)
1043
+
1044
+ h = if obj.is_a? Hash then
1045
+ obj
1046
+ elsif obj.is_a? Array
1047
+ e, macro = obj
1048
+ {tag_name: e.text('item/description').to_s}
1049
+ end
1050
+
1031
1051
  options = {
1032
1052
  }
1033
1053
 
@@ -1367,8 +1387,24 @@ end
1367
1387
  #
1368
1388
  class ActivityRecognitionTrigger < SensorsTrigger
1369
1389
 
1370
- def initialize(h={})
1371
-
1390
+ def initialize(obj=nil)
1391
+
1392
+ #puts 'obj: ' + obj.inspect
1393
+
1394
+ h = if obj.is_a? Hash then
1395
+
1396
+ obj
1397
+
1398
+ elsif obj.is_a? Array
1399
+
1400
+ e, macro, h2 = obj
1401
+ #puts 'h2: ' + h2.inspect
1402
+ #puts 'e: ' + e.xml
1403
+ s = e.text('item/description').to_s
1404
+ #puts 's: ' + s.inspect
1405
+ {confidence_level: s[/\d+%$/].to_i}.merge h2
1406
+ end
1407
+
1372
1408
  options = {
1373
1409
  confidence_level: 50,
1374
1410
  selected_index: 1
@@ -1380,9 +1416,18 @@ class ActivityRecognitionTrigger < SensorsTrigger
1380
1416
 
1381
1417
  end
1382
1418
 
1419
+ def match?(detail={})
1420
+ @h[:selected_index] == detail[:selected_index]
1421
+ end
1422
+
1383
1423
  def to_s(colour: false)
1424
+
1384
1425
  activity = @activity[@h[:selected_index]]
1385
- 'Activity - ' + activity
1426
+ @s = 'Activity - ' + activity #+ @h.inspect
1427
+ @s += "\nConfidence >= %s%%" % @h[:confidence_level]
1428
+
1429
+ super()
1430
+
1386
1431
  end
1387
1432
 
1388
1433
  def to_summary(colour: false)
@@ -1499,6 +1544,19 @@ class FlipDeviceTrigger < SensorsTrigger
1499
1544
 
1500
1545
  end
1501
1546
 
1547
+ def match?(detail={})
1548
+
1549
+ fd = detail[:face_down]
1550
+
1551
+ b = if fd.is_a? String then
1552
+ fd.downcase == 'true'
1553
+ else
1554
+ fd
1555
+ end
1556
+
1557
+ @h[:face_down] == b
1558
+ end
1559
+
1502
1560
  def to_pc()
1503
1561
  @h[:face_down] ? 'flip_device_down?' : 'flip_device_up?'
1504
1562
  end
@@ -1577,7 +1635,7 @@ class ShortcutTrigger < Trigger
1577
1635
  end
1578
1636
 
1579
1637
  def to_s(colour: false)
1580
- 'ShortcutTrigger ' + @h.inspect
1638
+ 'Shortcut Launched' #+ @h.inspect
1581
1639
  end
1582
1640
 
1583
1641
  alias to_summary to_s
@@ -1633,8 +1691,15 @@ end
1633
1691
  #
1634
1692
  class MediaButtonPressedTrigger < Trigger
1635
1693
 
1636
- def initialize(h={})
1694
+ def initialize(obj=nil)
1637
1695
 
1696
+ h = if obj.is_a? Hash then
1697
+ obj
1698
+ elsif obj.is_a? Array
1699
+ e, macro = obj
1700
+ {option: e.text('item/description').to_s}
1701
+ end
1702
+
1638
1703
  options = {
1639
1704
  option: 'Single Press',
1640
1705
  cancel_press: false
@@ -1645,7 +1710,50 @@ class MediaButtonPressedTrigger < Trigger
1645
1710
  end
1646
1711
 
1647
1712
  def to_s(colour: false)
1648
- 'MediaButtonPressedTrigger ' + @h.inspect
1713
+
1714
+ @s = 'Media Button Pressed ' #+ @h.inspect
1715
+ @s += "\n" + @h[:option]
1716
+ super()
1717
+
1718
+ end
1719
+
1720
+ alias to_summary to_s
1721
+ end
1722
+
1723
+ class MediaButtonV2Trigger < Trigger
1724
+
1725
+ def initialize(obj=nil)
1726
+
1727
+ @a = %w(Play Play/Pause Pause Stop Previous Next Headset Hook)
1728
+
1729
+ h = if obj.is_a? Hash then
1730
+ obj
1731
+ elsif obj.is_a? Array
1732
+ e, macro = obj
1733
+ s = e.text('item/description').to_s
1734
+ a = s.split(/, /)
1735
+ {options_enabled_array: @a.map {|x| a.include? x } }
1736
+ end
1737
+
1738
+
1739
+
1740
+ options = {
1741
+ options_enabled_array: [true, false, false, false, false, false, false]
1742
+ }
1743
+
1744
+ super(options.merge h)
1745
+
1746
+ end
1747
+
1748
+ def to_s(colour: false)
1749
+
1750
+
1751
+ options = @a.zip(@h[:options_enabled_array]).select(&:last).map(&:first)
1752
+
1753
+ @s = 'Media Button V2 ' #+ @h.inspect
1754
+ @s += "\n" + options.join(', ')
1755
+ super()
1756
+
1649
1757
  end
1650
1758
 
1651
1759
  alias to_summary to_s
@@ -1655,8 +1763,22 @@ end
1655
1763
  #
1656
1764
  class SwipeTrigger < Trigger
1657
1765
 
1658
- def initialize(h={})
1659
-
1766
+ def initialize(obj=nil)
1767
+
1768
+ h = if obj.is_a? Hash then
1769
+ obj
1770
+ elsif obj.is_a? Array
1771
+
1772
+ e, macro = obj
1773
+ s = e.text('item/description').to_s
1774
+ start, motion = s.split(/ - /,2)
1775
+
1776
+ {
1777
+ swipe_start_area: ['Top Left', 'Top Right'].index(start),
1778
+ swipe_motion: %w(Across Diagonal Down).index(motion)
1779
+ }
1780
+ end
1781
+
1660
1782
  options = {
1661
1783
  swipe_start_area: 0,
1662
1784
  swipe_motion: 0,
@@ -1666,9 +1788,29 @@ class SwipeTrigger < Trigger
1666
1788
  super(options.merge h)
1667
1789
 
1668
1790
  end
1791
+
1792
+ def match?(detail={}, model=nil)
1793
+
1794
+ puts 'detail : ' + detail.inspect
1795
+
1796
+ return false if detail.empty?
1797
+
1798
+ detail[:swipe_start_area] = detail[:start]
1799
+ detail[:swipe_motion] = detail[:motion]
1800
+ return unless detail[:swipe_start_area] and detail[:swipe_motion]
1801
+
1802
+ @h[:swipe_start_area] == detail[:swipe_start_area].to_i and \
1803
+ @h[:swipe_motion] == detail[:swipe_motion].to_i
1804
+
1805
+ end
1669
1806
 
1670
1807
  def to_s(colour: false)
1671
- 'SwipeTrigger ' + @h.inspect
1808
+
1809
+ direction = [['Top Left', 'Top Right'][@h[:swipe_start_area]],
1810
+ %w(Across Diagonal Down)[@h[:swipe_motion]]].join(' - ')
1811
+ @s = 'Swipe Screen'# + @h.inspect
1812
+ @s += "\n" + direction
1813
+ super()
1672
1814
  end
1673
1815
 
1674
1816
  alias to_summary to_s
@@ -0,0 +1,219 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: triggersnlp.rb
4
+
5
+
6
+ class TriggersNlp
7
+ include AppRoutes
8
+ using ColouredText
9
+
10
+ def initialize(macro=nil)
11
+
12
+ super()
13
+ params = {macro: macro}
14
+ triggers(params)
15
+
16
+ end
17
+
18
+ def triggers(params)
19
+
20
+ # -- Battery/Power ---------------------------------------------
21
+
22
+ get /Power Button Toggle \((\d)\)/i do |num|
23
+ [PowerButtonToggleTrigger, {num_toggles: num.to_i}]
24
+ end
25
+
26
+ get /^Power Connected: (Wired \([^\)]+\))/i do |s|
27
+
28
+ h = {
29
+ power_connected_options: [true, true, true],
30
+ has_set_usb_option: true,
31
+ power_connected: true
32
+ }
33
+
34
+ a = ['Wired (Fast Charge)', 'Wireless', 'Wired (Slow Charge)']
35
+
36
+ puts ('s: ' + s.inspect).debug
37
+
38
+ options = s.downcase.split(/ \+ /)
39
+ puts ('options: ' + options.inspect).debug
40
+
41
+ h[:power_connected_options] = a.map {|x| options.include? x.downcase }
42
+
43
+ [ExternalPowerTrigger, h]
44
+ end
45
+
46
+ get /^Power Connected: Any/i do |s|
47
+
48
+ h = {
49
+ power_connected_options: [true, true, true],
50
+ has_set_usb_option: true,
51
+ power_connected: true
52
+ }
53
+
54
+ [ExternalPowerTrigger, h]
55
+ end
56
+
57
+ # -- Connectivity ----------------------------------------------------
58
+ #
59
+
60
+ # Wifi State Change
61
+
62
+ get /^Connected to network (.*)$/i do |network|
63
+ [WifiConnectionTrigger, {ssid_list: [network], wifi_state: 2 }]
64
+ end
65
+
66
+ get /^Connected to network$/i do
67
+ [WifiConnectionTrigger, {}]
68
+ end
69
+
70
+ # -- Device Events ----------------------------------------------------
71
+
72
+ get /^NFC Tag$/i do |state|
73
+ [NFCTrigger, {}]
74
+ end
75
+
76
+ get /^Screen[ _](On|Off)/i do |state|
77
+ [ScreenOnOffTrigger, {screen_on: state.downcase == 'on'}]
78
+ end
79
+
80
+ # e.g. at 7:30pm daily
81
+ get /^(?:at )?(\d+:\d+(?:[ap]m)?) daily/i do |time, days|
82
+ [TimerTrigger, {time: time,
83
+ days: %w(Mon Tue Wed Thu Fri Sat Sun).join(', ')}]
84
+ end
85
+
86
+ get /^(?:at )?(\d+:\d+(?:[ap]m)?) (?:on )?(.*)/i do |time, days|
87
+ [TimerTrigger, {time: time, days: days}]
88
+ end
89
+
90
+ # time.is? 'at 18:30pm on Mon or Tue'
91
+ get /^time.is\? ['"](?:at )?(\d+:\d+(?:[ap]m)?) (?:on )?(.*)['"]/i do |time, days|
92
+ [TimerTrigger, {time: time, days: days.gsub(' or ',', ')}]
93
+ end
94
+
95
+ get /^shake[ _]device\??$/i do
96
+ [ShakeDeviceTrigger, {}]
97
+ end
98
+
99
+ get /^Flip Device (.*)$/i do |motion|
100
+ facedown = motion =~ /Face Up (?:->|to) Face Down/i
101
+ [FlipDeviceTrigger, {face_down: facedown }]
102
+ end
103
+
104
+ get /^flip_device_down\?$/i do
105
+ [FlipDeviceTrigger, {face_down: true }]
106
+ end
107
+
108
+ get /^flip_device_up\?$/i do
109
+ [FlipDeviceTrigger, {face_down: false }]
110
+ end
111
+
112
+ get /^Failed Login Attempt$/i do
113
+ [FailedLoginTrigger, {}]
114
+ end
115
+
116
+ get /^failed_login?$/i do
117
+ [FailedLoginTrigger, {}]
118
+ end
119
+
120
+ get /^Geofence (Entry|Exit) \(([^\)]+)/i do |direction, name|
121
+ enter_area = direction.downcase.to_sym == :entry
122
+ [GeofenceTrigger, {name: name, enter_area: enter_area}]
123
+ end
124
+
125
+ get /^location (entered|exited) \(([^\)]+)/i do |direction, name|
126
+ enter_area = direction.downcase.to_sym == :entered
127
+ [GeofenceTrigger, {name: name, enter_area: enter_area}]
128
+ end
129
+
130
+ # eg. Proximity Sensor (Near)
131
+ #
132
+ get /^Proximity Sensor \(([^\)]+)\)/i do |distance|
133
+
134
+ [ProximityTrigger, {distance: distance}]
135
+ end
136
+
137
+ # eg. Proximity near
138
+ #
139
+ get /^Proximity (near|far|slow wave|fast wave)/i do |distance|
140
+
141
+ [ProximityTrigger, {distance: distance}]
142
+ end
143
+
144
+ get /^WebHook \(Url\)/i do
145
+ [WebHookTrigger, params]
146
+ end
147
+
148
+ get /^WebHook/i do
149
+ [WebHookTrigger, params]
150
+ end
151
+
152
+ get /^wh/i do
153
+ [WebHookTrigger, params]
154
+ end
155
+
156
+ #-- MacroDroid specific ---------------------------------------------------------------
157
+
158
+ get /^EmptyTrigger$/i do
159
+ [EmptyTrigger, params]
160
+ end
161
+
162
+ #-- Sensors ---------------------------------------------------------------
163
+
164
+ get /^Activity - (.*)$/i do |s|
165
+
166
+ a = ['In Vehicle', 'On Bicycle', 'Running', 'Walking', 'Still']
167
+ r = a.find {|x| x.downcase == s.downcase}
168
+ h = r ? {selected_index: a.index(r)} : {}
169
+ [ActivityRecognitionTrigger , h]
170
+ end
171
+
172
+ # -- User Input ---------------------------------------------------------------
173
+
174
+ get /^Media Button Pressed$/i do
175
+ [MediaButtonPressedTrigger, {}]
176
+ end
177
+
178
+ get /^Media Button V2$/i do
179
+ [MediaButtonV2Trigger, {}]
180
+ end
181
+
182
+ get /^Shortcut Launched$/i do
183
+ [ShortcutTrigger, {}]
184
+ end
185
+
186
+ get /^Swipe Screen$/i do
187
+ [SwipeTrigger, {}]
188
+ end
189
+
190
+ get /^Swipe (top left) (across|diagonal|down)$/i do |start, motion|
191
+
192
+ swipe_motion = case motion.downcase.to_sym
193
+ when :across
194
+ 0
195
+ when :diagonal
196
+ 1
197
+ when :down
198
+ 2
199
+ end
200
+
201
+ h = {
202
+ swipe_start_area: (start.downcase == 'top left' ? 0 : 1),
203
+ swipe_motion: swipe_motion
204
+ }
205
+
206
+ [SwipeTrigger, h]
207
+
208
+ end
209
+
210
+ end
211
+
212
+ alias find_trigger run_route
213
+
214
+ def to_s(colour: false)
215
+ 'TriggersNlp ' + @h.inspect
216
+ end
217
+
218
+ alias to_summary to_s
219
+ end