iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / examples / groupclient.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 "OCPlatform.h"
22 #include "OCApi.h"
23
24 #include <functional>
25 #include <pthread.h>
26 #include <iostream>
27
28 using namespace std;
29 using namespace OC;
30 namespace PH = std::placeholders;
31
32 OCResourceHandle resourceHandle;
33
34 shared_ptr< OCResource > g_resource;
35 vector< string > lights;
36
37 bool isReady = false;
38
39 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
40
41 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
42
43 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
44
45 void foundResource(std::shared_ptr< OCResource > resource)
46 {
47     std::string resourceURI;
48     std::string hostAddress;
49
50     try
51     {
52         cout << "FOUND Resource" << endl;
53
54         if (resource)
55         {
56             string resourceURI = resource->uri();
57             cout << resourceURI << endl;
58             if (resourceURI == "/core/a/collection")
59             {
60                 g_resource = resource;
61             }
62
63             g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
64             printf("HOST :: %s\n", resource->host().c_str());
65         }
66     }
67     catch (std::exception& e)
68     {
69         std::cout << "" << std::endl;
70     }
71 }
72
73 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
74 {
75     // printf("onGet\n");
76
77     std::vector< OCRepresentation > children = rep.getChildren();
78
79     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
80     for (auto iter = children.begin(); iter != children.end(); ++iter)
81     {
82         lights.push_back((*iter).getUri());
83         cout << "\tURI :: " << (*iter).getUri() << endl;
84     }
85
86     isReady = true;
87 }
88
89 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
90 {
91     printf("\nonPut\n");
92 }
93
94 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
95 {
96     printf("\nonPost\n");
97
98     std::vector< OCRepresentation > children = rep.getChildren();
99
100     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
101     for (auto iter = children.begin(); iter != children.end(); ++iter)
102     {
103         std::string power;
104         (*iter).getValue("power", power);
105
106         cout << "\tURI :: " << (*iter).getUri() << endl;
107         cout << "\t\tpower :: " << power << endl;
108     }
109
110     if (rep.hasAttribute("ActionSet"))
111     {
112         std::string plainText;
113
114         rep.getValue("ActionSet", plainText);
115
116         printf("\tPlain Text :: %s\n", plainText.c_str());
117     }
118     else
119     {
120         printf("Not found ActionSet\n");
121     }
122 }
123
124 int main()
125 {
126     PlatformConfig config
127     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
128
129     bool isRun = true;
130
131     try
132     {
133         OCPlatform::Configure(config);
134
135         string resourceTypeName = "a.collection";
136         OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
137
138         isReady = false;
139
140         while (isRun)
141         {
142             usleep(100);
143             while (isReady)
144             {
145                 int n;
146
147                 cout << endl
148                         << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n";
149                 cout << "4 :: DELETE ACTIONSET 5 :: Quit\n";
150
151                 cin >> n;
152                 if (n == 1)
153                 {
154                     string actionsetDesc;
155                     //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|";
156
157                     actionsetDesc = "allbulboff";
158                     actionsetDesc.append("*");
159                     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
160                     {
161                         actionsetDesc.append("uri=").append((*iter));
162                         actionsetDesc.append("|");
163                         actionsetDesc.append("power=");
164                         actionsetDesc.append("off");
165                         if ((iter + 1) != lights.end())
166                         {
167                             actionsetDesc.append("*");
168                         }
169                     }
170
171                     cout << "ActionSet :: " << actionsetDesc << endl;
172
173                     OCRepresentation rep;
174                     rep.setValue("ActionSet", actionsetDesc);
175
176                     if (g_resource)
177                     {
178                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
179                                 &onPut);
180                     }
181                 }
182                 else if (n == 2)
183                 {
184                     OCRepresentation rep;
185
186                     rep.setValue("DoAction", std::string("allbulboff"));
187
188                     if (g_resource)
189                     {
190                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
191                                 &onPost);
192                     }
193                 }
194                 else if (n == 3)
195                 {
196                     OCRepresentation rep;
197
198                     rep.setValue("GetActionSet", std::string("allbulboff"));
199
200                     if (g_resource)
201                     {
202                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
203                                 &onPost);
204                     }
205                 }
206                 else if (n == 4)
207                 {
208                     OCRepresentation rep;
209
210                     rep.setValue("DelActionSet", std::string("allbulboff"));
211
212                     if (g_resource)
213                     {
214                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
215                                 &onPut);
216                     }
217                 }
218                 else if (n == 5)
219                 {
220                     isRun = false;
221                     break;
222                 }
223
224                 fflush(stdin);
225             }
226         }
227     }
228     catch (OCException& e)
229     {
230         cout << e.what() << endl;
231     }
232
233     return 0;
234 }