-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwrcntrl.cc
167 lines (142 loc) · 5.33 KB
/
pwrcntrl.cc
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <cxxopts.hpp>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include "webpowerswitchmanager.h"
// must persist beyond life of method
static std::vector<std::string> arguments;
std::vector<char*> loadCommandLineArguments(int iArgc, char* szArgv[], std::string optionsFilename) {
std::vector<char*> cla;
for (int iArg = 0; iArg < iArgc; iArg++, szArgv++) {
cla.push_back(*szArgv);
}
std::ifstream optionsFile(optionsFilename);
if (optionsFile.good()) {
const std::regex wsRE("\\s+");
arguments.clear();
for (std::string line; std::getline(optionsFile, line); ) {
if (line.empty() || line[0] == '#') {
continue;
}
std::copy(
std::sregex_token_iterator(line.begin(), line.end(), wsRE, -1),
std::sregex_token_iterator(),
std::back_inserter<std::vector<std::string>>(arguments));
}
for (const auto& argument : arguments) {
cla.push_back(const_cast<char*>(argument.c_str()));
}
}
return cla;
}
int main(int iArgc, char* szArgv[]) {
cxxopts::Options options(szArgv[0], "pwrcntrl: control web power switches");
std::string optionsFilename(getenv("HOME"));
optionsFilename += "/.pwrcntrlrc";
options
.positional_help("(all|switch_name|outlet_name) ([show]|on|off|toggle|cycle)")
.show_positional_help();
options
.allow_unrecognised_options()
.add_options()
("command", "show|on|off|toggle|cycle: show, turn on, turn off, toggle or cycle outlet.", cxxopts::value<std::string>())
("credentials", "provide pairs of username:password to use on switch(es).", cxxopts::value<std::vector<std::string>>())
("help", "show help")
("options", "<option_file>: read command line parameters from option_file.", cxxopts::value<std::string>()->default_value(optionsFilename))
("r,reset", "even if switch locations are known, go find them again.")
("t,target", "'all'|<name_of_switch|name_of_outlet", cxxopts::value<std::string>())
("v,verbose", "increate verbosity of output")
;
options.parse_positional({"target", "command"});
auto commandLineArguments = loadCommandLineArguments(iArgc, szArgv, optionsFilename);
auto optionsResult = options.parse(commandLineArguments.size(), &commandLineArguments[0]);
if (optionsResult.count("options") && optionsResult["options"].as<std::string>() != optionsFilename) {
commandLineArguments = loadCommandLineArguments(iArgc, szArgv, optionsResult["options"].as<std::string>());
optionsResult = options.parse(commandLineArguments.size(), &commandLineArguments[0]);
}
std::string target;
if (optionsResult.count("target")) {
target = optionsResult["target"].as<std::string>();
} else {
std::cout << options.help() << std::endl;
return 0;
}
if (optionsResult.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}
if (optionsResult.count("verbose") > 1) {
for (auto argument : commandLineArguments) {
std::cout << "commandLineArguments: " << argument << std::endl;
}
}
std::unique_ptr<WebPowerSwitchManager> wpsm(new WebPowerSwitchManager);
// Add credentials
if (optionsResult.count("credentials") != 0) {
for (const auto& credential : optionsResult["credentials"].as<std::vector<std::string>>()) {
auto separator = credential.find(":");
if (separator == std::string::npos) {
std::cerr << "Invalid credential missing separator ':' (" << credential << ")" << std::endl;
return -1;
}
auto username = credential.substr(0, separator);
auto password = credential.substr(separator + 1);
if (!wpsm->addUsernamePassword(username, password)) {
std::cerr << "Failed to add username (" << username << ") and password (" << password << ")." << std::endl;
return -1;
}
}
}
// Verbosity
if (optionsResult.count("verbose") != 0) {
wpsm->verbose(optionsResult.count("verbose"));
}
if (optionsResult.count("reset") != 0) {
wpsm->resetCache();
}
// If 'all', then only implement show.
if (target == "all") {
wpsm->dumpSwitches(std::cout);
return 0;
}
auto wps = wpsm->getSwitch(target, true);
if (wps != nullptr) {
wps->dumpOutlets(std::cout);
return 0;
}
wps = wpsm->getSwitchByIp(target, true);
if (wps != nullptr) {
wps->dumpOutlets(std::cout);
return 0;
}
wps = wpsm->getSwitchByOutletName(target);
if (wps == nullptr) {
std::cout << "unknown outlet (or switch): " << target << std::endl;
return -1;
}
std::cout << wps->name() << ": " << *(wps->getOutlet(target)) << std::endl;
std::string command;
if (optionsResult.count("command")) {
command = optionsResult["command"].as<std::string>();
} else {
// no command
return 0;
}
if (strncasecmp(command.c_str(), "on", 2) == 0) {
wps->on(target);
} else if (strncasecmp(command.c_str(), "off", 3) == 0) {
wps->off(target);
} else if (strncasecmp(command.c_str(), "cycle", 5) == 0) {
wps->toggle(target);
std::cout << wps->name() << ": " << *(wps->getOutlet(target)) << std::endl;
sleep(5);
wps->toggle(target);
} else if (strncasecmp(command.c_str(), "toggle", 5) == 0) {
wps->toggle(target);
} else {
std::cout << "ERROR: unrecognized command: " << command << std::endl;
return 0;
}
std::cout << wps->name() << ": " << *(wps->getOutlet(target)) << std::endl;
return 0;
}