Skip to content

Commit

Permalink
Add address
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Feb 16, 2016
1 parent b697532 commit 78599e0
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 40 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const tz = require('tz-parser');

const raw = new Buffer('$$B6869444005480041|91$GPRMC,194329.000,A,3321.6735,S,07030.7640,W,0.00,0.00,090216,,,A*6C|02.1|01.3|01.7|000000000000|20160209194326|13981188|00000000|32D3A03F|0000|0.6376|0100|7B20\r\n');
const data = tz.parse(raw);
const data = await tz.parse(raw);
45 changes: 30 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,63 @@
},
"homepage": "https://github.com/lgaticaq/tz-parser#readme",
"dependencies": {
"bluebird": "^3.3.1",
"bscoords": "0.0.1",
"crc": "^3.4.0",
"hiredis": "^0.4.1",
"moment": "^2.11.2",
"node-nmea": "^0.6.0"
"node-geocoder": "^3.6.2",
"node-nmea": "^0.6.0",
"redis-url": "^1.2.1"
},
"devDependencies": {
"babel-cli": "^6.5.1",
"babel-core": "^6.5.2",
"babel-eslint": "^4.1.8",
"babel-plugin-transform-async-to-generator": "^6.5.0",
"babel-plugin-transform-regenerator": "^6.5.2",
"babel-preset-es2015": "^6.5.0",
"eslint": "^2.1.0",
"eslint": "1.10.3",
"rimraf": "^2.5.2"
},
"eslintConfig": {
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"parser": "babel-eslint",
"rules": {
"strict": 0,
"indent": [
2,
2
],
"linebreak-style": [
2,
"unix"
],
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
}
},
"ecmaFeatures": {
"modules": true
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended"
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"syntax-async-functions",
"transform-regenerator"
]
},
"tonicExampleFilename": "example.js"
Expand Down
171 changes: 147 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
'use strict';

import 'babel-polyfill';

import crc from 'crc';
import moment from 'moment';
import nmea from 'node-nmea';
import redisUrl from 'redis-url';
import nodeGeocoder from 'node-geocoder';
import bscoords from 'bscoords';
import Promise from 'bluebird';

Promise.promisifyAll(bscoords);

let client;

