From a06039d788c64fd7288e85e7435e966b0e2abe51 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 22 May 2021 15:25:18 +0300 Subject: inverterd: add --delay option --- src/server/server.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/server/server.cc') 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 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 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) { -- cgit v1.2.3