Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / 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::Ptr >::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.r.light")
79     {
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;
87
88         m_pResourceContainer->registerResource(hueLight);
89         m_vecResources.push_back(hueLight);
90     }
91 }
92
93 void HueSampleBundleActivator::destroyResource(BundleResource::Ptr pBundleResource)
94 {
95     std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl;
96
97     std::vector< BundleResource::Ptr >::iterator itor;
98
99     itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource);
100
101     if (itor != m_vecResources.end())
102     {
103         m_pResourceContainer->unregisterResource(pBundleResource);
104         m_vecResources.erase(itor);
105     }
106 }
107
108 extern "C" void huesample_externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
109         std::string bundleId)
110 {
111     bundle = new HueSampleBundleActivator();
112     bundle->activateBundle(resourceContainer, bundleId);
113 }
114
115 extern "C" void huesample_externalDeactivateBundle()
116 {
117     bundle->deactivateBundle();
118     delete bundle;
119 }
120
121 extern "C" void huesample_externalCreateResource(resourceInfo resourceInfo)
122 {
123     bundle->createResource(resourceInfo);
124 }
125
126 extern "C" void huesample_externalDestroyResource(BundleResource::Ptr pBundleResource)
127 {
128     bundle->destroyResource(pBundleResource);
129 }