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
- #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 |
# 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 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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sms_serial.rb', line 52 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
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sms_serial.rb', line 66 def delete(idx) 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
77 78 79 80 81 82 83 |
# File 'lib/sms_serial.rb', line 77 def delete_all() # format: [index, delflag] # delflag 1 means delete all read messages cmd 'CMGD=1,1' end |
#list_all ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sms_serial.rb', line 85 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
99 100 101 102 103 |
# File 'lib/sms_serial.rb', line 99 def read(idx=1) cmd('CMGR=' + idx.to_s) {|r| parse_msg r } end |