smartware 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,10 +7,17 @@ module Smartware
7
7
 
8
8
  class Standard
9
9
 
10
+ ERRORS = {
11
+ "-1" => -1, # invalid device answer
12
+ "10" => 10, # invalid ussd answer
13
+ "11" => 11, # invalid ussd answer size
14
+ "12" => 12, # invalid ussd body answer size
15
+ "20" => 20, # invalid modem model
16
+ "21" => 21, # invalid modem signal level
17
+ }
18
+
10
19
  def initialize(port)
11
- # @port Class variable needs for ussd request
12
- @port = port
13
- @sp = SerialPort.new(@port, 115200, 8, 1, SerialPort::NONE)
20
+ @sp = SerialPort.new(port, 115200, 8, 1, SerialPort::NONE)
14
21
  end
15
22
 
16
23
  def error
@@ -18,14 +25,12 @@ module Smartware
18
25
  end
19
26
 
20
27
  def model
21
- res = send 'ATI' rescue -1
22
- return -1 unless res.last == "OK"
23
-
28
+ res = send 'ATI'
24
29
  res.shift
25
30
  res.pop
26
31
  res.join(' ')
27
32
  rescue
28
- -1
33
+ ERRORS["20"]
29
34
  end
30
35
 
31
36
  #
@@ -33,30 +38,30 @@ module Smartware
33
38
  #
34
39
  def signal_level
35
40
  res = send 'AT+CSQ'
36
- return -1 if res.last != "OK"
37
41
  value = res[1].gsub("+CSQ: ",'').split(',')[0].to_i
38
42
  "#{(-113 + value * 2)} dbm"
39
43
  rescue
40
- -1
44
+ ERRORS["21"]
41
45
  end
42
46
 
43
47
  #
44
48
  # Method send ussd to operator and return only valid answer body
45
- # Don`t call with method synchronously from app, method waits USSD answer 3 sec,
49
+ # Returns modem balance by default, it works for MTS and Megafon, use *102# for Beeline
50
+ # Do not call with method synchronously from app, method waits USSD answer some time,
46
51
  # Use some scheduler and buffer for balance value
47
52
  #
48
- def ussd(code="*100#", use_gets = false)
49
- port = SerialPort.new(@port, 115200, 8, 1, SerialPort::NONE)
50
- port.read_timeout = 3000
51
- port.write "AT+CUSD=1,\"#{code}\",15\r\n"
52
- result = use_gets ? port.gets : port.read
53
- # Parse USSD message body
54
- ussd_body = result.split(/[\r\n]+/).last.split(",")[1].gsub('"','')
53
+ # Valid ussd answer sample: ["", "+CUSD: 2,\"003100310035002C003000300440002E00320031002E00330031002004310430043B002E0020\",72", "OK"]
54
+ #
55
+ def ussd(code="*100#")
56
+ res = self.send "AT+CUSD=1,\"#{code}\",15\r\n"
57
+ return ERRORS["11"] unless res.size >= 3
58
+ return ERRORS["12"] if res[1].size < 10 # Check valid ussd answer by length
59
+
60
+ ussd_body = res[1].split(",")[1].gsub('"','') # Parse USSD message body
55
61
  port.close
56
- # Encode USSD message from broken ucs2 to utf-8
57
- ussd_body.scan(/\w{4}/).map{|i| [i.hex].pack("U") }.join
62
+ ussd_body.scan(/\w{4}/).map{|i| [i.hex].pack("U") }.join # Encode USSD message from broken ucs2 to utf-8
58
63
  rescue
59
- -1
64
+ ERRORS["10"]
60
65
  end
61
66
 
62
67
  def send(cmd, read_timeout = 0.25)
@@ -66,7 +71,9 @@ module Smartware
66
71
  chr = @sp.getc.chr
67
72
  answer << chr
68
73
  end
69
- answer.split(/[\r\n]+/)
74
+ res = answer.split(/[\r\n]+/)
75
+ return ERRORS["-1"] unless res.last == "OK"
76
+ res
70
77
  end
71
78
  end
72
79
 
@@ -1,3 +1,3 @@
1
1
  module Smartware
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: