-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_deploy_button.rb
More file actions
91 lines (71 loc) · 2.1 KB
/
big_deploy_button.rb
File metadata and controls
91 lines (71 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# coding: utf-8
class BigDeployButton
require 'pi_piper'
include PiPiper
DEPLOY_COMMAND_FILE = File.join(File.dirname(__FILE__), './deploy_command')
DEPLOY_COMMAND = "`cat #{DEPLOY_COMMAND_FILE}`"
LOG_FILE = File.join(File.dirname(__FILE__), './log')
def initialize
log_this "Initializing..."
log_this "Deploy command will be #{ `cat deploy_command` }"
@button = Pin.new(pin: 24, direction: :in, trigger: :rising)
@white_led = Pin.new(pin: 22, direction: :out)
@blue_led = Pin.new(pin: 21, direction: :out)
@blink_threads = {}
@deploy_in_progress = false
@blue_led.on
listen_to_button
end
def listen_to_button
Thread.new do
loop do
@button.wait_for_change
deploy if @button.on?
end
end.abort_on_exception = true
p "Waiting..."
PiPiper.wait
p "Finished waiting"
end
def deploy
return if @deploy_in_progress # Prevent multiple deploy requests
@deploy_in_progress = true
notify_deploy_in_progress
system(DEPLOY_COMMAND)
successful_deploy = $?.success?
successful_deploy ? notify_deploy_finished_ok : notify_deploy_finished_with_errors
@deploy_in_progress = false
end
def notify_deploy_in_progress
log_this "Deploying..."
@white_led_blink.exit if @white_led_blink
blink_white_led(on_time: 1, off_time: 0.7)
end
def notify_deploy_finished_ok
log_this "Deploy finished OK"
@white_led_blink.exit
@white_led.off
end
def notify_deploy_finished_with_errors
log_this "Deploy finished with errors!"
blink_white_led
log_this "Blinking white led"
end
def blink_white_led(options = { on_time: 0.2, off_time: 0.2 })
@white_led_blink = blink_led(@white_led, options)
end
def log_this(message)
p message
system("echo \"#{Time.now} #{message}\" >> #{LOG_FILE}")
end
def blink_led(led, options = { on_time: 0.2, off_time: 0.2 })
@blink_threads[led.pin] = Thread.new do
loop do
led.off
sleep options[:off_time]
led.on
sleep options[:on_time]
end.abort_on_exception = true
end
end
end