update container and sample bundles for so bundle dynamic configuraion
[platform/upstream/iotivity.git] / service / resource-manipulation / 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
24 SoftSensorBundleActivator *bundle;
25
26 SoftSensorBundleActivator::SoftSensorBundleActivator()
27 {
28 }
29
30 SoftSensorBundleActivator::~SoftSensorBundleActivator()
31 {
32 }
33
34 void SoftSensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
35         std::string bundleId)
36 {
37     std::cout << "SoftSensorSampleBundle::activateBundle called" << std::endl;
38
39     m_pResourceContainer = resourceContainer;
40     m_bundleId = bundleId;
41
42     vector<resourceInfo> resourceConfig;
43
44     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
45
46     for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
47          itor != resourceConfig.end(); itor++)
48     {
49         createResource(*itor);
50     }
51 }
52
53 void SoftSensorBundleActivator::deactivateBundle()
54 {
55     std::cout << "SoftSensorSampleBundle::deactivateBundle called" << std::endl;
56
57     for (unsigned int i = 0; i < m_vecResources.size(); i++)
58     {
59         destroyResource(m_vecResources.at(i));
60     }
61 }
62
63 void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo)
64 {
65     std::cout << "SoftSensorSampleBundle::createResource called" << std::endl;
66
67     if (resourceInfo.resourceType == "oic.softsensor")
68     {
69         static int discomfortIndexSensorCount = 1;
70
71         std::vector< std::map< std::string, std::string > >::iterator itor_vec;
72         std::map< std::string, std::string >::iterator itor_map;
73         std::vector <std::string> inputs;
74
75         for (itor_vec = resourceInfo.resourceProperty["input"].begin();
76              itor_vec != resourceInfo.resourceProperty["input"].end(); itor_vec++)
77         {
78             for (itor_map = (*itor_vec).begin(); itor_map != (*itor_vec).end(); itor_map++)
79             {
80                 inputs.push_back(itor_map->second);
81             }
82         }
83         std::cout << "SoftSensorSampleBundle::creating new discomfort index sensor " << std::endl;
84         // create DISensor resource
85         DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs);
86
87         newResource->m_bundleId = m_bundleId;
88         newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string(
89                                  discomfortIndexSensorCount++);
90         newResource->m_resourceType = resourceInfo.resourceType;
91         newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
92
93         // setting input Attributes count
94         newResource->inputCount = newResource->m_mapResourceProperty["input"].size();
95
96         m_pResourceContainer->registerResource(newResource);
97         m_vecResources.push_back(newResource);
98     }
99 }
100
101 void SoftSensorBundleActivator::destroyResource(BundleResource *resource)
102 {
103     std::cout << "SoftSensorSampleBundle::destroyResource called" << std::endl;
104
105     std::vector <BundleResource *>::iterator itor;
106
107     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
108
109     if (itor != m_vecResources.end())
110     {
111         m_pResourceContainer->unregisterResource(resource);
112         m_vecResources.erase(itor);
113     }
114 }
115
116 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
117                                        std::string bundleId)
118 {
119     bundle = new SoftSensorBundleActivator();
120     bundle->activateBundle(resourceContainer, bundleId);
121 }
122
123 extern "C" void externalDeactivateBundle()
124 {
125     bundle->deactivateBundle();
126     delete bundle;
127 }
128
129 extern "C" void externalCreateResource(resourceInfo resourceInfo)
130 {
131     bundle->createResource(resourceInfo);
132 }
133
134 extern "C" void externalDestroyResource(BundleResource *pBundleResource)
135 {
136     bundle->destroyResource(pBundleResource);
137 }