const patterns = {
avl05: /^\$\$([0-9A-F]{2})(\d{15})\|([0-9A]{2})(\$GPRMC\,(\d{6}\.\d{3})\,([AV])\,(\d{4}\.\d{4}\,[NS])\,(\d{5}\.\d{4}\,[WE])\,(\d{1,3}\.\d{1,3})?\,(\d{1,3}\.\d{1,3})?\,(\d{6})\,((\d{1,3}\.\d{1,3})?\,([WE])?)\,?([ADENS])?\*([0-9A-F]{2})|[0]{60})\|(\d{2}\.\d{1})\|(\d{2}\.\d{1})\|(\d{2}\.\d{1})\|([01])([01])([01])([01])([01])([01])([01])([01])([01])([01])([01])([01])\|(\d{14})\|([01])(\d{3})(\d{4})\|(\d{4})(\d{4})\|([0-9A-F]{4})([0-9A-F]{4})\|([01\-]\d{3})\|(\d{1,4}\.\d{1,12})\|(\d{4})\|([0-9A-F]{4})\r\n$/,
Expand Down Expand Up @@ -62,9 +72,19 @@ const isValid = (raw, len, checksum) =>{
return verifyCrc(raw, checksum) && verifyLen(raw, len);
};

const getAvl05 = (data) => {
const match = patterns.avl05.exec(data.toString());
const checkCurrentInfoPanel = (datetime) => {
moment.locale('es');
const now = moment.utc();
now.subtract(1, 'minutes');
return {
isCurrent: now < moment.utc(datetime),
diff: moment.duration(now.diff(datetime)).humanize()
};
};

const getAvl05 = async function(raw, options) {
const match = patterns.avl05.exec(raw.toString());
const data = {
raw: match[0],
type: 'TZ-AVL05',
imei: parseInt(match[2], 10),
Expand Down Expand Up @@ -103,11 +123,25 @@ const getAvl05 = (data) => {
serialId: parseInt(match[42]),
valid: isValid(match[0], parseInt(match[1], 16), parseInt(match[43], 16))
};
data.currentData = checkCurrentInfoPanel(data.datetime);
try {
if (!data.gprmcData.loc) {
const loc = await getLoc(options.mcc, options.mnc, data.lac, data.cid);
if (!loc) return data;
data.gprmcData.loc = loc;
}
const [lng, lat] = data.gprmcData.loc.geojson.coordinates;
const address = await getAddress(lat, lng);
data.gprmcData.address = address;
return data;
} catch (err) {
return data;
}
};

const getAvl08 = (data) => {
const match = patterns.avl05.exec(data.toString());
return {
const getAvl08 = async function(raw, options) {
const match = patterns.avl05.exec(raw.toString());
const data = {
raw: match[0],
type: 'TZ-AVL08',
imei: parseInt(match[2], 10),
Expand Down Expand Up @@ -149,11 +183,25 @@ const getAvl08 = (data) => {
rfidNumber: parseInt(match[43]),
valid: isValid(match[0], parseInt(match[1], 16), parseInt(match[44], 16))
};
data.currentData = checkCurrentInfoPanel(data.datetime);
try {
if (!data.gprmcData.loc) {
const loc = await getLoc(options.mcc, options.mnc, data.lac, data.cid);
if (!loc) return data;
data.gprmcData.loc = loc;
}
const [lng, lat] = data.gprmcData.loc.geojson.coordinates;
const address = await getAddress(lat, lng);
data.gprmcData.address = address;
return data;
} catch (err) {
return data;
}
};

const getAvl201 = (data) => {
const match = patterns.avl05.exec(data.toString());
return {
const getAvl201 = async function(raw, options) {
const match = patterns.avl05.exec(raw.toString());
const data = {
raw: match[0],
type: 'TZ-AVL201',
imei: parseInt(match[2], 10),
Expand Down Expand Up @@ -186,6 +234,20 @@ const getAvl201 = (data) => {
serialId: parseInt(match[40]),
valid: isValid(match[0], parseInt(match[1], 16), parseInt(match[41], 16))
};
data.currentData = checkCurrentInfoPanel(data.datetime);
try {
if (!data.gprmcData.loc) {
const loc = await getLoc(options.mcc, options.mnc, data.lac, data.cid);
if (!loc) return data;
data.gprmcData.loc = loc;
}
const [lng, lat] = data.gprmcData.loc.geojson.coordinates;
const address = await getAddress(lat, lng);
data.gprmcData.address = address;
return data;
} catch (err) {
return data;
}
};

const getCommand = (data) => {
Expand Down Expand Up @@ -253,22 +315,82 @@ const getCommandError = (data) => {
return {type: 'TZ-ERROR', command: match[0]};
};

const parse = (data) => {
let message;
if (patterns.avl05.test(data.toString())) {
message = getAvl05(data);
} else if (patterns.avl08.test(data.toString())) {
message = getAvl08(data);
} else if (patterns.avl201.test(data.toString())) {
message = getAvl201(data);
} else if (patterns.receiveOk.test(data.toString())) {
message = getCommand(data);
} else if (patterns.picture.test(data.toString())) {
message = getPicture(data);
} else if (patterns.receiveErr.test(data.toString())) {
message = getCommandError(data);
const setCache = (uri) => {
try {
client = redisUrl.connect(uri);
Promise.promisifyAll(Object.getPrototypeOf(client));
} catch (err) {
throw err;
}
return message;
};

const getReverse = async function(lat, lon) {
const geocoderProvider = 'google';
const httpAdapter = 'http';
const geocoder = nodeGeocoder(geocoderProvider, httpAdapter);
try {
const res = await geocoder.reverse({lat: lat, lon: lon});
if (res.length === 0) return null;
return res[0].formattedAddress.split(',').map(x => x.trim()).slice(0, 2).join(', ');
} catch (err) {
return null;
}
};

const getAddress = async function(lat, lng) {
try {
let address;
if (client) {
const reply = await client.getAsync(`geocoder:${lat}:${lng}`);
if (reply) return reply;
address = await getReverse(lat, lng);
if (!address) return null;
client.set(`geocoder:${lat}:${lng}`, address);
} else {
address = await getReverse(lat, lng);
}
return address;
} catch (err) {
return null;
}
};

const getLoc = async function(mcc, mnc, lac, cellid) {
try {
const coords = await bscoords.requestGoogleAsync(mcc, mnc, lac, cellid);
return {
dmm: {
latitude: nmea.latToDmm(coords.lat),
longitude: nmea.lngToDmm(coords.lon)
},
geojson: {
coordinates: [coords.lon, coords.lat],
type: 'Point'
}
};
} catch (err) {
return null;
}
};

const parse = async function(raw, options = {}) {
options.mcc = options.mcc || 730;
options.mnc = options.mnc || 1;
let result;
if (patterns.avl05.test(raw.toString())) {
result = await getAvl05(raw, options);
} else if (patterns.avl08.test(raw.toString())) {
result = await getAvl08(raw, options);
} else if (patterns.avl201.test(raw.toString())) {
result = await getAvl201(raw, options);
} else if (patterns.receiveOk.test(raw.toString())) {
result = getCommand(raw);
} else if (patterns.picture.test(raw.toString())) {
result = getPicture(raw);
} else if (patterns.receiveErr.test(raw.toString())) {
result = getCommandError(raw);
}
return result;
};

module.exports = {
Expand All @@ -279,5 +401,6 @@ module.exports = {
getAvl201: getAvl201,
getCommand: getCommand,
getPicture: getPicture,
getCommandError: getCommandError
getCommandError: getCommandError,
setCache: setCache
};

0 comments on commit 78599e0

Please sign in to comment.