-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.js
315 lines (296 loc) · 12.1 KB
/
converter.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var debug=false
const path=require('path')
var local_text="";
if(debug)
{
var fn=path.resolve(__dirname,'output.txt');
var fs = require("fs");
// get the data content separated by the empty line between
local_text = fs.readFileSync(fn)+ '';
console.log(convertToJson(local_text))
}
// add the double quotes around the json data
function wrap(str)
{
return "\""+str.trim()+"\""
}
module.exports.convertToJson=
function convertToJson(text)
{
//if(text===null)
// text=local_text;
//console.log("local-text="+text);
// default line end character
var lineend="\n";
var carriage_return="\r"
// watch out, this character MUST match what is used in the regex
var end_of_line_marker="!" // <-------, have code below to fix this automagically
var expression_line_marker="!"
// watch out
var started=false;
var v;
var limit='';
var global="g"
var l;
var r;
var r1;
let match_offset=0;
var begin_array="[";
var end_array="]"
var begin_structure="{"
var end_structure="}"
var colon=":"
var literal_comma=","
var open_paren=")"
var empty_string=""
var not_found=-1
var expressions=["([^!]*)!([^:]*):\\s+([^!]*)!(.*)", // 0, name and type
"([^:]*):\\s+([^!]*)!(.*)", // 1, colon separated data before paren
"\\((\\w*)\\s+=\\s+([^,]*),\\s+(\\w*)\\s+=\\s+([^\\)]*)\\)", // 2, data contains paren wrapped limits
"([^:]*):\\s+([^\\s]*)\\s+(.*)", // 3, data up to the paren wrapped limits
"([^:]*):\\s+([^\\s]*)\\s+\\((\\w*)\\s+=\\s+([^,]*),\\s+(\\w*)\\s+=\\s+([^\\)]*)\\)!"
]
// space for output
var output=[]
// lets do some setup first
// if the data looks like it contains a windows end of line (cr/lf) instead of linux (lf)
if(text.indexOf(carriage_return+lineend)>0)
// adjust the line end character
lineend=carriage_return+lineend;
// if the end of line marker is not what is used in the expressions
if(end_of_line_marker!==expression_line_marker)
{
// update the expressions with the selected character
var e=new RegExp(expression_line_marker,global)
// loop thru the array of expressions used
for(var i=0; i<expressions.length;i++)
{
// apply the change
expressions[i]=expressions[i].replace(e,end_of_line_marker)
if(debug)
console.log("new exp="+expressions[i])
}
}
// split sensors output based on blank line between adapters
var textForAdapter = text.trim().split(lineend+lineend);
// assume we will have an array of adapters.. only 1, still works
output.push(begin_structure);
// loop thru the adapters of info
// note that here the adapter info is still delimited as normal lines, which will make it harder to parse
for( var i in textForAdapter)
{
// watch out for empty lines.. shouldn't have any but
// editing manually may produce one we didn't see
if(textForAdapter[i].length>0)
{
var rr = new RegExp(lineend,global)
// replace all the default line end chars with the specified marker,
// puts the adapter data all on one 'line'
textForAdapter[i] = textForAdapter[i].replace(rr,end_of_line_marker)+end_of_line_marker; // replace all line ends with ! , so we don't get caught with the $ end of line problem
// remove the unusable 'ALARM' token
rr = new RegExp(" ALARM")
textForAdapter[i] = textForAdapter[i].replace(rr,empty_string);
if(started==false)
{
// adapter is a json structure
//output.push(begin_structure);
started=true;
}
// if no paren
if(textForAdapter[i].indexOf(open_paren)==not_found)
{
// and no colon
if(textForAdapter[i].indexOf(colon)==not_found)
{
// just a json label
output.push(wrap("Name")+colon+wrap(textForAdapter[i])+literal_comma);
}
else
{
// get the adapter name and type
var re=new RegExp(expressions[0]);
r=textForAdapter[i].match(re)
output.push(
wrap(r[1])+
colon+begin_structure+
wrap(r[2])+
colon+
wrap(r[3])
+literal_comma);
// remaining data will be in results array index 4
l=r[4]+empty_string;
// check for and strip off unusable info
if(l.indexOf(colon)==l.indexOf(end_of_line_marker)-1)
{
l=l.substring(l.indexOf(end_of_line_marker)+1)
}
// assume first time we don't need a preceding comma
comma=empty_string
// loop thru the rest of the data
do{
// get any individual data elements
re=new RegExp(expressions[1]);
r1=l.trim().match(re)
// add a comma between last and this entry
output.push(comma)
// format the data element
output.push(wrap(r1[1])+colon+wrap(r1[2]))
// indicate we will need a comma between items
comma=literal_comma
// get any remaining data from the match regex
l=r1[3];
// while there is data left
} while (l.length>0)
output.push(end_structure+literal_comma)
}
}
else // this data DOES have paren
{
// get the adapter name and type
var re = new RegExp(expressions[0])
r =textForAdapter[i].match(re)
output.push(
wrap(r[1])+
colon+begin_structure+
wrap(r[2])+
colon+
wrap(r[3])
+literal_comma);
// get the remaining data from match results, index 4 of the array
l=r[4]+empty_string; // rest of the line after regex
// loop thru the result of the data for this adapter
do
{
// there is colon separated data before the open paren still to process
if(l.indexOf(colon)>0 && l.indexOf(open_paren)>l.indexOf(colon) )
{
// paren further down the line
if(l.indexOf(open_paren)>l.indexOf(end_of_line_marker))
{
// get any data before the 1st paren, could be repeating
re=new RegExp(expressions[1]);
r1=l.match(re)
// format the data element (regex match elements 1 and 2)
output.push(wrap(r1[1])+colon+wrap(r1[2])+literal_comma)
// reset the remaining data
l=r1[3]; // rest of the line after regex
}
else // have some parenthesized data, no leading info
{
// by default the 1st match will be offset 3 in the returned match array
// offset 0 is the input
// offset 1 and 2 are used above
match_offset=3;
// set the work data variable
v=l;
// pick the right regex expression
if(v.indexOf(colon) == v.lastIndexOf(colon))
{
re=new RegExp(expressions[3])
}
else
{
re=new RegExp(expressions[4],global)
match_offset=0;
}
// get the number of matches
r1=v.match(re);
// if more than 1, and it better be!
if(r1.length>1)
{
let r2;
// again, useful match info will be offset 3 of the returned macth array
if(match_offset==3)
{
// output the item and value before the parens "Core 0: +44c "
output.push(wrap(r1[1])+colon+" "+begin_structure+ wrap("current")+colon+wrap(r1[2])+literal_comma)
// parse out the subgroups of paren wrapped info
re=new RegExp(expressions[2],global)
// each prior match has the text of each paren wrapped group, so parse those one at a time
for(; match_offset <r1.length;match_offset++)
{
// create a new array of values to hold the limts info)
output.push(wrap("limits")+colon+begin_array);
var comma=empty_string;
// have to use exec for groups
while(r2=re.exec(r1[match_offset]))
{
// if the exec results length is an odd number of things
if(r2.length & 1 >0)
{
// write the 'trailing' comma at the front, null first time.
// we will break the loop on no more data so won't have the extra trailing comma
output.push(comma)
output.push(begin_structure)
for(var q3=1; q3< r2.length; q3+=2)
{
output.push(wrap(r2[q3])+colon+wrap(r2[q3+1]))
if(q3< r2.length-2)
{
output.push(literal_comma)
}
}
output.push(end_structure)
comma=literal_comma;
}
}
output.push(end_array);
output.push(end_structure);
if(match_offset <r1.length-1)
output.push(literal_comma);
}
}
else // doing a different paren wrapped string approach,
{ // just paren wrapped "temp1: +34.5°C (high = +70.0°C, hyst = +60.0°C)!"
for(; match_offset <r1.length;match_offset++)
{
while(r2=re.exec(r1[match_offset]))
{
// format the element and its current value
output.push(wrap(r2[1])+colon+" "+begin_structure+ wrap("current")+colon+wrap(r2[2])+literal_comma)
// start a limits array
output.push(wrap("limits")+colon+begin_array+begin_structure);
comma=empty_string
if(r2.length & 1 >0)
{
// loop thru the list
for(var q3=3; q3< r2.length; q3+=2)
{
output.push(comma)
output.push(wrap(r2[q3])+colon+wrap(r2[q3+1]))
comma=literal_comma
}
}
output.push(end_structure);
output.push(end_array);
output.push(end_structure);
if(match_offset <r1.length-1)
output.push(literal_comma);
}
}
}
// done with this line (block/adapters) of data
break;
}
else
{
// shouldnt get here.. means data doesn't match the reg ex
output.push("there are "+r1.length+" matches="+r1[0]+ " "+ JSON.stringify(r1))
// get the remaining data from the regex match array
l=r1[3];
}
}
}
else
break;
} while(true)
output.push(end_structure); // close all the open structures for this adapter
started=false; // tell start of new adapter to wrap with structure
if(i<textForAdapter.length-1) // don't add comma for last adapter
output.push(literal_comma)
}
}
}
output.push(end_structure)
return output.join(' ');
}