Skip to content

Commit

Permalink
[P108] Implement Discovery support (for user-selected values)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonhuisman committed Jan 17, 2025
1 parent 865852a commit fa3afec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/_P108_DDS238.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// with most code copied from plugin 085: _P085_AcuDC243.ino

/** Changelog:
* 2025-01-17 tonhuisman: Implement support for MQTT AutoDiscovery (partially)
* 2025-01-12 tonhuisman: Add support for MQTT AutoDiscovery (not supported yet for DDS238)
*/

Expand Down Expand Up @@ -81,8 +82,11 @@ boolean Plugin_108(uint8_t function, struct EventStruct *event, String& string)
# if FEATURE_MQTT_DISCOVER
case PLUGIN_GET_DISCOVERY_VTYPES:
{
event->Par1 = static_cast<int>(Sensor_VType::SENSOR_TYPE_NONE); // Not yet supported
success = true;
for (uint8_t i = 0; i < event->Par5; ++i) {
const uint8_t choice = PCONFIG(i + P108_QUERY1_CONFIG_POS);
event->ParN[i] = static_cast<int>(Plugin_108_getQueryVType(choice));
}
success = true;
break;
}
# endif // if FEATURE_MQTT_DISCOVER
Expand Down
24 changes: 23 additions & 1 deletion src/src/PluginStructs/P108_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ bool P108_data_struct::init(ESPEasySerialPort port, const int16_t serial_rx, con
return modbus.init(port, serial_rx, serial_tx, baudrate, modbusAddress, dere_pin);
}


const __FlashStringHelper* Plugin_108_valuename(uint8_t value_nr, bool displayString) {
switch (value_nr) {
case P108_QUERY_V: return displayString ? F("Voltage (V)") : F("V");
Expand All @@ -32,6 +31,29 @@ const __FlashStringHelper* Plugin_108_valuename(uint8_t value_nr, bool displaySt
return F("");
}

# if FEATURE_MQTT_DISCOVER
Sensor_VType Plugin_108_getQueryVType(uint8_t value_nr) {
const Sensor_VType vtypes[] = {
Sensor_VType::SENSOR_TYPE_VOLTAGE_ONLY,
Sensor_VType::SENSOR_TYPE_CURRENT_ONLY,
Sensor_VType::SENSOR_TYPE_POWER_USG_ONLY,
Sensor_VType::SENSOR_TYPE_REACTIVE_POWER_ONLY,
Sensor_VType::SENSOR_TYPE_POWER_FACT_ONLY,
Sensor_VType::SENSOR_TYPE_NONE, // FIXME To implement Frequency and Energy options
Sensor_VType::SENSOR_TYPE_NONE,
Sensor_VType::SENSOR_TYPE_NONE,
Sensor_VType::SENSOR_TYPE_NONE,
};
constexpr uint8_t valueCount = NR_ELEMENTS(vtypes);

if (value_nr < valueCount) {
return vtypes[value_nr];
}
return Sensor_VType::SENSOR_TYPE_NONE;
}

# endif // if FEATURE_MQTT_DISCOVER

int p108_storageValueToBaudrate(uint8_t baudrate_setting) {
switch (baudrate_setting) {
case 0:
Expand Down
6 changes: 5 additions & 1 deletion src/src/PluginStructs/P108_data_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct P108_data_struct : public PluginTaskData_base {
uint8_t modbusAddress);

bool isInitialized() const {
return modbus.isInitialized();
return modbus.isInitialized();
}

ModbusRTU_struct modbus;
Expand All @@ -78,6 +78,10 @@ float p108_readValue(uint8_t query,

void p108_showValueLoadPage(uint8_t query,
struct EventStruct *event);
# if FEATURE_MQTT_DISCOVER
Sensor_VType Plugin_108_getQueryVType(uint8_t value_nr);
# endif // if FEATURE_MQTT_DISCOVER


#endif // ifdef USES_P108
#endif // ifndef PLUGINSTRUCTS_P108_DATA_STRUCT_H

0 comments on commit fa3afec

Please sign in to comment.