Skip to content

Commit

Permalink
sns: optional address method
Browse files Browse the repository at this point in the history
preserve existing ones where it is possible to retrieve some unique id
drop the other ones as it is the only device on the bus already
  • Loading branch information
mcspr committed Oct 24, 2024
1 parent 3cdccfa commit dc9ba63
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 56 deletions.
15 changes: 9 additions & 6 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3671,13 +3671,16 @@ void report(const Value& report, const Magnitude& magnitude) {
#if SENSOR_PUBLISH_ADDRESSES
STRING_VIEW_INLINE(AddressTopic, SENSOR_ADDRESS_TOPIC);

String address_topic;
address_topic.reserve(1 + report.topic.length() + AddressTopic.length());
address_topic.concat(AddressTopic.data(), AddressTopic.length());
address_topic += '/';
address_topic += report.topic;
const auto address = magnitude.sensor->address(magnitude.slot);
if (address.length()) {
String address_topic;
address_topic.reserve(1 + report.topic.length() + AddressTopic.length());
address_topic.concat(AddressTopic.data(), AddressTopic.length());
address_topic += '/';
address_topic += report.topic;

mqttSend(address_topic.c_str(), magnitude.sensor->address(magnitude.slot).c_str());
mqttSend(address_topic.c_str(), address.c_str());
}
#endif
}

Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/A02YYUSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class A02YYUSensor : public BaseSensor {
return F("A02YYU");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) const override {
return String(A02YYU_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_DISTANCE;
Expand Down
10 changes: 6 additions & 4 deletions code/espurna/sensors/BaseSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ class BaseSensor {
}

// Address of the sensor (it could be the GPIO or I2C address)
virtual String address(unsigned char index) const = 0;

// Type for slot # index
virtual unsigned char type(unsigned char index) const = 0;
virtual String address(unsigned char index) const {
return String();
}

// Unit of the slot # index
virtual espurna::sensor::Unit units(unsigned char index) const {
Expand Down Expand Up @@ -255,6 +254,9 @@ class BaseSensor {
return Unit::None;
}

// Type of slot # index
virtual unsigned char type(unsigned char index) const = 0;

// Current value for slot # index
virtual double value(unsigned char index) = 0;

Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/CSE7766Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ class CSE7766Sensor : public BaseEmonSensor {
return F("CSE7766");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) const override {
return String(CSE7766_PORT, 10);
}

// Loop-like method, call it in your main loop
void tick() override {
_read();
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/EZOPHSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class EZOPHSensor : public BaseSensor {
return F("EZOPH");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) const override {
return String(EZOPH_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_PH;
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/MHZ19Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ class MHZ19Sensor : public BaseSensor {
return F("MHZ19");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) const override {
return String(MHZ19_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_CO2;
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/PM1006Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class PM1006Sensor : public BaseSensor {
return F("PM1006");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) const override {
return String(PM1006_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_PM2DOT5;
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/PMSX003Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ class PMSX003Sensor : public BaseSensor, PMSX003 {
return out;
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) const override {
return String(PMS_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
return Specs[_type].slot_types[index];
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/SM300D2Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class SM300D2Sensor : public BaseSensor {
return F("SM300D2");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) const override {
return String(SM300D2_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_CO2;
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/sensors/SenseAirSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class SenseAirSensor : public BaseSensor, SenseAir {

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) const override {
return String(SENSEAIR_PORT, 10);
return STRING_VIEW("0xFE").toString();
}

// Type for slot # index
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/T6613Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ class T6613Sensor : public BaseSensor {
return F("T6613");
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) const override {
return String(T6613_PORT, 10);
}

// Type for slot # index
unsigned char type(unsigned char index) const override {
if (index == 0) return MAGNITUDE_CO2;
Expand Down
5 changes: 0 additions & 5 deletions code/espurna/sensors/V9261FSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ class V9261FSensor : public BaseEmonSensor {
return Name.toString();
}

// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char) const override {
return String(V9261F_PORT, 10);
}

// Loop-like method, call it in your main loop
void tick() override {
_read_some();
Expand Down

0 comments on commit dc9ba63

Please sign in to comment.