-
Notifications
You must be signed in to change notification settings - Fork 1
/
readallParser.js
98 lines (87 loc) · 2.68 KB
/
readallParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const headerStandard = "| wiringPi | GPIO | Phys | Name | Mode | Value |";
const headerBPlus = " | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |";
function determineFormat(header) {
switch (header) {
case headerStandard:
return 'standard';
break;
case headerBPlus:
return 'bplus';
break;
default:
return undefined;
break;
}
}
function tokenize(line) {
var tokens = [];
line.split('|').slice(1, -1).forEach(function(token) {
tokens.push(token.trim());
});
return tokens;
}
function parsePin(wiring, bcm, phys, name, mode, value) {
function highLowToBool(string) {
switch(string.toLowerCase()) {
case '1':
case 'high':
return true;
break;
case '0':
case 'low':
return false;
break;
default:
return undefined;
break;
}
}
var pinObj = {
'wiring': parseInt(wiring),
'bcm': parseInt(bcm),
'phys': parseInt(phys),
'name': name,
'mode': mode.toLowerCase(),
'value': highLowToBool(value)
};
return pinObj;
}
function parseLine(pins, format, tokens) {
if (format === 'standard') {
if (tokens.length !== 6) {
parseWarning("readall (line: '" + line + "')");
} else {
var t = tokens;
pins.push(parsePin(t[0], t[1], t[2], t[3], t[4], t[5]));
}
} else if (format === 'bplus') {
if (tokens.length !== 13) {
parseWarning("readall (line: '" + line + "')");
} else {
// B+ readall has two pins per line, symmetrical layout, [6] is empty
// left side
var t = tokens.slice(0, 6);
pins.push(parsePin(t[1], t[0], t[5], t[2], t[3], t[4]));
// right side, order mirrored
t = tokens.slice(7);
pins.push(parsePin(t[4], t[5], t[0], t[3], t[2], t[1]));
}
}
}
function parse(stdout) {
var header = stdout.split('\n')[1]; // second line is header
var lines = stdout.split('\n').slice(3, -3); // cut off header/footer
var format = determineFormat(header);
if (typeof format === 'undefined') {
parseWarning("readall (unknown format)");
(callback || function noop(){})(err, stdout, stderr);
} else if (format === 'bplus') {
lines.pop(); // remove extra empty-line
}
var pins = [];
lines.forEach(function(line) {
parseLine(pins, format, tokenize(line));
});
return pins;
}
exports.parse = parse;