2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2019-02-24 13:08:51 +00:00
|
|
|
Copyright (C) 2018-2019 Fredrik Öhrström
|
2018-04-01 06:53:37 +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/>.
|
|
|
|
*/
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
#include"meters.h"
|
|
|
|
#include"meters_common_implementation.h"
|
2019-11-26 15:12:32 +00:00
|
|
|
#include"dvparser.h"
|
2018-03-05 10:29:25 +00:00
|
|
|
#include"wmbus.h"
|
|
|
|
#include"wmbus_utils.h"
|
|
|
|
#include"util.h"
|
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
#define INFO_CODE_VOLTAGE_INTERRUPTED 1
|
|
|
|
#define INFO_CODE_WRONG_FLOW_DIRECTION 2
|
|
|
|
#define INFO_CODE_SENSOR_T2_OUT_OF_RANGE 4
|
|
|
|
#define INFO_CODE_SENSOR_T1_OUT_OF_RANGE 8
|
|
|
|
#define INFO_CODE_FLOW_SENSOR_WEAK_OR_AIR 16
|
|
|
|
#define INFO_CODE_TEMP_DIFF_WRONG_POLARITY 32
|
|
|
|
#define INFO_CODE_VOLTAGE_TOO_LOW 128
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
struct MeterMultical302 : public virtual HeatMeter, public virtual MeterCommonImplementation {
|
2019-05-21 12:19:54 +00:00
|
|
|
MeterMultical302(WMBus *bus, MeterInfo &mi);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-05-04 11:07:37 +00:00
|
|
|
double totalEnergyConsumption(Unit u);
|
2019-11-26 15:12:32 +00:00
|
|
|
double targetEnergyConsumption(Unit u);
|
2019-05-04 11:07:37 +00:00
|
|
|
double currentPowerConsumption(Unit u);
|
2019-11-26 15:12:32 +00:00
|
|
|
string status();
|
2019-05-04 11:07:37 +00:00
|
|
|
double totalVolume(Unit u);
|
2019-11-26 15:12:32 +00:00
|
|
|
double targetVolume(Unit u);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void processContent(Telegram *t);
|
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
uchar info_codes_ {};
|
2019-05-04 11:07:37 +00:00
|
|
|
double total_energy_kwh_ {};
|
2019-11-26 15:12:32 +00:00
|
|
|
double target_energy_kwh_ {};
|
2019-05-04 11:07:37 +00:00
|
|
|
double current_power_kw_ {};
|
|
|
|
double total_volume_m3_ {};
|
2019-11-26 15:12:32 +00:00
|
|
|
string target_date_ {};
|
2018-03-05 10:29:25 +00:00
|
|
|
};
|
|
|
|
|
2019-05-21 12:19:54 +00:00
|
|
|
MeterMultical302::MeterMultical302(WMBus *bus, MeterInfo &mi) :
|
2019-06-06 15:28:20 +00:00
|
|
|
MeterCommonImplementation(bus, mi, MeterType::MULTICAL302, MANUFACTURER_KAM)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2019-05-04 17:56:17 +00:00
|
|
|
setEncryptionMode(EncryptionMode::AES_CTR);
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
addMedia(0x04); // Heat media
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-06-06 15:28:20 +00:00
|
|
|
addLinkMode(LinkMode::C1);
|
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
addPrint("total_energy_consumption", Quantity::Energy,
|
2019-05-04 17:56:17 +00:00
|
|
|
[&](Unit u){ return totalEnergyConsumption(u); },
|
|
|
|
"The total energy consumption recorded by this meter.",
|
|
|
|
true, true);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
addPrint("current_power_consumption", Quantity::Power,
|
|
|
|
[&](Unit u){ return currentPowerConsumption(u); },
|
|
|
|
"Current power consumption.",
|
|
|
|
true, true);
|
|
|
|
|
2019-05-04 17:56:17 +00:00
|
|
|
addPrint("total_volume", Quantity::Volume,
|
|
|
|
[&](Unit u){ return totalVolume(u); },
|
|
|
|
"Total volume of heat media.",
|
|
|
|
true, true);
|
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
addPrint("at_date", Quantity::Text,
|
|
|
|
[&](){ return target_date_; },
|
|
|
|
"Date when total energy consumption was recorded.",
|
|
|
|
false, true);
|
|
|
|
|
|
|
|
addPrint("total_energy_consumption_at_date", Quantity::Energy,
|
|
|
|
[&](Unit u){ return targetEnergyConsumption(u); },
|
|
|
|
"The total energy consumption recorded at the target date.",
|
|
|
|
false, true);
|
|
|
|
|
|
|
|
addPrint("current_status", Quantity::Text,
|
|
|
|
[&](){ return status(); },
|
|
|
|
"Status of meter.",
|
2019-05-04 17:56:17 +00:00
|
|
|
true, true);
|
|
|
|
|
|
|
|
MeterCommonImplementation::bus()->onTelegram(calll(this,handleTelegram,Telegram*));
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 12:19:54 +00:00
|
|
|
unique_ptr<HeatMeter> createMultical302(WMBus *bus, MeterInfo &mi) {
|
|
|
|
return unique_ptr<HeatMeter>(new MeterMultical302(bus, mi));
|
2019-05-04 06:52:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:17 +00:00
|
|
|
double MeterMultical302::totalEnergyConsumption(Unit u)
|
2019-05-04 06:52:25 +00:00
|
|
|
{
|
2019-05-04 17:56:17 +00:00
|
|
|
assertQuantity(u, Quantity::Energy);
|
|
|
|
return convert(total_energy_kwh_, Unit::KWH, u);
|
2019-05-04 06:52:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
double MeterMultical302::targetEnergyConsumption(Unit u)
|
|
|
|
{
|
|
|
|
assertQuantity(u, Quantity::Energy);
|
|
|
|
return convert(target_energy_kwh_, Unit::KWH, u);
|
|
|
|
}
|
|
|
|
|
2019-05-04 11:07:37 +00:00
|
|
|
double MeterMultical302::totalVolume(Unit u)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2019-05-04 11:07:37 +00:00
|
|
|
assertQuantity(u, Quantity::Volume);
|
|
|
|
return convert(total_volume_m3_, Unit::M3, u);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:17 +00:00
|
|
|
double MeterMultical302::currentPowerConsumption(Unit u)
|
|
|
|
{
|
|
|
|
assertQuantity(u, Quantity::Power);
|
|
|
|
return convert(current_power_kw_, Unit::KW, u);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:56:17 +00:00
|
|
|
void MeterMultical302::processContent(Telegram *t)
|
|
|
|
{
|
2019-11-26 15:12:32 +00:00
|
|
|
/*
|
|
|
|
(multical302) 11: bcdb payload crc
|
|
|
|
(multical302) 13: 78 frame type (long frame)
|
|
|
|
(multical302) 14: 03 dif (24 Bit Integer/Binary Instantaneous value)
|
|
|
|
(multical302) 15: 06 vif (Energy kWh)
|
|
|
|
(multical302) 16: * 2C0000 total energy consumption (44.000000 kWh)
|
|
|
|
(multical302) 19: 43 dif (24 Bit Integer/Binary Instantaneous value storagenr=1)
|
|
|
|
(multical302) 1a: 06 vif (Energy kWh)
|
|
|
|
(multical302) 1b: * 000000 target energy consumption (0.000000 kWh)
|
|
|
|
(multical302) 1e: 03 dif (24 Bit Integer/Binary Instantaneous value)
|
|
|
|
(multical302) 1f: 14 vif (Volume 10⁻² m³)
|
|
|
|
(multical302) 20: * 630000 total volume (0.990000 m3)
|
|
|
|
(multical302) 23: 42 dif (16 Bit Integer/Binary Instantaneous value storagenr=1)
|
|
|
|
(multical302) 24: 6C vif (Date type G)
|
|
|
|
(multical302) 25: * 7F2A target date (2019-10-31 00:00)
|
|
|
|
(multical302) 27: 02 dif (16 Bit Integer/Binary Instantaneous value)
|
|
|
|
(multical302) 28: 2D vif (Power 10² W)
|
|
|
|
(multical302) 29: * 1300 current power consumption (1.900000 kW)
|
|
|
|
(multical302) 2b: 01 dif (8 Bit Integer/Binary Instantaneous value)
|
|
|
|
(multical302) 2c: FF vif (Vendor extension)
|
|
|
|
(multical302) 2d: 21 vife (per minute)
|
|
|
|
(multical302) 2e: * 00 info codes (00)
|
|
|
|
*/
|
|
|
|
map<string,pair<int,DVEntry>> values;
|
|
|
|
|
2018-04-01 06:53:37 +00:00
|
|
|
vector<uchar>::iterator bytes = t->content.begin();
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
int crc0 = t->content[0];
|
|
|
|
int crc1 = t->content[1];
|
2020-01-20 19:41:33 +00:00
|
|
|
t->addExplanationAndIncrementPos(bytes, 2, "%02x%02x payload crc", crc0, crc1);
|
2019-11-26 15:12:32 +00:00
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
int frame_type = t->content[2];
|
2020-01-20 19:41:33 +00:00
|
|
|
t->addExplanationAndIncrementPos(bytes, 1, "%02x frame type (%s)", frame_type, frameTypeKamstrupC1(frame_type).c_str());
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
if (frame_type == 0x79) {
|
2019-11-26 15:12:32 +00:00
|
|
|
// This is a "compact frame" in wmbus lingo.
|
|
|
|
// (Other such frame_types are Ci=0x69, 0x6a, 0x6b and Ci=0x79, 0x7b, compact frames and format frames)
|
|
|
|
// 0,1 = crc for format signature = hash over DRH (Data Record Header)
|
|
|
|
// The DRH is the dif(difes)vif(vifes) bytes for all the records...
|
|
|
|
// This hash is used to find the suitable format string, that has been previously
|
|
|
|
// seen in a long frame telegram.
|
|
|
|
uchar ecrc0 = t->content[3];
|
|
|
|
uchar ecrc1 = t->content[4];
|
2020-01-20 19:41:33 +00:00
|
|
|
t->addExplanationAndIncrementPos(bytes, 2, "%02x%02x format signature", ecrc0, ecrc1);
|
2019-11-26 15:12:32 +00:00
|
|
|
uint16_t format_signature = ecrc1<<8 | ecrc0;
|
|
|
|
|
|
|
|
vector<uchar> format_bytes;
|
|
|
|
bool ok = loadFormatBytesFromSignature(format_signature, &format_bytes);
|
|
|
|
if (!ok) {
|
2019-11-26 20:54:46 +00:00
|
|
|
|
|
|
|
// We have not yet seen a long frame, but we know the formats for these
|
|
|
|
// particular hashes:
|
|
|
|
if (format_signature == 0xf1e7)
|
|
|
|
{
|
|
|
|
hex2bin("030643060314426C022D01FF21", &format_bytes);
|
|
|
|
debug("(multical302) using hard coded format for hash f1e7\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
verbose("(%s) ignoring telegram since format signature hash 0x%02x is yet unknown.\n",
|
|
|
|
"multical302", format_signature);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-26 15:12:32 +00:00
|
|
|
}
|
|
|
|
vector<uchar>::iterator format = format_bytes.begin();
|
|
|
|
|
|
|
|
// 2,3 = crc for payload = hash over both DRH and data bytes. Or is it only over the data bytes?
|
|
|
|
int ecrc2 = t->content[5];
|
|
|
|
int ecrc3 = t->content[6];
|
2020-01-20 19:41:33 +00:00
|
|
|
t->addExplanationAndIncrementPos(bytes, 2, "%02x%02x data crc", ecrc2, ecrc3);
|
2019-11-26 15:12:32 +00:00
|
|
|
parseDV(t, t->content, t->content.begin()+7, t->content.size()-7, &values, &format, format_bytes.size());
|
|
|
|
}
|
|
|
|
else if (frame_type == 0x78)
|
|
|
|
{
|
|
|
|
parseDV(t, t->content, t->content.begin()+3, t->content.size()-3, &values);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
warning("(multical302) warning: unknown frame %02x (did you use the correct encryption key?)\n", frame_type);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
int offset;
|
|
|
|
string key;
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
extractDVuint8(&values, "01FF21", &offset, &info_codes_);
|
|
|
|
t->addMoreExplanation(offset, " info codes (%s)", status().c_str());
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
if(findKey(MeasurementType::Instantaneous, ValueInformation::EnergyWh, 0, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, &total_energy_kwh_);
|
|
|
|
t->addMoreExplanation(offset, " total energy consumption (%f kWh)", total_energy_kwh_);
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
if(findKey(MeasurementType::Instantaneous, ValueInformation::Volume, 0, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, &total_volume_m3_);
|
|
|
|
t->addMoreExplanation(offset, " total volume (%f m3)", total_volume_m3_);
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-11-26 15:12:32 +00:00
|
|
|
if(findKey(MeasurementType::Instantaneous, ValueInformation::EnergyWh, 1, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, &target_energy_kwh_);
|
|
|
|
t->addMoreExplanation(offset, " target energy consumption (%f kWh)", target_energy_kwh_);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
2019-11-26 15:12:32 +00:00
|
|
|
|
|
|
|
if(findKey(MeasurementType::Instantaneous, ValueInformation::PowerW, 0, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, ¤t_power_kw_);
|
|
|
|
t->addMoreExplanation(offset, " current power consumption (%f kW)", current_power_kw_);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
2019-11-26 15:12:32 +00:00
|
|
|
|
|
|
|
if (findKey(MeasurementType::Unknown, ValueInformation::Date, 1, &key, &values)) {
|
|
|
|
struct tm datetime;
|
|
|
|
extractDVdate(&values, key, &offset, &datetime);
|
|
|
|
target_date_ = strdatetime(&datetime);
|
|
|
|
t->addMoreExplanation(offset, " target date (%s)", target_date_.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
string MeterMultical302::status()
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
if (info_codes_ & INFO_CODE_VOLTAGE_INTERRUPTED) s.append("VOLTAGE_INTERRUPTED ");
|
|
|
|
if (info_codes_ & INFO_CODE_WRONG_FLOW_DIRECTION) s.append("WRONG_FLOW_DIRECTION ");
|
|
|
|
if (info_codes_ & INFO_CODE_SENSOR_T2_OUT_OF_RANGE) s.append("SENSOR_T2_OUT_OF_RANGE ");
|
|
|
|
if (info_codes_ & INFO_CODE_SENSOR_T1_OUT_OF_RANGE) s.append("SENSOR_T1_OUT_OF_RANGE ");
|
|
|
|
if (info_codes_ & INFO_CODE_FLOW_SENSOR_WEAK_OR_AIR) s.append("FLOW_SENSOR_WEAK_OR_AIR ");
|
|
|
|
if (info_codes_ & INFO_CODE_TEMP_DIFF_WRONG_POLARITY) s.append("TEMP_DIFF_WRONG_POLARITY ");
|
|
|
|
if (info_codes_ & 64) s.append("UNKNOWN_64 ");
|
|
|
|
if (info_codes_ & INFO_CODE_VOLTAGE_TOO_LOW) s.append("VOLTAGE_TOO_LOW ");
|
|
|
|
if (s.length() > 0) {
|
|
|
|
s.pop_back(); // Remove final space
|
|
|
|
return s;
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
2019-11-26 15:12:32 +00:00
|
|
|
return s;
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|