Use Forwardable in Biju::Modem

develop
Guillaume DOTT 2013-09-06 10:18:49 +02:00
parent a001604ee7
commit 548e5cd4cd
2 changed files with 9 additions and 15 deletions

View File

@ -21,7 +21,8 @@ module Biju
full_command = [command, (command_args.empty? ? nil : command_args)] full_command = [command, (command_args.empty? ? nil : command_args)]
.compact.join('=') .compact.join('=')
answer = write(full_command) modem.write(full_command + "\r\n")
answer = hayes_to_obj(modem.wait)
return block.call(answer) if block_given? return block.call(answer) if block_given?
answer answer
@ -75,16 +76,13 @@ module Biju
def send(sms, options = {}) def send(sms, options = {})
at_command('+CMGS', sms.phone_number) at_command('+CMGS', sms.phone_number)
write("#{sms.message}#{26.chr}") write("#{sms.message}#{26.chr}")
hayes_to_obj(modem.wait)
end end
private private
def write(text)
modem.write(text)
hayes_to_obj(modem.wait_answer)
end
def hayes_to_obj(str) def hayes_to_obj(str)
ATTransform.new.apply(ATParser.new.parse(str)) ATTransform.new.apply(ATParser.new.parse(str))
end end

View File

@ -1,7 +1,10 @@
require 'serialport' require 'serialport'
require 'forwardable'
module Biju module Biju
class Modem class Modem
extend Forwardable
DEFAULT_OPTIONS = { baud: 9600, data_bits: 8, DEFAULT_OPTIONS = { baud: 9600, data_bits: 8,
stop_bits: 1, parity: SerialPort::NONE } stop_bits: 1, parity: SerialPort::NONE }
@ -16,16 +19,9 @@ module Biju
@connection = SerialPort.new(port, DEFAULT_OPTIONS.merge!(options)) @connection = SerialPort.new(port, DEFAULT_OPTIONS.merge!(options))
end end
# Close the serial connection. def_delegators :connection, :close, :write
def close
connection.close
end
def write(text) def wait
connection.write(text + "\r")
end
def wait_answer
buffer = '' buffer = ''
while IO.select([connection], [], [], 0.25) while IO.select([connection], [], [], 0.25)
buffer << connection.getc.chr buffer << connection.getc.chr