Commit aec451652103819fdf521f059a5ce9529ef95a96
1 parent
6f14cc6245
Exists in
master
update balance
Showing 2 changed files with 56 additions and 2 deletions Side-by-side Diff
gentong.js
... | ... | @@ -15,6 +15,8 @@ var redisClient; |
15 | 15 | |
16 | 16 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
17 | 17 | |
18 | +var aaa; | |
19 | + | |
18 | 20 | var logTag = __filename.split('/').reverse()[0]; |
19 | 21 | |
20 | 22 | function getRedisKey(timestamp) { |
... | ... | @@ -169,6 +171,7 @@ function createServer() { |
169 | 171 | } |
170 | 172 | |
171 | 173 | message = qs.info; |
174 | + updateBalance(message); | |
172 | 175 | if (sn) { |
173 | 176 | message = 'SN=' + sn + '; ' + message; |
174 | 177 | } |
... | ... | @@ -217,10 +220,57 @@ function responseCodeFromMessage(message) { |
217 | 220 | return; |
218 | 221 | } |
219 | 222 | |
220 | -function start(_config, _callbackReport) { | |
223 | +function updateBalance(message) { | |
224 | + var balance = getBalanceFromMessage(message); | |
225 | + if (balance) { | |
226 | + console.log('Balance: ' + balance); | |
227 | + | |
228 | + try { | |
229 | + aaa.updateBalance(balance); | |
230 | + } | |
231 | + catch(err) { | |
232 | + console.log('Exception on partner.updateBalance: ' + err); | |
233 | + } | |
234 | + } | |
235 | +} | |
236 | + | |
237 | +function getBalanceFromMessage(message, balance_regex) { | |
238 | + if (!balance_regex) { | |
239 | + if (config && config.globals && config.globals.balance_regex) { | |
240 | + balance_regex = config.globals.balance_regex; | |
241 | + } | |
242 | + } | |
243 | + | |
244 | + if (!balance_regex) { | |
245 | + return; | |
246 | + } | |
247 | + | |
248 | + try { | |
249 | + var re = new RegExp(balance_regex); | |
250 | + var matches = message.match(re); | |
251 | + | |
252 | + var result = matches[1]; | |
253 | + result = result.replace(/\./g, ''); | |
254 | + result = result.replace(/,/g, ''); | |
255 | + | |
256 | + | |
257 | + return Number(result); | |
258 | + } | |
259 | + catch(err) { | |
260 | + return; | |
261 | + } | |
262 | +} | |
263 | + | |
264 | +function start(_config, _callbackReport, options) { | |
221 | 265 | config = _config; |
222 | 266 | callbackReport = _callbackReport |
223 | 267 | |
268 | + if (options) { | |
269 | + if (options.aaa) { | |
270 | + aaa = options.aaa; | |
271 | + } | |
272 | + } | |
273 | + | |
224 | 274 | createRedisClient(); |
225 | 275 | createServer(); |
226 | 276 | } |
index.js
... | ... | @@ -9,5 +9,9 @@ var httpServer = HttpServer.start(config); |
9 | 9 | var aaa = require('sate24/aaa.js'); |
10 | 10 | var partner = require('./gentong.js'); |
11 | 11 | |
12 | -partner.start(config, aaa.callbackReport); | |
12 | +var partner_options = { | |
13 | + 'aaa': aaa | |
14 | +} | |
15 | + | |
16 | +partner.start(config, aaa.callbackReport, partner_options); | |
13 | 17 | aaa.start(config, partner); |