diff --git a/lib/biju/hayes.rb b/lib/biju/hayes.rb index 3eacc0c..2a4444a 100644 --- a/lib/biju/hayes.rb +++ b/lib/biju/hayes.rb @@ -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 diff --git a/lib/biju/modem.rb b/lib/biju/modem.rb index a46e432..afb78cd 100644 --- a/lib/biju/modem.rb +++ b/lib/biju/modem.rb @@ -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