Skip to content

Commit

Permalink
Set time from webui (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzurikmiroslav authored Jan 20, 2024
1 parent ad02106 commit 813a849
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 53 deletions.
16 changes: 11 additions & 5 deletions components/protocols/include/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ typedef struct
void scheduler_init(void);

/**
* @brief Return number of shedulers
* @brief Execute schedules after change time
*
*/
void scheduler_execute_schedules(void);

/**
* @brief Return number of schedulers
*
* @return uint8_t
*/
uint8_t scheduler_get_num_of_schedules(void);
uint8_t scheduler_get_schedule_count(void);

/**
* @brief Return shedulers array
* @brief Return schedulers array
*
* @return scheduler_schedule_t*
*/
Expand All @@ -62,9 +68,9 @@ scheduler_schedule_t* scheduler_get_schedules(void);
* @brief Set schedulers config
*
* @param schedules
* @param num_of_schedules
* @param schedule_count
*/
void scheduler_set_schedule_config(const scheduler_schedule_t *schedules, uint8_t num_of_schedules);
void scheduler_set_schedule_config(const scheduler_schedule_t *schedules, uint8_t schedule_count);

/**
* @brief Return true if NTP is enabled, stored in NVS
Expand Down
25 changes: 10 additions & 15 deletions components/protocols/src/http_dav.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ static void propfind_response_directory(httpd_req_t* req, const char* path)
httpd_resp_send_chunk(req, "</href>\n", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "<propstat>\n", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "<prop>\n", HTTPD_RESP_USE_STRLEN);
if (total > 0 && used > 0) {
sprintf(str, "%zu", used);
httpd_resp_send_chunk(req, "<quota-used-bytes>", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, str, HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "</quota-used-bytes>\n", HTTPD_RESP_USE_STRLEN);

sprintf(str, "%zu", total - used);
httpd_resp_send_chunk(req, "<quota-available-bytes>", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, str, HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "</quota-available-bytes>\n", HTTPD_RESP_USE_STRLEN);
}
sprintf(str, "%zu", used);
httpd_resp_send_chunk(req, "<quota-used-bytes>", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, str, HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "</quota-used-bytes>\n", HTTPD_RESP_USE_STRLEN);
sprintf(str, "%zu", total - used);
httpd_resp_send_chunk(req, "<quota-available-bytes>", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, str, HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "</quota-available-bytes>\n", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "<resourcetype><collection/></resourcetype>\n", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "</prop>\n", HTTPD_RESP_USE_STRLEN);
httpd_resp_send_chunk(req, "<status>HTTP/1.1 200 OK</status>\n", HTTPD_RESP_USE_STRLEN);
Expand Down Expand Up @@ -134,8 +131,6 @@ static void propfind_response_file(httpd_req_t* req, const char* path)

static esp_err_t options_handler(httpd_req_t* req)
{
//ESP_LOGI(TAG, "http_dav_options_handler %s", req->uri);

const char* path = req->uri + DAV_BASE_PATH_LEN;

if (strcmp(path, "/") == 0 || strcmp(path, "/data/") == 0 || strcmp(path, "/cfg/") == 0) {
Expand Down Expand Up @@ -169,8 +164,6 @@ static esp_err_t propfind_handler(httpd_req_t* req)
{
const char* path = req->uri + DAV_BASE_PATH_LEN;

// ESP_LOGI(TAG, "http_dav_propfind_handler %s", req->uri);

if (strcmp(path, "/") == 0 || strcmp(path, "/data/") == 0 || strcmp(path, "/cfg/") == 0) {
//directories
set_resp_hdr(req, RESP_HDR_DIR);
Expand Down Expand Up @@ -247,6 +240,8 @@ static esp_err_t get_handler(httpd_req_t* req)
return ESP_FAIL;
}

httpd_resp_set_type(req, "application/octet-stream");

if (req->method == HTTP_GET) {
char buf[SCRATCH_BUFSIZE];
size_t len;
Expand Down
49 changes: 37 additions & 12 deletions components/protocols/src/http_json.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string.h>
#include <sys/time.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
Expand Down Expand Up @@ -328,9 +329,9 @@ cJSON* http_json_get_scheduler_config(void)
cJSON_AddStringToObject(json, "timezone", str);

cJSON* schedules_json = cJSON_CreateArray();
uint8_t num_of_schedules = scheduler_get_num_of_schedules();
uint8_t schedule_count = scheduler_get_schedule_count();
scheduler_schedule_t* schedules = scheduler_get_schedules();
for (uint8_t i = 0; i < num_of_schedules; i++) {
for (uint8_t i = 0; i < schedule_count; i++) {
cJSON* schedule_json = cJSON_CreateObject();
cJSON_AddStringToObject(schedule_json, "action", scheduler_action_to_str(schedules[i].action));

Expand Down Expand Up @@ -364,16 +365,16 @@ esp_err_t http_json_set_scheduler_config(cJSON* json)
scheduler_schedule_t* schedules = (scheduler_schedule_t*)malloc(sizeof(scheduler_schedule_t) * count);

uint8_t i = 0;
cJSON* item = NULL;
cJSON_ArrayForEach(item, schedules_json) {
schedules[i].action = scheduler_str_to_action(cJSON_GetStringValue(cJSON_GetObjectItem(item, "action")));
schedules[i].days.week.mon = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "mon"));
schedules[i].days.week.tue = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "tue"));
schedules[i].days.week.wed = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "wed"));
schedules[i].days.week.thu = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "thu"));
schedules[i].days.week.fri = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "fri"));
schedules[i].days.week.sat = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "sat"));
schedules[i].days.week.sun = cJSON_GetNumberValue(cJSON_GetObjectItem(item, "sun"));
cJSON* schedule_json = NULL;
cJSON_ArrayForEach(schedule_json, schedules_json) {
schedules[i].action = scheduler_str_to_action(cJSON_GetStringValue(cJSON_GetObjectItem(schedule_json, "action")));
schedules[i].days.week.mon = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "mon"));
schedules[i].days.week.tue = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "tue"));
schedules[i].days.week.wed = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "wed"));
schedules[i].days.week.thu = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "thu"));
schedules[i].days.week.fri = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "fri"));
schedules[i].days.week.sat = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "sat"));
schedules[i].days.week.sun = cJSON_GetNumberValue(cJSON_GetObjectItem(schedule_json, "sun"));
i++;
};

Expand All @@ -386,6 +387,30 @@ esp_err_t http_json_set_scheduler_config(cJSON* json)
return ESP_OK;
}

cJSON* http_json_get_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);

return cJSON_CreateNumber(tv.tv_sec);
}

esp_err_t http_json_set_time(cJSON* json)
{
if (cJSON_IsNumber(json)) {
struct timeval tv;
gettimeofday(&tv, NULL);
tv.tv_sec = cJSON_GetNumberValue(json);
settimeofday(&tv, NULL);

scheduler_execute_schedules();
} else {
return ESP_ERR_INVALID_ARG;
}

return ESP_OK;
}

cJSON* http_json_get_state(void)
{
cJSON* json = cJSON_CreateObject();
Expand Down
16 changes: 10 additions & 6 deletions components/protocols/src/http_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@

cJSON* http_json_get_evse_config(void);

esp_err_t http_json_set_evse_config(cJSON* root);
esp_err_t http_json_set_evse_config(cJSON* json);

cJSON* http_json_get_wifi_config(void);

esp_err_t http_json_set_wifi_config(cJSON* root, bool timeout);
esp_err_t http_json_set_wifi_config(cJSON* json, bool timeout);

cJSON* http_json_get_wifi_scan(void);

cJSON* http_json_get_serial_config(void);

esp_err_t http_json_set_serial_config(cJSON* root);
esp_err_t http_json_set_serial_config(cJSON* json);

cJSON* http_json_get_modbus_config(void);

esp_err_t http_json_set_modbus_config(cJSON* root);
esp_err_t http_json_set_modbus_config(cJSON* json);

cJSON* http_json_get_script_config(void);

esp_err_t http_json_set_script_config(cJSON* root);
esp_err_t http_json_set_script_config(cJSON* json);

cJSON* http_json_get_scheduler_config(void);

esp_err_t http_json_set_scheduler_config(cJSON* root);
esp_err_t http_json_set_scheduler_config(cJSON* json);

cJSON* http_json_get_time(void);

esp_err_t http_json_set_time(cJSON* json);

cJSON* http_json_get_state(void);

Expand Down
7 changes: 7 additions & 0 deletions components/protocols/src/http_rest.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <sys/param.h>
#include <sys/stat.h>
#include "esp_err.h"
Expand Down Expand Up @@ -201,6 +202,9 @@ esp_err_t get_handler(httpd_req_t* req)
if (strcmp(req->uri, REST_BASE_PATH"/state") == 0) {
root = http_json_get_state();
}
if (strcmp(req->uri, REST_BASE_PATH"/time") == 0) {
root = http_json_get_time();
}
if (strcmp(req->uri, REST_BASE_PATH"/config") == 0) {
root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "evse", http_json_get_evse_config());
Expand Down Expand Up @@ -291,6 +295,9 @@ esp_err_t post_handler(httpd_req_t* req)
set_credentials(root);
ret = ESP_OK;
}
if (strcmp(req->uri, REST_BASE_PATH"/time") == 0) {
ret = http_json_set_time(root);
}

cJSON_Delete(root);

Expand Down
35 changes: 20 additions & 15 deletions components/protocols/src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static TaskHandle_t scheduler_task = NULL;

static char ntp_server[64]; // if renew_servers_after_new_IP = false, will be used static string reference

static uint8_t num_of_schedules = 0;
static uint8_t schedule_count = 0;

static scheduler_schedule_t* schedules = NULL;

Expand Down Expand Up @@ -58,8 +58,8 @@ static const char* find_tz(const char* name)

void ntp_sync_cb(struct timeval* tv)
{
ESP_LOGI(TAG, "NTP sync");
xTaskNotifyGive(scheduler_task);
ESP_LOGD(TAG, "NTP sync");
scheduler_execute_schedules();
}

static void on_action(scheduler_action_t action)
Expand Down Expand Up @@ -111,7 +111,7 @@ static void scheduler_task_func(void* param)
struct tm timeinfo = { 0 };
localtime_r(&now, &timeinfo);

for (uint8_t i = 0; i < num_of_schedules; i++) {
for (uint8_t i = 0; i < schedule_count; i++) {
uint32_t day = schedules[i].days.order[timeinfo.tm_wday];
if (day & (1 << timeinfo.tm_hour)) {
if (schedules_state[i] != STATE_ON) {
Expand Down Expand Up @@ -171,12 +171,12 @@ void scheduler_init(void)
if (size % sizeof(scheduler_schedule_t) > 0) {
ESP_LOGW(TAG, "Schedules NVS incompatible size, schedules will be cleared");
} else {
num_of_schedules = size / sizeof(scheduler_schedule_t);
schedules = (scheduler_schedule_t*)malloc(sizeof(scheduler_schedule_t) * num_of_schedules);
schedule_count = size / sizeof(scheduler_schedule_t);
schedules = (scheduler_schedule_t*)malloc(sizeof(scheduler_schedule_t) * schedule_count);
nvs_get_blob(nvs, NVS_SCHEDULES, (void*)schedules, &size);

schedules_state = (uint8_t*)realloc((void*)schedules_state, sizeof(uint8_t) * num_of_schedules);
memset((void*)schedules_state, STATE_NONE, sizeof(uint8_t) * num_of_schedules);
schedules_state = (uint8_t*)realloc((void*)schedules_state, sizeof(uint8_t) * schedule_count);
memset((void*)schedules_state, STATE_NONE, sizeof(uint8_t) * schedule_count);
}
}

Expand All @@ -185,9 +185,14 @@ void scheduler_init(void)
xTaskCreate(scheduler_task_func, "scheduler_task", 2 * 1024, NULL, 1, &scheduler_task);
}

uint8_t scheduler_get_num_of_schedules(void)
void scheduler_execute_schedules(void)
{
xTaskNotifyGive(scheduler_task);
}

uint8_t scheduler_get_schedule_count(void)
{
return num_of_schedules;
return schedule_count;
}

scheduler_schedule_t* scheduler_get_schedules(void)
Expand All @@ -199,12 +204,12 @@ void scheduler_set_schedule_config(const scheduler_schedule_t* _schedules, uint8
{
xSemaphoreTake(mutex, portMAX_DELAY);

num_of_schedules = _num_of_schedules;
if (num_of_schedules > 0) {
schedules_state = (uint8_t*)realloc((void*)schedules_state, sizeof(uint8_t) * num_of_schedules);
memset((void*)schedules_state, STATE_NONE, sizeof(uint8_t) * num_of_schedules);
schedule_count = _num_of_schedules;
if (schedule_count > 0) {
schedules_state = (uint8_t*)realloc((void*)schedules_state, sizeof(uint8_t) * schedule_count);
memset((void*)schedules_state, STATE_NONE, sizeof(uint8_t) * schedule_count);

size_t size = sizeof(scheduler_schedule_t) * num_of_schedules;
size_t size = sizeof(scheduler_schedule_t) * schedule_count;
schedules = (scheduler_schedule_t*)realloc((void*)schedules, size);
memcpy((void*)schedules, _schedules, size);
nvs_set_blob(nvs, NVS_SCHEDULES, (void*)schedules, size);
Expand Down
Binary file modified components/protocols/web.cpio
Binary file not shown.

0 comments on commit 813a849

Please sign in to comment.