virtual ~IPresenter()
{}
- //TODO: define interface.
-
virtual ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) = 0;
virtual ErrorCode LogOut() = 0;
virtual ErrorCode OwnDevice(const std::string& deviceId) = 0;
+ virtual ErrorCode GetDevicePolicy(const std::string& deviceId, std::string& policy) = 0;
+
+ virtual ErrorCode SetDevicePolicy(const std::string& deviceId, const std::string& policy) = 0;
+
+ virtual ErrorCode GetDeviceReport(const std::string& devideId, std::string& report) = 0;
+
private:
};
virtual DeviceList GetUnownedDeviceList();
+ virtual ErrorCode UnownDevice(const std::string& deviceId);
+
+ virtual ErrorCode OwnDevice(const std::string& deviceId);
+
using ModelObserversList = std::vector<IModelObserverWPtr>;
virtual void AddObserver(IModelObserverWPtr observer);
#include "common_types.h"
#include "device.h"
+#include <functional>
#include <memory>
namespace iotswsec
{
public:
+ using NotificationCallback = std::function<void(const std::string& title, const std::string& body)>;
+
virtual ~INetworkManager() = default;
//TODO: define interface.
virtual ErrorCode UnownDevice(const std::string& deviceId) = 0;
virtual ErrorCode OwnDevice(const std::string& deviceId) = 0;
+
+// virtual ErrorCode AddNotificationCallback(const NotificationCallback&) = 0;
};
using INetworkManagerPtr = std::shared_ptr<INetworkManager>;
typedef void (*DeviceStateChangedCallback)(const char* devId, DeviceState state, void* userDefined);
typedef void (*DeviceEnumCallback)(NM_hDeviceList, const char* devId, void* userDefined);
+ typedef void (*NotificationCallback)(NM_NotificationData,void*);
typedef NM_ErrorCode (*InitFunc)(NM_hContext*);
typedef void (*CleanUpFunc)(NM_hContext*);
typedef void (*FreeDeviceInfoFunc)(NM_DeviceInfo*);
typedef NM_ErrorCode (*OwnDeviceFunc)(NM_hDeviceList, const char* devId);
typedef NM_ErrorCode (*UnownDeviceFunc)(NM_hDeviceList dev_list, const char* devId);
+ typedef NM_ErrorCode (*SubsOnNotificationsFunc)(NM_hContext, NotificationCallback, void*);
public:
ErrorCode OwnDevice(const std::string& deviceId) override;
+// ErrorCode AddNotificationCallback(const NotificationCallback&) override;
+
struct ModuleSymbols
{
ModuleSymbols();
FreeDeviceInfoFunc m_nmFreeDeviceInfoFunc;
OwnDeviceFunc m_nmOwnDeviceFunc;
UnownDeviceFunc m_nmUnownDeviceFunc;
+ SubsOnNotificationsFunc m_nmSubsOnNotifFunc;
};
DeviceList ToDeviceList(NM_hDeviceList& nmList);
DevicePtr ToDevice(const NM_DeviceInfo* device);
+
+private:
+ void FreeCache();
void FreeDeviceList(NM_hDeviceList* nmList);
+ ErrorCode SubscribeOnNotifications();
private:
ModuleSymbols m_moduleSymbols;
DeviceState state;
} NM_DeviceInfo;
+typedef struct
+{
+ int code;
+ const char* title;
+ const char* message;
+ int time;
+} NM_NotificationData;
+
#define NULL_HANDLE (void*)0
typedef struct NM_Context* NM_hContext;
ErrorCode OwnDevice(const std::string& deviceId) override;
+ ErrorCode GetDevicePolicy(const std::string& deviceId, std::string& policy) override;
+
+ ErrorCode SetDevicePolicy(const std::string& deviceId, const std::string& policy) override;
+
+ ErrorCode GetDeviceReport(const std::string& devideId, std::string& report) override;
+
private:
ModelPtr m_model;
ViewList m_views;
MessageProcessResult UnownDevice(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
+ MessageProcessResult GetDashboard(const JSONNode& request, JSONNode& response,
+ std::string& errDescription) const;
+
private:
typedef std::unordered_map<std::string, Callback> CallbackMap;
return m_networkManager->GetUnownedDeviceList();
}
+ErrorCode Model::UnownDevice(const std::string& deviceId)
+{
+ return m_networkManager->UnownDevice(deviceId);
+}
+
+ErrorCode Model::OwnDevice(const std::string& deviceId)
+{
+ return m_networkManager->OwnDevice(deviceId);
+}
+
}
#include "utils/log.h"
#include "utils/to_string.h"
#include "utils/assert_return.h"
+#include "push_notification.h"
#include <dlfcn.h>
#define NM_FREE_DEV_INFO_FUNC_NAME "NM_freeDeviceInfo"
#define NM_OWN_DEV_FUNC_NAME "NM_ownDevice"
#define NM_UNOWN_DEV_FUNC_NAME "NM_unOwnDevice"
+#define NM_SUBS_NOTIFICATIONS_FUNC_NAME "NM_subscribeNotifications"
namespace iotswsec
{
m_nmGetDeviceInfoFunc(nullptr),
m_nmFreeDeviceInfoFunc(nullptr),
m_nmOwnDeviceFunc(nullptr),
- m_nmUnownDeviceFunc(nullptr)
+ m_nmUnownDeviceFunc(nullptr),
+ m_nmSubsOnNotifFunc(nullptr)
{
if (LoadModule() != ErrorCode::Success)
{
dlsym(this->m_module, NM_OWN_DEV_FUNC_NAME));
m_nmUnownDeviceFunc = reinterpret_cast<UnownDeviceFunc>(
dlsym(this->m_module, NM_UNOWN_DEV_FUNC_NAME));
+ m_nmSubsOnNotifFunc = reinterpret_cast<SubsOnNotificationsFunc>(
+ dlsym(this->m_module, NM_SUBS_NOTIFICATIONS_FUNC_NAME));
if (!m_nmInitFunc ||
!m_nmCleanUpFunc ||
!m_nmGetDeviceInfoFunc ||
!m_nmFreeDeviceInfoFunc ||
!m_nmOwnDeviceFunc ||
- !m_nmUnownDeviceFunc)
+ !m_nmUnownDeviceFunc ||
+ !m_nmSubsOnNotifFunc)
{
LOG_ERR("Couldn't load one of the functions due to error: %s", dlerror());
m_nmFreeDeviceInfoFunc = nullptr;
m_nmOwnDeviceFunc = nullptr;
m_nmUnownDeviceFunc = nullptr;
+ m_nmSubsOnNotifFunc = nullptr;
if (dlclose(m_module) == 0)
{
};
}
+static void PushNotificationCallback(NM_NotificationData data, void*)
+{
+ LOG_DBG("BEGIN");
+ if (data.message && data.title)
+ {
+ PushNotification::GetInstance().Push(data.title, data.message);
+ }
+ else
+ {
+ LOG_ERR("Notification generated invalid data");
+ }
+ LOG_DBG("END");
+}
+
+//ErrorCode NetworkManager::AddNotificationCallback(
+// const NotificationCallback& callback)
+//{
+// return ErrorCode::Success;
+//}
+
+ErrorCode NetworkManager::SubscribeOnNotifications()
+{
+ ASSERT_RETURN(m_moduleSymbols.m_nmSubsOnNotifFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
+ NM_ErrorCode res;
+
+ if ((res = m_moduleSymbols.m_nmSubsOnNotifFunc(m_context, PushNotificationCallback, nullptr)) != NM_ErrorCode::EC_OK)
+ {
+ LOG_ERR("Couln't subscribe on notifications, nmanager returned: %s", std::to_string(res).c_str());
+
+ return ErrorCode::Error;
+ }
+
+ return ErrorCode::Success;
+}
+
+
+
+void NetworkManager::FreeCache()
+{
+ FreeDeviceList(&m_ownedDevices);
+ FreeDeviceList(&m_unownedDevices);
+}
NetworkManager::~NetworkManager()
{
if (m_context)
{
- FreeDeviceList(&m_ownedDevices);
- FreeDeviceList(&m_unownedDevices);
+ FreeCache();
m_moduleSymbols.m_nmCleanUpFunc(&m_context);
}
}
-ErrorCode iotswsec::NetworkManager::LogIn(const UserInfo& userInfo,
+ErrorCode NetworkManager::LogIn(const UserInfo& userInfo,
const std::string& psswd)
{
ASSERT_RETURN(m_moduleSymbols.m_nmSignInFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
}
}
+ SubscribeOnNotifications();
+
return ErrorCode::Success;
}
-ErrorCode iotswsec::NetworkManager::LogOut()
+ErrorCode NetworkManager::LogOut()
{
ASSERT_RETURN(m_moduleSymbols.m_nmSignOutFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
+ FreeCache();
m_moduleSymbols.m_nmSignOutFunc(m_context);
return ErrorCode::Success;
}
-SessionState iotswsec::NetworkManager::GetSessionState()
+SessionState NetworkManager::GetSessionState()
{
ASSERT_RETURN(m_moduleSymbols.m_nmGetSessionStateFunc, "function isn't loaded", SessionState::Closed);
SignInState state = m_moduleSymbols.m_nmGetSessionStateFunc(m_context);
nmList = nullptr;
}
-DeviceList iotswsec::NetworkManager::GetOwnedDeviceList()
+DeviceList NetworkManager::GetOwnedDeviceList()
{
ASSERT_RETURN(m_moduleSymbols.m_nmGetOwnedDevFunc, "function isn't loaded", DeviceList());
NM_ErrorCode res;
return ToDeviceList(m_ownedDevices);
}
-DeviceList iotswsec::NetworkManager::GetUnownedDeviceList()
+DeviceList NetworkManager::GetUnownedDeviceList()
{
ASSERT_RETURN(m_moduleSymbols.m_nmGetUnownedDevFunc, "function isn't loaded", DeviceList());
NM_ErrorCode res;
return ToDeviceList(m_unownedDevices);
}
-ErrorCode iotswsec::NetworkManager::UnownDevice(const std::string& deviceId)
+ErrorCode NetworkManager::UnownDevice(const std::string& deviceId)
{
ASSERT_RETURN(m_moduleSymbols.m_nmUnownDeviceFunc, "function isn't loaded", ErrorCode::Error);
NM_ErrorCode res;
return ErrorCode::Success;
}
-ErrorCode iotswsec::NetworkManager::OwnDevice(const std::string& deviceId)
+ErrorCode NetworkManager::OwnDevice(const std::string& deviceId)
{
ASSERT_RETURN(m_moduleSymbols.m_nmOwnDeviceFunc, "function isn't loaded", ErrorCode::Error);
NM_ErrorCode res;
return ErrorCode::Success;
}
-}
+} //namespace iotswsec
ErrorCode Presenter::UnownDevice(const std::string& deviceId)
{
- return ErrorCode::Success;
+ return m_model->UnownDevice(deviceId);
}
ErrorCode Presenter::OwnDevice(const std::string& deviceId)
{
+ return m_model->OwnDevice(deviceId);
+}
+
+ErrorCode Presenter::GetDevicePolicy(const std::string& deviceId,
+ std::string& policy)
+{
+ //TODO: implement
return ErrorCode::Success;
}
+ErrorCode Presenter::SetDevicePolicy(const std::string& deviceId,
+ const std::string& policy)
+{
+ //TODO: implement
+ return ErrorCode::Success;
+}
+
+ErrorCode Presenter::GetDeviceReport(const std::string& devideId,
+ std::string& report)
+{
+ //TODO: implement
+ return ErrorCode::Success;
+
+}
+
}
const std::string LogInCommand = "Authorization.login";
const std::string LogOutCommand = "Authorization.logout";
const std::string SessionStateCommand = "Authorization.session.get";
+
const std::string GetOwnedDevicesCommand = "devices.owned.get";
const std::string GetUnownedDevicesCommand = "devices.unowned.get";
const std::string OwnDeviceCommand = "devices.owned.add";
const std::string UnownDeviceCommand = "devices.owned.remove";
+const std::string GetDashboardCommand = "dashboard.get";
+
//node names:
const std::string ErrorCodeNodeName = "error_code";
const std::string DataNodeName = "data";
Callback unownDeviceCallback = std::bind(&WebView::UnownDevice, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3);
RegisterCallback(UnownDeviceCommand, unownDeviceCallback);
+ Callback getDashboardCallback = std::bind(&WebView::GetDashboard, this, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3);
+ RegisterCallback(GetDashboardCommand, getDashboardCallback);
}
void WebView::Update(const std::string& command, const std::string& message)
return MessageProcessResult::Error;
}
+MessageProcessResult WebView::GetDashboard(const JSONNode& request,
+ JSONNode& response, std::string& errDescription) const
+{
+
+
+ return MessageProcessResult::Error;
+}
+
} //namespace iotswsec
DeviceList GetOwnedDeviceList() override
{
- //TODO: implement this function here and on model side.
-
DeviceList list({std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)});
DeviceList GetUnownedDeviceList() override
{
- //TODO: implement this function here and on model side.
-
DeviceList list({std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)});
return ErrorCode::Success;
}
+ ErrorCode GetDevicePolicy(const std::string& deviceId, std::string& policy) override
+ {
+ return ErrorCode::Success;
+ }
+
+ ErrorCode SetDevicePolicy(const std::string& deviceId, const std::string& policy) override
+ {
+ return ErrorCode::Success;
+ }
+
+ ErrorCode GetDeviceReport(const std::string& devideId, std::string& report) override
+ {
+ return ErrorCode::Success;
+ }
+
private:
bool m_sessionState;
};
EXPECT_EQ(std::string(R"({"id":")" + id + R"("})"), unownResponse.write());
}
+TEST_F(WebViewTest, GetDashboardTest1)
+{
+ JSONNode getDashboardRequest;
+ JSONNode getDashboardResponse;
+ std::string errDesc;
+ const std::string id("123");
+ getDashboardRequest.push_back(JSONNode("id", id));
+ MessageProcessResult result = webViewPtr->GetDashboard(getDashboardRequest, getDashboardResponse, errDesc);
+
+ EXPECT_EQ(MessageProcessResult::Success, result);
+}
+
} //namespace iotswsec