iotivity 0.9.0
[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
25 #include "SSMTestApp.h"
26
27 using namespace std;
28
29 SSMTestApp::SSMTestApp()
30 {
31 }
32
33 void SSMTestApp::displayMenu()
34 {
35     printf("===============================================\n");
36     printf(" Iotivity Soft Sensor Manager Test Application \n");
37     printf("===============================================\n");
38     printf("   1. Register Query \n");
39     printf("   2. Unregister Query \n");
40     printf("   3. Register DiscomfortIndexSensor sample query \n");
41     printf("   9. exit \n");
42     printf("===============================================\n");
43     printf("   Please Enter the NO: ");
44 }
45
46 /* Register Query.*/
47 void SSMTestApp::registerQuery(std::string queryString)
48 {
49     int qid;
50     SSMRESULT rtn = SSM_E_FAIL;
51
52     if (queryString.size() == 0)
53     {
54         printf("   Please Enter query string: ");
55         cin.ignore();
56         getline(cin, queryString);
57     }
58
59     rtn = m_SSMClient.registerQuery(queryString, this, qid);
60
61     if (rtn == SSM_S_OK)
62     {
63         printf("The query has been registered!\n");
64         printf("QID : %d\n", qid);
65     }
66     else
67         printf("Error occured(%d)", rtn);
68 }
69
70 /* unRegister Query.*/
71 void SSMTestApp::unregisterQuery(void)
72 {
73     std::string qid;
74     SSMRESULT rtn = SSM_E_FAIL;
75
76     printf("   Please Enter query string: ");
77     cin.ignore();
78     getline(cin, qid);
79
80     rtn = m_SSMClient.unregisterQuery(atoi(qid.c_str()));
81
82     if (rtn == SSM_S_OK)
83         printf("The query has been unregistered!\n");
84     else
85         printf("Error occured(%d)\n", (int) rtn);
86 }
87
88 /* APP. Level Callback Function for Observer of client. */
89 SSMRESULT SSMTestApp::onQueryEngineEvent(int cqid, IDataReader *pResult)
90 {
91     int     dataCount = 0;
92     IModelData      *pModelData = NULL;
93     std::vector<std::string>        affectedModels;
94
95     cout << "Event received! cqid = " << cqid << endl;
96
97     pResult->getAffectedModels(&affectedModels);
98
99     for (std::vector<std::string>::iterator itor = affectedModels.begin();
100          itor != affectedModels.end(); ++itor)
101     {
102         cout << "Printing " << *itor << " model" << endl;
103         pResult->getModelDataCount(*itor, &dataCount);
104         for (int i = 0; i < dataCount; i++)
105         {
106             pResult->getModelData(*itor, i, &pModelData);
107             cout << "dataId: " << pModelData->getDataId() << endl;
108             for (int j = 0; j < pModelData->getPropertyCount(); j++)
109             {
110                 cout << "Type: " << pModelData->getPropertyName(j) << " Value: " << pModelData->getPropertyValue(
111                          j) << endl;
112             }
113         }
114     }
115
116     return SSM_S_OK;
117 }
118
119 /**
120  * APP. Main Function.
121  */
122 int main()
123 {
124     printf("program start.\n");
125     printf("searching SSMResource\n");
126     SSMTestApp *SSMApp = new SSMTestApp();
127     APPMenu::APPMenu menu = APPMenu::NONE;
128
129     while (menu != APPMenu::EXIT)
130     {
131         SSMApp->displayMenu();
132
133         menu = (APPMenu::APPMenu) (getchar() - '0');
134         if ((APPMenu::APPMenu) 0 > menu || menu > APPMenu::EXIT)
135             menu = (APPMenu::APPMenu) (getchar() - '0');
136
137         switch (menu)
138         {
139             case APPMenu::REGISTER:
140                 std::cout << "register operate." << std::endl;
141                 SSMApp->registerQuery("");
142                 break;
143
144             case APPMenu::UNREGISTER:
145                 std::cout << "unregister operate." << std::endl;
146                 SSMApp->unregisterQuery();
147                 break;
148
149             case APPMenu::DISCOMFORT_SAMPLE:
150                 SSMApp->registerQuery("subscribe Device.DiscomfortIndexSensor "\
151                                       "if Device.DiscomfortIndexSensor.discomfortIndex > 0");
152                 break;
153
154             case APPMenu::EXIT:
155                 std::cout << "program exit." << std::endl;
156                 break;
157
158             default:
159                 std::cout << "Not supported yet." << std::endl;
160         }
161     } // while
162
163     delete SSMApp;
164 }
165