Improve Hayes#send to check for prompt and wait for answer

develop
Guillaume DOTT 2013-09-10 11:20:59 +02:00
parent ea3106cfc2
commit 276f0b0093
3 changed files with 30 additions and 9 deletions

View File

@ -85,10 +85,12 @@ module Biju
end end
def send(sms, options = {}) 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}") if result[:prompt]
hayes_to_obj(modem.wait) modem.write("#{sms.to_pdu}#{26.chr}")
hayes_to_obj(modem.wait(length: 8).lstrip)
end
end end
private private

View File

@ -8,13 +8,16 @@ module Biju
rule(:at_string) { request | response } rule(:at_string) { request | response }
# REQUEST # 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') } rule(:prefix) { str('AT') | str('at') }
# RESPONSE # RESPONSE
rule(:response) { cr >> crlf >> (((status | command) >> crlf) | prompt) } rule(:response) { ((status | command) >> crlf) | prompt }
rule(:prompt) { str('> ') } rule(:prompt) { str('> ').as(:prompt) }
rule(:command) { mgl | pms | mgf | mserror } rule(:command) { mgl | pms | mgf | mgs | mserror }
rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message } rule(:mserror) { str('+CMS ERROR').as(:cmd) >> str(': ') >> message }
rule(:mgl) do rule(:mgl) do
@ -28,6 +31,9 @@ module Biju
rule(:mgf) do rule(:mgf) do
str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf >> status str('+CMGF').as(:cmd) >> str(': ') >> boolean.as(:result) >> crlf >> crlf >> status
end end
rule(:mgs) do
str('+CMGS').as(:cmd) >> str(': ') >> int.as(:result) >> crlf >> crlf >> status
end
rule(:array) do rule(:array) do
(data >> (comma >> data).repeat).as(:array) (data >> (comma >> data).repeat).as(:array)
@ -64,6 +70,7 @@ module Biju
end end
class ATTransform < Parslet::Transform class ATTransform < Parslet::Transform
rule(prompt: simple(:prompt)) { { prompt: true } }
rule(cmd: simple(:cmd), infos: subtree(:infos), message: simple(:message)) do rule(cmd: simple(:cmd), infos: subtree(:infos), message: simple(:message)) do
{cmd: cmd.to_s, infos: infos, message: message.to_s} {cmd: cmd.to_s, infos: infos, message: message.to_s}
end end

View File

@ -18,8 +18,10 @@ describe Biju::ATParser do
it "parses cmgs prompt" do it "parses cmgs prompt" do
mgs = "AT+CMGS=18\r\r\n> " mgs = "AT+CMGS=18\r\r\n> "
expect { Biju::ATTransform.new.apply( result = Biju::ATTransform.new.apply(
Biju::ATParser.new.parse(mgs)) }.not_to raise_error Biju::ATParser.new.parse(mgs))
expect(result).to include(prompt: true)
end end
it "parses messages list" do it "parses messages list" do
@ -71,6 +73,16 @@ describe Biju::ATParser do
expect(result).to include(status: true) expect(result).to include(status: true)
expect(result[:result]).to be_false expect(result[:result]).to be_false
end 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 end
it "raises ParseFailed exception" do it "raises ParseFailed exception" do