--- /dev/null
+/**
+ @device.h
+ @brief
+ @author Dmytro Logachev (d.logachev@samsung.com)
+ @date Created Apr 24, 2017
+ @par In Samsung Ukraine R&D Center (SRK) under a contract between
+ @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+ @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOT_SWC_DEVICE_H_
+#define IOT_SWC_DEVICE_H_
+
+#include <string>
+#include <vector>
+#include <memory>
+
+namespace iotswsec
+{
+class Device
+{
+public:
+ Device(const std::string& id = std::string(), const std::string& name = std::string(),
+ const std::string& type = std::string(),
+ const std::string& description = std::string(), int status = 0):
+ m_id(id),
+ m_name(name),
+ m_type(type),
+ m_description(description),
+ m_status(status)
+ {}
+
+ const std::string& GetDescription() const
+ {
+ return m_description;
+ }
+
+ void SetDescription(const std::string& description)
+ {
+ m_description = description;
+ }
+
+ const std::string& GetId() const
+ {
+ return m_id;
+ }
+
+ void SetId(const std::string& id)
+ {
+ m_id = id;
+ }
+
+ const std::string& GetName() const
+ {
+ return m_name;
+ }
+
+ void SetName(const std::string& name)
+ {
+ m_name = name;
+ }
+
+ const std::string& GetType() const
+ {
+ return m_type;
+ }
+
+ void SetType(const std::string& type)
+ {
+ m_type = type;
+ }
+
+ int GetStatus() const
+ {
+ return m_status;
+ }
+
+ void SetStatus(int status)
+ {
+ m_status = status;
+ }
+
+private:
+ std::string m_id;
+ std::string m_name;
+ std::string m_type;
+ std::string m_description;
+ int m_status;
+};
+
+using DevicePtr = std::shared_ptr<Device>;
+using DeviceList = std::vector<DevicePtr>;
+
+}
+
+
+#endif /* IOT_SWC_DEVICE_H_ */
--- /dev/null
+/**
+ @i_model_observer.h
+ @brief Contains declaraion of interface for model observer.
+ @author Dmytro Logachev (d.logachev@samsung.com)
+ @date Created Apr 19, 2017
+ @par In Samsung Ukraine R&D Center (SRK) under a contract between
+ @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+ @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef I_MODEL_OBSERVER_H_
+#define I_MODEL_OBSERVER_H_
+
+#include "common_types.h"
+
+#include <memory>
+
+namespace iotswsec
+{
+
+class IModelObserver
+{
+public:
+
+ virtual ~IModelObserver(){}
+
+ virtual void UpdateSessionState(SessionState) = 0;
+ //TODO: define interface for other functions
+
+};
+
+using IModelObserverWPtr = std::weak_ptr<IModelObserver>;
+
+} //namespave iotswsec
+
+#endif /* I_MODEL_OBSERVER_H_ */
#include "common_types.h"
#include <memory>
+#include "device.h"
namespace iotswsec
{
virtual SessionState GetSessionState() = 0;
+ virtual DeviceList GetOwnedDeviceList() = 0;
+
+ virtual DeviceList GetUnownedDeviceList() = 0;
+
+ virtual ErrorCode UnownDevice(const std::string& deviceId) = 0;
+
+ virtual ErrorCode OwnDevice(const std::string& deviceId) = 0;
+
private:
};
#ifndef IOTSWC_I_VIEW_H_
#define IOTSWC_I_VIEW_H_
+#include <string>
#include <memory>
#include <vector>
class IView
{
public:
- //TODO: define interface of this function;
- virtual void Update() = 0;
+
+ /**
+ @brief This function is used to notify View about update.
+ @param[in] command - describes target to be updated.
+ @param[in] message - contains extra information of update.
+ */
+ virtual void Update(const std::string& command, const std::string& message) = 0;
virtual ~IView()
{}
#include "common_types.h"
#include "userinfo.h"
+#include "i_model_observer.h"
#include <memory>
+#include <vector>
namespace iotswsec
{
{
public:
Model():
- m_sessionState(false)
+ m_sessionState(false),
+ m_observers()
{}
virtual ~Model()
virtual ErrorCode LogOut();
virtual SessionState GetSessionState();
+
+ using ModelObserversList = std::vector<IModelObserverWPtr>;
+
+ virtual void AddObserver(IModelObserverWPtr observer);
+
private:
bool m_sessionState;
+ ModelObserversList m_observers;
};
typedef std::shared_ptr<Model> ModelPtr;
#define IOTSWC_PRESENTER_H_
#include "i_presenter.h"
+#include "i_model_observer.h"
#include "i_view.h"
#include "model.h"
namespace iotswsec
{
-class Presenter : public IPresenter, /*public IModelObserver,*/ public std::enable_shared_from_this<Presenter>
+class Presenter : public IPresenter, public IModelObserver, public std::enable_shared_from_this<Presenter>
{
public:
Presenter():
ErrorCode LogOut() override;
SessionState GetSessionState() override;
+
+ void UpdateSessionState(SessionState) override;
+
+ DeviceList GetOwnedDeviceList() override;
+
+ DeviceList GetUnownedDeviceList() override;
+
+ ErrorCode UnownDevice(const std::string& deviceId) override;
+
+ ErrorCode OwnDevice(const std::string& deviceId) override;
+
private:
ModelPtr m_model;
ViewList m_views;
--- /dev/null
+/**
+ @json_utils.h
+ @brief
+ @author Dmytro Logachev (d.logachev@samsung.com)
+ @date Created Apr 24, 2017
+ @par In Samsung Ukraine R&D Center (SRK) under a contract between
+ @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+ @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef UTILS_JSON_UTILS_H_
+#define UTILS_JSON_UTILS_H_
+
+#include "device.h"
+#include "JSONOptions.h"
+#include "libjson.h"
+#include "_internal/Source/JSONNode.h"
+
+namespace iotswsec
+{
+
+JSONNode ToJson(const DevicePtr& device);
+
+JSONNode ToJson(const DeviceList& deviceList);
+
+}
+
+
+#endif /* UTILS_JSON_UTILS_H_ */
#include "i_view.h"
#include "i_presenter.h"
+#include "JSONOptions.h"
+#include "libjson.h"
#include "_internal/Source/JSONNode.h"
#include <string>
#include <unordered_map>
WebView(const IPresenterPtr& presenter, UpdateCallback updateCallback);
//IView implementation.
- void Update() override;
+ void Update(const std::string& command, const std::string& message) override;
/**
@brief Processes request from UI.
MessageProcessResult GetSessionState(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
+ MessageProcessResult GetOwnedDevices(const JSONNode& request, JSONNode& response,
+ std::string& errDescription) const;
+
+ MessageProcessResult GetUnownedDevices(const JSONNode& request, JSONNode& response,
+ std::string& errDescription) const;
+
+ MessageProcessResult OwnDevice(const JSONNode& request, JSONNode& response,
+ std::string& errDescription) const;
+
+ MessageProcessResult UnownDevice(const JSONNode& request, JSONNode& response,
+ std::string& errDescription) const;
+
private:
typedef std::unordered_map<std::string, Callback> CallbackMap;
--- /dev/null
+/**
+ @json_utils.cpp
+ @brief
+ @author Dmytro Logachev (d.logachev@samsung.com)
+ @date Created Apr 24, 2017
+ @par In Samsung Ukraine R&D Center (SRK) under a contract between
+ @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+ @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#include "utils/json_utils.h"
+
+namespace iotswsec
+{
+
+JSONNode ToJson(const DevicePtr& device)
+{
+ JSONNode json(JSON_NODE);
+
+ json.push_back(JSONNode("id", device->GetId()));
+ json.push_back(JSONNode("name", device->GetName()));
+ json.push_back(JSONNode("type", device->GetType()));
+ json.push_back(JSONNode("status", device->GetStatus()));
+ json.push_back(JSONNode("description", device->GetDescription()));
+
+ return json;
+}
+
+JSONNode ToJson(const DeviceList& deviceList)
+{
+ JSONNode json(JSON_ARRAY);
+
+ for (const auto& device : deviceList)
+ {
+ json.push_back(ToJson(device));
+ }
+
+ return json;
+}
+
+}
+
ErrorCode Model::LogIn(const UserInfo& userInfo, const std::string& psswd)
{
+ //TODO: implement
if (userInfo.GetUserLogin().compare("iotgod")
|| psswd.compare("1234"))
{
ErrorCode Model::LogOut()
{
+ //TODO: implement
if (!m_sessionState)
{
return ErrorCode::NotLoggedIn;
return m_sessionState ? SessionState::Open : SessionState::Closed;
}
+void Model::AddObserver(IModelObserverWPtr observer)
+{
+ m_observers.push_back(observer);
+}
+
}
{
return m_model->GetSessionState();
}
+
+void Presenter::UpdateSessionState(SessionState sessionState)
+{
+}
+
+DeviceList Presenter::GetOwnedDeviceList()
+{
+ //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 list;
+}
+
+DeviceList Presenter::GetUnownedDeviceList()
+{
+ //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 list;
+}
+
+ErrorCode Presenter::UnownDevice(const std::string& deviceId)
+{
+ return ErrorCode::Success;
+}
+
+ErrorCode Presenter::OwnDevice(const std::string& deviceId)
+{
+ return ErrorCode::Success;
+}
+
}
#include "web_view.h"
#include "utils/to_string.h"
#include "utils/log.h"
+#include "utils/json_utils.h"
namespace iotswsec
{
//Command names:
-const std::string LogInCommand = "Authorization.login";
-const std::string LogOutCommand = "Authorization.logout";
-const std::string SessionStateCommand = "Authorization.session.get";
+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";
//node names:
const std::string ErrorCodeNodeName = "error_code";
m_updateCallback(updateCallback)
{
Callback logInCallback = std::bind(&WebView::LogIn, this, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3);
+ std::placeholders::_2, std::placeholders::_3);
RegisterCallback(LogInCommand, logInCallback);
Callback logOutCallback = std::bind(&WebView::LogOut, this, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3);
+ std::placeholders::_2, std::placeholders::_3);
RegisterCallback(LogOutCommand, logOutCallback);
Callback getSessionStateCallback = std::bind(&WebView::GetSessionState, this, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3);
+ std::placeholders::_2, std::placeholders::_3);
RegisterCallback(SessionStateCommand, getSessionStateCallback);
-
+ Callback getOwnedDevicesCallback = std::bind(&WebView::GetOwnedDevices, this, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3);
+ RegisterCallback(GetOwnedDevicesCommand, getOwnedDevicesCallback);
+ Callback getUnownedDevicesCallback = std::bind(&WebView::GetUnownedDevices, this, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3);
+ RegisterCallback(GetUnownedDevicesCommand, getUnownedDevicesCallback);
+ Callback ownDeviceCallback = std::bind(&WebView::OwnDevice, this, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3);
+ RegisterCallback(OwnDeviceCommand, ownDeviceCallback);
+ Callback unownDeviceCallback = std::bind(&WebView::UnownDevice, this, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3);
+ RegisterCallback(UnownDeviceCommand, unownDeviceCallback);
}
-void WebView::Update()
+void WebView::Update(const std::string& command, const std::string& message)
{}
std::string WebView::ProcessRequest(const std::string& command, const JSONNode& attachedObject)
return MessageProcessResult::Success;
}
+MessageProcessResult WebView::GetOwnedDevices(const JSONNode& request,
+ JSONNode& response, std::string& errDescription) const
+{
+ //Expected JSON:
+ // {}
+ //Expected response:
+ // "devices" : [{"id":"123456789","name":"My TV","type":"smart-tv","status":0,"description":"TV device"},
+ // {"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,
+ // "description":"Smartphone device"}]
+ IPresenterPtr presenter = m_presenter.lock();
+
+ if (!presenter)
+ {
+ return MessageProcessResult::ErrorInternalConnectionFailed;
+ }
+
+ response.push_back(SetJSONNodeName("devices", ToJson(presenter->GetOwnedDeviceList())));
+
+ return MessageProcessResult::Success;
+}
+
+MessageProcessResult WebView::GetUnownedDevices(const JSONNode& request,
+ JSONNode& response, std::string& errDescription) const
+{
+ //Expected JSON:
+ // {}
+ //Expected response:
+ // "devices" : [{"id":"123456789","name":"My TV","type":"smart-tv","status":0,"description":"TV device"},
+ // {"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,
+ // "description":"Smartphone device"}]
+ IPresenterPtr presenter = m_presenter.lock();
+
+ if (!presenter)
+ {
+ return MessageProcessResult::ErrorInternalConnectionFailed;
+ }
+
+ response.push_back(SetJSONNodeName("devices", ToJson(presenter->GetUnownedDeviceList())));
+
+ return MessageProcessResult::Success;
+}
+
+MessageProcessResult WebView::OwnDevice(const JSONNode& request,
+ JSONNode& response, std::string& errDescription) const
+{
+ //Expected JSON:
+ // {"id":123}
+ //Expected response in case of success:
+ // {"id":123}
+ IPresenterPtr presenter = m_presenter.lock();
+
+ if (!presenter)
+ {
+ return MessageProcessResult::ErrorInternalConnectionFailed;
+ }
+
+ auto iteratorId = request.find("id");
+
+ if (iteratorId == request.end())
+ {
+ return MessageProcessResult::ErrorBadMessageFormat;
+ }
+
+ if (presenter->OwnDevice(iteratorId->as_string()) == ErrorCode::Success)
+ {
+ response.push_back(JSONNode("id", iteratorId->as_string()));
+
+ return MessageProcessResult::Success;
+ }
+
+ return MessageProcessResult::Error;
+}
+
+MessageProcessResult WebView::UnownDevice(const JSONNode& request,
+ JSONNode& response, std::string& errDescription) const
+{
+ //Expected JSON:
+ // "data" : {}
+ //Expected response in case of open session:
+ // "data" : {"state" : 0 }
+ IPresenterPtr presenter = m_presenter.lock();
+
+ if (!presenter)
+ {
+ return MessageProcessResult::ErrorInternalConnectionFailed;
+ }
+
+ auto iteratorId = request.find("id");
+
+ if (iteratorId == request.end())
+ {
+ return MessageProcessResult::ErrorBadMessageFormat;
+ }
+
+ if (presenter->UnownDevice(iteratorId->as_string()) == ErrorCode::Success)
+ {
+ response.push_back(JSONNode("id", iteratorId->as_string()));
+
+ return MessageProcessResult::Success;
+ }
+
+ return MessageProcessResult::Error;
+}
} //namespace iotswsec
{
return m_sessionState ? SessionState::Open : SessionState::Closed;
}
+
+ 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)});
+
+ return list;
+ }
+
+ 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 list;
+ }
+
+ ErrorCode UnownDevice(const std::string& deviceId) override
+ {
+ return ErrorCode::Success;
+ }
+
+ ErrorCode OwnDevice(const std::string& deviceId) override
+ {
+ return ErrorCode::Success;
+ }
+
private:
bool m_sessionState;
};
void SetUp() override
{
presenterPtr = std::make_shared<iotswsec::PresenterMock>();
- webViewPtr = std::make_shared < iotswsec::WebView
- > (presenterPtr, WebViewCallbackStub);
+ webViewPtr = std::make_shared<iotswsec::WebView>(presenterPtr,
+ WebViewCallbackStub);
}
std::shared_ptr<iotswsec::IPresenter> presenterPtr; ///< Pointer to presenter instance
EXPECT_EQ(std::string("{\"state\":0}"), stateResponse.write());
}
+TEST_F(WebViewTest, GetOwnedDevicesTest1)
+{
+ JSONNode ownedGetRequest;
+ JSONNode ownedGetResponse;
+ std::string errDesc;
+ MessageProcessResult result = webViewPtr->GetOwnedDevices(ownedGetRequest, ownedGetResponse, errDesc);
+
+ EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(std::string(R"({"devices":[{"id":"123456789","name":"My TV","type":"smart-tv","status":0,)"
+ R"("description":"TV device"},)"
+ R"({"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,)"
+ R"("description":"Smartphone device"}]})"), ownedGetResponse.write());
+}
+
+TEST_F(WebViewTest, GetUnownedDevicesTest1)
+{
+ JSONNode unownedGetRequest;
+ JSONNode unownedGetResponse;
+ std::string errDesc;
+ MessageProcessResult result = webViewPtr->GetUnownedDevices(unownedGetRequest, unownedGetResponse, errDesc);
+
+ EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(std::string(R"({"devices":[{"id":"123456789","name":"My TV","type":"smart-tv","status":0,)"
+ R"("description":"TV device"},)"
+ R"({"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,)"
+ R"("description":"Smartphone device"}]})"), unownedGetResponse.write());
+}
+
+TEST_F(WebViewTest, OwnDeviceTest1)
+{
+ JSONNode ownRequest;
+ JSONNode ownResponse;
+ std::string errDesc;
+ const std::string id("123");
+ ownRequest.push_back(JSONNode("id", id));
+ MessageProcessResult result = webViewPtr->OwnDevice(ownRequest, ownResponse, errDesc);
+
+ EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(std::string(R"({"id":")" + id + R"("})"), ownResponse.write());
+}
+
+TEST_F(WebViewTest, UnownDeviceTest1)
+{
+ JSONNode unownRequest;
+ JSONNode unownResponse;
+ std::string errDesc;
+ const std::string id("123");
+ unownRequest.push_back(JSONNode("id", id));
+ MessageProcessResult result = webViewPtr->UnownDevice(unownRequest, unownResponse, errDesc);
+
+ EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(std::string(R"({"id":")" + id + R"("})"), unownResponse.write());
+}
+
} //namespace iotswsec