27#ifdef _USE_SYSTEM_JSONLIB
28#include <nlohmann/json.hpp>
47 InputInterface::initProperties(
"Main Control", DIGITAL_INPUTS, ANALOG_INPUTS,
"Input",
"Analog");
50 OutputInterface::initProperties(
"Main Control", DIGITAL_OUTPUTS,
"Output");
58 tcpConnection->setDefaultHost(
"192.168.1.100");
59 tcpConnection->setDefaultPort(80);
60 tcpConnection->registerHandshake([&]()
68 APIKeyTP[0].fill(
"API_KEY",
"API Key",
"");
73 ModelVersionTP[0].fill(
"VERSION",
"Version",
"");
84 InputInterface::updateProperties();
85 OutputInterface::updateProperties();
109 if (strlen(APIKeyTP[0].
getText()) == 0)
118 std::string endpoint =
"/api/xdevices.json";
119 endpoint +=
"?key=" + std::string(APIKeyTP[0].
getText());
120 endpoint +=
"&Get=D";
121 auto result = cli.
Get(endpoint);
124 LOG_ERROR(
"Failed to connect to device");
130 auto j = json::parse(result->body);
131 if (j.contains(
"product"))
133 std::string product = j[
"product"].get<std::string>();
135 size_t pos = product.find(
"_");
136 if (pos != std::string::npos)
138 ModelVersionTP[0].setText(product.substr(pos + 1).c_str());
139 ModelVersionTP.setState(
IPS_OK);
168 std::string endpoint =
"/api/xdevices.json";
169 endpoint +=
"?key=" + std::string(APIKeyTP[0].
getText());
170 endpoint +=
"&Get=D";
171 auto result = cli.Get(endpoint);
174 LOG_ERROR(
"Failed to get digital inputs");
180 auto j = json::parse(result->body);
183 auto inputs = j.get<std::vector<int>>();
184 for (
size_t i = 0; i < DIGITAL_INPUTS && i < inputs.size(); i++)
207 httplib::Client cli(tcpConnection->host(), tcpConnection->port());
209 std::string endpoint =
"/api/xdevices.json";
210 endpoint +=
"?key=" + std::string(APIKeyTP[0].
getText());
211 endpoint +=
"&Get=A";
212 auto result = cli.Get(endpoint);
215 LOG_ERROR(
"Failed to get analog inputs");
221 auto j = json::parse(result->body);
224 for (
int i = 0; i < ANALOG_INPUTS; i++)
226 std::string
key =
"AN" + std::to_string(i + 1);
229 double value = j[
key].get<
double>();
252 std::string endpoint =
"/api/xdevices.json";
253 endpoint +=
"?key=" + std::string(APIKeyTP[0].
getText());
254 endpoint +=
"&Get=R";
255 auto result = cli.
Get(endpoint);
258 LOG_ERROR(
"Failed to get digital outputs");
264 auto j = json::parse(result->body);
267 auto outputs = j.get<std::vector<int>>();
268 for (
size_t i = 0; i < DIGITAL_OUTPUTS && i < outputs.size(); i++)
294 std::string endpoint =
"/api/xdevices.json";
295 endpoint +=
"?key=" + std::string(APIKeyTP[0].
getText());
296 endpoint +=
"&" + std::string(command ==
OutputState::On ?
"SetR=" :
"ClearR=") + std::to_string(index + 1);
298 auto result = cli.
Get(endpoint);
301 LOGF_ERROR(
"Failed to set output %d", index + 1);
305 return result->status == 200;
312 if (APIKeyTP.isNameMatch(name))
314 APIKeyTP.update(texts, names,
n);
315 APIKeyTP.setState(
IPS_OK);
322 if (InputInterface::processText(dev, name, texts, names,
n))
324 if (OutputInterface::processText(dev, name, texts, names,
n))
332 if (OutputInterface::processSwitch(dev, name, states, names,
n))
340 if (OutputInterface::processNumber(dev, name, values, names,
n))
349 InputInterface::saveConfigItems(fp);
350 OutputInterface::saveConfigItems(fp);
The TCP class manages connection with devices over the network via TCP/IP. Upon successful connection...
virtual const char * host() const
virtual uint32_t port() const
const char * getDeviceName() const
INDI::PropertyText getText(const char *name) const
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process the client newSwitch command.
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
void setDefaultPollingPeriod(uint32_t msec)
setDefaultPollingPeriod Change the default polling period to call TimerHit() function in the driver.
void setDriverInterface(uint32_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
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)
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
friend class OutputInterface
virtual bool initProperties()
Initialize properties initial state and value. The child class must implement this function.
friend class InputInterface
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process the client newSwitch command.
std::vector< INDI::PropertySwitch > DigitalOutputsSP
virtual bool CommandOutput(uint32_t index, OutputState command) override
Send command to output.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool UpdateDigitalOutputs() override
Update all digital outputs.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
const char * getDefaultName() override
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
Result Get(const std::string &path, DownloadProgress progress=nullptr)
{ using exception=detail::exception detail::parse_error parse_error
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
basic_json<> json
default specialization