File indexing completed on 2024-04-21 03:44:11

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "indiweather.h"
0008 #include "weatheradaptor.h"
0009 
0010 #include <basedevice.h>
0011 #include <QJsonObject>
0012 #include <qdbusmetatype.h>
0013 
0014 namespace ISD
0015 {
0016 
0017 Weather::Weather(GenericDevice *parent) : ConcreteDevice(parent)
0018 {
0019     qRegisterMetaType<ISD::Weather::Status>("ISD::Weather::Status");
0020     qDBusRegisterMetaType<ISD::Weather::Status>();
0021 
0022     new WeatherAdaptor(this);
0023     m_DBusObjectPath = QString("/KStars/INDI/Weather/%1").arg(getID());
0024     QDBusConnection::sessionBus().registerObject(m_DBusObjectPath, this);
0025 }
0026 
0027 void Weather::processLight(INDI::Property prop)
0028 {
0029     auto lvp = prop.getLight();
0030     if (lvp->isNameMatch("WEATHER_STATUS"))
0031     {
0032         Status currentStatus = static_cast<Status>(lvp->s);
0033         if (currentStatus != m_WeatherStatus)
0034         {
0035             m_WeatherStatus = currentStatus;
0036             emit newStatus(m_WeatherStatus);
0037         }
0038     }
0039 }
0040 
0041 void Weather::processNumber(INDI::Property prop)
0042 {
0043     auto nvp = prop.getNumber();
0044 
0045     if (nvp->isNameMatch("WEATHER_PARAMETERS"))
0046     {
0047         m_Data = QJsonArray();
0048 
0049         // read all sensor values received
0050         for (int i = 0; i < nvp->nnp; i++)
0051         {
0052             auto number = nvp->at(i);
0053             QJsonObject sensor =
0054             {
0055                 {"name", number->getName()},
0056                 {"label", number->getLabel()},
0057                 {"value", number->getValue()}
0058             };
0059             m_Data.push_back(sensor);
0060         }
0061         emit newData(m_Data);
0062         emit newJSONData(QJsonDocument(m_Data).toJson());
0063     }
0064 }
0065 
0066 Weather::Status Weather::status()
0067 {
0068     auto weatherLP = getLight("WEATHER_STATUS");
0069 
0070     if (!weatherLP)
0071         return WEATHER_IDLE;
0072 
0073     m_WeatherStatus = static_cast<Status>(weatherLP->getState());
0074 
0075     return static_cast<Status>(weatherLP->getState());
0076 }
0077 
0078 int Weather::refreshPeriod()
0079 {
0080     auto updateNP = getNumber("WEATHER_UPDATE");
0081 
0082     if (!updateNP)
0083         return 0;
0084 
0085     return static_cast<int>(updateNP->at(0)->getValue());
0086 }
0087 
0088 void Weather::setRefreshPeriod(int value)
0089 {
0090     auto updateNP = getNumber("WEATHER_UPDATE");
0091 
0092     if (!updateNP)
0093         return;
0094 
0095     updateNP->at(0)->setValue(value);
0096     sendNewProperty(updateNP);
0097 }
0098 
0099 bool Weather::refresh()
0100 {
0101     auto refreshSP = getSwitch("WEATHER_REFRESH");
0102 
0103     if (refreshSP == nullptr)
0104         return false;
0105 
0106     auto refreshSW = refreshSP->findWidgetByName("REFRESH");
0107 
0108     if (refreshSW == nullptr)
0109         return false;
0110 
0111     refreshSP->reset();
0112     refreshSW->setState(ISS_ON);
0113     sendNewProperty(refreshSP);
0114 
0115     return true;
0116 
0117 }
0118 }
0119 
0120 #ifndef KSTARS_LITE
0121 QDBusArgument &operator<<(QDBusArgument &argument, const ISD::Weather::Status &source)
0122 {
0123     argument.beginStructure();
0124     argument << static_cast<int>(source);
0125     argument.endStructure();
0126     return argument;
0127 }
0128 
0129 const QDBusArgument &operator>>(const QDBusArgument &argument, ISD::Weather::Status &dest)
0130 {
0131     int a;
0132     argument.beginStructure();
0133     argument >> a;
0134     argument.endStructure();
0135     dest = static_cast<ISD::Weather::Status>(a);
0136     return argument;
0137 }
0138 #endif