Class: Biju::Modem

Inherits:
Object
  • Object
show all
Defined in:
lib/biju/modem.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Modem

Returns a new instance of Modem.

Parameters:

  • Options (Hash)

    to serial connection.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :port (String)

    The modem port to connect

    Biju::Modem.new(:port => ‘/dev/ttyUSB0’)

Raises:

  • (Exception)


12
13
14
15
16
17
# File 'lib/biju/modem.rb', line 12

def initialize(options={})
  raise Exception.new("Port is required") unless options[:port]
  @connection = connection(options)
  cmd("AT")
  cmd("AT+CMGF=1")
end

Instance Method Details

#closeObject

Close the serial connection.



20
21
22
# File 'lib/biju/modem.rb', line 20

def close
  @connection.close
end

#delete(id) ⇒ Object

Delete a sms message by id.

Parameters:

  • Id (Fixnum)

    of sms message on modem.



34
35
36
# File 'lib/biju/modem.rb', line 34

def delete(id)
  cmd("AT+CMGD=#{id}")
end

#messagesObject

Return an Array of Sms if there is messages nad return nil if not.



25
26
27
28
29
30
# File 'lib/biju/modem.rb', line 25

def messages
  sms = cmd("AT+CMGL=\"ALL\"")
  msgs = sms.scan(/\+CMGL\:\s*?(\d+)\,.*?\,\"(.+?)\"\,.*?\,\"(.+?)\".*?\n(.*)/)
  return nil unless msgs
  msgs.collect!{ |msg| Biju::Sms.new(:id => msg[0], :phone_number => msg[1], :datetime => msg[2], :message => msg[3].chomp) }
end