Commit 2e49a3d2029d7fabcc902fad15b2d9d9abf48a62
1 parent
58d87d4cd9
Exists in
master
pduParsed
Showing 1 changed file with 17 additions and 2 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 | 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) { | ||
24 | if (!data) return null; | ||
25 | |||
26 | try { | ||
27 | const result = pdu.parse(data.toString().trim() || ''); | ||
28 | return result; | ||
29 | } catch (e) { | ||
30 | return null; | ||
31 | } | ||
32 | } | ||
33 | |||
23 | function onCSQ(data) { | 34 | function onCSQ(data) { |
24 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); | 35 | const val = data.toString().trim().match(/\+CSQ:\s*(.*)/); |
25 | if (!val || !val[1]) return null; | 36 | if (!val || !val[1]) return null; |
26 | 37 | ||
27 | logger.info('Signal quality extracted', { signalQuality: val[1] }); | 38 | logger.info('Signal quality extracted', { signalQuality: val[1] }); |
28 | return val[1]; | 39 | return val[1]; |
29 | } | 40 | } |
30 | 41 | ||
31 | function onCOPS(data) { | 42 | function onCOPS(data) { |
32 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); | 43 | const val = data.toString().trim().match(/\+COPS:\s*(.*)/); |
33 | if (!val || !val[1]) return null; | 44 | if (!val || !val[1]) return null; |
34 | 45 | ||
35 | const cops = val[1]; | 46 | const cops = val[1]; |
36 | 47 | ||
37 | if (!cops) return null; | 48 | if (!cops) return null; |
38 | const [mode, format, networkId] = cops.split(','); | 49 | const [mode, format, networkId] = cops.split(','); |
39 | const networkName = networkId ? dbCops[networkId] || networkId : null; | 50 | const networkName = networkId ? dbCops[networkId] || networkId : null; |
40 | 51 | ||
41 | logger.verbose('COPS extracted', { | 52 | logger.verbose('COPS extracted', { |
42 | cops, mode, format, networkId, networkName, | 53 | cops, mode, format, networkId, networkName, |
43 | }); | 54 | }); |
44 | 55 | ||
45 | return { | 56 | return { |
46 | cops, mode, format, networkId, networkName, | 57 | cops, mode, format, networkId, networkName, |
47 | }; | 58 | }; |
48 | } | 59 | } |
49 | 60 | ||
50 | 61 | ||
51 | function isResultCodeIs(data, resultCode) { | 62 | function isResultCodeIs(data, resultCode) { |
52 | if (!data) return false; | 63 | if (!data) return false; |
53 | const cleanedData = (data.toString() || '').trim(); | 64 | const cleanedData = (data.toString() || '').trim(); |
54 | if (!cleanedData) return false; | 65 | if (!cleanedData) return false; |
55 | 66 | ||
56 | if (resultCode.indexOf('+') !== 0) { | 67 | if (resultCode.indexOf('+') !== 0) { |
57 | // eslint-disable-next-line no-param-reassign | 68 | // eslint-disable-next-line no-param-reassign |
58 | resultCode = `+${resultCode}`; | 69 | resultCode = `+${resultCode}`; |
59 | } | 70 | } |
60 | 71 | ||
61 | if (resultCode.search(/:$/) < 0) { | 72 | if (resultCode.search(/:$/) < 0) { |
62 | // eslint-disable-next-line no-param-reassign | 73 | // eslint-disable-next-line no-param-reassign |
63 | resultCode += ':'; | 74 | resultCode += ':'; |
64 | } | 75 | } |
65 | 76 | ||
66 | return cleanedData.indexOf(resultCode) === 0; | 77 | return cleanedData.indexOf(resultCode) === 0; |
67 | } | 78 | } |
68 | 79 | ||
69 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); | 80 | const parserReadline = new ParserReadline({ delimiter: PARSER_READLINE_DELIMITER }); |
70 | parserReadline.on('data', (data) => { | 81 | parserReadline.on('data', (data) => { |
71 | logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` }); | 82 | logger.verbose('INCOMING', { parser: 'parserReadLine', data: `${data.toString()}${PARSER_READLINE_DELIMITER}` }); |
72 | 83 | ||
73 | if (!data) return; | 84 | if (!data) return; |
74 | if (isResultCodeIs(data, 'CSQ')) { | 85 | |
86 | const pduParsed = parsePdu(data); | ||
87 | if (pduParsed) { | ||
88 | logger.verbose('Got a PDU data', { pduParsed }); | ||
89 | } else if (isResultCodeIs(data, 'CSQ')) { | ||
75 | logger.verbose('Got a signal quality report', { data: data.toString() }); | 90 | logger.verbose('Got a signal quality report', { data: data.toString() }); |
76 | onCSQ(data); | 91 | onCSQ(data); |
77 | } else if (isResultCodeIs(data, 'COPS:')) { | 92 | } else if (isResultCodeIs(data, 'COPS:')) { |
78 | logger.verbose('Got a COPS report', { data: data.toString() }); | 93 | logger.verbose('Got a COPS report', { data: data.toString() }); |
79 | onCOPS(data); | 94 | onCOPS(data); |
80 | } else if (isResultCodeIs(data, 'CMT')) { | 95 | } else if (isResultCodeIs(data, 'CMT')) { |
81 | logger.verbose('Got a new message report', { data: data.toString() }); | 96 | logger.verbose('Got a new message report', { data: data.toString() }); |
82 | } else if (isResultCodeIs(data, 'CMTI')) { | 97 | } else if (isResultCodeIs(data, 'CMTI')) { |
83 | logger.verbose('Got a new message notification report', { data: data.toString() }); | 98 | logger.verbose('Got a new message notification report', { data: data.toString() }); |
84 | } | 99 | } |
85 | }); | 100 | }); |
86 | 101 | ||
87 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); | 102 | const parserWaitForOkOrError = new ParserRegex({ regex: PARSER_WAIT_FOR_OK_OR_ERROR_REGEX }); |
88 | parserWaitForOkOrError.on('data', (data) => { | 103 | parserWaitForOkOrError.on('data', (data) => { |
89 | logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); | 104 | logger.verbose('INCOMING', { parser: 'parserWaitForOkOrError', data: data.toString() }); |
90 | }); | 105 | }); |
91 | 106 | ||
92 | 107 | ||
93 | exports.parserReadline = parserReadline; | 108 | exports.parserReadline = parserReadline; |
94 | exports.parserWaitForOkOrError = parserWaitForOkOrError; | 109 | exports.parserWaitForOkOrError = parserWaitForOkOrError; |
95 | 110 |