File indexing completed on 2024-07-21 06:28:22

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <basedevice.h>
0008 #include <KLocalizedString>
0009 #include <qdbusmetatype.h>
0010 
0011 #include "indidustcap.h"
0012 #include "dustcapadaptor.h"
0013 
0014 namespace ISD
0015 {
0016 
0017 const QList<const char *> DustCap::capStates = { I18N_NOOP("Idle"), I18N_NOOP("Parking"), I18N_NOOP("UnParking"),
0018                                                  I18N_NOOP("Parked"), I18N_NOOP("Error")
0019                                                };
0020 
0021 DustCap::DustCap(GenericDevice *parent): ConcreteDevice(parent)
0022 {
0023     qRegisterMetaType<ISD::DustCap::Status>("ISD::DustCap::Status");
0024     qDBusRegisterMetaType<ISD::DustCap::Status>();
0025 
0026     new DustCapAdaptor(this);
0027     m_DBusObjectPath = QString("/KStars/INDI/DustCap/%1").arg(getID());
0028     QDBusConnection::sessionBus().registerObject(m_DBusObjectPath, this);
0029 }
0030 
0031 void DustCap::processSwitch(INDI::Property prop)
0032 {
0033     auto svp = prop.getSwitch();
0034     if (svp->isNameMatch("CAP_PARK"))
0035     {
0036         Status currentStatus = CAP_ERROR;
0037         ParkStatus currentParkStatus = PARK_UNKNOWN;
0038 
0039         switch (svp->getState())
0040         {
0041             case IPS_IDLE:
0042                 if (svp->at(0)->getState() == ISS_ON)
0043                 {
0044                     currentStatus = CAP_PARKED;
0045                     currentParkStatus = PARK_PARKED;
0046                 }
0047                 else if (svp->at(1)->getState() == ISS_ON)
0048                 {
0049                     currentStatus = CAP_IDLE;
0050                     currentParkStatus = PARK_UNPARKED;
0051                 }
0052                 break;
0053 
0054             case IPS_OK:
0055                 if (svp->at(0)->getState() == ISS_ON)
0056                 {
0057                     currentStatus = CAP_PARKED;
0058                     currentParkStatus = PARK_PARKED;
0059                 }
0060                 else
0061                 {
0062                     currentStatus = CAP_IDLE;
0063                     currentParkStatus = PARK_UNPARKED;
0064                 }
0065                 break;
0066 
0067             case IPS_BUSY:
0068                 if (svp->at(0)->getState() == ISS_ON)
0069                 {
0070                     currentStatus = CAP_PARKING;
0071                     currentParkStatus = PARK_PARKING;
0072                 }
0073                 else
0074                 {
0075                     currentStatus = CAP_UNPARKING;
0076                     currentParkStatus = PARK_UNPARKING;
0077                 }
0078                 break;
0079 
0080             case IPS_ALERT:
0081                 currentStatus = CAP_ERROR;
0082                 currentParkStatus = PARK_ERROR;
0083         }
0084 
0085         if (currentStatus != m_Status)
0086         {
0087             m_Status = currentStatus;
0088             emit newStatus(m_Status);
0089         }
0090 
0091         if (currentParkStatus != m_ParkStatus)
0092         {
0093             m_ParkStatus = currentParkStatus;
0094             emit newParkStatus(m_ParkStatus);
0095         }
0096 
0097 
0098     }
0099 }
0100 
0101 bool DustCap::canPark()
0102 {
0103     auto parkSP = getSwitch("CAP_PARK");
0104     if (!parkSP)
0105         return false;
0106     else
0107         return true;
0108 }
0109 
0110 bool DustCap::isParked()
0111 {
0112     auto parkSP = getSwitch("CAP_PARK");
0113     if (!parkSP)
0114         return false;
0115 
0116     return ((parkSP->getState() == IPS_OK || parkSP->getState() == IPS_IDLE) && parkSP->at(0)->getState() == ISS_ON);
0117 }
0118 
0119 bool DustCap::isUnParked()
0120 {
0121     auto parkSP = getSwitch("CAP_PARK");
0122     if (!parkSP)
0123         return false;
0124 
0125     return ( (parkSP->getState() == IPS_OK || parkSP->getState() == IPS_IDLE) && parkSP->at(1)->getState() == ISS_ON);
0126 }
0127 
0128 bool DustCap::park()
0129 {
0130     auto parkSP = getSwitch("CAP_PARK");
0131     if (!parkSP)
0132         return false;
0133 
0134     auto parkSW = parkSP->findWidgetByName("PARK");
0135     if (!parkSW)
0136         return false;
0137 
0138     parkSP->reset();
0139     parkSW->setState(ISS_ON);
0140     sendNewProperty(parkSP);
0141 
0142     return true;
0143 }
0144 
0145 bool DustCap::unpark()
0146 {
0147     auto parkSP = getSwitch("CAP_PARK");
0148     if (!parkSP)
0149         return false;
0150 
0151     auto parkSW = parkSP->findWidgetByName("UNPARK");
0152     if (!parkSW)
0153         return false;
0154 
0155     parkSP->reset();
0156     parkSW->setState(ISS_ON);
0157     sendNewProperty(parkSP);
0158 
0159     return true;
0160 }
0161 
0162 const QString DustCap::getStatusString(DustCap::Status status, bool translated)
0163 {
0164     return translated ? i18n(capStates[status]) : capStates[status];
0165 }
0166 
0167 }
0168 
0169 QDBusArgument &operator<<(QDBusArgument &argument, const ISD::DustCap::Status &source)
0170 {
0171     argument.beginStructure();
0172     argument << static_cast<int>(source);
0173     argument.endStructure();
0174     return argument;
0175 }
0176 
0177 const QDBusArgument &operator>>(const QDBusArgument &argument, ISD::DustCap::Status &dest)
0178 {
0179     int a;
0180     argument.beginStructure();
0181     argument >> a;
0182     argument.endStructure();
0183     dest = static_cast<ISD::DustCap::Status>(a);
0184     return argument;
0185 }