mirror of
https://github.com/EyeTrackVR/OpenIris.git
synced 2025-11-04 15:39:42 +08:00
Merge branch 'fix/serial-manager-invalid-json' into beta
# Conflicts: # ESP/lib/src/io/Serial/SerialManager.cpp
This commit is contained in:
commit
97d9c3f5e5
@ -3,61 +3,71 @@
|
|||||||
CommandManager::CommandManager(ProjectConfig *deviceConfig) : deviceConfig(deviceConfig) {}
|
CommandManager::CommandManager(ProjectConfig *deviceConfig) : deviceConfig(deviceConfig) {}
|
||||||
|
|
||||||
|
|
||||||
const CommandType CommandManager::getCommandType(Command &command){
|
const CommandType CommandManager::getCommandType(JsonVariant &command){
|
||||||
if (!command.data.containsKey("command"))
|
if (!command.containsKey("command"))
|
||||||
return CommandType::None;
|
return CommandType::None;
|
||||||
|
|
||||||
if (auto search = commandMap.find(command.data["command"]); search != commandMap.end())
|
if (auto search = commandMap.find(command["command"]); search != commandMap.end())
|
||||||
return search->second;
|
return search->second;
|
||||||
|
|
||||||
return CommandType::None;
|
return CommandType::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CommandManager::hasHasDataField(Command &command) {
|
bool CommandManager::hasDataField(JsonVariant &command) {
|
||||||
return command.data.containsKey("data");
|
return command.containsKey("data");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandManager::handleCommand(Command command) {
|
void CommandManager::handleCommands(CommandsPayload commandsPayload){
|
||||||
|
if (!commandsPayload.data.containsKey("commands")){
|
||||||
|
log_e("Json data sent not supported, lacks commands field");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(JsonVariant commandData : commandsPayload.data["commands"].as<JsonArray>()) {
|
||||||
|
this->handleCommand(commandData);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->deviceConfig->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CommandManager::handleCommand(JsonVariant command) {
|
||||||
auto command_type = this->getCommandType(command);
|
auto command_type = this->getCommandType(command);
|
||||||
|
|
||||||
switch(command_type)
|
switch(command_type)
|
||||||
{
|
{
|
||||||
case CommandType::SET_WIFI: {
|
case CommandType::SET_WIFI: {
|
||||||
if (!this->hasHasDataField(command))
|
if (!this->hasDataField(command))
|
||||||
// malformed command, lacked data field
|
// malformed command, lacked data field
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
if(!command["data"].containsKey("ssid") || !command["data"].containsKey("password"))
|
||||||
if(!command.data["data"].containsKey("ssid") || !command.data["data"].containsKey("password"))
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
std::string customNetworkName = "main";
|
std::string customNetworkName = "main";
|
||||||
if (command.data["data"].containsKey("network_name"))
|
if (command["data"].containsKey("network_name"))
|
||||||
customNetworkName = command.data["data"]["network_name"].as<std::string>();
|
customNetworkName = command["data"]["network_name"].as<std::string>();
|
||||||
|
|
||||||
this->deviceConfig->setWifiConfig(
|
this->deviceConfig->setWifiConfig(
|
||||||
customNetworkName,
|
customNetworkName,
|
||||||
command.data["data"]["ssid"],
|
command["data"]["ssid"],
|
||||||
command.data["data"]["password"],
|
command["data"]["password"],
|
||||||
0, // channel, should this be zero?
|
0, // channel, should this be zero?
|
||||||
0, // power, should this be zero?
|
0, // power, should this be zero?
|
||||||
false,
|
false,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
// we purposefully save here
|
|
||||||
this->deviceConfig->save();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CommandType::SET_MDNS: {
|
case CommandType::SET_MDNS: {
|
||||||
if (!this->hasHasDataField(command))
|
if (!this->hasDataField(command))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if(!command.data["data"].containsKey("hostname") || !strlen(command.data["data"]["hostname"]))
|
if(!command["data"].containsKey("hostname") || !strlen(command["data"]["hostname"]))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
this->deviceConfig->setMDNSConfig(
|
this->deviceConfig->setMDNSConfig(
|
||||||
command.data["data"]["hostname"],
|
command["data"]["hostname"],
|
||||||
"openiristracker",
|
"openiristracker",
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|||||||
@ -13,7 +13,7 @@ enum CommandType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Command {
|
struct CommandsPayload {
|
||||||
JsonDocument data;
|
JsonDocument data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -28,12 +28,13 @@ const std::unordered_map<std::string, CommandType> commandMap = {
|
|||||||
|
|
||||||
ProjectConfig* deviceConfig;
|
ProjectConfig* deviceConfig;
|
||||||
|
|
||||||
bool hasHasDataField(Command &command);
|
bool hasDataField(JsonVariant &command);
|
||||||
|
void handleCommand(JsonVariant command);
|
||||||
|
const CommandType getCommandType(JsonVariant &command);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CommandManager(ProjectConfig *deviceConfig);
|
CommandManager(ProjectConfig *deviceConfig);
|
||||||
void handleCommand(Command command);
|
void handleCommands(CommandsPayload commandsPayload);
|
||||||
const CommandType getCommandType(Command &command);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#include "SerialManager.hpp"
|
#include "SerialManager.hpp"
|
||||||
|
|
||||||
SerialManager::SerialManager(CommandManager* commandManager, ProjectConfig& configManager)
|
SerialManager::SerialManager(CommandManager* commandManager)
|
||||||
: commandManager(commandManager), configManager(configManager) {}
|
: commandManager(commandManager) {}
|
||||||
|
|
||||||
#ifdef ETVR_EYE_TRACKER_USB_API
|
#ifdef ETVR_EYE_TRACKER_USB_API
|
||||||
void SerialManager::send_frame() {
|
void SerialManager::send_frame() {
|
||||||
@ -61,21 +61,18 @@ void SerialManager::init() {
|
|||||||
|
|
||||||
void SerialManager::run() {
|
void SerialManager::run() {
|
||||||
if (Serial.available()) {
|
if (Serial.available()) {
|
||||||
auto serialData = Serial.readString();
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
DeserializationError deserializationError = deserializeJson(doc, serialData);
|
DeserializationError deserializationError = deserializeJson(doc, Serial);
|
||||||
|
|
||||||
if (deserializationError) {
|
if (deserializationError) {
|
||||||
log_e("Command deserialization failed: %s",
|
log_e("Command deserialization failed: %s",
|
||||||
deserializationError.c_str());
|
deserializationError.c_str());
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Command command = {doc};
|
CommandsPayload commands = {doc};
|
||||||
|
this->commandManager->handleCommands(commands);
|
||||||
configManager.setDeviceConfig("", "", serialData.c_str(), 81, true);
|
|
||||||
configManager.deviceConfigSave();
|
|
||||||
this->commandManager->handleCommand(command);
|
|
||||||
configManager.save(); // in case we receive something totally different than supported command, save anyway and restart
|
|
||||||
}
|
}
|
||||||
#ifdef ETVR_EYE_TRACKER_USB_API
|
#ifdef ETVR_EYE_TRACKER_USB_API
|
||||||
else {
|
else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user