Class: ComunikaGsm::SMS::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/comunika_gsm/sms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, debug = false) ⇒ Provider

Returns a new instance of Provider.



5
6
7
8
# File 'lib/comunika_gsm/sms.rb', line 5

def initialize(port,debug=false)
  @gsm = GSM.new(:port => port, :debug => debug)
  @total = 0
end

Instance Attribute Details

#gsmObject (readonly)

Returns the value of attribute gsm.



4
5
6
# File 'lib/comunika_gsm/sms.rb', line 4

def gsm
  @gsm
end

#totalObject (readonly)

Returns the value of attribute total.



4
5
6
# File 'lib/comunika_gsm/sms.rb', line 4

def total
  @total
end

Instance Method Details

#messagesObject



42
43
44
45
46
47
48
# File 'lib/comunika_gsm/sms.rb', line 42

def messages
  sms = @gsm.cmd("AT+CMGL=4\r\n")
  msgs = sms.scan(/\+CMGL\:\s*?(\d+),\s*?(\d+),.*?\,s*?(\d+)\r\n(.*)/)

  ## IDS: 0 - ID, 1 -- ,2 - size, 3 - PDU
  msgs.collect!{ |m| PDU::PDUDecode.new(connection: @gsm, id: m[0], size: m[2], pdu: m[3].chomp).decode }# rescue nil
end

#send_sms(msg) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/comunika_gsm/sms.rb', line 10

def send_sms(msg)
  return {id: nil, status: "ERROR", code: "304"} if msg[:number].length == 0
  return {id: nil, status: "ERROR", code: "304"} if msg[:number].length < 11
  return {id: nil, status: "ERROR", code: "503"} if msg[:message].length == 0

  ## GENERATE PDU TO MESSAGE ##
  pdu = PDU.encode(:number => msg[:number].prepend("+55"), :smsc => msg[:smsc], :message => msg[:message])

    # cmd("AT+CMGS=\"#{options[:number]}\"\r")
    # res = cmd("#{options[:message][0..160]}#{26.chr}")
    @gsm.cmd("AT+CMGS=#{pdu.size}\r")
    res = @gsm.cmd("#{pdu.body}#{26.chr}")

    sleep 3
    while res.empty?
      res = @gsm.wait
    end

    if res.include?('+CMGS')
      res = res.scan(/\+(\S+)\: (\d+)\r\n/)
      status = 'OK'
      code = "-1"
      id = res.first[1]
    elsif res.include?('+CMS')
      res = res.scan(/\+CMS (\S+)\: (\d+)/).first
      status = 'ERROR'
      code = res[1]
      id = nil
    end
    {:id => id, :code => code, :status => status}
end