GPS.min.js 7.4 KB

1234567891011121314151617181920212223
  1. /*
  2. https://github.com/infusion/GPS.js
  3. GPS.js v0.6.1 26/01/2016
  4. Copyright (c) 2016, Robert Eisele (robert@xarg.org)
  5. Dual licensed under the MIT or GPL Version 2 licenses.
  6. */
  7. (function(x){function k(b,a){if(""===b)return null;var d=new Date;if(a){var c=a.slice(4),f=a.slice(2,4)-1,h=a.slice(0,2);4===c.length?d.setUTCFullYear(Number(c),Number(f),Number(h)):d.setUTCFullYear(Number("20"+c),Number(f),Number(h))}d.setUTCHours(Number(b.slice(0,2)));d.setUTCMinutes(Number(b.slice(2,4)));d.setUTCSeconds(Number(b.slice(4,6)));c=b.slice(7);f=c.length;h=0;0!==f&&(h=parseFloat(c)*Math.pow(10,3-f));d.setUTCMilliseconds(Number(h));return d}function l(b,a){if(""===b)return null;var d=
  8. 1;switch(a){case "S":d=-1;case "N":var c=2;break;case "W":d=-1;case "E":c=3}return d*(parseFloat(b.slice(0,c))+parseFloat(b.slice(c))/60)}function e(b){return""===b?null:parseFloat(b)}function q(b){return""===b?null:1.852*parseFloat(b)}function y(b){if(""===b)return null;switch(parseInt(b,10)){case 0:return null;case 1:return"fix";case 2:return"dgps-fix";case 3:return"pps-fix";case 4:return"rtk";case 5:return"rtk-float";case 6:return"estimated";case 7:return"manual";case 8:return"simulated"}throw Error("INVALID GGA FIX: "+
  9. b);}function r(b){switch(b){case "A":return"active";case "V":return"void";case "":return null}throw Error("INVALID RMC/GLL STATUS: "+b);}function n(b){switch(b){case "":return null;case "A":return"autonomous";case "D":return"differential";case "E":return"estimated";case "M":return"manual input";case "S":return"simulated";case "N":return"not valid";case "P":return"precise";case "R":return"rtk";case "F":return"rtk-float"}throw Error("INVALID FAA MODE: "+b);}function t(b,a){if("M"===a||""===a)return e(b);
  10. throw Error("Unknown unit: "+a);}function g(){if(!(this instanceof g))return new g;this.events={};this.state={errors:0,processed:0}}var m=Math.PI/180,p={},u={};g.prototype.events=null;g.prototype.state=null;g.mod={GGA:function(b,a){if(16!==a.length&&14!==a.length)throw Error("Invalid GGA length: "+b);return{time:k(a[1]),lat:l(a[2],a[3]),lon:l(a[4],a[5]),alt:t(a[9],a[10]),quality:y(a[6]),satellites:e(a[7]),hdop:e(a[8]),geoidal:t(a[11],a[12]),age:void 0===a[13]?null:e(a[13]),stationID:void 0===a[14]?
  11. null:e(a[14])}},GSA:function(b,a){if(19!==a.length&&20!==a.length)throw Error("Invalid GSA length: "+b);for(var d=[],c=3;15>c;c++)""!==a[c]&&d.push(parseInt(a[c],10));a:{c=a[1];switch(c){case "M":c="manual";break a;case "A":c="automatic";break a;case "":c=null;break a}throw Error("INVALID GSA MODE: "+c);}a:{var f=a[2];switch(f){case "1":case "":f=null;break a;case "2":f="2D";break a;case "3":f="3D";break a}throw Error("INVALID GSA FIX: "+f);}return{mode:c,fix:f,satellites:d,pdop:e(a[15]),hdop:e(a[16]),
  12. vdop:e(a[17]),systemId:19<a.length?e(a[18]):null}},RMC:function(b,a){if(13!==a.length&&14!==a.length&&15!==a.length)throw Error("Invalid RMC length: "+b);var d=k(a[1],a[9]),c=r(a[2]),f=l(a[3],a[4]),h=l(a[5],a[6]),z=q(a[7]),A=e(a[8]),v=a[10],w=a[11];return{time:d,status:c,lat:f,lon:h,speed:z,track:A,variation:""===v||""===w?null:parseFloat(v)*("W"===w?-1:1),faa:13<a.length?n(a[12]):null,navStatus:14<a.length?a[13]:null}},VTG:function(b,a){if(10!==a.length&&11!==a.length)throw Error("Invalid VTG length: "+
  13. b);if(""===a[2]&&""===a[8]&&""===a[6])return{track:null,trackMagetic:null,speed:null,faa:null};if("T"!==a[2])throw Error("Invalid VTG track mode: "+b);if("K"!==a[8]||"N"!==a[6])throw Error("Invalid VTG speed tag: "+b);return{track:e(a[1]),trackMagnetic:""===a[3]?null:e(a[3]),speed:q(a[5]),faa:11===a.length?n(a[9]):null}},GSV:function(b,a){if(0===a.length%4)throw Error("Invalid GSV length: "+b);for(var d=[],c=4;c<a.length-3;c+=4){var f=e(a[c]),h=e(a[c+3]);d.push({prn:f,elevation:e(a[c+1]),azimuth:e(a[c+
  14. 2]),snr:h,status:null!==f?null!==h?"tracking":"in view":null})}return{msgNumber:e(a[2]),msgsTotal:e(a[1]),satsInView:e(a[3]),satellites:d,signalId:2===a.length%4?e(a[a.length-2]):null}},GLL:function(b,a){if(9!==a.length&&8!==a.length)throw Error("Invalid GLL length: "+b);return{time:k(a[5]),status:r(a[6]),lat:l(a[1],a[2]),lon:l(a[3],a[4]),faa:9===a.length?n(a[7]):null}},ZDA:function(b,a){return{time:k(a[1],a[2]+a[3]+a[4])}},GST:function(b,a){if(10!==a.length)throw Error("Invalid GST length: "+b);
  15. return{time:k(a[1]),rms:e(a[2]),ellipseMajor:e(a[3]),ellipseMinor:e(a[4]),ellipseOrientation:e(a[5]),latitudeError:e(a[6]),longitudeError:e(a[7]),heightError:e(a[8])}},HDT:function(b,a){if(4!==a.length)throw Error("Invalid HDT length: "+b);return{heading:parseFloat(a[1]),trueNorth:"T"===a[2]}},GRS:function(b,a){if(18!==a.length)throw Error("Invalid GRS length: "+b);for(var d=[],c=3;14>=c;c++){var f=e(a[c]);null!==f&&d.push(f)}return{time:k(a[1]),mode:e(a[2]),res:d}},GBS:function(b,a){if(10!==a.length&&
  16. 12!==a.length)throw Error("Invalid GBS length: "+b);return{time:k(a[1]),errLat:e(a[2]),errLon:e(a[3]),errAlt:e(a[4]),failedSat:e(a[5]),probFailedSat:e(a[6]),biasFailedSat:e(a[7]),stdFailedSat:e(a[8]),systemId:12===a.length?e(a[9]):null,signalId:12===a.length?e(a[10]):null}},GNS:function(b,a){if(14!==a.length&&15!==a.length)throw Error("Invalid GNS length: "+b);return{time:k(a[1]),lat:l(a[2],a[3]),lon:l(a[4],a[5]),mode:a[6],satsUsed:e(a[7]),hdop:e(a[8]),alt:e(a[9]),sep:e(a[10]),diffAge:e(a[11]),diffStation:e(a[12]),
  17. navStatus:15===a.length?a[13]:null}}};g.Parse=function(b){if("string"!==typeof b)return!1;var a=b.split(","),d=a.pop();if(2>a.length||"$"!==b.charAt(0)||-1===d.indexOf("*"))return!1;d=d.split("*");a.push(d[0]);a.push(d[1]);a[0]=a[0].slice(3);if(void 0!==g.mod[a[0]]){d=this.mod[a[0]](b,a);d.raw=b;for(var c=0,f=1;f<b.length;f++){var h=b.charCodeAt(f);if(42===h)break;c^=h}d.valid=c===parseInt(a[a.length-1],16);d.type=a[0];return d}return!1};g.Heading=function(b,a,d,c){a=(c-a)*m;b*=m;d*=m;c=Math.cos(d);
  18. return(180*Math.atan2(Math.sin(a)*c,Math.cos(b)*Math.sin(d)-Math.sin(b)*c*Math.cos(a))/Math.PI+360)%360};g.Distance=function(b,a,d,c){var f=(d-b)*m*.5;a=(c-a)*m*.5;b*=m;d*=m;f=Math.sin(f);a=Math.sin(a);return 12745.6*Math.asin(Math.sqrt(f*f+Math.cos(b)*Math.cos(d)*a*a))};g.TotalDistance=function(b){if(2>b.length)return 0;for(var a=0,d=0;d<b.length-1;d++){var c=b[d],f=b[d+1];a+=g.Distance(c.lat,c.lon,f.lat,f.lon)}return a};g.prototype.update=function(b){b=g.Parse(b);this.state.processed++;if(!1===
  19. b)return this.state.errors++,!1;var a=this.state;if("RMC"===b.type||"GGA"===b.type||"GLL"===b.type||"GNS"===b.type)a.time=b.time,a.lat=b.lat,a.lon=b.lon;"ZDA"===b.type&&(a.time=b.time);"GGA"===b.type&&(a.alt=b.alt);"RMC"===b.type&&(a.speed=b.speed,a.track=b.track);"GSA"===b.type&&(a.satsActive=b.satellites,a.fix=b.fix,a.hdop=b.hdop,a.pdop=b.pdop,a.vdop=b.vdop);if("GSV"===b.type){for(var d=(new Date).getTime(),c=b.satellites,f=0;f<c.length;f++){var h=c[f].g;u[h]=d;p[h]=c[f]}c=[];for(h in p)3E3>d-u[h]&&
  20. c.push(p[h]);a.satsVisible=c}this.emit("data",b);this.emit(b.type,b);return!0};g.prototype.partial="";g.prototype.updatePartial=function(b){for(this.partial+=b;;){b=this.partial.indexOf("\r\n");if(-1===b)break;var a=this.partial.slice(0,b);if("$"===a.charAt(0))try{this.update(a)}catch(d){throw this.partial="",Error(d);}this.partial=this.partial.slice(b+2)}};g.prototype.on=function(b,a){return void 0===this.events[b]?(this.events[b]=a,this):null};g.prototype.off=function(b){void 0!==this.events[b]&&
  21. (this.events[b]=void 0);return this};g.prototype.emit=function(b,a){void 0!==this.events[b]&&this.events[b].call(this,a)};"object"===typeof exports?(Object.defineProperty(g,"__esModule",{value:!0}),g["default"]=g,g.GPS=g,module.exports=g):x.GPS=g})(this);