diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-05-22 15:25:18 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-05-22 15:25:18 +0300 |
commit | a06039d788c64fd7288e85e7435e966b0e2abe51 (patch) | |
tree | 3263cec8c3e12f9697c16178d1f99cc66c93a376 /src/server | |
parent | e2df212b9f03835fe3fbe990ec45c00be43b40a3 (diff) |
inverterd: add --delay option
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/server.cc | 19 | ||||
-rw-r--r-- | src/server/server.h | 3 |
2 files changed, 21 insertions, 1 deletions
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_; } |