1+ + ( function ( factory ) {
2+ if ( typeof exports === 'undefined' ) {
3+ factory ( webduino || { } ) ;
4+ } else {
5+ module . exports = factory ;
6+ }
7+ } ( function ( scope ) {
8+ 'use strict' ;
9+
10+ var Module = scope . Module ,
11+ BoardEvent = scope . BoardEvent ,
12+ proto ;
13+
14+ var GPS_MESSAGE = [ 0x04 , 0x0C ] ,
15+ MIN_READ_INTERVAL = 1000 ,
16+ MIN_RESPONSE_TIME = 30 ,
17+ RETRY_INTERVAL = 6000 ;
18+
19+ var GPSEvent = {
20+ READ : 'read' ,
21+ READ_ERROR : 'readError'
22+ } ;
23+
24+ function GPS ( board , rx , tx ) {
25+ Module . call ( this ) ;
26+
27+ this . _type = 'GPS' ;
28+ this . _board = board ;
29+ this . _rx = rx ;
30+ this . _tx = tx ;
31+ this . _longitude = null ;
32+ this . _latitude = null ;
33+ this . _time = null ;
34+ this . _lastRecv = null ;
35+ this . _readTimer = null ;
36+ this . _readCallback = function ( ) { } ;
37+
38+ this . _board . on ( BoardEvent . BEFOREDISCONNECT , this . stopRead . bind ( this ) ) ;
39+ this . _messageHandler = onMessage . bind ( this ) ;
40+ this . _board . on ( BoardEvent . ERROR , this . stopRead . bind ( this ) ) ;
41+ }
42+
43+ function onMessage ( event ) {
44+ var message = event . message ;
45+
46+ if ( message [ 0 ] !== GPS_MESSAGE [ 0 ] || message [ 1 ] !== GPS_MESSAGE [ 1 ] ) {
47+ return ;
48+ } else {
49+ processGPSData ( this , message ) ;
50+ }
51+ }
52+
53+ function processGPSData ( self , data ) {
54+ var str = '' ;
55+ for ( var i = 4 ; i < data . length ; i ++ ) {
56+ str += String . fromCharCode ( data [ i ] ) ;
57+ }
58+ str = str . split ( ' ' ) ;
59+ var location = str [ 0 ] . split ( ',' ) ;
60+ self . _lastRecv = Date . now ( ) ;
61+ self . _longitude = location [ 0 ] ;
62+ self . _latitude = location [ 1 ] ;
63+ self . _time = str [ 1 ] ;
64+ self . emit ( GPSEvent . READ , location [ 0 ] , location [ 1 ] , str [ 1 ] ) ;
65+ }
66+
67+ GPS . prototype = proto = Object . create ( Module . prototype , {
68+ constructor : {
69+ value : GPS
70+ } ,
71+ longitude : {
72+ get : function ( ) {
73+ return this . _longitude ;
74+ }
75+ } ,
76+ latitude : {
77+ get : function ( ) {
78+ return this . _latitude ;
79+ }
80+ } ,
81+ time : {
82+ get : function ( ) {
83+ return this . _time ;
84+ }
85+ }
86+ } ) ;
87+
88+ proto . read = function ( callback , interval ) {
89+ var self = this ,
90+ timer ;
91+
92+ self . stopRead ( ) ;
93+
94+ if ( typeof callback === 'function' ) {
95+ self . _readCallback = function ( longitude , latitude , time ) {
96+ self . _location = location ;
97+ self . _time = time ;
98+ callback ( {
99+ longitude : longitude ,
100+ latitude : latitude ,
101+ time : time
102+ } ) ;
103+ } ;
104+ self . _board . on ( BoardEvent . SYSEX_MESSAGE , self . _messageHandler ) ;
105+ self . on ( GPSEvent . READ , self . _readCallback ) ;
106+
107+ timer = function ( ) {
108+ self . _board . sendSysex ( GPS_MESSAGE [ 0 ] , [ GPS_MESSAGE [ 1 ] ] ) ;
109+ if ( interval ) {
110+ interval = Math . max ( interval , MIN_READ_INTERVAL ) ;
111+ if ( self . _lastRecv === null || Date . now ( ) - self . _lastRecv < 5 * interval ) {
112+ self . _readTimer = setTimeout ( timer , interval ) ;
113+ } else {
114+ self . stopRead ( ) ;
115+ setTimeout ( function ( ) {
116+ self . read ( callback , interval ) ;
117+ } , RETRY_INTERVAL ) ;
118+ }
119+ }
120+ } ;
121+
122+ timer ( ) ;
123+ } else {
124+ return new Promise ( function ( resolve , reject ) {
125+ self . read ( function ( data ) {
126+ self . _location = data . location ;
127+ self . _time = data . time ;
128+ setTimeout ( function ( ) {
129+ resolve ( data ) ;
130+ } , MIN_RESPONSE_TIME ) ;
131+ } ) ;
132+ } ) ;
133+ }
134+ } ;
135+
136+ proto . stopRead = function ( ) {
137+ this . removeListener ( GPSEvent . READ , this . _readCallback ) ;
138+ this . _board . removeListener ( BoardEvent . SYSEX_MESSAGE , this . _messageHandler ) ;
139+ this . _lastRecv = null ;
140+
141+ if ( this . _readTimer ) {
142+ clearTimeout ( this . _readTimer ) ;
143+ delete this . _readTimer ;
144+ }
145+ } ;
146+
147+ scope . module . GPSEvent = GPSEvent ;
148+ scope . module . GPS = GPS ;
149+ } ) ) ;
0 commit comments