14#include <libnova/julian_day.h>
18namespace AlignmentSubsystem
21 pGetApproximateMountAlignment(&
MathPlugin::GetApproximateMountAlignment),
23 pSetApproximateMountAlignment(&
MathPlugin::SetApproximateMountAlignment),
24 pLoadedMathPlugin(&BuiltInPlugin), LoadedMathPluginHandle(nullptr)
26 memset(&AlignmentSubsystemCurrentMathPlugin, 0,
sizeof(
IText));
32 AlignmentSubsystemMathPlugins.reset(
new ISwitch[MathPluginDisplayNames.size() + 1]);
33 IUFillSwitch(AlignmentSubsystemMathPlugins.get(),
"INBUILT_MATH_PLUGIN",
"Inbuilt Math Plugin",
ISS_ON);
35 for (
int i = 0; i < (int)MathPluginDisplayNames.size(); i++)
37 IUFillSwitch(AlignmentSubsystemMathPlugins.get() + i + 1, MathPluginDisplayNames[i].c_str(),
38 MathPluginDisplayNames[i].c_str(),
ISS_OFF);
41 IUFillSwitchVector(&AlignmentSubsystemMathPluginsV, AlignmentSubsystemMathPlugins.get(),
42 MathPluginDisplayNames.size() + 1, ChildTelescope->
getDeviceName(),
46 int configPlugin = -1;
48 if (configPlugin >= 0 && configPlugin < AlignmentSubsystemMathPluginsV.
nsp)
51 AlignmentSubsystemMathPluginsV.
sp[configPlugin].
s =
ISS_ON;
52 HandlePluginLoading(ChildTelescope, 0, configPlugin);
61 for (
int i = 0; i < AlignmentSubsystemMathPluginsV.
nsp; i++)
63 if (!strcmp(AlignmentSubsystemMathPluginsV.
sp[i].
name, sp->name))
66 HandlePluginLoading(ChildTelescope, 0, i);
74 IUFillSwitch(&AlignmentSubsystemMathPluginInitialise,
"ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE",
"OK",
ISS_OFF);
75 IUFillSwitchVector(&AlignmentSubsystemMathPluginInitialiseV, &AlignmentSubsystemMathPluginInitialise, 1,
76 ChildTelescope->
getDeviceName(),
"ALIGNMENT_SUBSYSTEM_MATH_PLUGIN_INITIALISE",
80 IUFillSwitch(&AlignmentSubsystemActive,
"ALIGNMENT SUBSYSTEM ACTIVE",
"Alignment Subsystem Active",
ISS_OFF);
87 IUFillText(&AlignmentSubsystemCurrentMathPlugin,
"ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN",
"Current Math Plugin",
88 AlignmentSubsystemMathPlugins.get()[0].label);
89 IUFillTextVector(&AlignmentSubsystemCurrentMathPluginV, &AlignmentSubsystemCurrentMathPlugin, 1,
90 ChildTelescope->
getDeviceName(),
"ALIGNMENT_SUBSYSTEM_CURRENT_MATH_PLUGIN",
"Current Math Plugin",
98 if (strcmp(name, AlignmentSubsystemCurrentMathPluginV.
name) == 0)
100 AlignmentSubsystemCurrentMathPluginV.
s =
IPS_OK;
101 IUUpdateText(&AlignmentSubsystemCurrentMathPluginV, texts, names, n);
107 if (currentSwitch > 0 && currentSwitch <= (
int)MathPluginFiles.size())
109 const char *switchPath = MathPluginFiles[currentSwitch - 1].c_str();
110 if (strcmp(AlignmentSubsystemCurrentMathPlugin.text, switchPath) != 0)
111 IUSaveText(&AlignmentSubsystemCurrentMathPlugin, switchPath);
115 if (0 != strcmp(AlignmentSubsystemMathPlugins.get()[0].label, AlignmentSubsystemCurrentMathPlugin.text))
124 if (
nullptr != LoadedMathPluginHandle)
127 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
131 pLoadedMathPlugin =
nullptr;
132 if (0 == dlclose(LoadedMathPluginHandle))
134 LoadedMathPluginHandle =
nullptr;
139 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
146 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
151 if (
nullptr != (LoadedMathPluginHandle = dlopen(AlignmentSubsystemCurrentMathPlugin.text, RTLD_NOW)))
154 Create_t *
Create = (Create_t *)dlsym(LoadedMathPluginHandle,
"Create");
157 pLoadedMathPlugin =
Create();
164 for (i = 0; i < (int)MathPluginFiles.size(); i++)
166 if (0 == strcmp(AlignmentSubsystemCurrentMathPlugin.text, MathPluginFiles[i].c_str()))
169 if (i < (
int)MathPluginFiles.size())
172 (AlignmentSubsystemMathPlugins.get() + i + 1)->s =
ISS_ON;
174 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
179 "MathPluginManagement - cannot find %s in list of plugins", MathPluginFiles[i].c_str());
191 AlignmentSubsystemCurrentMathPlugin.text, dlerror());
198 if (
nullptr != LoadedMathPluginHandle)
201 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
205 pLoadedMathPlugin =
nullptr;
206 if (0 == dlclose(LoadedMathPluginHandle))
208 LoadedMathPluginHandle =
nullptr;
213 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
220 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
224 pLoadedMathPlugin = &BuiltInPlugin;
226 AlignmentSubsystemMathPlugins.get()->s =
ISS_ON;
228 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
234 char *names[],
int n)
238 if (strcmp(name, AlignmentSubsystemMathPluginsV.
name) == 0)
241 IUUpdateSwitch(&AlignmentSubsystemMathPluginsV, states, names, n);
242 AlignmentSubsystemMathPluginsV.
s =
IPS_OK;
244 HandlePluginLoading(pTelescope, CurrentPlugin, NewPlugin);
246 IDSetSwitch(&AlignmentSubsystemMathPluginsV,
nullptr);
248 else if (strcmp(name, AlignmentSubsystemMathPluginInitialiseV.
name) == 0)
250 AlignmentSubsystemMathPluginInitialiseV.
s =
IPS_OK;
253 IDSetSwitch(&AlignmentSubsystemMathPluginInitialiseV,
nullptr);
258 else if (strcmp(name, AlignmentSubsystemActiveV.
name) == 0)
260 AlignmentSubsystemActiveV.
s =
IPS_OK;
261 if (0 ==
IUUpdateSwitch(&AlignmentSubsystemActiveV, states, names, n))
267void MathPluginManagement::HandlePluginLoading(
Telescope *pTelescope,
int CurrentPlugin,
int NewPlugin)
269 if (NewPlugin != CurrentPlugin)
274 if (0 != CurrentPlugin)
277 Destroy_t *
Destroy = (Destroy_t *)dlsym(LoadedMathPluginHandle,
"Destroy");
281 pLoadedMathPlugin =
nullptr;
282 if (0 == dlclose(LoadedMathPluginHandle))
284 LoadedMathPluginHandle =
nullptr;
289 "MathPluginManagement - dlclose failed on loaded plugin - %s", dlerror());
296 "MathPluginManagement - cannot get Destroy function - %s", dlerror());
303 std::string PluginPath(MathPluginFiles[NewPlugin - 1]);
304 if (
nullptr != (LoadedMathPluginHandle = dlopen(PluginPath.c_str(), RTLD_NOW)))
306 typedef MathPlugin *Create_t();
307 Create_t *
Create = (Create_t *)dlsym(LoadedMathPluginHandle,
"Create");
310 pLoadedMathPlugin =
Create();
313 IUSaveText(&AlignmentSubsystemCurrentMathPlugin, PluginPath.c_str());
325 PluginPath.c_str(), dlerror());
332 pLoadedMathPlugin = &BuiltInPlugin;
340 AlignmentSubsystemActiveV.
s =
IPS_OK;
358 if (Position.latitude >= 0)
375 return (pLoadedMathPlugin->*pGetApproximateMountAlignment)();
385 (pLoadedMathPlugin->*pSetApproximateMountAlignment)(ApproximateAlignment);
392 if (AlignmentSubsystemActive.s ==
ISS_ON)
394 ApparentTelescopeDirectionVector);
403 if (AlignmentSubsystemActive.s ==
ISS_ON)
405 Declination, JulianDate);
415 ln_get_julian_from_sys() + JulianOffset,
416 ApparentTelescopeDirectionVector);
424 ln_get_julian_from_sys() + JulianOffset);
427void MathPluginManagement::EnumeratePlugins()
429 MathPluginFiles.clear();
430 MathPluginDisplayNames.clear();
431#ifndef OSX_EMBEDED_MODE
436 char MATH_PLUGINS_DIRECTORY[2048];
439 const char *pluginsDirectory = getenv(
"INDI_MATH_PLUGINS_DIRECTORY");
440 if (pluginsDirectory && pluginsDirectory[0] !=
'\0')
441 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1,
"%s", pluginsDirectory);
444#if defined(__APPLE__)
445 const char *indiprefix = getenv(
"INDIPREFIX");
447 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1,
"%s/Contents/Resources/MathPlugins", indiprefix);
449 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
451 snprintf(MATH_PLUGINS_DIRECTORY, 2048 - 1, INDI_MATH_PLUGINS_DIRECTORY);
455 dp = opendir(MATH_PLUGINS_DIRECTORY);
456 strncat(MATH_PLUGINS_DIRECTORY,
"/", 2);
462 std::string PluginPath(MATH_PLUGINS_DIRECTORY);
468 if (0 == strcmp(de->d_name,
"."))
470 if (0 == strcmp(de->d_name,
".."))
474 PluginPath.append(de->d_name);
475 Handle = dlopen(PluginPath.c_str(), RTLD_NOW);
476 if (
nullptr == Handle)
478 IDLog(
"EnumeratePlugins - cannot load plugin %s error %s\n", PluginPath.c_str(), dlerror());
483 typedef const char *GetDisplayName_t();
484 GetDisplayName_t *GetDisplayNamePtr = (GetDisplayName_t *)dlsym(Handle,
"GetDisplayName");
485 if (
nullptr == GetDisplayNamePtr)
487 IDLog(
"EnumeratePlugins - cannot get plugin %s DisplayName error %s\n", PluginPath.c_str(), dlerror());
490 IDLog(
"EnumeratePlugins - found plugin %s\n", GetDisplayNamePtr());
492 MathPluginDisplayNames.push_back(GetDisplayNamePtr());
493 MathPluginFiles.push_back(PluginPath);
500 IDLog(
"EnumeratePlugins - Failed to open %s error %s\n", MATH_PLUGINS_DIRECTORY, strerror(
errno));
This class provides the driver side API to the in memory alignment database.
bool GetDatabaseReferencePosition(IGeographicCoordinates &Position)
Get the database reference position.
void ProcessSwitchProperties(Telescope *pTelescope, const char *name, ISState *states, char *names[], int n)
Call this function from within the ISNewSwitch processing path. The function will handle any math plu...
void InitProperties(Telescope *pTelescope)
Initialize alignment math plugin properties. It is recommended to call this function within initPrope...
void SaveConfigProperties(FILE *fp)
Call this function to save persistent math plugin properties. This function should be called from wit...
MathPluginManagement()
Default constructor.
bool TransformTelescopeToCelestialJD(const TelescopeDirectionVector &ApparentTelescopeDirectionVector, double &RightAscension, double &Declination, double JulianDate)
TransformTelescopeToCelestialJD Transforms Mount Coords to Celestial (Sky) Coordinates.
bool TransformCelestialToTelescopeJD(double RightAscension, double Declination, double JulianDate, TelescopeDirectionVector &ApparentTelescopeDirectionVector)
TransformCelestialToTelescopeJD Transforms Celestial (Sky) Coords to Mount Coordinates.
void SetApproximateMountAlignment(MountAlignment_t ApproximateAlignment)
Set the approximate alignment of the mount.
void ProcessTextProperties(Telescope *pTelescope, const char *name, char *texts[], char *names[], int n)
Call this function from within the ISNewText processing path. The function will handle any math plugi...
bool TransformCelestialToTelescope(const double RightAscension, const double Declination, double JulianOffset, TelescopeDirectionVector &ApparentTelescopeDirectionVector)
TransformCelestialToTelescope Compat shim - resolves JD and delegates to TransformCelestialToTelescop...
bool Initialise(InMemoryDatabase *pInMemoryDatabase)
Initialise or re-initialise the math plugin. Re-reading the in memory database as necessary.
MountAlignment_t GetApproximateMountAlignment()
Get the approximate alignment of the mount.
enum INDI::AlignmentSubsystem::MathPluginManagement::MountType MountType_t
bool TransformTelescopeToCelestial(const TelescopeDirectionVector &ApparentTelescopeDirectionVector, double &RightAscension, double &Declination, double JulianOffset=0)
TransformTelescopeToCelestial Compat shim - resolves JD and delegates to TransformTelescopeToCelestia...
void SetAlignmentSubsystemActive(bool enable)
SetAlignmentSubsystemActive Enable or Disable alignment subsystem.
void SetApproximateMountAlignmentFromMountType(MountType_t Type)
Call this function to set the ApproximateMountAlignment property of the current Math Plugin....
Provides alignment subsystem functions to INDI alignment math plugins.
InMemoryDatabase * pInMemoryDatabase
virtual bool TransformTelescopeToCelestialJD(const TelescopeDirectionVector &ApparentTelescopeDirectionVector, double &RightAscension, double &Declination, double JulianDate)
Get the true celestial coordinates for the supplied telescope pointing direction.
virtual bool TransformCelestialToTelescopeJD(double RightAscension, double Declination, double JulianDate, TelescopeDirectionVector &ApparentTelescopeDirectionVector)
Get the alignment corrected telescope pointing direction for the supplied celestial coordinates.
const char * getDeviceName() const
void registerProperty(const INDI::Property &property)
Register the property to be able to observe and update.
void IDLog(const char *fmt,...)
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
ISwitch * IUFindSwitch(const ISwitchVectorProperty *svp, const char *name)
Find an ISwitch member in a vector switch property.
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
IUGetConfigOnSwitchIndex Opens configuration file and reads single switch property to find ON switch ...
#define DEBUGFDEVICE(device, priority, msg,...)
void Destroy(DummyMathPlugin *pPlugin)
DummyMathPlugin * Create()
enum INDI::AlignmentSubsystem::MountAlignment MountAlignment_t
Namespace to encapsulate INDI client, drivers, and mediator classes.
Holds a normalised direction vector (direction cosines)
Holds the connection type.