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::Ptr >::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.r.light")
80 static int lightCount = 1;
81 BundleResource::Ptr hueLight = std::make_shared< HueLight >(m_connector, resourceInfo.address);
82 resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++);
83 hueLight->m_bundleId = m_bundleId;
84 hueLight->m_uri = resourceInfo.uri;
85 hueLight->m_resourceType = resourceInfo.resourceType;
86 hueLight->m_name = resourceInfo.name;
88 m_pResourceContainer->registerResource(hueLight);
89 m_vecResources.push_back(hueLight);
93 void HueSampleBundleActivator::destroyResource(BundleResource::Ptr pBundleResource)
95 std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl;
97 std::vector< BundleResource::Ptr >::iterator itor;
99 itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource);
101 if (itor != m_vecResources.end())
103 m_pResourceContainer->unregisterResource(pBundleResource);
104 m_vecResources.erase(itor);
108 extern "C" void huesample_externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
109 std::string bundleId)
111 bundle = new HueSampleBundleActivator();
112 bundle->activateBundle(resourceContainer, bundleId);
115 extern "C" void huesample_externalDeactivateBundle()
117 bundle->deactivateBundle();
121 extern "C" void huesample_externalCreateResource(resourceInfo resourceInfo)
123 bundle->createResource(resourceInfo);
126 extern "C" void huesample_externalDestroyResource(BundleResource::Ptr pBundleResource)
128 bundle->destroyResource(pBundleResource);