summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-05-22 23:43:16 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-05-22 23:43:16 +0300
commita2e133a11fc7e91f18bf23723a975bf69fb218db (patch)
tree127e52b610d5e4106d69f692e7444a537b1e61fd
parent1c6fa70ab65e4b267b5163b18dd8743d3fdc8e8d (diff)
server: add --device-error-limit
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/common.h1
-rw-r--r--src/inverterd.cc46
-rw-r--r--src/numeric_types.h1
-rw-r--r--src/server/server.cc12
-rw-r--r--src/server/server.h6
6 files changed, 48 insertions, 20 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e2b98f6..6abeea6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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)
diff --git a/src/common.h b/src/common.h
index 7b9740e..c0b5a36 100644
--- a/src/common.h
+++ b/src/common.h
@@ -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,
diff --git a/src/inverterd.cc b/src/inverterd.cc
index 057431c..3373c54 100644
--- a/src/inverterd.cc
+++ b/src/inverterd.cc
@@ -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);
diff --git a/src/numeric_types.h b/src/numeric_types.h
index b581228..24fd203 100644
--- a/src/numeric_types.h
+++ b/src/numeric_types.h
@@ -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
diff --git a/src/server/server.cc b/src/server/server.cc
index c464b13..a025a7d 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -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) {
diff --git a/src/server/server.h b/src/server/server.h
index dc24ee8..705503f 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -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_; }