Update Resource container - server builder integration, softsensor template,
[platform/upstream/iotivity.git] / service / basis / resourceContainer / examples / SampleBundle / src / SampleBundle.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 "SampleBundle.h"
22 #include "DiscomfortIndexSensorResource.h"
23
24 using namespace RC;
25
26 SampleBundle *bundle;
27
28 SampleBundle::SampleBundle()
29 {
30 }
31
32 SampleBundle::~SampleBundle()
33 {
34 }
35
36 void SampleBundle::activateBundle(ResourceContainerBundleAPI *resourceContainer,
37                                   std::string bundleId)
38 {
39     std::cout << "SampleBundle::activateBundle called" << std::endl;
40
41     m_pResourceContainer = resourceContainer;
42     m_bundleId = bundleId;
43
44     vector<Configuration::resourceInfo> resourceConfig;
45
46     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
47
48     for (vector<Configuration::resourceInfo>::iterator itor = resourceConfig.begin();
49          itor != resourceConfig.end(); itor++)
50     {
51         createResource(*itor);
52     }
53 }
54
55 void SampleBundle::deactivateBundle()
56 {
57     std::cout << "SampleBundle::deactivateBundle called" << std::endl;
58
59     for (std::vector<BundleResource *>::iterator itor = m_vecResources.begin();
60          itor != m_vecResources.end(); itor++)
61     {
62         destroyResource(*itor);
63     }
64 }
65
66 // TODO : has to be updated for setting the info
67 void SampleBundle::createResource(Configuration::resourceInfo resourceInfo)
68 {
69     std::cout << "SampleBundle::createResource called" << std::endl;
70
71     DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource();
72     newResource->setResourceInfo(resourceInfo);
73
74     m_pResourceContainer->registerResource(newResource);
75
76     m_vecResources.push_back(newResource);
77 }
78
79 void SampleBundle::destroyResource(BundleResource *resource)
80 {
81     std::cout << "SampleBundle::destroyResource called" << std::endl;
82
83     std::vector <BundleResource *>::iterator itor;
84
85     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
86
87     if (itor != m_vecResources.end())
88         m_vecResources.erase(itor);
89
90     // check
91     //delete resource;
92
93     m_pResourceContainer->unregisterResource(resource);
94 }
95
96 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
97                                        std::string bundleId)
98 {
99     bundle = new SampleBundle();
100     bundle->activateBundle(resourceContainer, bundleId);
101 }
102
103 extern "C" void externalDeactivateBundle()
104 {
105     if (!bundle)
106     {
107         bundle->deactivateBundle();
108     }
109 }