Created
January 16, 2013 08:14
-
-
Save pphetra/4545447 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var statePool = require('./state.js'); | |
| var eventBus = require('./eventBus.js'); | |
| var dbService = require('./dbService.js'); | |
| var generator = require('./cmdGenerator.js'); | |
| var blockPhone = require('./phoneLockService.js'); | |
| var blockService = require('./blockService.js'); | |
| var txService = require('./txService.js'); | |
| var mcService = require('./machineService.js'); | |
| var async = require('async'); | |
| var service = require('./service.js'); | |
| var spayUtil = require('./spayUtil.js'); | |
| var util = require('util'); | |
| var _ = require('underscore'); | |
| var config = require('../config.js').config; | |
| var loggers = require('./logger.js').loggers; | |
| var logger = loggers.get('dev'); | |
| var loggerErr = loggers.get('error'); | |
| var auditLog = loggers.get('audit'); | |
| var txLog = loggers.get('tx'); | |
| var moment = require('moment'); | |
| var TIMEOUT = 10000; | |
| var DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss'; | |
| var DATE_SEND_FORMAT = 'DDMMYYYY'; | |
| var WAIT_STATUS = config.status.wait; | |
| var A_STATUS = config.status.a; | |
| var C_STATUS = config.status.c; | |
| var E_STATUS = config.status.e; | |
| var AA_STATUS = config.status.activeAlarm; | |
| var providerMap = { | |
| '0101': config.label.ais, | |
| '0102': config.label.dtac, | |
| '0103': config.label.truemove, | |
| '3006': config.label.cashcard01, | |
| '3007': config.label.cashcard02, | |
| '0301': config.label.asiasoft, | |
| '0302': config.label.winner, | |
| '0303': config.label.cookieCard, | |
| '0304': config.label.tot, | |
| '0401': config.label.scale, | |
| '0601': config.label.trueMoney, | |
| '0602': config.label.trueLife | |
| } | |
| function process00(command, callback) { | |
| statePool.get(command.machineId, checkState); | |
| var now = new Date(); | |
| var nowMc = now.toISOString(); | |
| function checkState(err, state) { | |
| if (err) { return callback(err) }; | |
| getMachine(command.machineId, function(errMachine, objMachine) { | |
| // console.log('errMachine -> ',errMachine , 'objMachine -> ',objMachine); | |
| if (errMachine) { | |
| loggerErr.error(command.machineId+' process00 getMachine err : '+errMachine); | |
| statePool.delProperty(command.machineId, 'activeStatus', function(err) { | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: '0' | |
| }), | |
| true, | |
| 'errMachine' | |
| ); | |
| return; | |
| }); | |
| } else { | |
| switch(command.status) { | |
| case '1': | |
| // กรณีมี state อยุ่แล้ว | |
| // -> เครื่องใหม่ที่มี machineId ซ้ำ พยายามต่อเข้ามา หรือ | |
| // -> เครื่องเก่าแต่ reboot | |
| getAccount(objMachine.groupId, function(errAccount, objAccount) { | |
| if (errAccount) { | |
| loggerErr.error('process00 getAccount err : '+errAccount); | |
| statePool.delProperty(command.machineId, 'activeStatus', function(err) { | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: objMachine.activeStatus | |
| }), | |
| true, | |
| 'errAccount' | |
| ); | |
| return; | |
| }); | |
| } else { | |
| // console.log(command.machineId,' state > ',state,' chk ',(state != null)); | |
| if (state != null) { | |
| if (state.sid) { | |
| if (state.sid != command.sid) { // มี connection ใหม่ต่อเข้ามา โดยมี machineId ตรงกัน แต่ socket คนละตัว | |
| checkDuplicateConnection(command, state, objMachine, objAccount); | |
| } else { | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| status: '1', | |
| version: command.version, | |
| accountId: objAccount.accountId, | |
| groupMemberId: objAccount.groupMemberId, | |
| groupId: objMachine.groupId, | |
| groupName: objAccount.groupName, | |
| patternId: objMachine.patternId, | |
| activeStatus: objMachine.activeStatus | |
| }, function(errMcService){ | |
| if (errMcService) { | |
| loggerErr.error('machineId '+command.machineId+ | |
| 'process00 status = 0 mcService err : '+errMcService); | |
| } | |
| statePool.setPropertyMap(command.machineId, { | |
| groupMemberId:objAccount.groupMemberId, | |
| groupName:objAccount.groupName, | |
| accountId: objAccount.accountId, | |
| groupId: objMachine.groupId, | |
| patternId: objMachine.patternId, | |
| activeStatus: objMachine.activeStatus, | |
| isCharge: objAccount.isCharge, | |
| machineType: objMachine.machineType | |
| }); | |
| }); | |
| dbService.updateMachineInfo(command, function(errUpdateMachine){ | |
| loggerErr.error('updateMachineInfo state.sid['+state.sid+']>',command); | |
| }); | |
| if (objMachine.activeStatus && objAccount.credit < 10) { | |
| updateActiveStatusMachine(0, command.machineId); | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: 0 | |
| }); | |
| } else { | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: objMachine.activeStatus | |
| }); | |
| } | |
| } | |
| } else { | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| status: '1', | |
| version: command.version, | |
| accountId: objAccount.accountId, | |
| groupMemberId: objAccount.groupMemberId, | |
| groupId: objMachine.groupId, | |
| groupName: objAccount.groupName, | |
| patternId: objMachine.patternId, | |
| activeStatus: objMachine.activeStatus | |
| }, function(errMcService){ | |
| }); | |
| statePool.setPropertyMap(command.machineId, { | |
| id: state.id ? state.id : '00', | |
| sid: command.sid, | |
| groupMemberId:objAccount.groupMemberId, | |
| groupName:objAccount.groupName, | |
| accountId: objAccount.accountId, | |
| groupId: objMachine.groupId, | |
| patternId: objMachine.patternId, | |
| activeStatus: objMachine.activeStatus, | |
| isCharge: objAccount.isCharge, | |
| machineType: objMachine.machineType | |
| }); | |
| dbService.updateMachineInfo(command, function(errUpdateMachine){ | |
| //loggerErr.error('['+state.id+']updateMachineInfo not have state id > ',command); | |
| }); | |
| if (objMachine.activeStatus && objAccount.credit < 10) { | |
| updateActiveStatusMachine(0, command.machineId); | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: 0 | |
| }); | |
| } else { | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: objMachine.activeStatus | |
| }); | |
| } | |
| } | |
| } else { // สร้าง state | |
| statePool.set(command.machineId, { | |
| id: '00', | |
| sid: command.sid, | |
| updatedDate: new Date(), | |
| activeStatus:objMachine.activeStatus, | |
| accountId: objAccount.accountId, | |
| groupMemberId: objAccount.groupMemberId, | |
| groupId: objMachine.groupId, | |
| groupName: objAccount.groupName, | |
| patternId: objMachine.patternId, | |
| isCharge: objAccount.isCharge, | |
| machineType: objMachine.machineType | |
| }, function(err) { | |
| if (err) { | |
| loggerErr.error('machineId '+command.machineId+' process00 set statePool err : '+err); | |
| } | |
| dbService.updateMachineInfo(command, function(errGetMachine, results){ | |
| if (errGetMachine) { | |
| loggerErr.error('machineId '+command.machineId+' errGetMachine : '+errGetMachine); | |
| } | |
| mcService.setPropertyNewMap(command.machineId, { | |
| id: command.machineId, | |
| reqUpdate: now, | |
| dataUpdate: nowMc, | |
| status: command.status, | |
| ip: '', | |
| inbox:objMachine.inbox, | |
| version: command.version, | |
| owner:objMachine.owner, | |
| toUse:objMachine.activeStatus, | |
| groupMemberId:objAccount.groupMemberId, | |
| groupName:objAccount.groupName, | |
| accountId: objAccount.accountId, | |
| groupId: objMachine.groupId, | |
| patternId: objMachine.patternId, | |
| status: '1' | |
| }, function(errMcService){ | |
| }); | |
| }); | |
| if (objMachine.activeStatus && objAccount.credit < 10) { | |
| updateActiveStatusMachine(0, command.machineId); | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: 0 | |
| }); | |
| } else { | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: objMachine.activeStatus | |
| }); | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| break; | |
| case '2': // keep connection | |
| // console.log('state --> '+JSON.stringify(objMachine)); | |
| if (state && state.sid && (state.sid == command.sid)) { | |
| if (state.waitForValidate) { // | |
| var sid2 = state.waitForValidate; | |
| statePool.delProperty(command.machineId, 'waitForValidate', function(err) { | |
| eventBus.emit('writeThroughSocket', sid2, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| activeStatus: '0', | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| true, | |
| 'waitForValidate : '+state.waitForValidate | |
| ); | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| status: '1' | |
| }, function(errMcService){ | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| activeStatus: objMachine.activeStatus, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }); | |
| }); | |
| }); | |
| } else { | |
| statePool.setProperty(command.machineId, 'updatedDate', new Date(), function(err) { | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| machineType: objMachine.machineType, | |
| status: '1' | |
| }, function(errMcService){ | |
| return callback(null, { | |
| code: '00', | |
| status: '1', | |
| activeStatus: objMachine.activeStatus, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }); | |
| }); | |
| }); | |
| } | |
| } else { | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: '0' | |
| }), | |
| true, | |
| state.sid+' != '+command.sid | |
| ); | |
| return; | |
| } | |
| // update last ping time | |
| break; | |
| case '0': // disconnect | |
| if (state && state.sid && (state.sid == command.sid)) { | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: '0' | |
| }), | |
| true, | |
| 'disconnect' | |
| ); | |
| dbService.updateMachineInfo(command, function(errUpdateMachine){ | |
| if (errUpdateMachine) { | |
| loggerErr.error('machineId '+command.machineId+ | |
| 'process00 status = 0 updateMachineInfo err : '+errUpdateMachine); | |
| } | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| status: command.status | |
| }, function(errMcService){ | |
| if (errMcService) { | |
| loggerErr.error('machineId '+command.machineId+ | |
| 'process00 status = 0 mcService err : '+errMcService); | |
| } | |
| }); | |
| }); | |
| } else { | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: '0' | |
| }), | |
| true, | |
| 'disconnect ' +state.sid+' != '+command.sid | |
| ); | |
| } | |
| return ; | |
| break; | |
| } | |
| } | |
| }); | |
| }//end if | |
| function checkDuplicateConnection(command, state, objMachine, objAccount) { | |
| statePool.setProperty(command.machineId, 'waitForValidate', command.sid, responseToFirstSocket); | |
| function responseToFirstSocket(err) { | |
| if (err) { loggerErr.error('err in responseToFirstSocket : '+err); | |
| return callback(err) }; | |
| //server ส่ง command ไปถาม connection แรก ว่ายังมีชีวิตอยู่ไหม | |
| logger.info('write confirm socket state id '+state.sid); | |
| eventBus.emit('writeThroughSocket', | |
| state.sid, | |
| generator.command({ | |
| req: true, | |
| code: '00', | |
| status: '2', | |
| activeStatus: objMachine.activeStatus, | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| machineId: command.machineId | |
| }), | |
| false, | |
| 'checkDuplicateConnection state id '+state.sid | |
| ); | |
| // server set timeout เพื่อตัด connection 1 ทิ้งไป ในกรณีที่ไม่ตอบข้อมูลกลับมา | |
| setTimeout(function() { | |
| statePool.get(command.machineId, function(err, state) { | |
| if (state.waitForValidate) { | |
| logger.info('write close socket state id '+state.sid); | |
| eventBus.emit('writeThroughSocket', | |
| state.sid, | |
| generator.command({ | |
| req: true, | |
| code: '00', | |
| status: '0', | |
| activeStatus: '0', | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| true, | |
| 'chk disconnect state id '+state.sid | |
| ); | |
| var sid2 = state.waitForValidate; | |
| statePool.delProperty(command.machineId, 'waitForValidate'); | |
| statePool.setPropertyMap(command.machineId, { | |
| //id: '00', | |
| sid: sid2, | |
| groupMemberId:objAccount.groupMemberId, | |
| groupName:objAccount.groupName, | |
| accountId: objAccount.accountId, | |
| groupId: objMachine.groupId, | |
| patternId: objMachine.patternId, | |
| isCharge: objAccount.isCharge, | |
| machineType: objMachine.machineType | |
| }, function(err) { | |
| logger.info('write upgrade socket state id '+sid2); | |
| dbService.updateMachineInfo(command, function(){ | |
| eventBus.emit('writeThroughSocket', | |
| sid2, | |
| generator.command({ | |
| req: false, | |
| code: '00', | |
| status: '1', | |
| activeStatus: objMachine.activeStatus, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| false, | |
| 'new connection state id '+sid2 | |
| ); | |
| mcService.setPropertyMap(command.machineId, { | |
| id: command.machineId, | |
| dataUpdate: nowMc, | |
| inbox:objMachine.inbox, | |
| status: '1', | |
| version: command.version, | |
| groupMemberId:objAccount.groupMemberId, | |
| groupName:objAccount.groupName, | |
| accountId: objAccount.accountId, | |
| groupId: objMachine.groupId, | |
| patternId: objMachine.patternId | |
| }, function(errMcService){ | |
| if (errMcService) { | |
| loggerErr.error('machineId '+command.machineId+ | |
| 'process00 status = 0 mcService err : '+errMcService); | |
| } | |
| if (objMachine.activeStatus && objAccount.credit < 10) { | |
| updateActiveStatusMachine(0, command.machineId); | |
| return callback(null,{ | |
| code: '13', | |
| action: 0, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }); | |
| } | |
| }); | |
| }); | |
| }) | |
| } | |
| }); | |
| }, TIMEOUT); | |
| } | |
| } | |
| } | |
| function process01(command, callback) { | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| if (state && state.waitForValidate) { | |
| loggerErr.error('process01 for waitForValidate phoneNumber '+command.phoneNumber); | |
| } | |
| if (state.id === '01') { | |
| loggerErr.error('state.id = 01 '); | |
| if ((command.command === state.command) && state.sequence) { | |
| loggerErr.error('command equals in state > ',command.command); | |
| prepareTransaction(null); | |
| } else { | |
| setTx(err, command, state); | |
| } | |
| } else { | |
| setTx(err, command, state); | |
| } | |
| }//end checkState | |
| function setTx(err, command, state) { | |
| var seq = spayUtil.generateSeq(command.machineId); | |
| var now = new Date(); | |
| txService.setPropertyNewMap(seq, { | |
| machineId: command.machineId, | |
| phoneNumber: command.phoneNumber, | |
| providerName:providerMap[command.typeMenu+command.provider], | |
| typeMenu: command.typeMenu, | |
| provider: command.provider, | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| sequence: seq, | |
| dataUpdate: spayUtil.formatDateTime(now), | |
| resultDisplayCode: '', | |
| resultDisplay:WAIT_STATUS, | |
| groupMemberId:state.groupMemberId, | |
| accountId: state.accountId, | |
| groupId: state.groupId, | |
| patternId: state.patternId, | |
| stateId: state ? state.id : null | |
| }, function(errtxService){ | |
| if(err || errtxService){ | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '10'), { | |
| code: '01', | |
| sequence: seq | |
| })); | |
| } else { | |
| if (state.id === '00') { | |
| // generate seq for keep transaction | |
| statePool.setPropertyMap(command.machineId, { | |
| 'id': '01', | |
| 'typeMenu' : command.typeMenu, | |
| 'sequence': seq, | |
| 'command' : command.command | |
| }, prepareTransaction); | |
| } else if (state.id === '01') { | |
| // generate seq for keep transaction | |
| loggerErr.error(command.command+' command not equals in state ',state.command); | |
| var tmpSeq = state.sequence; | |
| loggerErr.error('gen new seq > ',seq); | |
| statePool.setPropertyMap(command.machineId, { | |
| 'typeMenu' : command.typeMenu, | |
| 'sequence': seq, | |
| 'command' : command.command | |
| }, prepareTransaction); | |
| txService.setPropertyMap(tmpSeq, | |
| getResultNotWork(command.typeMenu, '30'), | |
| function(){ | |
| dbService.insertTxError(tmpSeq, function(errInTxError){ | |
| if (errInTxError) { | |
| loggerErr.error(command.phoneNumber+' state is 01 insertTxError err : '+errInTxError); | |
| } | |
| }); | |
| }); | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '31'), { | |
| code: '01', | |
| sequence: seq | |
| })); | |
| } | |
| } | |
| }); | |
| } | |
| function prepareTransaction(err) { | |
| statePool.get(command.machineId, function(errState, state){ | |
| if (err) { | |
| loggerErr.error('machineId '+command.machineId+'process01 prepareTransaction err = '+err); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else if (command.typeMenu != '04' && (command.phoneNumber === null || command.phoneNumber === '')) { | |
| loggerErr.error('machineId '+command.machineId+' process01 prepareTransaction phoneNumber empty'); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '19'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| getMachine(command.machineId, function(errMachine, objMachine) { | |
| if (errMachine) { | |
| loggerErr.error('machineId '+command.machineId+'process01 getMachine err = '+errMachine); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| if (objMachine.groupId == '-1') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '12'), { | |
| code: '01', | |
| groupId : '-1', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| getAccount(objMachine.groupId, function(errAccount, objAccount) { | |
| //console.log('objAccount > ',objAccount); | |
| if (errAccount) { | |
| loggerErr.error('machineId '+command.machineId+'process01 getAccount err = '+errAccount); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| statePool.setProperty(command.machineId,'isCharge',objAccount.isCharge); | |
| txService.setPropertyMap(state.sequence, { | |
| groupId : objMachine.groupId, | |
| accountId: objAccount.accountId, | |
| patternId: objMachine.patternId, | |
| inbox: objMachine.inbox, | |
| accountBalance: objAccount.credit, | |
| groupMemberId: objAccount.groupMemberId, | |
| userLevel: objAccount.userLevel, | |
| isCharge : objAccount.isCharge, | |
| machineType: objMachine.machineType, | |
| typeTrueTopup: objMachine.typeTrueTopup | |
| }, function(errTxService){ | |
| if (errTxService) { | |
| loggerErr.error('prepareTransaction setPropertyMap err : '+errTxService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| if (objMachine.activeStatus == '0') { | |
| //เครื่องปิดบริการ | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '11'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else if (objAccount.activeStatus == '0') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '13'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else if (objAccount.credit < 10){ | |
| console.log('credit > ',objAccount.credit ); | |
| dbService.updateMachineActiveStatus(0, command.machineId, function(){ | |
| }); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '18'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| var getMenuFunc = async.apply( | |
| getMenu, | |
| state, | |
| objMachine, | |
| objAccount | |
| ); | |
| getMenuFunc(null); | |
| } | |
| } | |
| }); | |
| }// end if | |
| });//end getAccount | |
| }//end if | |
| } | |
| }); | |
| }//end if | |
| }); | |
| } | |
| function getMenu(state, objMachine , objAccount) { | |
| blockService.getProperty( | |
| command.typeMenu+command.provider, 'statusUrgent', | |
| function(errBlockService, status){ | |
| if (errBlockService) { | |
| loggerErr.error('getMenu blockService getProperty err : '+errBlockService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else if(status == '0') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '32'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| dbService.getMenu({ | |
| patternId: objMachine.patternId, | |
| typeMenu: command.typeMenu, | |
| provider: command.provider | |
| }, async.apply(getOrCreatePhoneNumberBalance, state, objMachine , objAccount)); | |
| } | |
| } | |
| ); | |
| } | |
| function getOrCreatePhoneNumberBalance(state, objMachine , objAccount, err, menuResult) { | |
| if (err) { | |
| statePool.setProperty(command.machineId,'id','00', | |
| function(){ | |
| statePool.delProperty(command.machineId,'sequence', | |
| function(){ | |
| }); | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| } | |
| ); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '16'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| if (menuResult) { | |
| //logger.info(JSON.stringify(menuResult)); | |
| dbService.getCustomerBalance({ | |
| groupId: objMachine.groupId, | |
| groupMemberId: objAccount.groupMemberId, | |
| phoneNumber: command.phoneNumber | |
| }, function(errGetBalance, objCustomer) { | |
| var answer01Func = async.apply( | |
| answer01, | |
| state, | |
| objCustomer, | |
| menuResult, | |
| objAccount | |
| ); | |
| if (errGetBalance) { | |
| if (objCustomer) { | |
| var now = new Date(); | |
| var expire = now; | |
| //var expire = now + '60 day'; | |
| var providerFromCmd = command.provider; | |
| if (command.typeMenu == '04') { | |
| providerFromCmd = '' | |
| } | |
| dbService.createCustomerBalance({ | |
| phoneNumber: command.phoneNumber, | |
| dataUpdate: now, | |
| expDate: now, | |
| provider: providerFromCmd, | |
| groupMemberId: objAccount.groupMemberId, | |
| groupId: objMachine.groupId | |
| }, answer01Func) | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } | |
| } else { | |
| answer01Func(null); | |
| } | |
| } | |
| ); | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '16'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } | |
| } | |
| function answer01(state, objCustomer, menuResult, objAccount, err) { | |
| if (err) { | |
| loggerErr.error('function answer01 error : '+err); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| var now = new Date(); | |
| var userBalance = 0 ; | |
| if (objCustomer.userBalance >= 0) { | |
| userBalance = objCustomer.userBalance; | |
| if(objCustomer.expDate < now){ | |
| userBalance = 0; | |
| } | |
| } | |
| txService.setPropertyMap(state.sequence, { | |
| menuId: menuResult.menuId, | |
| userBalance: userBalance ? userBalance : 0, | |
| dataUpdate: spayUtil.formatDateTime(now), | |
| resultDisplayCode: '', | |
| percent: menuResult.dividend | |
| }, | |
| function(errTxService) { | |
| if (errTxService) { | |
| loggerErr.error('errTxService error : '+errTxService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| blockService.getProperty(command.typeMenu+command.provider, 'statusNormal', function(errBlockService, activeStatus){ | |
| if (errBlockService) { | |
| loggerErr.error('errBlockService : '+errBlockService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| blockPhone.lock(command, | |
| function(errLock, res){ | |
| if (errLock || !res) { | |
| statePool.setProperty(command.machineId,'id','00', | |
| function(){ | |
| statePool.delProperty(command.machineId,'sequence', | |
| function(){ | |
| }); | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| }); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '17'), { | |
| code: '01', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| return callback(null,{ | |
| code: '01', | |
| menuAll: menuResult.menuAll, | |
| typeMenu: command.typeMenu, | |
| provider: command.provider, | |
| userBalance: userBalance ? userBalance : 0, | |
| accountBalance: objAccount.credit, | |
| sequence: state.sequence, | |
| activeStatus: activeStatus | |
| }); | |
| } | |
| });//blockPhone lock | |
| } | |
| }); | |
| }//end if | |
| }); | |
| } | |
| }//end function answer01 | |
| } | |
| } | |
| function process02(command, callback) { | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| if (err) { | |
| loggerErr.error(command.sequence+' checkState cmd 02 fail '); | |
| if (state.typeMenu) { | |
| return callback(true, | |
| _.extend(getResultFailure(state.typeMenu, '31'), { | |
| code: command.code, | |
| sequence: command.sequence | |
| })); | |
| } else { | |
| return callback(true, { | |
| code: command.code, | |
| sequence: command.sequence | |
| }); | |
| } | |
| } | |
| if (state.id == '01') { | |
| getCurrentTx(state); | |
| } else { | |
| loggerErr.error(command.sequence+' state id '+state.id+ ' not add to q cmd 02'); | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| } | |
| } | |
| function getCurrentTx(state) { | |
| logger.info(state.sequence+' compare '+command.sequence); | |
| if (state.sequence === command.sequence) { | |
| statePool.setProperty( | |
| command.machineId, | |
| 'id', | |
| '00', | |
| function(){ | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| txService.getPropertyAll( | |
| command.sequence, | |
| async.apply(processTransaction, state)); | |
| } | |
| ); | |
| } else { | |
| getCurrentTxFromDb(state); | |
| loggerErr.error(state.sequence + ' !== '+ command.sequence+' and go to getCurrentTxFromDb'); | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| } | |
| } | |
| function getCurrentTxFromDb(state) { | |
| dbService.getTx(command.sequence, function(err, results){ | |
| if (results) { | |
| if (results.resultDisplayCode == 'C') { | |
| txService.setPropertyMap(command.sequence, | |
| results | |
| , function(){ | |
| dbService.deleteTx(command.sequence, function() { | |
| processUpdateTransaction(results, state); | |
| }); | |
| }); | |
| } else { | |
| loggerErr.error(command.sequence+' from db inoperative cmd 02 state seq : '+state.sequence); | |
| } | |
| } else { | |
| loggerErr.error(command.sequence+' inoperative cmd 02 state seq : '+state.sequence+' results = null'); | |
| } | |
| }); | |
| } | |
| function processTransaction(state, err, currentState) { | |
| if (err) { loggerErr.error('processTransaction err : '+err); }; | |
| statePool.delProperty(currentState.machineId, 'sequence'); | |
| blockPhone.wait(currentState, function(errWait){ | |
| if (errWait) { | |
| loggerErr.error('processTransaction wait err : '+errWait); | |
| } | |
| if((parseInt(command.menu, 10)+parseInt(command.fees, 10)) <= | |
| (parseInt(command.money, 10)+parseInt(currentState.userBalance, 10))){ | |
| if ((command.menu != '0') || (currentState.typeMenu == '04')) { | |
| txService.setPropertyMap(state.sequence, { | |
| menu: command.menu, | |
| money: command.money, | |
| userBalance: currentState.userBalance, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| groupMemberId: currentState.groupMemberId | |
| }, function(errSetTx) { | |
| if (errSetTx) { | |
| loggerErr.error('updateTransaction set tx err : '+errSetTx); | |
| } | |
| if (currentState.phoneNumber != null && (typeof currentState.phoneNumber != 'undefined')) { | |
| if (state.machineType == '1' || currentState.typeMenu == '04') { | |
| queue_callback = function(errQueueTask){ | |
| if (errQueueTask) { | |
| loggerErr.error('queueTask err : '+errQueueTask); | |
| } | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| } | |
| service.queueTask( | |
| currentState.patternId, | |
| currentState.typeMenu, | |
| currentState.provider, | |
| currentState.phoneNumber, | |
| command.menu, | |
| command.sequence, | |
| currentState.machineId, | |
| currentState.typeTrueTopup, | |
| queue_callback | |
| ); | |
| txLog.info(util.format("[queueTask:02] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| currentState.patternId, currentState.typeMenu, currentState.provider, currentState.phoneNumber, command.menu, command.sequence)); | |
| } else { | |
| txService.setPropertyMap(state.sequence,getResultSuccess(currentState.typeMenu), function(er) { | |
| dbService.insertTx(state.sequence, function(){ | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| }); | |
| txLog.info(util.format("[test:02] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| currentState.patternId, currentState.typeMenu, currentState.provider, currentState.phoneNumber, command.menu, command.sequence)); | |
| } | |
| } else { | |
| loggerErr.error('updateTransaction cmd 02 can not add to q : ',currentState); | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| } | |
| }); | |
| } else { | |
| loggerErr.error(state.sequence+' can not add to q menu = 0 ',currentState); | |
| txService.setPropertyMap(state.sequence, | |
| _.extend(getResultFailure(currentState.typeMenu, '35'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| groupMemberId: currentState.groupMemberId | |
| }), function(err) { | |
| dbService.insertTx(state.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction insertTx err : '+errInsertTx); | |
| } | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| }); | |
| } | |
| } else { | |
| loggerErr.error(state.sequence+' money < menu can not add to q cmd 02 ',currentState); | |
| txService.setPropertyMap(state.sequence, | |
| _.extend(getResultFailure(state.typeMenu, '34'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| groupMemberId: currentState.groupMemberId | |
| }), function(err) { | |
| dbService.insertTx(state.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction insertTx err : '+errInsertTx); | |
| } | |
| return callback(null, { | |
| code: '02', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| }); | |
| } | |
| }); | |
| } | |
| function processUpdateTransaction(results, currentState) { | |
| blockPhone.wait(results, function(errWait){ | |
| if (errWait) { | |
| loggerErr.error('processUpdateTransaction wait err : '+errWait); | |
| } | |
| if((parseInt(command.menu, 10)+parseInt(command.fees, 10)) <= | |
| (parseInt(command.money, 10)+parseInt(results.userBalance, 10))){ | |
| if ((command.menu != '0') || (results.typeMenu == '04')) { | |
| txService.setPropertyMap(results.sequence, { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: currentState.isCharge | |
| }, function(errSetTx) { | |
| if (errSetTx) { | |
| loggerErr.error('updateTransaction set tx err : '+errSetTx); | |
| } | |
| if (results.phoneNumber != null && (typeof results.phoneNumber != 'undefined')) { | |
| if (currentState.machineType == '1') { | |
| queue_callback = function(errQueueTask){ | |
| if (errQueueTask) { | |
| loggerErr.error('queueTask retry-request err : '+errQueueTask); | |
| } | |
| } | |
| service.queueTask( | |
| results.patternId, | |
| results.typeMenu, | |
| results.provider, | |
| results.phoneNumber, | |
| command.menu, | |
| command.sequence, | |
| results.machineId, | |
| results.typeTrueTopup, | |
| queue_callback | |
| ); | |
| txLog.info(util.format("[queueTask-retry-request:02] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| results.patternId, results.typeMenu, results.provider, results.phoneNumber, command.menu, command.sequence)); | |
| } else { | |
| txService.setPropertyMap(results.sequence,getResultSuccess(results.typeMenu), function(er) { | |
| dbService.insertTx(results.sequence, function(){ | |
| }) | |
| }); | |
| txLog.info(util.format("[test:queueTask-retry-request:02] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| results.patternId, results.typeMenu, results.provider, results.phoneNumber, command.menu, command.sequence)); | |
| } | |
| } else { | |
| loggerErr.error('processUpdateTransaction retry-request can not add to q : ',results); | |
| } | |
| }); | |
| } else { | |
| loggerErr.error(results.sequence+' retry-request can not add to q menu = 0 ', results); | |
| txService.setPropertyMap(results.sequence, | |
| _.extend(getResultFailure(results.typeMenu, '35'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: currentState.isCharge | |
| }), function(err) { | |
| dbService.insertTx(results.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processUpdateTransaction insertTx err : '+errInsertTx); | |
| } | |
| }); | |
| }); | |
| } | |
| } else { | |
| loggerErr.error(results.sequence+' money < menu can not add to q cmd 02 processUpdateTransaction ',results); | |
| txService.setPropertyMap(results.sequence, | |
| _.extend(getResultFailure(results.typeMenu, '34'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: currentState.isCharge | |
| }), function(err) { | |
| dbService.insertTx(results.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processUpdateTransaction insertTx err : '+errInsertTx); | |
| } | |
| }); | |
| }); | |
| } | |
| }); | |
| } | |
| } | |
| function process03(command, callback) { | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| if (err) { | |
| loggerErr.error(command.sequence+' checkState cmd 03 fail '); | |
| return callback(true, | |
| _.extend(getResultFailure(state.typeMenu, '31'), { | |
| code: command.code, | |
| sequence: command.sequence | |
| })); | |
| } | |
| txService.getPropertyAll( | |
| command.sequence, | |
| function(errGetTx, currentState){ | |
| if (errGetTx) { | |
| loggerErr.error(command.sequence+' get txService cmd 03 fail '); | |
| } | |
| if (currentState != null && (typeof currentState != 'undefined') && currentState.sequence) { | |
| if (currentState.cmd) { | |
| logger.info('inoperative cmd 03 state currentState cmd >>> ',command.sequence); | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| } else { | |
| txService.setProperty(command.sequence, 'cmd', '03', | |
| function(errSetCmd){ | |
| if (state.sequence === command.sequence) { | |
| statePool.delProperty(command.machineId, 'sequence'); | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| statePool.setProperty( | |
| command.machineId, | |
| 'id', | |
| '00', | |
| async.apply(processTransaction, state, currentState) | |
| ); | |
| } else { | |
| loggerErr.error(state.sequence+' != '+command.sequence+' cmd 03'); | |
| if (state.sequence) { | |
| loggerErr.error(state.sequence+' != null '+' add to q 03'); | |
| var processTransactionFunc = async.apply( | |
| processTransaction, | |
| state, | |
| currentState | |
| ); | |
| processTransactionFunc(null); | |
| } else { | |
| loggerErr.error(state.sequence+' = null '+' not add to q 03'); | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| } | |
| } | |
| } | |
| ); | |
| } | |
| } else { | |
| getCurrentTxFromDb(state); | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| } | |
| } | |
| ); | |
| } | |
| function getCurrentTxFromDb(state) { | |
| dbService.getTx(command.sequence, function(err, results){ | |
| console.log('getTx in getCurrentTxFromDb in cmd 03 >>> ',results); | |
| if (results) { | |
| console.log('getTx in getCurrentTxFromDb resultDisplayCode >>> ',results.resultDisplayCode); | |
| if (results.resultDisplayCode == 'C') { | |
| txService.setPropertyMap(command.sequence, | |
| results | |
| , function(){ | |
| txService.setProperty(command.sequence, 'cmd', '03', function() { | |
| dbService.deleteTx(command.sequence, function() { | |
| processUpdateTransaction(results, state); | |
| }); | |
| }); | |
| }); | |
| } else { | |
| loggerErr.error(command.sequence+' from db inoperative cmd 03 state seq : '+state.sequence); | |
| } | |
| } else { | |
| logger.info(command.sequence+' from db inoperative cmd 03 state because task is done'); | |
| } | |
| }); | |
| } | |
| function processTransaction(state, currentState, err) { | |
| if (err) { loggerErr.error('command 03 processTransaction err : '+err); }; | |
| blockPhone.wait(currentState, function(errWait){ | |
| if (errWait) { | |
| loggerErr.error('processTransaction wait err : '+errWait); | |
| } | |
| if((parseInt(command.menu, 10)+parseInt(command.fees, 10)) <= (parseInt(command.money, 10)+parseInt(currentState.userBalance, 10))){ | |
| if ((command.menu != '0') || (currentState.typeMenu == '04')) { | |
| if (currentState.phoneNumber != null && (typeof currentState.phoneNumber != 'undefined')) { | |
| if (state.machineType == '1' || currentState.typeMenu == '04') { | |
| service.queueTask( | |
| currentState.patternId, | |
| currentState.typeMenu, | |
| currentState.provider, | |
| currentState.phoneNumber, | |
| command.menu, | |
| command.sequence, | |
| currentState.machineId, | |
| currentState.typeTrueTopup, | |
| async.apply(updateTransaction, state, currentState) | |
| ); | |
| txLog.info(util.format("[queueTask:03] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| currentState.patternId, currentState.typeMenu, currentState.provider, currentState.phoneNumber, command.menu, command.sequence)); | |
| } else { | |
| txService.setPropertyMap(command.sequence,getResultSuccess(currentState.typeMenu), function(er) { | |
| dbService.insertTx(command.sequence, function(){ | |
| }); | |
| }); | |
| txLog.info(util.format("[test:03] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| currentState.patternId, currentState.typeMenu, currentState.provider, currentState.phoneNumber, command.menu, command.sequence)); | |
| } | |
| } else { | |
| loggerErr.error('processTransaction retry-request can not add to q cmd 03: ',results); | |
| } | |
| } else { | |
| loggerErr.error(state.sequence+' can not add to q menu = 0'); | |
| txService.setPropertyMap(state.sequence, | |
| _.extend(getResultFailure(currentState.typeMenu, '35'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| groupMemberId: currentState.groupMemberId | |
| }), function(err) { | |
| dbService.insertTx(state.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction menu = 0 err : '+errInsertTx); | |
| } | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| }); | |
| } | |
| } else { | |
| loggerErr.error(state.sequence+' can not add to q money < menu cmd 03'); | |
| txService.setPropertyMap(state.sequence, | |
| _.extend(getResultFailure(state.typeMenu, '34'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| groupMemberId: currentState.groupMemberId | |
| }), function(err) { | |
| dbService.insertTx(state.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction insertTx err : '+errInsertTx); | |
| } | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| }); | |
| } | |
| }); | |
| } | |
| function processUpdateTransaction(results, state) { | |
| blockPhone.wait(results, function(errWait){ | |
| if (errWait) { | |
| loggerErr.error('processTransaction cmd 03 wait err : '+errWait); | |
| } | |
| if((parseInt(command.menu, 10)+parseInt(command.fees, 10)) <= (parseInt(command.money, 10)+parseInt(results.userBalance, 10))){ | |
| if ((command.menu != '0') || (results.typeMenu == '04')) { | |
| if (state.machineType == '1' || currentState.typeMenu == '04') { | |
| service.queueTask( | |
| results.patternId, | |
| results.typeMenu, | |
| results.provider, | |
| results.phoneNumber, | |
| command.menu, | |
| command.sequence, | |
| results.machineId, | |
| results.typeTrueTopup, | |
| async.apply(updateRetryTransaction, state, results) | |
| ); | |
| txLog.info(util.format("[queueTask-retry-request:03] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| results.patternId, results.typeMenu, results.provider, results.phoneNumber, command.menu, command.sequence)); | |
| } else { | |
| txService.setPropertyMap(command.sequence,getResultSuccess(results.typeMenu), function(er) { | |
| dbService.insertTx(command.sequence, function(){ | |
| }); | |
| }); | |
| txLog.info(util.format("[test:queueTask-retry-request:03] ptId:%s, menu:%s, provider:%s, phoneNumber:%s, amount:%s, sequence:%s", | |
| results.patternId, results.typeMenu, results.provider, results.phoneNumber, command.menu, command.sequence)); | |
| } | |
| } else { | |
| loggerErr.error(results.sequence+' can not add to q menu = 0'); | |
| txService.setPropertyMap(results.sequence, | |
| _.extend(getResultFailure(results.typeMenu, '35'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: results.isCharge | |
| }), function(err) { | |
| dbService.insertTx(results.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction menu = 0 err : '+errInsertTx); | |
| } | |
| }); | |
| }); | |
| } | |
| } else { | |
| loggerErr.error(results.sequence+' can not add to q money < menu cmd 03 processUpdateTransaction'); | |
| txService.setPropertyMap(results.sequence, | |
| _.extend(getResultFailure(results.typeMenu, '34'), { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: results.isCharge | |
| }), function(err) { | |
| dbService.insertTx(results.sequence, function(errInsertTx){ | |
| if (errInsertTx) { | |
| loggerErr.error('processTransaction insertTx err : '+errInsertTx); | |
| } | |
| }); | |
| }); | |
| } | |
| }); | |
| } | |
| function updateTransaction(state, currentState, err) { | |
| if (err) { return callback(err) }; | |
| txService.setPropertyMap(state.sequence, { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| //accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| //groupMemberId: currentState.groupMemberId | |
| }, function(err) { | |
| return callback(null, { | |
| code: '03', | |
| sequence: command.sequence | |
| }); | |
| }); | |
| } | |
| function updateRetryTransaction(state, currentState, err) { | |
| if (err) { return callback(err) }; | |
| txService.setPropertyMap(currentState.sequence, { | |
| menu: command.menu, | |
| money: command.money, | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| fees: command.fees, | |
| isCharge: state.isCharge | |
| }, function(err) { | |
| }); | |
| } | |
| } | |
| function process04(command, callback) { | |
| if(command.categoryMoney != null){ | |
| var map = {} | |
| var aCategorys = command.categoryMoney.split( "," ); | |
| map['sequence'] = command.sequence; | |
| _.each(aCategorys, function(aCategory) { | |
| var aTmps = aCategory.split( "-" ); | |
| map[aTmps[0]] = aTmps[1]; | |
| }); | |
| dbService.Ins04(map, function(err){ | |
| if (err) { | |
| logger.info('process04 err :'+err); | |
| } | |
| return callback(null, { | |
| code: command.code, | |
| sequence: command.sequence | |
| }); | |
| }); | |
| } | |
| } | |
| function processCancel(command, callback) { | |
| //08 or 09 | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| //logger.info('processCancel checkState : '+state); | |
| if (err) { | |
| loggerErr.error('processCancel checkState err : '+err); | |
| return callback(true, | |
| _.extend(getResultFailure(state.typeMenu, '31'), { | |
| code: command.code, | |
| sequence: command.sequence | |
| })); | |
| }; | |
| if (command.sequence) { | |
| if (state.id == '01') { | |
| if (state.sequence === command.sequence) { | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| statePool.setProperty( | |
| command.machineId, | |
| 'id', | |
| '00', | |
| async.apply(validateTx, state) | |
| ); | |
| } else { | |
| var validateTxFunc = async.apply( | |
| validateTx, | |
| state | |
| ); | |
| validateTxFunc(null); | |
| } | |
| } else { | |
| loggerErr.error('cmd : '+command.code+' '+command.sequence+' ignore because state id = '+state.id); | |
| } | |
| return callback(null, { | |
| code: command.code, | |
| codeCmd:command.step, | |
| sequence: command.sequence ? command.sequence : spayUtil.generateSeq(command.machineId) | |
| }); | |
| } else { | |
| loggerErr.error(' cmd : '+command.code+' can\'t process sequnece = '+command.sequence); | |
| return callback(null, { | |
| code: command.code, | |
| codeCmd:'1', | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }); | |
| } | |
| } | |
| function validateTx(state, err){ | |
| logger.info('validateTx > ', command.sequence); | |
| txService.getPropertyAll(command.sequence, function(errGetState, objTx){ | |
| if (errGetState) { | |
| logger.info('processCancel validateTx errGetState : '+errGetState); | |
| } else { | |
| logger.info('validateTx objTx > ',objTx); | |
| if (objTx && objTx.cmd) { | |
| loggerErr.error('cmd : '+command.code+' '+command.sequence+' ignore because have cmd = '+objTx.cmd); | |
| } else { | |
| if (objTx != null && (typeof objTx != 'undefined') && objTx != '') { | |
| txService.setProperty( | |
| command.sequence, 'cmd', command.code, | |
| async.apply(insertTransactionCancel, state, objTx)); | |
| } else { | |
| getCurrentTxFromDb(state); | |
| } | |
| } | |
| } | |
| }); | |
| } | |
| function getCurrentTxFromDb(state) { | |
| dbService.getTx(command.sequence, function(err, results){ | |
| loggerErr.error('cmd : '+command.code+' '+command.sequence+' getCurrentTxFromDb = ',results); | |
| if (results) { | |
| if (results.resultStatus == '30') { | |
| txService.setPropertyMap(command.sequence, | |
| results | |
| , function(){ | |
| dbService.deleteTx(command.sequence, function() { | |
| insertTransactionCancel(state, results); | |
| }); | |
| }); | |
| } else { | |
| loggerErr.error(command.sequence+' inoperative cmd '+command.code+' state seq : '+ | |
| command.sequence+' because task is done'); | |
| } | |
| } else { | |
| loggerErr.error(command.sequence+' inoperative cmd '+command.code); | |
| } | |
| }); | |
| } | |
| function insertTransactionCancel(state, currentState, err) { | |
| if (err) { | |
| loggerErr.error('tx sequence '+command.sequence+ | |
| ' processCancel insertTransactionCancel err : '+err); | |
| } | |
| txService.setPropertyMap(command.sequence, { | |
| menu : command.menu, | |
| fees: command.fees, | |
| money: command.money, | |
| dataUpdate: spayUtil.formatDateTime(new Date()), | |
| resultStatus: command.code+command.step, | |
| resultDisplayCode: command.code == '08'? 'A' : 'E', | |
| resultStatusDetails: getResultStatus(command.code+command.step), | |
| resultDisplay: getResultCancel(command.code), | |
| expense: parseInt(command.fees, 10) + parseInt(command.menu, 10), | |
| //accountId: currentState.accountId, | |
| fees: command.fees, | |
| patternId: currentState.patternId, | |
| //groupMemberId: currentState.groupMemberId, | |
| cmd:command.code | |
| }, function(){ | |
| dbService.insertTx(command.sequence, function(errInsertTx){ | |
| if(errInsertTx){ | |
| loggerErr.error('sequence '+command.sequence+ | |
| ' processCancel insertTransactionCancel insertTx err : '+errInsertTx); | |
| } | |
| }); | |
| } | |
| ); | |
| } | |
| } | |
| function process10(command, callback) { | |
| var now = new Date(); | |
| var seq = spayUtil.generateSeqMachine(command.machineId, '99'); | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| txService.setPropertyMap(seq,{ | |
| machineId: command.machineId, | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| resultStatus: '99', | |
| resultDisplayCode:'10', | |
| resultDisplay:AA_STATUS, | |
| sequence: seq, | |
| groupMemberId:state.groupMemberId, | |
| accountId: state.accountId, | |
| groupId: state.groupId, | |
| patternId: state.patternId, | |
| cmd:command.code | |
| }, async.apply(updateTransaction10)); | |
| return callback(null, { | |
| code: '10', | |
| result: '1' | |
| }); | |
| } | |
| function updateTransaction10() { | |
| dbService.InsTxFromMachine(seq, function(errUpdate10){ | |
| if (errUpdate10) { | |
| loggerErr.error('updateTransaction10 err : ',errUpdate10); | |
| } | |
| }) | |
| } | |
| } | |
| function process14(command, callback) { | |
| var seq = spayUtil.generateSeq(command.machineId); | |
| var now = new Date(); | |
| // 0 Old door | |
| // 1 Top door | |
| // 2 Lower door | |
| // 3 Shock sensor | |
| // 4 Fire | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| var display = ''; | |
| var status = '00'; | |
| var details ; | |
| if (command.action == 0) { | |
| switch(command.sensorStatus){ | |
| case '0': status = '84'; | |
| display = 'ปิดประตู'; | |
| break; | |
| case '1': status = '94'; | |
| display = 'ปิดประตูบน'; | |
| break; | |
| case '2': status = '96'; | |
| display = 'ปิดประตูล่าง'; | |
| break; | |
| case '3': status = '88'; | |
| display = 'Shock sensor ปิด'; | |
| break; | |
| case '4': status = '86'; | |
| display = 'ไฟฟ้าดับ'; | |
| break; | |
| } | |
| } else if(command.action == 1) { | |
| switch(command.sensorStatus){ | |
| case '0': status = '85'; | |
| display = 'เปิดประตู'; | |
| break; | |
| case '1': status = '95'; | |
| display = 'เปิดประตูบน'; | |
| break; | |
| case '2': status = '97'; | |
| display = 'เปิดประตูล่าง'; | |
| break; | |
| case '3': status = '89'; | |
| display = 'Shock sensor เปิด'; | |
| break; | |
| case '4': status = '87'; | |
| display = 'ไฟฟ้าปกติ'; | |
| break; | |
| case '5': status = '98'; | |
| display = 'HW-Boot'; | |
| break; | |
| } | |
| } | |
| seq = seq + status; | |
| updateTransaction14(state, display, status, command.action); | |
| return callback(null, { | |
| code: '14', | |
| result: '1' | |
| }); | |
| } | |
| function getSensorInfo(sensor) { | |
| switch(sensor){ | |
| case '0': | |
| return 'Door sensor' | |
| break; | |
| case '1': | |
| return 'Top door sensor' | |
| break; | |
| case '2': | |
| return 'Bottom door sensor' | |
| break; | |
| case '3': | |
| return 'Shock sensor' | |
| break; | |
| case '4': | |
| return 'Electric sensor' | |
| break; | |
| } | |
| } | |
| function updateTransaction14(state, display, status, sensor) { | |
| txService.setPropertyMap(seq,{ | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| machineId: command.machineId, | |
| resultStatus: status, | |
| resultDisplayCode:status, | |
| resultDisplay:display, | |
| resultStatusDetails: getSensorInfo(sensor), | |
| sequence: seq, | |
| groupMemberId:state.groupMemberId, | |
| accountId: state.accountId, | |
| groupId: state.groupId, | |
| patternId: state.patternId, | |
| cmd : command.code, | |
| }, function(){ | |
| dbService.InsTxFromMachine(seq, function(errUpdate14){ | |
| if (errUpdate14) { | |
| loggerErr.error('updateTransaction14 err : ',errUpdate14); | |
| } | |
| }) | |
| }); | |
| } | |
| } | |
| function getMachine(machineId, callback) { | |
| dbService.getMachineDetails(machineId, function(err, results){ | |
| if (err) { | |
| return callback(err); | |
| } else { | |
| if (results.activeStatus == 0 || results.activeCritical == 0) { | |
| results['activeStatus'] = 0; | |
| } | |
| return callback(null, results); | |
| } | |
| }); | |
| } | |
| function getMachineStatus(mc) { | |
| if (mc.activeStatus == 0 || mc.activeCritical == 0) { | |
| return 0; | |
| } else { | |
| return 1; | |
| } | |
| } | |
| function getAccount(groupId, callback) { | |
| dbService.getAccountDetails(groupId, function(err, results){ | |
| if (err) { | |
| return callback(err); | |
| } else { | |
| return callback(null, results); | |
| } | |
| }); | |
| } | |
| function process20(command, callback) { | |
| var seq = spayUtil.generateSeq(command.machineId); | |
| if (command.sequence) { | |
| seq = command.sequence; | |
| } | |
| seq = seq + command.code; | |
| var now = new Date(); | |
| txService.setPropertyMap(seq,{ | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| machineId: command.machineId, | |
| resultDisplayCode:command.code, | |
| resultDisplay:'Clear Inbox', | |
| resultStatus: command.code, | |
| sequence: seq, | |
| cmd : command.code | |
| }, function(){ | |
| dbService.InsTxFromMachine(seq, function(){ | |
| return callback(null, { | |
| code: command.code, | |
| result: '1', | |
| sequence: command.sequence ? command.sequence : seq | |
| }); | |
| }) | |
| }); | |
| } | |
| function process21(command, callback) { | |
| var seq = spayUtil.generateSeq(command.machineId); | |
| if (command.sequence) { | |
| seq = command.sequence; | |
| } | |
| seq = seq + command.code; | |
| var now = new Date(); | |
| if(command.categoryMoney != null){ | |
| var map = {} | |
| var aCategorys = command.categoryMoney.split( "," ); | |
| _.each(aCategorys, function(aCategory) { | |
| var aTmps = aCategory.split( "-" ); | |
| map[aTmps[0]] = aTmps[1]; | |
| }); | |
| txService.setPropertyMap(seq, | |
| map | |
| , function(){ | |
| txService.setPropertyMap(seq,{ | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| machineId: command.machineId, | |
| inbox: command.money, | |
| resultStatus: command.code, | |
| resultDisplayCode:command.code, | |
| resultDisplay:'Receive Inbox', | |
| sequence: seq, | |
| cmd : command.code | |
| }, function(){ | |
| dbService.InsTxFromMachine(seq, function(){ | |
| return callback(null, { | |
| code: command.code, | |
| result: '1', | |
| sequence: command.sequence ? command.sequence : seq | |
| }); | |
| }); | |
| }); | |
| }); | |
| } else { | |
| txService.setPropertyMap(seq,{ | |
| machineId: command.machineId, | |
| resultStatus: command.code, | |
| resultDisplayCode:command.code, | |
| resultDisplay:'Receive Inbox', | |
| sequence: seq, | |
| cmd : command.code | |
| }, function(){ | |
| dbService.InsTxFromMachine(seq, function(){ | |
| return callback(null, { | |
| code: command.code, | |
| result: '1', | |
| sequence: command.sequence ? command.sequence : seq | |
| }); | |
| }); | |
| }); | |
| } | |
| } | |
| function process23(command, callback) { | |
| async.waterfall([ | |
| function(callback){ | |
| var startMonth = moment().startOf('month').format(DATE_FORMAT); | |
| var EndMonth = moment().format(DATE_FORMAT); | |
| var EndSendMonth = moment().format(DATE_SEND_FORMAT); | |
| callback(null, startMonth, EndMonth, EndSendMonth); | |
| }, | |
| function(startMonth, EndMonth, EndSendMonth, callback){ | |
| var BackMonth = moment().subtract('months', 1); | |
| var startBackMonth = BackMonth.startOf('month').format(DATE_FORMAT); | |
| var EndBackMonth = BackMonth.endOf('month').format(DATE_FORMAT); | |
| var EndSendBackMonth = BackMonth.endOf('month').format(DATE_SEND_FORMAT); | |
| var arr = []; | |
| arr.push({start: startMonth, end: EndMonth, endSend: EndSendMonth , spec: 1}); | |
| arr.push({start: startBackMonth, end: EndBackMonth, endSend: EndSendBackMonth , spec: 2}); | |
| callback(null, arr); | |
| } | |
| ], function (err, results) { | |
| var arrs = []; | |
| async.forEach(results, function(list, callback) { | |
| var dtData = '01'+list.endSend; | |
| dbService.getReportCmd23(list.start, list.end, command.machineId, function(err, results) { | |
| if (results && results.rows[0]) { | |
| var datas = results.rows; | |
| var startData = ''; | |
| async.forEachSeries(datas, function(data, callback){ | |
| if (startData !== '') { | |
| startData += ';'; | |
| } | |
| startData += util.format('%s%s(%s,%s,%s)', | |
| data.type_menu, | |
| data.provider, | |
| data.dividend, | |
| data.fees, | |
| data.cutcredit); | |
| callback(); | |
| }, function(err){ | |
| arrs.push({ | |
| spec: list.spec, | |
| date: dtData, | |
| data: startData | |
| }); | |
| }); | |
| } else { | |
| arrs.push({ | |
| spec: list.spec, | |
| date: dtData, | |
| data: '' | |
| }); | |
| } | |
| return callback(null); | |
| }); | |
| }, async.apply(response, arrs)); | |
| }); | |
| function response(arrs) { | |
| console.log('response > ',arrs); | |
| _.each(arrs, function(arr) { | |
| return callback(null, { | |
| code: command.code, | |
| spec: arr.spec, | |
| date: arr.date, | |
| data: arr.data | |
| }); | |
| }); | |
| } | |
| } | |
| function process24(command, callback) { | |
| async.waterfall([ | |
| function(callback){ | |
| var startMonth = moment().startOf('month').format(DATE_FORMAT); | |
| var EndMonth = moment().format(DATE_FORMAT); | |
| var EndSendMonth = moment().format(DATE_SEND_FORMAT); | |
| callback(null, startMonth, EndMonth,EndSendMonth); | |
| }, | |
| function(startMonth, EndMonth, EndSendMonth, callback){ | |
| var BackMonth = moment().subtract('months', 1); | |
| var startBackMonth = BackMonth.startOf('month').format(DATE_FORMAT); | |
| var EndBackMonth = BackMonth.endOf('month').format(DATE_FORMAT); | |
| var EndSendBackMonth = BackMonth.endOf('month').format(DATE_SEND_FORMAT); | |
| var arr = []; | |
| arr.push({start: startMonth, end: EndMonth, endSend: EndSendMonth , spec: 1}); | |
| arr.push({start: startBackMonth, end: EndBackMonth, endSend: EndSendBackMonth , spec: 2}); | |
| callback(null, arr); | |
| } | |
| ], function (err, results) { | |
| var arrs = []; | |
| async.forEach(results, function(list, callback) { | |
| var dtData = '01'+list.endSend; | |
| dbService.getReportCmd24(list.start, list.end, command.machineId, function(err, results) { | |
| if (results && results.rows[0]) { | |
| var datas = results.rows; | |
| var startData = ''; | |
| async.forEachSeries(datas, function(data, callback){ | |
| if (startData !== '') { | |
| startData += ';'; | |
| } | |
| startData += util.format('%s%s(%s)', | |
| data.type_menu, | |
| data.provider, | |
| data.total); | |
| callback(); | |
| }, function(err){ | |
| arrs.push({ | |
| spec: list.spec, | |
| date: dtData, | |
| data: startData | |
| }); | |
| }); | |
| } else { | |
| arrs.push({ | |
| spec: list.spec, | |
| date: dtData, | |
| data: '' | |
| }); | |
| } | |
| return callback(null); | |
| }); | |
| }, async.apply(response, arrs)); | |
| }); | |
| function response(arrs) { | |
| console.log('response > ',arrs); | |
| _.each(arrs, function(arr) { | |
| return callback(null, { | |
| code: command.code, | |
| spec: arr.spec, | |
| date: arr.date, | |
| data: arr.data | |
| }); | |
| }); | |
| } | |
| } | |
| function process30(command, callback) { | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| console.log("--- CHECK STATE ---- \n") | |
| if (state && state.waitForValidate) { | |
| loggerErr.error('process01 for waitForValidate phoneNumber '+command.phoneNumber); | |
| } | |
| if (state && state.id === '01') { | |
| loggerErr.error('state.id = 01 '); | |
| if ((command.command === state.command) && state.sequence) { | |
| loggerErr.error('command equals in state > ',command.command); | |
| prepareTransaction(null); | |
| } else { | |
| setTx(err, command, state); | |
| } | |
| } else { | |
| setTx(err, command, state); | |
| } | |
| }//end checkState | |
| function setTx(err, command, state) { | |
| var request = require('request'); | |
| console.log("--- SET TX---- \n") | |
| var seq = spayUtil.generateSeq(command.machineId); | |
| var now = new Date(); | |
| var baseCashcardUrl = "http://cashcard.opendream.co.th"; | |
| var formObj = { form: { cardId: command.phoneNumber } }; | |
| var smsNumber | |
| request.post( baseCashcardUrl + '/machine/getMobileNumber', formObj, function postCallback(e, r, memberMobileNumber) { | |
| if (e) { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '51'), { | |
| code: '30', | |
| sequence: seq | |
| })); | |
| } | |
| else { | |
| if (!memberMobileNumber || memberMobileNumber.length != 10) { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '50'), { | |
| code: '30', | |
| sequence: seq | |
| })); | |
| } | |
| else { | |
| txService.setPropertyNewMap(seq, { | |
| machineId: command.machineId, | |
| phoneNumber: command.phoneNumber, | |
| providerName:providerMap[command.typeMenu+command.provider], | |
| typeMenu: command.typeMenu, | |
| provider: command.provider, | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| sequence: seq, | |
| dataUpdate: spayUtil.formatDateTime(now), | |
| resultDisplayCode: '', | |
| resultDisplay:WAIT_STATUS, | |
| groupMemberId:state.groupMemberId, | |
| accountId: state.accountId, | |
| groupId: state.groupId, | |
| patternId: state.patternId, | |
| stateId: state ? state.id : null, | |
| smsNumber: memberMobileNumber | |
| }, function(errtxService){ | |
| if(err || errtxService){ | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '10'), { | |
| code: '30', | |
| sequence: seq | |
| })); | |
| } else { | |
| if (state.id === '00') { | |
| // generate seq for keep transaction | |
| statePool.setPropertyMap(command.machineId, { | |
| 'id': '01', | |
| 'typeMenu' : command.typeMenu, | |
| 'sequence': seq, | |
| 'command' : command.command | |
| }, prepareTransaction); | |
| } else if (state.id === '01') { | |
| // generate seq for keep transaction | |
| loggerErr.error(command.command+' command not equals in state ',state.command); | |
| var tmpSeq = state.sequence; | |
| loggerErr.error('gen new seq > ',seq); | |
| statePool.setPropertyMap(command.machineId, { | |
| 'typeMenu' : command.typeMenu, | |
| 'sequence': seq, | |
| 'command' : command.command | |
| }, prepareTransaction); | |
| txService.setPropertyMap(tmpSeq, | |
| getResultNotWork(command.typeMenu, '30'), | |
| function(){ | |
| dbService.insertTxError(tmpSeq, function(errInTxError){ | |
| if (errInTxError) { | |
| loggerErr.error(command.phoneNumber+' state is 01 insertTxError err : '+errInTxError); | |
| } | |
| }); | |
| }); | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '31'), { | |
| code: '30', | |
| sequence: seq | |
| })); | |
| } | |
| } | |
| }); | |
| } | |
| } | |
| }) | |
| } | |
| function prepareTransaction(err) { | |
| console.log("--- PREPARE TX---- \n") | |
| statePool.get(command.machineId, function(errState, state){ | |
| if (err) { | |
| loggerErr.error('machineId '+command.machineId+'process01 prepareTransaction err = '+err); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else if (command.typeMenu != '04' && (command.phoneNumber === null || command.phoneNumber === '')) { | |
| loggerErr.error('machineId '+command.machineId+' process01 prepareTransaction phoneNumber empty'); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '19'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| getMachine(command.machineId, function(errMachine, objMachine) { | |
| if (errMachine) { | |
| loggerErr.error('machineId '+command.machineId+'process01 getMachine err = '+errMachine); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| if (objMachine.groupId == '-1') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '12'), { | |
| code: '30', | |
| groupId : '-1', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| getAccount(objMachine.groupId, function(errAccount, objAccount) { | |
| if (errAccount) { | |
| loggerErr.error('machineId '+command.machineId+'process01 getAccount err = '+errAccount); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| statePool.setProperty(command.machineId,'isCharge',objAccount.isCharge); | |
| txService.setPropertyMap(state.sequence, { | |
| groupId : objMachine.groupId, | |
| accountId: objAccount.accountId, | |
| patternId: objMachine.patternId, | |
| inbox: objMachine.inbox, | |
| accountBalance: objAccount.credit, | |
| groupMemberId: objAccount.groupMemberId, | |
| userLevel: objAccount.userLevel, | |
| isCharge : objAccount.isCharge, | |
| machineType: objMachine.machineType, | |
| typeTrueTopup: objMachine.typeTrueTopup | |
| }, function(errTxService){ | |
| if (errTxService) { | |
| loggerErr.error('prepareTransaction setPropertyMap err : '+errTxService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| if (objMachine.activeStatus == '0') { | |
| //เครื่องปิดบริการ | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '11'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else if (objAccount.activeStatus == '0') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '13'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else if (objAccount.credit < 10){ | |
| dbService.updateMachineActiveStatus(0, command.machineId, function(){ | |
| }); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '18'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| var getMenuFunc = async.apply( | |
| getMenu, | |
| state, | |
| objMachine, | |
| objAccount | |
| ); | |
| getMenuFunc(null); | |
| } | |
| } | |
| }); | |
| }// end if | |
| });//end getAccount | |
| }//end if | |
| } | |
| }); | |
| }//end if | |
| }); | |
| } | |
| function getMenu(state, objMachine , objAccount) { | |
| console.log("--- GET MENU ---- \n") | |
| blockService.getProperty( | |
| command.typeMenu+command.provider, 'statusUrgent', | |
| function(errBlockService, status){ | |
| if (errBlockService) { | |
| loggerErr.error('getMenu blockService getProperty err : '+errBlockService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else if(status == '0') { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '32'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| dbService.getMenu({ | |
| patternId: objMachine.patternId, | |
| typeMenu: command.typeMenu, | |
| provider: command.provider | |
| }, async.apply(getOrCreatePhoneNumberBalance, state, objMachine , objAccount)); | |
| } | |
| } | |
| ); | |
| } | |
| function getOrCreatePhoneNumberBalance(state, objMachine , objAccount, err, menuResult) { | |
| console.log("GET OR CREATE PHONE") | |
| if (err) { | |
| statePool.setProperty(command.machineId,'id','00', | |
| function(){ | |
| statePool.delProperty(command.machineId,'sequence', | |
| function(){ | |
| }); | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| } | |
| ); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '16'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| logger.debug('getMenu '); | |
| if (menuResult) { | |
| //logger.debug(JSON.stringify(menuResult)); | |
| dbService.getCustomerBalance({ | |
| groupId: objMachine.groupId, | |
| groupMemberId: objAccount.groupMemberId, | |
| phoneNumber: command.phoneNumber | |
| }, function(errGetBalance, objCustomer) { | |
| var answer01Func = async.apply( | |
| answer01, | |
| state, | |
| objCustomer, | |
| menuResult, | |
| objAccount | |
| ); | |
| if (errGetBalance) { | |
| if (objCustomer) { | |
| var now = new Date(); | |
| var expire = now; | |
| //var expire = now + '60 day'; | |
| var providerFromCmd = command.provider; | |
| if (command.typeMenu == '04') { | |
| providerFromCmd = '' | |
| } | |
| dbService.createCustomerBalance({ | |
| phoneNumber: command.phoneNumber, | |
| dataUpdate: now, | |
| expDate: now, | |
| provider: providerFromCmd, | |
| groupMemberId: objAccount.groupMemberId, | |
| groupId: objMachine.groupId | |
| }, answer01Func) | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } | |
| } else { | |
| answer01Func(null); | |
| } | |
| } | |
| ); | |
| } else { | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '16'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } | |
| } | |
| function answer01(state, objCustomer, menuResult, objAccount, err) { | |
| if (err) { | |
| loggerErr.error('function answer01 error : '+err); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| var now = new Date(); | |
| var userBalance = 0 ; | |
| if (objCustomer.userBalance >= 0) { | |
| userBalance = objCustomer.userBalance; | |
| if(objCustomer.expDate < now){ | |
| userBalance = 0; | |
| } | |
| } | |
| txService.setPropertyMap(state.sequence, { | |
| menuId: menuResult.menuId, | |
| userBalance: userBalance ? userBalance : 0, | |
| dataUpdate: spayUtil.formatDateTime(now), | |
| resultDisplayCode: '', | |
| percent: menuResult.dividend | |
| }, | |
| function(errTxService) { | |
| if (errTxService) { | |
| loggerErr.error('errTxService error : '+errTxService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| blockService.getProperty(command.typeMenu+command.provider, 'statusNormal', function(errBlockService, activeStatus){ | |
| if (errBlockService) { | |
| loggerErr.error('errBlockService : '+errBlockService); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '00'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| blockPhone.lock(command, | |
| function(errLock, res){ | |
| if (errLock || !res) { | |
| statePool.setProperty(command.machineId,'id','00', | |
| function(){ | |
| statePool.delProperty(command.machineId,'sequence', | |
| function(){ | |
| }); | |
| statePool.delProperty(command.machineId,'command', | |
| function(){ | |
| }); | |
| }); | |
| return callback(true, | |
| _.extend(getResultNotWork(command.typeMenu, '17'), { | |
| code: '30', | |
| sequence: state.sequence | |
| })); | |
| } else { | |
| return callback(null,{ | |
| code: '30', | |
| menuAll: menuResult.menuAll, | |
| typeMenu: command.typeMenu, | |
| provider: command.provider, | |
| userBalance: userBalance ? userBalance : 0, | |
| accountBalance: objAccount.credit, | |
| sequence: state.sequence, | |
| activeStatus: activeStatus | |
| }); | |
| } | |
| });//blockPhone lock | |
| } | |
| }); | |
| }//end if | |
| }); | |
| } | |
| }//end function answer01 | |
| } | |
| } | |
| function process40(command, callback) { | |
| dbService.getAccountPermission(command, function(err, results){ | |
| var result = '1'; | |
| if (err) { | |
| result = '0'; | |
| } | |
| return callback(null, { | |
| code: command.code, | |
| result: result, | |
| accountCredit: results.credit, | |
| balanceInbox: results.inbox | |
| }); | |
| }); | |
| } | |
| function process41(command, callback) { | |
| if (command.newPassword == null || command.newPassword == '') { | |
| return callback(null, { | |
| code: command.code, | |
| result: 2 | |
| }); | |
| } else { | |
| dbService.changePasswordAccount(command.machineId, command.oldPassword, command.newPassword, | |
| function(err, results){ | |
| if (err) { | |
| return callback(null, { | |
| code: command.code, | |
| result: 2 | |
| }); | |
| } else { | |
| return callback(null, { | |
| code: command.code, | |
| result: results | |
| }); | |
| } | |
| }); | |
| } | |
| } | |
| function process90(command, callback) { | |
| var now = new Date(); | |
| var seq = spayUtil.generateSeqMachine(command.machineId, command.status); | |
| statePool.get(command.machineId, checkState); | |
| function checkState(err, state) { | |
| var display = ''; | |
| switch(command.status){ | |
| case '001': | |
| display = 'RESET AIRCARD'; | |
| break; | |
| case '002': | |
| display = 'RESET MACHINE'; | |
| break; | |
| case '100': | |
| display = 'FTP_LOGIN_OK'; | |
| break; | |
| case '101': | |
| display = 'FTP_LOGIN_ERROR'; | |
| break; | |
| case '102': | |
| display = 'FTP_LOGIN_CANCEL'; | |
| break; | |
| case '103': | |
| display = 'FTP_FILE_NOT_EXISTS'; | |
| break; | |
| case '104': | |
| display = 'FTP_FILE_SIZE_ERR'; | |
| break; | |
| case '105': | |
| display = 'FTP_SUCCESS'; | |
| break; | |
| case '106': | |
| display = 'FTP_RESET_MACHINE'; | |
| break; | |
| case '107': | |
| display = 'FTP_TIMEOUT'; | |
| break; | |
| } | |
| updateTransaction90(state, display); | |
| } | |
| function updateTransaction90(state, display) { | |
| txService.setPropertyMap(seq, { | |
| machineId: command.machineId, | |
| resultStatus: command.status, | |
| resultDisplayCode:command.status, | |
| resultDisplay:display, | |
| resultStatusDetails: display, | |
| sequence: seq, | |
| reqDateTime: spayUtil.formatDateTime(now), | |
| groupMemberId:state.groupMemberId, | |
| accountId: state.accountId, | |
| groupId: state.groupId, | |
| patternId: state.patternId, | |
| cmd : command.code | |
| },function(err){ | |
| dbService.InsTxFromMachine(seq, function(errUpdate90){ | |
| if (errUpdate90) { | |
| loggerErr.error('updateTransaction90 err : '+errUpdate90); | |
| } | |
| }); | |
| }); | |
| } | |
| } | |
| /* | |
| * รับ command มาจากหน้าเว็บตอนอัพเกรดเครื่อง | |
| */ | |
| function process81(command, callback) { | |
| /* return upgrade status to webapp */ | |
| function sendWebUpgradeResult(status) { | |
| console.log('send web result >', status) | |
| return callback(null, { | |
| code: '81', | |
| machineId: command.machineId, | |
| result: status | |
| }); | |
| } | |
| // get machine socket from state pool | |
| statePool.get(command.machineId, function(err, state) { | |
| if (! state || !state.sid) { | |
| console.log('no socket id : machine id >', command.machineId); | |
| // มาจากเว็บเพื่ออัพเกรดเครื่อง ถ้าเครื่องยังไม่ต่อ | |
| // ตอบกลับไปว่าไม่เจอเครื่อง | |
| return sendWebUpgradeResult(12); | |
| } | |
| statePool.setProperty(command.machineId, 'waitUpgrade', 1, | |
| function(err, res) { | |
| console.log('found machine: set waitUpgrade: res >', res); | |
| if (err) return sendWebUpgradeResult(10); | |
| // send upgrade cmd to machine | |
| var cmd = '11'; | |
| if (command.path) { | |
| cmd = '16'; | |
| } | |
| eventBus.emit('writeThroughSocket', | |
| state.sid, | |
| generator.command({ | |
| code: cmd, | |
| data: command.path, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| false | |
| ); | |
| // ตอบกลับเลยว่าได้แล้ว | |
| sendWebUpgradeResult(11); | |
| // wait 15 sec for machine to response on upgrade request | |
| setTimeout(function() { | |
| console.log('upgrade time out'); | |
| statePool.getProperty(command.machineId, 'waitUpgrade', function(err, stillWait) { | |
| console.log('check waitUpgrade > ', stillWait); | |
| var upgradeStatus = 13; // succcess | |
| if (stillWait == 1) { | |
| upgradeStatus = 10; | |
| } | |
| // if already received response from machine, null for stillWait | |
| if (stillWait != null) { | |
| console.log('new upgrade status >', upgradeStatus) | |
| // publish upgrade status to web | |
| mcService.publishUpgradeStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': upgradeStatus | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| }); | |
| }, 15000); | |
| } | |
| ); | |
| }); | |
| } | |
| /* | |
| * command ตอบกลับจากเครื่องหลังจากเว็บส่ง command 81 ผ่าน server | |
| */ | |
| function process11(command, callback) { | |
| if (command.result == 1) { | |
| statePool.delProperty(command.machineId, 'waitUpgrade', | |
| function(err, res) { | |
| console.log('del waitUpgrade property: res', res); | |
| if (err) { | |
| return callback(true); | |
| } | |
| // publish upgrade status to web | |
| mcService.publishUpgradeStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': 13 | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| ); | |
| } | |
| } | |
| /* | |
| * command ตอบกลับจากเครื่องหลังจากเว็บส่ง command 81 โดยมี source และ destination ผ่าน server | |
| */ | |
| function process16(command, callback) { | |
| if (command.result == 1) { | |
| statePool.delProperty(command.machineId, 'waitUpgrade', | |
| function(err, res) { | |
| console.log('del waitUpgrade property: res', res); | |
| if (err) { | |
| return callback(true); | |
| } | |
| // publish upgrade status to web | |
| mcService.publishUpgradeStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': 13 | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| ); | |
| } | |
| } | |
| /* | |
| * รับ command มาจากหน้าเว็บตอนสั่งเปิด/ปิด alarm | |
| */ | |
| function process82(command, callback) { | |
| /* return upgrade status to webapp */ | |
| function sendWebAlarmResult(status) { | |
| console.log('send web result >', status) | |
| return callback(null, { | |
| code: '82', | |
| machineId: command.machineId, | |
| result: status | |
| }); | |
| } | |
| // get machine socket from state pool | |
| statePool.get(command.machineId, function(err, state) { | |
| if (! state || !state.sid) { | |
| console.log('no socket id : machine id >', command.machineId); | |
| // มาจากเว็บเพื่ออัพเกรดเครื่อง ถ้าเครื่องยังไม่ต่อ | |
| // ตอบกลับไปว่าไม่เจอเครื่อง | |
| return sendWebAlarmResult(22); | |
| } | |
| statePool.setProperty(command.machineId, 'waitAlarm', 1, | |
| function(err, res) { | |
| console.log('found machine: set waitAlarm: res >', res); | |
| if (err) return sendWebAlarmResult(20); | |
| // send upgrade cmd to machine | |
| eventBus.emit('writeThroughSocket', | |
| state.sid, | |
| generator.command({ | |
| code: '12', | |
| action: command.action, | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| false | |
| ); | |
| // ตอบกลับเลยว่าได้แล้ว | |
| sendWebAlarmResult(21); | |
| // wait 15 sec for machine to response on upgrade request | |
| setTimeout(function() { | |
| console.log('wait alarm response time out'); | |
| statePool.getProperty(command.machineId, 'waitAlarm', function(err, stillWait) { | |
| console.log('check waitAlarm > ', stillWait); | |
| var status = 23; // succcess | |
| if (stillWait == 1) { | |
| status = 20; | |
| } | |
| if (stillWait != null) { | |
| console.log('new alarm status >', status); | |
| // publish upgrade status to web | |
| mcService.publishAlarmStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': status | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| }); | |
| }, 15000); | |
| } | |
| ); | |
| }); | |
| } | |
| /* | |
| * command ตอบกลับจากเครื่องหลังจากเว็บส่ง command 82 ผ่าน server | |
| */ | |
| function process12(command, callback) { | |
| if (command.result == 1) { | |
| statePool.delProperty(command.machineId, 'waitAlarm', | |
| function(err, res) { | |
| console.log('del waitAlarm property: ok >', res); | |
| if (err) { | |
| return callback(true); | |
| } | |
| // publish alarm status to web | |
| mcService.publishAlarmStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': 23 | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| ); | |
| } | |
| } | |
| /* | |
| * รับ command มาจากหน้าเว็บตอน reset เครื่อง | |
| */ | |
| function process85(command, callback) { | |
| function sentWebResetResult(status) { | |
| console.log('send web result >', status) | |
| return callback(null, { | |
| code: '85', | |
| machineId: command.machineId, | |
| result: status | |
| }); | |
| } | |
| // get machine socket from state pool | |
| statePool.get(command.machineId, function(err, state) { | |
| if (! state || !state.sid) { | |
| console.log('no socket id : machine id >', command.machineId); | |
| return sentWebResetResult(52); | |
| } | |
| statePool.setProperty(command.machineId, 'waitReset', 1, | |
| function(err, res) { | |
| console.log('found machine: set waitReset: resp >', res); | |
| if (err) return sentWebResetResult(50); | |
| // send reset cmd to machine | |
| eventBus.emit('writeThroughSocket', | |
| state.sid, | |
| generator.command({ | |
| code: '15', | |
| sequence: spayUtil.generateSeq(command.machineId) | |
| }), | |
| false | |
| ); | |
| // ตอบกลับเลยว่าได้แล้ว | |
| sentWebResetResult(51); | |
| // wait 15 sec for machine to response on reset request | |
| setTimeout(function() { | |
| console.log('reset time out'); | |
| statePool.getProperty(command.machineId, 'waitReset', function(err, stillWait) { | |
| console.log('check waitReset > ', stillWait); | |
| var status = 53; // succcess | |
| if (stillWait == 1) { | |
| status = 50; | |
| } | |
| if (stillWait != null) { | |
| console.log('current reset status >', status) | |
| // publish reset status to web | |
| mcService.publishResetStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': status | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| }); | |
| }, 15000); | |
| } | |
| ); | |
| }); | |
| } | |
| /* | |
| * reply from machine after machine received command %15 | |
| */ | |
| function process15(command, callback) { | |
| if (command.result == 1) { | |
| statePool.delProperty(command.machineId, 'waitReset', | |
| function(err, res) { | |
| console.log('waitReset property deleted'); | |
| if (err) { | |
| return callback(true); | |
| } | |
| // publish reset status ok to web | |
| mcService.publishResetStatus( | |
| command.machineId, { | |
| 'id': command.machineId, | |
| 'status': 53 | |
| }, function(err) { | |
| if (err) console.error(err); | |
| } | |
| ); | |
| } | |
| ); | |
| } | |
| } | |
| var pmap = { | |
| '00': process00, | |
| '01': process01, | |
| '02': process02, | |
| '03': process03, | |
| '04': process04, | |
| '08': processCancel, | |
| '09': processCancel, | |
| '10': process10, | |
| '14': process14, | |
| '20': process20, | |
| '21': process21, | |
| '23': process23, | |
| '24': process24, | |
| '30': process30, | |
| '40': process40, | |
| '41': process41, | |
| '11': process11, | |
| '16': process16, | |
| '12': process12, | |
| '15': process15, | |
| '81': process81, | |
| '82': process82, | |
| '85': process85, | |
| '90': process90 | |
| } | |
| function getResultNotWork(typeMenu, status) { | |
| // console.log('typeMenu >> '+typeMenu); | |
| if (typeMenu) { | |
| var rCode; | |
| var rDisplay; | |
| switch(typeMenu){ | |
| case '01': | |
| rCode = 'C'; | |
| rDisplay = C_STATUS; | |
| break; | |
| case '02': | |
| break; | |
| case '03': | |
| rCode = 'C'; | |
| rDisplay = C_STATUS; | |
| break; | |
| case '04': | |
| rCode = '6'; | |
| rDisplay = 'ไม่สำเร็จ'; | |
| break; | |
| case '06': | |
| rCode = 'C'; | |
| rDisplay = C_STATUS; | |
| break; | |
| } | |
| var details = getResultStatus(status); | |
| return { | |
| resultDisplayCode: rCode, | |
| resultDisplay: rDisplay, | |
| resultStatus: status, | |
| resultStatusDetails: details | |
| } | |
| } else { | |
| return {}; | |
| } | |
| } | |
| function getResultSuccess(typeMenu) { | |
| //console.log('typeMenu >> '+typeMenu); | |
| if (typeMenu) { | |
| var rCode; | |
| var rDisplay; | |
| switch(typeMenu){ | |
| case '01': | |
| rCode = '1'; | |
| rDisplay = config.status.succ; | |
| break; | |
| case '02': | |
| break; | |
| case '03': | |
| rCode = '5'; | |
| rDisplay = config.status.gameSucc; | |
| break; | |
| case '04': | |
| rCode = '4'; | |
| rDisplay = config.status.scale; | |
| break; | |
| case '06': | |
| rCode = '1'; | |
| rDisplay = config.status.succ; | |
| break; | |
| } | |
| return { | |
| resultDisplayCode: rCode, | |
| resultDisplay: rDisplay, | |
| simBalance: -1 | |
| } | |
| } else { | |
| return {}; | |
| } | |
| } | |
| function getResultFailure(typeMenu, status) { | |
| //console.log('typeMenu >> '+typeMenu); | |
| if (typeMenu) { | |
| var rCode; | |
| var rDisplay; | |
| switch(typeMenu){ | |
| case '01': | |
| rCode = '0'; | |
| rDisplay = config.status.fail; | |
| break; | |
| case '02': | |
| break; | |
| case '03': | |
| rCode = '3'; | |
| rDisplay = config.status.gameFail; | |
| break; | |
| case '04': | |
| rCode = '6'; | |
| rDisplay = config.status.scaleFail; | |
| break; | |
| case '06': | |
| rCode = '0'; | |
| rDisplay = config.status.fail; | |
| break; | |
| } | |
| var details = getResultStatus(status); | |
| console.log('results >> ',{ | |
| resultDisplayCode: rCode, | |
| resultDisplay: rDisplay, | |
| resultStatus: status, | |
| resultStatusDetails: details | |
| } ); | |
| return { | |
| resultDisplayCode: rCode, | |
| resultDisplay: rDisplay, | |
| resultStatus: status, | |
| resultStatusDetails: details | |
| } | |
| } else { | |
| return {}; | |
| } | |
| } | |
| function getResultCancel(status) { | |
| if (status) { | |
| var rDisplay; | |
| switch(status){ | |
| case '08': | |
| rDisplay = A_STATUS; | |
| break; | |
| case '09': | |
| rDisplay = E_STATUS; | |
| break; | |
| } | |
| return rDisplay; | |
| } else { | |
| return ''; | |
| } | |
| } | |
| function getResultStatus(status) { | |
| var details; | |
| switch(status){ | |
| case '00': | |
| details = config.status.reqDataError; | |
| break; | |
| case '081': | |
| case '082': | |
| case '083': | |
| case '084': | |
| case '085': | |
| details = config.status.cancelByUser+ ' ('+status+')'; | |
| break; | |
| case '091': | |
| case '092': | |
| case '093': | |
| case '094': | |
| case '095': | |
| details = config.status.cancelByMc+ ' ('+status+')'; | |
| break; | |
| case '10': | |
| details = config.status.dbError; | |
| break; | |
| case '11': | |
| details = config.status.mcNoPermission; | |
| break; | |
| case '12': | |
| details = config.status.mcNoGroup; | |
| break; | |
| case '13': | |
| details = config.status.groupNoPermission; | |
| break; | |
| case '16': | |
| details = config.status.emptyMenu; | |
| break; | |
| case '17': | |
| details = config.status.mobilExist; | |
| break; | |
| case '18': | |
| details = config.status.creditNotEnough; | |
| break; | |
| case '19': | |
| details = config.status.wrongPayer; | |
| break; | |
| case '30': | |
| details = config.status.mcNotResponse; | |
| break; | |
| case '31': | |
| details =config.status.stateError; | |
| break; | |
| case '32': | |
| details =config.status.turnOff; | |
| break; | |
| case '34': | |
| details =config.status.moneyNotEnough; | |
| break; | |
| case '35': | |
| details =config.status.notHaveMenu; | |
| break; | |
| case '50': | |
| details =config.status.invalidMobileNumber; | |
| break; | |
| case '51': | |
| details =config.status.mobileCheckerServiceDown; | |
| break; | |
| } | |
| return details; | |
| } | |
| function updateActiveStatusMachine(actStatus, machineId, callback) { | |
| dbService.updateMachineActiveStatus(actStatus, machineId, function(err, results){ | |
| }); | |
| } | |
| function process(command, callback) { | |
| auditLog.info('process command', { machineId: command.machineId, command: command }); | |
| var p = pmap[command.code]; | |
| if (p) { | |
| if (command.machineId) { | |
| filter(p, command, callback); | |
| } else { | |
| loggerErr.error('process command id = null ',command); | |
| eventBus.emit('writeThroughSocket', | |
| command.sid, | |
| generator.command({ | |
| code: '00', | |
| status: '0', | |
| sequence: spayUtil.generateSeq(command.machineId), | |
| activeStatus: '0' | |
| }), | |
| true, | |
| 'process machine id '+command.machineId | |
| ); | |
| } | |
| } | |
| } | |
| function defaultFilter(chain, command, callback) { | |
| // do nothing | |
| chain(command, callback); | |
| } | |
| var filter = defaultFilter; | |
| exports.setFilter = function(f) { filter = f }; | |
| exports.setTimeout = function(t) { TIMEOUT = t; }; | |
| exports.process = process; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment