2020-04-08 20:28:24 +00:00
|
|
|
(() => {
|
2020-04-14 11:55:36 +00:00
|
|
|
const Storage = require("Storage");
|
|
|
|
|
|
|
|
const switchableConsumers = {
|
2020-04-09 06:19:38 +00:00
|
|
|
none: 0,
|
|
|
|
lcd: 1,
|
|
|
|
compass: 2,
|
|
|
|
bluetooth: 4,
|
|
|
|
gps: 8,
|
|
|
|
hrm: 16
|
2020-04-09 14:10:10 +00:00
|
|
|
};
|
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
var batChartFile; // file for battery percentage recording
|
2020-04-09 06:19:38 +00:00
|
|
|
const recordingInterval10Min = 60 * 10 * 1000;
|
2020-04-16 09:24:17 +00:00
|
|
|
const recordingInterval1Min = 60 * 1000; //For testing
|
|
|
|
const recordingInterval10S = 10 * 1000; //For testing
|
2020-04-08 20:28:24 +00:00
|
|
|
var recordingInterval = null;
|
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
var compassEventReceived = false;
|
|
|
|
var gpsEventReceived = false;
|
|
|
|
var hrmEventReceived = false;
|
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
// draw your widget
|
|
|
|
function draw() {
|
2020-04-14 20:15:15 +00:00
|
|
|
let x = this.x;
|
|
|
|
let y = this.y;
|
|
|
|
|
|
|
|
g.setColor(0, 1, 0);
|
2020-04-16 09:24:17 +00:00
|
|
|
g.fillPoly([x + 5, y, x + 5, y + 4, x + 1, y + 4, x + 1, y + 20, x + 18, y + 20, x + 18, y + 4, x + 13, y + 4, x + 13, y], true);
|
2020-04-14 20:15:15 +00:00
|
|
|
|
2020-04-16 09:24:17 +00:00
|
|
|
g.setColor(0, 0, 0);
|
|
|
|
g.drawPoly([x + 5, y + 6, x + 8, y + 12, x + 13, y + 12, x + 16, y + 18], false);
|
2020-04-14 20:15:15 +00:00
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
g.reset();
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:24:17 +00:00
|
|
|
function onMag() {
|
2020-04-14 15:29:24 +00:00
|
|
|
compassEventReceived = true;
|
|
|
|
// Stop handling events when no longer necessarry
|
|
|
|
Bangle.removeListener("mag", onMag);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onGps() {
|
|
|
|
gpsEventReceived = true;
|
|
|
|
Bangle.removeListener("GPS", onGps);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onHrm() {
|
2020-04-14 16:04:02 +00:00
|
|
|
hrmEventReceived = true;
|
2020-04-14 15:29:24 +00:00
|
|
|
Bangle.removeListener("HRM", onHrm);
|
|
|
|
}
|
|
|
|
|
2020-04-09 06:19:38 +00:00
|
|
|
function getEnabledConsumersValue() {
|
2020-04-16 09:24:17 +00:00
|
|
|
// Wait for an event from each of the devices to see if they are switched on
|
2020-04-09 06:19:38 +00:00
|
|
|
var enabledConsumers = switchableConsumers.none;
|
|
|
|
|
2020-04-14 15:29:24 +00:00
|
|
|
Bangle.on('mag', onMag);
|
|
|
|
Bangle.on('GPS', onGps);
|
|
|
|
Bangle.on('HRM', onHrm);
|
2020-04-14 05:48:07 +00:00
|
|
|
|
|
|
|
// Wait two seconds, that should be enough for each of the events to get raised once
|
2020-04-16 09:24:17 +00:00
|
|
|
setTimeout(() => {
|
2020-04-14 20:15:15 +00:00
|
|
|
Bangle.removeAllListeners();
|
2020-04-14 05:48:07 +00:00
|
|
|
}, 2000);
|
|
|
|
|
2020-04-09 06:19:38 +00:00
|
|
|
if (Bangle.isLCDOn())
|
|
|
|
enabledConsumers = enabledConsumers | switchableConsumers.lcd;
|
2020-04-14 05:48:07 +00:00
|
|
|
if (compassEventReceived)
|
|
|
|
enabledConsumers = enabledConsumers | switchableConsumers.compass;
|
|
|
|
if (gpsEventReceived)
|
|
|
|
enabledConsumers = enabledConsumers | switchableConsumers.gps;
|
|
|
|
if (hrmEventReceived)
|
|
|
|
enabledConsumers = enabledConsumers | switchableConsumers.hrm;
|
|
|
|
//if (Bangle.isBluetoothOn())
|
2020-04-09 06:19:38 +00:00
|
|
|
// enabledConsumers = enabledConsumers | switchableConsumers.bluetooth;
|
2020-04-14 19:02:39 +00:00
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
// Reset the event registration vars
|
|
|
|
compassEventReceived = false;
|
|
|
|
gpsEventReceived = false;
|
|
|
|
hrmEventReceived = false;
|
|
|
|
|
2020-04-14 20:15:15 +00:00
|
|
|
return enabledConsumers.toString();
|
2020-04-08 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:10:10 +00:00
|
|
|
function logBatteryData() {
|
|
|
|
const previousWriteLogName = "bcprvday";
|
2020-04-14 16:33:56 +00:00
|
|
|
const previousWriteDay = parseInt(Storage.open(previousWriteLogName, "r").readLine());
|
2020-04-09 14:10:10 +00:00
|
|
|
const currentWriteDay = new Date().getDay();
|
|
|
|
|
|
|
|
const logFileName = "bclog" + currentWriteDay;
|
|
|
|
|
|
|
|
// Change log target on day change
|
2020-04-16 09:24:17 +00:00
|
|
|
if (!isNaN(previousWriteDay) && previousWriteDay != currentWriteDay) {
|
2020-04-09 14:10:10 +00:00
|
|
|
//Remove a log file containing data from a week ago
|
2020-04-14 15:46:59 +00:00
|
|
|
Storage.open(logFileName, "r").erase();
|
2020-04-14 16:33:56 +00:00
|
|
|
Storage.open(previousWriteLogName, "w").write(parseInt(currentWriteDay));
|
2020-04-09 14:10:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
var bcLogFileA = Storage.open(logFileName, "a");
|
2020-04-09 14:10:10 +00:00
|
|
|
if (bcLogFileA) {
|
2020-04-14 20:15:15 +00:00
|
|
|
let logTime = getTime().toFixed(0);
|
|
|
|
let logPercent = E.getBattery();
|
|
|
|
let logTemperature = E.getTemperature();
|
|
|
|
let logConsumers = getEnabledConsumersValue();
|
|
|
|
|
|
|
|
let logString = [logTime, logPercent, logTemperature, logConsumers].join(",");
|
2020-04-14 19:20:09 +00:00
|
|
|
|
|
|
|
bcLogFileA.write(logString + "\n");
|
2020-04-09 14:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reload() {
|
2020-04-16 09:24:17 +00:00
|
|
|
WIDGETS.batchart.width = 24;
|
2020-04-09 14:10:10 +00:00
|
|
|
|
2020-04-14 20:15:15 +00:00
|
|
|
recordingInterval = setInterval(logBatteryData, recordingInterval10Min);
|
2020-04-09 14:10:10 +00:00
|
|
|
|
|
|
|
logBatteryData();
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
// add the widget
|
2020-04-16 09:24:17 +00:00
|
|
|
WIDGETS.batchart = {
|
|
|
|
area: "tl", width: 24, draw: draw, reload: function () {
|
|
|
|
reload();
|
|
|
|
Bangle.drawWidgets();
|
|
|
|
}
|
|
|
|
};
|
2020-04-14 19:02:39 +00:00
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
reload();
|
2020-04-16 09:24:17 +00:00
|
|
|
})();
|