aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-05-22 15:25:18 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-05-22 15:25:18 +0300
commita06039d788c64fd7288e85e7435e966b0e2abe51 (patch)
tree3263cec8c3e12f9697c16178d1f99cc66c93a376
parente2df212b9f03835fe3fbe990ec45c00be43b40a3 (diff)
inverterd: add --delay option
-rw-r--r--src/common.h1
-rw-r--r--src/inverterd.cc13
-rw-r--r--src/server/server.cc19
-rw-r--r--src/server/server.h3
4 files changed, 33 insertions, 3 deletions
diff --git a/src/common.h b/src/common.h
index 6786b5e..7b9740e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -18,6 +18,7 @@ enum {
LO_RAW,
LO_TIMEOUT,
LO_CACHE_TIMEOUT,
+ LO_DELAY,
LO_FORMAT,
LO_DEVICE,
LO_USB_VENDOR_ID,
diff --git a/src/inverterd.cc b/src/inverterd.cc
index 4e7e750..057431c 100644
--- a/src/inverterd.cc
+++ b/src/inverterd.cc
@@ -7,6 +7,7 @@
#include <ios>
#include <getopt.h>
+#include "numeric_types.h"
#include "common.h"
#include "voltronic/device.h"
#include "voltronic/exceptions.h"
@@ -29,6 +30,7 @@ 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"
" Cache validity time, in ms (default: " << server::Server::CACHE_TIMEOUT << ")\n"
" --verbose: Be verbose\n"
"\n";
@@ -53,8 +55,9 @@ static void usage(const char* progname) {
int main(int argc, char *argv[]) {
// common params
- uint64_t timeout = voltronic::Device::TIMEOUT;
- uint64_t cacheTimeout = server::Server::CACHE_TIMEOUT;
+ u64 timeout = voltronic::Device::TIMEOUT;
+ u64 cacheTimeout = server::Server::CACHE_TIMEOUT;
+ u64 delay = 0;
bool verbose = false;
// server params
@@ -80,6 +83,7 @@ int main(int argc, char *argv[]) {
{"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},
@@ -139,6 +143,10 @@ int main(int argc, char *argv[]) {
cacheTimeout = std::stoull(arg);
break;
+ case LO_DELAY:
+ delay = std::stoull(arg);
+ break;
+
case LO_USB_VENDOR_ID:
try {
if (arg.size() != 4)
@@ -266,6 +274,7 @@ int main(int argc, char *argv[]) {
server::Server server(dev);
server.setVerbose(verbose);
+ server.setDelay(delay);
server.setCacheTimeout(cacheTimeout);
server.start(host, port);
diff --git a/src/server/server.cc b/src/server/server.cc
index 2c2ff33..8361b19 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -25,6 +25,8 @@ Server::Server(std::shared_ptr<voltronic::Device> device)
: sock_(0)
, port_(0)
, cacheTimeout_(CACHE_TIMEOUT)
+ , delay_(0)
+ , endExecutionTime_(0)
, verbose_(false)
, device_(std::move(device)) {
client_.setDevice(device_);
@@ -39,6 +41,10 @@ void Server::setCacheTimeout(u64 timeout) {
cacheTimeout_ = timeout;
}
+void Server::setDelay(u64 delay) {
+ delay_ = delay;
+}
+
Server::~Server() {
if (sock_ > 0)
close(sock_);
@@ -121,14 +127,25 @@ std::shared_ptr<p18::response_type::BaseResponse> Server::executeCommand(p18::Co
cache_.erase(it);
}
+ if (delay_ != 0 && endExecutionTime_ != 0) {
+ u64 now = voltronic::timestamp();
+ u64 diff = now - endExecutionTime_;
+
+ if (diff < delay_)
+ usleep(delay_ - diff);
+ }
+
try {
auto response = client_.execute(commandType, arguments);
+ endExecutionTime_ = voltronic::timestamp();
+
CachedResponse cr {
- .time = voltronic::timestamp(),
+ .time = endExecutionTime_,
.arguments = arguments,
.response = response
};
cache_[commandType] = cr;
+
return response;
}
catch (voltronic::DeviceError& e) {
diff --git a/src/server/server.h b/src/server/server.h
index e083c7e..dc24ee8 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -42,6 +42,8 @@ private:
std::shared_ptr<voltronic::Device> device_;
u64 cacheTimeout_;
+ u64 delay_;
+ u64 endExecutionTime_;
std::map<p18::CommandType, CachedResponse> cache_;
std::mutex threads_mutex_;
@@ -59,6 +61,7 @@ public:
void setVerbose(bool verbose);
void setCacheTimeout(u64 timeout);
+ void setDelay(u64 delay);
void start(std::string& host, int port);
bool verbose() const { return verbose_; }