Browse code

Stream data to the phone using outbox callback instead of dialog

Natasha Kerensikova authored on17/03/2016 18:59:31
Showing2 changed files
... ...
@@ -56,7 +56,6 @@ Pebble.addEventListener("appmessage", function(e) {
56 56
    console.log('Received message: ' + JSON.stringify(e.payload));
57 57
    if (e.payload.dataKey && e.payload.dataLine) {
58 58
       enqueue(e.payload.dataKey, e.payload.dataLine);
59
-      Pebble.sendAppMessage({ "lastSent": e.payload.dataKey });
60 59
    }
61 60
 });
62 61
 
... ...
@@ -10,6 +10,8 @@ static Window *window;
10 10
 static TextLayer *text_layer;
11 11
 static char buffer[256];
12 12
 static HealthMinuteData minute_data[1440];
13
+static uint16_t minute_data_size = 0;
14
+static uint16_t minute_index = 0;
13 15
 static time_t minute_first = 0, minute_last = 0;
14 16
 static unsigned sent = 0;
15 17
 static char global_buffer[1024];
... ...
@@ -148,6 +150,37 @@ send_minute_data(HealthMinuteData *data, time_t key) {
148 150
 	sent += 1;
149 151
 }
150 152
 
153
+static bool
154
+load_minute_data_page(time_t start) {
155
+	minute_first = start;
156
+	minute_last = time(0);
157
+	minute_data_size = health_service_get_minute_history(minute_data,
158
+	    ARRAY_LENGTH(minute_data),
159
+	    &minute_first, &minute_last);
160
+	minute_index = 0;
161
+
162
+	if (!minute_data_size) {
163
+		APP_LOG(APP_LOG_LEVEL_ERROR,
164
+		    "health_service_get_minute_history returned 0");
165
+		minute_first = minute_last = 0;
166
+		return false;
167
+	}
168
+
169
+	return true;
170
+}
171
+
172
+static void
173
+send_next_line(void) {
174
+	if (minute_index >= minute_data_size
175
+	    && !load_minute_data_page(minute_last)) {
176
+		return;
177
+	}
178
+
179
+	send_minute_data(minute_data + minute_index,
180
+	    minute_first + 60 * minute_index);
181
+	minute_index += 1;
182
+}
183
+
151 184
 static int16_t
152 185
 get_next_minute_index(time_t previous_t) {
153 186
 	time_t t = previous_t ? previous_t + 60 - (previous_t % 60) : 0;
... ...
@@ -212,7 +245,8 @@ handle_last_sent(Tuple *tuple) {
212 245
 	    "got index %" PRIi16 ", sending time %" PRIi32,
213 246
 	    index, minute_first + index * 60);
214 247
 
215
-	send_minute_data(minute_data + index, minute_first + index * 60);
248
+	if (load_minute_data_page(ikey * 60))
249
+		send_next_line();
216 250
 }
217 251
 
218 252
 static void
... ...
@@ -252,9 +286,26 @@ init_text(void) {
252 286
 	minute_first = t;
253 287
 }
254 288
 
289
+static void
290
+outbox_sent_handler(DictionaryIterator *iterator, void *context) {
291
+	(void)iterator;
292
+	(void)context;
293
+	send_next_line();
294
+}
295
+
296
+static void
297
+outbox_failed_handler(DictionaryIterator *iterator, AppMessageResult reason,
298
+    void *context) {
299
+	(void)iterator;
300
+	(void)context;
301
+	APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox failed: 0x%x", (unsigned)reason);
302
+}
303
+
255 304
 static void
256 305
 init(void) {
257 306
 	app_message_register_inbox_received(inbox_received_handler);
307
+	app_message_register_outbox_failed(outbox_failed_handler);
308
+	app_message_register_outbox_sent(outbox_sent_handler);
258 309
 	app_message_open(256, 2048);
259 310
 
260 311
 	init_text();