Instrument Neutral Distributed Interface INDI 2.2.5
watchdeviceproperty.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 Pawel Soja <[email protected]>
3 Copyright (C) 2011 Jasem Mutlaq. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#include "watchdeviceproperty.h"
20#include "basedevice.h"
21#include "basedevice_p.h"
22
23namespace INDI
24{
25
26std::vector<BaseDevice> WatchDeviceProperty::getDevices() const
27{
28 std::vector<BaseDevice> result;
29 for (const auto &it : data)
30 {
31 result.push_back(it.second.device);
32 }
33 return result;
34}
35
37{
38 auto it = data.find(name);
39 return it != data.end() ? it->second.device : BaseDevice();
40}
41
43 const std::function<ParentDevice()> &constructor)
44{
45 auto &it = data[name];
46 if (!it.device.isValid())
47 {
48 it.device = constructor();
49 it.device.setDeviceName(name);
50 it.device.attach();
51 it.emitWatchDevice();
52 }
53 return it;
54}
55
57{
58 return data.empty();
59}
60
61bool WatchDeviceProperty::isDeviceWatched(const char *name) const
62{
63 return watchedDevice.size() == 0 || watchedDevice.find(name) != watchedDevice.end();
64}
65
70
71void WatchDeviceProperty::watchDevice(const std::string &deviceName)
72{
73 watchedDevice.insert(deviceName);
74}
75
76void WatchDeviceProperty::watchDevice(const std::string &deviceName, const std::function<void (BaseDevice)> &callback)
77{
78 watchedDevice.insert(deviceName);
79 data[deviceName].newDeviceCallback = callback;
80}
81
82void WatchDeviceProperty::watchProperty(const std::string &deviceName, const std::string &propertyName)
83{
84 watchedDevice.insert(deviceName);
85 data[deviceName].properties.insert(propertyName);
86}
87
89{
90 data.clear();
91}
92
94{
95 for (auto &deviceInfo : data)
96 {
97 deviceInfo.second.device = ParentDevice(ParentDevice::Invalid);
98 }
99}
100
102{
103 for (auto it = data.begin(); it != data.end();)
104 {
105 if (it->second.device.getDeviceName() == device.getDeviceName())
106 {
107 it = data.erase(it);
108 return true;
109 }
110 else
111 ++it;
112 }
113 return false;
114}
115
117 const std::function<ParentDevice()> &constructor)
118{
119 auto deviceName = root.getAttribute("device");
120 if (!deviceName.isValid() || deviceName.toString() == "" || !isDeviceWatched(deviceName))
121 {
122 return 0;
123 }
124
125 // Get the device information, if not available, create it
126 auto &deviceInfo = ensureDeviceByName(deviceName, constructor);
127
128 // If we are asked to watch for specific properties only, we ignore everything else
129 if (deviceInfo.properties.size() != 0)
130 {
131 const auto it = deviceInfo.properties.find(root.getAttribute("name").toString());
132 if (it == deviceInfo.properties.end())
133 return 0;
134 }
135
136 static const std::set<std::string> defVectors
137 {
138 "defTextVector", "defNumberVector", "defSwitchVector",
139 "defLightVector", "defBLOBVector"
140 };
141
142 if (defVectors.find(root.tagName()) != defVectors.end())
143 {
144 return deviceInfo.device.buildProp(root, errmsg);
145 }
146
147 static const std::set<std::string> setVectors
148 {
149 "setTextVector", "setNumberVector", "setSwitchVector",
150 "setLightVector", "setBLOBVector"
151 };
152
153 if (setVectors.find(root.tagName()) != setVectors.end())
154 {
155 return deviceInfo.device.setValue(root, errmsg);
156 }
157
158 return INDI_DISPATCH_ERROR;
159}
160
161}
hid_device * device
Class to provide basic INDI device functionality.
Definition basedevice.h:52
std::string tagName() const
Definition indililxml.h:372
LilXmlAttribute getAttribute(const char *name) const
Definition indililxml.h:405
std::string toString() const
Definition indililxml.h:265
The class is used to create device instances. Class copying is not allowed. When an object is destroy...
void watchProperty(const std::string &deviceName, const std::string &propertyName)
bool isDeviceWatched(const char *deviceName) const
checks if the device is being watched by something
std::vector< BaseDevice > getDevices() const
DeviceInfo & ensureDeviceByName(const char *name, const std::function< ParentDevice()> &constructor)
std::map< std::string, DeviceInfo > data
BaseDevice getDeviceByName(const char *name)
std::set< std::string > watchedDevice
void watchDevice(const std::string &deviceName)
int processXml(const INDI::LilXmlElement &root, char *errmsg, const std::function< ParentDevice()> &constructor=[] { return ParentDevice(ParentDevice::Valid);})
bool deleteDevice(const BaseDevice &device)
@ INDI_DISPATCH_ERROR
Namespace to encapsulate INDI client, drivers, and mediator classes.