class ComunikaGsm::GSM2

Attributes

busy[RW]
debug[RW]
iccid[RW]
imei[RW]
last_send_at[RW]
last_status_checked_at[RW]
port[RW]
port_name[RW]
provider[RW]
signal_level[RW]
status[RW]

Public Class Methods

new(port,params = {}) click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 5
def initialize(port,params = {})
  raise "Invalid params" if params.empty?
  self.busy = false
  
  begin
    self.port_name = port
    self.port = SerialPort.new("/dev/#{port}",19200,8,1, SerialPort::NONE)
    self.port.read_timeout = params[:read_timeout] || 150 ## Set timeout of command

    self.debug = params[:debug] || false

    res = cmd("ATE0\r\n") ## Set echo off
    if res.length > 0
      cmd("AT+CMGF=0\r\n") ## Set PDU mode
      self.status = true

      if params[:load_infos]
        info("imei")
        info("iccid")
        info("provider")
        info("signal_level")
      end
    else
      return @status = false
    end
  rescue => ex
    puts ex.message if @debug
    return @status = false
  end
end

Public Instance Methods

close() click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 54
def close
  puts "Fechando conexão" if self.debug
  self.port.close if self.port
  self.status = false
end
cmd(c) click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 36
def cmd(c)
  begin
    self.port.write(c)
    normalize(c,wait)
  rescue => ex
    puts ex
    self.status = false
    close if self.port
  end
end
info(type) click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 109
def info(type)
  case type
  when "imei"
    self.imei = cmd("AT+CGSN\r\n")
  when "iccid"
    self.iccid = cmd("AT+CRSM=176,12258,0,0,10\r\n")
  when "provider"
    self.provider = cmd("AT+COPS?\r\n")
  when "signal_level"
    self.signal_level = cmd("AT+CSQ\r\n")
  when "port"
    self.port
  else
    cmd("AT\r\n")
  end
end
messages() click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 98
def messages
  self.busy = true
  sms = cmd("AT+CMGL=4\r\n")
  sleep 3
  msgs = sms.scan(/\+CMGL\:\s*?(\d+),\s*?(\d+),.*?\,s*?(\d+)\r\n(.*)/) if sms

  ## IDS: 0 - ID, 1 -- ,2 - size, 3 - PDU
  self.busy = false
  msgs.collect!{ |m| PDU::PDUDecode.new(connection: self, id: m[0], size: m[2], pdu: m[3].chomp).decode } rescue nil
end
send_sms(num,msg,params = {}) click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 60
def send_sms(num,msg,params = {})
  self.busy = true
  puts "-- Send message to #{num} -- msg #{msg}" if self.debug
  if num.length == 0 || num.length < 11 || num.length > 11 || msg.length == 0
    {id: nil, status: "ERROR", code: "600"}
  else
    ## GENERATE PDU TO MESSAGE ##
    pdu = PDU.encode("+55" + num, msg, :smsc => params[:smsc])

    cmd("AT+CMGS=#{pdu[:size]}\r")
    res = cmd("#{pdu[:pdu]}#{26.chr}")

    puts "--- Resultado do envio: #{res}" if self.debug
 
    sleep 3
    while res.length == 0
      puts "--- Esperando resultado envio: #{res}" if self.debug
      res = 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

    self.busy = false
    self.last_send_at = Time.now
    {:id => id, :code => code, :status => status, pdu_size: pdu[:size], pdu: pdu[:pdu]}
  end
end
wait() click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 47
def wait
  buffer = self.port.read
  puts buffer if self.debug
  self.port.flush()
  buffer
end

Private Instance Methods

normalize(cmd,res) click to toggle source
# File lib/comunika_gsm/gsm2.rb, line 127
def normalize(cmd,res)
  case cmd.gsub(/\r\n/,'')
  when 'AT' || 'ATE0'
    res.scan(/\r\n(\S+)\r\n/)[0].first unless res.empty?
  when 'AT+COPS?'
    res.scan(/\"(.*?)\"/)[0].first
  when 'AT+CNUM'
    res.scan(/\"(.*?)\"/)
  when 'AT+CGSN'
    res.scan(/\r\n(\d+)\r\n/)[0].first
  when 'AT+CGMI'
    res.scan(/\r\n(\S+)\r\n/)[0].first
  when 'AT+CRSM=176,12258,0,0,10'
    res.scan(/\"(.*?)\"/)[0].first
  when 'AT+CSQ'
    res.scan(/\d+/)[0]
  else
    res
  end
end