Skip to content

Commit

Permalink
Fix sintax
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Feb 29, 2016
1 parent fc3a3b5 commit 0c26860
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
39 changes: 25 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const getEvent = (event) => {
'79': 'fall',
'80': 'install',
'81': 'dropOff',
'139': 'maintenance',
'139': 'maintenance'
};
return events[alarm];
return events[event];
};

const getMvt380 = (raw) => {
Expand All @@ -83,10 +83,10 @@ const getMvt380 = (raw) => {
type: 'MVT380',
imei: parseInt(match[3], 10),
command: match[4],
event: getEvent(match[4]),
event: getEvent(match[5]),
loc: {
type: 'Point',
coordinates: [match[7], match[6]]
coordinates: [parseFloat(match[7]), parseFloat(match[6])]
},
datetime: moment(match[8], 'DDMMYYHHmmss').toDate(),
gpsSignal: match[9],
Expand All @@ -111,7 +111,7 @@ const getMvt380 = (raw) => {
'5': status[12] === '1',
'6': status[13] === '1',
'7': status[14] === '1',
'8': status[15] === '1',
'8': status[15] === '1'
},
output: {
'1': status[0] === '1',
Expand All @@ -121,7 +121,7 @@ const getMvt380 = (raw) => {
'5': status[4] === '1',
'6': status[5] === '1',
'7': status[6] === '1',
'8': status[7] === '1',
'8': status[7] === '1'
}
},
voltage: {
Expand Down Expand Up @@ -153,7 +153,7 @@ const getReverse = (lat, lon) => {
if (res.length === 0) resolve(null);
resolve(res[0].formattedAddress.split(',').map(x => x.trim()).slice(0, 2).join(', '));
}).catch(reject);
}
});
};

const getAddress = (lat, lng) => {
Expand All @@ -170,20 +170,31 @@ const getAddress = (lat, lng) => {
} else {
getReverse(lat, lng).then(resolve).catch(reject);
}
}
});
};

const parse = (raw) => {
let result = {type: 'UNKNOWN', raw: raw.toString()};
if (patterns.mvt380.test(raw.toString())) {
result = getMvt380(raw);
}
return result;
return new Promise((resolve) => {
let result = {type: 'UNKNOWN', raw: raw.toString()};
if (patterns.mvt380.test(raw.toString())) {
result = getMvt380(raw);
const [lng, lat] = result.loc.coordinates;
getAddress(lat, lng).then(address => {
result.address = address;
resolve(result);
}).catch(() => {
resolve(result);
});
}
resolve(result);
return result;
});
};

module.exports = {
parse: parse,
patterns: patterns,
getMvt380: getMvt380,
setCache: setCache
setCache: setCache,
getAddress: getAddress
};
59 changes: 29 additions & 30 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,35 @@ describe('tz-parzer', () => {
expect(data.event).to.eql('timeIntervalTracking');
expect(data.loc.type).to.eql('Point');
expect(data.loc.coordinates).to.eql([79.960473, 7.092076]);
expect(data.datetime).to.be.a.date;
expect(gpsSignal).to.eql('A');
expect(satellites).to.eql(10);
expect(gsmSignal).to.eql(9);
expect(speed).to.eql(57);
expect(direction).to.eql(275);
expect(hdop).to.eql(1);
expect(altitude).to.eql(14);
expect(odometer).to.eql(5783799);
expect(runtime).to.eql(7403612);
expect(mcc).to.eql('413');
expect(mnc).to.eql('1');
expect(lac).to.eql(63200);
expect(ci).to.eql(14643);
expect(status.input['1'].to.be.false;
expect(status.input['2'].to.be.false;
expect(status.input['3'].to.be.false;
expect(status.input['4'].to.be.false;
expect(status.input['5'].to.be.false;
expect(status.input['6'].to.be.false;
expect(status.input['7'].to.be.false;
expect(status.input['8'].to.be.false;
expect(status.output['1'].to.be.false;
expect(status.output['2'].to.be.false;
expect(status.output['3'].to.be.false;
expect(status.output['4'].to.be.false;
expect(status.output['5'].to.be.false;
expect(status.output['6'].to.be.false;
expect(status.output['7'].to.be.false;
expect(status.output['8'].to.be.false;
expect(data.gpsSignal).to.eql('A');
expect(data.satellites).to.eql(10);
expect(data.gsmSignal).to.eql(9);
expect(data.speed).to.eql(57);
expect(data.direction).to.eql(275);
expect(data.hdop).to.eql(1);
expect(data.altitude).to.eql(14);
expect(data.odometer).to.eql(5783799);
expect(data.runtime).to.eql(7403612);
expect(data.mcc).to.eql('413');
expect(data.mnc).to.eql('1');
expect(data.lac).to.eql(63200);
expect(data.ci).to.eql(14643);
expect(data.status.input['1']).to.be.false;
expect(data.status.input['2']).to.be.false;
expect(data.status.input['3']).to.be.false;
expect(data.status.input['4']).to.be.false;
expect(data.status.input['5']).to.be.false;
expect(data.status.input['6']).to.be.false;
expect(data.status.input['7']).to.be.false;
expect(data.status.input['8']).to.be.false;
expect(data.status.output['1']).to.be.false;
expect(data.status.output['2']).to.be.false;
expect(data.status.output['3']).to.be.false;
expect(data.status.output['4']).to.be.false;
expect(data.status.output['5']).to.be.false;
expect(data.status.output['6']).to.be.false;
expect(data.status.output['7']).to.be.false;
expect(data.status.output['8']).to.be.false;
done();
}).catch(err => {
expect(err).to.be.undefined;
Expand Down

0 comments on commit 0c26860

Please sign in to comment.