Commit 952b3eb3 authored by Jacob Priddy's avatar Jacob Priddy 👌

Fix the bugs and add some debug logging code

parent 7330cedf
......@@ -62,4 +62,6 @@
// maximum number of keypresses to remember at once
#define KEYPAD_KEY_PRESS_MEMORY_SIZE 100
//#define DEBUG_LOGGING
#endif //DOOR_CONFIG_H
......@@ -7,6 +7,9 @@
#include "app/list.h"
#include "app/token.h"
#include "app/status.h"
#ifdef DEBUG_LOGGING
#include <esp_log.h>
#endif
#define MAX_WAITING_REQUESTS 10
#define QUEUE_DONE_CHECK_PERIOD_MS 300
......@@ -75,6 +78,9 @@ static bool status_gatherer(const void* data, const int len)
static void status_on_finish(int32_t status_code)
{
#ifdef DEBUG_LOGGING
ESP_LOGI("API", "Finished request to server, returned data is: %s", status_state.incoming_data);
#endif
// Don't free status state as we hand it over to the status parser.
if (status_code == HTTP_SUCCESS || status_code == HTTP_UNAUTHORIZED)
door_status_parse_server_status(status_state.incoming_data);
......
......@@ -68,6 +68,7 @@ void door_latch_watch_task(void* pvParameters)
//
// prev_state = next_state;
} while (!done);
door_rtos_task_current_kill();
}
void door_latch_stop(void)
......
......@@ -12,6 +12,9 @@
#include "app/list.h"
#include "app/event.h"
#include "connectors/sntp.h"
#ifdef DEBUG_LOGGING
#include <esp_log.h>
#endif
#ifdef DOOR_TESTING
/*
......@@ -32,6 +35,8 @@ extern const char* strptime(const char*, const char*, struct tm*);
#define UPDATE_MUTEX_HANDLE_WAIT_TIME_MS 100
#define TOKEN_COUNT_FOR_TIME_SLOT_JSON_OBJECT 5
// REALLY shouldn't take more than 10 seconds to get through the list
#define LIST_UPDATE_WAIT_TIME_MS 1000 * 10
......@@ -90,6 +95,25 @@ void door_status_refresh_task(void* pvParameters)
schedule_data_p schedule_data = (schedule_data_p)curr->data;
time_t start = schedule_data->start;
time_t end = schedule_data->end;
#ifdef DEBUG_LOGGING
char buffer[80];
struct tm s;
s = (const struct tm) { 0 };
s.tm_isdst = -1;
localtime_r(&current_time, &s);
strftime(buffer,80,"Current time: %x - %I:%M%p", &s);
ESP_LOGI("Status", "%s", buffer);
s = (const struct tm) { 0 };
s.tm_isdst = -1;
localtime_r(&start, &s);
strftime(buffer,80,"Start time: %x - %I:%M%p", &s);
ESP_LOGI("Status", "%s", buffer);
s = (const struct tm) { 0 };
s.tm_isdst = -1;
localtime_r(&end, &s);
strftime(buffer,80,"End time: %x - %I:%M%p", &s);
ESP_LOGI("Status", "%s", buffer);
#endif
/*
* We are guaranteed to have them in sorted order by begin date by the API specification.
......@@ -122,6 +146,7 @@ void door_status_refresh_task(void* pvParameters)
found = true;
break;
}
curr = curr->next;
}
END_PROTECT_RESOURCE(schedule_list_guard);
......@@ -130,15 +155,14 @@ void door_status_refresh_task(void* pvParameters)
else
door_rtos_event_clear(DOOR_OPEN_MODE_EVENT);
} while (!done);
door_rtos_task_current_kill();
}
static int jsoneq(const char* json, jsmntok_t* tok, const char* s)
{
if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0)
{
return 0;
}
return -1;
}
......@@ -201,7 +225,16 @@ static int parse_open_time_slot(schedule_data_p sc, const char* json, jsmntok_t*
for (int i = 1; i < 4; i += 2)
{
time_t* curr_time;
int tz_hour, tz_min;
struct tm tm_parsed = (const struct tm){ 0 };
// let mktime figure out DST cause I don't wanna deal with figuring that out cause it's 3:30 AM rn and I have
// A class at 9 tomorrow. prolly gonna end up skipping it
// Update: right as i was about to go to bed, I looked over, and low and behold, my room was flooding
// right before my very eyes. I had to go outside in the heavy rain, scoop out the lake of water in my window
// well, and then build up ramparts to keep water out. I did indeed skip my 9 AM. T_T
tm_parsed.tm_isdst = -1;
if (jsoneq(json, &tok[i], "begins_at") == 0)
curr_time = &sc->start;
else if (jsoneq(json, &tok[i], "ends_at") == 0)
......@@ -213,14 +246,6 @@ static int parse_open_time_slot(schedule_data_p sc, const char* json, jsmntok_t*
if (!(end = strptime(json + tok[i + 1].start, TIME_FORMAT, &tm_parsed)))
return -1;
// let mktime figure out DST cause I don't wanna deal with figuring that out cause it's 3:30 AM rn and I have
// A class at 9 tomorrow. prolly gonna end up skipping it
// Update: right as i was about to go to bed, I looked over, and low and behold, my room was flooding
// right before my very eyes. I had to go outside in the heavy rain, scoop out the lake of water in my window
// well, and then build up ramparts to keep water out. I did indeed skip my 9 AM. T_T
tm_parsed.tm_isdst = -1;
int tz_hour, tz_min;
// Check to see if a timezone was set
if (sscanf(end, "%d:%d", &tz_hour, &tz_min) != 2)
// If no timezone was set, assume it is the local one to the device
......@@ -230,7 +255,7 @@ static int parse_open_time_slot(schedule_data_p sc, const char* json, jsmntok_t*
*curr_time = parsed_UTC_to_unix_time(&tm_parsed, tz_hour, tz_min);
}
return 5;
return TOKEN_COUNT_FOR_TIME_SLOT_JSON_OBJECT;
}
void door_status_sync_status_task(void* pvParameters)
......@@ -248,11 +273,28 @@ void door_status_sync_status_task(void* pvParameters)
char* status_json_raw;
if (!door_rtos_queue_receive(json_data_queue, &status_json_raw, STATUS_MAX_NO_CONTACT_TIME_MS))
{
#ifdef DEBUG_LOGGING
ESP_LOGI("STATUS", "I'm lonely, talking to the API.");
#endif
// submit request
door_api_get_status();
continue;
}
#ifdef DEBUG_LOGGING
ESP_LOGI("STATUS", "Got new JSON data to parse, waiting for time sync...");
#endif
/*
* We need the current actual time to be able to parse time properly otherwise they all immediately
* get discarded in the refresh task
*/
door_rtos_wait_forever_for_event(TIME_SYNCED_EVENT);
#ifdef DEBUG_LOGGING
ESP_LOGI("STATUS", "Time is synced. Parsing new JSON Data");
#endif
r = jsmn_parse(&p, status_json_raw, strlen(status_json_raw), tokens, sizeof(tokens) / sizeof(tokens[0]));
// There was an error or no data to parse
......@@ -301,6 +343,7 @@ void door_status_sync_status_task(void* pvParameters)
cleanup:
free(status_json_raw);
} while (!done);
door_rtos_task_current_kill();
}
void door_status_stop(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment