Adding the ability to remove a device from the history and the data table.

This commit is contained in:
stephb9959
2021-11-21 23:36:27 -08:00
parent 95d8ef42ae
commit 7e0da73c54
11 changed files with 172 additions and 15 deletions

View File

@@ -87,7 +87,7 @@ add_executable( owfms
src/FirmwareCache.cpp src/FirmwareCache.h
src/SDK/Prov_SDK.cpp src/SDK/Prov_SDK.h
src/AutoUpdater.cpp src/AutoUpdater.h src/SDK/GW_SDK.cpp src/SDK/GW_SDK.h
)
src/NewCommandHandler.cpp src/NewCommandHandler.h)
target_link_libraries(owfms PUBLIC
${Poco_LIBRARIES} ${MySQL_LIBRARIES}

2
build
View File

@@ -1 +1 @@
6
9

View File

@@ -17,6 +17,7 @@
#include "DeviceCache.h"
#include "FirmwareCache.h"
#include "AutoUpdater.h"
#include "NewCommandHandler.h"
namespace OpenWifi {
class Daemon *Daemon::instance_ = nullptr;
@@ -35,7 +36,8 @@ namespace OpenWifi {
DeviceCache(),
NewConnectionHandler(),
ManifestCreator(),
AutoUpdater()
AutoUpdater(),
NewCommandHandler()
});
}
return instance_;

86
src/NewCommandHandler.cpp Normal file
View File

@@ -0,0 +1,86 @@
//
// Created by stephane bourque on 2021-11-21.
//
#include "NewCommandHandler.h"
#include "StorageService.h"
namespace OpenWifi {
void NewCommandHandler::run() {
Running_ = true ;
while(Running_) {
Poco::Thread::trySleep(2000);
if(!Running_)
break;
while(!NewCommands_.empty()) {
if(!Running_)
break;
Types::StringPair S;
{
std::lock_guard G(Mutex_);
S = NewCommands_.front();
NewCommands_.pop();
}
try {
auto SerialNumber = S.first;
auto M = nlohmann::json::parse(S.second);
std::string EndPoint;
if(M.contains(uCentralProtocol::SYSTEM)) {
auto SystemObj = M[uCentralProtocol::SYSTEM];
if(SystemObj.contains(uCentralProtocol::HOST))
EndPoint = SystemObj[uCentralProtocol::HOST];
}
if(M.contains(uCentralProtocol::PAYLOAD)) {
auto PayloadSection = M[uCentralProtocol::PAYLOAD];
if(PayloadSection.contains("command")) {
auto Command = PayloadSection["command"];
if(Command=="delete_device") {
auto pSerialNumber = PayloadSection["payload"]["serialNumber"];
if(pSerialNumber==SerialNumber) {
Logger_.debug(Poco::format("Removing device '%s' from upgrade history.",SerialNumber));
StorageService()->DeleteHistory(SerialNumber);
Logger_.debug(Poco::format("Removing device '%s' from device table.",SerialNumber));
StorageService()->DeleteDevice(SerialNumber);
}
}
}
}
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
}
}
};
int NewCommandHandler::Start() {
Types::TopicNotifyFunction F = [this](std::string s1,std::string s2) { this->CommandReceived(s1,s2); };
WatcherId_ = KafkaManager()->RegisterTopicWatcher(KafkaTopics::COMMAND, F);
Worker_.start(*this);
return 0;
};
void NewCommandHandler::Stop() {
KafkaManager()->UnregisterTopicWatcher(KafkaTopics::COMMAND, WatcherId_);
Running_ = false;
Worker_.wakeUp();
Worker_.join();
};
bool NewCommandHandler::Update() {
Worker_.wakeUp();
return true;
}
void NewCommandHandler::CommandReceived( const std::string & Key, const std::string & Message) {
std::lock_guard G(Mutex_);
NewCommands_.push(std::make_pair(Key,Message));
}
}

41
src/NewCommandHandler.h Normal file
View File

@@ -0,0 +1,41 @@
//
// Created by stephane bourque on 2021-11-21.
//
#ifndef OWFMS_NEWCOMMANDHANDLER_H
#define OWFMS_NEWCOMMANDHANDLER_H
#include "framework/MicroService.h"
#include "framework/OpenWifiTypes.h"
namespace OpenWifi {
class NewCommandHandler : public SubSystemServer, Poco::Runnable {
public:
static NewCommandHandler *instance() {
static NewCommandHandler *instance_ = new NewCommandHandler;
return instance_;
}
void run() override;
int Start() override;
void Stop() override;
bool Update();
void CommandReceived( const std::string & Key, const std::string & Message);
private:
Poco::Thread Worker_;
std::atomic_bool Running_ = false;
int WatcherId_=0;
Types::StringPairQueue NewCommands_;
NewCommandHandler() noexcept:
SubSystemServer("NewCommandHandler", "NEWCOM-MGR", "commanmdhandler") {
}
};
inline NewCommandHandler * NewCommandHandler() { return NewCommandHandler::instance(); };
}
#endif //OWFMS_NEWCOMMANDHANDLER_H

View File

@@ -12,6 +12,7 @@
#include "Daemon.h"
#ifdef TIP_GATEWAY_SERVICE
#include "DeviceRegistry.h"
#include "CapabilitiesCache.h"
#endif
#include "RESTAPI_GWobjects.h"
@@ -26,7 +27,7 @@ namespace OpenWifi::GWObjects {
void Device::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"serialNumber", SerialNumber);
#ifdef TIP_GATEWAY_SERVICE
field_to_json(Obj,"deviceType", Daemon::instance()->IdentifyDevice(Compatible));
field_to_json(Obj,"deviceType", CapabilitiesCache::instance()->Get(Compatible));
#endif
field_to_json(Obj,"macAddress", MACAddress);
field_to_json(Obj,"manufacturer", Manufacturer);

View File

@@ -6,8 +6,7 @@
// Arilia Wireless Inc.
//
#ifndef UCENTRAL_RESTAPI_OBJECTS_H
#define UCENTRAL_RESTAPI_OBJECTS_H
#pragma once
#include "Poco/JSON/Object.h"
#include "RESTAPI_SecurityObjects.h"
@@ -111,7 +110,7 @@ namespace OpenWifi::GWObjects {
struct DefaultConfiguration {
std::string Name;
std::string Configuration;
std::string Models;
Types::StringVec Models;
std::string Description;
uint64_t Created;
uint64_t LastModified;
@@ -191,5 +190,3 @@ namespace OpenWifi::GWObjects {
void to_json(Poco::JSON::Object &Obj) const;
};
}
#endif //UCENTRAL_RESTAPI_OBJECTS_H

View File

@@ -6,9 +6,7 @@
// Arilia Wireless Inc.
//
#ifndef OWPROV_RESTAPI_PROVOBJECTS_H
#define OWPROV_RESTAPI_PROVOBJECTS_H
#pragma once
#include <string>
#include "RESTAPI_SecurityObjects.h"
@@ -380,6 +378,3 @@ namespace OpenWifi::ProvObjects {
bool UpdateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I);
bool CreateObjectInfo(const Poco::JSON::Object::Ptr &O, const SecurityObjects::UserInfo &U, ObjectInfo &I);
};
#endif //OWPROV_RESTAPI_PROVOBJECTS_H

View File

@@ -51,10 +51,12 @@ namespace OpenWifi {
bool SetDeviceRevision(std::string &SerialNumber, std::string & Revision, std::string & DeviceType, std::string &EndPoint);
bool AddHistory( std::string & SerialNumber, std::string &DeviceType, std::string & PreviousRevision, std::string & NewVersion);
bool DeleteHistory( std::string & SerialNumber, std::string &Id);
bool DeleteHistory( std::string & SerialNumber);
bool GetDevices(uint64_t From, uint64_t HowMany, std::vector<FMSObjects::DeviceConnectionInformation> & Devices);
bool GetDevice(std::string &SerialNumber, FMSObjects::DeviceConnectionInformation & Device);
bool SetDeviceDisconnected(std::string &SerialNumber, std::string &EndPoint);
bool DeleteDevice( std::string & SerialNumber);
bool GenerateDeviceReport(FMSObjects::DeviceReport &Report);
static std::string TrimRevision(const std::string &R);

View File

@@ -107,6 +107,22 @@ namespace OpenWifi {
}
bool Storage::DeleteDevice( std::string & SerialNumber) {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Delete(Sess);
std::string st{"DELETE FROM " + DBNAME_DEVICES + " where serialNumber=?"};
Delete << ConvertParams(st) ,
Poco::Data::Keywords::use(SerialNumber);
Delete.execute();
return true;
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
bool Storage::SetDeviceDisconnected(std::string &SerialNumber, std::string &EndPoint) {
try {
Poco::Data::Session Sess = Pool_->get();

View File

@@ -126,5 +126,22 @@ namespace OpenWifi {
return false;
}
bool Storage::DeleteHistory( std::string & SerialNumber) {
try {
Poco::Data::Session Sess = Pool_->get();
Poco::Data::Statement Delete(Sess);
std::string st{"DELETE FROM " + DBNAME_HISTORY + " where serialnumber=?"};
Delete << ConvertParams(st),
Poco::Data::Keywords::use(SerialNumber);
Delete.execute();
return true;
} catch(const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
}