Skip to content

Commit

Permalink
adding nonProvisioned parameter to GET devices API call
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavesh Patel <[email protected]>
  • Loading branch information
bhaveshpatel88 authored and Ivan Chvets committed Mar 21, 2024
1 parent 5bee5b1 commit fcdb742
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/RESTAPI/RESTAPI_devices_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace OpenWifi {
auto serialOnly = GetBoolParameter(RESTAPI::Protocol::SERIALONLY, false);
auto deviceWithStatus = GetBoolParameter(RESTAPI::Protocol::DEVICEWITHSTATUS, false);
auto completeInfo = GetBoolParameter("completeInfo", false);
auto nonProvisioned = HasParameter("nonProvisioned", Arg);

if(!platform.empty() && (platform!="ap" && platform!="switch" && platform!="all")) {
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
Expand Down Expand Up @@ -131,7 +132,7 @@ namespace OpenWifi {
}
} else if (serialOnly) {
std::vector<std::string> SerialNumbers;
StorageService()->GetDeviceSerialNumbers(QB_.Offset, QB_.Limit, SerialNumbers, OrderBy, platform);
StorageService()->GetDeviceSerialNumbers(QB_.Offset, QB_.Limit, SerialNumbers, OrderBy, platform, nonProvisioned);
Poco::JSON::Array Objects;
for (const auto &i : SerialNumbers) {
Objects.add(i);
Expand All @@ -149,7 +150,7 @@ namespace OpenWifi {
RetObj.set("serialNumbers", Objects);
} else {
std::vector<GWObjects::Device> Devices;
StorageService()->GetDevices(QB_.Offset, QB_.Limit, Devices, OrderBy, platform);
StorageService()->GetDevices(QB_.Offset, QB_.Limit, Devices, OrderBy, platform, nonProvisioned);
Poco::JSON::Array Objects;
for (const auto &i : Devices) {
Poco::JSON::Object Obj;
Expand Down
6 changes: 4 additions & 2 deletions src/StorageService.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ namespace OpenWifi {
bool GetDevice(const std::string &SerialNumber, GWObjects::Device &);
bool GetDevices(uint64_t From, uint64_t HowMany, std::vector<GWObjects::Device> &Devices,
const std::string &orderBy = "",
const std::string &platform = "");
const std::string &platform = "",
bool nonProvisioned = false);
// bool GetDevices(uint64_t From, uint64_t HowMany, const std::string & Select,
// std::vector<GWObjects::Device> &Devices, const std::string & orderBy="");
bool DeleteDevice(std::string &SerialNumber);
Expand All @@ -164,7 +165,8 @@ namespace OpenWifi {
bool GetDeviceSerialNumbers(uint64_t From, uint64_t HowMany,
std::vector<std::string> &SerialNumbers,
const std::string &orderBy = "",
const std::string &platform = "");
const std::string &platform = "",
bool nonProvisioned = false);
bool GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy);
bool SetDevicePassword(LockedDbSession &Session, std::string &SerialNumber, std::string &Password);
bool UpdateSerialNumberCache();
Expand Down
52 changes: 40 additions & 12 deletions src/storage/storage_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,32 @@ namespace OpenWifi {
bool Storage::GetDeviceSerialNumbers(uint64_t From, uint64_t HowMany,
std::vector<std::string> &SerialNumbers,
const std::string &orderBy,
const std::string &platform) {
const std::string &platform, bool nonProvisioned) {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);

std::string st;
std::string whereClause = "";
if(!platform.empty()) {
st = "SELECT SerialNumber From Devices WHERE DeviceType='" + platform + "' ";
if (nonProvisioned) {

whereClause = fmt::format("WHERE entity='' and venue='' and DeviceType='" + platform + "'");
} else {
whereClause = fmt::format("WHERE DeviceType='" + platform + "'");
}


//st = "SELECT SerialNumber From Devices WHERE DeviceType='" + platform + "' ";
} else {
st = "SELECT SerialNumber From Devices ";
if (nonProvisioned) {
whereClause = fmt::format("WHERE entity='' and venue=''");
}
//st = "SELECT SerialNumber From Devices ";
}

st = fmt::format("SELECT SerialNumber From Devices {}", whereClause);

if (orderBy.empty())
st += " ORDER BY SerialNumber ASC ";
else
Expand Down Expand Up @@ -835,25 +850,38 @@ namespace OpenWifi {
}

bool Storage::GetDevices(uint64_t From, uint64_t HowMany,
std::vector<GWObjects::Device> &Devices, const std::string &orderBy, const std::string &platform) {
std::vector<GWObjects::Device> &Devices, const std::string &orderBy, const std::string &platform,
bool nonProvisioned) {
DeviceRecordList Records;
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Select(Sess);

std::string st;
std::string whereClause = "";
if(platform.empty()) {
st =
fmt::format("SELECT {} FROM Devices {} {}", DB_DeviceSelectFields,
orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy,
ComputeRange(From, HowMany));

if (nonProvisioned) {
whereClause = fmt::format("WHERE entity='' and venue=''");
}

} else {
st =
fmt::format("SELECT {} FROM Devices WHERE DeviceType='{}' {} {}", DB_DeviceSelectFields, platform,
orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy,
ComputeRange(From, HowMany));

if (nonProvisioned) {
whereClause = fmt::format("WHERE DeviceType='{}' and entity='' and venue=''",platform);
} else {
whereClause = fmt::format("WHERE DeviceType='{}'", platform);
}

}

st =
fmt::format("SELECT {} FROM Devices {} {} {}", DB_DeviceSelectFields, whereClause,
orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy,
ComputeRange(From, HowMany));

//Logger().information(fmt::format(" GetDevices st is {} ", st));

Select << ConvertParams(st), Poco::Data::Keywords::into(Records);
Select.execute();

Expand Down

0 comments on commit fcdb742

Please sign in to comment.