Instrument Neutral Distributed Interface INDI 2.2.5
basedevice_p.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2011 Jasem Mutlaq. All rights reserved.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*******************************************************************************/
18
19#pragma once
20
21#include "indimacros.h"
22#include "basedevice.h"
23#include "lilxml.h"
24#include "indibase.h"
25
26#include <deque>
27#include <string>
28#include <mutex>
29#include <map>
30#include <functional>
31
32#include "indipropertyblob.h"
33#include "indililxml.h"
34
35namespace INDI
36{
37
38class BaseDevice;
40{
41 public:
43 virtual ~BaseDevicePrivate();
44
46 int setBLOB(INDI::PropertyBlob propertyBlob, const INDI::LilXmlElement &root, char *errmsg);
47
48 void emitWatchProperty(const INDI::Property &property, bool isNew)
49 {
50 auto it = watchPropertyMap.find(property.getName());
51 if (it != watchPropertyMap.end())
52 {
53 if (
54 (it->second.watch == BaseDevice::WATCH_NEW_OR_UPDATE) ||
55 (it->second.watch == BaseDevice::WATCH_NEW && isNew) ||
56 (it->second.watch == BaseDevice::WATCH_UPDATE && !isNew)
57 )
58 it->second.callback(property);
59 }
60 }
61
62 void addProperty(const INDI::Property &property)
63 {
64 {
65 std::unique_lock<std::mutex> lock(m_Lock);
66 pAll.push_back(property);
67 }
68
69 emitWatchProperty(property, true);
70 }
71
72 public: // mediator
74 {
75 if (mediator)
76 {
77#if INDI_VERSION_MAJOR < 2
79#endif
80 mediator->newDevice(baseDevice);
81 }
82 }
83
85 {
86 if (mediator)
87 {
88#if INDI_VERSION_MAJOR < 2
90#endif
91 mediator->removeDevice(baseDevice);
92 }
93 }
94
96 {
97 if (mediator)
98 {
99#if INDI_VERSION_MAJOR < 2
100 mediator->newProperty(property.self());
101#endif
102 mediator->newProperty(property);
103 }
104 }
105
107 {
108 emitWatchProperty(property, false);
109 if (mediator)
110 {
111 mediator->updateProperty(property);
112#if INDI_VERSION_MAJOR < 2
113 switch (property.getType())
114 {
115 case INDI_NUMBER:
116 mediator->newNumber(property.getNumber()->cast());
117 break;
118 case INDI_SWITCH:
119 mediator->newSwitch(property.getSwitch()->cast());
120 break;
121 case INDI_TEXT:
122 mediator->newText(property.getText()->cast());
123 break;
124 case INDI_LIGHT:
125 mediator->newLight(property.getLight()->cast());
126 break;
127 case INDI_BLOB:
128 for (auto &it : PropertyBlob(property))
129 {
130 mediator->newBLOB(it.cast());
131 }
132 break;
133 case INDI_UNKNOWN:
134 ;
135 }
136#endif
137 }
138 }
139
141 {
142 if (mediator)
143 {
144#if INDI_VERSION_MAJOR < 2
145 mediator->removeProperty(property.self());
146#endif
147 mediator->removeProperty(property);
148 }
149 }
150
151 void mediateNewMessage(BaseDevice baseDevice, int messageID)
152 {
153 if (mediator)
154 {
155#if INDI_VERSION_MAJOR < 2
156 mediator->newMessage(&self, messageID);
157#endif
158 mediator->newMessage(baseDevice, messageID);
159 }
160 }
161 public:
162 static std::shared_ptr<BaseDevicePrivate> invalid()
163 {
164 static struct Invalid : public BaseDevicePrivate
165 {
166 Invalid() { valid = false; }
167 } invalid;
168 return make_shared_weak(&invalid);
169 }
170
171 public:
177
178 public:
179 BaseDevice self {make_shared_weak(this)}; // backward compatible (for operators as pointer)
180 std::string deviceName;
182 std::map<std::string, WatchDetails> watchPropertyMap;
184
186 std::deque<std::string> messageLog;
187 mutable std::mutex m_Lock;
188
189 bool valid {true};
190};
191
192}
void emitWatchProperty(const INDI::Property &property, bool isNew)
BaseDevice::Properties pAll
INDI::BaseMediator * mediator
void mediateRemoveDevice(BaseDevice baseDevice)
void addProperty(const INDI::Property &property)
static std::shared_ptr< BaseDevicePrivate > invalid()
void mediateNewMessage(BaseDevice baseDevice, int messageID)
void mediateUpdateProperty(Property property)
void mediateNewProperty(Property property)
void mediateNewDevice(BaseDevice baseDevice)
std::map< std::string, WatchDetails > watchPropertyMap
void mediateRemoveProperty(Property property)
int setBLOB(INDI::PropertyBlob propertyBlob, const INDI::LilXmlElement &root, char *errmsg)
Parse and store BLOB in the respective vector.
std::deque< std::string > messageLog
Class to provide basic INDI device functionality.
Definition basedevice.h:52
Meditates event notification as generated by driver and passed to clients.
Definition indibase.h:98
virtual void newText(ITextVectorProperty *tvp)
Emmited when a new text value arrives from INDI server.
Definition indibase.cpp:59
virtual void updateProperty(INDI::Property property)
Emmited when a new property value arrives from INDI server.
Definition indibase.cpp:20
virtual void removeProperty(INDI::Property property)
Emmited when a property is deleted for an INDI driver.
Definition indibase.cpp:23
virtual void newProperty(INDI::Property property)
Emmited when a new property is created for an INDI driver.
Definition indibase.cpp:17
virtual void newDevice(INDI::BaseDevice baseDevice)
Emmited when a new device is created from INDI server.
Definition indibase.cpp:9
virtual void newSwitch(ISwitchVectorProperty *svp)
Emmited when a new switch value arrives from INDI server.
Definition indibase.cpp:53
virtual void newLight(ILightVectorProperty *lvp)
Emmited when a new light value arrives from INDI server.
Definition indibase.cpp:62
virtual void newBLOB(IBLOB *bp)
Emmited when a new property value arrives from INDI server.
Definition indibase.cpp:65
virtual void removeDevice(INDI::BaseDevice baseDevice)
Emmited when a device is deleted from INDI server.
Definition indibase.cpp:12
virtual void newNumber(INumberVectorProperty *nvp)
Emmited when a new number value arrives from INDI server.
Definition indibase.cpp:56
virtual void newMessage(INDI::BaseDevice baseDevice, int messageID)
Emmited when a new message arrives from INDI server.
Definition indibase.cpp:28
void push_back(const INDI::Property &property)
Provides generic container for INDI properties.
INDI::PropertyViewSwitch * getSwitch() const
INDI::PropertyViewText * getText() const
INDI::Property * self()
INDI::PropertyViewNumber * getNumber() const
const char * getName() const
INDI::PropertyViewLight * getLight() const
INDI_PROPERTY_TYPE getType() const
@ INDI_LIGHT
Definition indidriver.c:60
@ INDI_TEXT
Definition indidriver.c:59
@ INDI_UNKNOWN
Definition indidriver.c:62
@ INDI_NUMBER
Definition indidriver.c:57
@ INDI_SWITCH
Definition indidriver.c:58
@ INDI_BLOB
Definition indidriver.c:61
A little DOM-style library to handle parsing and processing an XML file.
Namespace to encapsulate INDI client, drivers, and mediator classes.
std::function< void(INDI::Property)> callback
static PropertyView< T > * cast(PropertyType *raw)