Disable text mode and generate Sms objects in PDU mode

Text mode is only useful for debug purposes.
To parse answers, PDU mode is required (multiline messages, ...).
develop
Guillaume DOTT 2013-09-06 17:32:09 +02:00
parent b4f49569bb
commit 3c677b7d29
3 changed files with 27 additions and 11 deletions

View File

@ -2,6 +2,14 @@ module Biju
class Hayes class Hayes
attr_reader :modem attr_reader :modem
MESSAGE_STATUS = {
unread: [0, 'REC UNREAD'],
read: [1, 'REC UNREAD'],
unsent: [2, ''],
sent: [3, ''],
all: [4, 'ALL'],
}
def initialize(port, options = {}) def initialize(port, options = {})
pin = options.delete(:pin) || '0000' pin = options.delete(:pin) || '0000'
@modem = Modem.new(port, options) @modem = Modem.new(port, options)
@ -10,7 +18,7 @@ module Biju
init_modem init_modem
unlock_pin pin unlock_pin pin
text_mode text_mode(false)
extended_error extended_error
end end
@ -59,17 +67,14 @@ module Biju
at_command('+CPIN', pin)[:status] at_command('+CPIN', pin)[:status]
end end
def messages(which = "ALL") def messages(which = :all)
prefered_storage 'MT' which = MESSAGE_STATUS[which][text_mode? ? 1 : 0] if which.is_a?(Symbol)
sms = at_command('+CMGL', which) sms = at_command('+CMGL', which)
return sms[:status] if !sms.has_key?(:sms) || sms[:sms].empty? return sms[:status] if !sms.has_key?(:sms) || sms[:sms].empty?
sms[:sms].map do |msg| sms[:sms].map do |msg|
Biju::Sms.new( Biju::Sms.from_pdu(msg[:message].chomp, msg[:infos][0])
id: msg[:infos][0],
phone_number: msg[:infos][2],
datetime: msg[:infos][4],
message: msg[:message].chomp)
end end
end end

View File

@ -5,9 +5,10 @@ module Biju
attr_accessor :id, :phone_number, :message attr_accessor :id, :phone_number, :message
attr_reader :datetime attr_reader :datetime
def self.from_pdu(string) def self.from_pdu(string, id = nil)
sms_infos = PDU.decode(string) sms_infos = PDU.decode(string)
new(phone_number: sms_infos[:sender_number], new(id: id,
phone_number: sms_infos[:sender_number],
datetime: sms_infos[:timestamp], datetime: sms_infos[:timestamp],
message: sms_infos[:user_data]) message: sms_infos[:user_data])
end end

View File

@ -1,5 +1,5 @@
require 'spec_helper' require 'spec_helper'
require 'biju/sms' require 'biju'
describe Biju::Sms do describe Biju::Sms do
subject do subject do
@ -14,4 +14,14 @@ describe Biju::Sms do
its(:phone_number) { should eq("144") } its(:phone_number) { should eq("144") }
its(:datetime) { should eq(DateTime.new(2011, 7, 28, 15, 34, 8, '-12')) } its(:datetime) { should eq(DateTime.new(2011, 7, 28, 15, 34, 8, '-12')) }
its(:message) { should eq("Some text here") } its(:message) { should eq("Some text here") }
describe '::from_pdu' do
subject do
Biju::Sms.from_pdu(
'07913396050066F3040B913366666666F600003190509095928004D4F29C0E')
end
its(:datetime) { should eq(DateTime.new(2013, 9, 5, 9, 59, 29, '+08')) }
its(:message) { should eq('Test') }
end
end end