ruby-macrodroid 0.7.9 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ # file: ruby-macrodroid/base.rb
2
+
3
+ # This file contains the following classes:
4
+ #
5
+ # ## Object class
6
+ #
7
+ # MacroObject
8
+ #
9
+
10
+
11
+
12
+ class MacroObject
13
+ using ColouredText
14
+
15
+ attr_reader :type, :siguid
16
+ attr_accessor :options
17
+
18
+ def initialize(h={})
19
+
20
+ $env ||= {}
21
+
22
+ @attributes = %i(constraint_list is_or_condition is_disabled siguid)
23
+ @h = {constraint_list: [], is_or_condition: false,
24
+ is_disabled: false, siguid: nil}.merge(h)
25
+ @list = []
26
+
27
+ # fetch the class name and convert from camelCase to snake_eyes
28
+ @type = self.class.to_s.sub(/Trigger|Action$/,'')\
29
+ .gsub(/\B[A-Z][a-z]/){|x| '_' + x.downcase}\
30
+ .gsub(/[a-z][A-Z]/){|x| x[0] + '_' + x[1].downcase}\
31
+ .downcase.to_sym
32
+ @constraints = []
33
+ end
34
+
35
+ def to_h()
36
+
37
+ h = @h
38
+
39
+ h2 = h.inject({}) do |r,x|
40
+ puts 'x: ' + x.inspect if @debug
41
+ key, value = x
42
+ puts 'key: ' + key.inspect if @debug
43
+ new_key = key.to_s.gsub(/\w_\w/){|x| x[0] + x[-1].upcase}
44
+ new_key = new_key.prepend 'm_' unless @list.include? new_key
45
+ new_key = 'm_SIGUID' if new_key == 'm_siguid'
46
+ r.merge(new_key => value)
47
+ end
48
+
49
+ h2.merge('m_classType' => self.class.to_s)
50
+
51
+ end
52
+
53
+ def siguid()
54
+ @h[:siguid]
55
+ end
56
+
57
+ def to_s(colour: false)
58
+
59
+ h = @h.clone
60
+ h.delete :macro
61
+ @s ||= "#<%s %s>" % [self.class, h.inspect]
62
+ operator = @h[:is_or_condition] ? 'OR' : 'AND'
63
+ constraints = @constraints.map \
64
+ {|x| x.to_summary(colour: colour)}.join(" %s " % operator)
65
+
66
+ @s + constraints
67
+
68
+ end
69
+
70
+ alias to_summary to_s
71
+
72
+ protected
73
+
74
+ def filter(options, h)
75
+
76
+ (h.keys - (options.keys + @attributes.to_a)).each {|key| h.delete key }
77
+ return h
78
+
79
+ end
80
+
81
+ def uuid()
82
+ UUID.new.generate
83
+ end
84
+
85
+ def object(h={})
86
+
87
+ puts ('inside object h:' + h.inspect).debug if @debug
88
+ klass = Object.const_get h[:class_type]
89
+ puts klass.inspect.highlight if $debug
90
+
91
+ klass.new h
92
+
93
+ end
94
+
95
+ end
96
+
@@ -0,0 +1,1582 @@
1
+ # file: ruby-macrodroid/triggers.rb
2
+
3
+ # This file contains the following classes:
4
+ #
5
+ #
6
+ # ## Trigger classes
7
+ #
8
+ # Trigger WebHookTrigger WifiConnectionTrigger
9
+ # ApplicationInstalledRemovedTrigger ApplicationLaunchedTrigger
10
+ # BatteryLevelTrigger BatteryTemperatureTrigger PowerButtonToggleTrigger
11
+ # ExternalPowerTrigger CallActiveTrigger IncomingCallTrigger
12
+ # OutgoingCallTrigger CallEndedTrigger CallMissedTrigger IncomingSMSTrigger
13
+ # WebHookTrigger WifiConnectionTrigger BluetoothTrigger HeadphonesTrigger
14
+ # SignalOnOffTrigger UsbDeviceConnectionTrigger WifiSSIDTrigger
15
+ # CalendarTrigger TimerTrigger StopwatchTrigger DayTrigger
16
+ # RegularIntervalTrigger DeviceEventsTrigger AirplaneModeTrigger
17
+ # AutoSyncChangeTrigger DayDreamTrigger DockTrigger FailedLoginTrigger
18
+ # GPSEnabledTrigger MusicPlayingTrigger DeviceUnlockedTrigger
19
+ # AutoRotateChangeTrigger ClipboardChangeTrigger BootTrigger
20
+ # IntentReceivedTrigger NotificationTrigger ScreenOnOffTrigger
21
+ # SilentModeTrigger WeatherTrigger GeofenceTrigger SunriseSunsetTrigger
22
+ # SensorsTrigger ActivityRecognitionTrigger ProximityTrigger
23
+ # ShakeDeviceTrigger FlipDeviceTrigger OrientationTrigger
24
+ # FloatingButtonTrigger ShortcutTrigger VolumeButtonTrigger
25
+ # MediaButtonPressedTrigger SwipeTrigger
26
+ #
27
+
28
+
29
+
30
+
31
+ class Trigger < MacroObject
32
+ using Params
33
+
34
+ attr_reader :constraints
35
+
36
+ def initialize(h={})
37
+ super({fakeIcon: 0}.merge(h))
38
+ @list << 'fakeIcon'
39
+
40
+ # fetch the constraints
41
+ @constraints = @h[:constraint_list].map do |constraint|
42
+ object(constraint.to_snake_case)
43
+ end
44
+
45
+ end
46
+
47
+ def match?(detail={}, model=nil)
48
+
49
+ # only match where the key exists in the trigger object
50
+ detail.select {|k,v| @h.include? k }.all? {|key,value| @h[key] == value}
51
+
52
+ end
53
+
54
+ end
55
+
56
+
57
+
58
+
59
+ # Category: Applications
60
+ #
61
+ # Also known as Wifi State Change
62
+ #
63
+ # wifi_state options:
64
+ # 0 - Wifi Enabled
65
+ # 1 - Wifi Disabled
66
+ # 2 - Connected to network
67
+ # ssid_list options:
68
+ # ["Any Network"]
69
+ # ["some Wifi SSID"] - 1 or more SSID can be supplied
70
+ # 3 - Disconnected from network
71
+ # ssid_list options:
72
+ # ["Any Network"]
73
+ # ["some Wifi SSID"] - 1 or more SSID can be supplied
74
+
75
+ class WifiConnectionTrigger < Trigger
76
+
77
+ def initialize(h={})
78
+
79
+ options = {
80
+ ssid_list: [""],
81
+ wifi_state: 2
82
+ }
83
+
84
+ super(options.merge h)
85
+
86
+ end
87
+
88
+ def to_s(colour: false)
89
+ 'WifiConnectionTrigger ' + @h.inspect
90
+ end
91
+
92
+ alias to_summary to_s
93
+ end
94
+
95
+ # Category: Applications
96
+ #
97
+ class ApplicationInstalledRemovedTrigger < Trigger
98
+
99
+ def initialize(h={})
100
+
101
+ options = {
102
+ application_name_list: [],
103
+ package_name_list: [],
104
+ installed: true,
105
+ application_option: 0,
106
+ updated: false
107
+ }
108
+
109
+ super(options.merge h)
110
+
111
+ end
112
+
113
+ def to_s(colour: false)
114
+ 'ApplicationInstalledRemovedTrigger ' + @h.inspect
115
+ end
116
+
117
+ alias to_summary to_s
118
+ end
119
+
120
+ # Category: Applications
121
+ #
122
+ class ApplicationLaunchedTrigger < Trigger
123
+
124
+ def initialize(h={})
125
+
126
+ options = {
127
+ application_name_list: ["Chrome"],
128
+ package_name_list: ["com.android.chrome"],
129
+ launched: true
130
+ }
131
+
132
+ super(options.merge h)
133
+
134
+ end
135
+
136
+ def to_s(colour: false)
137
+ 'ApplicationLaunchedTrigger ' + @h.inspect
138
+ end
139
+
140
+ alias to_summary to_s
141
+ end
142
+
143
+ # Category: Battery/Power
144
+ #
145
+ class BatteryLevelTrigger < Trigger
146
+
147
+ def initialize(h={})
148
+
149
+ options = {
150
+ battery_level: 50,
151
+ decreases_to: true,
152
+ option: 0
153
+ }
154
+
155
+ super(options.merge h)
156
+
157
+ end
158
+
159
+ def to_s(colour: false)
160
+ operator = @h[:decreases_to] ? '<=' : '>='
161
+ "Battery %s %s%%" % [operator, @h[:battery_level]]
162
+ end
163
+
164
+ end
165
+
166
+ # Category: Battery/Power
167
+ #
168
+ class BatteryTemperatureTrigger < Trigger
169
+
170
+ def initialize(h={})
171
+
172
+ options = {
173
+ decreases_to: true,
174
+ option: 0,
175
+ temperature: 30
176
+ }
177
+
178
+ super(options.merge h)
179
+
180
+ end
181
+
182
+ def to_s(colour: false)
183
+ 'BatteryTemperatureTrigger ' + @h.inspect
184
+ end
185
+
186
+ alias to_summary to_s
187
+ end
188
+
189
+ # Category: Battery/Power
190
+ #
191
+ class PowerButtonToggleTrigger < Trigger
192
+
193
+ def initialize(h={})
194
+
195
+ options = {
196
+ num_toggles: 3
197
+ }
198
+
199
+ super(options.merge h)
200
+
201
+ end
202
+
203
+ def to_s(colour: false)
204
+ 'PowerButtonToggleTrigger ' + @h.inspect
205
+ end
206
+
207
+ alias to_summary to_s
208
+ end
209
+
210
+
211
+ # Category: Battery/Power
212
+ #
213
+ class ExternalPowerTrigger < Trigger
214
+
215
+ def initialize(h={})
216
+
217
+ options = {
218
+ power_connected_options: [true, true, true],
219
+ has_set_usb_option: true,
220
+ power_connected: true,
221
+ has_set_new_power_connected_options: true
222
+ }
223
+
224
+ super(options.merge h)
225
+
226
+ end
227
+
228
+ def to_s(colour: false)
229
+
230
+ return 'Power Disconnected' unless @h[:power_connected]
231
+
232
+ status = 'Power Connectd'
233
+ options = if @h[:power_connected_options].all? then
234
+ 'Any'
235
+ else
236
+
237
+ a = ['Wired (Fast Charge)', 'Wireless', 'Wired (Slow Charge)']
238
+ @h[:power_connected_options].map.with_index {|x,i| x ? i : nil}\
239
+ .compact.map {|i| a[i] }.join(' + ')
240
+
241
+ end
242
+
243
+ "%s: %s" % [status, options]
244
+
245
+ end
246
+
247
+ alias to_summary to_s
248
+
249
+ end
250
+
251
+ # Category: Call/SMS
252
+ #
253
+ class CallActiveTrigger < Trigger
254
+
255
+ def initialize(h={})
256
+
257
+ options = {
258
+ contact_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}],
259
+ secondary_class_type: 'CallActiveTrigger',
260
+ signal_on: true
261
+ }
262
+
263
+ super(options.merge h)
264
+
265
+ end
266
+
267
+ def to_s(colour: false)
268
+ 'CallActiveTrigger ' + @h.inspect
269
+ end
270
+
271
+ alias to_summary to_s
272
+ end
273
+
274
+ # Category: Call/SMS
275
+ #
276
+ class IncomingCallTrigger < Trigger
277
+
278
+ def initialize(h={})
279
+
280
+ options = {
281
+ incoming_call_from_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}],
282
+ group_id_list: [],
283
+ group_name_list: [],
284
+ option: 0,
285
+ phone_number_exclude: false
286
+ }
287
+
288
+ super(options.merge h)
289
+
290
+ end
291
+
292
+ def to_s(colour: false)
293
+ caller = @h[:incoming_call_from_list].map {|x| "%s" % x[:name]}.join(', ')
294
+ "Call Incoming [%s]" % caller
295
+ end
296
+
297
+ alias to_summary to_s
298
+ end
299
+
300
+ # Category: Call/SMS
301
+ #
302
+ class OutgoingCallTrigger < Trigger
303
+
304
+ def initialize(h={})
305
+
306
+ options = {
307
+ outgoing_call_to_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}],
308
+ group_id_list: [],
309
+ group_name_list: [],
310
+ option: 0,
311
+ phone_number_exclude: false
312
+ }
313
+
314
+ super(options.merge h)
315
+
316
+ end
317
+
318
+ def to_s(colour: false)
319
+ 'OutgoingCallTrigger ' + @h.inspect
320
+ end
321
+
322
+ alias to_summary to_s
323
+ end
324
+
325
+ # Category: Call/SMS
326
+ #
327
+ class CallEndedTrigger < Trigger
328
+
329
+ def initialize(h={})
330
+
331
+ options = {
332
+ contact_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}],
333
+ group_id_list: [],
334
+ group_name_list: [],
335
+ option: 0,
336
+ phone_number_exclude: false
337
+ }
338
+
339
+ super(options.merge h)
340
+
341
+ end
342
+
343
+ def to_s(colour: false)
344
+ 'CallEndedTrigger ' + @h.inspect
345
+ end
346
+
347
+ alias to_summary to_s
348
+ end
349
+
350
+ # Category: Call/SMS
351
+ #
352
+ class CallMissedTrigger < Trigger
353
+
354
+ def initialize(h={})
355
+
356
+ options = {
357
+ contact_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}]
358
+ }
359
+
360
+ super(options.merge h)
361
+
362
+ end
363
+
364
+ def to_s(colour: false)
365
+ 'CallMissedTrigger ' + @h.inspect
366
+ end
367
+
368
+ alias to_summary to_s
369
+ end
370
+
371
+ # Category: Call/SMS
372
+ #
373
+ class IncomingSMSTrigger < Trigger
374
+
375
+ def initialize(h={})
376
+
377
+ options = {
378
+ sms_from_list: [{:m_id=>"-2", :m_lookupKey=>"-2", :m_name=>"[Any Number]"}],
379
+ group_id_list: [],
380
+ group_name_list: [],
381
+ sms_content: '',
382
+ option: 0,
383
+ excludes: false,
384
+ exact_match: false,
385
+ enable_regex: false,
386
+ sms_number_exclude: false
387
+ }
388
+
389
+ super(options.merge h)
390
+
391
+ end
392
+
393
+ def to_s(colour: false)
394
+ 'IncomingSMSTrigger ' + @h.inspect
395
+ end
396
+
397
+ alias to_summary to_s
398
+ end
399
+
400
+ # Category: Connectivity
401
+ #
402
+ class WebHookTrigger < Trigger
403
+
404
+ def initialize(h={})
405
+
406
+ options = {
407
+ identifier: ''
408
+ }
409
+
410
+ super(options.merge h)
411
+
412
+ end
413
+
414
+ def to_s(colour: false)
415
+
416
+ url = "https://trigger.macrodroid.com/%s/%s" % \
417
+ [@h[:macro].deviceid, @h[:identifier]]
418
+ @s = 'WebHook (Url)' + "\n " + url
419
+ super()
420
+
421
+ end
422
+
423
+ alias to_summary to_s
424
+ end
425
+
426
+ # Category: Connectivity
427
+ #
428
+ class WifiConnectionTrigger < Trigger
429
+
430
+ def initialize(h={})
431
+
432
+ options = {
433
+ ssid_list: [],
434
+ wifi_state: 0
435
+ }
436
+
437
+ super(options.merge h)
438
+
439
+ end
440
+
441
+ def to_s(colour: false)
442
+ access_point = @h[:ssid_list].first
443
+ 'Connected to network ' + access_point
444
+ end
445
+
446
+ alias to_summary to_s
447
+ end
448
+
449
+ # Category: Connectivity
450
+ #
451
+ class BluetoothTrigger < Trigger
452
+
453
+ def initialize(h={})
454
+
455
+ options = {
456
+ device_name: 'Any Device',
457
+ bt_state: 0,
458
+ any_device: false
459
+ }
460
+
461
+ super(options.merge h)
462
+
463
+ end
464
+
465
+ def to_s(colour: false)
466
+ 'BluetoothTrigger ' + @h.inspect
467
+ end
468
+
469
+ alias to_summary to_s
470
+ end
471
+
472
+ # Category: Connectivity
473
+ #
474
+ class HeadphonesTrigger < Trigger
475
+
476
+ def initialize(h={})
477
+
478
+ options = {
479
+ headphones_connected: true,
480
+ mic_option: 0
481
+ }
482
+
483
+ super(options.merge h)
484
+
485
+ end
486
+
487
+ def to_s(colour: false)
488
+ 'HeadphonesTrigger ' + @h.inspect
489
+ end
490
+
491
+ alias to_summary to_s
492
+ end
493
+
494
+ # Category: Connectivity
495
+ #
496
+ class SignalOnOffTrigger < Trigger
497
+
498
+ def initialize(h={})
499
+
500
+ options = {
501
+ signal_on: true
502
+ }
503
+
504
+ super(options.merge h)
505
+
506
+ end
507
+
508
+ def to_s(colour: false)
509
+ 'SignalOnOffTrigger ' + @h.inspect
510
+ end
511
+
512
+ alias to_summary to_s
513
+ end
514
+
515
+ # Category: Connectivity
516
+ #
517
+ class UsbDeviceConnectionTrigger < Trigger
518
+
519
+ def initialize(h={})
520
+
521
+ options = {
522
+ option: 0
523
+ }
524
+
525
+ super(options.merge h)
526
+
527
+ end
528
+
529
+ def to_s(colour: false)
530
+ 'UsbDeviceConnectionTrigger ' + @h.inspect
531
+ end
532
+
533
+ alias to_summary to_s
534
+ end
535
+
536
+ # Category: Connectivity
537
+ #
538
+ # Also known as Wifi SSID Transition
539
+ #
540
+ # options:
541
+ # in_range: true | false
542
+ # wifi_cell_info: {display_name: "some Wifi SSID",
543
+ # ssid: "some Wifi SSID"} - 1 or more allowed
544
+ #
545
+ class WifiSSIDTrigger < Trigger
546
+
547
+ def initialize(h={})
548
+
549
+ options = {
550
+ wifi_cell_info_list: [{:display_name=>"", :ssid=>""}],
551
+ ssid_list: [],
552
+ in_range: true
553
+ }
554
+
555
+ super(options.merge h)
556
+
557
+ end
558
+
559
+ def to_h()
560
+
561
+ h = super()
562
+ val = h[:m_inRange]
563
+
564
+ h[:m_InRange] = val
565
+ h.delete :m_inRange
566
+
567
+ return h
568
+
569
+ end
570
+
571
+ def to_s(colour: false)
572
+ 'WifiSSIDTrigger ' + @h.inspect
573
+ end
574
+
575
+ alias to_summary to_s
576
+ end
577
+
578
+ # Category: Date/Time
579
+ #
580
+ class CalendarTrigger < Trigger
581
+
582
+ def initialize(h={})
583
+
584
+ options = {
585
+ title_text: '',
586
+ detail_text: '',
587
+ calendar_name: 'Contacts',
588
+ calendar_id: '3',
589
+ availability: 0,
590
+ check_in_advance: false,
591
+ advance_time_seconds: 0,
592
+ event_start: true,
593
+ ignore_all_day: false,
594
+ negative_advance_check: false,
595
+ enable_regex: false
596
+ }
597
+
598
+ super(options.merge h)
599
+
600
+ end
601
+
602
+ def to_s(colour: false)
603
+ 'CalendarTrigger ' + @h.inspect
604
+ end
605
+
606
+ alias to_summary to_s
607
+ end
608
+
609
+ # Category: Date/Time
610
+ #
611
+ class TimerTrigger < Trigger
612
+ using ColouredText
613
+
614
+
615
+ def initialize(h={})
616
+
617
+ puts 'TimerTrigger h: ' + h.inspect if $debug
618
+
619
+ if h[:days] then
620
+
621
+ days = [false] * 7
622
+
623
+ h[:days].split(/, */).each do |x|
624
+
625
+ r = Date::DAYNAMES.grep /#{x}/i
626
+ i = Date::DAYNAMES.index(r.first)
627
+ days[i-1] = true
628
+
629
+ end
630
+
631
+ h[:days_of_week] = days
632
+
633
+ end
634
+
635
+ if h[:time] then
636
+
637
+ t = Time.parse(h[:time])
638
+ h[:hour], h[:minute] = t.hour, t.min
639
+
640
+ end
641
+
642
+ #puts ('h: ' + h.inspect).debug
643
+
644
+ @options = {
645
+ alarm_id: uuid(),
646
+ days_of_week: [false, false, false, false, false, false, false],
647
+ minute: 10,
648
+ hour: 7,
649
+ use_alarm: false
650
+ }
651
+
652
+ #super(options.merge filter(options, h))
653
+ super(@options.merge h)
654
+
655
+ end
656
+
657
+ def match?(detail={time: $env[:time]}, model=nil)
658
+
659
+ time() == detail[:time]
660
+
661
+ end
662
+
663
+ # sets the environmental conditions for this trigger to fire
664
+ #
665
+ def set_env()
666
+ $env[:time] = time()
667
+ end
668
+
669
+ def to_pc()
670
+ "time.is? '%s'" % self.to_s.gsub(',', ' or')
671
+ end
672
+
673
+ def to_s(colour: false)
674
+
675
+ dow = @h[:days_of_week]
676
+
677
+ wd = Date::ABBR_DAYNAMES
678
+ a = (wd[1..-1] << wd.first)
679
+
680
+ a2 = dow.map.with_index.to_a
681
+ start = a2.find {|x,i| x}.last
682
+ r = a2[start..-1].take_while {|x,i| x == true}
683
+ r2 = a2[start..-1].select {|x,i| x}
684
+
685
+ days = if r == r2 then
686
+
687
+ x1, x2 = a2[start].last, a2[r.length-1].last
688
+
689
+ if (x2 - x1) >= 2 then
690
+ "%s-%s" % [a[x1],a[x2]]
691
+ else
692
+ a.zip(dow).select {|_,b| b}.map(&:first).join(', ')
693
+ end
694
+ else
695
+ a.zip(dow).select {|_,b| b}.map(&:first).join(', ')
696
+ end
697
+
698
+ time = Time.parse("%s:%s" % [@h[:hour], @h[:minute]]).strftime("%H:%M")
699
+
700
+ "%s %s" % [time, days]
701
+ end
702
+
703
+ alias to_summary to_s
704
+
705
+ private
706
+
707
+ def time()
708
+
709
+ a = @h[:days_of_week].clone
710
+ a.unshift a.pop
711
+
712
+ dow = a.map.with_index {|x, i| x ? i : nil }.compact.join(',')
713
+ s = "%s %s * * %s" % [@h[:minute], @h[:hour], dow]
714
+ recent_time = ($env && $env[:time]) ? $env[:time] : Time.now
715
+ ChronicCron.new(s, recent_time).to_time
716
+
717
+ end
718
+
719
+ end
720
+
721
+ # Category: Date/Time
722
+ #
723
+ class StopwatchTrigger < Trigger
724
+
725
+ def initialize(h={})
726
+
727
+ options = {
728
+ stopwatch_name: 'timer1',
729
+ seconds: 240
730
+ }
731
+
732
+ super(options.merge h)
733
+
734
+ end
735
+
736
+ def to_s(colour: false)
737
+ 'StopwatchTrigger ' + @h.inspect
738
+ end
739
+
740
+ alias to_summary to_s
741
+ end
742
+
743
+ # Category: Date/Time
744
+ #
745
+ # Also known as Day of Week/Month
746
+ #
747
+ # month_of_year equal to 0 means it occurs every month
748
+ # day_of_week starts with a Monday (value is 0)
749
+ #
750
+ class DayTrigger < Trigger
751
+
752
+ def initialize(h={})
753
+
754
+ options = {
755
+ alarm_id: uuid(),
756
+ hour: 9,
757
+ minute: 0,
758
+ month_of_year: 0,
759
+ option: 0,
760
+ day_of_week: 2,
761
+ day_of_month: 0,
762
+ use_alarm: false
763
+ }
764
+
765
+ super(options.merge h)
766
+
767
+ end
768
+
769
+ def to_s(colour: false)
770
+ 'DayTrigger ' + @h.inspect
771
+ end
772
+
773
+ alias to_summary to_s
774
+ end
775
+
776
+ # Category: Date/Time
777
+ #
778
+ # Regular Interval
779
+ #
780
+ class RegularIntervalTrigger < Trigger
781
+
782
+ def initialize(h={})
783
+
784
+ options = {
785
+ ignore_reference_start_time: false,
786
+ minutes: 0,
787
+ seconds: 7200,
788
+ start_hour: 9,
789
+ start_minute: 10,
790
+ use_alarm: false
791
+ }
792
+
793
+ super(options.merge h)
794
+
795
+ end
796
+
797
+ def to_s(colour: false)
798
+
799
+ interval = Subunit.new(units={minutes:60, hours:60}, \
800
+ seconds: @h[:seconds]).strfunit("%c")
801
+ 'Regular Interval ' + "\n Interval: " + interval
802
+
803
+ end
804
+
805
+ alias to_summary to_s
806
+ end
807
+
808
+ class DeviceEventsTrigger < Trigger
809
+
810
+ def initialize(h={})
811
+ super(h)
812
+ @group = 'device_events'
813
+ end
814
+
815
+ end
816
+
817
+ # Category: Device Events
818
+ #
819
+ # Airplane Mode Changed
820
+ #
821
+ # options:
822
+ # Airplane Mode Enabled
823
+ # Airplane Mode Disabled
824
+ #
825
+ # shorthand example:
826
+ # airplanemode: enabled
827
+ #
828
+ class AirplaneModeTrigger < DeviceEventsTrigger
829
+
830
+ def initialize(h={})
831
+
832
+ options = {
833
+ airplane_mode_enabled: true
834
+ }
835
+
836
+ super(options.merge h)
837
+
838
+ end
839
+
840
+ def to_s(colour: false)
841
+ 'AirplaneModeTrigger ' + @h.inspect
842
+ end
843
+
844
+ alias to_summary to_s
845
+ end
846
+
847
+ # Category: Device Events
848
+ #
849
+ class AutoSyncChangeTrigger < DeviceEventsTrigger
850
+
851
+ def initialize(h={})
852
+
853
+ options = {
854
+ option: 0
855
+ }
856
+
857
+ super(options.merge h)
858
+
859
+ end
860
+
861
+ def to_s(colour: false)
862
+ 'AutoSyncChangeTrigger ' + @h.inspect
863
+ end
864
+
865
+ alias to_summary to_s
866
+ end
867
+
868
+ # Category: Device Events
869
+ #
870
+ class DayDreamTrigger < DeviceEventsTrigger
871
+
872
+ def initialize(h={})
873
+
874
+ options = {
875
+ day_dream_enabled: true
876
+ }
877
+
878
+ super(options.merge h)
879
+
880
+ end
881
+
882
+ def to_s(colour: false)
883
+ 'DayDreamTrigger ' + @h.inspect
884
+ end
885
+
886
+ alias to_summary to_s
887
+ end
888
+
889
+ # Category: Device Events
890
+ #
891
+ class DockTrigger < DeviceEventsTrigger
892
+
893
+ def initialize(h={})
894
+
895
+ options = {
896
+ dock_type: 0
897
+ }
898
+
899
+ super(options.merge h)
900
+
901
+ end
902
+
903
+ def to_s(colour: false)
904
+ 'DockTrigger ' + @h.inspect
905
+ end
906
+
907
+ alias to_summary to_s
908
+ end
909
+
910
+ # Category: Device Events
911
+ #
912
+ class FailedLoginTrigger < DeviceEventsTrigger
913
+
914
+ def initialize(h={})
915
+
916
+ options = {
917
+ num_failures: 1
918
+ }
919
+
920
+ super(options.merge h)
921
+
922
+ end
923
+
924
+ def to_pc()
925
+ 'failed_login?'
926
+ end
927
+
928
+ def to_s(colour: false)
929
+ 'Failed Login Attempt'
930
+ end
931
+ end
932
+
933
+ # Category: Device Events
934
+ #
935
+ class GPSEnabledTrigger < DeviceEventsTrigger
936
+
937
+ def initialize(h={})
938
+
939
+ options = {
940
+ gps_mode_enabled: true
941
+ }
942
+
943
+ super(options.merge h)
944
+
945
+ end
946
+
947
+ def to_s(colour: false)
948
+ 'GPSEnabledTrigger ' + @h.inspect
949
+ end
950
+
951
+ alias to_summary to_s
952
+ end
953
+
954
+ # Category: Device Events
955
+ #
956
+ class MusicPlayingTrigger < DeviceEventsTrigger
957
+
958
+ def initialize(h={})
959
+
960
+ options = {
961
+ option: 0
962
+ }
963
+
964
+ super(options.merge h)
965
+
966
+ end
967
+
968
+ def to_s(colour: false)
969
+ 'MusicPlayingTrigger ' + @h.inspect
970
+ end
971
+
972
+ alias to_summary to_s
973
+ end
974
+
975
+
976
+ # Category: Device Events
977
+ #
978
+ class NFCTrigger < DeviceEventsTrigger
979
+
980
+ def initialize(h={})
981
+
982
+ options = {
983
+ }
984
+
985
+ super(options.merge h)
986
+
987
+ end
988
+
989
+ def to_s(colour: false)
990
+ 'NFC Tag' + "\n " + @h[:tag_name]
991
+ end
992
+
993
+ alias to_summary to_s
994
+
995
+ end
996
+
997
+
998
+ # Category: Device Events
999
+ #
1000
+ class DeviceUnlockedTrigger < DeviceEventsTrigger
1001
+
1002
+ def initialize(h={})
1003
+
1004
+ options = {
1005
+ }
1006
+
1007
+ super(options.merge h)
1008
+
1009
+ end
1010
+
1011
+ def to_s(colour: false)
1012
+ 'Screen Unlocked'
1013
+ end
1014
+
1015
+ alias to_summary to_s
1016
+
1017
+ end
1018
+
1019
+ # Category: Device Events
1020
+ #
1021
+ class AutoRotateChangeTrigger < DeviceEventsTrigger
1022
+
1023
+ def initialize(h={})
1024
+
1025
+ options = {
1026
+ option: 0
1027
+ }
1028
+
1029
+ super(options.merge h)
1030
+
1031
+ end
1032
+
1033
+ def to_s(colour: false)
1034
+ 'AutoRotateChangeTrigger ' + @h.inspect
1035
+ end
1036
+
1037
+ alias to_summary to_s
1038
+ end
1039
+
1040
+ # Category: Device Events
1041
+ #
1042
+ class ClipboardChangeTrigger < DeviceEventsTrigger
1043
+
1044
+ def initialize(h={})
1045
+
1046
+ options = {
1047
+ text: '',
1048
+ enable_regex: false
1049
+ }
1050
+
1051
+ super(options.merge h)
1052
+
1053
+ end
1054
+
1055
+ def to_s(colour: false)
1056
+ 'ClipboardChangeTrigger ' + @h.inspect
1057
+ end
1058
+
1059
+ alias to_summary to_s
1060
+ end
1061
+
1062
+ # Category: Device Events
1063
+ #
1064
+ class BootTrigger < DeviceEventsTrigger
1065
+
1066
+ def initialize(h={})
1067
+
1068
+ options = {
1069
+ }
1070
+
1071
+ super(options.merge h)
1072
+
1073
+ end
1074
+
1075
+ def to_s(colour: false)
1076
+ 'BootTrigger ' + @h.inspect
1077
+ end
1078
+
1079
+ alias to_summary to_s
1080
+ end
1081
+
1082
+ # Category: Device Events
1083
+ #
1084
+ class IntentReceivedTrigger < DeviceEventsTrigger
1085
+
1086
+ def initialize(h={})
1087
+
1088
+ options = {
1089
+ action: '',
1090
+ extra_params: [],
1091
+ extra_value_patterns: [],
1092
+ extra_variables: [],
1093
+ enable_regex: false
1094
+ }
1095
+
1096
+ super(options.merge h)
1097
+
1098
+ end
1099
+
1100
+ def to_s(colour: false)
1101
+ 'IntentReceivedTrigger ' + @h.inspect
1102
+ end
1103
+
1104
+ alias to_summary to_s
1105
+ end
1106
+
1107
+ # Category: Device Events
1108
+ #
1109
+ class NotificationTrigger < DeviceEventsTrigger
1110
+
1111
+ def initialize(h={})
1112
+
1113
+ options = {
1114
+ text_content: '',
1115
+ package_name_list: ["Any Application"],
1116
+ application_name_list: ["Any Application"],
1117
+ exclude_apps: false,
1118
+ ignore_ongoing: true,
1119
+ option: 0,
1120
+ exact_match: false,
1121
+ excludes: false,
1122
+ sound_option: 0,
1123
+ supress_multiples: true,
1124
+ enable_regex: false
1125
+ }
1126
+
1127
+ super(options.merge h)
1128
+
1129
+ end
1130
+
1131
+ def to_s(colour: false)
1132
+ s = (@h[:package_name_list] + @h[:application_name_list]).uniq.join(', ')
1133
+ 'Notification Received ' + "\n Any Content (%s)" % s
1134
+ end
1135
+
1136
+ alias to_summary to_s
1137
+ end
1138
+
1139
+ # Category: Device Events
1140
+ #
1141
+ class ScreenOnOffTrigger < DeviceEventsTrigger
1142
+
1143
+ def initialize(h={})
1144
+
1145
+ options = {
1146
+ screen_on: true
1147
+ }
1148
+
1149
+ super(options.merge h)
1150
+
1151
+ end
1152
+
1153
+ def to_s(colour: false)
1154
+ 'Screen ' + (@h[:screen_on] ? 'On' : 'Off')
1155
+ end
1156
+
1157
+ alias to_summary to_s
1158
+
1159
+ end
1160
+
1161
+ # Category: Device Events
1162
+ #
1163
+ class SilentModeTrigger < DeviceEventsTrigger
1164
+
1165
+ def initialize(h={})
1166
+
1167
+ options = {
1168
+ silent_enabled: true
1169
+ }
1170
+
1171
+ super(options.merge h)
1172
+
1173
+ end
1174
+
1175
+ def to_s(colour: false)
1176
+ 'SilentModeTrigger ' + @h.inspect
1177
+ end
1178
+
1179
+ alias to_summary to_s
1180
+ end
1181
+
1182
+ # Category: Location
1183
+ #
1184
+ class WeatherTrigger < Trigger
1185
+
1186
+ def initialize(h={})
1187
+
1188
+ options = {
1189
+ humidity_above: true,
1190
+ humidity_value: 50,
1191
+ option: 4,
1192
+ temp_below: true,
1193
+ temp_celcius: true,
1194
+ temperature: 0,
1195
+ weather_condition: 0,
1196
+ wind_speed_above: true,
1197
+ wind_speed_value: 0,
1198
+ wind_speed_value_mph: 0
1199
+ }
1200
+
1201
+ super(options.merge h)
1202
+
1203
+ end
1204
+
1205
+ def to_s(colour: false)
1206
+ 'WeatherTrigger ' + @h.inspect
1207
+ end
1208
+
1209
+ alias to_summary to_s
1210
+ end
1211
+
1212
+ # Category: Location
1213
+ #
1214
+ class GeofenceTrigger < Trigger
1215
+
1216
+ def initialize( h={}, geofences: {})
1217
+
1218
+ if h[:name] then
1219
+ puts ('geofences2: ' + geofences.inspect) if $debug
1220
+ found = geofences.find {|x| x.name.downcase == h[:name].downcase}
1221
+ h[:geofence_id] = found.id if found
1222
+
1223
+ end
1224
+
1225
+ options = {
1226
+ update_rate_text: '5 Minutes',
1227
+ geofence_id: '',
1228
+ geofence_update_rate_minutes: 5,
1229
+ trigger_from_unknown: false,
1230
+ enter_area: true
1231
+ }
1232
+
1233
+ super(options.merge filter(options, h))
1234
+ @geofences = geofences
1235
+
1236
+ end
1237
+
1238
+ def to_s(colour: false)
1239
+
1240
+ if $debug then
1241
+ puts ' @geofences: ' + @geofences.inspect
1242
+ puts '@h: ' + @h.inspect
1243
+ puts '@h[:geofence_id]: ' + @h[:geofence_id].inspect
1244
+ end
1245
+
1246
+ direction = @h[:enter_area] ? 'Entry' : 'Exit'
1247
+
1248
+ found = @geofences.find {|x| x.id == @h[:geofence_id]}
1249
+ puts 'found: ' + found.inspect if @debug
1250
+ label = found ? found.name : 'error: name not found'
1251
+
1252
+ "Geofence %s (%s)" % [direction, label]
1253
+
1254
+ end
1255
+
1256
+ end
1257
+
1258
+ # Category: Location
1259
+ #
1260
+ class SunriseSunsetTrigger < Trigger
1261
+
1262
+ def initialize(h={})
1263
+
1264
+ options = {
1265
+ option: 0,
1266
+ time_adjust_seconds: 0
1267
+ }
1268
+
1269
+ super(options.merge h)
1270
+
1271
+ end
1272
+
1273
+ def to_s(colour: false)
1274
+ 'SunriseSunsetTrigger ' + @h.inspect
1275
+ end
1276
+
1277
+ alias to_summary to_s
1278
+ end
1279
+
1280
+
1281
+ class SensorsTrigger < Trigger
1282
+
1283
+ def initialize(h={})
1284
+ super(h)
1285
+ @group = 'sensors'
1286
+ end
1287
+
1288
+ end
1289
+
1290
+ # Category: Sensors
1291
+ #
1292
+ class ActivityRecognitionTrigger < SensorsTrigger
1293
+
1294
+ def initialize(h={})
1295
+
1296
+ options = {
1297
+ confidence_level: 50,
1298
+ selected_index: 1
1299
+ }
1300
+
1301
+ super(options.merge h)
1302
+
1303
+ @activity = ['In Vehicle', 'On Bicycle', 'Running', 'Walking', 'Still']
1304
+
1305
+ end
1306
+
1307
+ def to_s(colour: false)
1308
+ activity = @activity[@h[:selected_index]]
1309
+ 'Activity - ' + activity
1310
+ end
1311
+
1312
+ def to_summary(colour: false)
1313
+
1314
+ activity = @activity[@h[:selected_index]]
1315
+ s = if activity.length > 10 then
1316
+ activity[0..7] + '..'
1317
+ else
1318
+ activity
1319
+ end
1320
+
1321
+ 'Activity - ' + s
1322
+
1323
+ end
1324
+
1325
+ end
1326
+
1327
+ # Category: Sensors
1328
+ #
1329
+ class ProximityTrigger < SensorsTrigger
1330
+
1331
+ def initialize(h={})
1332
+
1333
+ if h[:distance] then
1334
+
1335
+ case h[:distance].to_sym
1336
+ when :near
1337
+ options[:near] = true
1338
+ end
1339
+ end
1340
+
1341
+ options = {
1342
+ near: true,
1343
+ selected_option: 0
1344
+ }
1345
+
1346
+ super(options.merge h)
1347
+
1348
+ end
1349
+
1350
+ def to_s(colour: false)
1351
+
1352
+ distance = if @h[:near] then
1353
+ 'Near'
1354
+ else
1355
+ 'Far'
1356
+ end
1357
+
1358
+ "Proximity Sensor (%s)" % distance
1359
+ end
1360
+
1361
+ alias to_summary to_s
1362
+
1363
+ end
1364
+
1365
+ # Category: Sensors
1366
+ #
1367
+ class ShakeDeviceTrigger < SensorsTrigger
1368
+
1369
+ def initialize(h={})
1370
+
1371
+ options = {
1372
+ }
1373
+
1374
+ super(options.merge h)
1375
+
1376
+ end
1377
+
1378
+ def to_pc()
1379
+ 'shake_device?'
1380
+ end
1381
+
1382
+ def to_s(colour: false)
1383
+ 'Shake Device'
1384
+ end
1385
+
1386
+ end
1387
+
1388
+ # Category: Sensors
1389
+ #
1390
+ # options:
1391
+ # Face Up -> Face Down
1392
+ # Face Down -> Face Up
1393
+ # Any -> Face Down
1394
+ #
1395
+ class FlipDeviceTrigger < SensorsTrigger
1396
+
1397
+ def initialize(h={})
1398
+
1399
+ options = {
1400
+ any_start: false,
1401
+ face_down: true,
1402
+ work_with_screen_off: false
1403
+ }
1404
+
1405
+ super(options.merge h)
1406
+
1407
+ end
1408
+
1409
+ def to_pc()
1410
+ @h[:face_down] ? 'flip_device_down?' : 'flip_device_up?'
1411
+ end
1412
+
1413
+ def to_s(colour: false)
1414
+
1415
+ action = @h[:face_down] ? 'Face Up -> Face Down' : 'Face Down -> Face Up'
1416
+ 'Flip Device ' + action
1417
+ end
1418
+
1419
+ end
1420
+
1421
+ # Category: Sensors
1422
+ #
1423
+ class OrientationTrigger < SensorsTrigger
1424
+
1425
+ def initialize(h={})
1426
+
1427
+ options = {
1428
+ check_orientation_alive: true,
1429
+ option: 0
1430
+ }
1431
+
1432
+ super(options.merge h)
1433
+
1434
+ end
1435
+
1436
+ def to_s(colour: false)
1437
+ 'OrientationTrigger ' + @h.inspect
1438
+ end
1439
+
1440
+ alias to_summary to_s
1441
+ end
1442
+
1443
+ # Category: User Input
1444
+ #
1445
+ class FloatingButtonTrigger < Trigger
1446
+
1447
+ def initialize(h={})
1448
+
1449
+ options = {
1450
+ image_resource_id: 0,
1451
+ icon_bg_color: -9079435,
1452
+ alpha: 100,
1453
+ padding: 20,
1454
+ force_location: false,
1455
+ show_on_lock_screen: false,
1456
+ size: 0,
1457
+ transparent_background: false,
1458
+ x_location: 0,
1459
+ y_location: 0
1460
+ }
1461
+
1462
+ super(options.merge h)
1463
+
1464
+ end
1465
+
1466
+ def to_s(colour: false)
1467
+ 'Floating Button'
1468
+ end
1469
+
1470
+ alias to_summary to_s
1471
+ end
1472
+
1473
+ # Category: User Input
1474
+ #
1475
+ class ShortcutTrigger < Trigger
1476
+
1477
+ def initialize(h={})
1478
+
1479
+ options = {
1480
+ }
1481
+
1482
+ super(options.merge h)
1483
+
1484
+ end
1485
+
1486
+ def to_s(colour: false)
1487
+ 'ShortcutTrigger ' + @h.inspect
1488
+ end
1489
+
1490
+ alias to_summary to_s
1491
+ end
1492
+
1493
+ # Category: User Input
1494
+ #
1495
+ class VolumeButtonTrigger < Trigger
1496
+
1497
+ def initialize(h={})
1498
+
1499
+ options = {
1500
+ dont_change_volume: true,
1501
+ monitor_option: 1,
1502
+ not_configured: false,
1503
+ option: 0
1504
+ }
1505
+
1506
+ super(options.merge h)
1507
+
1508
+ end
1509
+
1510
+ def to_s(colour: false)
1511
+
1512
+ a = [
1513
+ 'Volume Up',
1514
+ 'Volume Down',
1515
+ 'Volume Up - Long Press',
1516
+ 'Volume Down - Long Press'
1517
+ ]
1518
+
1519
+ lines = [a[@h[:option]]]
1520
+ lines << ' ' + (@h[:dont_change_volume] ? 'Retain Previous Volume' : 'Update Volume')
1521
+ @s = lines.join("\n")
1522
+ end
1523
+
1524
+ def to_summary(colour: false)
1525
+ a = [
1526
+ 'Volume Up',
1527
+ 'Volume Down',
1528
+ 'Volume Up - Long Press',
1529
+ 'Volume Down - Long Press'
1530
+ ]
1531
+
1532
+ lines = [a[@h[:option]]]
1533
+ lines << (@h[:dont_change_volume] ? 'Retain Previous Volume' : 'Update Volume')
1534
+ @s = lines.join(": ")
1535
+ end
1536
+
1537
+ end
1538
+
1539
+ # Category: User Input
1540
+ #
1541
+ class MediaButtonPressedTrigger < Trigger
1542
+
1543
+ def initialize(h={})
1544
+
1545
+ options = {
1546
+ option: 'Single Press',
1547
+ cancel_press: false
1548
+ }
1549
+
1550
+ super(options.merge h)
1551
+
1552
+ end
1553
+
1554
+ def to_s(colour: false)
1555
+ 'MediaButtonPressedTrigger ' + @h.inspect
1556
+ end
1557
+
1558
+ alias to_summary to_s
1559
+ end
1560
+
1561
+ # Category: User Input
1562
+ #
1563
+ class SwipeTrigger < Trigger
1564
+
1565
+ def initialize(h={})
1566
+
1567
+ options = {
1568
+ swipe_start_area: 0,
1569
+ swipe_motion: 0,
1570
+ cleared: true
1571
+ }
1572
+
1573
+ super(options.merge h)
1574
+
1575
+ end
1576
+
1577
+ def to_s(colour: false)
1578
+ 'SwipeTrigger ' + @h.inspect
1579
+ end
1580
+
1581
+ alias to_summary to_s
1582
+ end