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
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

View File

@ -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

View File

@ -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