Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / 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     m_pResourceContainer = nullptr;
34     m_connector = nullptr;
35 }
36
37 HueSampleBundleActivator::~HueSampleBundleActivator()
38 {
39     m_pResourceContainer = nullptr;
40     m_connector = nullptr;
41 }
42
43 void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
44         std::string bundleId)
45 {
46
47     m_pResourceContainer = resourceContainer;
48     m_bundleId = bundleId;
49     m_connector = new HueConnector();
50
51     vector< resourceInfo > resourceConfig;
52
53     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
54
55     for (vector< resourceInfo >::iterator itor = resourceConfig.begin();
56          itor != resourceConfig.end(); itor++)
57     {
58         createResource(*itor);
59     }
60 }
61
62 void HueSampleBundleActivator::deactivateBundle()
63 {
64     std::cout << "HueSampleBundle::deactivateBundle called" << std::endl;
65
66     std::vector<BundleResource *>::iterator itor;
67     for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
68     {
69         destroyResource(*itor);
70     }
71
72     delete m_connector;
73 }
74
75 void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
76 {
77
78     if (resourceInfo.resourceType == "oic.light.control")
79     {
80         static int lightCount = 1;
81         HueLight *hueLight = new HueLight(m_connector, resourceInfo.address);
82         resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++);
83         std::cout << "Registering resource " << resourceInfo.uri << std::endl;
84         hueLight->m_bundleId = m_bundleId;
85         hueLight->m_uri = resourceInfo.uri;
86         hueLight->m_resourceType = resourceInfo.resourceType;
87         hueLight->m_name = resourceInfo.name;
88
89         m_pResourceContainer->registerResource(hueLight);
90         m_vecResources.push_back(hueLight);
91     }
92 }
93
94 void HueSampleBundleActivator::destroyResource(BundleResource *pBundleResource)
95 {
96     std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl;
97
98     std::vector< BundleResource * >::iterator itor;
99
100     itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource);
101
102     if (itor != m_vecResources.end())
103     {
104         m_pResourceContainer->unregisterResource(pBundleResource);
105         m_vecResources.erase(itor);
106     }
107 }
108
109 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
110                                        std::string bundleId)
111 {
112     bundle = new HueSampleBundleActivator();
113     bundle->activateBundle(resourceContainer, bundleId);
114 }
115
116 extern "C" void externalDeactivateBundle()
117 {
118     bundle->deactivateBundle();
119     delete bundle;
120 }
121
122 extern "C" void externalCreateResource(resourceInfo resourceInfo)
123 {
124     bundle->createResource(resourceInfo);
125 }
126
127 extern "C" void externalDestroyResource(BundleResource *pBundleResource)
128 {
129     bundle->destroyResource(pBundleResource);
130 }