[Resource-Encapsulation] Enabled Sample bundle build for Android platform
[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 #include <sstream>
25
26 SoftSensorBundleActivator *bundle;
27
28 SoftSensorBundleActivator::SoftSensorBundleActivator()
29 {
30     m_pResourceContainer = nullptr;
31 }
32
33 SoftSensorBundleActivator::~SoftSensorBundleActivator()
34 {
35     m_pResourceContainer = nullptr;
36 }
37
38 void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
39         std::string bundleId)
40 {
41     m_pResourceContainer = resourceContainer;
42     m_bundleId = bundleId;
43
44     std::vector<resourceInfo> resourceConfig;
45
46     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
47
48     for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
49          itor != resourceConfig.end(); itor++)
50     {
51         createResource(*itor);
52     }
53 }
54
55 void SoftSensorBundleActivator::deactivateBundle()
56 {
57     std::vector<BundleResource *>::iterator itor;
58     for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
59     {
60         destroyResource(*itor);
61     }
62 }
63
64 void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo)
65 {
66     if (resourceInfo.resourceType == "oic.softsensor")
67     {
68         static int discomfortIndexSensorCount = 1;
69
70         // create DISensor resource
71         DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource();
72
73         newResource->m_bundleId = m_bundleId;
74
75         std::string indexCount;//string which will contain the indexCount
76         stringstream convert; // stringstream used for the conversion
77         convert << discomfortIndexSensorCount++;//add the value of Number to the characters in the stream
78         indexCount = convert.str();//set indexCount to the content of the stream
79
80         newResource->m_uri = "/softsensor/discomfortIndex/" + indexCount;
81
82         newResource->m_resourceType = resourceInfo.resourceType;
83         newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
84
85         newResource->initAttributes();
86
87         m_pResourceContainer->registerResource(newResource);
88         m_vecResources.push_back(newResource);
89     }
90 }
91
92 void SoftSensorBundleActivator::destroyResource(BundleResource *resource)
93 {
94     std::vector <BundleResource *>::iterator itor;
95
96     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
97
98     if (itor != m_vecResources.end())
99     {
100         m_pResourceContainer->unregisterResource(resource);
101         m_vecResources.erase(itor);
102     }
103 }
104
105 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
106                                        std::string bundleId)
107 {
108     bundle = new SoftSensorBundleActivator();
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 }