update container and sample bundles for so bundle dynamic configuraion
[platform/upstream/iotivity.git] / service / resource-manipulation / src / resourceContainer / examples / HueSampleBundle / src / HueSampleBundleActivator.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 "HueSampleBundleActivator.h"
22 #include "HueLight.h"
23
24 #include <algorithm>
25 #include <vector>
26
27 using namespace OIC::Service;
28
29 HueSampleBundleActivator *bundle;
30
31 HueSampleBundleActivator::HueSampleBundleActivator()
32 {
33 }
34
35 HueSampleBundleActivator::~HueSampleBundleActivator()
36 {
37 }
38
39 void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
40         std::string bundleId)
41 {
42     std::cout << "HueSampleBundle::activateBundle called" << std::endl;
43
44     m_pResourceContainer = resourceContainer;
45     m_bundleId = bundleId;
46     m_connector = new HueConnector();
47
48     vector< resourceInfo > resourceConfig;
49
50     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
51
52     for (vector< resourceInfo >::iterator itor = resourceConfig.begin();
53          itor != resourceConfig.end(); itor++)
54     {
55         createResource(*itor);
56     }
57 }
58
59 void HueSampleBundleActivator::deactivateBundle()
60 {
61     std::cout << "HueSampleBundle::deactivateBundle called" << std::endl;
62
63     for (unsigned int i = 0; i < m_vecResources.size(); i++)
64     {
65         destroyResource(m_vecResources.at(i));
66     }
67
68     delete m_connector;
69 }
70
71 void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
72 {
73     std::cout << "HueSampleBundle::createResource called" << std::endl;
74
75     if (resourceInfo.resourceType == "oic.light.control")
76     {
77         static int lightCount = 1;
78         HueLight *hueLight = new HueLight(m_connector, resourceInfo.address);
79         resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++);
80         std::cout << "Registering resource " << resourceInfo.uri << std::endl;
81         hueLight->m_bundleId = m_bundleId;
82         hueLight->m_uri = resourceInfo.uri;
83         hueLight->m_resourceType = resourceInfo.resourceType;
84         hueLight->m_name = resourceInfo.name;
85
86         m_pResourceContainer->registerResource(hueLight);
87         m_vecResources.push_back(hueLight);
88     }
89 }
90
91 void HueSampleBundleActivator::destroyResource(BundleResource *pBundleResource)
92 {
93     std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl;
94
95     std::vector< BundleResource * >::iterator itor;
96
97     itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource);
98
99     if (itor != m_vecResources.end())
100     {
101         m_pResourceContainer->unregisterResource(pBundleResource);
102         m_vecResources.erase(itor);
103     }
104
105     //TODO
106     /*std::cout << "Clearing up memory.\n";
107
108     if (itor != m_vecResources.end())
109         m_vecResources.erase(itor);*/
110
111     // check
112     //delete resource;
113
114 }
115
116 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
117                                        std::string bundleId)
118 {
119     bundle = new HueSampleBundleActivator();
120     bundle->activateBundle(resourceContainer, bundleId);
121 }
122
123 extern "C" void externalDeactivateBundle()
124 {
125     bundle->deactivateBundle();
126     delete bundle;
127 }
128
129 extern "C" void externalCreateResource(resourceInfo resourceInfo)
130 {
131     bundle->createResource(resourceInfo);
132 }
133
134 extern "C" void externalDestroyResource(BundleResource *pBundleResource)
135 {
136     bundle->destroyResource(pBundleResource);
137 }