2019-08-02 21:48:36 +00:00
|
|
|
#include "controller.h"
|
2019-08-06 18:50:11 +00:00
|
|
|
#include "spi_master.h"
|
2019-11-10 13:45:33 +00:00
|
|
|
#include "config.h"
|
2019-11-14 19:42:02 +00:00
|
|
|
#include "playlist.h"
|
2019-11-16 22:03:13 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
#include <ArduinoJson.h>
|
2019-08-02 21:48:36 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
Controller::Controller(Player* p, PlaylistManager* pm) {
|
2019-08-06 18:50:11 +00:00
|
|
|
_player = p;
|
2019-11-14 19:42:02 +00:00
|
|
|
_pm = pm;
|
2019-11-11 04:32:41 +00:00
|
|
|
_rfid = new MFRC522(17, MFRC522::UNUSED_PIN);
|
2019-11-16 22:03:13 +00:00
|
|
|
|
|
|
|
_player->register_controller(this);
|
2019-08-06 18:50:11 +00:00
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
BTN_NEXT_SETUP();
|
|
|
|
BTN_PREV_SETUP();
|
|
|
|
BTN_VOL_UP_SETUP();
|
|
|
|
BTN_VOL_DOWN_SETUP();
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522();
|
2019-11-10 13:45:33 +00:00
|
|
|
DEBUG("Initializing RC522...\n");
|
2019-08-06 18:50:11 +00:00
|
|
|
_rfid->PCD_Init();
|
2019-08-09 04:27:33 +00:00
|
|
|
#ifdef SHOW_DEBUG
|
|
|
|
_rfid->PCD_DumpVersionToSerial();
|
|
|
|
#endif
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522(false);
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO("RC522 initialized.\n");
|
2019-08-11 15:15:22 +00:00
|
|
|
|
|
|
|
for (uint8_t i=0; i<NUM_BUTTONS; i++) _button_last_pressed_at[i]=0;
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:06:51 +00:00
|
|
|
void Controller::set_mqtt_client(MQTTClient* m) {
|
|
|
|
_mqtt_client = m;
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
void Controller::register_http_server(HTTPServer* h) {
|
|
|
|
_http_server = h;
|
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Controller::loop() {
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("Controller::loop()...\n");
|
2019-08-08 03:31:27 +00:00
|
|
|
unsigned long now = millis();
|
|
|
|
if ((_last_rfid_scan_at < now - RFID_SCAN_INTERVAL) || (now < _last_rfid_scan_at)) {
|
|
|
|
_check_rfid();
|
|
|
|
_last_rfid_scan_at = now;
|
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
if ((_last_position_info_at < now - POSITION_SEND_INTERVAL) || (now < _last_position_info_at)) {
|
|
|
|
send_position();
|
|
|
|
_last_position_info_at = now;
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
_check_serial();
|
2019-08-11 15:15:22 +00:00
|
|
|
_check_buttons();
|
2019-11-16 22:03:13 +00:00
|
|
|
if (_cmd_queue.length() > 0) {
|
|
|
|
process_message(_cmd_queue);
|
|
|
|
_cmd_queue = "";
|
2019-08-14 19:01:01 +00:00
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("Controller::loop() done.\n");
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 04:49:35 +00:00
|
|
|
uint32_t Controller::_get_rfid_card_uid() {
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522();
|
2019-08-06 18:50:11 +00:00
|
|
|
if (!_rfid->PICC_ReadCardSerial()) {
|
2019-08-08 04:49:35 +00:00
|
|
|
if (!_rfid->PICC_IsNewCardPresent()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!_rfid->PICC_ReadCardSerial()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522(false);
|
2019-08-08 04:49:35 +00:00
|
|
|
uint32_t uid = _rfid->uid.uidByte[0]<<24 | _rfid->uid.uidByte[1]<<16 | _rfid->uid.uidByte[2]<<8 | _rfid->uid.uidByte[3];
|
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::_check_rfid() {
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("check_rfid running...\n");
|
2019-11-14 05:48:21 +00:00
|
|
|
MFRC522::StatusCode status;
|
|
|
|
if (_rfid_present) {
|
|
|
|
byte buffer[2];
|
|
|
|
byte buffer_size = 2;
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522();
|
2019-11-14 05:48:21 +00:00
|
|
|
status = _rfid->PICC_WakeupA(buffer, &buffer_size);
|
|
|
|
if (status == MFRC522::STATUS_OK) {
|
|
|
|
// Card is still present.
|
|
|
|
_rfid->PICC_HaltA();
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522(false);
|
2019-11-14 05:48:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_rc522(false);
|
2019-11-14 05:48:21 +00:00
|
|
|
// Card is now gone
|
|
|
|
_rfid_present = false;
|
|
|
|
INFO("No more RFID card.\n");
|
2019-11-14 19:42:02 +00:00
|
|
|
if (_state != LOCKED) {
|
|
|
|
_player->stop();
|
|
|
|
}
|
2019-11-16 23:35:23 +00:00
|
|
|
send_controller_status();
|
2019-11-14 05:48:21 +00:00
|
|
|
} else {
|
|
|
|
uint32_t uid = _get_rfid_card_uid();
|
2019-08-09 04:27:33 +00:00
|
|
|
if (uid > 0) {
|
2019-08-14 19:01:01 +00:00
|
|
|
_mqtt_client->publish_rfid_uid(uid);
|
2019-11-14 05:48:21 +00:00
|
|
|
//_no_rfid_card_count = 0;
|
2019-11-12 18:29:55 +00:00
|
|
|
String temp = String(uid, HEX);
|
|
|
|
String s_uid = "";
|
2019-11-13 05:51:38 +00:00
|
|
|
for (int i=0; i<(8-temp.length()); i++) {
|
2019-11-12 18:29:55 +00:00
|
|
|
s_uid.concat("0");
|
|
|
|
}
|
|
|
|
s_uid.concat(temp);
|
|
|
|
INFO("New RFID card uid: %s\n", s_uid.c_str());
|
2019-11-16 22:03:13 +00:00
|
|
|
_last_rfid_uid = s_uid;
|
2019-11-14 05:48:21 +00:00
|
|
|
_rfid_present = true;
|
2019-11-13 19:14:09 +00:00
|
|
|
|
2019-11-14 05:48:21 +00:00
|
|
|
String data = _read_rfid_data();
|
2019-11-16 22:03:13 +00:00
|
|
|
_last_rfid_data = data;
|
2019-11-14 05:48:21 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
Playlist* pl = _pm->get_playlist_for_id(s_uid);
|
|
|
|
if (data.indexOf("[lock]") != -1) {
|
|
|
|
if (_state == LOCKED) {
|
|
|
|
_state = NORMAL;
|
|
|
|
DEBUG("ControllerState is now UNLOCKED\n");
|
|
|
|
} else {
|
|
|
|
DEBUG("ControllerState is now LOCKING\n");
|
|
|
|
_state = LOCKING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pl==NULL) {
|
|
|
|
INFO("Could not find album for id '%s'.", s_uid.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int index;
|
2019-11-16 22:03:13 +00:00
|
|
|
if (data.indexOf("[advent]") != -1 && pl->is_fresh()) {
|
|
|
|
struct tm time;
|
|
|
|
getLocalTime(&time);
|
|
|
|
if (time.tm_mon == 11) { // tm_mon is "months since january", so 11 means december.
|
|
|
|
pl->advent_shuffle(time.tm_mday);
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
} else if (data.indexOf("[random]") != -1 && pl->is_fresh()) {
|
2019-11-14 19:42:02 +00:00
|
|
|
pl->shuffle();
|
|
|
|
} else if ((index=data.indexOf("[random:")) != -1 && pl->is_fresh()) {
|
|
|
|
String temp = data.substring(index + 8);
|
|
|
|
index = temp.indexOf("]");
|
|
|
|
TRACE("temp: %s, temp.substring(0, %d): %s\n", temp.c_str(), index, temp.substring(0, index).c_str());
|
|
|
|
if (index>0) {
|
|
|
|
uint8_t random_offset = temp.substring(0, index).toInt();
|
|
|
|
pl->shuffle(random_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_state == LOCKED) {
|
|
|
|
DEBUG("ControllerState is LOCKED, ignoring card.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_state == LOCKING) {
|
|
|
|
_state = LOCKED;
|
|
|
|
DEBUG("ControllerState is now LOCKED.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
_player->play(pl);
|
2019-11-16 23:35:23 +00:00
|
|
|
//send_playlist_manager_status();
|
|
|
|
send_controller_status();
|
2019-11-14 05:48:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String Controller::_read_rfid_data() {
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("_read_rfid_data() running...\n");
|
2019-11-14 19:42:02 +00:00
|
|
|
static MFRC522::MIFARE_Key keys[8] = {
|
|
|
|
{{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}}, // D3 F7 D3 F7 D3 F7
|
|
|
|
{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, // FF FF FF FF FF FF = factory default
|
|
|
|
{{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}}, // A0 A1 A2 A3 A4 A5
|
|
|
|
{{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}}, // B0 B1 B2 B3 B4 B5
|
|
|
|
{{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}}, // 4D 3A 99 C3 51 DD
|
|
|
|
{{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}}, // 1A 98 2C 7E 45 9A
|
|
|
|
{{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}}, // AA BB CC DD EE FF
|
|
|
|
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}} // 00 00 00 00 00 00
|
|
|
|
};
|
|
|
|
SPIMaster::select_rc522();
|
2019-11-14 05:48:21 +00:00
|
|
|
DEBUG("Trying to read RFID data...\n");
|
|
|
|
|
|
|
|
String data = "";
|
|
|
|
MFRC522::PICC_Type type = _rfid->PICC_GetType(_rfid->uid.sak);
|
|
|
|
|
|
|
|
uint8_t sectors = 0;
|
|
|
|
switch(type) {
|
|
|
|
case MFRC522::PICC_TYPE_MIFARE_MINI: sectors = 5; break;
|
|
|
|
case MFRC522::PICC_TYPE_MIFARE_1K: sectors = 16; break;
|
|
|
|
case MFRC522::PICC_TYPE_MIFARE_4K: sectors = 40; break;
|
|
|
|
default: INFO("Unknown PICC type %s\n", String(MFRC522::PICC_GetTypeName(type)).c_str());
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
int good_key_index = -1;
|
2019-11-14 05:48:21 +00:00
|
|
|
for (uint8_t sector=1; sector<sectors; sector++) {
|
|
|
|
uint8_t blocks = (sector < 32) ? 4 : 16;
|
|
|
|
uint8_t block_offset = (sector < 32) ? sector * 4 : 128 + (sector - 32) * 16;
|
|
|
|
|
|
|
|
MFRC522::StatusCode status;
|
2019-11-14 19:42:02 +00:00
|
|
|
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
MFRC522::MIFARE_Key *k = &keys[i];
|
|
|
|
TRACE("Trying MIFARE key %02X %02X %02X %02X %02X %02X...\n", k->keyByte[0], k->keyByte[1], k->keyByte[2], k->keyByte[3], k->keyByte[4], k->keyByte[5]);
|
|
|
|
status = _rfid->PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block_offset, k, &_rfid->uid);
|
|
|
|
if (status == MFRC522::STATUS_OK) {
|
|
|
|
TRACE("Authentication succeeded with key #%d\n", i);
|
|
|
|
good_key_index = i;
|
|
|
|
break;
|
2019-11-13 19:14:09 +00:00
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
}
|
|
|
|
if (good_key_index == -1) {
|
|
|
|
TRACE("Could not find a valid MIFARE key.\n");
|
|
|
|
} else {
|
|
|
|
for (uint8_t block=0; block<blocks-1; block++) {
|
|
|
|
byte buffer[18];
|
|
|
|
uint8_t byte_count = 18;
|
|
|
|
status = _rfid->MIFARE_Read(block_offset + block, buffer, &byte_count);
|
|
|
|
if (status != MFRC522::STATUS_OK) {
|
|
|
|
DEBUG("MIFARE_Read() failed: %s\n", String(_rfid->GetStatusCodeName(status)).c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
if (buffer[i]>=0x20 && buffer[i]<0x7F) data.concat((char)buffer[i]);
|
|
|
|
}
|
2019-08-12 18:15:00 +00:00
|
|
|
}
|
2019-08-09 04:27:33 +00:00
|
|
|
}
|
2019-08-08 04:49:35 +00:00
|
|
|
}
|
2019-11-14 05:48:21 +00:00
|
|
|
|
|
|
|
_rfid->PICC_HaltA();
|
|
|
|
_rfid->PCD_StopCrypto1();
|
2019-11-14 19:42:02 +00:00
|
|
|
DEBUG("Data from RFID: %s\n", data.c_str());
|
|
|
|
SPIMaster::select_rc522(false);
|
2019-11-14 05:48:21 +00:00
|
|
|
return data;
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
void Controller::_check_serial() {
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("check_serial running...\n");
|
|
|
|
|
2019-08-06 18:50:11 +00:00
|
|
|
if (Serial.available() > 0) {
|
|
|
|
char c = Serial.read();
|
2019-08-09 04:27:33 +00:00
|
|
|
Serial.printf("%c", c);
|
|
|
|
if (c==10 || c==13) {
|
|
|
|
if (_serial_buffer.length()>0) {
|
2019-11-16 22:03:13 +00:00
|
|
|
process_message(_serial_buffer);
|
2019-08-09 04:27:33 +00:00
|
|
|
_serial_buffer = String();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_serial_buffer.concat(c);
|
2019-08-06 18:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 21:48:36 +00:00
|
|
|
}
|
2019-08-09 04:27:33 +00:00
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
bool Controller::process_message(String cmd) {
|
2019-08-12 18:15:00 +00:00
|
|
|
DEBUG("Executing command: %s\n", cmd.c_str());
|
2019-08-11 15:15:22 +00:00
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
if (cmd.startsWith("play ")) {
|
2019-11-16 22:03:13 +00:00
|
|
|
Playlist* p = _pm->get_playlist_for_folder(cmd.substring(5));
|
2019-11-14 19:42:02 +00:00
|
|
|
_player->play(p);
|
|
|
|
//} else if (cmd.equals("ls")) {
|
|
|
|
// _execute_command_ls("/");
|
|
|
|
//} else if (cmd.startsWith("ls ")) {
|
|
|
|
// _execute_command_ls(cmd.substring(3));
|
2019-11-16 22:03:13 +00:00
|
|
|
} else if (cmd.equals("play")) {
|
|
|
|
_player->play();
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
} else if (cmd.equals("stop")) {
|
|
|
|
_player->stop();
|
|
|
|
} else if (cmd.equals("help")) {
|
|
|
|
_execute_command_help();
|
|
|
|
} else if (cmd.equals("-")) {
|
|
|
|
_player->vol_down();
|
|
|
|
} else if (cmd.equals("+")) {
|
|
|
|
_player->vol_up();
|
2019-11-16 22:03:13 +00:00
|
|
|
} else if (cmd.startsWith("volume=")) {
|
|
|
|
uint8_t vol = cmd.substring(7).toInt();
|
|
|
|
_player->set_volume(vol);
|
|
|
|
} else if (cmd.equals("track_prev")) {
|
2019-08-09 04:27:33 +00:00
|
|
|
_player->track_prev();
|
2019-11-16 22:03:13 +00:00
|
|
|
} else if (cmd.equals("track_next")) {
|
2019-08-09 04:27:33 +00:00
|
|
|
_player->track_next();
|
2019-11-16 22:03:13 +00:00
|
|
|
} else if (cmd.startsWith("track=")) {
|
|
|
|
uint8_t track = cmd.substring(6).toInt();
|
|
|
|
_player->set_track(track);
|
2019-11-13 05:51:38 +00:00
|
|
|
} else if (cmd.equals("ids")) {
|
2019-11-14 19:42:02 +00:00
|
|
|
_pm->dump_ids();
|
2019-11-16 22:03:13 +00:00
|
|
|
} else if (cmd.equals("reset_vs1053")) {
|
|
|
|
_player->stop();
|
|
|
|
_player->init();
|
|
|
|
} else if (cmd.equals("reboot")) {
|
|
|
|
ESP.restart();
|
2019-11-16 23:35:23 +00:00
|
|
|
} 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();
|
2019-08-09 04:27:33 +00:00
|
|
|
} else {
|
|
|
|
ERROR("Unknown command: %s\n", cmd.c_str());
|
2019-11-16 22:03:13 +00:00
|
|
|
return false;
|
2019-08-09 04:27:33 +00:00
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
return true;
|
2019-08-09 04:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::_execute_command_ls(String path) {
|
|
|
|
INFO("Listing contents of %s:\n", path.c_str());
|
2019-11-14 19:42:02 +00:00
|
|
|
// TODO
|
|
|
|
//std::list<String> files = _player->ls(path);
|
|
|
|
//for(std::list<String>::iterator it=files.begin(); it!=files.end(); ++it) {
|
|
|
|
// INFO(" %s\n", (*it).c_str());
|
|
|
|
//}
|
2019-11-13 05:51:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 04:27:33 +00:00
|
|
|
void Controller::_execute_command_help() {
|
|
|
|
INFO("Valid commands are:");
|
|
|
|
INFO(" help - Displays this help\n");
|
2019-11-14 19:42:02 +00:00
|
|
|
//INFO(" ls [dir] - Lists the contents of [dir] or, if not given, of /\n");
|
2019-11-13 05:51:38 +00:00
|
|
|
INFO(" ids - Lists all known ID-to-folder mappings\n");
|
2019-08-09 04:27:33 +00:00
|
|
|
INFO(" play [id] - Plays the album with the given id\n");
|
|
|
|
INFO(" stop - Stops playback\n");
|
|
|
|
INFO(" - / + - Decrease or increase the volume\n");
|
|
|
|
INFO(" p / n - Previous or next track\n");
|
|
|
|
}
|
2019-08-11 15:15:22 +00:00
|
|
|
|
|
|
|
void Controller::_check_buttons() {
|
2019-11-16 22:03:13 +00:00
|
|
|
TRACE("check_buttons running...\n");
|
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
if (BTN_PREV() && _debounce_button(0)) {
|
2019-11-14 19:42:02 +00:00
|
|
|
if (_state == NORMAL) {
|
|
|
|
_player->track_prev();
|
|
|
|
} else {
|
|
|
|
DEBUG("Ignoring btn_prev because state is LOCKED.\n");
|
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
} else if (BTN_VOL_UP() && _debounce_button(1)) {
|
2019-08-11 15:15:22 +00:00
|
|
|
_player->vol_up();
|
2019-11-10 13:45:33 +00:00
|
|
|
} else if (BTN_VOL_DOWN() && _debounce_button(2)) {
|
2019-08-11 15:15:22 +00:00
|
|
|
_player->vol_down();
|
2019-11-10 13:45:33 +00:00
|
|
|
} else if (BTN_NEXT() && _debounce_button(3)) {
|
2019-11-14 19:42:02 +00:00
|
|
|
if (_state == NORMAL) {
|
|
|
|
_player->track_next();
|
|
|
|
} else {
|
|
|
|
DEBUG("Ignoring btn_next because state is LOCKED.\n");
|
|
|
|
}
|
2019-08-11 15:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:45:33 +00:00
|
|
|
bool Controller::_debounce_button(uint8_t index) {
|
2019-08-11 15:15:22 +00:00
|
|
|
bool ret = false;
|
2019-11-10 13:45:33 +00:00
|
|
|
if (_button_last_pressed_at[index] + DEBOUNCE_MILLIS < millis()) {
|
|
|
|
DEBUG("Button %d pressed.\n", index);
|
|
|
|
ret = true;
|
2019-08-11 15:15:22 +00:00
|
|
|
}
|
2019-11-10 13:45:33 +00:00
|
|
|
_button_last_pressed_at[index] = millis();
|
2019-08-11 15:15:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
|
|
|
|
String Controller::json() {
|
|
|
|
DynamicJsonDocument json(1024);
|
|
|
|
json["_type"] = "controller";
|
|
|
|
switch(_state) {
|
|
|
|
case LOCKED: json["state"] = "locked"; break;
|
|
|
|
case LOCKING: json["state"] = "locking"; break;
|
|
|
|
case NORMAL: json["state"] = "normal"; break;
|
2019-08-14 19:01:01 +00:00
|
|
|
}
|
2019-11-16 23:35:23 +00:00
|
|
|
json["is_rfid_present"] = _rfid_present;
|
2019-11-16 22:03:13 +00:00
|
|
|
JsonObject rfid = json.createNestedObject("last_rfid");
|
|
|
|
rfid["uid"] = _last_rfid_uid;
|
|
|
|
rfid["data"] = _last_rfid_data;
|
|
|
|
return json.as<String>();
|
2019-08-14 19:01:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
void Controller::send_player_status() {
|
2019-11-16 23:35:23 +00:00
|
|
|
TRACE("In send_player_status()...\n");
|
2019-11-16 22:03:13 +00:00
|
|
|
|
|
|
|
if (_http_server->ws->count() > 0) {
|
|
|
|
_http_server->ws->textAll(_player->json());
|
|
|
|
_http_server->ws->textAll(_player->position_json());
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 19:01:01 +00:00
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
void Controller::send_playlist_manager_status() {
|
|
|
|
TRACE("In send_playlist_manager_status()...\n");
|
|
|
|
if (_http_server->ws->count() > 0) {
|
|
|
|
_http_server->ws->textAll(_pm->json());
|
|
|
|
}
|
2019-08-14 19:01:01 +00:00
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
|
|
|
|
void Controller::send_position() {
|
|
|
|
TRACE("In send_position()...\n");
|
2019-11-16 23:35:23 +00:00
|
|
|
if (_http_server->ws->count() > 0 && _player->is_playing()) {
|
2019-11-16 22:03:13 +00:00
|
|
|
_http_server->ws->textAll(_player->position_json());
|
|
|
|
}
|
|
|
|
_last_position_info_at = millis();
|
|
|
|
}
|
|
|
|
|
2019-11-16 23:35:23 +00:00
|
|
|
void Controller::send_controller_status() {
|
|
|
|
TRACE("In send_controller_status()...\n");
|
|
|
|
if (_http_server->ws->count() > 0) {
|
|
|
|
_http_server->ws->textAll(json());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 22:03:13 +00:00
|
|
|
void Controller::inform_new_client(AsyncWebSocketClient* client) {
|
|
|
|
String s;
|
|
|
|
s += _pm->json();
|
|
|
|
s += '\n';
|
|
|
|
s += _player->json();
|
|
|
|
s += '\n';
|
|
|
|
s += _player->position_json();
|
2019-11-16 23:35:23 +00:00
|
|
|
s += '\n';
|
|
|
|
s += json();
|
2019-11-16 22:03:13 +00:00
|
|
|
client->text(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::queue_command(String s) {
|
|
|
|
_cmd_queue = s;
|
|
|
|
}
|