Class: ComunikaGsm::GSM
- Inherits:
-
Object
- Object
- ComunikaGsm::GSM
- Defined in:
- lib/comunika_gsm/gsm.rb
Instance Attribute Summary collapse
-
#status ⇒ Object
, :imei, :iccid, :provider, :signal_level.
Instance Method Summary collapse
- #close ⇒ Object
- #cmd(c) ⇒ Object
- #iccid ⇒ Object
- #imei ⇒ Object
-
#initialize(params) ⇒ GSM
constructor
A new instance of GSM.
- #messages ⇒ Object
- #normalize(cmd, res) ⇒ Object
- #provider ⇒ Object
- #send_sms(msg) ⇒ Object
- #signal_level ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(params) ⇒ GSM
Returns a new instance of GSM.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/comunika_gsm/gsm.rb', line 5 def initialize(params) return nil if params.empty? begin @@port = SerialPort.new("/dev/#{params[:port]}",19200,8,1, SerialPort::NONE) @@port.read_timeout = 250 ## Set timeout of command @@debug = params[:debug] res = cmd("ATE0\r\n") ## Set echo off if res.length > 0 cmd("AT+CMGF=0\r\n") ## Set PDU mode # @imei = imei # @iccid = iccid # @provider = provider # @signal_level = signal_level @status = true else return @status = false end rescue => ex return @status = false end end |
Instance Attribute Details
#status ⇒ Object
, :imei, :iccid, :provider, :signal_level
3 4 5 |
# File 'lib/comunika_gsm/gsm.rb', line 3 def status @status end |
Instance Method Details
#close ⇒ Object
47 48 49 50 51 |
# File 'lib/comunika_gsm/gsm.rb', line 47 def close puts "Fechando conexão" @@port.close @status = false end |
#cmd(c) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/comunika_gsm/gsm.rb', line 29 def cmd(c) begin @@port.write(c) normalize(c,wait) rescue => ex puts ex @status = false #close end end |
#iccid ⇒ Object
96 97 98 |
# File 'lib/comunika_gsm/gsm.rb', line 96 def iccid cmd("AT+CRSM=176,12258,0,0,10\r\n") end |
#imei ⇒ Object
92 93 94 |
# File 'lib/comunika_gsm/gsm.rb', line 92 def imei cmd("AT+CGSN\r\n") end |
#messages ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/comunika_gsm/gsm.rb', line 84 def sms = 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: self, id: m[0], size: m[2], pdu: m[3].chomp).decode }# rescue nil end |
#normalize(cmd, res) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/comunika_gsm/gsm.rb', line 109 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 |
#provider ⇒ Object
100 101 102 |
# File 'lib/comunika_gsm/gsm.rb', line 100 def provider cmd("AT+COPS?\r\n") end |
#send_sms(msg) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/comunika_gsm/gsm.rb', line 53 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=#{pdu[:size]}\r") res = cmd("#{pdu[:pdu]}#{26.chr}") sleep 3 while res.length == 0 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 {:id => id, :code => code, :status => status} end |
#signal_level ⇒ Object
104 105 106 |
# File 'lib/comunika_gsm/gsm.rb', line 104 def signal_level cmd("AT+CSQ\r\n") end |
#wait ⇒ Object
40 41 42 43 44 45 |
# File 'lib/comunika_gsm/gsm.rb', line 40 def wait buffer = @@port.read puts buffer if @@debug @@port.flush() buffer end |