Commit e18bd31fe435bf818e1ede9f1a1140a0d77f0352
1 parent
c8ebb34a2b
Exists in
master
advice
Showing 2 changed files with 63 additions and 6 deletions Side-by-side Diff
main.py
... | ... | @@ -42,6 +42,7 @@ DISABLE_SEM=0 |
42 | 42 | DISABLE_SEM_ON_TRX=60 |
43 | 43 | |
44 | 44 | LAST_REQUEST_ID = None |
45 | +LAST_SN = None | |
45 | 46 | |
46 | 47 | PULSA = 0 |
47 | 48 | MASA_AKTIF = "" |
... | ... | @@ -136,7 +137,7 @@ def getRequestIdByNominalDestination(nominal, destination): |
136 | 137 | #return redis_client.spop(redis_key) |
137 | 138 | return redis_client.rpop(redis_key) |
138 | 139 | |
139 | -def pushTopupStatus(request_id, response_code, _message): | |
140 | +def pushTopupStatus(request_id, response_code, _message, dontparsesn = False): | |
140 | 141 | global BALANCE |
141 | 142 | global CHIPINFO |
142 | 143 | |
... | ... | @@ -153,8 +154,9 @@ def pushTopupStatus(request_id, response_code, _message): |
153 | 154 | |
154 | 155 | timestamp = strftime('%Y%m%d%H%M%S') |
155 | 156 | |
156 | - if response_code == '00': | |
157 | + if response_code == '00' and not dontparsesn: | |
157 | 158 | sn = xltunai.getSNFromMessage(message) |
159 | + LAST_SN = sn | |
158 | 160 | message = 'SN={0};{1}'.format(sn, message) |
159 | 161 | |
160 | 162 | push_message = CHIPINFO + '$' + message |
... | ... | @@ -225,6 +227,52 @@ def disablePull(): |
225 | 227 | except: |
226 | 228 | return |
227 | 229 | |
230 | +def adviceLastSN(requestId, modem): | |
231 | + global DISABLE_SEM | |
232 | + global LAST_SN | |
233 | + global TOPUP_USSD_TIMEOUT | |
234 | + | |
235 | + ussd_command = xltunai.getHistoryUSSDCommand() | |
236 | + | |
237 | + logger.info(u'Executing advice {0}'.format(ussd_command)) | |
238 | + try: | |
239 | + response = modem.sendUssd(ussd_command, TOPUP_USSD_TIMEOUT) | |
240 | + | |
241 | + message = response.message.strip() | |
242 | + logger.info(u'USSD response: {0}'.format(message)) | |
243 | + | |
244 | + lastSNFromHistory = xltunai.getLastSNFromHistoryMessage(message) | |
245 | + | |
246 | + if response.sessionActive: | |
247 | + response.cancel() | |
248 | + | |
249 | + if not lastSNFromHistory: | |
250 | + if DISABLE_SEM: | |
251 | + logger.info('Failed to get last sn from history, retrying in 15 secs') | |
252 | + sleep(15) | |
253 | + adviceLastSN(requestid, modem) | |
254 | + | |
255 | + elif lastSNFromHistory == LAST_SN: | |
256 | + topupMessage = "Topup gagal berdasarkan advice. {0} = {1}".format(lastSNFromHistory, LAST_SN) | |
257 | + pushTopupStatus(requestId, '40', topupMessage) | |
258 | + DISABLE_SEM = 0 | |
259 | + | |
260 | + | |
261 | + else: | |
262 | + topupMessage = "SN={0}; Topup berhasil berdasarkan advice. SN is not {1}".format(lastSNFromHistory, LAST_SN) | |
263 | + LAST_SN = lastSNFromHistory | |
264 | + pushTopupStatus(requestId, '00', topupMessage, True) | |
265 | + DISABLE_SEM = 0 | |
266 | + | |
267 | + except: | |
268 | + message = "USSD Error: Something wrong when executing USSD topup" | |
269 | + logger.warning(message) | |
270 | + pushTopupStatus(task['requestId'], '68', message) | |
271 | + waitForResultSmsAfterUssdError(modem, task['requestId'], message) | |
272 | + return | |
273 | + | |
274 | + | |
275 | + | |
228 | 276 | def topupTask(task, modem): |
229 | 277 | global LAST_REQUEST_ID |
230 | 278 | |
... | ... | @@ -278,13 +326,17 @@ def topupTask(task, modem): |
278 | 326 | message = "USSD Error: Timeout when executing USSD topup" |
279 | 327 | logger.warning(message) |
280 | 328 | pushTopupStatus(task['requestId'], '68', message) |
281 | - waitForResultSmsAfterUssdError(modem, task['requestId'], message) | |
329 | + #waitForResultSmsAfterUssdError(modem, task['requestId'], message) | |
330 | + sleep(15) | |
331 | + adviceLastSN(task['requestId'], modem) | |
282 | 332 | return |
283 | 333 | except: |
284 | 334 | message = "USSD Error: Something wrong when executing USSD topup" |
285 | 335 | logger.warning(message) |
286 | 336 | pushTopupStatus(task['requestId'], '68', message) |
287 | - waitForResultSmsAfterUssdError(modem, task['requestId'], message) | |
337 | + #waitForResultSmsAfterUssdError(modem, task['requestId'], message) | |
338 | + sleep(15) | |
339 | + adviceLastSN(task['requestId'], modem) | |
288 | 340 | return |
289 | 341 | |
290 | 342 | DISABLE_SEM = DISABLE_SEM_ON_TRX |
xltunai.py
... | ... | @@ -89,6 +89,9 @@ def toInternationalNumber(number): |
89 | 89 | |
90 | 90 | def getSIMCardInfoUSSDCommand(): |
91 | 91 | return '*123*7*3*1*1#' |
92 | + | |
93 | +def getHistoryUSSDCommand(): | |
94 | + return '*123*120*8*3#' | |
92 | 95 | |
93 | 96 | def getMSISDNFromSIMCardInfo(message): |
94 | 97 | msisdn = "".join(re.findall(r'Nomor \d+ adalah nomor', message)) |
... | ... | @@ -146,5 +149,7 @@ def getMasaAktifFromUssdResponseMessage(message): |
146 | 149 | masa_aktif = time.strptime(masa_aktif[0], '%d%b%y') |
147 | 150 | return time.strftime("%Y-%m-%d", masa_aktif) |
148 | 151 | |
149 | - | |
150 | - | |
152 | +def getLastSNFromHistoryMessage(message): | |
153 | + sn = re.findall(r"Transaksi Terakhir: (\d+)", message) | |
154 | + if sn: | |
155 | + return sn |