13static std::unique_ptr<PegasusSPB> spb(
new PegasusSPB());
24 lastSensorData.reserve(
PA_N);
25 lastConsumptionData.reserve(
PS_N);
26 lastMetricsData.reserve(
PC_N);
30double PegasusSPB::map(
double value,
double from1,
double to1,
double from2,
double to2)
33 value = std::max(from1, std::min(to1, value));
36 double mappedValue = from2 + (value - from1) * (to2 - from2) / (to1 - from1);
51 serialConnection->registerHandshake([&]()
63 PowerDewSwitchASP[0].fill(
"DEW",
"Dew",
ISS_OFF);
64 PowerDewSwitchASP[1].fill(
"POWER",
"Power",
ISS_ON);
68 PowerDewSwitchBSP[0].fill(
"DEW",
"Dew",
ISS_OFF);
69 PowerDewSwitchBSP[1].fill(
"POWER",
"Power",
ISS_ON);
73 DewAggressNP[0].fill(
"DEW_AGGRESS",
"Agg Level",
"%.2f", 0, 100, 1, 0);
80 std::string name (1,
static_cast<char>(
'A' + i));
114 AutoDewSP[0].setLabel(
"Auto Dew Control");
118 PowerStatisticsNP[STATS_AVG_AMPS].fill(
"STATS_AVG_AMPS",
"Average Current (A)",
"%4.2f", 0, 999, 100, 0);
119 PowerStatisticsNP[STATS_AMP_HOURS].fill(
"STATS_AMP_HOURS",
"Amp hours (Ah)",
"%4.2f", 0, 999, 100, 0);
120 PowerStatisticsNP[STATS_WATT_HOURS].fill(
"STATS_WATT_HOURS",
"Watt hours (Wh)",
"%4.2f", 0, 999, 100, 0);
121 PowerStatisticsNP[STATS_TOTAL_CURRENT].fill(
"STATS_TOTAL_CURRENT",
"Total current (A)",
"%4.2f", 0, 999, 100, 0);
125 FirmwareTP[FIRMWARE_VERSION].fill(
"VERSION",
"Version",
"NA");
126 FirmwareTP[FIRMWARE_UPTIME].fill(
"UPTIME",
"Uptime (h)",
"NA");
127 FirmwareTP.fill(
getDeviceName(),
"FIRMWARE_INFO",
"Firmware", FIRMWARE_TAB,
IP_RO, 60,
131 addParameter(
"WEATHER_TEMPERATURE",
"Temperature (C)", -15, 35, 15);
132 addParameter(
"WEATHER_HUMIDITY",
"Humidity %", 0, 100, 15);
133 addParameter(
"WEATHER_DEWPOINT",
"Dew Point (C)", 0, 100, 15);
137 HumidityOffsetNP[0].fill(
"HUM_OFFSET",
"Level",
"%.0f", -50, 50, 1, 0);
140 TemperatureOffsetNP[0].fill(
"TEMP_OFFSET",
"Level",
"%.0f", -40, 40, 1, 0);
154 int aggressiveness = 0;
155 if (getDewAggressiveness(aggressiveness))
157 DewAggressNP[0].setValue(aggressiveness);
158 DewAggressNP.setState(
IPS_OK);
162 DewAggressNP.apply();
165 updatePortMode(PowerDewSwitchASP, getPowerDewPortMode(1));
167 updatePortMode(PowerDewSwitchBSP, getPowerDewPortMode(2));
171 int temperatureOffset = 0, humidityOffset = 0;
172 const bool offsetsRead = getOffsets(temperatureOffset, humidityOffset);
173 HumidityOffsetNP[0].setValue(humidityOffset);
174 TemperatureOffsetNP[0].setValue(temperatureOffset);
177 HumidityOffsetNP.apply();
178 TemperatureOffsetNP.apply();
188 setupComplete =
true;
201 setupComplete =
false;
218 return "Pegasus SPB";
222bool PegasusSPB::Handshake()
224 int tty_rc = 0, nbytes_written = 0, nbytes_read = 0;
225 char command[PEGASUS_LEN] = {0}, response[PEGASUS_LEN] = {0};
227 PortFD = serialConnection->getPortFD();
231 tcflush(PortFD, TCIOFLUSH);
232 strncpy(command,
"P#\n", PEGASUS_LEN);
237 LOGF_ERROR(
"Serial write error: %s", errorMessage);
247 tcflush(PortFD, TCIOFLUSH);
250 tty_rc =
tty_nread_section(PortFD, response, PEGASUS_LEN, stopChar, 1, &nbytes_read);
257 LOGF_ERROR(
"Serial read error: %s", errorMessage);
262 tcflush(PortFD, TCIOFLUSH);
263 response[nbytes_read - 1] =
'\0';
266 setupComplete =
false;
268 lastSensorData.clear();
269 lastConsumptionData.clear();
270 lastMetricsData.clear();
271 m_AutoDewShown =
false;
273 return (strstr(response,
"SPB") !=
nullptr);
288 getConsumptionData();
291 catch (
const std::exception &e)
293 LOGF_WARN(
"Failed to parse the device status: %s", e.what());
308 if(PowerDewSwitchASP.isNameMatch(name))
312 PowerDewSwitchASP.update(states, names,
n);
314 PowerDewSwitchASP.setState(setPowerDewPortMode(1, newState) ?
IPS_OK :
IPS_ALERT);
315 PowerDewSwitchASP.apply();
319 if(PowerDewSwitchBSP.isNameMatch(name))
323 PowerDewSwitchBSP.update(states, names,
n);
325 PowerDewSwitchBSP.setState(setPowerDewPortMode(2, newState) ?
IPS_OK :
IPS_ALERT);
326 PowerDewSwitchBSP.apply();
331 return DefaultDevice::ISNewSwitch(dev, name, states, names,
n);
343 if(DewAggressNP.isNameMatch(name))
348 DewAggressNP.update(values, names,
n);
352 if(!setDewAggressiveness(
static_cast<uint8_t
>(values[0])))
357 DewAggressNP.setState(result);
358 DewAggressNP.apply();
362 if(HumidityOffsetNP.isNameMatch(name))
367 HumidityOffsetNP.update(values, names,
n);
371 if(!setHumdityOffset(
static_cast<int>(values[0])))
376 HumidityOffsetNP.setState(result);
377 HumidityOffsetNP.apply();
381 if(TemperatureOffsetNP.isNameMatch(name))
386 TemperatureOffsetNP.update(values, names,
n);
390 if(!setTemperatureOffset(
static_cast<int>(values[0])))
395 TemperatureOffsetNP.setState(result);
396 TemperatureOffsetNP.apply();
414bool PegasusSPB::setDewPortPower(
int portNumber,
double power)
416 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
417 snprintf(
cmd, PEGASUS_LEN,
"P%d:%ld", portNumber + 2, std::lround(std::clamp(power, 0.0, 100.0) / 100.0 * 255.0));
418 if(sendCommand(
cmd, res))
422 LOGF_ERROR(
"Error on set dew port power [Cmd=%s Res=%s]",
cmd, res);
428 LOGF_ERROR(
"Error on set dew port power [Port=%d Power=%d]", portNumber, power);
434int PegasusSPB::getPowerDewPortMode(
int portNumber)
436 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
437 snprintf(
cmd, PEGASUS_LEN,
"D%d:99", portNumber + 2);
438 if(sendCommand(
cmd, res))
440 std::vector<std::string> result = split(res,
":");
441 if (result.size() == 2 && (result[1] ==
"0" || result[1] ==
"1"))
442 return std::stoi(result[1]);
444 LOGF_ERROR(
"Error on get power-dew port mode [Port=%d Res=%s]", portNumber, res);
448bool PegasusSPB::setPowerDewPortMode(
int portNumber,
int mode)
450 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
451 snprintf(
cmd, PEGASUS_LEN,
"D%d:%d", portNumber + 2, mode);
452 if(sendCommand(
cmd, res))
456 LOGF_ERROR(
"Error on set power-dew port mode [Cmd=%s Res=%s]",
cmd, res);
462 LOGF_ERROR(
"Error on set power-dew port mode [Port=%d Mode=%d]", portNumber, mode);
468bool PegasusSPB::setDewAutoState(
bool enabled)
470 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
471 snprintf(
cmd, PEGASUS_LEN,
"PD:%d", enabled ? 1 : 0);
472 if(sendCommand(
cmd, res))
476 LOGF_ERROR(
"Error on set dew auto state [Cmd=%s Res=%s]",
cmd, res);
482 LOG_ERROR(
"Error on set set dew auto state");
488bool PegasusSPB::setDewAggressiveness(uint8_t level)
490 int mappedLevel = std::lround(map(level, 0, 100, 10, 254));
491 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
492 snprintf(
cmd, PEGASUS_LEN,
"PD:%d", mappedLevel);
493 if(sendCommand(
cmd, res))
497 LOGF_ERROR(
"Error on set dew aggresiveness [Cmd=%s Res=%s]",
cmd, res);
503 LOG_ERROR(
"Error on set dew aggresiveness");
509bool PegasusSPB::getDewAggressiveness(
int &level)
511 char res[PEGASUS_LEN] = {0};
512 if (sendCommand(
"DA", res))
514 std::vector<std::string> result = split(res,
":");
517 if (result.size() == 2)
519 level = std::lround(map(std::stoi(result[1]), 10, 254, 0, 100));
523 catch (
const std::exception &) {}
525 LOGF_ERROR(
"Error on get dew aggressiveness [Res=%s]", res);
529bool PegasusSPB::setHumdityOffset(
int level)
531 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
532 snprintf(
cmd, PEGASUS_LEN,
"CH:%d", level);
533 if(sendCommand(
cmd, res))
537 LOGF_ERROR(
"Error on set humidity offset [Cmd=%s Res=%s]",
cmd, res);
543 LOG_ERROR(
"Error on set humidity offset");
551 char res[PEGASUS_LEN] = {0};
552 if (sendCommand(
"CR", res))
554 std::vector<std::string> result = split(res,
":");
557 if (result.size() == 3)
560 temperature = std::lround(std::stoi(result[1]) / 100.0);
565 catch (
const std::exception &) {}
567 LOGF_ERROR(
"Error on get sensor offsets [Res=%s]", res);
571bool PegasusSPB::setTemperatureOffset(
int level)
573 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
574 snprintf(
cmd, PEGASUS_LEN,
"CT:%d", (level * 100));
575 if(sendCommand(
cmd, res))
579 LOGF_ERROR(
"Error on set temperature offset [Cmd=%s Res=%s]",
cmd, res);
585 LOG_ERROR(
"Error on set temperature offset");
591bool PegasusSPB::getFirmware()
593 char res[PEGASUS_LEN] = {0};
594 if (sendCommand(
"PV", res))
596 std::vector<std::string> result = split(res,
":");
597 if (result.size() > 1)
599 LOGF_INFO(
"Detected firmware %s", result[1].c_str());
600 FirmwareTP[FIRMWARE_VERSION].setText(result[1]);
601 FirmwareTP.setState(
IPS_OK);
607 LOGF_WARN(
"Unexpected firmware response: %s", res);
613 LOGF_WARN(
"Retreiving firmware failed: %s", res);
621bool PegasusSPB::setFixedPowerPortState(
int portNumber,
bool enabled)
623 char cmd[PEGASUS_LEN] = {0},
res[PEGASUS_LEN] = {0};
626 snprintf(
cmd, PEGASUS_LEN,
"P%d:%d", portNumber + 1, enabled ? 1 : 0);
627 if (sendCommand(
cmd, res) && !strcmp(
cmd, res))
630 LOGF_ERROR(
"Error on set power port [Cmd=%s Res=%s]",
cmd, res);
634bool PegasusSPB::getSensorData()
636 char res[PEGASUS_LEN] = {0};
637 if (sendCommand(
"PA", res))
639 std::vector<std::string> result = split(res,
":");
640 if (result.size() <
PA_N)
642 LOG_WARN(
"Received wrong number of detailed sensor data. Retrying...");
646 if (result == lastSensorData)
688 const bool autodew = std::stoi(result[
PA_AUTO_DEW]);
692 if (autodew && !m_AutoDewShown)
695 const double reported = std::stod(result[i == 0 ?
PA_DEW_1 :
PA_DEW_2]) / 255.0 * 100.0;
696 if (autodew || reported > 0)
698 else if (m_AutoDewShown)
703 || lastSensorData[
PA_DEW_2] != result[
PA_DEW_2] || autodew != m_AutoDewShown)
705 m_AutoDewShown = autodew;
715 bool changed =
false;
718 const bool on = autodew || std::stod(result[i == 0 ?
PA_DEW_1 :
PA_DEW_2]) > 0;
723 LOGF_INFO(
"Auto dew on, enabling channel %s", (i == 0 ?
"A" :
"B"));
734 if (lastSensorData.size() <
PA_N ||
745 lastSensorData = result;
752bool PegasusSPB::getConsumptionData()
754 char res[PEGASUS_LEN] = {0};
755 if (sendCommand(
"PS", res))
757 std::vector<std::string> result = split(res,
":");
758 if (result.size() <
PS_N)
760 LOG_WARN(
"Received wrong number of detailed consumption data. Retrying...");
764 if (result == lastConsumptionData)
768 PowerStatisticsNP[STATS_AVG_AMPS].setValue(std::stod(result[
PS_AVG_AMPS]));
769 PowerStatisticsNP[STATS_AMP_HOURS].setValue(std::stod(result[
PS_AMP_HOURS]));
770 PowerStatisticsNP[STATS_WATT_HOURS].setValue(std::stod(result[
PS_WATT_HOURS]));
771 PowerStatisticsNP.setState(
IPS_OK);
772 if (lastConsumptionData.size() <
PS_N ||
776 PowerStatisticsNP.apply();
778 lastConsumptionData = result;
786bool PegasusSPB::getMetricsData()
788 char res[PEGASUS_LEN] = {0};
789 if (sendCommand(
"PC", res))
791 std::vector<std::string> result = split(res,
":");
792 if (result.size() <
PC_N)
794 LOG_WARN(
"Received wrong number of detailed metrics data. Retrying...");
798 if (result == lastMetricsData)
802 PowerStatisticsNP[STATS_TOTAL_CURRENT].setValue(std::max(0.0, std::stod(result[
PC_TOTAL_CURRENT])));
804 PowerStatisticsNP.apply();
823 if (lastMetricsData.size() <
PC_N ||
829 std::chrono::milliseconds uptime(std::stol(result[
PC_UPTIME]));
830 using dhours = std::chrono::duration<double, std::ratio<3600 >>;
831 std::stringstream ss;
832 ss << std::fixed << std::setprecision(3) << dhours(uptime).count();
833 FirmwareTP[FIRMWARE_UPTIME].setText(ss.str().c_str());
836 lastMetricsData = result;
845bool PegasusSPB::sendCommand(
const char *
cmd,
char * res)
847 int nbytes_read = 0, nbytes_written = 0, tty_rc = 0;
848 char command[PEGASUS_LEN] = {0};
851 for (
int i = 0; i < 2; i++)
853 tcflush(PortFD, TCIOFLUSH);
854 snprintf(command, PEGASUS_LEN,
"%s\n",
cmd);
860 tcflush(PortFD, TCIOFLUSH);
868 tcflush(PortFD, TCIOFLUSH);
869 res[nbytes_read - 1] =
'\0';
884std::vector<std::string> PegasusSPB::split(
const std::string &input,
const std::string ®ex)
887 std::regex re(regex);
888 std::sregex_token_iterator
889 first{input.begin(), input.end(), re, -1},
891 return {first, last};
910 return setFixedPowerPortState(port, enabled);
916 int originalPortNumber = port + 1;
922 return setDewPortPower(originalPortNumber, enabled ? dutyCycle : 0);
945 return setDewAutoState(enabled);
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
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....
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
bool isConfigLoading() const
isConfigLoading Check if driver configuration is currently in the process of getting loaded.
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.
virtual bool initProperties()
Initialize properties initial state and value. The child class must implement this function.
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.
INDI::PropertySwitch DewChannelsSP
@ POWER_HAS_VOLTAGE_SENSOR
@ POWER_HAS_OVERALL_CURRENT
@ POWER_HAS_PER_PORT_CURRENT
bool saveConfigItems(FILE *fp)
saveConfigItems Save power properties in config file
INDI::PropertySwitch AutoDewSP
void SetCapability(uint32_t cap)
SetPowerCapability sets the Power capabilities. All capabilities must be initialized.
INDI::PropertyNumber PowerSensorsNP
INDI::PropertyNumber PowerChannelCurrentNP
INDI::PropertyNumber DewChannelCurrentNP
bool processNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process Power number properties.
void initProperties(const char *groupName, size_t nPowerPorts=0, size_t nDewPorts=0, size_t nVariablePorts=0, size_t nAutoDewPorts=0, size_t nUSBPorts=0)
Initialize Power properties. It is recommended to call this function within initProperties() of your ...
INDI::PropertyText PowerChannelLabelsTP
bool processText(const char *dev, const char *name, char *texts[], char *names[], int n)
Process Power text properties.
bool processSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process Power switch properties.
INDI::PropertySwitch PowerChannelsSP
INDI::PropertyNumber DewChannelDutyCycleNP
bool updateProperties()
updateProperties Define or Delete Power properties based on the connection status of the base device
INDI::PropertyText DewChannelLabelsTP
void addParameter(std::string name, std::string label, double numMinOk, double numMaxOk, double percWarning, bool flipWarning=false)
Add a physical weather measurable parameter to the weather driver.
bool setCriticalParameter(std::string name)
setCriticalParameter Set parameter that is considered critical to the operation of the observatory....
bool syncCriticalParameters()
Synchronize and update the state of all critical parameters.
void setParameterValue(std::string name, double value)
setParameterValue Update weather parameter value
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save parameters ranges in the config file.
INDI::PropertyNumber ParametersNP
void initProperties(const char *statusGroup, const char *paramsGroup)
Initialize focuser properties. It is recommended to call this function within initProperties() of you...
bool updateProperties()
updateProperties Define or Delete Rotator properties based on the connection status of the base devic...
INDI::PropertyLight critialParametersLP
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 SetAutoDewEnabled(size_t port, bool enabled) override
SetAutoDewEnabled Enable/disable automatic dew control.
virtual bool SetPowerPort(size_t port, bool enabled) override
Power Interface Implementations.
virtual bool SetLEDEnabled(bool enabled) override
SetLEDEnabled Enable/disable power LEDs.
virtual bool SetVariablePort(size_t port, bool enabled, double voltage) override
SetVariablePort Set variable voltage port.
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 ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
virtual bool SetDewPort(size_t port, bool enabled, double dutyCycle) override
SetDewPort Set DEW/Dew port duty cycle.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool SetUSBPort(size_t port, bool enabled) override
SetUSBPort Set USB port on/off.
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Implementations for common driver routines.
*brief user defined string literal for JSON values *sa std::size_t n
#define LOGF_INFO(fmt,...)
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
INDI::WeatherInterface WI
Namespace to encapsulate INDI client, drivers, and mediator classes.