Skip to content

Commit 1d341ee

Browse files
authored
Change filename once we know the time (#173)
* Change filename once we know the time (fixes #95) * Use a different name if the file is in upload already (fixes #113) * Do not write file already with header creation to avoid empty files (fixes #62)
1 parent c50bef5 commit 1d341ee

File tree

11 files changed

+275
-340
lines changed

11 files changed

+275
-340
lines changed

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/software/firmware/csv_format.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ understand.
3737
| `DistanceSensorsUsed` | `HC-SR04/JSN-SR04T` | enum currently only one possible value |
3838
| `DeviceId` | `affe` | internal Id of the OBS |
3939
| `PresetId` | `Wade` | Id to identify the selected preset. A owner might define multiple presets |
40+
| `BluetoothEnabled` | `1` | 1 if bluetooth is enabled, 0 otherwise
4041

4142
## CSV
4243

src/OpenBikeSensorFirmware.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void setup() {
315315
}
316316
}
317317

318-
if (gps.satellites.value() == config.satsForFix) {
318+
if (gps.satellites.value() >= config.satsForFix) {
319319
Serial.print("Got GPS Fix: ");
320320
Serial.println(String(gps.satellites.value()));
321321
displayTest->showTextOnGrid(2, 5, "Got GPS Fix");
@@ -466,7 +466,7 @@ void loop() {
466466
&& dataBuffer.isEmpty()) {
467467
Serial.write("Empty Buffer, writing directly ");
468468
if (writer) {
469-
writer->writeDataBuffered(currentSet);
469+
writer->append(*currentSet);
470470
}
471471
delete currentSet;
472472
} else {
@@ -489,7 +489,7 @@ void loop() {
489489
DataSet* dataset = dataBuffer.shift();
490490
if (dataset->confirmedDistances.size() == 0) {
491491
if (writer) {
492-
writer->writeDataBuffered(dataset);
492+
writer->append(*dataset);
493493
}
494494
}
495495
// write record as many times as we have confirmed values
@@ -499,15 +499,15 @@ void loop() {
499499
dataset->confirmed = dataset->confirmedDistancesTimeOffset[i];
500500
confirmedMeasurements++;
501501
if (writer) {
502-
writer->writeDataBuffered(dataset);
502+
writer->append(*dataset);
503503
}
504504
}
505505
delete dataset;
506506
}
507507
if (writer) { // "flush"
508-
writer->writeDataToSD();
508+
writer->flush();
509509
}
510-
Serial.printf(">>> writeDataToSD - reset <<<");
510+
Serial.printf(">>> flush - reset <<<");
511511
transmitConfirmedData = false;
512512
// back to normal display mode
513513
if (config.displayConfig & DisplayInvert) {
@@ -522,7 +522,7 @@ void loop() {
522522
DataSet* dataset = dataBuffer.shift();
523523
Serial.printf("data buffer full, writing set to file buffer\n");
524524
if (writer) {
525-
writer->writeDataBuffered(dataset);
525+
writer->append(*dataset);
526526
}
527527
// we are about to delete the to be confirmed dataset, so take care for this.
528528
if (datasetToConfirm == dataset) {

src/VoltageMeter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "OpenBikeSensorFirmware.h"
2-
1+
#include "VoltageMeter.h"
2+
#include "globals.h"
33

44
/* Class to encapsulate voltage readings and smoothing thereof.
55
* Still the values are not as accurate as expected, and it is not

src/configServer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,13 @@ void startServer() {
708708
if(uploader::instance()->upload(file.name())) {
709709

710710
SDFileSystem.mkdir("/uploaded");
711-
SDFileSystem.rename(file.name(),String("/uploaded")+file.name());
711+
int i = 0;
712+
while (!SDFileSystem.rename(file.name(), String("/uploaded") + file.name() + (i == 0 ? "" : String(i)))) {
713+
i++;
714+
if (i > 100) {
715+
break;
716+
}
717+
}
712718
html += String(file.name());
713719
html += "<br>";
714720
}

src/uploader.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "config.h"
2424
#include "globals.h"
2525
#include "utils/multipart.h"
26+
#include "writer.h"
2627

2728
#include <WiFi.h>
2829
#include <WiFiMulti.h>
@@ -101,17 +102,16 @@ uploader::uploader() {
101102
*
102103
*/
103104
bool uploader::upload(const String& fileName) {
104-
int number=0;
105-
if(fileName.substring(0,7)!="/sensor") {
105+
if(fileName.substring(0,7) != "/sensor"
106+
&& !fileName.endsWith(CSVFileWriter::EXTENSION)) {
106107
Serial.printf(("not sending " + fileName + "\n").c_str());
107108
return false;
108109
}
109110
Serial.printf(("sending " + fileName + "\n").c_str());
110-
sscanf(fileName.c_str(),"/sensorData%d",&number);
111-
112111
File csvFile = SD.open(fileName.c_str(), "r");
113112
if (csvFile) {
114113
HTTPClient https;
114+
https.setTimeout(30 * 1000); // give the api some time
115115
https.setUserAgent(String("OBS/") + String(OBSVersion));
116116
boolean res = false;
117117

@@ -122,7 +122,8 @@ bool uploader::upload(const String& fileName) {
122122

123123
if (res) { // HTTPS
124124
MultipartStream mp(&https);
125-
MultipartDataString title("title", "AutoUpload " + String(number));
125+
MultipartDataString title("title",
126+
"AutoUpload " + fileName.substring(1, 25));
126127
mp.add(title);
127128
MultipartDataString description("description", "Uploaded with OpenBikeSensor " + String(OBSVersion));
128129
mp.add(description);
@@ -134,11 +135,15 @@ bool uploader::upload(const String& fileName) {
134135
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
135136
String payload = https.getString();
136137
Serial.println(payload);
138+
https.end();
139+
csvFile.close();
137140
return false;
138141
}
139-
https.end();
142+
csvFile.close();
140143
} else {
141144
Serial.printf("[HTTPS] Unable to connect\n");
145+
https.end();
146+
csvFile.close();
142147
return false;
143148
}
144149
} else {

src/utils/file.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright (C) 2019 Zweirat
3+
Contact: https://openbikesensor.org
4+
5+
This file is part of the OpenBikeSensor project.
6+
7+
The OpenBikeSensor sensor firmware is free software: you can redistribute
8+
it and/or modify it under the terms of the GNU General Public License as
9+
published by the Free Software Foundation, either version 3 of the License,
10+
or (at your option) any later version.
11+
12+
The OpenBikeSensor sensor firmware is distributed in the hope that it will
13+
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15+
Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License along with
18+
the OpenBikeSensor sensor firmware. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
#include "file.h"
21+
22+
bool FileUtil::appendFile(fs::FS &fs, const char * path, const char * message) {
23+
bool result = false;
24+
Serial.printf("Appending to file: %s\n", path);
25+
26+
File file = fs.open(path, FILE_APPEND);
27+
if (!file) {
28+
Serial.println("Failed to open file for appending");
29+
return false;
30+
}
31+
if (file.print(message)) {
32+
result = true;
33+
Serial.println("Message appended");
34+
} else {
35+
Serial.println("Append failed");
36+
}
37+
file.close();
38+
return result;
39+
}

src/utils/file.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (C) 2019 Zweirat
3+
Contact: https://openbikesensor.org
4+
5+
This file is part of the OpenBikeSensor project.
6+
7+
The OpenBikeSensor sensor firmware is free software: you can redistribute
8+
it and/or modify it under the terms of the GNU General Public License as
9+
published by the Free Software Foundation, either version 3 of the License,
10+
or (at your option) any later version.
11+
12+
The OpenBikeSensor sensor firmware is distributed in the hope that it will
13+
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15+
Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License along with
18+
the OpenBikeSensor sensor firmware. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
#ifndef OPENBIKESENSORFIRMWARE_FILE_H
21+
#define OPENBIKESENSORFIRMWARE_FILE_H
22+
23+
#include <FS.h>
24+
25+
class FileUtil {
26+
public:
27+
static bool appendFile(fs::FS &fs, const char * path, const char * message);
28+
};
29+
30+
#endif //OPENBIKESENSORFIRMWARE_FILE_H

src/utils/streams.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ size_t StreamOfStreams::readBytes(char *buffer, size_t length) {
9696
}
9797

9898
Stream *StreamOfStreams::getCurrent() {
99-
// FIXME We assume available is 0 only at the very end!
10099
if (current == nullptr || current->available() == 0) {
101100
current = getNext();
102101
}
@@ -105,9 +104,8 @@ Stream *StreamOfStreams::getCurrent() {
105104

106105
Stream *StreamOfStreams::getNext() {
107106
if (pos < streams.size()) {
108-
// TODO: there should be a better method in this vector
109-
log_d("About to switch to stream %d/%d", pos, streams.size());
110107
current = streams.at(pos++);
108+
log_d("About to switch to stream %d/%d", pos, streams.size());
111109
} else {
112110
current = &EMPTY_STREAM;
113111
}

0 commit comments

Comments
 (0)