Implementation of APIs for managing resource list and callback
[platform/upstream/iotivity.git] / service / simulator / src / simulator_attribute_automation.h
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 #ifndef SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_
22 #define SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_
23
24 #include "simulator_resource_model.h"
25 #include "simulator_error_codes.h"
26
27 class SimulatorResourceServer;
28
29 enum class AutomationType
30 {
31     NORMAL,
32     RECURRENT
33 };
34
35 typedef std::function<void (const std::string &, const int)> updateCompleteCallback;
36
37 class AttributeUpdateAutomation
38 {
39     public:
40         AttributeUpdateAutomation(SimulatorResourceServer *resource,
41                                   const std::string &attrName, updateCompleteCallback callback, int automationId,
42                                   std::function<void (const int)> finishedCallback, AutomationType type = AutomationType::NORMAL,
43                                   int interval = -1);
44         SimulatorResult start();
45         void stop();
46
47     private:
48         void updateAttribute();
49         void setAttributeValue();
50
51         SimulatorResourceServer *m_resource;
52         std::string m_attrName;
53         AutomationType m_type;
54         int m_id;
55         bool m_status;
56         std::thread *m_thread;
57         bool m_stopRequested;
58         int m_updateInterval;
59         SimulatorResourceModel::Attribute m_attribute;
60         updateCompleteCallback m_callback;
61         std::function<void (const int)> m_finishedCallback;
62 };
63
64 typedef std::shared_ptr<AttributeUpdateAutomation> AttributeUpdateAutomationPtr;
65
66 class ResourceUpdateAutomation
67 {
68     public:
69         ResourceUpdateAutomation(SimulatorResourceServer *resource, updateCompleteCallback callback,
70                                  int automationId, std::function<void (const int)> finishedCallback,
71                                  AutomationType type = AutomationType::NORMAL, int interval = -1);
72         SimulatorResult start();
73         void stop();
74         void finished(int id);
75
76     private:
77         void updateAttribute();
78         void setAttributeValue();
79
80         SimulatorResourceServer *m_resource;
81         AutomationType m_type;
82         int m_id;
83         bool m_status;
84         std::thread *m_thread;
85         int m_updateInterval;
86         SimulatorResourceModel m_resModel;
87         std::map<int, AttributeUpdateAutomationPtr> m_attrUpdationList;
88         updateCompleteCallback m_callback;
89         std::function<void (const int)> m_finishedCallback;
90 };
91
92 typedef std::shared_ptr<ResourceUpdateAutomation> ResourceUpdateAutomationPtr;
93
94 class UpdateAutomationManager
95 {
96     public:
97         UpdateAutomationManager();
98         SimulatorResult startResourceAutomation(SimulatorResourceServer *resource,
99                                                 int &id, updateCompleteCallback callback,
100                                                 AutomationType type = AutomationType::NORMAL, int interval = -1);
101         SimulatorResult startAttributeAutomation(SimulatorResourceServer *resource,
102                 const std::string &attrName, int &id, updateCompleteCallback callback,
103                 AutomationType type = AutomationType::NORMAL, int interval = -1);
104         std::vector<int> getResourceAutomationIds();
105         std::vector<int> getAttributeAutomationIds();
106         void stop(int automationId);
107         void stopAll();
108         void automationFinished(int id);
109
110     private:
111         std::map<int, ResourceUpdateAutomationPtr> m_resourceUpdationList;
112         std::map<int, AttributeUpdateAutomationPtr> m_attrUpdationList;
113         int m_automationId;
114         std::mutex m_mutex;
115 };
116
117 typedef std::shared_ptr<UpdateAutomationManager> UpdateAutomationManagerPtr;
118
119 #endif //SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_