inverterd: add --delay option
This commit is contained in:
parent
e2df212b9f
commit
a06039d788
@ -18,6 +18,7 @@ enum {
|
||||
LO_RAW,
|
||||
LO_TIMEOUT,
|
||||
LO_CACHE_TIMEOUT,
|
||||
LO_DELAY,
|
||||
LO_FORMAT,
|
||||
LO_DEVICE,
|
||||
LO_USB_VENDOR_ID,
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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_; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user