This repository was archived by the owner on Dec 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathCommandManager.cpp
More file actions
136 lines (108 loc) · 4.5 KB
/
CommandManager.cpp
File metadata and controls
136 lines (108 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "CommandManager.hpp"
#include "tasks/tasks.hpp"
CommandManager::CommandManager(ProjectConfig* deviceConfig)
: deviceConfig(deviceConfig) {}
const CommandType CommandManager::getCommandType(JsonVariant& command) {
if (!command["command"].is<const char*>())
return CommandType::None;
if (auto search = commandMap.find(command["command"]);
search != commandMap.end())
return search->second;
return CommandType::None;
}
bool CommandManager::hasDataField(JsonVariant& command) {
return command["data"].is<JsonObject>();
}
void CommandManager::handleCommands(CommandsPayload commandsPayload) {
if (!commandsPayload.data["commands"].is<JsonArray>()) {
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);
switch (command_type) {
case CommandType::SET_WIFI: {
if (!this->hasDataField(command))
// malformed command, lacked data field
break;
if (!command["data"]["ssid"].is<const char*>() ||
!command["data"]["password"].is<const char*>())
break;
std::string customNetworkName = "main";
if (command["data"]["network_name"].is<const char*>())
customNetworkName = command["data"]["network_name"].as<std::string>();
this->deviceConfig->setWifiConfig(customNetworkName,
command["data"]["ssid"],
command["data"]["password"],
0, // channel, should this be zero?
0, // power, should this be zero?
false, false);
this->deviceConfig->setHasWiFiCredentials(true, false);
this->deviceConfig->setDeviceMode(DeviceMode::WIFI_MODE, true);
log_i("[CommandManager] Switching to WiFi mode after receiving credentials");
break;
}
case CommandType::SET_MDNS: {
if (!this->hasDataField(command))
break;
if (!command["data"]["hostname"].is<const char*>() ||
!strlen(command["data"]["hostname"]))
break;
this->deviceConfig->setMDNSConfig(command["data"]["hostname"],
"openiristracker", false);
break;
}
case CommandType::PING: {
Serial.println("PONG \n\r");
break;
}
case CommandType::SWITCH_MODE: {
if (!this->hasDataField(command))
break;
if (!command["data"]["mode"].is<int>())
break;
int modeValue = command["data"]["mode"];
DeviceMode newMode = static_cast<DeviceMode>(modeValue);
DeviceMode currentMode = this->deviceConfig->getDeviceModeConfig().mode;
// If switching to USB mode from WiFi or AP mode, disconnect WiFi immediately
if (newMode == DeviceMode::USB_MODE &&
(currentMode == DeviceMode::WIFI_MODE || currentMode == DeviceMode::AP_MODE)) {
log_i("[CommandManager] Immediately switching to USB mode");
WiFi.disconnect(true);
}
this->deviceConfig->setDeviceMode(newMode, true);
log_i("[CommandManager] Switching to mode: %d", modeValue);
// Removed automatic restart to allow explicit control via RESTART_DEVICE command
// if (!(newMode == DeviceMode::USB_MODE &&
// (currentMode == DeviceMode::WIFI_MODE || currentMode == DeviceMode::AP_MODE) &&
// wifiStateManager.getCurrentState() == WiFiState_e::WiFiState_Connecting)) {
// OpenIrisTasks::ScheduleRestart(2000);
// }
break;
}
case CommandType::WIPE_WIFI_CREDS: {
auto networks = this->deviceConfig->getWifiConfigs();
for (auto& network : networks) {
this->deviceConfig->deleteWifiConfig(network.name, false);
}
this->deviceConfig->setHasWiFiCredentials(false, false);
this->deviceConfig->setDeviceMode(DeviceMode::USB_MODE, true);
log_i("[CommandManager] Switching to USB mode after wiping credentials");
// Removed automatic restart to allow processing of all commands in payload
break;
}
case CommandType::RESTART_DEVICE: {
log_i("[CommandManager] Explicit restart requested");
OpenIrisTasks::ScheduleRestart(2000);
break;
}
default:
break;
}
}