forked from JFF-Bohdan/sim-module
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_ussd.py
87 lines (65 loc) · 2.31 KB
/
test_ussd.py
1
2
3
4
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
83
84
85
86
87
#!/usr/bin/python3
from test_shared import *
from lib.sim900.ussdhandler import SimUssdHandler
import re
from lib.sim900.simshared import *
COMPORT_NAME = "com22"
#logging levels
CONSOLE_LOGGER_LEVEL = logging.INFO
LOGGER_LEVEL = logging.INFO
def parseBalanceResult(value, prefix, suffix, mustBeFloat = True):
"""
Parses string which contains information about current account balance.
Information about current balance value must be stored between prefix and suffix.
:param value: text for scanning
:param prefix: text prefix for searching
:param suffix: suffix for searching
:param mustBeFloat: does balance must be float
:return: current balance value or None on error
"""
m = re.search("{0}(.+?){1}".format(prefix, suffix), value)
if not m:
return None
found = m.group(1)
if found is None:
found = ''
else:
found = str(found).strip()
if mustBeFloat:
return strToFloat(found)
if not str(found).isnumeric():
return None
return int(found)
def main():
"""
Tests USSD commands execution and results retrieving.
:return: true if everything was OK, otherwise returns false
"""
#adding & initializing port object
port = initializeUartPort(portName=COMPORT_NAME)
#initializing logger
(formatter, logger, consoleLogger,) = initializeLogs(LOGGER_LEVEL, CONSOLE_LOGGER_LEVEL)
#making base operations
d = baseOperations(port, logger)
if d is None:
return False
(gsm, imei) = d
ussd = SimUssdHandler(port, logger)
logger.info("running USSD code")
#calling USSD command for balance information retrieving ( 'life :)' cell operator from Ukraine )
if not ussd.runUssdCode("*111#"):
logger.error("error running USSD code")
return False
logger.info("USSD result = {0}".format(ussd.lastUssdResult))
logger.info("Reading current balance value...")
#Parsing balance for 'life :)' cell operator from Ukraine
balance = parseBalanceResult(ussd.lastUssdResult, "Balans ", "grn,")
if balance is not None:
logger.info("Current balance value: {0}".format(balance))
else:
logger.warn("balance retrieving error")
gsm.closePort()
return True
if __name__ == "__main__":
main()
print("DONE")