messaging subsystem wip

This commit is contained in:
Sebastien
2020-02-19 08:02:58 -05:00
parent 8c3a52d40c
commit 4de4e07d99
28 changed files with 369 additions and 143 deletions

View File

@@ -646,12 +646,24 @@ function refreshAPHTML(data){
$( "#wifi-list" ).html(h)
}
function getMessages() {
$.getJSON("/messages.json", function(data) {
data.forEach(function(msg) {
});
})
.fail(function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
if (thrownError != '') showMessage(thrownError, 'ERROR');
});
}
function checkStatus(){
RepeatCheckStatusInterval();
if (!enableStatusTimer) return;
if (blockAjax) return;
blockAjax = true;
getMessages();
$.getJSON( "/status.json", function( data ) {
if (data.hasOwnProperty('ssid') && data['ssid'] != ""){
if (data["ssid"] === selectedSSID){
@@ -881,6 +893,7 @@ function getConfig() {
});
}
function showMessage(message, severity) {
if (severity == 'INFO') {
$('#message').css('background', '#6af');

View File

@@ -7,13 +7,7 @@
# please read the SDK documents if you need to do this.
#
COMPONENT_EMBED_FILES := style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO \
-I$(COMPONENT_PATH)/../tools
COMPONENT_ADD_INCLUDEDIRS := .
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../tools
COMPONENT_ADD_INCLUDEDIRS += $(COMPONENT_PATH)/../squeezelite-ota
COMPONENT_EXTRA_INCLUDES += $(PROJECT_PATH)/main/
CFLAGS += -D LOG_LOCAL_LEVEL=ESP_LOG_INFO

View File

@@ -49,6 +49,7 @@ function to process requests, decode URLs, serve files, etc. etc.
#include "sys/param.h"
#include "esp_vfs.h"
#include "lwip/ip_addr.h"
#include "messaging.h"
#define HTTP_STACK_SIZE (5*1024)
#define FREE_AND_NULL(p) if(p!=NULL){ free(p); p=NULL;}
@@ -59,7 +60,7 @@ static const char TAG[] = "httpd_handlers";
/* @brief task handle for the http server */
SemaphoreHandle_t http_server_config_mutex = NULL;
extern RingbufHandle_t messaging;
#define AUTH_TOKEN_SIZE 50
typedef struct session_context {
char * auth_token;
@@ -1039,6 +1040,31 @@ esp_err_t redirect_processor(httpd_req_t *req, httpd_err_code_t error){
esp_err_t redirect_ev_handler(httpd_req_t *req){
return redirect_processor(req,0);
}
esp_err_t messages_get_handler(httpd_req_t *req){
ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(req)){
// todo: redirect to login page
// return ESP_OK;
}
esp_err_t err = set_content_type_from_req(req);
if(err != ESP_OK){
return err;
}
cJSON * json_messages= messaging_retrieve_messages(messaging);
if(json_messages!=NULL){
char * json_text= cJSON_Print(json_messages);
httpd_resp_send(req, (const char *)json_text, strlen(json_text));
cJSON_free(json_messages);
free(json_text);
}
else {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR , "Unable to retrieve messages");
}
return ESP_OK;
}
esp_err_t status_get_handler(httpd_req_t *req){
ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
if(!is_user_authenticated(req)){

View File

@@ -92,6 +92,8 @@ esp_err_t recovery_post_handler(httpd_req_t *req);
esp_err_t flash_post_handler(httpd_req_t *req);
#endif
esp_err_t status_get_handler(httpd_req_t *req);
esp_err_t messages_get_handler(httpd_req_t *req);
esp_err_t ap_scan_handler(httpd_req_t *req);
esp_err_t redirect_ev_handler(httpd_req_t *req);
esp_err_t redirect_200_ev_handler(httpd_req_t *req);

View File

@@ -29,11 +29,12 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
#include "messaging.h"
static const char TAG[] = "http_server";
static httpd_handle_t _server = NULL;
rest_server_context_t *rest_context = NULL;
RingbufHandle_t messaging=NULL;
void register_common_handlers(httpd_handle_t server){
httpd_uri_t res_get = { .uri = "/res/*", .method = HTTP_GET, .handler = resource_filehandler, .user_ctx = rest_context };
@@ -52,6 +53,8 @@ void register_regular_handlers(httpd_handle_t server){
httpd_register_uri_handler(server, &config_get);
httpd_uri_t status_get = { .uri = "/status.json", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &status_get);
httpd_uri_t messages_get = { .uri = "/messages.json", .method = HTTP_GET, .handler = messages_get_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &messages_get);
httpd_uri_t config_post = { .uri = "/config.json", .method = HTTP_POST, .handler = config_post_handler, .user_ctx = rest_context };
httpd_register_uri_handler(server, &config_post);
@@ -112,6 +115,7 @@ void register_regular_handlers(httpd_handle_t server){
esp_err_t http_server_start()
{
ESP_LOGI(TAG, "Initializing HTTP Server");
messaging = messaging_register_subscriber(10, "http_server");
rest_context = calloc(1, sizeof(rest_server_context_t));
if(rest_context==NULL){
ESP_LOGE(TAG,"No memory for http context");