Class: ComunikaGsm::GSM2
- Inherits:
-
Object
- Object
- ComunikaGsm::GSM2
- Defined in:
- lib/comunika_gsm/gsm2.rb
Instance Attribute Summary collapse
-
#busy ⇒ Object
Returns the value of attribute busy.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#iccid ⇒ Object
Returns the value of attribute iccid.
-
#imei ⇒ Object
Returns the value of attribute imei.
-
#last_send_at ⇒ Object
Returns the value of attribute last_send_at.
-
#last_status_checked_at ⇒ Object
Returns the value of attribute last_status_checked_at.
-
#port ⇒ Object
Returns the value of attribute port.
-
#port_name ⇒ Object
Returns the value of attribute port_name.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#signal_level ⇒ Object
Returns the value of attribute signal_level.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #close ⇒ Object
- #cmd(c) ⇒ Object
- #info(type) ⇒ Object
-
#initialize(port, params = {}) ⇒ GSM2
constructor
A new instance of GSM2.
- #messages ⇒ Object
- #send_sms(num, msg, params = {}) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(port, params = {}) ⇒ GSM2
Returns a new instance of GSM2.
5 6 7 8 9 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 |
# 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. if @debug return @status = false end end |
Instance Attribute Details
#busy ⇒ Object
Returns the value of attribute busy.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def busy @busy end |
#debug ⇒ Object
Returns the value of attribute debug.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def debug @debug end |
#iccid ⇒ Object
Returns the value of attribute iccid.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def iccid @iccid end |
#imei ⇒ Object
Returns the value of attribute imei.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def imei @imei end |
#last_send_at ⇒ Object
Returns the value of attribute last_send_at.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def last_send_at @last_send_at end |
#last_status_checked_at ⇒ Object
Returns the value of attribute last_status_checked_at.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def last_status_checked_at @last_status_checked_at end |
#port ⇒ Object
Returns the value of attribute port.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def port @port end |
#port_name ⇒ Object
Returns the value of attribute port_name.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def port_name @port_name end |
#provider ⇒ Object
Returns the value of attribute provider.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def provider @provider end |
#signal_level ⇒ Object
Returns the value of attribute signal_level.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def signal_level @signal_level end |
#status ⇒ Object
Returns the value of attribute status.
3 4 5 |
# File 'lib/comunika_gsm/gsm2.rb', line 3 def status @status end |
Instance Method Details
#close ⇒ Object
54 55 56 57 58 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# 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 ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/comunika_gsm/gsm2.rb', line 98 def 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 = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 ⇒ Object
47 48 49 50 51 52 |
# File 'lib/comunika_gsm/gsm2.rb', line 47 def wait buffer = self.port.read puts buffer if self.debug self.port.flush() buffer end |