Last active
December 29, 2015 09:59
-
-
Save iddar/7654033 to your computer and use it in GitHub Desktop.
Pequeño modulo para node.js en el que tradusco una cadena NMEA al formato del estandar HTML5 la cadena puede venir desde un gps serial, el modulo genera eventos al compretar el parse de los datos o al encontrar un error en la cadena de datos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
events = require 'events' | |
class gpsparse | |
gpsparse.prototype = new events.EventEmitter | |
errors = | |
parse: "Error de conexion del GPS." | |
format: "Error al codificar latlng" | |
flags = | |
lat: 2 | |
lng: 3 | |
data: (@nmea) -> | |
if nmea.search('GPRMC') is 1 | |
try | |
position = format nmea | |
@emit 'found', position | |
catch err | |
@emit 'errors', err | |
return | |
#Retorna un objeto con los datos del estring "GPRMC" | |
format = (nmea) -> | |
item = nmea.split ',' | |
throw errors.parse if !item[3] | |
{ #return | |
id: item[0] | |
hora: item[1] | |
estado: item[2] | |
latlng: calc item[3],item[4],item[5],item[6] | |
vel: item[7] | |
curso: item[8] | |
fecha: item[9] | |
} | |
#Retorna lat y lng con un formato compatible con HTML5 | |
calc = (lat,clat,lng,clng) -> | |
lat = lat.toCoords flags.lat, clat | |
lng = lng.toCoords flags.lng, clng | |
{ #return | |
lat: lat | |
lng: lng | |
} | |
String::toCoords = (latorlng, part) -> | |
coords = parseFloat @substr(latorlng, @length) | |
coords /= 60 | |
coords += parseInt @substr(0, latorlng) | |
coords *= -1 if part is 'W' or part is 'S' | |
throw errors.format if isNaN coords | |
coords | |
String::toFloat = -> | |
parseFloat @ | |
String::toInt = -> | |
parseInt @ | |
#Retorna una instacia del modulo | |
module.exports = new gpsparse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "item", | |
"hora": "item", | |
"estado": "item", | |
"latlng": { | |
"lat": 19.4414455, | |
"lng": -99.2037539 | |
}, | |
"vel": "item", | |
"curso": "item", | |
"fecha": "item", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment