Accept offset form extras meterinfo in apator162

pull/328/head
Tomasz Gramza 2021-08-21 16:02:12 +00:00
rodzic 8dd3e87c44
commit 70476b1027
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6D31F809D5A1AA9B
1 zmienionych plików z 111 dodań i 73 usunięć

Wyświetl plik

@ -33,8 +33,10 @@ struct MeterApator162 : public virtual WaterMeter, public virtual MeterCommonImp
private:
void processContent(Telegram *t);
void processExtras(string miExtras);
double total_water_consumption_m3_ {};
unsigned short offset_ = 0;
};
shared_ptr<WaterMeter> createApator162(MeterInfo &mi)
@ -45,6 +47,8 @@ shared_ptr<WaterMeter> createApator162(MeterInfo &mi)
MeterApator162::MeterApator162(MeterInfo &mi) :
MeterCommonImplementation(mi, MeterDriver::APATOR162)
{
processExtras(mi.extras);
setExpectedTPLSecurityMode(TPLSecurityMode::AES_CBC_IV);
addLinkMode(LinkMode::T1);
@ -56,6 +60,32 @@ MeterApator162::MeterApator162(MeterInfo &mi) :
true, true);
}
void MeterApator162::processExtras(string miExtras)
{
map<string,string> extras;
bool ok = parseExtras(miExtras, &extras);
if (!ok)
{
error("(apator162) invalid extra parameters (%s)\n", miExtras.c_str());
}
if (extras.size() > 0)
{
if (extras.count("offset") > 0)
{
try {
offset_ = stoi(extras["offset"]);
}
catch (const std::invalid_argument &e) {
error("(apator162) invalid extra parameters offset (%s)\n", extras["offset"].c_str());
}
catch (const std::out_of_range &e) {
error("(apator162) out of range extra parameters offset (%s)\n", extras["offset"].c_str());
}
}
}
}
double MeterApator162::totalWaterConsumption(Unit u)
{
assertQuantity(u, Quantity::Volume);
@ -82,87 +112,95 @@ void MeterApator162::processContent(Telegram *t)
// Current assumption of this proprietary protocol is that byte 13 tells
// us where the current total water consumption is located.
int o = 0;
uchar guess10 = content[10];
uchar guess11 = content[11];
uchar guess12 = content[12];
if ((guess11 & 0x84) == 0x84)
{
o = 23;
if (offset_ > 0) {
o = offset_;
strprintf(total, "%02x%02x%02x%02x", content[o], content[o+1], content[o+2], content[o+3]);
debug("(apator162) using offset from the configuration (offset=%d) that results with total value: %s\n", o, total.c_str());
}
else
if ((guess11 & 0x83) == 0x83)
{
o = 23;
}
else
if ((guess11 & 0x81) == 0x81)
{
if (guess10 == 02)
else {
uchar guess10 = content[10];
uchar guess11 = content[11];
uchar guess12 = content[12];
if ((guess11 & 0x84) == 0x84)
{
o = 23;
}
else
{
o = 20;
}
}
else
if ((guess11 & 0x40) == 0x40)
{
o = 20;
}
else
if ((guess11 & 0x10) == 0x10)
{
o = 12;
}
else
if ((guess11 & 0x01) == 0x01)
{
o = 9;
}
else
{
warning("(apator162) Unknown value in proprietary(unknown) apator162 protocol. Ignoring telegram. Found 0x%02x expected bit 0x01, 0x10, 0x40 or 0x80 to be set.\n", guess11);
return;
}
uint32_t o9 = content[9] | content[9+1] <<8 | content[9+2] << 16 | content[9+3] << 24;
uint32_t o12 = content[12] | content[12+1] <<8 | content[12+2] << 16 | content[12+3] << 24;
uint32_t o20 = content[20] | content[20+1] <<8 | content[20+2] << 16 | content[20+3] << 24;
uint32_t o23 = content[23] | content[23+1] <<8 | content[23+2] << 16 | content[23+3] << 24;
uint32_t guess = content[o] | content[o+1] <<8 | content[o+2] << 16 | content[o+3] << 24;
strprintf(total, "%02x%02x%02x%02x", content[o], content[o+1], content[o+2], content[o+3]);
debug("(apator162) Guessing offset to be %d from byte >10=%02x 11=%02x 12=%02x<: total %s\n",
o, guess10, guess11, guess12, total.c_str());
debug("(apator162) other potential values o9=%u o12=%u o20=%u o23=%u guess=%u\n", o9, o12, o20, o23, guess);
// Ok, the guess might not good enough. Lets do a sanity check and revert to another offset
// that has a reasonable value.....at least it should be less than 58400000 liters.....why?
// Let us assume a 16 year lifetime of the apator meter, 10 m3 per day for 16 years = 16*365*10000 = 58400000
#define MAX 58400000
if (guess > MAX)
{
if (o9 < MAX)
{
o = 9;
}
else if (o12 < MAX)
{
o = 12;
}
else if (o20 < MAX)
{
o = 20;
}
else if (o23 < MAX)
if ((guess11 & 0x83) == 0x83)
{
o = 23;
}
else
if ((guess11 & 0x81) == 0x81)
{
if (guess10 == 02)
{
o = 23;
}
else
{
o = 20;
}
}
else
if ((guess11 & 0x40) == 0x40)
{
o = 20;
}
else
if ((guess11 & 0x10) == 0x10)
{
o = 12;
}
else
if ((guess11 & 0x01) == 0x01)
{
o = 9;
}
else
{
warning("(apator162) Unknown value in proprietary(unknown) apator162 protocol. Ignoring telegram. Found 0x%02x expected bit 0x01, 0x10, 0x40 or 0x80 to be set.\n", guess11);
return;
}
uint32_t o9 = content[9] | content[9+1] <<8 | content[9+2] << 16 | content[9+3] << 24;
uint32_t o12 = content[12] | content[12+1] <<8 | content[12+2] << 16 | content[12+3] << 24;
uint32_t o20 = content[20] | content[20+1] <<8 | content[20+2] << 16 | content[20+3] << 24;
uint32_t o23 = content[23] | content[23+1] <<8 | content[23+2] << 16 | content[23+3] << 24;
uint32_t guess = content[o] | content[o+1] <<8 | content[o+2] << 16 | content[o+3] << 24;
strprintf(total, "%02x%02x%02x%02x", content[o], content[o+1], content[o+2], content[o+3]);
debug("(apator162) adjusting to offset %d instead\n", o);
debug("(apator162) Guessing offset to be %d from byte >10=%02x 11=%02x 12=%02x<: total %s\n",
o, guess10, guess11, guess12, total.c_str());
debug("(apator162) other potential values o9=%u o12=%u o20=%u o23=%u guess=%u\n", o9, o12, o20, o23, guess);
// Ok, the guess might not good enough. Lets do a sanity check and revert to another offset
// that has a reasonable value.....at least it should be less than 58400000 liters.....why?
// Let us assume a 16 year lifetime of the apator meter, 10 m3 per day for 16 years = 16*365*10000 = 58400000
#define MAX 58400000
if (guess > MAX)
{
if (o9 < MAX)
{
o = 9;
}
else if (o12 < MAX)
{
o = 12;
}
else if (o20 < MAX)
{
o = 20;
}
else if (o23 < MAX)
{
o = 23;
}
strprintf(total, "%02x%02x%02x%02x", content[o], content[o+1], content[o+2], content[o+3]);
debug("(apator162) adjusting to offset %d instead\n", o);
}
}
vendor_values["0413"] = { 25, DVEntry(MeasurementType::Instantaneous, 0x13, 0, 0, 0, total) };