UNPKG

2.08 kBJavaScriptView Raw
1'use strict';
2const config = require('./data.js');
3const moment = require('moment');
4
5const dateParse = (date, format) => {
6
7 if(format!=undefined){
8 date = date.split("/");
9 format = format.split("/");
10 let year,month,day;
11
12 for(var key in format){
13 if(format[key]==="yy"){
14 year ="20"+date[key]
15 }else if(format[key]==="mm"){
16 month = date[key]
17 }else if(format[key]==="dd"){
18 day = date[key]
19 }
20 }
21 return (`${year}-${month}-${day}`);
22 }
23}
24
25const getIndex = raw => {
26 for (var key in config.patterns) {
27 if (config.patterns.hasOwnProperty(key)) {
28 if(raw.protocol === key){
29 return config.mapIndex[key];
30 }
31 }
32 }
33};
34
35const parseMeitrack = raw => {
36 let parsedData = parse(raw);
37 let jsonResult = {"alert" : null,"latitude":null,"longitude":null,"speed":null,"date":null,"parsedDate": Date,"dateTime":Date,"time":null,"power":null,"door":null,"acc":null,"lastlatitude":null,"lastlongitude":null,"mnc":null,"mcc":null,"timestampsent":null,"direction":null, "GPSPosition":null, "GPSSIgnal":null, "vehicleBattery":null};
38
39 if(parsedData.status == "Failed"){
40 jsonResult = parsedData;
41 }else{
42 let dataIndex = getIndex(parsedData);
43 for (var key in dataIndex) {
44 if (dataIndex.hasOwnProperty(key)) {
45 if(key === "alert"){
46 jsonResult[key] = config.parseAlarm(parsedData[dataIndex[key]]).AlertType;
47 }else{
48 jsonResult[key] = parsedData[dataIndex[key]];
49 }
50 }
51 }
52 }
53 // jsonResult["parsedDate"] = dateParse(jsonResult["date"], config.dateFormat[parsedData.protocol])
54 return jsonResult;
55};
56
57const parse = raw => {
58 let result = {status: 'Failed', message: 'UnknownProtocol', raw: raw.toString()};
59 for (var key in config.patterns) {
60 if (config.patterns.hasOwnProperty(key)) {
61 if(config.patterns[key].test(raw)){
62 result = config.patterns[key].exec(raw);
63 result.protocol = key;
64 }
65 }
66 }
67 return result;
68};
69
70module.exports = {
71 parse: parse,
72 parseMeitrack: parseMeitrack
73};