From 276f0b0093b4e0392db525e7a28e8fb50cd04a0a Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Tue, 10 Sep 2013 11:20:59 +0200 Subject: [PATCH] Improve Hayes#send to check for prompt and wait for answer --- lib/biju/hayes.rb | 8 +++++--- lib/biju/parser.rb | 15 +++++++++++---- spec/biju/parser_spec.rb | 16 ++++++++++++++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index 07cbbda..a99e125 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -85,10 +85,12 @@ module Biju end def send(sms, options = {}) - at_command('+CMGS', sms.phone_number) + result = at_command('+CMGS', (sms.to_pdu.length - 2) / 2) - write("#{sms.message}#{26.chr}") - hayes_to_obj(modem.wait) + if result[:prompt] + modem.write("#{sms.to_pdu}#{26.chr}") + hayes_to_obj(modem.wait(length: 8).lstrip) + end end private diff --git a/lib/biju/parser.rb b/lib/biju/parser.rb index 885ffdf..8ec83b1 100644 --- a/lib/biju/parser.rb +++ b/lib/biju/parser.rb @@ -8,13 +8,16 @@ module Biju rule(:at_string) { request | response } # REQUEST - rule(:request) { str('+++') | str('A/') | (prefix >> (cr.absent? >> lf.absent? >> any).repeat(0)) >> response.maybe } + rule(:request) do + str('+++') | str('A/') | (prefix >> (cr.absent? >> lf.absent? >> any).repeat(0)) >> + (cr >> crlf >> response).maybe + end rule(:prefix) { str('AT') | str('at') } # RESPONSE - rule(:response) { cr >> crlf >> (((status | command) >> crlf) | prompt) } - rule(:prompt) { str('> ') } - rule(:command) { mgl | pms | mgf | mserror } + rule(:response) { ((status | command) >> crlf) | prompt } + rule(:prompt) { str('> ').as(:prompt) } + rule(:command) { mgl | pms | mgf | mgs | mserror } rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message } rule(:mgl) do @@ -28,6 +31,9 @@ module Biju rule(:mgf) do str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf >> status end + rule(:mgs) do + str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf >> status + end rule(:array) do (data >> (comma >> data).repeat).as(:array) @@ -64,6 +70,7 @@ module Biju end class ATTransform < Parslet::Transform + rule(prompt: simple(:prompt)) { { prompt: true } } rule(cmd: simple(:cmd), infos: subtree(:infos), message: simple(:message)) do {cmd: cmd.to_s, infos: infos, message: message.to_s} end diff --git a/spec/biju/parser_spec.rb b/spec/biju/parser_spec.rb index d71edc7..2223f7b 100644 --- a/spec/biju/parser_spec.rb +++ b/spec/biju/parser_spec.rb @@ -18,8 +18,10 @@ describe Biju::ATParser do it "parses cmgs prompt" do mgs = "AT+CMGS=18\r\r\n> " - expect { Biju::ATTransform.new.apply( - Biju::ATParser.new.parse(mgs)) }.not_to raise_error + result = Biju::ATTransform.new.apply( + Biju::ATParser.new.parse(mgs)) + + expect(result).to include(prompt: true) end it "parses messages list" do @@ -71,6 +73,16 @@ describe Biju::ATParser do expect(result).to include(status: true) expect(result[:result]).to be_false end + + it "parses message sent response" do + mgs = "+CMGS: 163\r\n\r\nOK\r\n" + + result = Biju::ATTransform.new.apply( + Biju::ATParser.new.parse(mgs)) + + expect(result).to include(status: true) + expect(result[:result]).to eq(163) + end end it "raises ParseFailed exception" do