From 3c677b7d29cbcfbecf0fb527f0307a2da1ad8264 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Fri, 6 Sep 2013 17:32:09 +0200 Subject: [PATCH] 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, ...). --- lib/biju/hayes.rb | 21 +++++++++++++-------- lib/biju/sms.rb | 5 +++-- spec/biju/sms_spec.rb | 12 +++++++++++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index 3643359..9eac3f8 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -2,6 +2,14 @@ module Biju class Hayes attr_reader :modem + MESSAGE_STATUS = { + unread: [0, 'REC UNREAD'], + read: [1, 'REC UNREAD'], + unsent: [2, ''], + sent: [3, ''], + all: [4, 'ALL'], + } + def initialize(port, options = {}) pin = options.delete(:pin) || '0000' @modem = Modem.new(port, options) @@ -10,7 +18,7 @@ module Biju init_modem unlock_pin pin - text_mode + text_mode(false) extended_error end @@ -59,17 +67,14 @@ module Biju at_command('+CPIN', pin)[:status] end - def messages(which = "ALL") - prefered_storage 'MT' + def messages(which = :all) + which = MESSAGE_STATUS[which][text_mode? ? 1 : 0] if which.is_a?(Symbol) + sms = at_command('+CMGL', which) return sms[:status] if !sms.has_key?(:sms) || sms[:sms].empty? sms[:sms].map do |msg| - Biju::Sms.new( - id: msg[:infos][0], - phone_number: msg[:infos][2], - datetime: msg[:infos][4], - message: msg[:message].chomp) + Biju::Sms.from_pdu(msg[:message].chomp, msg[:infos][0]) end end diff --git a/lib/biju/sms.rb b/lib/biju/sms.rb index 7130b30..e46887e 100644 --- a/lib/biju/sms.rb +++ b/lib/biju/sms.rb @@ -5,9 +5,10 @@ module Biju attr_accessor :id, :phone_number, :message attr_reader :datetime - def self.from_pdu(string) + def self.from_pdu(string, id = nil) 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], message: sms_infos[:user_data]) end diff --git a/spec/biju/sms_spec.rb b/spec/biju/sms_spec.rb index 3478b19..f3ade4a 100644 --- a/spec/biju/sms_spec.rb +++ b/spec/biju/sms_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -require 'biju/sms' +require 'biju' describe Biju::Sms do subject do @@ -14,4 +14,14 @@ describe Biju::Sms do its(:phone_number) { should eq("144") } its(:datetime) { should eq(DateTime.new(2011, 7, 28, 15, 34, 8, '-12')) } 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