34#ifdef _USE_SYSTEM_JSONLIB
35#include <nlohmann/json.hpp>
126 LOG_INFO(
"Connection settings updated.");
136 LOG_INFO(
"Alpaca device number updated.");
165 return "Alpaca Dome";
179 m_ConfigHost = configHost;
181 m_ConfigPort = configPort;
190 DeviceNumberNP[0].fill(
"DEVICE_NUMBER",
"Device Number",
"%.0f", 0, 10, 1, 0);
246 LOG_INFO(
"Alpaca dome is ready for operation.");
253 LOG_INFO(
"Alpaca dome is disconnected.");
278 LOG_ERROR(
"Server address or port is not set.");
286 httpClient->set_connection_timeout(std::chrono::milliseconds(connectionTimeout));
287 httpClient->set_read_timeout(std::chrono::milliseconds(timeout));
288 httpClient->set_write_timeout(std::chrono::milliseconds(timeout));
291 bool connected =
false;
292 if (!alpacaGetBool(
"/connected", connected))
294 LOG_ERROR(
"Failed to connect to Alpaca dome. Please check server address and port.");
301 if (!alpacaConnect())
305 LOG_INFO(
"Successfully connected to Alpaca dome.");
308 alpacaGetString(
"/name", m_DeviceName);
309 alpacaGetString(
"/description", m_Description);
310 alpacaGetString(
"/driverinfo", m_DriverInfo);
311 alpacaGetString(
"/driverversion", m_DriverVersion);
312 alpacaGetInt(
"/interfaceversion", m_InterfaceVersion);
315 alpacaGetBool(
"/canfindhome", m_CanFindHome);
316 alpacaGetBool(
"/canpark", m_CanPark);
317 alpacaGetBool(
"/cansetaltitude", m_CanSetAltitude);
318 alpacaGetBool(
"/cansetazimuth", m_CanSetAzimuth);
319 alpacaGetBool(
"/cansetpark", m_CanSetPark);
320 alpacaGetBool(
"/cansetshutter", m_CanSetShutter);
321 alpacaGetBool(
"/canslave", m_CanSlave);
322 alpacaGetBool(
"/cansyncazimuth", m_CanSyncAzimuth);
330 if (m_CanSyncAzimuth)
337 bool slewing, parked;
338 if (alpacaGetBool(
"/slewing", slewing,
false) && alpacaGetBool(
"/atpark", parked,
true))
352 if (!alpacaDisconnect())
354 LOG_ERROR(
"Failed to set disconnected state on Alpaca dome.");
362 LOG_INFO(
"Disconnected from Alpaca dome.");
380 LOG_ERROR(
"Dome does not support azimuth slewing.");
394 LOG_ERROR(
"Dome does not support azimuth slewing.");
398 if (!alpacaDomeSlewToAzimuth(
range360(az)))
409 LOG_ERROR(
"Dome does not support azimuth slewing.");
437 LOG_ERROR(
"Dome does not support syncing.");
442 bool success = alpacaDomeSyncToAzimuth(targetAz);
446 LOGF_INFO(
"Dome successfully synchronized to azimuth angle %.2f.", targetAz);
451 LOGF_ERROR(
"Error: cannot synchronize dome to azimuth angle %.2f.", targetAz);
462 if (alpacaDomeAbortSlew())
468 LOG_WARN(
"Finding home operation is aborted.");
472 LOG_WARN(
"Calibrating operation is aborted.");
480 LOG_WARN(
"Shutter motion aborted!");
499 if (alpacaDomePark())
529 if (alpacaDomeOpenShutter())
542 LOG_WARN(
"Cannot close dome when mount is locking. See: Telescope parking policy, in options tab");
547 if (alpacaDomeCloseShutter())
566 LOG_ERROR(
"Dome does not support setting park position.");
570 if (alpacaDomeSetPark())
572 LOG_INFO(
"Dome park position successfully set to current azimuth");
576 LOG_ERROR(
"Error: cannot set park position to current azimuth");
593 LOG_ERROR(
"Dome does not support homing.");
597 if (alpacaDomeFindHome())
599 LOG_INFO(
"Dome is moving to home position...");
604 LOG_ERROR(
"Error: cannot move the dome to the home position");
612 LOG_ERROR(
"Calibration is not supported by standard basic Alpaca dome.");
620 return "/api/v1/dome/" + std::to_string(
static_cast<int>(
DeviceNumberNP[0].getValue())) + endpoint;
628 std::string form_data;
629 for (
auto & [key, value] : request.items())
631 if (!form_data.empty()) form_data +=
"&";
633 if (value.is_string())
635 form_data += key +
"=" + value.get<std::string>();
637 else if (value.is_number_integer())
639 form_data += key +
"=" + std::to_string(value.get<
int>());
641 else if (value.is_number_float())
643 form_data += key +
"=" + std::to_string(value.get<
double>());
645 else if (value.is_boolean())
647 form_data += key +
"=" + (value.get<
bool>() ?
"true" :
"false");
652 if (!form_data.empty()) form_data +=
"&";
653 form_data +=
"ClientID=" + std::to_string(getpid());
668 if (force || m_ConfigHost.empty())
674 if (force || m_ConfigPort.empty())
683 return ++m_ClientTransactionID;
688void AlpacaDome::updateStatus()
698 AlapacaDomeState
value;
699 if (alpacaGetDeviceState(value))
703 updateShutterStatus(value);
706 updateDomeStatus(value);
712void AlpacaDome::updateDomeStatus(AlapacaDomeState &state)
719 if (isHoming && state.atHome)
721 LOG_INFO(
"Dome has found home position.");
727 else if (isCalibrating)
729 LOG_INFO(
"Dome calibration complete.");
737 LOGF_INFO(
"Dome reached requested azimuth: %.3f Degrees", state.azimuth);
759void AlpacaDome::updateShutterStatus(AlapacaDomeState &state)
768 switch(state.shutterStatus)
781 LOG_INFO(
"Shutter is fully closed.");
819bool AlpacaDome::sendAlpacaGET(
const std::string &endpoint,
nlohmann::json &response)
823 LOG_ERROR(
"HTTP client not initialized.");
828 url +=
"?ClientID=" + std::to_string(getpid()) +
"&ClientTransactionID=" + std::to_string(
getTransactionId());
830 auto result = httpClient->Get(url.c_str());
834 LOGF_ERROR(
"HTTP GET failed for %s: %s", endpoint.c_str(),
839 if (result->status != 200)
841 LOGF_ERROR(
"HTTP GET %s returned status %d", endpoint.c_str(), result->status);
847 response = nlohmann::json::parse(result->body);
850 if (response.contains(
"ErrorNumber") && response[
"ErrorNumber"].get<
int>() != 0)
852 LOGF_ERROR(
"Alpaca error in %s: %d - %s", endpoint.c_str(),
853 response[
"ErrorNumber"].get<
int>(),
854 response[
"ErrorMessage"].get<std::string>().c_str());
862 LOGF_ERROR(
"JSON parse error for %s: %s", endpoint.c_str(), e.what());
873 LOG_ERROR(
"HTTP client not initialized.");
880 auto result = httpClient->Put(url, form_data,
"application/x-www-form-urlencoded");
884 LOGF_ERROR(
"HTTP PUT failed for %s: %s", endpoint.c_str(),
889 if (result->status != 200)
891 LOGF_ERROR(
"HTTP PUT %s returned status %d", endpoint.c_str(), result->status);
897 response = nlohmann::json::parse(result->body);
900 if (response.contains(
"ErrorNumber") && response[
"ErrorNumber"].get<
int>() != 0)
908 LOGF_ERROR(
"Alpaca error in %s: %d - %s", endpoint.c_str(),
909 response[
"ErrorNumber"].get<
int>(),
910 response[
"ErrorMessage"].get<std::string>().c_str());
919 LOGF_ERROR(
"JSON parse error for %s: %s", endpoint.c_str(), e.what());
926bool AlpacaDome::alpacaConnect()
931 if (m_InterfaceVersion >= 3)
933 if (!sendAlpacaPUT(
"/connect", {}, response))
936 bool connecting =
true;
937 int retryDelay = 100;
940 while (connecting && retryDelay < 4000)
942 if (!alpacaGetBool(
"/connecting", connecting))
946 std::this_thread::sleep_for(std::chrono::milliseconds(retryDelay));
953 if (!sendAlpacaPUT(
"/connected", {{
"Connected",
true}}, response))
956 bool connected =
false;
957 int retryDelay = 100;
960 while (!connected && retryDelay < 4000)
962 if (!alpacaGetBool(
"/connected", connected))
966 std::this_thread::sleep_for(std::chrono::milliseconds(retryDelay));
977bool AlpacaDome::alpacaDisconnect()
982 if (m_InterfaceVersion >= 3)
984 if (!sendAlpacaPUT(
"/disconnect", {}, response))
989 if (!sendAlpacaPUT(
"/connected", {{
"Connected",
false}}, response))
998bool AlpacaDome::alpacaGetBool(
const std::string &endpoint,
bool& value,
bool defaultValue)
1001 if (!sendAlpacaGET(endpoint, response))
1003 if (response.contains(
"Value") && response[
"Value"].is_boolean())
1004 value = response[
"Value"].get<
bool>();
1006 value = defaultValue;
1012bool AlpacaDome::alpacaGetInt(
const std::string &endpoint,
int &value,
int defaultValue)
1015 if (!sendAlpacaGET(endpoint, response))
1017 if (response.contains(
"Value") && response[
"Value"].is_number_integer())
1018 value = response[
"Value"].get<
int>();
1020 value = defaultValue;
1026bool AlpacaDome::alpacaGetDouble(
const std::string &endpoint,
double &value,
double defaultValue)
1029 if (!sendAlpacaGET(endpoint, response))
1031 if (response.contains(
"Value") && response[
"Value"].is_number_float())
1032 value = response[
"Value"].get<
double>();
1034 value = defaultValue;
1040bool AlpacaDome::alpacaGetString(
const std::string &endpoint, std::string &value,
const std::string& defaultValue)
1043 if (!sendAlpacaGET(endpoint, response))
1045 if (response.contains(
"Value") && response[
"Value"].is_string())
1046 value = response[
"Value"].get<std::string>();
1048 value = defaultValue;
1054bool AlpacaDome::alpacaGetDeviceState(AlapacaDomeState &value,
double defaultAz,
double defaultAlt)
1057 value.azimuth = defaultAz;
1058 value.altitude = defaultAlt;
1059 value.shutterStatus = -1;
1062 if (m_InterfaceVersion >= 3)
1066 if (!sendAlpacaGET(
"/devicestate", response))
1069 if (!response.contains(
"Value") || !response[
"Value"].is_array())
1071 LOG_ERROR(
"Error: an array of objects is expected in Value attribute of response");
1075 for (
auto &item : response[
"Value"])
1077 if (!item.contains(
"Name") || !item[
"Name"].is_string())
1079 LOG_ERROR(
"Error: missing string element Name in Value array item, as returned by /devicestate API method");
1082 if (!item.contains(
"Value"))
1084 LOG_ERROR(
"Error: missing element Value in Value array item, as returned by /devicestate API method");
1088 const std::string &
key = item[
"Name"].get<std::string>();
1089 if (key ==
"Altitude")
1090 value.altitude = item[
"Value"].get<
double>();
1091 else if (key ==
"AtHome")
1092 value.atHome = item[
"Value"].get<
bool>();
1093 else if (key ==
"AtPark")
1094 value.atPark = item[
"Value"].get<
bool>();
1095 else if (key ==
"Azimuth")
1096 value.azimuth = item[
"Value"].get<
double>();
1097 else if (key ==
"ShutterStatus")
1098 value.shutterStatus = item[
"Value"].get<
int>();
1099 else if (key ==
"Slewing")
1100 value.slewing = item[
"Value"].get<
bool>();
1106 alpacaGetBool(
"/slewing",
value.slewing,
false);
1108 alpacaGetBool(
"/athome",
value.atHome,
false);
1110 alpacaGetBool(
"/atpark",
value.atPark,
false);
1112 alpacaGetDouble(
"/azimuth",
value.azimuth, defaultAz);
1114 alpacaGetDouble(
"/altitude",
value.altitude, defaultAlt);
1116 alpacaGetInt(
"/shutterstatus",
value.shutterStatus, -1);
1124bool AlpacaDome::alpacaDomeAbortSlew()
1127 return sendAlpacaPUT(
"/abortslew", {}, response);
1132bool AlpacaDome::alpacaDomeCloseShutter()
1135 return sendAlpacaPUT(
"/closeshutter", {}, response);
1140bool AlpacaDome::alpacaDomeFindHome()
1143 return sendAlpacaPUT(
"/findhome", {}, response);
1148bool AlpacaDome::alpacaDomeOpenShutter()
1151 return sendAlpacaPUT(
"/openshutter", {}, response);
1156bool AlpacaDome::alpacaDomePark()
1159 return sendAlpacaPUT(
"/park", {}, response);
1164bool AlpacaDome::alpacaDomeSetPark()
1167 return sendAlpacaPUT(
"/setpark", {}, response);
1172bool AlpacaDome::alpacaDomeSlewToAltitude(
double alt)
1176 return sendAlpacaPUT(
"/slewtoaltitude", request, response);
1181bool AlpacaDome::alpacaDomeSlewToAzimuth(
double az)
1185 return sendAlpacaPUT(
"/slewtoazimuth", request, response);
1190bool AlpacaDome::alpacaDomeSyncToAzimuth(
double az)
1194 return sendAlpacaPUT(
"/synctoazimuth", request, response);
std::unique_ptr< AlpacaDome > alpaca_dome(new AlpacaDome())
INDI::PropertySwitch OperationSP
virtual bool saveConfigItems(FILE *fp) override
Save all dome parameters in the configuration file. This overridden method adds the HTTP connection i...
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool Abort() override
Abort all dome motion (spinning and shutter).
virtual IPState FindHome()
Request the dome for moving and finding its Home position. The method is asynchronous and the dome sh...
virtual bool SetCurrentPark() override
Set parking position of the dome with the current position.
virtual IPState Park() override
Park the dome.
virtual bool SetSpeed(double rpm) override
Set Dome speed. This does not initiate motion, it sets the speed for the next motion command....
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual IPState MoveAbs(double az) override
Move the Dome to an absolute azimuth.
virtual bool Disconnect() override
Disconnect from the remote dome device.
virtual IPState MoveRel(double azDiff) override
Move the Dome to an relative position.
virtual void ISGetProperties(const char *dev) override
Define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual uint32_t getTransactionId()
INDI::PropertyText ServerAddressTP
Additional properties.
virtual IPState Move(DomeDirection dir, DomeMotionCommand operation) override
Move the Dome in a particular direction.
INDI::PropertyNumber DeviceNumberNP
virtual IPState ControlShutter(ShutterOperation operation) override
Open or Close the shutter.
virtual bool updateProperties() override
Called whenever there is a change in the CONNECTION status of the driver. This will enable the driver...
INDI::PropertyText InfoTP
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual std::string buildAlpacaFormData(const nlohmann::json &request)
INDI::PropertyNumber ConnectionSettingsNP
virtual bool initProperties() override
Initialize properties state and value for Alpaca dome device.
virtual const char * getDefaultName() override
virtual void setDefaultServerAddress(const char *host, const char *port, bool force=false)
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual IPState Calibrate()
Request the dome for moving in order to calibrate itself. The calibration is an asynchronous process ...
virtual bool Sync(double az) override
Set the dome current azimuth with the supplied azimuth position.
virtual bool Connect() override
Connect to the remote dome device. For Alpaca drivers, this method tries to connect to the device usi...
virtual IPState UnPark() override
UnPark the dome. The action of the Unpark command is dome specific, but it may include opening the sh...
virtual bool SetDefaultPark() override
Set parking position of the dome with the current position.
virtual std::string getAlpacaURL(const std::string &endpoint)
INDI::PropertyText getText(const char *name) const
bool isDeviceNameMatch(const char *otherName) const
Check that the device name matches the argument.
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void SetParked(bool isparked)
SetParked Change the mount parking status. The data park file (stored in ~/.indi/ParkData....
void SetDomeCapability(uint32_t cap)
SetDomeCapability set the dome capabilities. All capabilities must be initialized.
void setDomeConnection(const uint8_t &value)
setDomeConnection Set Dome connection mode. Child class should call this in the constructor before Do...
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
ShutterOperation
Shutter operation command.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
bool isLocked()
isLocked, is the dome currently locked?
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
INDI::PropertyNumber DomeAbsPosNP
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Saves the Device Port and Dome Presets in the configuration file
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
void setShutterState(const ShutterState &value)
void setDomeState(const DomeState &value)
void SetParkDataType(DomeParkData type)
setParkDataType Sets the type of parking data stored in the park data file and presented to the user.
INDI::PropertySwitch DomeShutterSP
ShutterState getShutterState() const
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
DomeState getDomeState() const
INDI::PropertyNumber DomeSyncNP
friend class ::nlohmann::detail::exception
const char * CONNECTION_TAB
CONNECTION_TAB Where all device connection settings (serial, usb, ethernet) are defined and controlle...
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
const char * INFO_TAB
INFO_TAB Where all the properties for general information are located.
double range360(double r)
range360 Limits an angle to be between 0-360 degrees.
const char * IUFindOnSwitchName(ISState *states, char *names[], int n)
Returns the name of the first ON switch it finds in the supplied arguments.
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
std::string to_string(Error error)
basic_json<> json
default specialization