Commit 5a8de15542c4e63a79efdafe36a96979534118fd

Authored by Adhidarma Hadiwinoto
1 parent b691bfa013
Exists in master

matrix.not_ready_ts dan kawan-kawan

Showing 1 changed file with 22 additions and 11 deletions Side-by-side Diff

lib/partner-mkios.js
... ... @@ -22,7 +22,12 @@ if (!config || !config.partner || !config.partner.pin) {
22 22 }
23 23  
24 24 matrix.modem = {};
  25 +
25 26 matrix.not_ready = true;
  27 +matrix.not_ready_ts = null;
  28 +matrix.not_ready_ts_readable = null;
  29 +matrix.not_ready_max_age_secs = null;
  30 +
26 31 matrix.stock = {};
27 32  
28 33 const db = require('./local-db').getConnection();
... ... @@ -145,7 +150,7 @@ function onUSSDResponse(data) {
145 150 }
146 151  
147 152 deleteResumeHandler(last_trx_id);;
148   - matrix.not_ready = false;
  153 + unsuspendPull();
149 154  
150 155 report({
151 156 trx_id: last_trx_id,
... ... @@ -172,13 +177,7 @@ modem.open(function(err) {
172 177 }
173 178  
174 179 logger.info('Modem open successfully, going to ready in 30 secs');
175   - setTimeout(
176   - function() {
177   - matrix.not_ready = false;
178   - logger.info('Gateway is ready');
179   - },
180   - 30 * 1000
181   - )
  180 + setTimeout( unsuspendPull, 30 * 1000 )
182 181  
183 182 })
184 183  
... ... @@ -243,16 +242,28 @@ function getStockBalanceFromMesssage(msg, rules) {
243 242 function suspendPull(trx_id) {
244 243 logger.verbose('Set modem to not ready so no other task can be entered and registering delayed resume');
245 244 matrix.not_ready = true;
  245 + matrix.not_ready_ts = new Date();
  246 + matrix.not_ready_ts_readable = moment(matrix.not_ready_ts).format('YYYY-MM-DD HH:mm:ss');
246 247  
247 248 resumeHandlers['trx' + trx_id] = setTimeout(function() {
248 249 logger.verbose('Resuming supsended modem', {trx_id: trx_id});
249   - matrix.not_ready = false;
  250 + unsuspendPull();
250 251 report({
251 252 trx_id: trx_id,
252 253 rc: '68',
253 254 message: 'USSD timeout'
254 255 })
255   - }, Number(config.partner.modem.suspend_time_ms) || 20 * 1000 )
  256 + }, Number(config.partner.modem.suspend_time_ms) || 20 * 1000 );
  257 +}
  258 +
  259 +function unsuspendPull() {
  260 + matrix.not_ready = false;
  261 +
  262 + if (matrix.not_ready_ts) {
  263 + matrix.not_ready_max_age_secs = Math.max( (new Date() - matrix.not_ready_ts) / 1000, matrix.not_ready_max_age_secs );
  264 + }
  265 +
  266 + logger.verbose('Modem is ready');
256 267 }
257 268  
258 269 function deleteResumeHandler(trx_id) {
... ... @@ -266,7 +277,7 @@ function deleteResumeHandler(trx_id) {
266 277 function onTrxFinish(trx_id) {
267 278 deleteResumeHandler(trx_id);
268 279 pendingArchive.remove(trx_id);
269   - matrix.not_ready = false;
  280 + unsuspendPull();
270 281 }
271 282  
272 283 function buy(task) {