Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 75 additions & 54 deletions clients/nutclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void Socket::connect(const std::string& host, int port)
{
case EAFNOSUPPORT:
case EINVAL:
break;
break;
default:
throw nut::SystemException();
}
Expand Down Expand Up @@ -437,7 +437,7 @@ std::set<Device> Client::getDevices()
std::set<std::string> devs = getDeviceNames();
for(std::set<std::string>::iterator it=devs.begin(); it!=devs.end(); ++it)
{
res.insert(Device(this, *it));
res.insert(Device(this, *it));
}

return res;
Expand All @@ -451,16 +451,16 @@ bool Client::hasDeviceVariable(const std::string& dev, const std::string& name)

std::map<std::string,std::vector<std::string> > Client::getDeviceVariableValues(const std::string& dev)
{
std::map<std::string,std::vector<std::string> > res;
std::map<std::string,std::vector<std::string> > res;

std::set<std::string> names = getDeviceVariableNames(dev);
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
const std::string& name = *it;
res[name] = getDeviceVariableValue(dev, name);
}
std::set<std::string> names = getDeviceVariableNames(dev);
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
const std::string& name = *it;
res[name] = getDeviceVariableValue(dev, name);
}

return res;
return res;
}

std::map<std::string,std::map<std::string,std::vector<std::string> > > Client::getDevicesVariableValues(const std::set<std::string>& devs)
Expand All @@ -477,8 +477,8 @@ std::map<std::string,std::map<std::string,std::vector<std::string> > > Client::g

bool Client::hasDeviceCommand(const std::string& dev, const std::string& name)
{
std::set<std::string> names = getDeviceCommandNames(dev);
return names.find(name) != names.end();
std::set<std::string> names = getDeviceCommandNames(dev);
return names.find(name) != names.end();
}

bool Client::hasFeature(const Feature& feature)
Expand Down Expand Up @@ -612,7 +612,7 @@ std::set<std::string> TcpClient::getDeviceNames()

std::string TcpClient::getDeviceDescription(const std::string& name)
{
return get("UPSDESC", name)[0];
return get("UPSDESC", name)[0];
}

std::set<std::string> TcpClient::getDeviceVariableNames(const std::string& dev)
Expand Down Expand Up @@ -1084,6 +1084,17 @@ _name(dev._name)
{
}

Device& Device::operator=(const Device& dev)
{
// Self assignment?
if (this==&dev)
return *this;

_client = dev._client;
_name = dev._name;
return *this;
}

Device::~Device()
{
}
Expand Down Expand Up @@ -1125,7 +1136,7 @@ bool Device::operator==(const Device& dev)const

bool Device::operator<(const Device& dev)const
{
return getName()<dev.getName();
return getName()<dev.getName();
}

std::string Device::getDescription()
Expand Down Expand Up @@ -1174,23 +1185,23 @@ void Device::setVariable(const std::string& name, const std::vector<std::string>

Variable Device::getVariable(const std::string& name)
{
if (!isOk()) throw NutException("Invalid device");
if(getClient()->hasDeviceVariable(getName(), name))
return Variable(this, name);
else
return Variable(NULL, "");
if (!isOk()) throw NutException("Invalid device");
if(getClient()->hasDeviceVariable(getName(), name))
return Variable(this, name);
else
return Variable(NULL, "");
}

std::set<Variable> Device::getVariables()
{
std::set<Variable> set;
if (!isOk()) throw NutException("Invalid device");

std::set<std::string> names = getClient()->getDeviceVariableNames(getName());
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
std::set<std::string> names = getClient()->getDeviceVariableNames(getName());
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
set.insert(Variable(this, *it));
}
}

return set;
}
Expand All @@ -1200,11 +1211,11 @@ std::set<Variable> Device::getRWVariables()
std::set<Variable> set;
if (!isOk()) throw NutException("Invalid device");

std::set<std::string> names = getClient()->getDeviceRWVariableNames(getName());
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
std::set<std::string> names = getClient()->getDeviceRWVariableNames(getName());
for(std::set<std::string>::iterator it=names.begin(); it!=names.end(); ++it)
{
set.insert(Variable(this, *it));
}
}

return set;
}
Expand All @@ -1230,29 +1241,29 @@ std::set<Command> Device::getCommands()

Command Device::getCommand(const std::string& name)
{
if (!isOk()) throw NutException("Invalid device");
if(getClient()->hasDeviceCommand(getName(), name))
return Command(this, name);
else
return Command(NULL, "");
if (!isOk()) throw NutException("Invalid device");
if(getClient()->hasDeviceCommand(getName(), name))
return Command(this, name);
else
return Command(NULL, "");
}

