Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / examples / SoftSensorSampleBundle / src / SoftSensorBundleActivator.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 "SoftSensorBundleActivator.h"
22 #include "DiscomfortIndexSensorResource.h"
23 #include <algorithm>
24
25 SoftSensorBundleActivator *bundle;
26
27 SoftSensorBundleActivator::SoftSensorBundleActivator()
28 {
29     m_pResourceContainer = nullptr;
30 }
31
32 SoftSensorBundleActivator::~SoftSensorBundleActivator()
33 {
34     m_pResourceContainer = nullptr;
35 }
36
37 void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
38         std::string bundleId)
39 {
40     m_pResourceContainer = resourceContainer;
41     m_bundleId = bundleId;
42
43     std::vector<resourceInfo> resourceConfig;
44
45     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
46
47     for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
48          itor != resourceConfig.end(); itor++)
49     {
50         createResource(*itor);
51     }
52 }
53
54 void SoftSensorBundleActivator::deactivateBundle()
55 {
56     std::vector<BundleResource *>::iterator itor;
57     for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
58     {
59         destroyResource(*itor);
60     }
61 }
62
63 void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo)
64 {
65     if (resourceInfo.resourceType == "oic.softsensor")
66     {
67         static int discomfortIndexSensorCount = 1;
68
69         // create DISensor resource
70         DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource();
71
72         newResource->m_bundleId = m_bundleId;
73         newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string(
74                                  discomfortIndexSensorCount++);
75         newResource->m_resourceType = resourceInfo.resourceType;
76         newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
77
78         newResource->initAttributes();
79
80         m_pResourceContainer->registerResource(newResource);
81         m_vecResources.push_back(newResource);
82     }
83 }
84
85 void SoftSensorBundleActivator::destroyResource(BundleResource *resource)
86 {
87     std::vector <BundleResource *>::iterator itor;
88
89     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
90
91     if (itor != m_vecResources.end())
92     {
93         m_pResourceContainer->unregisterResource(resource);
94         m_vecResources.erase(itor);
95     }
96 }
97
98 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
99                                        std::string bundleId)
100 {
101     bundle = new SoftSensorBundleActivator();
102     bundle->activateBundle(resourceContainer, bundleId);
103 }
104
105 extern "C" void externalDeactivateBundle()
106 {
107     bundle->deactivateBundle();
108     delete bundle;
109 }
110
111 extern "C" void externalCreateResource(resourceInfo resourceInfo)
112 {
113     bundle->createResource(resourceInfo);
114 }
115
116 extern "C" void externalDestroyResource(BundleResource *pBundleResource)
117 {
118     bundle->destroyResource(pBundleResource);
119 }