2024-03-20 11:28:43 +01:00
|
|
|
#include <chrono>
|
2024-03-12 23:26:23 +01:00
|
|
|
#include <iostream>
|
2024-04-05 16:48:06 +02:00
|
|
|
#include <signal.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2024-03-13 00:18:10 +01:00
|
|
|
#include <string>
|
2024-03-13 16:54:03 +01:00
|
|
|
#include <thread>
|
2024-03-20 11:28:43 +01:00
|
|
|
#include <vector>
|
2024-03-13 16:54:03 +01:00
|
|
|
|
2024-03-20 11:28:43 +01:00
|
|
|
#include "CLI11.hpp"
|
2024-03-13 00:18:10 +01:00
|
|
|
#include "modbus.h"
|
2024-03-20 11:28:43 +01:00
|
|
|
|
2024-03-16 21:15:15 +01:00
|
|
|
#include "dtu.h"
|
2024-03-12 23:26:23 +01:00
|
|
|
|
2024-03-20 16:15:07 +01:00
|
|
|
void sigHandler(int signal);
|
|
|
|
|
|
2024-03-20 11:28:43 +01:00
|
|
|
int main(int argc, char **argv) {
|
2024-03-20 16:15:07 +01:00
|
|
|
signal(SIGINT, sigHandler);
|
|
|
|
|
signal(SIGTERM, sigHandler);
|
|
|
|
|
signal(SIGABRT, sigHandler);
|
|
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
std::string version{"v2.2w"};
|
2024-03-20 19:11:10 +01:00
|
|
|
std::cout << version << std::endl;
|
|
|
|
|
|
2024-03-20 11:28:43 +01:00
|
|
|
CLI::App hoymilesClient{"Client for DTU-Pro/DTU-ProS"};
|
|
|
|
|
|
2024-04-05 16:48:06 +02:00
|
|
|
hoymilesClient.set_version_flag("-v,--version", version);
|
|
|
|
|
|
2024-03-20 11:28:43 +01:00
|
|
|
std::string ipAddress{"127.0.0.1"};
|
|
|
|
|
std::string ipAddressHelp{"ipv4 address of DTU {default: " + ipAddress + "}"};
|
2024-03-20 19:55:36 +01:00
|
|
|
hoymilesClient.add_option<std::string>("-i,--ip_address", ipAddress, ipAddressHelp)->required()->group("Networking");
|
2024-03-20 11:28:43 +01:00
|
|
|
|
|
|
|
|
int port{502};
|
|
|
|
|
std::string portHelp{"Port of DTU {default: " + std::to_string(port) + "}"};
|
2024-03-20 19:55:36 +01:00
|
|
|
hoymilesClient.add_option<int>("-p,--port", port, portHelp)->group("Networking");
|
2024-03-20 11:28:43 +01:00
|
|
|
|
|
|
|
|
std::vector<std::string> parametersToGet{};
|
2024-04-05 16:48:06 +02:00
|
|
|
std::string parametersToGetHelp{"List of parameters to fetch, delimited by ','; possible parameters:\n - pvVoltage [pvU]\n - pvCurrentMI [pvIMI]\n - pvCurrentHM [pvIHM]\n - gridVoltage [gU]\n - gridFrequency [gF]\n - pvPower [gP]\n - "
|
|
|
|
|
"todayProduction [tdP]\n - totalProduction [ttP]\n - temperature [t]\n - operatingStatus [oS]\n - alarmCode [aC]\n - alarmCount [aCnt]\n - linkStatus [lS]"};
|
2024-03-20 19:55:36 +01:00
|
|
|
hoymilesClient.add_option<std::vector<std::string>>("-P,--parameters", parametersToGet, parametersToGetHelp)->delimiter(',')->group("Parameters");
|
2024-03-20 11:28:43 +01:00
|
|
|
|
|
|
|
|
bool allParameters = false;
|
|
|
|
|
std::string allParametersHelp{"Fetch all parameters"};
|
2024-03-20 19:55:36 +01:00
|
|
|
hoymilesClient.add_flag<bool>("-a,--all_parameters", allParameters, allParametersHelp)->group("Parameters");
|
2024-03-20 11:28:43 +01:00
|
|
|
|
2024-03-20 22:48:59 +01:00
|
|
|
bool shortNames = false;
|
|
|
|
|
std::string shortNamesHelp{"Print short parameter names"};
|
|
|
|
|
hoymilesClient.add_flag<bool>("-s,--short", shortNames, shortNamesHelp)->group("Parameters");
|
2024-03-20 11:28:43 +01:00
|
|
|
|
2024-03-20 16:39:25 +01:00
|
|
|
std::vector<long long> microinvertersToGet{};
|
2024-03-20 19:55:36 +01:00
|
|
|
std::string microinvertersToGetHelp{"List of microinverters to fetch, delimited by ','; if omitted, all are fetched"};
|
|
|
|
|
hoymilesClient.add_option<std::vector<long long>>("-m,--microinverters", microinvertersToGet, microinvertersToGetHelp)->delimiter(',')->group("Microinverters");
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
bool microinvertersGetTodayProduction{false};
|
2024-03-20 22:48:59 +01:00
|
|
|
std::string microinvertersGetTodayProductionHelp{"Show today production for microinverters"};
|
|
|
|
|
hoymilesClient.add_flag<bool>("-t,--today_production", microinvertersGetTodayProduction, microinvertersGetTodayProductionHelp)->group("Microinverters");
|
|
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
bool microinvertersGetTotalProduction{false};
|
2024-03-20 22:48:59 +01:00
|
|
|
std::string microinvertersGetTotalProductionHelp{"Show total production for microinverters"};
|
|
|
|
|
hoymilesClient.add_flag<bool>("-T,--total_production", microinvertersGetTotalProduction, microinvertersGetTotalProductionHelp)->group("Microinverters");
|
2024-03-20 20:17:15 +01:00
|
|
|
|
2024-03-20 19:55:36 +01:00
|
|
|
bool ignoreNotConnected = false;
|
|
|
|
|
std::string ignoreNotConnectedHelp{"Ignore conn_error"};
|
|
|
|
|
hoymilesClient.add_flag<bool>("-I,--ignore_conn_error", ignoreNotConnected, ignoreNotConnectedHelp)->group("Debug");
|
2024-03-20 14:40:02 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
hoymilesClient.parse(argc, argv);
|
|
|
|
|
} catch (const CLI::ParseError &e) {
|
|
|
|
|
return hoymilesClient.exit(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "Mapping out DTU" << std::endl;
|
2024-03-20 11:28:43 +01:00
|
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
Dtu dtu{ipAddress.c_str(), port};
|
|
|
|
|
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;
|
2024-04-05 16:48:06 +02:00
|
|
|
std::cout << std::endl << std::endl;
|
2024-03-12 23:26:23 +01:00
|
|
|
|
2024-03-28 19:44:54 +01:00
|
|
|
while ((dtu.isConnected() || ignoreNotConnected) && (!parametersToGet.empty() || allParameters)) {
|
2024-04-05 16:48:06 +02:00
|
|
|
time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
|
|
|
|
2024-03-20 14:40:02 +01:00
|
|
|
startTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
dtu.updateMicroinverters(parametersToGet, allParameters, microinvertersToGet);
|
|
|
|
|
endTime = std::chrono::high_resolution_clock::now();
|
2024-04-05 16:48:06 +02:00
|
|
|
|
|
|
|
|
std::cout << "Pass time: " << std::put_time(localtime(&now), "%F %T") << std::endl;
|
2024-03-20 14:40:02 +01:00
|
|
|
std::cout << "DTU update time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms" << std::endl;
|
|
|
|
|
|
2024-03-20 22:48:59 +01:00
|
|
|
dtu.printMicroinverters(parametersToGet, allParameters, microinvertersToGet, shortNames, microinvertersGetTodayProduction, microinvertersGetTotalProduction);
|
2024-03-20 14:40:02 +01:00
|
|
|
std::cout << std::endl;
|
2024-03-20 11:28:43 +01:00
|
|
|
}
|
2024-03-28 19:44:54 +01:00
|
|
|
// if(dtu.modbusError()) {
|
|
|
|
|
// std::cerr << dtu.modbusErrorMessage() << std::endl;
|
|
|
|
|
// }
|
2024-03-15 11:46:44 +01:00
|
|
|
|
2024-03-20 11:28:43 +01:00
|
|
|
return 0;
|
2024-03-20 16:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sigHandler(int signal) {
|
2024-03-20 19:39:25 +01:00
|
|
|
std::cerr << "Interrupted\n";
|
2024-03-20 16:15:07 +01:00
|
|
|
exit(0);
|
2024-03-12 23:26:23 +01:00
|
|
|
}
|