Use Forwardable in Biju::Modem
parent
a001604ee7
commit
548e5cd4cd
|
@ -21,7 +21,8 @@ module Biju
|
|||
full_command = [command, (command_args.empty? ? nil : command_args)]
|
||||
.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?
|
||||
answer
|
||||
|
@ -75,16 +76,13 @@ module Biju
|
|||
|
||||
def send(sms, options = {})
|
||||
at_command('+CMGS', sms.phone_number)
|
||||
|
||||
write("#{sms.message}#{26.chr}")
|
||||
hayes_to_obj(modem.wait)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def write(text)
|
||||
modem.write(text)
|
||||
hayes_to_obj(modem.wait_answer)
|
||||
end
|
||||
|
||||
def hayes_to_obj(str)
|
||||
ATTransform.new.apply(ATParser.new.parse(str))
|
||||
end
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
require 'serialport'
|
||||
require 'forwardable'
|
||||
|
||||
module Biju
|
||||
class Modem
|
||||
extend Forwardable
|
||||
|
||||
DEFAULT_OPTIONS = { baud: 9600, data_bits: 8,
|
||||
stop_bits: 1, parity: SerialPort::NONE }
|
||||
|
||||
|
@ -16,16 +19,9 @@ module Biju
|
|||
@connection = SerialPort.new(port, DEFAULT_OPTIONS.merge!(options))
|
||||
end
|
||||
|
||||
# Close the serial connection.
|
||||
def close
|
||||
connection.close
|
||||
end
|
||||
def_delegators :connection, :close, :write
|
||||
|
||||
def write(text)
|
||||
connection.write(text + "\r")
|
||||
end
|
||||
|
||||
def wait_answer
|
||||
def wait
|
||||
buffer = ''
|
||||
while IO.select([connection], [], [], 0.25)
|
||||
buffer << connection.getc.chr
|
||||
|
|
Loading…
Reference in New Issue