Browse code

Handle lastSent messages, creating a streaming loop

Natasha Kerensikova authored on10/03/2016 23:50:43
Showing2 changed files
... ...
@@ -21,4 +21,7 @@ Pebble.addEventListener("ready", function() {
21 21
 
22 22
 Pebble.addEventListener("appmessage", function(e) {
23 23
    console.log('Received message: ' + JSON.stringify(e.payload));
24
+   if (e.payload.dataKey) {
25
+      Pebble.sendAppMessage({ "lastSent": e.payload.dataKey });
26
+   }
24 27
 });
... ...
@@ -9,7 +9,8 @@ static Window *window;
9 9
 static TextLayer *text_layer;
10 10
 static char buffer[256];
11 11
 static HealthMinuteData minute_data[1440];
12
-static time_t minute_first;
12
+static time_t minute_first = 0, minute_last = 0;
13
+static unsigned sent = 0;
13 14
 static char global_buffer[1024];
14 15
 
15 16
 static void
... ...
@@ -82,9 +83,10 @@ minute_data_image(char *buffer, size_t size,
82 83
 /* send_minute_data - use AppMessage to send the given minute data to phone */
83 84
 static void
84 85
 send_minute_data(HealthMinuteData *data, time_t key) {
85
-	int i;
86 86
 	int32_t int_key = key / 60;
87 87
 
88
+	if (sent > 10) return;
89
+
88 90
 	if (key % 60 != 0) {
89 91
 		APP_LOG(APP_LOG_LEVEL_WARNING,
90 92
 		    "Discarding %" PRIi32 " second from time key %" PRIi32,
... ...
@@ -129,17 +131,86 @@ send_minute_data(HealthMinuteData *data, time_t key) {
129 131
 		    "send_minute_data: app_message_outbox_send returned %d",
130 132
 		    (int)msg_result);
131 133
 	}
134
+
135
+	APP_LOG(APP_LOG_LEVEL_INFO, "sent data for key %" PRIi32, int_key);
136
+
137
+	sent += 1;
138
+}
139
+
140
+static int16_t
141
+get_next_minute_index(time_t previous_t) {
142
+	time_t t = previous_t ? previous_t + 60 - (previous_t % 60) : 0;
143
+	int16_t index = 0;
144
+
145
+	if (t < minute_first || t >= minute_last) {
146
+		uint32_t u;
147
+		minute_first = t;
148
+		minute_last = time(0);
149
+		u = health_service_get_minute_history(minute_data,
150
+		    ARRAY_LENGTH(minute_data),
151
+		    &minute_first, &minute_last);
152
+
153
+		if (!u) {
154
+			APP_LOG(APP_LOG_LEVEL_ERROR,
155
+			    "health_service_get_minute_history returned 0");
156
+			minute_first = minute_last = 0;
157
+			return -1;
158
+		}
159
+
160
+		if (t >= minute_last) {
161
+			APP_LOG(APP_LOG_LEVEL_ERROR,
162
+			    "Unexpected minute_last %" PRIi32
163
+			    " when t is %" PRIi32,
164
+			    minute_last, t);
165
+			return -1;
166
+		}
167
+	} else {
168
+		index = (t - minute_first) / 60;
169
+	}
170
+
171
+	while (minute_data[index].is_invalid) {
172
+		index += 1;
173
+		t = minute_first + 60 * index;
174
+		if (t >= minute_last)
175
+			return get_next_minute_index(t);
176
+	}
177
+
178
+	return index;
179
+}
180
+
181
+static void
182
+handle_last_sent(Tuple *tuple) {
183
+	uint32_t ikey = 0;
184
+	if (tuple->length == 4 && tuple->type == TUPLE_UINT)
185
+		ikey = tuple->value->uint32;
186
+	else if (tuple->length == 4 && tuple->type == TUPLE_INT)
187
+		ikey = tuple->value->int32;
188
+	else {
189
+		APP_LOG(APP_LOG_LEVEL_ERROR,
190
+		    "Unexpected type %d or length %" PRIu16
191
+		    " for MSG_KEY_LAST_SENT",
192
+		    (int)tuple->type, tuple->length);
193
+		return;
194
+	}
195
+	APP_LOG(APP_LOG_LEVEL_INFO, "received LAST_SENT %" PRIu32, ikey);
196
+
197
+	int16_t index = get_next_minute_index(ikey * 60);
198
+	if (index < 0) return;
199
+
200
+	APP_LOG(APP_LOG_LEVEL_INFO,
201
+	    "got index %" PRIi16 ", sending time %" PRIi32,
202
+	    index, minute_first + index * 60);
203
+
204
+	send_minute_data(minute_data + index, minute_first + index * 60);
132 205
 }
133 206
 
134 207
 static void
135 208
 inbox_received_handler(DictionaryIterator *iterator, void *context) {
136
-	(void)iterator;
209
+	Tuple *tuple;
137 210
 	(void)context;
138
-	APP_LOG(APP_LOG_LEVEL_INFO, "inbox_received_handler called");
139 211
 
140
-	if (minute_data->is_invalid) return;
141
-	send_minute_data(minute_data, minute_first);
142
-	APP_LOG(APP_LOG_LEVEL_INFO, "message sent");
212
+	tuple = dict_find(iterator, MSG_KEY_LAST_SENT);
213
+	if (tuple) handle_last_sent (tuple);
143 214
 }
144 215
 
145 216
 static time_t