1 /*
2     SPDX-FileCopyrightText: 2015 Jasem Mutlaq <mutlaqja@ikarustech.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "dustcap.h"
8 
9 #include "dustcapadaptor.h"
10 #include "ekos/manager.h"
11 #include "kstars.h"
12 
13 #include <basedevice.h>
14 
15 namespace Ekos
16 {
DustCap()17 DustCap::DustCap()
18 {
19     new DustCapAdaptor(this);
20     QDBusConnection::sessionBus().registerObject("/KStars/Ekos/DustCap", this);
21 }
22 
setDustCap(ISD::GDInterface * newDustCap)23 void DustCap::setDustCap(ISD::GDInterface *newDustCap)
24 {
25     if (newDustCap == currentDustCap)
26         return;
27 
28     currentDustCap = static_cast<ISD::DustCap *>(newDustCap);
29 
30     currentDustCap->disconnect(this);
31 
32     connect(currentDustCap, &ISD::GDInterface::propertyDefined, this, &DustCap::processProp);
33     connect(currentDustCap, &ISD::GDInterface::switchUpdated, this, &DustCap::processSwitch);
34     connect(currentDustCap, &ISD::GDInterface::numberUpdated, this, &DustCap::processNumber);
35     connect(currentDustCap, &ISD::DustCap::newStatus, this, &DustCap::newStatus);
36     connect(currentDustCap, &ISD::DustCap::ready, this, &DustCap::ready);
37 }
38 
removeDevice(ISD::GDInterface * device)39 void DustCap::removeDevice(ISD::GDInterface *device)
40 {
41     device->disconnect(this);
42     if (currentDustCap && (currentDustCap->getDeviceName() == device->getDeviceName()))
43     {
44         currentDustCap = nullptr;
45     }
46 }
47 
processProp(INDI::Property prop)48 void DustCap::processProp(INDI::Property prop)
49 {
50     if (!prop->getRegistered())
51         return;
52 
53     if (prop->isNameMatch("FLAT_LIGHT_CONTROL"))
54     {
55         auto svp = prop->getSwitch();
56         if ((svp->at(0)->getState() == ISS_ON) != m_LightEnabled)
57         {
58             m_LightEnabled = (svp->at(0)->getState() == ISS_ON);
59             emit lightToggled(m_LightEnabled);
60         }
61     }
62     else if (prop->isNameMatch("FLAT_LIGHT_INTENSITY"))
63     {
64         auto nvp = prop->getNumber();
65         uint16_t newIntensity = static_cast<uint16_t>(nvp->at(0)->getValue());
66         if (newIntensity != m_lightIntensity)
67         {
68             m_lightIntensity = newIntensity;
69             emit lightIntensityChanged(m_lightIntensity);
70         }
71     }
72 }
processSwitch(ISwitchVectorProperty * svp)73 void DustCap::processSwitch(ISwitchVectorProperty *svp)
74 {
75     if (!strcmp(svp->name, "CAP_PARK"))
76     {
77         ISD::ParkStatus newStatus = ISD::PARK_UNKNOWN;
78 
79         switch (svp->s)
80         {
81             case IPS_IDLE:
82                 if (svp->sp[0].s == ISS_ON)
83                     newStatus = ISD::PARK_PARKED;
84                 else if (svp->sp[1].s == ISS_ON)
85                     newStatus = ISD::PARK_UNPARKED;
86                 else
87                     newStatus = ISD::PARK_UNKNOWN;
88                 break;
89 
90             case IPS_OK:
91                 if (svp->sp[0].s == ISS_ON)
92                     newStatus = ISD::PARK_PARKED;
93                 else
94                     newStatus = ISD::PARK_UNPARKED;
95                 break;
96 
97             case IPS_BUSY:
98                 if (svp->sp[0].s == ISS_ON)
99                     newStatus = ISD::PARK_PARKING;
100                 else
101                     newStatus = ISD::PARK_UNPARKING;
102                 break;
103 
104             case IPS_ALERT:
105                 newStatus = ISD::PARK_ERROR;
106         }
107 
108         if (newStatus != m_ParkStatus)
109         {
110             m_ParkStatus = newStatus;
111             emit newParkStatus(newStatus);
112         }
113     }
114     else if (!strcmp(svp->name, "FLAT_LIGHT_CONTROL"))
115     {
116         if ((svp->sp[0].s == ISS_ON) != m_LightEnabled)
117         {
118             m_LightEnabled = (svp->sp[0].s == ISS_ON);
119             emit lightToggled(m_LightEnabled);
120         }
121     }
122 }
123 
processNumber(INumberVectorProperty * nvp)124 void DustCap::processNumber(INumberVectorProperty *nvp)
125 {
126     if (!strcmp(nvp->name, "FLAT_LIGHT_INTENSITY"))
127     {
128         uint16_t newIntensity = static_cast<uint16_t>(nvp->np[0].value);
129         if (newIntensity != m_lightIntensity)
130         {
131             m_lightIntensity = newIntensity;
132             emit lightIntensityChanged(m_lightIntensity);
133         }
134     }
135 }
136 
park()137 bool DustCap::park()
138 {
139     if (currentDustCap == nullptr)
140         return false;
141 
142     return currentDustCap->Park();
143 }
144 
unpark()145 bool DustCap::unpark()
146 {
147     if (currentDustCap == nullptr)
148         return false;
149 
150     return currentDustCap->UnPark();
151 }
152 
canPark()153 bool DustCap::canPark()
154 {
155     if (currentDustCap == nullptr)
156         return false;
157 
158     return currentDustCap->canPark();
159 }
160 
hasLight()161 bool DustCap::hasLight()
162 {
163     if (currentDustCap == nullptr)
164         return false;
165 
166     return currentDustCap->hasLight();
167 }
168 
setLightEnabled(bool enable)169 bool DustCap::setLightEnabled(bool enable)
170 {
171     if (currentDustCap == nullptr)
172         return false;
173 
174     return currentDustCap->SetLightEnabled(enable);
175 }
176 
setBrightness(uint16_t val)177 bool DustCap::setBrightness(uint16_t val)
178 {
179     if (currentDustCap == nullptr)
180         return false;
181 
182     return currentDustCap->SetBrightness(val);
183 }
184 }
185