Kstars

indidustcap.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Jasem Mutlaq <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include <basedevice.h>
8 #include <KLocalizedString>
9 #include <QtDBus/qdbusmetatype.h>
10 
11 #include "indidustcap.h"
12 #include "dustcapadaptor.h"
13 
14 namespace ISD
15 {
16 
17 const QList<const char *> DustCap::capStates = { I18N_NOOP("Idle"), I18N_NOOP("Parking"), I18N_NOOP("UnParking"),
18  I18N_NOOP("Parked"), I18N_NOOP("Error")
19  };
20 
21 DustCap::DustCap(GenericDevice *parent): ConcreteDevice(parent)
22 {
23  qRegisterMetaType<ISD::DustCap::Status>("ISD::DustCap::Status");
24  qDBusRegisterMetaType<ISD::DustCap::Status>();
25 
26  new DustCapAdaptor(this);
27  m_DBusObjectPath = QString("/KStars/INDI/DustCap/%1").arg(getID());
28  QDBusConnection::sessionBus().registerObject(m_DBusObjectPath, this);
29 }
30 
31 void DustCap::processSwitch(INDI::Property prop)
32 {
33  auto svp = prop.getSwitch();
34  if (svp->isNameMatch("CAP_PARK"))
35  {
36  Status currentStatus = CAP_ERROR;
37  ParkStatus currentParkStatus = PARK_UNKNOWN;
38 
39  switch (svp->getState())
40  {
41  case IPS_IDLE:
42  if (svp->at(0)->getState() == ISS_ON)
43  {
44  currentStatus = CAP_PARKED;
45  currentParkStatus = PARK_PARKED;
46  }
47  else if (svp->at(1)->getState() == ISS_ON)
48  {
49  currentStatus = CAP_IDLE;
50  currentParkStatus = PARK_UNPARKED;
51  }
52  break;
53 
54  case IPS_OK:
55  if (svp->at(0)->getState() == ISS_ON)
56  {
57  currentStatus = CAP_PARKED;
58  currentParkStatus = PARK_PARKED;
59  }
60  else
61  {
62  currentStatus = CAP_IDLE;
63  currentParkStatus = PARK_UNPARKED;
64  }
65  break;
66 
67  case IPS_BUSY:
68  if (svp->at(0)->getState() == ISS_ON)
69  {
70  currentStatus = CAP_PARKING;
71  currentParkStatus = PARK_PARKING;
72  }
73  else
74  {
75  currentStatus = CAP_UNPARKING;
76  currentParkStatus = PARK_UNPARKING;
77  }
78  break;
79 
80  case IPS_ALERT:
81  currentStatus = CAP_ERROR;
82  currentParkStatus = PARK_ERROR;
83  }
84 
85  if (currentStatus != m_Status)
86  {
87  m_Status = currentStatus;
88  emit newStatus(m_Status);
89  }
90 
91  if (currentParkStatus != m_ParkStatus)
92  {
93  m_ParkStatus = currentParkStatus;
94  emit newParkStatus(m_ParkStatus);
95  }
96 
97 
98  }
99 }
100 
101 bool DustCap::canPark()
102 {
103  auto parkSP = getSwitch("CAP_PARK");
104  if (!parkSP)
105  return false;
106  else
107  return true;
108 }
109 
110 bool DustCap::isParked()
111 {
112  auto parkSP = getSwitch("CAP_PARK");
113  if (!parkSP)
114  return false;
115 
116  return ((parkSP->getState() == IPS_OK || parkSP->getState() == IPS_IDLE) && parkSP->at(0)->getState() == ISS_ON);
117 }
118 
119 bool DustCap::isUnParked()
120 {
121  auto parkSP = getSwitch("CAP_PARK");
122  if (!parkSP)
123  return false;
124 
125  return ( (parkSP->getState() == IPS_OK || parkSP->getState() == IPS_IDLE) && parkSP->at(1)->getState() == ISS_ON);
126 }
127 
129 {
130  auto parkSP = getSwitch("CAP_PARK");
131  if (!parkSP)
132  return false;
133 
134  auto parkSW = parkSP->findWidgetByName("PARK");
135  if (!parkSW)
136  return false;
137 
138  parkSP->reset();
139  parkSW->setState(ISS_ON);
140  sendNewProperty(parkSP);
141 
142  return true;
143 }
144 
146 {
147  auto parkSP = getSwitch("CAP_PARK");
148  if (!parkSP)
149  return false;
150 
151  auto parkSW = parkSP->findWidgetByName("UNPARK");
152  if (!parkSW)
153  return false;
154 
155  parkSP->reset();
156  parkSW->setState(ISS_ON);
157  sendNewProperty(parkSP);
158 
159  return true;
160 }
161 
162 const QString DustCap::getStatusString(DustCap::Status status, bool translated)
163 {
164  return translated ? i18n(capStates[status]) : capStates[status];
165 }
166 
167 }
168 
169 QDBusArgument &operator<<(QDBusArgument &argument, const ISD::DustCap::Status &source)
170 {
171  argument.beginStructure();
172  argument << static_cast<int>(source);
173  argument.endStructure();
174  return argument;
175 }
176 
177 const QDBusArgument &operator>>(const QDBusArgument &argument, ISD::DustCap::Status &dest)
178 {
179  int a;
180  argument.beginStructure();
181  argument >> a;
182  argument.endStructure();
183  dest = static_cast<ISD::DustCap::Status>(a);
184  return argument;
185 }
Q_SCRIPTABLE bool park()
Park Close dust cap.
INDI::PropertyView< ISwitch > * getSwitch(const QString &name) const
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
QString i18n(const char *text, const TYPE &arg...)
QDBusConnection sessionBus()
Q_SCRIPTABLE bool unpark()
UnPark Open dust cap.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QDebug operator<<(QDebug d, const QCPVector2D &vec)
Definition: qcustomplot.h:446
void beginStructure()
void sendNewProperty(INDI::Property prop)
Send new property command to server.
void endStructure()
#define I18N_NOOP(text)
QDataStream & operator>>(QDataStream &in, KDateTime &dateTime)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Feb 5 2023 04:03:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.