Fix for observe request handling issue.
[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 SimulatorResource;
28
29 enum class AutomationType
30 {
31     NORMAL,
32     RECURRENT
33 };
34
35 class AttributeUpdateAutomation
36 {
37     public:
38         AttributeUpdateAutomation(SimulatorResource *resource,
39                                   const std::string &attrName, AutomationType type = AutomationType::NORMAL, int interval = -1);
40         SimulatorResult start();
41         void stop();
42
43     private:
44         void updateAttribute();
45         void setAttributeValue();
46
47         SimulatorResource *m_resource;
48         std::string m_attrName;
49         AutomationType m_type;
50         bool m_status;
51         std::thread *m_thread;
52         bool m_stopRequested;
53         int m_updateInterval;
54         SimulatorResourceModel::Attribute m_attribute;
55 };
56
57 typedef std::shared_ptr<AttributeUpdateAutomation> AttributeUpdateAutomationPtr;
58
59 class ResourceUpdateAutomation
60 {
61     public:
62         ResourceUpdateAutomation(SimulatorResource *resource,
63                                  AutomationType type = AutomationType::NORMAL, int interval = -1);
64         SimulatorResult start();
65         void stop();
66
67     private:
68         void updateAttribute();
69         void setAttributeValue();
70
71         SimulatorResource *m_resource;
72         AutomationType m_type;
73         bool m_status;
74         std::thread *m_thread;
75         int m_updateInterval;
76         SimulatorResourceModel m_resModel;
77         std::vector<AttributeUpdateAutomationPtr> m_attrUpdationList;
78 };
79
80 typedef std::shared_ptr<ResourceUpdateAutomation> ResourceUpdateAutomationPtr;
81
82 class UpdateAutomationManager
83 {
84     public:
85         UpdateAutomationManager();
86         SimulatorResult startResourceAutomation(SimulatorResource *resource,
87                                                 int &id, AutomationType type = AutomationType::NORMAL, int interval = -1);
88         SimulatorResult startAttributeAutomation(SimulatorResource *resource,
89                 const std::string &attrName, int &id, AutomationType type = AutomationType::NORMAL,
90                 int interval = -1);
91         void stop(int automationId);
92         void stopAll();
93
94     private:
95         std::map<int, ResourceUpdateAutomationPtr> m_resourceUpdationList;
96         std::map<int, AttributeUpdateAutomationPtr> m_attrUpdationList;
97         int m_automationId;
98         std::mutex m_mutex;
99 };
100
101 typedef std::shared_ptr<UpdateAutomationManager> UpdateAutomationManagerPtr;
102
103 #endif //SIMULATOR_RESOURCE_UPDATE_AUTOMATION_H_