server: add --device-error-limit
This commit is contained in:
parent
1c6fa70ab6
commit
a2e133a11f
@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.0)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
add_compile_options(-Wno-psabi)
|
||||
|
||||
project(inverter-tools VERSION 1.2.2)
|
||||
project(inverter-tools VERSION 1.3.0)
|
||||
|
||||
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set(CMAKE_INSTALL_PREFIX /usr/local/bin)
|
||||
|
@ -21,6 +21,7 @@ enum {
|
||||
LO_DELAY,
|
||||
LO_FORMAT,
|
||||
LO_DEVICE,
|
||||
LO_DEVICE_ERROR_LIMIT,
|
||||
LO_USB_VENDOR_ID,
|
||||
LO_USB_DEVICE_ID,
|
||||
LO_SERIAL_NAME,
|
||||
|
@ -30,8 +30,11 @@ static void usage(const char* progname) {
|
||||
" --device <DEVICE>: 'usb' (default), 'serial' or 'pseudo'\n"
|
||||
" --timeout <TIMEOUT>: Device timeout in ms (default: " << voltronic::Device::TIMEOUT << ")\n"
|
||||
" --cache-timeout <TIMEOUT>\n"
|
||||
" --delay <DELAY>: Delay between commands in ms (default: 0)\n"
|
||||
" Default: " << server::Server::CACHE_TIMEOUT << "\n"
|
||||
" --delay <DELAY>: Delay between commands in ms (default: " << server::Server::DELAY << ")\n"
|
||||
" Cache validity time, in ms (default: " << server::Server::CACHE_TIMEOUT << ")\n"
|
||||
" --device-error-limit <LIMIT>\n"
|
||||
" Default: " << server::Server::DEVICE_ERROR_LIMIT << "\n"
|
||||
" --verbose: Be verbose\n"
|
||||
"\n";
|
||||
|
||||
@ -57,7 +60,8 @@ int main(int argc, char *argv[]) {
|
||||
// common params
|
||||
u64 timeout = voltronic::Device::TIMEOUT;
|
||||
u64 cacheTimeout = server::Server::CACHE_TIMEOUT;
|
||||
u64 delay = 0;
|
||||
u64 delay = server::Server::DELAY;
|
||||
u32 deviceErrorLimit = server::Server::DEVICE_ERROR_LIMIT;
|
||||
bool verbose = false;
|
||||
|
||||
// server params
|
||||
@ -79,22 +83,23 @@ int main(int argc, char *argv[]) {
|
||||
try {
|
||||
int opt;
|
||||
struct option long_options[] = {
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"verbose", no_argument, nullptr, LO_VERBOSE},
|
||||
{"timeout", required_argument, nullptr, LO_TIMEOUT},
|
||||
{"cache-timeout", required_argument, nullptr, LO_CACHE_TIMEOUT},
|
||||
{"delay", required_argument, nullptr, LO_DELAY},
|
||||
{"device", required_argument, nullptr, LO_DEVICE},
|
||||
{"usb-vendor-id", required_argument, nullptr, LO_USB_VENDOR_ID},
|
||||
{"usb-device-id", required_argument, nullptr, LO_USB_DEVICE_ID},
|
||||
{"serial-name", required_argument, nullptr, LO_SERIAL_NAME},
|
||||
{"serial-baud-rate", required_argument, nullptr, LO_SERIAL_BAUD_RATE},
|
||||
{"serial-data-bits", required_argument, nullptr, LO_SERIAL_DATA_BITS},
|
||||
{"serial-stop-bits", required_argument, nullptr, LO_SERIAL_STOP_BITS},
|
||||
{"serial-parity", required_argument, nullptr, LO_SERIAL_PARITY},
|
||||
{"host", required_argument, nullptr, LO_HOST},
|
||||
{"port", required_argument, nullptr, LO_PORT},
|
||||
{nullptr, 0, nullptr, 0}
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"verbose", no_argument, nullptr, LO_VERBOSE},
|
||||
{"timeout", required_argument, nullptr, LO_TIMEOUT},
|
||||
{"cache-timeout", required_argument, nullptr, LO_CACHE_TIMEOUT},
|
||||
{"delay", required_argument, nullptr, LO_DELAY},
|
||||
{"device", required_argument, nullptr, LO_DEVICE},
|
||||
{"device-error-limit", required_argument, nullptr, LO_DEVICE_ERROR_LIMIT},
|
||||
{"usb-vendor-id", required_argument, nullptr, LO_USB_VENDOR_ID},
|
||||
{"usb-device-id", required_argument, nullptr, LO_USB_DEVICE_ID},
|
||||
{"serial-name", required_argument, nullptr, LO_SERIAL_NAME},
|
||||
{"serial-baud-rate", required_argument, nullptr, LO_SERIAL_BAUD_RATE},
|
||||
{"serial-data-bits", required_argument, nullptr, LO_SERIAL_DATA_BITS},
|
||||
{"serial-stop-bits", required_argument, nullptr, LO_SERIAL_STOP_BITS},
|
||||
{"serial-parity", required_argument, nullptr, LO_SERIAL_PARITY},
|
||||
{"host", required_argument, nullptr, LO_HOST},
|
||||
{"port", required_argument, nullptr, LO_PORT},
|
||||
{nullptr, 0, nullptr, 0}
|
||||
};
|
||||
|
||||
bool getoptError = false; // FIXME
|
||||
@ -147,6 +152,10 @@ int main(int argc, char *argv[]) {
|
||||
delay = std::stoull(arg);
|
||||
break;
|
||||
|
||||
case LO_DEVICE_ERROR_LIMIT:
|
||||
deviceErrorLimit = static_cast<u32>(std::stoul(arg));
|
||||
break;
|
||||
|
||||
case LO_USB_VENDOR_ID:
|
||||
try {
|
||||
if (arg.size() != 4)
|
||||
@ -275,6 +284,7 @@ int main(int argc, char *argv[]) {
|
||||
server::Server server(dev);
|
||||
server.setVerbose(verbose);
|
||||
server.setDelay(delay);
|
||||
server.setDeviceErrorLimit(deviceErrorLimit);
|
||||
server.setCacheTimeout(cacheTimeout);
|
||||
|
||||
server.start(host, port);
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
#endif //INVERTER_TOOLS_NUMERIC_TYPES_H
|
||||
|
@ -25,8 +25,10 @@ Server::Server(std::shared_ptr<voltronic::Device> device)
|
||||
: sock_(0)
|
||||
, port_(0)
|
||||
, cacheTimeout_(CACHE_TIMEOUT)
|
||||
, delay_(0)
|
||||
, delay_(DELAY)
|
||||
, endExecutionTime_(0)
|
||||
, deviceErrorLimit_(DEVICE_ERROR_LIMIT)
|
||||
, deviceErrorCounter_(0)
|
||||
, verbose_(false)
|
||||
, device_(std::move(device)) {
|
||||
client_.setDevice(device_);
|
||||
@ -45,6 +47,10 @@ void Server::setDelay(u64 delay) {
|
||||
delay_ = delay;
|
||||
}
|
||||
|
||||
void Server::setDeviceErrorLimit(u32 deviceErrorLimit) {
|
||||
deviceErrorLimit_ = deviceErrorLimit;
|
||||
}
|
||||
|
||||
Server::~Server() {
|
||||
if (sock_ > 0)
|
||||
close(sock_);
|
||||
@ -146,9 +152,13 @@ std::shared_ptr<p18::response_type::BaseResponse> Server::executeCommand(p18::Co
|
||||
};
|
||||
cache_[commandType] = cr;
|
||||
|
||||
deviceErrorCounter_ = 0;
|
||||
return response;
|
||||
}
|
||||
catch (voltronic::DeviceError& e) {
|
||||
deviceErrorCounter_++;
|
||||
if (!shutdownCaught && deviceErrorCounter_ >= deviceErrorLimit_)
|
||||
shutdownCaught = true;
|
||||
throw std::runtime_error("device error: " + std::string(e.what()));
|
||||
}
|
||||
catch (voltronic::TimeoutError& e) {
|
||||
|
@ -44,6 +44,8 @@ private:
|
||||
u64 cacheTimeout_;
|
||||
u64 delay_;
|
||||
u64 endExecutionTime_;
|
||||
u32 deviceErrorLimit_;
|
||||
u32 deviceErrorCounter_;
|
||||
std::map<p18::CommandType, CachedResponse> cache_;
|
||||
|
||||
std::mutex threads_mutex_;
|
||||
@ -53,6 +55,8 @@ private:
|
||||
|
||||
public:
|
||||
static const u64 CACHE_TIMEOUT = 1000;
|
||||
static const u32 DEVICE_ERROR_LIMIT = 10;
|
||||
static const u64 DELAY = 0;
|
||||
|
||||
volatile std::atomic<bool> sigCaught = 0;
|
||||
|
||||
@ -62,6 +66,8 @@ public:
|
||||
void setVerbose(bool verbose);
|
||||
void setCacheTimeout(u64 timeout);
|
||||
void setDelay(u64 delay);
|
||||
void setDeviceErrorLimit(u32 deviceErrorLimit);
|
||||
|
||||
void start(std::string& host, int port);
|
||||
|
||||
bool verbose() const { return verbose_; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user