Commit 8914d7abb93b7c2693b2ce5b5a8bc0e0e7a70bad
1 parent
9cfc0203ff
Exists in
master
onPduDeliver ts
Showing 1 changed file with 3 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 moment = require('moment'); | 4 | const moment = require('moment'); |
5 | const pdu = require('node-pdu'); | 5 | const pdu = require('node-pdu'); |
6 | const ParserReadline = require('@serialport/parser-readline'); | 6 | const ParserReadline = require('@serialport/parser-readline'); |
7 | const ParserRegex = require('@serialport/parser-regex'); | 7 | const ParserRegex = require('@serialport/parser-regex'); |
8 | 8 | ||
9 | const logger = require('komodo-sdk/logger'); | 9 | const logger = require('komodo-sdk/logger'); |
10 | 10 | ||
11 | const dbCops = require('./db-cops'); | 11 | const dbCops = require('./db-cops'); |
12 | const modemInfo = require('./modem-info'); | 12 | const modemInfo = require('./modem-info'); |
13 | 13 | ||
14 | let port; | 14 | let port; |
15 | 15 | ||
16 | exports.setPort = function setPort(val) { | 16 | exports.setPort = function setPort(val) { |
17 | logger.info('SERIALPORT-PARSERS: setting port'); | 17 | logger.info('SERIALPORT-PARSERS: setting port'); |
18 | port = val; | 18 | port = val; |
19 | }; | 19 | }; |
20 | 20 | ||
21 | exports.getPort = function getPort() { | 21 | exports.getPort = function getPort() { |
22 | return port; | 22 | return port; |
23 | }; | 23 | }; |
24 | 24 | ||
25 | function parsePdu(data) { | 25 | function parsePdu(data) { |
26 | if (!data) return null; | 26 | if (!data) return null; |
27 | 27 | ||
28 | try { | 28 | try { |
29 | const result = pdu.parse(data.toString().trim() || ''); | 29 | const result = pdu.parse(data.toString().trim() || ''); |
30 | return result; | 30 | return result; |
31 | } catch (e) { | 31 | } catch (e) { |
32 | return null; | 32 | return null; |
33 | } | 33 | } |
34 | } | 34 | } |
35 | 35 | ||
36 | function onCSQ(data) { | 36 | function onCSQ(data) { |
37 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); | 37 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); |
38 | if (!val || !val[1]) return null; | 38 | if (!val || !val[1]) return null; |
39 | 39 | ||
40 | const [, signalStrength] = val; | 40 | const [, signalStrength] = val; |
41 | 41 | ||
42 | logger.info('Signal quality extracted', { signalQuality: val[1] }); | 42 | logger.info('Signal quality extracted', { signalQuality: val[1] }); |
43 | 43 | ||
44 | modemInfo.signalStrength = signalStrength; | 44 | modemInfo.signalStrength = signalStrength; |
45 | modemInfo.signalStrengthTs = new Date(); | 45 | modemInfo.signalStrengthTs = new Date(); |
46 | modemInfo.signalStrengthTsReadable = moment(modemInfo.signalStrengthTs).format('YYYY-MM-DD HH:mm:ss'); | 46 | modemInfo.signalStrengthTsReadable = moment(modemInfo.signalStrengthTs).format('YYYY-MM-DD HH:mm:ss'); |
47 | 47 | ||
48 | return signalStrength; | 48 | return signalStrength; |
49 | } | 49 | } |
50 | 50 | ||
51 | function onPduDeliver(data, parsedData) { | 51 | function onPduDeliver(data, parsedData) { |
52 | const from = parsedData.getAddress && parsedData.getAddress().getPhone | 52 | const from = parsedData.getAddress && parsedData.getAddress().getPhone |
53 | ? parsedData.getAddress().getPhone() : null; | 53 | ? parsedData.getAddress().getPhone() : null; |
54 | 54 | ||
55 | const msg = parsedData.getData && parsedData.getData().getData | 55 | const msg = parsedData.getData && parsedData.getData().getData |
56 | ? parsedData.getData().getData() : null; | 56 | ? parsedData.getData().getData() : null; |
57 | 57 | ||
58 | logger.verbose('PDU processed', { from, msg }); | 58 | const ts = new Date(parsedData.getScts().getIsoString()); |
59 | |||
60 | logger.verbose('PDU processed', { ts, from, msg }); | ||
59 | return { from, msg }; | 61 | return { from, msg }; |
60 | } | 62 | } |
61 | 63 | ||
62 | function onCOPS(data) { | 64 | function onCOPS(data) { |
63 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); | 65 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); |
64 | if (!val || !val[1]) return null; | 66 | if (!val || !val[1]) return null; |
65 | 67 | ||
66 | const cops = val[1]; | 68 | const cops = val[1]; |
67 | 69 | ||
68 | if (!cops) return null; | 70 | if (!cops) return null; |
69 | const [mode, format, networkId] = cops.split(','); | 71 | const [mode, format, networkId] = cops.split(','); |
70 | const networkName = networkId ? dbCops[networkId] || networkId : null; | 72 | const networkName = networkId ? dbCops[networkId] || networkId : null; |
71 | 73 | ||
72 | logger.info('COPS extracted', { | 74 | logger.info('COPS extracted', { |
73 | cops, mode, format, networkId, networkName, | 75 | cops, mode, format, networkId, networkName, |
74 | }); | 76 | }); |
75 | 77 | ||
76 | modemInfo.cops = cops; | 78 | modemInfo.cops = cops; |
77 | modemInfo.networkId = networkId; | 79 | modemInfo.networkId = networkId; |
78 | modemInfo.networkName = networkName; | 80 | modemInfo.networkName = networkName; |
79 | 81 | ||
80 | return { | 82 | return { |
81 | cops, mode, format, networkId, networkName, | 83 | cops, mode, format, networkId, networkName, |
82 | }; | 84 | }; |
83 | } | 85 | } |
84 | 86 | ||
85 | 87 | ||
86 | function isResultCodeIs(data, resultCode) { | 88 | function isResultCodeIs(data, resultCode) { |
87 | if (!data) return false; | 89 | if (!data) return false; |
88 | const cleanedData = (data.toString() || '').trim(); | 90 | const cleanedData = (data.toString() || '').trim(); |
89 | if (!cleanedData) return false; | 91 | if (!cleanedData) return false; |
90 | 92 | ||
91 | if (resultCode.indexOf('+') !== 0) { | 93 | if (resultCode.indexOf('+') !== 0) { |
92 | // eslint-disable-next-line no-param-reassign | 94 | // eslint-disable-next-line no-param-reassign |
93 | resultCode = `+${resultCode}`; | 95 | resultCode = `+${resultCode}`; |
94 | } | 96 | } |
95 | 97 | ||
96 | if (resultCode.search(/:$/) < 0) { | 98 | if (resultCode.search(/:$/) < 0) { |
97 | // eslint-disable-next-line no-param-reassign | 99 | // eslint-disable-next-line no-param-reassign |
98 | resultCode += ':'; | 100 | resultCode += ':'; |
99 | } | 101 | } |
100 | 102 | ||
101 | return cleanedData.indexOf(resultCode) === 0; | 103 | return cleanedData.indexOf(resultCode) === 0; |
102 | } | 104 | } |
103 | 105 | ||
104 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); | 106 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); |
105 | parserReadline.on('data', (data) => { | 107 | parserReadline.on('data', (data) => { |
106 | logger.verbose('INCOMING', { data: `${data.toString()}${PARSER_READLINE_DELIMITER}`, parser: 'parserReadLine' }); | 108 | logger.verbose('INCOMING', { data: `${data.toString()}${PARSER_READLINE_DELIMITER}`, parser: 'parserReadLine' }); |
107 | 109 | ||
108 | if (!data) return; | 110 | if (!data) return; |
109 | 111 | ||
110 | const pduParsed = parsePdu(data); | 112 | const pduParsed = parsePdu(data); |
111 | if (pduParsed) { | 113 | if (pduParsed) { |
112 | logger.verbose('PDU parsed', { type: (typeof pduParsed.getType === 'function') && pduParsed.getType() }); | 114 | logger.verbose('PDU parsed', { type: (typeof pduParsed.getType === 'function') && pduParsed.getType() }); |
113 | } | 115 | } |
114 | 116 | ||
115 | if (pduParsed && pduParsed.constructor.name !== 'Deliver') { | 117 | if (pduParsed && pduParsed.constructor.name !== 'Deliver') { |
116 | logger.warn('Unknown PDU message type name. PLEASE REPORT IT TO DEVELOPER AT TEKTRANS', { typeName: pduParsed.constructor.name, type: pduParsed.getType(), data: data.toString().trim() }); | 118 | logger.warn('Unknown PDU message type name. PLEASE REPORT IT TO DEVELOPER AT TEKTRANS', { typeName: pduParsed.constructor.name, type: pduParsed.getType(), data: data.toString().trim() }); |
117 | } | 119 | } |
118 | 120 | ||
119 | if (pduParsed && pduParsed.constructor.name === 'Deliver' && pduParsed.getType && pduParsed.getType().getSrr()) { | 121 | if (pduParsed && pduParsed.constructor.name === 'Deliver' && pduParsed.getType && pduParsed.getType().getSrr()) { |
120 | logger.verbose('Got a PDU SMS-DELIVER', { type: pduParsed.getType() }); | 122 | logger.verbose('Got a PDU SMS-DELIVER', { type: pduParsed.getType() }); |
121 | onPduDeliver(data, pduParsed); | 123 | onPduDeliver(data, pduParsed); |
122 | } else if (isResultCodeIs(data, 'CSQ')) { | 124 | } else if (isResultCodeIs(data, 'CSQ')) { |
123 | logger.verbose('Got a signal quality report', { data: data.toString() }); | 125 | logger.verbose('Got a signal quality report', { data: data.toString() }); |
124 | onCSQ(data); | 126 | onCSQ(data); |
125 | } else if (isResultCodeIs(data, 'COPS:')) { | 127 | } else if (isResultCodeIs(data, 'COPS:')) { |
126 | logger.verbose('Got a COPS report', { data: data.toString() }); | 128 | logger.verbose('Got a COPS report', { data: data.toString() }); |
127 | onCOPS(data); | 129 | onCOPS(data); |
128 | } else if (isResultCodeIs(data, 'CMT')) { | 130 | } else if (isResultCodeIs(data, 'CMT')) { |
129 | logger.verbose('Got a new message report', { data: data.toString() }); | 131 | logger.verbose('Got a new message report', { data: data.toString() }); |
130 | } else if (isResultCodeIs(data, 'CMTI')) { | 132 | } else if (isResultCodeIs(data, 'CMTI')) { |
131 | logger.verbose('Got a new message notification report', { data: data.toString() }); | 133 | logger.verbose('Got a new message notification report', { data: data.toString() }); |
132 | } | 134 | } |
133 | }); | 135 | }); |
134 | 136 | ||
135 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); | 137 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
136 | parserWaitForOkOrError.on('data', (data) => { | 138 | parserWaitForOkOrError.on('data', (data) => { |
137 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserWaitForOkOrError' }); | 139 | logger.verbose('INCOMING', { data: data.toString(), parser: 'parserWaitForOkOrError' }); |
138 | }); | 140 | }); |
139 | 141 | ||
140 | 142 | ||
141 | exports.PARSER_READLINE_DELIMITER = PARSER_READLINE_DELIMITER; | 143 | exports.PARSER_READLINE_DELIMITER = PARSER_READLINE_DELIMITER; |
142 | exports.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = PARSER_WAIT_FOR_OK_OR_ERROR_REGEX; | 144 | exports.PARSER_WAIT_FOR_OK_OR_ERROR_REGEX = PARSER_WAIT_FOR_OK_OR_ERROR_REGEX; |
143 | 145 | ||
144 | exports.parserReadline = parserReadline; | 146 | exports.parserReadline = parserReadline; |
145 | exports.parserWaitForOkOrError = parserWaitForOkOrError; | 147 | exports.parserWaitForOkOrError = parserWaitForOkOrError; |
146 | 148 |