Skip to content

Commit 09b66e3

Browse files
committed
fix bug
1 parent f4f9a8d commit 09b66e3

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

GPS.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323

2424
function GPS(board, rx, tx) {
2525
Module.call(this);
26-
2726
this._type = 'GPS';
2827
this._board = board;
2928
this._rx = rx;
3029
this._tx = tx;
3130
this._longitude = null;
3231
this._latitude = null;
32+
this._date = null;
3333
this._time = null;
3434
this._lastRecv = null;
3535
this._readTimer = null;
3636
this._readCallback = function () {};
37-
3837
this._board.on(BoardEvent.BEFOREDISCONNECT, this.stopRead.bind(this));
3938
this._messageHandler = onMessage.bind(this);
4039
this._board.on(BoardEvent.ERROR, this.stopRead.bind(this));
40+
board.send([0xF0, 0x04, 0x0C, 0x0 /*init*/ , rx, tx, 0xF7]);
4141
}
4242

4343
function onMessage(event) {
@@ -50,18 +50,34 @@
5050
}
5151
}
5252

53+
function toDateTime(date, time) {
54+
var t = Date.parse(date + ' ' + time) + 8 * 60 * 60 * 1000; //GMT+8
55+
var date = new Date(t);
56+
var hours = date.getHours();
57+
var minutes = "0" + date.getMinutes();
58+
var seconds = "0" + date.getSeconds();
59+
var time = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
60+
var mm = date.getMonth() + 1;
61+
var dd = "0" + date.getDate();
62+
var yyyy = date.getFullYear();
63+
var date = mm + '/' + dd.substr(-2) + '/' + yyyy;
64+
return [date, time];
65+
}
66+
5367
function processGPSData(self, data) {
5468
var str = '';
55-
for (var i = 4; i < data.length; i++) {
69+
for (var i = 3; i < data.length; i++) {
5670
str += String.fromCharCode(data[i]);
5771
}
5872
str = str.split(' ');
5973
var location = str[0].split(',');
6074
self._lastRecv = Date.now();
6175
self._longitude = location[0];
6276
self._latitude = location[1];
63-
self._time = str[1];
64-
self.emit(GPSEvent.READ, location[0], location[1], str[1]);
77+
var date_time = toDateTime(str[1], str[2]);
78+
self._date = date_time[0];
79+
self._time = date_time[1];
80+
self.emit(GPSEvent.READ, location[0], location[1], date_time[0], date_time[1]);
6581
}
6682

6783
GPS.prototype = proto = Object.create(Module.prototype, {
@@ -78,6 +94,11 @@
7894
return this._latitude;
7995
}
8096
},
97+
date: {
98+
get: function () {
99+
return this._date;
100+
}
101+
},
81102
time: {
82103
get: function () {
83104
return this._time;
@@ -92,20 +113,21 @@
92113
self.stopRead();
93114

94115
if (typeof callback === 'function') {
95-
self._readCallback = function (longitude, latitude, time) {
96-
self._location = location;
116+
self._readCallback = function (longitude, latitude, date, time) {
117+
self._date = date;
97118
self._time = time;
98119
callback({
99120
longitude: longitude,
100121
latitude: latitude,
122+
date: date,
101123
time: time
102124
});
103125
};
104126
self._board.on(BoardEvent.SYSEX_MESSAGE, self._messageHandler);
105127
self.on(GPSEvent.READ, self._readCallback);
106128

107129
timer = function () {
108-
self._board.sendSysex(GPS_MESSAGE[0], [GPS_MESSAGE[1]]);
130+
self._board.sendSysex(GPS_MESSAGE[0], [GPS_MESSAGE[1], 1]);
109131
if (interval) {
110132
interval = Math.max(interval, MIN_READ_INTERVAL);
111133
if (self._lastRecv === null || Date.now() - self._lastRecv < 5 * interval) {
@@ -123,8 +145,10 @@
123145
} else {
124146
return new Promise(function (resolve, reject) {
125147
self.read(function (data) {
126-
self._location = data.location;
148+
self._longitude = data.longitude;
149+
self._latitude = data.latitude;
127150
self._time = data.time;
151+
self._date = data.date;
128152
setTimeout(function () {
129153
resolve(data);
130154
}, MIN_RESPONSE_TIME);

blockly/blocks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Blockly.Blocks['gps_read'] = {
5555
this.setNextStatement(true, null);
5656
this.setColour(65);
5757
this.setTooltip('');
58-
this.setHelpUrl('http://www.example.com/');
58+
this.setHelpUrl('https://webduino.io');
5959
}
6060
};
6161

@@ -65,10 +65,10 @@ Blockly.Blocks['gps_get_data'] = {
6565
this.appendDummyInput()
6666
.appendField(new Blockly.FieldVariable("gps"), "gps")
6767
.appendField("所測得目前的")
68-
.appendField(new Blockly.FieldDropdown([["經度", "longitude"], ["緯度", "latitude"], ["時間", "time"]]), "dataType");
68+
.appendField(new Blockly.FieldDropdown([["經度", "longitude"], ["緯度", "latitude"], ["日期", "date"], ["時間", "time"]]), "dataType");
6969
this.setOutput(true, null);
7070
this.setColour(20);
7171
this.setTooltip('');
72-
this.setHelpUrl('http://www.example.com/');
72+
this.setHelpUrl('https://webduino.io');
7373
}
7474
};

0 commit comments

Comments
 (0)