TrackingID Device::executeCommand(const std::string& name, const std::string& param)
{
if (!isOk()) throw NutException("Invalid device");
return getClient()->executeDeviceCommand(getName(), name, param);
if (!isOk()) throw NutException("Invalid device");
return getClient()->executeDeviceCommand(getName(), name, param);
}

void Device::login()
{
if (!isOk()) throw NutException("Invalid device");
getClient()->deviceLogin(getName());
if (!isOk()) throw NutException("Invalid device");
getClient()->deviceLogin(getName());
}

void Device::master()
{
if (!isOk()) throw NutException("Invalid device");
getClient()->deviceMaster(getName());
if (!isOk()) throw NutException("Invalid device");
getClient()->deviceMaster(getName());
}

void Device::forcedShutdown()
Expand All @@ -1261,8 +1272,8 @@ void Device::forcedShutdown()

int Device::getNumLogins()
{
if (!isOk()) throw NutException("Invalid device");
return getClient()->deviceGetNumLogins(getName());
if (!isOk()) throw NutException("Invalid device");
return getClient()->deviceGetNumLogins(getName());
}

/*
Expand All @@ -1283,6 +1294,17 @@ _name(var._name)
{
}

Variable& Variable::operator=(const Variable& var)
{
// Self assignment?
if (this==&var)
return *this;

_device = var._device;
_name = var._name;
return *this;
}

Variable::~Variable()
{
}
Expand Down Expand Up @@ -1330,12 +1352,12 @@ bool Variable::operator<(const Variable& var)const

std::vector<std::string> Variable::getValue()
{
return getDevice()->getClient()->getDeviceVariableValue(getDevice()->getName(), getName());
return getDevice()->getClient()->getDeviceVariableValue(getDevice()->getName(), getName());
}

std::string Variable::getDescription()
{
return getDevice()->getClient()->getDeviceVariableDescription(getDevice()->getName(), getName());
return getDevice()->getClient()->getDeviceVariableDescription(getDevice()->getName(), getName());
}

void Variable::setValue(const std::string& value)
Expand Down Expand Up @@ -1367,6 +1389,17 @@ _name(cmd._name)
{
}

Command& Command::operator=(const Command& cmd)
{
// Self assignment?
if (this==&cmd)
return *this;

_device = cmd._device;
_name = cmd._name;
return *this;
}

Command::~Command()
{
}
Expand All @@ -1389,7 +1422,6 @@ Device* Command::getDevice()
bool Command::isOk()const
{
return _device!=NULL && !_name.empty();

}

Command::operator bool()const
Expand Down Expand Up @@ -1449,7 +1481,6 @@ void strarr_free(strarr arr)
free(arr);
}


strarr stringset_to_strarr(const std::set<std::string>& strset)
{
strarr arr = strarr_alloc(strset.size());
Expand All @@ -1474,7 +1505,6 @@ strarr stringvector_to_strarr(const std::vector<std::string>& strset)
return arr;
}


NUTCLIENT_TCP_t nutclient_tcp_create_client(const char* host, unsigned short port)
{
nut::TcpClient* client = new nut::TcpClient;
Expand Down Expand Up @@ -1525,7 +1555,6 @@ void nutclient_tcp_disconnect(NUTCLIENT_TCP_t client)
}
}


int nutclient_tcp_reconnect(NUTCLIENT_TCP_t client)
{
if(client)
Expand Down Expand Up @@ -1569,7 +1598,6 @@ long nutclient_tcp_get_timeout(NUTCLIENT_TCP_t client)
return -1;
}


void nutclient_authenticate(NUTCLIENT_t client, const char* login, const char* passwd)
{
if(client)
Expand Down Expand Up @@ -1635,7 +1663,6 @@ int nutclient_get_device_num_logins(NUTCLIENT_t client, const char* dev)
return -1;
}


void nutclient_device_master(NUTCLIENT_t client, const char* dev)
{
if(client)
Expand Down Expand Up @@ -1844,8 +1871,6 @@ void nutclient_set_device_variable_values(NUTCLIENT_t client, const char* dev, c
}
}



strarr nutclient_get_device_commands(NUTCLIENT_t client, const char* dev)
{
if(client)
Expand All @@ -1863,7 +1888,6 @@ strarr nutclient_get_device_commands(NUTCLIENT_t client, const char* dev)
return NULL;
}


int nutclient_has_device_command(NUTCLIENT_t client, const char* dev, const char* cmd)
{
if(client)
Expand All @@ -1881,7 +1905,6 @@ int nutclient_has_device_command(NUTCLIENT_t client, const char* dev, const char
return 0;
}


char* nutclient_get_device_command_description(NUTCLIENT_t client, const char* dev, const char* cmd)
{
if(client)
Expand Down Expand Up @@ -1916,5 +1939,3 @@ void nutclient_execute_device_command(NUTCLIENT_t client, const char* dev, const
}

} /* extern "C" */


Loading