Merge branch 'connectivity-abstraction' to master
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SampleApp / linux / SSMTesterApp / src / SSMTestApp.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <string>
22 #include <stdio.h>
23 #include <iostream>
24 #include <stdlib.h>
25
26 #include "SSMTestApp.h"
27
28 using namespace std;
29
30 SSMTestApp::SSMTestApp()
31 {
32 }
33
34 void SSMTestApp::displayMenu()
35 {
36     printf("===============================================\n");
37     printf(" Iotivity Soft Sensor Manager Test Application \n");
38     printf("===============================================\n");
39     printf("   1. Register Query \n");
40     printf("   2. Unregister Query \n");
41     printf("   3. Register DiscomfortIndexSensor sample query \n");
42     printf("   4. Register IndoorTrajectorySensor sample query \n");
43     printf("   5. Register BMISensor sample query \n");
44     printf("   9. exit \n");
45     printf("===============================================\n");
46     printf("   Please Enter the NO: ");
47 }
48
49 /* Register Query.*/
50 void SSMTestApp::registerQuery(std::string queryString)
51 {
52     int qid;
53     SSMRESULT rtn = SSM_E_FAIL;
54
55     if (queryString.size() == 0)
56     {
57         printf("   Please Enter query string: ");
58         getline(cin, queryString);
59     }
60
61     rtn = RegisterQuery(queryString, this, qid);
62
63     if (rtn == SSM_S_OK)
64     {
65         printf("The query has been registered!\n");
66         printf("QID : %d\n", qid);
67     }
68     else
69         printf("Error occured(%d)\n", rtn);
70 }
71
72 /* unRegister Query.*/
73 void SSMTestApp::unregisterQuery(void)
74 {
75     std::string qid;
76     SSMRESULT rtn = SSM_E_FAIL;
77
78     printf("   Please Enter query Id: ");
79     getline(cin, qid);
80
81     rtn = UnregisterQuery(atoi(qid.c_str()));
82
83     if (rtn == SSM_S_OK)
84         printf("The query has been unregistered!\n");
85     else
86         printf("Error occured(%d)\n", (int) rtn);
87 }
88
89
90 #define INPUT_EA        9
91 char input_name[INPUT_EA][10] = { "trackeeID", "timeT0", "ref01T0", "ref02T0", "ref03T0", "timeT1", "ref01T1", "ref02T1", "ref03T1" };
92
93 void SSMTestApp::TrajectoryDataOutput(IModelData *pModelData)
94 {
95     std::string name = "";
96     int l = 0;
97
98     std::string trackeeID;
99     std::string T0DateTime;
100     std::string T0Ref01;
101     std::string T0Ref02;
102     std::string T0Ref03;
103     std::string T1DateTime;
104     std::string T1Ref01;
105     std::string T1Ref02;
106     std::string T1Ref03;
107
108     for (int j = 0; j < pModelData->getPropertyCount(); j++)
109     {
110         name = pModelData->getPropertyName(j);
111
112         for (l = 0; l < INPUT_EA; l++)
113         {
114             if (name.compare(input_name[l]) == 0)
115                 break;
116         }
117
118         switch (l)
119         {
120             case 0:
121                 trackeeID = pModelData->getPropertyValue(j);
122                 break;
123             case 1:
124                 T0DateTime = pModelData->getPropertyValue(j);
125                 break;
126             case 2:
127                 T0Ref01 = pModelData->getPropertyValue(j);
128                 break;
129             case 3:
130                 T0Ref02 = pModelData->getPropertyValue(j);
131                 break;
132             case 4:
133                 T0Ref03 = pModelData->getPropertyValue(j);
134                 break;
135             case 5:
136                 T1DateTime = pModelData->getPropertyValue(j);
137                 break;
138             case 6:
139                 T1Ref01 = pModelData->getPropertyValue(j);
140                 break;
141             case 7:
142                 T1Ref02 = pModelData->getPropertyValue(j);
143                 break;
144             case 8:
145                 T1Ref03 = pModelData->getPropertyValue(j);
146                 break;
147             default:
148                 ;
149         }
150     }
151
152     printf("===========================================\n");
153     printf("        ITS Trajectory Data Output         \n");
154     printf("===========================================\n");
155     printf("\n");
156     printf(" < Trackee Thing ID : %s > \n", trackeeID.c_str());
157     printf("   - Trajectory 01         \n");
158     printf("     0. Date, Time : %s    \n", T0DateTime.c_str());
159     printf("     1. Ref. Thing : %s    \n", T0Ref01.c_str());
160     printf("     2. Ref. Thing : %s    \n", T0Ref02.c_str());
161     printf("     3. Ref. Thing : %s    \n", T0Ref03.c_str());
162     printf("\n");
163     printf("   - Trajectory 02         \n");
164     printf("     0. Date, Time : %s    \n", T1DateTime.c_str());
165     printf("     1. Ref. Thing : %s    \n", T1Ref01.c_str());
166     printf("     2. Ref. Thing : %s    \n", T1Ref02.c_str());
167     printf("     3. Ref. Thing : %s    \n", T1Ref03.c_str());
168     printf("\n");
169     printf("===========================================\n");
170 }
171
172 /* APP. Level Callback Function for Observer of client. */
173 SSMRESULT SSMTestApp::onQueryEngineEvent(int cqid, IDataReader *pResult)
174 {
175     int     dataCount = 0;
176     IModelData      *pModelData = NULL;
177     std::vector<std::string>        affectedModels;
178
179     cout << "Event received! cqid = " << cqid << endl;
180
181     pResult->getAffectedModels(&affectedModels);
182
183     for (std::vector<std::string>::iterator itor = affectedModels.begin();
184          itor != affectedModels.end(); ++itor)
185     {
186         cout << "Printing " << *itor << " model" << endl;
187         pResult->getModelDataCount(*itor, &dataCount);
188         for (int i = 0; i < dataCount; i++)
189         {
190             pResult->getModelData(*itor, i, &pModelData);
191             cout << "dataId: " << pModelData->getDataId() << endl;
192             for (int j = 0; j < pModelData->getPropertyCount(); j++)
193             {
194                 cout << "Type: " << pModelData->getPropertyName(j) << " Value: " << pModelData->getPropertyValue(
195                          j) << endl;
196             }
197
198             //TrajectoryDataOutput(pModelData);
199         }
200     }
201
202     return SSM_S_OK;
203 }
204
205 /**
206  * APP. Main Function.
207  */
208 int main()
209 {
210     printf("program start.\n");
211     printf("searching SSMResource\n");
212     SSMTestApp *SSMApp = new SSMTestApp();
213     APPMenu::APPMenu menu = APPMenu::NONE;
214     std::string strMenu;
215
216     std::string xmlDescription = "<SSMCore>"
217                                  "<Device>"
218                                  "<UDN>abcde123-31f8-11b4-a222-08002b34c003</UDN>"
219                                  "<Name>MyPC</Name>"
220                                  "<Type>PC</Type>"
221                                  "</Device>"
222                                  "</SSMCore>";
223
224     if (InitializeSSM(xmlDescription) != SSM_S_OK)
225         std::cout << "core init failed" << std::endl;
226
227     while (menu != APPMenu::EXIT)
228     {
229         SSMApp->displayMenu();
230
231         getline(cin, strMenu);
232         menu = (APPMenu::APPMenu) (atoi(strMenu.c_str()));
233
234         switch (menu)
235         {
236             case APPMenu::REGISTER:
237                 std::cout << "register operate." << std::endl;
238                 SSMApp->registerQuery("");
239                 break;
240
241             case APPMenu::UNREGISTER:
242                 std::cout << "unregister operate." << std::endl;
243                 SSMApp->unregisterQuery();
244                 break;
245
246             case APPMenu::DISCOMFORT_SAMPLE:
247                 SSMApp->registerQuery("subscribe Device.DiscomfortIndexSensor "\
248                                       "if Device.DiscomfortIndexSensor.discomfortIndex > 0");
249                 break;
250
251             case APPMenu::ITS_SAMPLE:
252                 SSMApp->registerQuery("subscribe Device.IndoorTrajectorySensor "\
253                                       "if Device.IndoorTrajectorySensor.trackeeID == \"9059AF16FEF7\"");
254                 break;
255
256             case APPMenu::BMI_SAMPLE:
257                 SSMApp->registerQuery("subscribe Device.BMISensor "\
258                                       "if Device.BMISensor.BMIresult > 0");
259
260                 break;
261
262
263
264
265
266             case APPMenu::EXIT:
267                 std::cout << "program exit." << std::endl;
268                 break;
269
270             default:
271                 std::cout << "Not supported yet." << std::endl;
272         }
273     } // while
274
275     TerminateSSM();
276
277     delete SSMApp;
278 }
279