Repository navigation
Expand file tree
/
Copy pathValueStore.cpp
More file actions
207 lines (186 loc) · 6.79 KB
/
Copy pathValueStore.cpp
File metadata and controls
207 lines (186 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//-----------------------------------------------------------------------------
//
// ValueStore.cpp
//
// Container for Value objects
//
// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include "value_classes/ValueStore.h"
#include "value_classes/Value.h"
#include "Manager.h"
#include "Notification.h"
#include "Localization.h"
#include "platform/Log.h"
namespace OpenZWave
{
namespace Internal
{
namespace VC
{
//-----------------------------------------------------------------------------
// <ValueStore::ValueStore>
// Destructor
//-----------------------------------------------------------------------------
ValueStore::~ValueStore()
{
map<uint32, Value*>::iterator it = m_values.begin();
while (!m_values.empty())
{
ValueID const& valueId = it->second->GetID();
RemoveValue(valueId.GetValueStoreKey());
it = m_values.begin();
}
}
//-----------------------------------------------------------------------------
// <ValueStore::AddValue>
// Add a value to the store
//-----------------------------------------------------------------------------
bool ValueStore::AddValue(Value* _value)
{
if (!_value)
{
return false;
}
uint32 key = _value->GetID().GetValueStoreKey();
map<uint32, Value*>::iterator it = m_values.find(key);
if (it != m_values.end())
{
// There is already a value in the store with this key, so we give up.
return false;
}
m_values[key] = _value;
_value->AddRef();
// Notify the watchers of the new value and Check our GetChangeVerified Flag
if (Driver* driver = Manager::Get()->GetDriver(_value->GetID().GetHomeId()))
{
Node *node = driver->GetNodeUnsafe(_value->GetID().GetNodeId());
if (node) {
Internal::CC::CommandClass *cc = node->GetCommandClass(_value->GetID().GetCommandClassId());
if (cc) {
if (cc->m_com.GetFlagBool(COMPAT_FLAG_VERIFYCHANGED, _value->GetID().GetIndex())) {
Log::Write(LogLevel_Info, _value->GetID().GetNodeId(), "Setting VerifiedChanged Flag on Value %d for CC %s", _value->GetID().GetIndex(), cc->GetCommandClassName().c_str());
_value->SetChangeVerified(true);
}
if (cc->m_com.GetFlagBool(COMPAT_FLAG_NO_REFRESH_AFTER_SET, _value->GetID().GetIndex())) {
Log::Write(LogLevel_Info, _value->GetID().GetNodeId(), "Setting NoRefreshAfterSet Flag on Value %d for CC %s", _value->GetID().GetIndex(), cc->GetCommandClassName().c_str());
_value->SetRefreshAfterSet(false);
}
}
}
Notification* notification = new Notification(Notification::Type_ValueAdded);
notification->SetValueId(_value->GetID());
driver->QueueNotification(notification);
}
return true;
}
//-----------------------------------------------------------------------------
// <ValueStore::RemoveValue>
// Remove a value from the store
//-----------------------------------------------------------------------------
bool ValueStore::RemoveValue(uint32 const& _key)
{
map<uint32, Value*>::iterator it = m_values.find(_key);
if (it != m_values.end())
{
Value* value = it->second;
ValueID const& valueId = value->GetID();
// First notify the watchers
if (Driver* driver = Manager::Get()->GetDriver(valueId.GetHomeId()))
{
Notification* notification = new Notification(Notification::Type_ValueRemoved);
notification->SetValueId(valueId);
driver->QueueNotification(notification);
}
// Now release and remove the value from the store
int32 references = value->Release();
if (references > 0)
Log::Write(LogLevel_Warning, "Value Not Deleted - Still in use %d times: CC: %d - %s - %s - %d", references, valueId.GetCommandClassId(), valueId.GetTypeAsString().c_str(), value->GetLabel().c_str(), value->GetID());
else
Log::Write(LogLevel_Debug, "Value Deleted");
m_values.erase(it);
return true;
}
// Value not found in the store
return false;
}
////-----------------------------------------------------------------------------
//// <ValueStore::RemoveValue>
//// Remove a value from the store
////-----------------------------------------------------------------------------
//bool ValueStore::RemoveValue
//(
// ValueID const& _id
//)
//-----------------------------------------------------------------------------
// <ValueStore::RemoveCommandClassValues>
// Remove all the values associated with a command class from the store
//-----------------------------------------------------------------------------
void ValueStore::RemoveCommandClassValues(uint8 const _commandClassId)
{
map<uint32, Value*>::iterator it = m_values.begin();
while (it != m_values.end())
{
Value* value = it->second;
ValueID const& valueId = value->GetID();
if (_commandClassId == valueId.GetCommandClassId())
{
// The value belongs to the specified command class
// First notify the watchers
if (Driver* driver = Manager::Get()->GetDriver(valueId.GetHomeId()))
{
Notification* notification = new Notification(Notification::Type_ValueRemoved);
notification->SetValueId(valueId);
driver->QueueNotification(notification);
}
// Now release and remove the value from the store
value->Release();
m_values.erase(it++);
}
else
{
++it;
}
}
}
//-----------------------------------------------------------------------------
// <ValueStore::GetValue>
// Get a value from the store
//-----------------------------------------------------------------------------
Value* ValueStore::GetValue(uint32 const& _key) const
{
Value* value = NULL;
map<uint32, Value*>::const_iterator it = m_values.find(_key);
if (it != m_values.end())
{
value = it->second;
if (value)
{
// Add a reference to the value. The caller must
// call Release on the value when they are done with it.
value->AddRef();
}
}
return value;
}
} // namespace VC
} // namespace Internal
} // namespace OpenZWave