Class: SmsSerial
- Inherits:
-
Object
- Object
- SmsSerial
- Defined in:
- lib/sms_serial.rb
Instance Attribute Summary collapse
-
#t ⇒ Object
readonly
Returns the value of attribute t.
Instance Method Summary collapse
-
#count ⇒ Object
count the number of SMS messages.
- #delete(idx) ⇒ Object (also: #del)
- #delete_all ⇒ Object
-
#initialize(dev = '/dev/ttyUSB0', callback: nil, debug: false) ⇒ SmsSerial
constructor
A new instance of SmsSerial.
- #list_all ⇒ Object
-
#read(idx = 1) ⇒ Object
read an SMS message.
Constructor Details
#initialize(dev = '/dev/ttyUSB0', callback: nil, debug: false) ⇒ SmsSerial
Returns a new instance of SmsSerial.
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 |
# File 'lib/sms_serial.rb', line 19 def initialize(dev='/dev/ttyUSB0', callback: nil, debug: false) @debug = debug @sp = SerialPort.new dev, 115200 puts 'running ' + "SmsSerial".highlight @t = Thread.new { Thread.current[:v] = [] loop do = @sp.read if .length > 0 callback.call() if callback and @debug if =~ /^\+CMTI:/ then callback.call(.lines[2]) if callback else Thread.current[:v] ||= [] Thread.current[:v] << end end sleep 0.3 end } end |
Instance Attribute Details
#t ⇒ Object (readonly)
Returns the value of attribute t.
17 18 19 |
# File 'lib/sms_serial.rb', line 17 def t @t end |
Instance Method Details
#count ⇒ Object
count the number of SMS messages
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sms_serial.rb', line 55 def count() puts 'inside count'.info if @debug cmd 'CPMS?' do |r| puts 'r: ' + r.inspect if @debug total = r.lines[2].split(',')[1].to_i puts ('message count: ' + total.to_s).debug if @debug total end end |
#delete(idx) ⇒ Object Also known as: del
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sms_serial.rb', line 69 def delete(idx) return delete_all() if idx.to_sym == :all cmd 'CMD' do |r| total = r.lines[2].split(',')[1].to_i puts ('message count: ' + total.to_s).debug if @debug total end end |
#delete_all ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/sms_serial.rb', line 84 def delete_all() # format: [index, delflag] # delflag 1 means delete all read messages cmd 'CMGD=1,1' end |
#list_all ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sms_serial.rb', line 92 def list_all() n = count() sleep 0.3 @sp.write %Q(AT+CMGL="ALL"\r\n) n.times.map {r = read_buffer; parse_msg(r)} flush_output() if n < 1 end |
#read(idx = 1) ⇒ Object
read an SMS message
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sms_serial.rb', line 106 def read(idx=1) cmd('CMGR=' + idx.to_s) do |r| begin parse_msg r rescue SmsSerialError => e puts 'Invalid message no.'.warning nil end end end |