battery-minus

Pebble activity tracker that records battery events
git clone https://git.instinctive.eu/battery-minus.git
Log | Files | Refs | README | LICENSE

commit 50339805dcb371328720c30c680dfe2c06070175
parent c095ac419343f86453de178338aaecf5678f88a9
Author: Natasha Kerensikova <natacha@instinctive.eu>
Date:   Mon,  9 May 2016 21:57:13 +0000

Add javascript to handle the configuration
Diffstat:
Mappinfo.json | 2++
Asrc/js/app.js | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/appinfo.json b/appinfo.json @@ -6,6 +6,8 @@ "versionLabel": "1.1", "sdkVersion": "3", "targetPlatforms": ["aplite", "basalt", "chalk"], + "enableMultiJS": true, + "capabilities": [ "configurable" ], "watchapp": { "watchface": false }, diff --git a/src/js/app.js b/src/js/app.js @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016, Natacha Porté + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +var cfg_endpoint = null; +var cfg_data_field = null; +var cfg_extra_fields = []; + +var to_send = []; + +Pebble.addEventListener("ready", function(e) { + console.log("Battery- JS ready"); + + var str_to_send = localStorage.getItem("toSend"); + to_send = str_to_send ? str_to_send.split("|") : []; + + var str_extra_fields = localStorage.getItem("extraFields"); + cfg_extra_fields = str_extra_fields ? str_extra_fields.split(",") : []; + + cfg_endpoint = localStorage.getItem("cfgEndpoint"); + cfg_data_field = localStorage.getItem("cfgDataField"); +}); + +Pebble.addEventListener("showConfiguration", function() { + var settings = "?v=1.0"; + + if (cfg_endpoint) { + settings += "&url=" + encodeURIComponent(cfg_endpoint); + } + + if (cfg_data_field) { + settings += "&data_field=" + encodeURIComponent(cfg_data_field); + } + + if (cfg_extra_fields.length > 0) { + settings += "&extra=" + cfg_extra_fields.join(","); + } + + Pebble.openURL("https://cdn.rawgit.com/faelys/battery-minus/v1.0/config.html" + settings); +}); + +Pebble.addEventListener("webviewclosed", function(e) { + var configData = JSON.parse(e.response); + var wasConfigured = (cfg_endpoint && cfg_data_field); + + if (configData.url) { + cfg_endpoint = decodeURIComponent(configData.url); + localStorage.setItem("cfgEndpoint", cfg_endpoint); + } + + if (configData.dataField) { + cfg_data_field = configData.dataField; + localStorage.setItem("cfgDataField", cfg_data_field); + } + + if (configData.extraFields !== null) { + console.log("received extraFields \"" + configData.extraFields + "\""); + cfg_extra_fields = configData.extraFields + ? configData.extraFields.split(",") : []; + localStorage.setItem("extraFields", cfg_extra_fields.join(",")); + } + + if (configData.resend) { + senders[0].abort(); + senders[1].abort(); + localStorage.setItem("toSend", ""); + localStorage.setItem("lastSent", "0"); + to_send = []; + wasConfigured = false; + } +});