From 9851328c69f3b8701fe25c8a2c4b3ca2a84aca86 Mon Sep 17 00:00:00 2001 From: msdeibel Date: Thu, 9 Apr 2020 16:10:10 +0200 Subject: [PATCH] Rotate log files once a week --- apps.json | 2 +- apps/batchart/ChangeLog | 3 ++- apps/batchart/app-icon.js | 2 +- apps/batchart/widget.js | 55 +++++++++++++++++++++++---------------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/apps.json b/apps.json index beddd2878..0347dd0c1 100644 --- a/apps.json +++ b/apps.json @@ -1136,7 +1136,7 @@ "name": "Battery Chart", "shortName":"BatChart", "icon": "app.png", - "version":"0.02", + "version":"0.03", "description": "A widget and an app for recording and visualizing battery percentage over time.", "tags": "app,widget,battery,time,record,chart,tool", "storage": [ diff --git a/apps/batchart/ChangeLog b/apps/batchart/ChangeLog index d885f8fb0..1b77ff82f 100644 --- a/apps/batchart/ChangeLog +++ b/apps/batchart/ChangeLog @@ -1,2 +1,3 @@ 0.01: New app and widget -0.02: Widget stores data to file (1 dataset/min) +0.02: Widget stores data to file (1 dataset/10min) +0.03: Rotate log files once a week. diff --git a/apps/batchart/app-icon.js b/apps/batchart/app-icon.js index 0841ea920..b41629e01 100644 --- a/apps/batchart/app-icon.js +++ b/apps/batchart/app-icon.js @@ -1 +1 @@ -require("heatshrink").decompress(atob("mEwxH+AH4A/AH4AS64AIF/4pZABYuuGDIv/F/4v/F9+Gw0rAQIASF7YxTF7cxwAvtrdVF9qQTF/4vMYCQvcYCQvcSCQvdqpgQF7oEBYJ4veAoNbF9uGmMrrgvsw2AGILFKF8IACrYxJF8gxDSowvmBwWAF9oPGF9NbmIvtCAovqMAgvqCIgvrrdVF9oSDF9iPuF7crACxf/F++wFqmG2AvXGCouZAH4A/AGY")) \ No newline at end of file +require("heatshrink").decompress(atob("mEwxH+AH4A/AH4AS64AIF/4pZABYuuGDIv/F/4v/F9+Gw0rAQIASF7YxTF7cxwAvtrdVF9qQTF/4vMYCQvcYCQvcSCQvdqpgQF7oEBYJ4veAoNbF9uGmMrrgvsw2AGILFKF8IACrYxJF8gxDSowvmBwWAF9oPGF9NbmIvtCAovqMAgvqCIgvrrdVF9oSDF9iPuF7crACxf/F++wFqmG2AvXGCouZAH4A/AGY")) diff --git a/apps/batchart/widget.js b/apps/batchart/widget.js index b002da5d9..2e2f43cdf 100644 --- a/apps/batchart/widget.js +++ b/apps/batchart/widget.js @@ -6,7 +6,8 @@ bluetooth: 4, gps: 8, hrm: 16 - } + }; + var settings = {}; var batChartFile; // file for battery percentage recording const recordingInterval10Min = 60 * 10 * 1000; @@ -20,26 +21,6 @@ g.drawString("BC", this.x, this.y); } - // Called by the heart app to reload settings and decide what's - function reload() { - WIDGETS["batchart"].width = 24; - - // Check if the data file exists, if not try to create it. - var batChartFileCheck = require("Storage").open("batchart.dat", "r"); - if (!batChartFileCheck) - if (!require("Storage").write("batchart.dat", "")) - //Only continue if the file was created - return; - - recordingInterval = setInterval(() => { - var batChartFileAppend = require("Storage").open("batchart.dat", "a"); - if (batChartFileAppend) { - console.log([getTime().toFixed(0), E.getBattery(), E.getTemperature(), getEnabledConsumersValue()].join(",")); - batChartFileAppend.write([getTime().toFixed(0),E.getBattery].join(",")+"\n"); - } - }, recordingInterval1Min) - } - function getEnabledConsumersValue() { var enabledConsumers = switchableConsumers.none; @@ -54,10 +35,40 @@ // enabledConsumers = enabledConsumers | switchableConsumers.gps; // if (Bangle.isHrmOn()) // enabledConsumers = enabledConsumers | switchableConsumers.hrm; - + return enabledConsumers; } + function logBatteryData() { + const previousWriteLogName = "bcprvday"; + const previousWriteDay = require("Storage").read(previousWriteLogName); + const currentWriteDay = new Date().getDay(); + + const logFileName = "bclog" + currentWriteDay; + + // Change log target on day change + if (previousWriteDay != currentWriteDay) { + //Remove a log file containing data from a week ago + require("Storage").erase(logFileName); + require("Storage").write(previousWriteLogName, currentWriteDay); + } + + var bcLogFileA = require("Storage").open(logFileName, "a"); + if (bcLogFileA) { + console.log([getTime().toFixed(0), E.getBattery(), E.getTemperature(), getEnabledConsumersValue()].join(",")); + bcLogFileA.write([[getTime().toFixed(0), E.getBattery(), E.getTemperature(), getEnabledConsumersValue()].join(",")].join(",")+"\n"); + } + } + + // Called by the heart app to reload settings and decide what's + function reload() { + WIDGETS["batchart"].width = 24; + + recordingInterval = setInterval(logBatteryData, recordingInterval10Min); + + logBatteryData(); + } + // add the widget WIDGETS["batchart"]={area:"tl",width:24,draw:draw,reload:function() { reload();