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/server.cc | |
parent | e2df212b9f03835fe3fbe990ec45c00be43b40a3 (diff) |
inverterd: add --delay option
Diffstat (limited to 'src/server/server.cc')
-rw-r--r-- | src/server/server.cc | 19 |
1 files changed, 18 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) { |