Module: ComunikaGsm::Device

Defined in:
lib/comunika_gsm/modem.rb

Defined Under Namespace

Classes: Device

Constant Summary collapse

@@devices =
[]

Class Method Summary collapse

Class Method Details

.add(device) ⇒ Object



42
43
44
# File 'lib/comunika_gsm/modem.rb', line 42

def self.add(device)
  @@devices << device
end

.allObject

Get all devices intances



10
11
12
# File 'lib/comunika_gsm/modem.rb', line 10

def self.all
  @@devices
end

.clearObject



14
15
16
# File 'lib/comunika_gsm/modem.rb', line 14

def self.clear
  @devices = []
end

.load(params = {}) ⇒ Object

Initialize devices



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/modem.rb', line 19

def self.load(params={})
  self.clear
  threads = []
  self.ports.each do |port|
    threads << Thread.new {
      ## Check port and add in to devices ##
      modem = self.load_modem(port, params[:debug])
      if modem
        self.add(modem)
      end

      Thread.current.kill
    }
  end

  while threads.count > 0
    threads.delete_if {|t| !t.alive? }
  end

  @@devices
  #@@devices.collect! {|d| Device.new(d) }#.uniq {|d| d[:imei] }#.find_all{|dev| dev.status == true }.uniq!{|d| d.imei }
end

.load_modem(port, debug = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/comunika_gsm/modem.rb', line 46

def self.load_modem(port, debug=false)
  gsm = GSM2.new(:port => port, :debug => debug)
  if gsm.status
    result = gsm.cmd("AT\r\n")
    if result.to_s.length > 0
      imei = gsm.cmd("AT+CGSN\r\n")
      iccid = gsm.cmd("AT+CRSM=176,12258,0,0,10\r\n")
      provider = gsm.cmd("AT+COPS?\r\n")
      signal = gsm.cmd("AT+CSQ\r\n")
      unless @@devices.find {|m| m[:imei] == imei}
        return Device.new(:port => port,:gsm => gsm, :imei => imei, :iccid => iccid, :provider => provider, :signal => signal)
      end
    else
      gsm.close
      return nil
    end
  end
end

.portsObject



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

def self.ports
  ports = %x{ls /dev | grep ttyUSB*}.split(/\n/)
end