change location of resource container directory
[platform/upstream/iotivity.git] / service / resource-container / examples / DiscomfortIndexSensorBundle / src / DISensorBundleActivator.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 "DISensorBundleActivator.h"
22
23 #include <algorithm>
24 #include <sstream>
25 #include "DiscomfortIndexSensorResource.h"
26
27 DISensorBundleActivator *bundle;
28
29 DISensorBundleActivator::DISensorBundleActivator()
30 {
31     m_pResourceContainer = nullptr;
32 }
33
34 DISensorBundleActivator::~DISensorBundleActivator()
35 {
36     m_pResourceContainer = nullptr;
37 }
38
39 void DISensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
40         std::string bundleId)
41 {
42     m_pResourceContainer = resourceContainer;
43     m_bundleId = bundleId;
44
45     std::vector<resourceInfo> resourceConfig;
46
47     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
48
49     for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
50          itor != resourceConfig.end(); itor++)
51     {
52         createResource(*itor);
53     }
54 }
55
56 void DISensorBundleActivator::deactivateBundle()
57 {
58     std::vector< BundleResource::Ptr >::iterator itor;
59     for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
60     {
61         destroyResource(*itor);
62     }
63 }
64
65 void DISensorBundleActivator::createResource(resourceInfo resourceInfo)
66 {
67     if (resourceInfo.resourceType == "oic.r.sensor")
68     {
69         static int discomfortIndexSensorCount = 1;
70
71         // create DISensor resource
72         BundleResource::Ptr newResource = std::make_shared< DiscomfortIndexSensorResource >();
73
74         newResource->m_bundleId = m_bundleId;
75
76         std::string indexCount;//string which will contain the indexCount
77         stringstream convert; // stringstream used for the conversion
78         convert << discomfortIndexSensorCount++;//add the value of Number to the characters in the stream
79         indexCount = convert.str();//set indexCount to the content of the stream
80
81         newResource->m_uri = "/softsensor/discomfortIndex/" + indexCount;
82
83         newResource->m_resourceType = resourceInfo.resourceType;
84         newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
85
86         newResource->initAttributes();
87
88         m_pResourceContainer->registerResource(newResource);
89         m_vecResources.push_back(newResource);
90     }
91 }
92
93 void DISensorBundleActivator::destroyResource(BundleResource::Ptr resource)
94 {
95     std::vector< BundleResource::Ptr >::iterator itor;
96
97     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
98
99     if (itor != m_vecResources.end())
100     {
101         m_pResourceContainer->unregisterResource(resource);
102         m_vecResources.erase(itor);
103     }
104 }
105
106 extern "C" void disensor_externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
107         std::string bundleId)
108 {
109     bundle = new DISensorBundleActivator();
110     bundle->activateBundle(resourceContainer, bundleId);
111 }
112
113 extern "C" void disensor_externalDeactivateBundle()
114 {
115     bundle->deactivateBundle();
116     delete bundle;
117 }
118
119 extern "C" void disensor_externalCreateResource(resourceInfo resourceInfo)
120 {
121     bundle->createResource(resourceInfo);
122 }
123
124 extern "C" void disensor_externalDestroyResource(BundleResource::Ptr pBundleResource)
125 {
126     bundle->destroyResource(pBundleResource);
127 }