Commit 5d7d9426333e9892ed997783fb5aa0221c06c302
1 parent
3e149fe8c6
Exists in
master
reverse-parser
Showing 3 changed files with 126 additions and 36 deletions Inline Diff
package.json
1 | { | 1 | { |
2 | "name": "sate24-to-sc", | 2 | "name": "sate24-to-sc", |
3 | "version": "0.0.4", | 3 | "version": "0.0.4", |
4 | "description": "ST24 to SimpleConnect H2H OUT", | 4 | "description": "ST24 to SimpleConnect H2H OUT", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha" | 7 | "test": "mocha" |
8 | }, | 8 | }, |
9 | "repository": { | 9 | "repository": { |
10 | "type": "git", | 10 | "type": "git", |
11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-sc.git" | 11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-sc.git" |
12 | }, | 12 | }, |
13 | "keywords": [ | 13 | "keywords": [ |
14 | "st24", | 14 | "st24", |
15 | "reload97", | 15 | "reload97", |
16 | "ppob", | 16 | "ppob", |
17 | "h2h", | 17 | "h2h", |
18 | "m2m", | 18 | "m2m", |
19 | "xmlrpc" | 19 | "xmlrpc" |
20 | ], | 20 | ], |
21 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 21 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
22 | "license": "BSD", | 22 | "license": "BSD", |
23 | "dependencies": { | 23 | "dependencies": { |
24 | "fs": "0.0.2", | 24 | "fs": "0.0.2", |
25 | "ini": "~1.3.4", | 25 | "ini": "~1.3.4", |
26 | "iniparser": "~1.0.5", | 26 | "iniparser": "~1.0.5", |
27 | "lru-cache": "^4.0.1", | 27 | "lru-cache": "^4.0.1", |
28 | "mathjs": "~1.7.0", | 28 | "mathjs": "~1.7.0", |
29 | "minimist": "~1.2.0", | 29 | "minimist": "~1.2.0", |
30 | "mocha": "~2.2.5", | 30 | "mocha": "~2.2.5", |
31 | "moment": "^2.13.0", | 31 | "moment": "^2.13.0", |
32 | "mongodb": "^2.1.16", | 32 | "mongodb": "^2.1.16", |
33 | "parse5": "~2.1.4", | 33 | "parse5": "~2.1.4", |
34 | "request": "~2.57.0", | 34 | "request": "~2.57.0", |
35 | "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", | 35 | "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", |
36 | "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git", | 36 | "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git", |
37 | "strftime": "~0.9.2", | 37 | "strftime": "~0.9.2", |
38 | "winston": "~1.0.1", | 38 | "winston": "~1.0.1", |
39 | "xml2js": "~0.4.9", | 39 | "xml2js": "~0.4.9", |
40 | "xmldom": "~0.1.21", | 40 | "xmldom": "~0.1.21", |
41 | "xmlrpc": "~1.3.1", | 41 | "xmlrpc": "~1.3.1", |
42 | "xmlserializer": "~0.3.3", | 42 | "xmlserializer": "~0.3.3", |
43 | "xpath": "0.0.21" | 43 | "xpath": "0.0.21" |
44 | }, | ||
45 | "devDependencies": { | ||
46 | "should": "^11.2.0" | ||
44 | } | 47 | } |
45 | } | 48 | } |
46 | 49 |
reverse-parser.js
File was created | 1 | "use strict"; | |
2 | |||
3 | function parseMessage(msg) { | ||
4 | const splited = msg.split(','); | ||
5 | |||
6 | return { | ||
7 | namapel: _getNamaPel(splited), | ||
8 | tarifdaya: _getTarifDaya(splited), | ||
9 | tarif: _getTarif(splited), | ||
10 | daya: _getDaya(splited), | ||
11 | jumlahkwh: _getJumlahKwh(splited) | ||
12 | } | ||
13 | } | ||
14 | |||
15 | function _getNamaPel(splited) { | ||
16 | try { | ||
17 | return splited[2]; | ||
18 | } | ||
19 | catch(e) { | ||
20 | console.trace('Exception on _getNamaPel:', e); | ||
21 | return; | ||
22 | } | ||
23 | } | ||
24 | |||
25 | function _getTarifDaya(splited) { | ||
26 | try { | ||
27 | let value = splited[5]; | ||
28 | |||
29 | if (value.indexOf('VA') < 0) { | ||
30 | value += 'VA'; | ||
31 | } | ||
32 | |||
33 | return value; | ||
34 | } | ||
35 | catch(e) { | ||
36 | console.trace('Exception on _getTarifDaya:', e); | ||
37 | return; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | function _getJumlahKwh(splited) { | ||
42 | try { | ||
43 | let value = splited[7]; | ||
44 | value = value.replace(/^kWh\s*/, ''); | ||
45 | return value; | ||
46 | } | ||
47 | catch(e) { | ||
48 | console.trace('Exception on _getJumlahKwh:', e); | ||
49 | return; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | function _getTarif(splited) { | ||
54 | const tarifdaya = _getTarifDaya(splited); | ||
55 | |||
56 | try { | ||
57 | return tarifdaya.split('/')[0]; | ||
58 | } | ||
59 | catch(e) { | ||
60 | console.trace('Exception on _getTarif:', e); | ||
61 | return; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | function _getDaya(splited) { | ||
66 | const tarifdaya = _getTarifDaya(splited); | ||
67 | |||
68 | try { | ||
69 | return tarifdaya.split('/')[1]; | ||
70 | } | ||
71 | catch(e) { | ||
72 | console.trace('Exception on _getDaya:', e); | ||
73 | return; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | exports.parseMessage = parseMessage; | ||
78 |
test.js
1 | var assert = require("assert"); | 1 | "use strict"; |
2 | const should = require("should"); | ||
3 | const reverseParser = require('./reverse-parser'); | ||
2 | 4 | ||
5 | describe('#reverse-parser', function() { | ||
6 | describe('#parseMessage', function() { | ||
3 | 7 | ||
4 | describe('aaa', function() { | 8 | describe('test with valid message', function () { |
5 | var aaa = require('sate24/aaa'); | 9 | const msg = 'IDPEL:2017-01-30 16:58:19,1BNS2550C539F5B40545,ERRY-FEBRIANTO,32024560198,32024560198,R1M/900,Rp ,kWh 24.40,Stroom/Token 4260-5779-4249-2925-9618,SUKSES'; |
10 | const data = reverseParser.parseMessage(msg); | ||
6 | 11 | ||
7 | describe("#unaliasResponseCode()", function() { | 12 | it('should to have correct nama pelanggan', function() { |
8 | it('should return 68', function() { | 13 | data.namapel.should.equal('ERRY-FEBRIANTO'); |
9 | assert.equal('68', aaa.unaliasResponseCode('01', '01:68')); | 14 | }) |
10 | }); | ||
11 | 15 | ||
12 | it('should return 68', function() { | 16 | it('should to have correct tarif daya', function() { |
13 | assert.equal('68', aaa.unaliasResponseCode('68', '01:68')); | 17 | data.tarifdaya.should.equal('R1M/900VA'); |
14 | }); | 18 | }) |
15 | 19 | ||
16 | it('should return 00', function() { | 20 | it('should to have correct tarif', function() { |
17 | assert.equal('00', aaa.unaliasResponseCode('00', '01:68')); | 21 | data.tarif.should.equal('R1M'); |
18 | }); | 22 | }) |
19 | 23 | ||
20 | it('should return 40', function() { | 24 | it('should to have correct daya', function() { |
21 | assert.equal('40', aaa.unaliasResponseCode('40', '')); | 25 | data.daya.should.equal('900VA'); |
22 | }); | 26 | }) |
23 | 27 | ||
24 | it('should return 40', function() { | 28 | it('should to have correct jumlah kwh', function() { |
25 | assert.equal('40', aaa.unaliasResponseCode('40', '')); | 29 | data.jumlahkwh.should.equal('24.40'); |
26 | }); | 30 | }) |
31 | }) | ||
27 | 32 | ||
28 | it('should return 40', function() { | 33 | describe('test with invalid message', function () { |
29 | assert.equal('40', aaa.unaliasResponseCode('40')); | 34 | const msg = ''; |
30 | }); | 35 | const data = reverseParser.parseMessage(msg); |
31 | 36 | ||
32 | }); | 37 | it('should to have correct nama pelanggan', function() { |
33 | }); | 38 | should(data.namapel).not.be.ok(); |
39 | }) | ||
34 | 40 | ||
35 | describe('aaa', function() { | 41 | it('should to have correct tarif daya', function() { |
36 | var partner = require('./httppulsakita'); | 42 | should(data.tarifdaya).not.be.ok(); |
43 | }) | ||
37 | 44 | ||
38 | describe("#parseResult()", function() { | 45 | it('should to have correct tarif', function() { |
39 | message = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><respon><tanggal>2015/6/16 15:43:35</tanggal><idagen>P0039</idagen><refid>AEE15B32987941D89FFF4BC7EF676C13</refid><produk>PLN20</produk><tujuan>14204279369</tujuan><rc>0000</rc><data> </data><token> </token><pesan>#14836 PLN20 ke:14204279369 SUKSES. SN:3520-2887-6627-6699-4826/TestDummyPanjang6955555/P1/7000VA/32,4. \ | 46 | should(data.tarif).not.be.ok(); |
40 | Sisa saldo Rp. 5,000,000 - Rp. 18,700 = Rp. 4,981,300</pesan></respon>'; | 47 | }) |
41 | 48 | ||
42 | data = partner.parseResult(message); | 49 | it('should to have correct daya', function() { |
43 | console.log(data); | 50 | should(data.daya).not.be.ok(); |
51 | }) | ||
44 | 52 | ||
45 | it('should return 2015/6/16 15:43:35', function() { | 53 | it('should to have correct jumlah kwh', function() { |
46 | assert.equal('2015/6/16 15:43:35', data.respon.tanggal); | 54 | should(data.jumlahkwh).not.be.ok(); |
47 | }); | 55 | }) |
48 | }); | 56 | }) |
49 | }); | 57 | |
58 | }) | ||
59 | }) |