-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (173 loc) · 5.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var SmyrnaSentence = (function(){
'use strict';
/**
* Kipyo tracking sentence
* $KPYT,1,fused:root,10,38.467416,27.159084,1423167000,90,90
*
* Kipyo geofencing sentence
* $KPYG,1,fused:root,10,38.467416,27.159084,1423167000,90,90,10,1
*/
var fieldSeperator = ',';
var sentenceSeperator = ';';
/**
* Kipyo tracking sentence : $KPYT
* $KPYT includes 9 tokens (9 fields about tracking)
*
* @Identifier
* @Device id
* @Provider
* @Accuracy
* @Latitude
* @Longitude
* @Date
* @Speed
* @Bearing
*/
var kpytTokenLength = 9;
var kpytIdentifier = 'KPYT';
/**
* Kipyo geofencing sentence: $KPYG
* $KPYG includes 11 tokens (11 fields about tracking)
*
* @Identifier
* @Device id
* @Provider
* @Accuracy
* @Latitude
* @Longitude
* @Date
* @Speed
* @Bearing
* @Geofence id
* @Geofencing type
*/
var kpygTokenLength = 11;
var kpygIdentifier = 'KPYG';
var protocol = {};
protocol.parse = function(sentence){
if((typeof sentence)!== 'string') throw new Error('sentence parameter is not a string');
if(sentence.length === 0) throw new Error('sentence length can not be 0');
var merged = sentence.split(sentenceSeperator);
if(merged.length === 1) return protocol.parseSingle(sentence);
if(merged.length > 1) return protocol.parseMerged(merged);
throw new Error('sentence parameter is not a string');
};
protocol.kpyt = function(data){
var k = [];
k.push('$KPYT');
k.push(data.deviceId);
k.push(data.provider);
k.push(data.accuracy);
k.push(data.latitude.toFixed(6));
k.push(data.longitude.toFixed(6));
k.push(data.date);
k.push(data.speed);
k.push(data.bearing);
var kpyt = k.join();
return kpyt;
};
protocol.kpyg = function(data){
var k = [];
k.push('$KPYG');
k.push(data.deviceId);
k.push(data.provider);
k.push(data.accuracy);
k.push(data.latitude.toFixed(6));
k.push(data.longitude.toFixed(6));
k.push(data.date);
k.push(data.speed);
k.push(data.bearing);
k.push(data.geofenceId);
k.push(data.geofenceType);
var kpyg = k.join();
return kpyg;
};
protocol.parseMerged = function(merged){
/**
* Multi (merged)
* $KPYT,1,fused:root,10,38.467416,27.159084,1423167000,90,90;
* $KPYT,1,fused:root,10,38.467332,27.159461,1423167010,88,90;
* $KPYT,1,fused:root,10,38.467227,27.159606,1423167020,85,90;
* $KPYT,1,fused:root,10,38.467148,27.159727,1423167030,88,90;
* $KPYT,1,fused:root,10,38.467048,27.159889,1423174140,92,90
*/
var sentences = [];
for(var i = 0; i < merged.length; i++){
sentences.push(protocol.parseSingle(merged[i]));
}
return sentences;
};
protocol.parseSingle = function(sentence){
/**
* Single
* $KPYT,1,fused:root,10,38.467416,27.159084,1423167000,90,90
*/
var tokens = sentence.split(fieldSeperator);
if(tokens.length !== kpytTokenLength && tokens.length !== kpygTokenLength)
throw new Error('Token length error');
var identifier = tokens[0].substring(1);
if(identifier !== kpytIdentifier && identifier !== kpygIdentifier)
throw new Error('Identifier error');
/* Kipyo tracking sentence $KPYT
1. @Identifier
2. @Device id
3. @Provider
4. @Accuracy
5. @Latitude
6. @Longitude
7. @Date
8. @Speed
9. @Bearing
*/
if(identifier === kpytIdentifier){
if(tokens.length === kpytTokenLength){
return {
identifier: identifier,
device: tokens[1],
provider: tokens[2],
accuracy: tokens[3],
latitude: tokens[4],
longitude: tokens[5],
date: tokens[6],
speed: tokens[7],
bearing: tokens[8]
};
}
}
/* Kipyo geofencing sentence $KPYG
1. @Identifier
2. @Device id
3. @Provider
4. @Accuracy
5. @Latitude
6. @Longitude
7. @Date
8. @Speed
9. @Bearing
10. @Geofence id
11. @Geofencing type
*/
if(identifier === kpygIdentifier){
if(tokens.length === kpygTokenLength){
return {
identifier: identifier,
device: tokens[1],
provider: tokens[2],
accuracy: tokens[3],
latitude: tokens[4],
longitude: tokens[5],
date: tokens[6],
speed: tokens[7],
bearing: tokens[8],
geofence: tokens[9],
geotype: tokens[10]
};
}
}
throw new Error('Sentence cant recognized');
};
return protocol;
}());
var hasModule = (typeof module !== 'undefined' && module.exports && typeof require !== 'undefined');
if (hasModule) module.exports = SmyrnaSentence;
else window.SmyrnaSentence = SmyrnaSentence;