Push notifications implemented
authorDmytro Logachev <d.logachev@samsung.com>
Mon, 15 May 2017 14:57:14 +0000 (17:57 +0300)
committerDmytro Logachev <d.logachev@samsung.com>
Mon, 15 May 2017 14:57:14 +0000 (17:57 +0300)
13 files changed:
control_app/iot-manager-service/.gitignore
control_app/iot-manager-service/inc/i_presenter.h
control_app/iot-manager-service/inc/model.h
control_app/iot-manager-service/inc/network/i_network_manager.h
control_app/iot-manager-service/inc/network/network_manager.h
control_app/iot-manager-service/inc/network/nm_types.h
control_app/iot-manager-service/inc/presenter.h
control_app/iot-manager-service/inc/web_view.h
control_app/iot-manager-service/src/model.cpp
control_app/iot-manager-service/src/network/network_manager.cpp
control_app/iot-manager-service/src/presenter.cpp
control_app/iot-manager-service/src/web_view.cpp
control_app/iot-manager-service/tests/web_view_test/web_view_tests.cpp

index 9cfa832..71eb9b1 100644 (file)
@@ -26,8 +26,6 @@ public:
     virtual ~IPresenter()
     {}
 
-    //TODO: define interface.
-
     virtual ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) = 0;
 
     virtual ErrorCode LogOut() = 0;
@@ -42,6 +40,12 @@ public:
 
     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:
 };
 
index d598f18..748a1ee 100644 (file)
@@ -44,6 +44,10 @@ public:
 
     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);
index 1961102..c3df345 100644 (file)
@@ -16,6 +16,7 @@
 #include "common_types.h"
 #include "device.h"
 
+#include <functional>
 #include <memory>
 
 namespace iotswsec
@@ -25,6 +26,8 @@ class INetworkManager
 {
 public:
 
+    using NotificationCallback = std::function<void(const std::string& title, const std::string& body)>;
+
     virtual ~INetworkManager() = default;
 
     //TODO: define interface.
@@ -42,6 +45,8 @@ public:
     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>;
index 7f39181..d2b3282 100644 (file)
@@ -25,6 +25,7 @@ public:
 
     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*);
@@ -44,6 +45,7 @@ public:
     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:
 
@@ -68,6 +70,8 @@ public:
 
     ErrorCode OwnDevice(const std::string& deviceId) override;
 
+//    ErrorCode AddNotificationCallback(const NotificationCallback&) override;
+
     struct ModuleSymbols
     {
         ModuleSymbols();
@@ -98,12 +102,17 @@ public:
         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;
index 8253a39..d773f5d 100644 (file)
@@ -50,6 +50,14 @@ typedef struct
     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;
index 1e0d6ce..b043536 100644 (file)
@@ -56,6 +56,12 @@ public:
 
     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;
index 46e0462..bb642d1 100644 (file)
@@ -78,6 +78,9 @@ public:
     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;
index 5648a2f..71324bf 100644 (file)
@@ -50,5 +50,15 @@ DeviceList Model::GetUnownedDeviceList()
     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);
+}
+
 }
 
index 6ead4b9..5201ef2 100644 (file)
@@ -13,6 +13,7 @@
 #include "utils/log.h"
 #include "utils/to_string.h"
 #include "utils/assert_return.h"
+#include "push_notification.h"
 
 #include <dlfcn.h>
 
@@ -37,6 +38,7 @@
 #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
 {
@@ -61,7 +63,8 @@ NetworkManager::ModuleSymbols::ModuleSymbols():
     m_nmGetDeviceInfoFunc(nullptr),
     m_nmFreeDeviceInfoFunc(nullptr),
     m_nmOwnDeviceFunc(nullptr),
-    m_nmUnownDeviceFunc(nullptr)
+    m_nmUnownDeviceFunc(nullptr),
+    m_nmSubsOnNotifFunc(nullptr)
 {
     if (LoadModule() != ErrorCode::Success)
     {
@@ -119,6 +122,8 @@ ErrorCode NetworkManager::ModuleSymbols::LoadModule()
                                                      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 ||
@@ -137,7 +142,8 @@ ErrorCode NetworkManager::ModuleSymbols::LoadModule()
         !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());
 
@@ -184,6 +190,7 @@ ErrorCode NetworkManager::ModuleSymbols::UnloadModule()
         m_nmFreeDeviceInfoFunc          = nullptr;
         m_nmOwnDeviceFunc               = nullptr;
         m_nmUnownDeviceFunc             = nullptr;
+        m_nmSubsOnNotifFunc             = nullptr;
 
         if (dlclose(m_module) == 0)
          {
@@ -216,19 +223,60 @@ NetworkManager::NetworkManager():
     };
 }
 
+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);
@@ -253,18 +301,21 @@ ErrorCode iotswsec::NetworkManager::LogIn(const UserInfo& userInfo,
         }
     }
 
+    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);
@@ -360,7 +411,7 @@ void NetworkManager::FreeDeviceList(NM_hDeviceList* nmList)
     nmList = nullptr;
 }
 
-DeviceList iotswsec::NetworkManager::GetOwnedDeviceList()
+DeviceList NetworkManager::GetOwnedDeviceList()
 {
     ASSERT_RETURN(m_moduleSymbols.m_nmGetOwnedDevFunc, "function isn't loaded", DeviceList());
     NM_ErrorCode res;
@@ -380,7 +431,7 @@ DeviceList iotswsec::NetworkManager::GetOwnedDeviceList()
     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;
@@ -397,7 +448,7 @@ DeviceList iotswsec::NetworkManager::GetUnownedDeviceList()
     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;
@@ -412,7 +463,7 @@ ErrorCode iotswsec::NetworkManager::UnownDevice(const std::string& deviceId)
     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;
@@ -427,4 +478,4 @@ ErrorCode iotswsec::NetworkManager::OwnDevice(const std::string& deviceId)
     return ErrorCode::Success;
 }
 
-}
+} //namespace iotswsec
index 31cd8d7..8185049 100644 (file)
@@ -59,12 +59,34 @@ DeviceList Presenter::GetUnownedDeviceList()
 
 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;
+
+}
+
 }
index e0a039e..94c159c 100644 (file)
@@ -21,11 +21,14 @@ namespace iotswsec
 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";
@@ -64,6 +67,9 @@ WebView::WebView(const IPresenterPtr& presenter, UpdateCallback updateCallback):
     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)
@@ -303,5 +309,13 @@ MessageProcessResult WebView::UnownDevice(const JSONNode& request,
     return MessageProcessResult::Error;
 }
 
+MessageProcessResult WebView::GetDashboard(const JSONNode& request,
+        JSONNode& response, std::string& errDescription) const
+{
+
+
+    return MessageProcessResult::Error;
+}
+
 } //namespace iotswsec
 
index bcc1593..88c7ec2 100644 (file)
@@ -51,8 +51,6 @@ public:
 
     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)});
 
@@ -61,8 +59,6 @@ public:
 
     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)});
 
@@ -79,6 +75,21 @@ public:
         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;
 };
@@ -239,4 +250,16 @@ TEST_F(WebViewTest, UnownDeviceTest1)
     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