Skip to content

Commit

Permalink
timestamped data so watch always uses latest data
Browse files Browse the repository at this point in the history
  • Loading branch information
wildhart committed Jan 22, 2016
1 parent 400df82 commit 25dab2d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion appinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"KEY_MEDICATIONS": 100,
"KEY_MODE": 1,
"KEY_SORT": 3,
"KEY_TIMESTAMP": 7,
"KEY_VERSION": 4
},
"capabilities": [
Expand Down Expand Up @@ -34,7 +35,7 @@
"shortName": "Meds Timer",
"uuid": "95d07e1a-2451-4ffa-aa44-836e523a7648",
"versionCode": 1,
"versionLabel": "1.2",
"versionLabel": "1.3",
"watchapp": {
"watchface": false
}
Expand Down
12 changes: 10 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ GBitmap *bitmap_tick;
Settings settings={MODE_COUNT_UP, false /*alarm*/, true /*sort*/};
static bool JS_ready = false;
static bool data_loaded_from_watch = false;
static uint32_t data_timestamp = 0;
uint8_t stored_version=0;
bool export_after_save=false;

Expand All @@ -35,16 +36,19 @@ bool export_after_save=false;
#define KEY_VERSION 4
#define KEY_APP_VERSION 5
#define KEY_EXPORT 6
#define KEY_TIMESTAMP 7

static void inbox_received_handler(DictionaryIterator *iter, void *context) {
LOG("Inbox received...");
JS_ready = true;
Tuple *tuple_t;

bool new_data_from_config_page = dict_find(iter, KEY_CONFIG_DATA);
tuple_t= dict_find(iter, KEY_TIMESTAMP);
uint32_t inbox_timestamp = tuple_t ? tuple_t->value->int32 : 0;

if (!data_loaded_from_watch || new_data_from_config_page) {
if (new_data_from_config_page || inbox_timestamp > data_timestamp) {
LOG("Loading settings from phone...");
data_timestamp=inbox_timestamp;
tuple_t=dict_find(iter, KEY_VERSION); stored_version = (tuple_t) ? tuple_t->value->int32 : 1;
tuple_t=dict_find(iter, KEY_MODE); if (tuple_t) settings.Mode = tuple_t->value->int32;
tuple_t=dict_find(iter, KEY_ALARM); if (tuple_t) settings.Alarm = tuple_t->value->int8 > 0; // convert int to boolean
Expand All @@ -71,6 +75,7 @@ static void send_settings_to_phone() {

dict_write_cstring(iter, KEY_APP_VERSION, app_version);
dummy_int=CURRENT_STORAGE_VERSION; dict_write_int(iter, KEY_VERSION, &dummy_int, sizeof(int), true);
dummy_int=data_timestamp; dict_write_int(iter, KEY_TIMESTAMP, &dummy_int, sizeof(int), true);
dummy_int=settings.Mode; dict_write_int(iter, KEY_MODE, &dummy_int, sizeof(int), true);
dummy_int=settings.Alarm; dict_write_int(iter, KEY_ALARM, &dummy_int, sizeof(int), true);
dummy_int=settings.Sort; dict_write_int(iter, KEY_SORT, &dummy_int, sizeof(int), true);
Expand Down Expand Up @@ -109,6 +114,8 @@ void main_wakeup_set() {
void main_save_data(void) {
data_loaded_from_watch = true;
persist_write_int(STORAGE_KEY_VERSION, CURRENT_STORAGE_VERSION);
data_timestamp=time(NULL);
persist_write_int(STORAGE_KEY_TIMESTAMP, data_timestamp);
persist_write_data(STORAGE_KEY_SETTINGS, &settings, sizeof(Settings));
if (settings.Sort) jobs_list_sort();
jobs_list_save(STORAGE_KEY_FIRST_MED);
Expand All @@ -121,6 +128,7 @@ static void main_load_data(void) {

if (stored_version) {
data_loaded_from_watch = true;
if (persist_exists(STORAGE_KEY_TIMESTAMP)) data_timestamp=persist_read_int(STORAGE_KEY_TIMESTAMP);
persist_read_data(STORAGE_KEY_SETTINGS, &settings, sizeof(Settings));
jobs_list_load(STORAGE_KEY_FIRST_MED, stored_version);
if (stored_version < CURRENT_STORAGE_VERSION) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "update.h"
#include "tertiary_text.h"

#define DISABLE_LOGGING true
#define DISABLE_LOGGING false

#if DISABLE_LOGGING
#define LOG(...)
Expand Down Expand Up @@ -73,6 +73,7 @@ extern GBitmap *bitmap_tick;
// Persistent Storage Keys
#define STORAGE_KEY_VERSION 1
#define STORAGE_KEY_SETTINGS 2
#define STORAGE_KEY_TIMESTAMP 3
#define STORAGE_KEY_FIRST_MED 100

#define CURRENT_STORAGE_VERSION 3
Expand Down
8 changes: 7 additions & 1 deletion src/pebble-js-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Pebble.addEventListener('ready', function() {
var settings=localStorage.getItem("settings");
//settings='{"1":2,"2":1,"3":1,"4":3,"5":"1.2","100":"Ibuprofin|1453318208|12|1","101":"Omeprazole|1453275012|24|1","102":"Paracetamol|1453347465|6|0","103":"Tremadol|1453347472|8|0","104":"Zopiclone|1453330855|24|1","KEY_MODE":2,"KEY_ALARM":1,"KEY_SORT":1,"KEY_VERSION":3,"KEY_APP_VERSION":"1.2","KEY_MEDICATIONS":"Ibuprofin|1453318208|12|1"}';
var dict=settings ? JSON.parse(settings) : {};
if (!dict.KEY_TIMESTAMP) {
var d=new Date();
dict.KEY_TIMESTAMP = Math.floor(d.getTime()/1000 - d.getTimezoneOffset()*60);
}
Pebble.sendAppMessage(dict, function() {
console.log('Send successful: ' + JSON.stringify(dict));
}, function() {
Expand Down Expand Up @@ -70,12 +74,14 @@ Pebble.addEventListener('webviewclosed', function(e) {
dict.KEY_ALARM = configData.alarm ? 1 : 0; // Send a boolean as an integer
dict.KEY_SORT = configData.sort ? 1 : 0; // Send a boolean as an integer
dict.KEY_VERSION = configData.data_version;
var d=new Date();
dict.KEY_TIMESTAMP = Math.floor(d.getTime()/1000 - d.getTimezoneOffset()*60);

var med=0;
while (configData["med_"+med]) {
var data=decodeURIComponent(configData["med_"+med]).split("|");
var hhmm=data[1].split(":");
var d = new Date(); // The 0 there is the key, which sets the date to the epoch
d = new Date(); // The 0 there is the key, which sets the date to the epoch
d.setHours(hhmm[0]);
d.setMinutes(hhmm[1]);
d.setSeconds(data[4]);
Expand Down

0 comments on commit 25dab2d

Please sign in to comment.