diff --git a/include/index.html b/data/index.html similarity index 75% rename from include/index.html rename to data/index.html index e93957c..c2b14c2 100644 --- a/include/index.html +++ b/data/index.html @@ -1,4 +1,4 @@ -const char* html = R"V0G0N( + @@ -60,6 +60,10 @@ const char* html = R"V0G0N(
+
+
+ +
@@ -112,6 +116,35 @@ const char* html = R"V0G0N( + + - -)V0G0N"; diff --git a/include/controller.h b/include/controller.h index 4131a3d..a8c5032 100644 --- a/include/controller.h +++ b/include/controller.h @@ -49,6 +49,7 @@ public: void set_mqtt_client(MQTTClient* m); void register_http_server(HTTPServer* h); void loop(); + void send_controller_status(); void send_player_status(); void send_playlist_manager_status(); void send_position(); diff --git a/include/playlist_manager.h b/include/playlist_manager.h index d642c7c..ab2bdd1 100644 --- a/include/playlist_manager.h +++ b/include/playlist_manager.h @@ -15,4 +15,5 @@ public: Playlist* get_playlist_for_folder(String folder); void dump_ids(); String json(); + bool add_mapping(String id, String folder); }; diff --git a/src/controller.cpp b/src/controller.cpp index 178b40c..6092f51 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -93,6 +93,7 @@ void Controller::_check_rfid() { if (_state != LOCKED) { _player->stop(); } + send_controller_status(); } else { uint32_t uid = _get_rfid_card_uid(); if (uid > 0) { @@ -157,7 +158,8 @@ void Controller::_check_rfid() { } _player->play(pl); - send_playlist_manager_status(); + //send_playlist_manager_status(); + send_controller_status(); } } } @@ -285,6 +287,13 @@ bool Controller::process_message(String cmd) { _player->init(); } else if (cmd.equals("reboot")) { ESP.restart(); + } else if (cmd.startsWith("add_mapping=")) { + String rest = cmd.substring(12); + uint8_t idx = rest.indexOf('='); + String id = rest.substring(0, idx); + String folder = rest.substring(idx + 1); + _pm->add_mapping(id, folder); + send_playlist_manager_status(); } else { ERROR("Unknown command: %s\n", cmd.c_str()); return false; @@ -352,6 +361,7 @@ String Controller::json() { case LOCKING: json["state"] = "locking"; break; case NORMAL: json["state"] = "normal"; break; } + json["is_rfid_present"] = _rfid_present; JsonObject rfid = json.createNestedObject("last_rfid"); rfid["uid"] = _last_rfid_uid; rfid["data"] = _last_rfid_data; @@ -359,7 +369,7 @@ String Controller::json() { } void Controller::send_player_status() { - TRACE("In send_player_status()...\n"); + TRACE("In send_player_status()...\n"); if (_http_server->ws->count() > 0) { _http_server->ws->textAll(_player->json()); @@ -376,12 +386,19 @@ void Controller::send_playlist_manager_status() { void Controller::send_position() { TRACE("In send_position()...\n"); - if (_http_server->ws->count() > 0) { + if (_http_server->ws->count() > 0 && _player->is_playing()) { _http_server->ws->textAll(_player->position_json()); } _last_position_info_at = millis(); } +void Controller::send_controller_status() { + TRACE("In send_controller_status()...\n"); + if (_http_server->ws->count() > 0) { + _http_server->ws->textAll(json()); + } +} + void Controller::inform_new_client(AsyncWebSocketClient* client) { String s; s += _pm->json(); @@ -389,6 +406,8 @@ void Controller::inform_new_client(AsyncWebSocketClient* client) { s += _player->json(); s += '\n'; s += _player->position_json(); + s += '\n'; + s += json(); client->text(s); } diff --git a/src/http_server.cpp b/src/http_server.cpp index 9697576..d7172aa 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -1,5 +1,6 @@ #include "http_server.h" #include +#include HTTPServer::HTTPServer(Player* p, Controller* c) { _player = p; @@ -8,7 +9,7 @@ HTTPServer::HTTPServer(Player* p, Controller* c) { ws = new AsyncWebSocket("/ws"); _server->addHandler(ws); ws->onEvent([&](AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){this->_onEvent(server, client, type, arg, data, len);}); - _server->on("/", [&](AsyncWebServerRequest* req) {_handle_index(req);}); + _server->on("/", [&](AsyncWebServerRequest* req) {req->send(SPIFFS, "/index.html", "text/html");}); _server->begin(); MDNS.addService("http", "tcp", 80); } @@ -101,11 +102,4 @@ void HTTPServer::_onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client _controller->queue_command((char*)data); } } -} - -void HTTPServer::_handle_index(AsyncWebServerRequest* r) { - -#include "index.html" - - r->send(200, "text/html", html); } \ No newline at end of file diff --git a/src/playlist_manager.cpp b/src/playlist_manager.cpp index e962583..7054fc2 100644 --- a/src/playlist_manager.cpp +++ b/src/playlist_manager.cpp @@ -110,3 +110,19 @@ String PlaylistManager::json() { } return json.as(); } + +bool PlaylistManager::add_mapping(String id, String folder) { + DEBUG("Adding Mapping: '%s'=>'%s'\n", id.c_str(), folder.c_str()); + if (!SD.exists(folder)) return false; + File ids = SD.open(folder + "/ids.txt", "a"); + String data = "\n"; + data += id; + data += '\n'; + char* buffer = new char[data.length()]; + data.toCharArray(buffer, data.length()); + ids.write((uint8_t *)buffer, data.length()); + delete buffer; + ids.close(); + _map[id] = folder; + return true; +} \ No newline at end of file