Skip to content

Commit

Permalink
recorder: resume logs recorded within 4 hours
Browse files Browse the repository at this point in the history
We now look at the latest log's last timestamp - if it was within
four hours, we prompt the user to resume recording to that file.

Fixes espruino#3135
  • Loading branch information
bobrippling committed Dec 20, 2023
1 parent ba082f6 commit 9b35547
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/recorder/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@
0.36: When recording with 1 second periods, log time with one decimal.
0.37: 1 second periods + gps log => log when gps event is received, not with
setInterval.
0.38: Resume previous recorder logs, if within four hours - regardless of date
2 changes: 1 addition & 1 deletion apps/recorder/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "recorder",
"name": "Recorder",
"shortName": "Recorder",
"version": "0.37",
"version": "0.38",
"description": "Record GPS position, heart rate and more in the background, then download to your PC.",
"icon": "app.png",
"tags": "tool,outdoors,gps,widget,clkinfo",
Expand Down
36 changes: 31 additions & 5 deletions apps/recorder/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,36 @@
options = options||{};
if (isOn && !settings.recording) {
var date=(new Date()).toISOString().substr(0,10).replace(/-/g,""), trackNo=10;
function getTrackFilename() { return "recorder.log" + date + trackNo.toString(36) + ".csv"; }
if (!settings.file || !settings.file.startsWith("recorder.log" + date)) {
// if no filename set or date different, set up a new filename
settings.file = getTrackFilename();
function nextTrackFilename() { return "recorder.log" + date + trackNo.toString(36) + ".csv"; }
function mostRecentFile() {
const logs = require("Storage")
.list(/^recorder\.log/)
.map(x => x.replace(/^recorder\.log/, ""));

if(logs.length > 0){
logs.sort();
const latest = "recorder.log" + logs[logs.length-1].replace("\1", "");
const f = require("Storage").open(latest, "r");
const timeIdx = f.readLine().replace("\n", "").split(",").indexOf("Time");
if(timeIdx >= 0){
let last, tmp;
while(tmp = f.readLine())
last = tmp;

const when = new Date(last.replace("\n", "").split(",")[timeIdx]);
const now = new Date;
const age = now.getTime() - when.getTime();
if (age < 14400000) { // 4 hours
return latest;
}
}
}
}
const recentlyRecorded = mostRecentFile();
if (recentlyRecorded) {
settings.file = recentlyRecorded;
} else if (!settings.file) {
settings.file = nextTrackFilename();
}
var headers = require("Storage").open(settings.file,"r").readLine();
if (headers){ // if file exists
Expand Down Expand Up @@ -289,7 +315,7 @@
// new file - use the current date
var newFileName;
do { // while a file exists, add one to the letter after the date
newFileName = getTrackFilename();
newFileName = nextTrackFilename();
trackNo++;
} while (require("Storage").list(newFileName).length);
settings.file = newFileName;
Expand Down

0 comments on commit 9b35547

Please sign in to comment.