2019-10-29 20:26:01 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2019 Jacek Tomasiak
|
2020-10-04 20:52:05 +00:00
|
|
|
2020 Fredrik Öhrström
|
2021-02-12 19:12:46 +00:00
|
|
|
2021 Vincent Privat
|
2019-10-29 20:26:01 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include"meters.h"
|
|
|
|
#include"meters_common_implementation.h"
|
|
|
|
#include"wmbus.h"
|
|
|
|
#include"wmbus_utils.h"
|
2021-02-12 19:12:46 +00:00
|
|
|
#include"manufacturer_specificities.h"
|
2019-10-29 20:26:01 +00:00
|
|
|
|
2020-04-25 11:30:23 +00:00
|
|
|
#include<algorithm>
|
|
|
|
#include<stdbool.h>
|
2019-10-29 20:26:01 +00:00
|
|
|
|
2020-04-25 11:30:23 +00:00
|
|
|
/** Contains all the booleans required to store the alarms of a PRIOS device. */
|
|
|
|
typedef struct _izar_alarms {
|
|
|
|
bool general_alarm;
|
|
|
|
bool leakage_currently;
|
|
|
|
bool leakage_previously;
|
|
|
|
bool meter_blocked;
|
|
|
|
bool back_flow;
|
|
|
|
bool underflow;
|
|
|
|
bool overflow;
|
|
|
|
bool submarine;
|
|
|
|
bool sensor_fraud_currently;
|
|
|
|
bool sensor_fraud_previously;
|
|
|
|
bool mechanical_fraud_currently;
|
|
|
|
bool mechanical_fraud_previously;
|
|
|
|
} izar_alarms;
|
|
|
|
|
2019-10-29 20:26:01 +00:00
|
|
|
struct MeterIzar : public virtual WaterMeter, public virtual MeterCommonImplementation {
|
2020-08-30 19:33:48 +00:00
|
|
|
MeterIzar(MeterInfo &mi);
|
2019-10-29 20:26:01 +00:00
|
|
|
|
|
|
|
// Total water counted through the meter
|
|
|
|
double totalWaterConsumption(Unit u);
|
|
|
|
bool hasTotalWaterConsumption();
|
|
|
|
|
2021-02-13 17:29:58 +00:00
|
|
|
string serialNumber();
|
2019-10-29 20:29:31 +00:00
|
|
|
double lastMonthTotalWaterConsumption(Unit u);
|
2020-04-22 20:09:48 +00:00
|
|
|
string setH0Date();
|
2020-04-25 11:30:23 +00:00
|
|
|
string currentAlarmsText();
|
|
|
|
string previousAlarmsText();
|
2019-10-29 20:29:31 +00:00
|
|
|
|
2019-10-29 20:26:01 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void processContent(Telegram *t);
|
2021-02-07 22:06:44 +00:00
|
|
|
vector<uchar> decodePrios(const vector<uchar> &origin, const vector<uchar> &payload, uint32_t key);
|
2019-10-29 20:26:01 +00:00
|
|
|
|
2021-02-13 17:29:58 +00:00
|
|
|
string prefix;
|
|
|
|
uint32_t serial_number {0};
|
2020-04-22 20:38:40 +00:00
|
|
|
double remaining_battery_life;
|
2020-04-22 20:09:48 +00:00
|
|
|
uint16_t h0_year;
|
|
|
|
uint8_t h0_month;
|
|
|
|
uint8_t h0_day;
|
2019-10-29 20:26:01 +00:00
|
|
|
double total_water_consumption_l_ {};
|
2019-10-29 20:29:31 +00:00
|
|
|
double last_month_total_water_consumption_l_ {};
|
2021-02-08 20:13:44 +00:00
|
|
|
uint32_t transmit_period_s_ {};
|
2021-02-13 17:29:58 +00:00
|
|
|
uint16_t manufacture_year {0};
|
2020-04-25 11:30:23 +00:00
|
|
|
izar_alarms alarms;
|
2019-10-29 20:25:10 +00:00
|
|
|
|
|
|
|
vector<uint32_t> keys;
|
2019-10-29 20:26:01 +00:00
|
|
|
};
|
|
|
|
|
2020-09-21 19:55:21 +00:00
|
|
|
shared_ptr<WaterMeter> createIzar(MeterInfo &mi)
|
2019-10-29 20:26:01 +00:00
|
|
|
{
|
2020-09-21 19:55:21 +00:00
|
|
|
return shared_ptr<WaterMeter>(new MeterIzar(mi));
|
2019-10-29 20:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 19:33:48 +00:00
|
|
|
MeterIzar::MeterIzar(MeterInfo &mi) :
|
2021-03-08 07:40:48 +00:00
|
|
|
MeterCommonImplementation(mi, MeterDriver::IZAR)
|
2019-10-29 20:26:01 +00:00
|
|
|
{
|
2021-02-12 19:12:46 +00:00
|
|
|
initializeDiehlDefaultKeySupport(meterKeys()->confidentiality_key, keys);
|
2019-10-29 20:26:01 +00:00
|
|
|
addLinkMode(LinkMode::T1);
|
|
|
|
|
2021-02-13 17:29:58 +00:00
|
|
|
addPrint("prefix", Quantity::Text,
|
|
|
|
[&](){ return prefix; },
|
|
|
|
"The alphanumeric prefix printed before serial number on device.",
|
|
|
|
true, true);
|
|
|
|
|
|
|
|
addPrint("serial_number", Quantity::Text,
|
|
|
|
[&](){ return serialNumber(); },
|
|
|
|
"The meter serial number.",
|
|
|
|
true, true);
|
|
|
|
|
2019-10-29 20:26:01 +00:00
|
|
|
addPrint("total", Quantity::Volume,
|
|
|
|
[&](Unit u){ return totalWaterConsumption(u); },
|
|
|
|
"The total water consumption recorded by this meter.",
|
|
|
|
true, true);
|
|
|
|
|
2019-10-29 20:29:31 +00:00
|
|
|
addPrint("last_month_total", Quantity::Volume,
|
|
|
|
[&](Unit u){ return lastMonthTotalWaterConsumption(u); },
|
|
|
|
"The total water consumption recorded by this meter around end of last month.",
|
|
|
|
true, true);
|
2020-04-22 20:09:48 +00:00
|
|
|
|
|
|
|
addPrint("last_month_measure_date", Quantity::Text,
|
|
|
|
[&](){ return setH0Date(); },
|
|
|
|
"The date when the meter recorded the most recent billing value.",
|
|
|
|
true, true);
|
|
|
|
|
2020-04-22 20:38:40 +00:00
|
|
|
addPrint("remaining_battery_life", Quantity::Time, Unit::Year,
|
|
|
|
[&](Unit u){ return convert(remaining_battery_life, Unit::Year, u); },
|
|
|
|
"How many more years the battery is expected to last",
|
|
|
|
true, true);
|
|
|
|
|
2020-04-25 11:30:23 +00:00
|
|
|
addPrint("current_alarms", Quantity::Text,
|
|
|
|
[&](){ return currentAlarmsText(); },
|
|
|
|
"Alarms currently reported by the meter.",
|
|
|
|
true, true);
|
|
|
|
|
|
|
|
addPrint("previous_alarms", Quantity::Text,
|
|
|
|
[&](){ return previousAlarmsText(); },
|
2021-02-04 20:58:11 +00:00
|
|
|
"Alarms previously reported by the meter.",
|
2020-04-25 11:30:23 +00:00
|
|
|
true, true);
|
|
|
|
|
2021-02-08 20:13:44 +00:00
|
|
|
addPrint("transmit_period", Quantity::Time, Unit::Second,
|
|
|
|
[&](Unit u){ return convert(transmit_period_s_, Unit::Second, u); },
|
|
|
|
"The period at which the meter transmits its data.",
|
|
|
|
true, true);
|
2021-02-13 17:29:58 +00:00
|
|
|
|
|
|
|
addPrint("manufacture_year", Quantity::Text,
|
|
|
|
[&](){ return to_string(manufacture_year); },
|
|
|
|
"The year during which the meter was manufactured.",
|
|
|
|
true, true);
|
2019-10-29 20:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double MeterIzar::totalWaterConsumption(Unit u)
|
|
|
|
{
|
|
|
|
assertQuantity(u, Quantity::Volume);
|
|
|
|
return convert(total_water_consumption_l_, Unit::L, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MeterIzar::hasTotalWaterConsumption()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:29:31 +00:00
|
|
|
double MeterIzar::lastMonthTotalWaterConsumption(Unit u)
|
|
|
|
{
|
|
|
|
assertQuantity(u, Quantity::Volume);
|
|
|
|
return convert(last_month_total_water_consumption_l_, Unit::L, u);
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:09:48 +00:00
|
|
|
string MeterIzar::setH0Date()
|
|
|
|
{
|
2020-05-11 10:04:45 +00:00
|
|
|
string date;
|
2020-05-27 03:57:10 +00:00
|
|
|
strprintf(date, "%d-%02d-%02d", h0_year, h0_month%99, h0_day%99);
|
2020-05-11 10:04:45 +00:00
|
|
|
return date;
|
2020-04-22 20:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 17:29:58 +00:00
|
|
|
string MeterIzar::serialNumber()
|
|
|
|
{
|
|
|
|
string result;
|
|
|
|
strprintf(result, "%06d", serial_number);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-25 11:30:23 +00:00
|
|
|
string MeterIzar::currentAlarmsText()
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
if (alarms.leakage_currently) {
|
|
|
|
s.append("leakage,");
|
|
|
|
}
|
|
|
|
if (alarms.meter_blocked) {
|
|
|
|
s.append("meter_blocked,");
|
|
|
|
}
|
|
|
|
if (alarms.back_flow) {
|
|
|
|
s.append("back_flow,");
|
|
|
|
}
|
|
|
|
if (alarms.underflow) {
|
|
|
|
s.append("underflow,");
|
|
|
|
}
|
|
|
|
if (alarms.overflow) {
|
|
|
|
s.append("overflow,");
|
|
|
|
}
|
|
|
|
if (alarms.submarine) {
|
|
|
|
s.append("submarine,");
|
|
|
|
}
|
|
|
|
if (alarms.sensor_fraud_currently) {
|
|
|
|
s.append("sensor_fraud,");
|
|
|
|
}
|
|
|
|
if (alarms.mechanical_fraud_currently) {
|
|
|
|
s.append("mechanical_fraud,");
|
|
|
|
}
|
|
|
|
if (s.length() > 0) {
|
|
|
|
if (alarms.general_alarm) {
|
|
|
|
return "general_alarm";
|
|
|
|
}
|
|
|
|
s.pop_back();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return "no_alarm";
|
|
|
|
}
|
|
|
|
|
|
|
|
string MeterIzar::previousAlarmsText()
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
if (alarms.leakage_previously) {
|
|
|
|
s.append("leakage,");
|
|
|
|
}
|
|
|
|
if (alarms.sensor_fraud_previously) {
|
|
|
|
s.append("sensor_fraud,");
|
|
|
|
}
|
|
|
|
if (alarms.mechanical_fraud_previously) {
|
|
|
|
s.append("mechanical_fraud,");
|
|
|
|
}
|
|
|
|
if (s.length() > 0) {
|
|
|
|
s.pop_back();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return "no_alarm";
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:26:01 +00:00
|
|
|
void MeterIzar::processContent(Telegram *t)
|
|
|
|
{
|
|
|
|
vector<uchar> frame;
|
2020-01-27 08:29:40 +00:00
|
|
|
t->extractFrame(&frame);
|
2021-02-13 17:29:58 +00:00
|
|
|
vector<uchar> origin = t->original.empty() ? frame : t->original;
|
2019-10-29 20:26:01 +00:00
|
|
|
|
|
|
|
vector<uchar> decoded_content;
|
|
|
|
for (auto& key : keys) {
|
2021-02-13 17:29:58 +00:00
|
|
|
decoded_content = decodePrios(origin, frame, key);
|
2019-10-29 20:26:01 +00:00
|
|
|
if (!decoded_content.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("(izar) Decoded PRIOS data: %s\n", bin2hex(decoded_content).c_str());
|
|
|
|
|
|
|
|
if (decoded_content.empty())
|
|
|
|
{
|
|
|
|
warning("(izar) Decoding PRIOS data failed. Ignoring telegram.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:29:58 +00:00
|
|
|
if (detectDiehlFrameInterpretation(frame) == DiehlFrameInterpretation::SAP_PRIOS)
|
|
|
|
{
|
2021-02-15 01:21:15 +00:00
|
|
|
string digits = to_string((origin[7] & 0x03) << 24 | origin[6] << 16 | origin[5] << 8 | origin[4]);
|
2021-02-13 17:29:58 +00:00
|
|
|
// get the manufacture year
|
|
|
|
uint8_t yy = atoi(digits.substr(0, 2).c_str());
|
|
|
|
manufacture_year = yy > 70 ? (1900 + yy) : (2000 + yy); // Maybe to adjust in 2070, if this code stills lives :D
|
|
|
|
// get the serial number
|
|
|
|
serial_number = atoi(digits.substr(3, digits.size()).c_str());
|
|
|
|
// get letters
|
|
|
|
uchar supplier_code = '@' + (((origin[9] & 0x0F) << 1) | (origin[8] >> 7));
|
|
|
|
uchar meter_type = '@' + ((origin[8] & 0x7C) >> 2);
|
|
|
|
uchar diameter = '@' + (((origin[8] & 0x03) << 3) | (origin[7] >> 5));
|
|
|
|
// build the prefix
|
|
|
|
strprintf(prefix, "%c%02d%c%c", supplier_code, yy, meter_type, diameter);
|
|
|
|
}
|
|
|
|
|
2021-02-08 20:13:44 +00:00
|
|
|
// get the remaining battery life (in year) and transmission period (in seconds)
|
2020-04-22 20:38:40 +00:00
|
|
|
remaining_battery_life = (frame[12] & 0x1F) / 2.0;
|
2021-02-08 20:13:44 +00:00
|
|
|
transmit_period_s_ = 1 << ((frame[11] & 0x0F) + 2);
|
2020-04-22 20:38:40 +00:00
|
|
|
|
2019-10-29 20:26:01 +00:00
|
|
|
total_water_consumption_l_ = uint32FromBytes(decoded_content, 1, true);
|
2019-10-29 20:29:31 +00:00
|
|
|
last_month_total_water_consumption_l_ = uint32FromBytes(decoded_content, 5, true);
|
2019-11-01 22:30:16 +00:00
|
|
|
|
2020-04-22 20:09:48 +00:00
|
|
|
// get the date when the second measurement was taken
|
|
|
|
h0_year = ((decoded_content[10] & 0xF0) >> 1) + ((decoded_content[9] & 0xE0) >> 5);
|
|
|
|
if (h0_year > 80) {
|
|
|
|
h0_year += 1900;
|
|
|
|
} else {
|
|
|
|
h0_year += 2000;
|
|
|
|
}
|
|
|
|
h0_month = decoded_content[10] & 0xF;
|
|
|
|
h0_day = decoded_content[9] & 0x1F;
|
|
|
|
|
2020-04-25 11:30:23 +00:00
|
|
|
// read the alarms:
|
|
|
|
alarms.general_alarm = frame[11] >> 7;
|
|
|
|
alarms.leakage_currently = frame[12] >> 7;
|
|
|
|
alarms.leakage_previously = frame[12] >> 6 & 0x1;
|
|
|
|
alarms.meter_blocked = frame[12] >> 5 & 0x1;
|
|
|
|
alarms.back_flow = frame[13] >> 7;
|
|
|
|
alarms.underflow = frame[13] >> 6 & 0x1;
|
|
|
|
alarms.overflow = frame[13] >> 5 & 0x1;
|
|
|
|
alarms.submarine = frame[13] >> 4 & 0x1;
|
|
|
|
alarms.sensor_fraud_currently = frame[13] >> 3 & 0x1;
|
|
|
|
alarms.sensor_fraud_previously = frame[13] >> 2 & 0x1;
|
|
|
|
alarms.mechanical_fraud_currently = frame[13] >> 1 & 0x1;
|
|
|
|
alarms.mechanical_fraud_previously = frame[13] & 0x1;
|
2019-10-29 20:26:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 22:06:44 +00:00
|
|
|
vector<uchar> MeterIzar::decodePrios(const vector<uchar> &origin, const vector<uchar> &frame, uint32_t key)
|
2019-10-29 20:26:01 +00:00
|
|
|
{
|
2021-02-12 19:12:46 +00:00
|
|
|
return decodeDiehlLfsr(origin, frame, key, DiehlLfsrCheckMethod::HEADER_1_BYTE, 0x4B);
|
2019-10-29 20:26:01 +00:00
|
|
|
}
|