Commit e74c83d19c55f072b9492529684f89fe98a4eb70
1 parent
95d289a503
Exists in
master
FILTER pdu DELIVER
Showing 1 changed file with 5 additions and 1 deletions Inline Diff
lib/serialport-parsers.js
1 | const PARSER_READLINE_DELIMITER = '\r\n'; | 1 | const PARSER_READLINE_DELIMITER = '\r\n'; |
2 | const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r\n(?:OK|ERROR)\r\n/; | 2 | const PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = /\r\n(?:OK|ERROR)\r\n/; |
3 | 3 | ||
4 | const pdu = require('node-pdu'); | 4 | const pdu = require('node-pdu'); |
5 | const ParserReadline = require('@serialport/parser-readline'); | 5 | const ParserReadline = require('@serialport/parser-readline'); |
6 | const ParserRegex = require('@serialport/parser-regex'); | 6 | const ParserRegex = require('@serialport/parser-regex'); |
7 | 7 | ||
8 | const logger = require('komodo-sdk/logger'); | 8 | const logger = require('komodo-sdk/logger'); |
9 | 9 | ||
10 | const dbCops = require('./db-cops'); | 10 | const dbCops = require('./db-cops'); |
11 | 11 | ||
12 | let port; | 12 | let port; |
13 | 13 | ||
14 | exports.setPort = function setPort(val) { | 14 | exports.setPort = function setPort(val) { |
15 | logger.info('SERIALPORT-PARSERS: setting port'); | 15 | logger.info('SERIALPORT-PARSERS: setting port'); |
16 | port = val; | 16 | port = val; |
17 | }; | 17 | }; |
18 | 18 | ||
19 | exports.getPort = function getPort() { | 19 | exports.getPort = function getPort() { |
20 | return port; | 20 | return port; |
21 | }; | 21 | }; |
22 | 22 | ||
23 | function parsePdu(data) { | 23 | function parsePdu(data) { |
24 | if (!data) return null; | 24 | if (!data) return null; |
25 | 25 | ||
26 | try { | 26 | try { |
27 | const result = pdu.parse(data.toString().trim() || ''); | 27 | const result = pdu.parse(data.toString().trim() || ''); |
28 | return result; | 28 | return result; |
29 | } catch (e) { | 29 | } catch (e) { |
30 | return null; | 30 | return null; |
31 | } | 31 | } |
32 | } | 32 | } |
33 | 33 | ||
34 | function onCSQ(data) { | 34 | function onCSQ(data) { |
35 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); | 35 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); |
36 | if (!val || !val[1]) return null; | 36 | if (!val || !val[1]) return null; |
37 | 37 | ||
38 | logger.info('Signal quality extracted', { signalQuality: val[1] }); | 38 | logger.info('Signal quality extracted', { signalQuality: val[1] }); |
39 | return val[1]; | 39 | return val[1]; |
40 | } | 40 | } |
41 | 41 | ||
42 | function onPdu(data, parsedData) { | 42 | function onPdu(data, parsedData) { |
43 | const from = parsedData.getAddress && parsedData.getAddress().getPhone | 43 | const from = parsedData.getAddress && parsedData.getAddress().getPhone |
44 | ? parsedData.getAddress().getPhone() : null; | 44 | ? parsedData.getAddress().getPhone() : null; |
45 | 45 | ||
46 | const msg = parsedData.getData && parsedData.getData().getData | 46 | const msg = parsedData.getData && parsedData.getData().getData |
47 | ? parsedData.getData().getData() : null; | 47 | ? parsedData.getData().getData() : null; |
48 | 48 | ||
49 | logger.verbose('PDU processed', { from, msg }); | 49 | logger.verbose('PDU processed', { from, msg }); |
50 | return { from, msg }; | 50 | return { from, msg }; |
51 | } | 51 | } |
52 | 52 | ||
53 | function onCOPS(data) { | 53 | function onCOPS(data) { |
54 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); | 54 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); |
55 | if (!val || !val[1]) return null; | 55 | if (!val || !val[1]) return null; |
56 | 56 | ||
57 | const cops = val[1]; | 57 | const cops = val[1]; |
58 | 58 | ||
59 | if (!cops) return null; | 59 | if (!cops) return null; |
60 | const [mode, format, networkId] = cops.split(','); | 60 | const [mode, format, networkId] = cops.split(','); |
61 | const networkName = networkId ? dbCops[networkId] || networkId : null; | 61 | const networkName = networkId ? dbCops[networkId] || networkId : null; |
62 | 62 | ||
63 | logger.verbose('COPS extracted', { | 63 | logger.verbose('COPS extracted', { |
64 | cops, mode, format, networkId, networkName, | 64 | cops, mode, format, networkId, networkName, |
65 | }); | 65 | }); |
66 | 66 | ||
67 | return { | 67 | return { |
68 | cops, mode, format, networkId, networkName, | 68 | cops, mode, format, networkId, networkName, |
69 | }; | 69 | }; |
70 | } | 70 | } |
71 | 71 | ||
72 | 72 | ||
73 | function isResultCodeIs(data, resultCode) { | 73 | function isResultCodeIs(data, resultCode) { |
74 | if (!data) return false; | 74 | if (!data) return false; |
75 | const cleanedData = (data.toString() || '').trim(); | 75 | const cleanedData = (data.toString() || '').trim(); |
76 | if (!cleanedData) return false; | 76 | if (!cleanedData) return false; |
77 | 77 | ||
78 | if (resultCode.indexOf('+') !== 0) { | 78 | if (resultCode.indexOf('+') !== 0) { |
79 | // eslint-disable-next-line no-param-reassign | 79 | // eslint-disable-next-line no-param-reassign |
80 | resultCode = `+${resultCode}`; | 80 | resultCode = `+${resultCode}`; |
81 | } | 81 | } |
82 | 82 | ||
83 | if (resultCode.search(/:$/) < 0) { | 83 | if (resultCode.search(/:$/) < 0) { |
84 | // eslint-disable-next-line no-param-reassign | 84 | // eslint-disable-next-line no-param-reassign |
85 | resultCode += ':'; | 85 | resultCode += ':'; |
86 | } | 86 | } |
87 | 87 | ||
88 | return cleanedData.indexOf(resultCode) === 0; | 88 | return cleanedData.indexOf(resultCode) === 0; |
89 | } | 89 | } |
90 | 90 | ||
91 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); | 91 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); |
92 | parserReadline.on('data', (data) => { | 92 | parserReadline.on('data', (data) => { |
93 | logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` }); | 93 | logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` }); |
94 | 94 | ||
95 | if (!data) return; | 95 | if (!data) return; |
96 | 96 | ||
97 | const pduParsed = parsePdu(data); | 97 | const pduParsed = parsePdu(data); |
98 | if (pduParsed) { | 98 | if (pduParsed) { |
99 | logger.verbose('Got a PDU data', { type: typeof pduParsed }); | 99 | logger.verbose('PDU parsed', { type: pduParsed.getType() }); |
100 | } | ||
101 | |||
102 | if (pduParsed && pduParsed.getType.getSrr()) { | ||
103 | logger.verbose('Got a PDU SMS-DELIVER', { type: pduParsed.getType() }); | ||
100 | onPdu(data, pduParsed); | 104 | onPdu(data, pduParsed); |
101 | } else if (isResultCodeIs(data, 'CSQ')) { | 105 | } else if (isResultCodeIs(data, 'CSQ')) { |
102 | logger.verbose('Got a signal quality report', { data: data.toString() }); | 106 | logger.verbose('Got a signal quality report', { data: data.toString() }); |
103 | onCSQ(data); | 107 | onCSQ(data); |
104 | } else if (isResultCodeIs(data, 'COPS:')) { | 108 | } else if (isResultCodeIs(data, 'COPS:')) { |
105 | logger.verbose('Got a COPS report', { data: data.toString() }); | 109 | logger.verbose('Got a COPS report', { data: data.toString() }); |
106 | onCOPS(data); | 110 | onCOPS(data); |
107 | } else if (isResultCodeIs(data, 'CMT')) { | 111 | } else if (isResultCodeIs(data, 'CMT')) { |
108 | logger.verbose('Got a new message report', { data: data.toString() }); | 112 | logger.verbose('Got a new message report', { data: data.toString() }); |
109 | } else if (isResultCodeIs(data, 'CMTI')) { | 113 | } else if (isResultCodeIs(data, 'CMTI')) { |
110 | logger.verbose('Got a new message notification report', { data: data.toString() }); | 114 | logger.verbose('Got a new message notification report', { data: data.toString() }); |
111 | } | 115 | } |
112 | }); | 116 | }); |
113 | 117 | ||
114 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); | 118 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
115 | parserWaitForOkOrError.on('data', (data) => { | 119 | parserWaitForOkOrError.on('data', (data) => { |
116 | logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); | 120 | logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); |
117 | }); | 121 | }); |
118 | 122 | ||
119 | 123 | ||
120 | exports.parserReadline = parserReadline; | 124 | exports.parserReadline = parserReadline; |
121 | exports.parserWaitForOkOrError = parserWaitForOkOrError; | 125 | exports.parserWaitForOkOrError = parserWaitForOkOrError; |
122 | 126 |