Quick rewrite

This commit is contained in:
TraYali 2024-04-09 12:21:21 +02:00
parent 266dcefdfd
commit e6928a3aa6
27 changed files with 6041 additions and 773 deletions

View file

@ -19,16 +19,32 @@ int main(int argc, char **argv) {
signal(SIGTERM, sigHandler);
signal(SIGABRT, sigHandler);
std::string version{"v2.2w"};
std::string version{"v2.3"};
std::cout << version << std::endl;
CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"};
hoymilesClient.set_version_flag("-v,--version", version);
std::string serialDeviceAddress{""};
std::string serialDeviceAddressHelp{"Serial device address"};
hoymilesClient.add_option<std::string>("-d,--serial_device_address", serialDeviceAddress, serialDeviceAddressHelp)->group("Serial");
bool rtuMode{false};
std::string rtuModeHelp{"Work in RTU mode"};
hoymilesClient.add_flag<bool>("-r,--rtu", rtuMode, rtuModeHelp)->needs(hoymilesClient.get_option("-d"))->group("Serial");
int rtuDeviceAddress{1};
std::string rtuDeviceAddressHelp{"Address on RS485 {default: }" + std::to_string(rtuDeviceAddress) + "}"};
hoymilesClient.add_option<int>("-a,--rt_device_address", rtuDeviceAddress, rtuDeviceAddressHelp)->group("Serial");
std::string ipAddress{"127.0.0.1"};
std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"};
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required()->group("Networking");
std::string ipAddressHelp{"Ipv4 address of DTU {default: " + ipAddress + "}"};
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->group("Networking");
bool tcpMode{false};
std::string tcpModeHelp{"Work in Ipv4 mode"};
hoymilesClient.add_flag<bool>("-t,--tcp", tcpMode, tcpModeHelp)->needs(hoymilesClient.get_option("-i"))->group("Networking");
int port{502};
std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"};
@ -39,21 +55,25 @@ int main(int argc, char **argv) {
"todayProduction [tdP]\n - totalProduction [ttP]\n - temperature [t]\n - operatingStatus [oS]\n - alarmCode [aC]\n - alarmCount [aCnt]\n - linkStatus [lS]"};
hoymilesClient.add_option<std::vector<std::string>>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',')->group("Parameters");
bool allParameters = false;
bool allParameters{false};
std::string allParametersHelp{"Fetch all parameters"};
hoymilesClient.add_flag<bool>("-a,--all_parameters", allParameters, allParametersHelp)->group("Parameters");
hoymilesClient.add_flag<bool>("-A,--all_parameters", allParameters, allParametersHelp)->group("Parameters");
bool shortNames = false;
bool shortNames{false};
std::string shortNamesHelp{"Print short parameter names"};
hoymilesClient.add_flag<bool>("-s,--short", shortNames, shortNamesHelp)->group("Parameters");
bool getMicroinverters{false};
std::string getMicroinvertersHelp{"Print all microinverters instead of getting parameters"};
hoymilesClient.add_flag<bool>("-M,--get_microinverters", getMicroinverters, getMicroinvertersHelp)->group("Microinverters");
std::vector<long long> microinvertersToChoose{};
std::string microinvertersToChooseHelp{"List of microinverters to work on, delimited by ','; if omitted, all are selected"};
hoymilesClient.add_option<std::vector<long long>>("-m,--microinverters", microinvertersToChoose, microinvertersToChooseHelp)->delimiter(',')->group("Microinverters");
bool microinvertersGetTodayProduction{false};
std::string microinvertersGetTodayProductionHelp{"Show today production for microinverters"};
hoymilesClient.add_flag<bool>("-t,--today_production", microinvertersGetTodayProduction, microinvertersGetTodayProductionHelp)->group("Microinverters");
hoymilesClient.add_flag<bool>("-D,--today_production", microinvertersGetTodayProduction, microinvertersGetTodayProductionHelp)->group("Microinverters");
bool microinvertersGetTotalProduction{false};
std::string microinvertersGetTotalProductionHelp{"Show total production for microinverters"};
@ -83,12 +103,22 @@ int main(int argc, char **argv) {
std::cout << "Mapping out DTU" << std::endl;
auto startTime = std::chrono::high_resolution_clock::now();
Dtu dtu{ipAddress.c_str(), port};
std::string address;
int id;
if (tcpMode) {
address = ipAddress;
id = port;
}
if (rtuMode) {
address = serialDeviceAddress;
id = rtuDeviceAddress;
}
Dtu dtu{address.c_str(), id, rtuMode, tcpMode};
auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "DTU construction time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
std::cout << std::endl << std::endl;
while (!writeMode && (!dtu.empty() && ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)))) {
while ((!writeMode && !getMicroinverters) && (!dtu.empty() && ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)))) {
time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
startTime = std::chrono::high_resolution_clock::now();
@ -105,7 +135,11 @@ int main(int argc, char **argv) {
// std::cerr << dtu.modbusErrorMessage() << std::endl;
// }
if(writeMode) {
if(getMicroinverters) {
dtu.listOfMicroinverters();
}
if (writeMode) {
std::cout << "Starting DTU write" << std::endl;
startTime = std::chrono::high_resolution_clock::now();