diff --git a/src/AP_WS_Process_connect.cpp b/src/AP_WS_Process_connect.cpp index 7e7a61bd..85f44896 100644 --- a/src/AP_WS_Process_connect.cpp +++ b/src/AP_WS_Process_connect.cpp @@ -83,6 +83,8 @@ namespace OpenWifi { State_.Address = Utils::FormatIPv6(WS_->peerAddress().toString()); CId_ = SerialNumber_ + "@" + CId_; + auto &Platform = Caps.Platform(); + if(ParamsObj->has("reason")) { State_.connectReason = ParamsObj->get("reason").toString(); } @@ -204,8 +206,13 @@ namespace OpenWifi { ++Updated; } - if (Compatible_ != DeviceInfo.DeviceType) { - DeviceInfo.DeviceType = Compatible_; + if (Compatible_ != DeviceInfo.Compatible) { + DeviceInfo.Compatible = Compatible_; + ++Updated; + } + + if (Platform != DeviceInfo.DeviceType) { + DeviceInfo.DeviceType = Platform; ++Updated; } diff --git a/src/RESTAPI/RESTAPI_devices_handler.cpp b/src/RESTAPI/RESTAPI_devices_handler.cpp index beef733e..dc4c0643 100644 --- a/src/RESTAPI/RESTAPI_devices_handler.cpp +++ b/src/RESTAPI/RESTAPI_devices_handler.cpp @@ -82,10 +82,15 @@ namespace OpenWifi { } } + auto platform = Poco::toLower(GetParameter("platform", "")); auto serialOnly = GetBoolParameter(RESTAPI::Protocol::SERIALONLY, false); auto deviceWithStatus = GetBoolParameter(RESTAPI::Protocol::DEVICEWITHSTATUS, false); auto completeInfo = GetBoolParameter("completeInfo", false); + if(!platform.empty() && (platform!="ap" && platform!="switch")) { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } + Poco::JSON::Object RetObj; if (!QB_.Select.empty()) { Poco::JSON::Array Objects; @@ -116,9 +121,9 @@ namespace OpenWifi { else RetObj.set(RESTAPI::Protocol::DEVICES, Objects); - } else if (QB_.CountOnly == true) { + } else if (QB_.CountOnly) { uint64_t Count = 0; - if (StorageService()->GetDeviceCount(Count)) { + if (StorageService()->GetDeviceCount(Count, platform)) { return ReturnCountOnly(Count); } } else if (serialOnly) { diff --git a/src/StorageService.h b/src/StorageService.h index 7d2ef093..726790f2 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -147,7 +147,8 @@ namespace OpenWifi { bool GetDevice(Poco::Data::Session &Session, const std::string &SerialNumber, GWObjects::Device &DeviceDetails); bool GetDevice(const std::string &SerialNumber, GWObjects::Device &); bool GetDevices(uint64_t From, uint64_t HowMany, std::vector &Devices, - const std::string &orderBy = ""); + const std::string &orderBy = "", + const std::string &platform = ""); // bool GetDevices(uint64_t From, uint64_t HowMany, const std::string & Select, // std::vector &Devices, const std::string & orderBy=""); bool DeleteDevice(std::string &SerialNumber); @@ -159,10 +160,11 @@ namespace OpenWifi { bool UpdateDevice(Poco::Data::Session &Sess, GWObjects::Device &NewDeviceDetails); bool DeviceExists(std::string &SerialNumber); bool SetConnectInfo(std::string &SerialNumber, std::string &Firmware); - bool GetDeviceCount(uint64_t &Count); + bool GetDeviceCount(uint64_t &Count, const std::string &platform = ""); bool GetDeviceSerialNumbers(uint64_t From, uint64_t HowMany, std::vector &SerialNumbers, - const std::string &orderBy = ""); + const std::string &orderBy = "", + const std::string &platform = ""); bool GetDeviceFWUpdatePolicy(std::string &SerialNumber, std::string &Policy); bool SetDevicePassword(LockedDbSession &Session, std::string &SerialNumber, std::string &Password); bool UpdateSerialNumberCache(); diff --git a/src/storage/storage_device.cpp b/src/storage/storage_device.cpp index 4f869475..b2f746f7 100644 --- a/src/storage/storage_device.cpp +++ b/src/storage/storage_device.cpp @@ -172,14 +172,18 @@ namespace OpenWifi { R.set<30>(D.connectReason); } - bool Storage::GetDeviceCount(uint64_t &Count) { + bool Storage::GetDeviceCount(uint64_t &Count, const std::string &platform) { try { Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); - std::string st{"SELECT COUNT(*) FROM Devices"}; - - Select << st, Poco::Data::Keywords::into(Count); + if(!platform.empty()) { + std::string st{"SELECT COUNT(*) FROM Devices WHERE DeviceType='" + platform + "'"}; + Select << st, Poco::Data::Keywords::into(Count); + } else { + std::string st{"SELECT COUNT(*) FROM Devices"}; + Select << st, Poco::Data::Keywords::into(Count); + } Select.execute(); return true; } catch (const Poco::Exception &E) { @@ -190,16 +194,22 @@ namespace OpenWifi { bool Storage::GetDeviceSerialNumbers(uint64_t From, uint64_t HowMany, std::vector &SerialNumbers, - const std::string &orderBy) { + const std::string &orderBy, + const std::string &platform) { try { Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); std::string st; + if(!platform.empty()) { + st = "SELECT SerialNumber From Devices WHERE DeviceType='" + platform + "' "; + } else { + st = "SELECT SerialNumber From Devices "; + } if (orderBy.empty()) - st = "SELECT SerialNumber From Devices ORDER BY SerialNumber ASC "; + st += " ORDER BY SerialNumber ASC "; else - st = "SELECT SerialNumber From Devices " + orderBy; + st += orderBy; Select << st + ComputeRange(From, HowMany), Poco::Data::Keywords::into(SerialNumbers); Select.execute(); @@ -536,7 +546,7 @@ namespace OpenWifi { } catch (const Poco::Exception &E) { Logger().log(E); } - return true; + return false; } bool Storage::CreateDefaultDevice(Poco::Data::Session &Session, std::string &SerialNumber, const Config::Capabilities &Caps, @@ -582,7 +592,7 @@ namespace OpenWifi { D.locale = InsertRadiosCountyRegulation(D.Configuration, IPAddress); D.SerialNumber = Poco::toLower(SerialNumber); D.Compatible = Caps.Compatible(); - D.DeviceType = Daemon()->IdentifyDevice(D.Compatible); + D.DeviceType = Caps.Platform(); D.MACAddress = Utils::SerialToMAC(SerialNumber); D.Manufacturer = Caps.Model(); D.Firmware = Firmware; @@ -825,17 +835,24 @@ namespace OpenWifi { } bool Storage::GetDevices(uint64_t From, uint64_t HowMany, - std::vector &Devices, const std::string &orderBy) { + std::vector &Devices, const std::string &orderBy, const std::string &platform) { DeviceRecordList Records; try { Poco::Data::Session Sess = Pool_->get(); Poco::Data::Statement Select(Sess); - // std::string st{"SELECT " + DB_DeviceSelectFields + " FROM Devices " + orderBy.empty() - // ? " ORDER BY SerialNumber ASC " + ComputeRange(From, HowMany)}; - std::string st = fmt::format("SELECT {} FROM Devices {} {}", DB_DeviceSelectFields, - orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy, - ComputeRange(From, HowMany)); + std::string st; + if(platform.empty()) { + st = + fmt::format("SELECT {} FROM Devices {} {}", DB_DeviceSelectFields, + orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy, + ComputeRange(From, HowMany)); + } else { + st = + fmt::format("SELECT {} FROM Devices WHERE DeviceType='{}' {} {}", DB_DeviceSelectFields, platform, + orderBy.empty() ? " ORDER BY SerialNumber ASC " : orderBy, + ComputeRange(From, HowMany)); + } Select << ConvertParams(st), Poco::Data::Keywords::into(Records); Select.execute();