-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsms_service.rb
78 lines (64 loc) · 1.59 KB
/
sms_service.rb
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
require_relative 'lib/sms_service'
# ******* Constants *******
LED1 = 23
LED2 = 24
LED3 = 25
SERIAL_PORT = '/dev/ttyAMA0'.freeze
SERIAL_BAUD_RATE = 115_200
REDIS_JOB_KEY = 'queue:sms'.freeze
REDIS_POLLING_FREQUENCY = 5
# ******* End Constants *******
interrupted = false
# Trap ^C
Signal.trap('INT') do
puts "\n"
puts 'Shutting down...'
interrupted = true
end
# Trap `Kill`
Signal.trap('TERM') do
puts "\n"
puts 'Shutting down...'
interrupted = true
end
# Setup LEDs
pins = SMSService::LEDHandler.new([LED1, LED2, LED3])
# End Setup LEDs
# Start setup: Flash indicator
pins.flash
# Setup serialport
sim = SMSService::SIMHandler.new(Serial.new(SERIAL_PORT, SERIAL_BAUD_RATE))
# End Setup serialport
# Set to text mode
raise 'SIM: Text mode could not be activated' unless sim.set_text_mode
puts 'Text mode activated...'
# End Set to text mode
# Setup complete: Stop flash indicator
pins.stop_animations
puts 'Setup complete...'
# Setup Redis
host = ENV['REDIS_HOST']
port = ENV['REDIS_PORT']
password = ENV['REDIS_PASSWORD']
redis = SMSService::RedisHandler.new(Redis.new(host: host, port: port,
password: password),
REDIS_JOB_KEY)
# Actual worker
loop do
exit if interrupted
j = redis.dequeue
if !j.nil?
# Start led game
pins.ascending
puts 'Sending message...'
if sim.send_sms(j['number'], j['text'])
puts 'Message sent...'
else
puts 'Something went wrong sending the message...'
end
# Stop led game
pins.stop_animations
else
sleep REDIS_POLLING_FREQUENCY
end
end