smartware 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/smartware/drivers/modem/standard.rb +28 -21
- data/lib/smartware/version.rb +1 -1
- metadata +1 -1
@@ -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
|
-
|
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'
|
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
|
-
|
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
|
-
|
44
|
+
ERRORS["21"]
|
41
45
|
end
|
42
46
|
|
43
47
|
#
|
44
48
|
# Method send ussd to operator and return only valid answer body
|
45
|
-
#
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
#
|
54
|
-
|
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
|
-
|
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
|
|
data/lib/smartware/version.rb
CHANGED