1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "HueSampleBundleActivator.h"
27 using namespace OIC::Service;
29 HueSampleBundleActivator *bundle;
31 HueSampleBundleActivator::HueSampleBundleActivator()
33 m_pResourceContainer = nullptr;
34 m_connector = nullptr;
37 HueSampleBundleActivator::~HueSampleBundleActivator()
39 m_pResourceContainer = nullptr;
40 m_connector = nullptr;
43 void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
47 m_pResourceContainer = resourceContainer;
48 m_bundleId = bundleId;
49 m_connector = new HueConnector();
51 vector< resourceInfo > resourceConfig;
53 resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
55 for (vector< resourceInfo >::iterator itor = resourceConfig.begin();
56 itor != resourceConfig.end(); itor++)
58 createResource(*itor);
62 void HueSampleBundleActivator::deactivateBundle()
64 std::cout << "HueSampleBundle::deactivateBundle called" << std::endl;
66 std::vector<BundleResource *>::iterator itor;
67 for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
69 destroyResource(*itor);
75 void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
78 if (resourceInfo.resourceType == "oic.light.control")
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;
89 m_pResourceContainer->registerResource(hueLight);
90 m_vecResources.push_back(hueLight);
94 void HueSampleBundleActivator::destroyResource(BundleResource *pBundleResource)
96 std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl;
98 std::vector< BundleResource * >::iterator itor;
100 itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource);
102 if (itor != m_vecResources.end())
104 m_pResourceContainer->unregisterResource(pBundleResource);
105 m_vecResources.erase(itor);
109 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
110 std::string bundleId)
112 bundle = new HueSampleBundleActivator();
113 bundle->activateBundle(resourceContainer, bundleId);
116 extern "C" void externalDeactivateBundle()
118 bundle->deactivateBundle();
122 extern "C" void externalCreateResource(resourceInfo resourceInfo)
124 bundle->createResource(resourceInfo);
127 extern "C" void externalDestroyResource(BundleResource *pBundleResource)
129 bundle->destroyResource(pBundleResource);