[HAM] update logs
[platform/core/api/webapi-plugins.git] / src / humanactivitymonitor / humanactivitymonitor_instance.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "humanactivitymonitor/humanactivitymonitor_instance.h"
18
19 #include <functional>
20 #include <memory>
21 #include <string>
22
23 #include "common/picojson.h"
24 #include "common/logger.h"
25 #include "common/platform_result.h"
26 #include "common/task-queue.h"
27 #include "humanactivitymonitor/humanactivitymonitor_manager.h"
28
29 namespace extension {
30 namespace humanactivitymonitor {
31
32 using common::PlatformResult;
33 using common::ErrorCode;
34 using common::TaskQueue;
35
36 HumanActivityMonitorInstance::HumanActivityMonitorInstance() {
37   LoggerD("Enter");
38   using std::placeholders::_1;
39   using std::placeholders::_2;
40
41 #define REGISTER_SYNC(c, x) \
42     RegisterSyncHandler(c, std::bind(&HumanActivityMonitorInstance::x, this, _1, _2));
43   REGISTER_SYNC("HumanActivityMonitorManager_getHumanActivityData",
44                 HumanActivityMonitorManagerGetHumanActivityData);
45   REGISTER_SYNC("HumanActivityMonitorManager_start",
46                 HumanActivityMonitorManagerStart);
47   REGISTER_SYNC("HumanActivityMonitorManager_stop",
48                 HumanActivityMonitorManagerStop);
49   REGISTER_SYNC("HumanActivityMonitorManager_setAccumulativePedometerListener",
50                 HumanActivityMonitorManagerSetAccumulativePedometerListener);
51   REGISTER_SYNC("HumanActivityMonitorManager_unsetAccumulativePedometerListener",
52                 HumanActivityMonitorManagerUnsetAccumulativePedometerListener);
53 #undef REGISTER_SYNC
54 }
55
56 HumanActivityMonitorInstance::~HumanActivityMonitorInstance() {
57   LoggerD("Enter");
58 }
59
60 PlatformResult HumanActivityMonitorInstance::Init() {
61   LoggerD("Enter");
62   if (!manager_) {
63
64     manager_ = std::make_shared<HumanActivityMonitorManager>();
65     const PlatformResult& result = manager_->Init();
66     if (!result) {
67       LOGGER(ERROR) << "Error initializing manager: " << result.message();
68       manager_.reset();
69       return result;
70     }
71   }
72
73   return PlatformResult(ErrorCode::NO_ERROR);
74 }
75
76 #define CHECK_EXIST(args, name, out) \
77     if (!args.contains(name)) { \
78       LogAndReportError(PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, \
79           name" is required argument"), &out); \
80       return; \
81     }
82
83
84 void HumanActivityMonitorInstance::HumanActivityMonitorManagerGetHumanActivityData(
85     const picojson::value& args, picojson::object& out) {
86   LoggerD("Enter");
87   CHECK_EXIST(args, "type", out)
88
89   PlatformResult result = Init();
90   if (!result) {
91     LogAndReportError(result, &out, ("Failed: Init()"));
92     return;
93   }
94
95   auto get = [this, args]() -> void {
96     picojson::value response = picojson::value(picojson::object());
97     picojson::object& response_obj = response.get<picojson::object>();
98     response_obj["callbackId"] = args.get("callbackId");
99
100     picojson::value data = picojson::value();
101     PlatformResult result = manager_->GetHumanActivityData(
102         args.get("type").get<std::string>(),
103         &data);
104
105     if (result) {
106       ReportSuccess(data, response_obj);
107     } else {
108       LogAndReportError(result, &response_obj, ("Failed: manager_->GetHumanActivityData()"));
109     }
110
111     Instance::PostMessage(this, response.serialize().c_str());
112   };
113
114   TaskQueue::GetInstance().Async(get);
115
116   ReportSuccess(out);
117 }
118
119 void HumanActivityMonitorInstance::HumanActivityMonitorManagerStart(
120     const picojson::value& args, picojson::object& out) {
121   LoggerD("Enter");
122   CHECK_EXIST(args, "type", out)
123
124   PlatformResult result = Init();
125   if (!result) {
126     LogAndReportError(result, &out, ("Failed: Init()"));
127     return;
128   }
129
130   JsonCallback cb = [this, args](picojson::value* data) -> void {
131     if (!data) {
132       LOGGER(ERROR) << "No data passed to json callback";
133       return;
134     }
135
136     picojson::object& data_o = data->get<picojson::object>();
137     data_o["listenerId"] = args.get("listenerId");
138
139     Instance::PostMessage(this, data->serialize().c_str());
140   };
141
142   result = manager_->SetListener(args.get("type").get<std::string>(), cb);
143   if (result) {
144     ReportSuccess(out);
145   } else {
146     LogAndReportError(result, &out, ("Failed: manager_->SetListener()"));
147   }
148 }
149
150 void HumanActivityMonitorInstance::HumanActivityMonitorManagerStop(
151     const picojson::value& args, picojson::object& out) {
152   LoggerD("Enter");
153   CHECK_EXIST(args, "type", out)
154
155   PlatformResult result = Init();
156   if (!result) {
157     LogAndReportError(result, &out, ("Failed: Init()"));
158     return;
159   }
160
161   result = manager_->UnsetListener(args.get("type").get<std::string>());
162   if (result) {
163     ReportSuccess(out);
164   } else {
165     LogAndReportError(result, &out, ("Failed: manager_->UnsetListener()"));
166   }
167 }
168
169 void HumanActivityMonitorInstance::HumanActivityMonitorManagerSetAccumulativePedometerListener(
170     const picojson::value& args, picojson::object& out) {
171   // TODO(r.galka) implement
172 }
173
174 void HumanActivityMonitorInstance::HumanActivityMonitorManagerUnsetAccumulativePedometerListener(
175     const picojson::value& args, picojson::object& out) {
176   // TODO(r.galka) implement
177 }
178
179 #undef CHECK_EXIST
180
181 }  // namespace humanactivitymonitor
182 }  // namespace extension