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