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"
|
|
|
|
#include"wmbus.h"
|
|
|
|
#include"wmbus_utils.h"
|
|
|
|
#include"util.h"
|
|
|
|
|
|
|
|
#include<memory.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<string>
|
|
|
|
#include<time.h>
|
|
|
|
#include<vector>
|
|
|
|
|
|
|
|
struct MeterMultical302 : public virtual HeatMeter, public virtual MeterCommonImplementation {
|
2019-02-23 12:41:17 +00:00
|
|
|
MeterMultical302(WMBus *bus, string& name, string& id, string& key);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
double totalEnergyConsumption();
|
2019-05-04 06:52:25 +00:00
|
|
|
double currentPeriodEnergyConsumption();
|
|
|
|
double previousPeriodEnergyConsumption();
|
2018-04-20 09:42:46 +00:00
|
|
|
double currentPowerConsumption();
|
|
|
|
double totalVolume();
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
void printMeter(Telegram *t,
|
2019-03-05 17:38:54 +00:00
|
|
|
string *human_readable,
|
2018-11-02 12:50:12 +00:00
|
|
|
string *fields, char separator,
|
|
|
|
string *json,
|
|
|
|
vector<string> *envs);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void handleTelegram(Telegram *t);
|
|
|
|
void processContent(Telegram *t);
|
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
double total_energy_ {};
|
|
|
|
double current_power_ {};
|
|
|
|
double total_volume_ {};
|
2018-03-05 10:29:25 +00:00
|
|
|
};
|
|
|
|
|
2019-02-23 12:41:17 +00:00
|
|
|
MeterMultical302::MeterMultical302(WMBus *bus, string& name, string& id, string& key) :
|
2019-05-04 08:52:18 +00:00
|
|
|
MeterCommonImplementation(bus, name, id, key, MeterType::MULTICAL302, MANUFACTURER_KAM, LinkMode::C1)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2019-03-05 20:19:05 +00:00
|
|
|
addMedia(0x04); // Heat media
|
2018-03-05 10:29:25 +00:00
|
|
|
MeterCommonImplementation::bus()->onTelegram(calll(this,handleTelegram,Telegram*));
|
|
|
|
}
|
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
double MeterMultical302::totalEnergyConsumption()
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2018-03-16 10:48:04 +00:00
|
|
|
return total_energy_;
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
double MeterMultical302::currentPowerConsumption()
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
return current_power_;
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:52:25 +00:00
|
|
|
double MeterMultical302::currentPeriodEnergyConsumption()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MeterMultical302::previousPeriodEnergyConsumption()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
double MeterMultical302::totalVolume()
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
return total_volume_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeterMultical302::handleTelegram(Telegram *t) {
|
|
|
|
|
|
|
|
if (!isTelegramForMe(t)) {
|
|
|
|
// This telegram is not intended for this meter.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verbose("(multical302) %s %02x%02x%02x%02x ",
|
|
|
|
name().c_str(),
|
|
|
|
t->a_field_address[0], t->a_field_address[1], t->a_field_address[2],
|
|
|
|
t->a_field_address[3]);
|
|
|
|
|
2019-03-19 17:51:57 +00:00
|
|
|
if (t->isEncrypted() && !useAes() && !t->isSimulated()) {
|
2019-03-15 14:06:45 +00:00
|
|
|
warning("(multical302) warning: telegram is encrypted but no key supplied!\n");
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
if (useAes()) {
|
|
|
|
vector<uchar> aeskey = key();
|
2018-11-25 11:33:14 +00:00
|
|
|
decryptMode1_AES_CTR(t, aeskey);
|
2018-03-05 10:29:25 +00:00
|
|
|
} else {
|
|
|
|
t->content = t->payload;
|
|
|
|
}
|
|
|
|
logTelegram("(multical302) log", t->parsed, t->content);
|
|
|
|
int content_start = t->parsed.size();
|
|
|
|
processContent(t);
|
|
|
|
if (isDebugEnabled()) {
|
|
|
|
t->explainParse("(multical302)", content_start);
|
|
|
|
}
|
|
|
|
triggerUpdate(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeterMultical302::processContent(Telegram *t) {
|
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];
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 2, "%02x%02x payload crc", crc0, crc1);
|
2018-03-05 10:29:25 +00:00
|
|
|
int frame_type = t->content[2];
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(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) {
|
2018-11-29 21:25:50 +00:00
|
|
|
/*if (t->content.size() != 17) {
|
|
|
|
|
2019-02-26 22:40:44 +00:00
|
|
|
warning("(multical302) warning: Unexpected length of frame %zu. Expected 17 bytes! ", t->content.size());
|
2018-04-01 06:53:37 +00:00
|
|
|
padWithZeroesTo(&t->content, 17, &t->content);
|
2018-03-05 10:29:25 +00:00
|
|
|
warning("\n");
|
2018-11-29 21:25:50 +00:00
|
|
|
}*/
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-04-01 06:53:37 +00:00
|
|
|
// This code should be rewritten to use parseDV see the Multical21 code.
|
|
|
|
// But I cannot do this without more examples of 302 telegrams.
|
|
|
|
t->addExplanation(bytes, 4, "%02x%02x%02x%02x unknown", t->content[3], t->content[4], t->content[5], t->content[6]);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
int rec1val0 = t->content[7];
|
|
|
|
int rec1val1 = t->content[8];
|
|
|
|
int rec1val2 = t->content[9];
|
|
|
|
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 4, "%02x%02x%02x unknown", t->content[10], t->content[11], t->content[12]);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-03-16 10:48:04 +00:00
|
|
|
int total_energy_raw = rec1val2*256*256 + rec1val1*256 + rec1val0;
|
|
|
|
total_energy_ = total_energy_raw;
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 3, "%02x%02x%02x total power (%d)",
|
2018-03-16 10:48:04 +00:00
|
|
|
rec1val0, rec1val1, rec1val2, total_energy_raw);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
int rec2val0 = t->content[13];
|
|
|
|
int rec2val1 = t->content[14];
|
|
|
|
int rec2val2 = t->content[15];
|
|
|
|
|
|
|
|
int total_volume_raw = rec2val2*256*256 + rec2val1*256 + rec2val0;
|
|
|
|
total_volume_ = total_volume_raw;
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 3, "%02x%02x%02x total volume (%d)",
|
2018-03-05 10:29:25 +00:00
|
|
|
rec2val0, rec2val1, rec2val2, total_volume_raw);
|
|
|
|
}
|
|
|
|
else if (frame_type == 0x78)
|
|
|
|
{
|
2018-11-29 21:25:50 +00:00
|
|
|
/*if (t->content.size() != 26) {
|
2019-02-26 22:40:44 +00:00
|
|
|
warning("(multical302) warning: Unexpected length of frame %zu. Expected 26 bytes! ", t->content.size());
|
2018-04-01 06:53:37 +00:00
|
|
|
padWithZeroesTo(&t->content, 26, &t->content);
|
2018-03-05 10:29:25 +00:00
|
|
|
warning("\n");
|
2018-11-29 21:25:50 +00:00
|
|
|
}*/
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-04-01 06:53:37 +00:00
|
|
|
// This code should be rewritten to use parseDV see the Multical21 code.
|
|
|
|
// But I cannot do this without more examples of 302 telegrams.
|
2018-03-05 10:29:25 +00:00
|
|
|
vector<uchar> unknowns;
|
|
|
|
unknowns.insert(unknowns.end(), t->content.begin()+3, t->content.begin()+24);
|
|
|
|
string hex = bin2hex(unknowns);
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 23-2, "%s unknown", hex.c_str());
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
int rec1val0 = t->content[24];
|
|
|
|
int rec1val1 = t->content[25];
|
|
|
|
|
|
|
|
int current_power_raw = (rec1val1*256 + rec1val0)*100;
|
|
|
|
current_power_ = current_power_raw;
|
2018-04-01 06:53:37 +00:00
|
|
|
t->addExplanation(bytes, 2, "%02x%02x current power (%d)",
|
2018-03-05 10:29:25 +00:00
|
|
|
rec1val0, rec1val1, current_power_raw);
|
|
|
|
}
|
|
|
|
else {
|
2018-03-05 14:12:58 +00:00
|
|
|
warning("(multical302) warning: unknown frame %02x (did you use the correct encryption key?)\n", frame_type);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 12:41:17 +00:00
|
|
|
unique_ptr<HeatMeter> createMultical302(WMBus *bus, string& name, string& id, string& key) {
|
2018-12-28 17:35:32 +00:00
|
|
|
return unique_ptr<HeatMeter>(new MeterMultical302(bus,name,id,key));
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
void MeterMultical302::printMeter(Telegram *t,
|
2019-03-05 17:38:54 +00:00
|
|
|
string *human_readable,
|
2018-11-02 12:50:12 +00:00
|
|
|
string *fields, char separator,
|
|
|
|
string *json,
|
|
|
|
vector<string> *envs)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2018-11-02 12:50:12 +00:00
|
|
|
char buf[65536];
|
|
|
|
buf[65535] = 0;
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-11-02 12:50:12 +00:00
|
|
|
snprintf(buf, sizeof(buf)-1, "%s\t%s\t% 3.3f kwh\t% 3.3f m3\t% 3.3f kwh\t%s",
|
|
|
|
name().c_str(),
|
2019-03-05 20:19:05 +00:00
|
|
|
t->id.c_str(),
|
2018-11-02 12:50:12 +00:00
|
|
|
totalEnergyConsumption(),
|
|
|
|
totalVolume(),
|
|
|
|
currentPowerConsumption(),
|
|
|
|
datetimeOfUpdateHumanReadable().c_str());
|
|
|
|
|
|
|
|
*human_readable = buf;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf)-1, "%s%c%s%c%f%c%f%c%f%c%s",
|
|
|
|
name().c_str(), separator,
|
2019-03-05 20:19:05 +00:00
|
|
|
t->id.c_str(), separator,
|
2018-11-02 12:50:12 +00:00
|
|
|
totalEnergyConsumption(), separator,
|
|
|
|
totalVolume(), separator,
|
|
|
|
currentPowerConsumption(), separator,
|
|
|
|
datetimeOfUpdateRobot().c_str());
|
|
|
|
|
|
|
|
*fields = buf;
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
#define Q(x,y) "\""#x"\":"#y","
|
|
|
|
#define QS(x,y) "\""#x"\":\""#y"\","
|
|
|
|
#define QSE(x,y) "\""#x"\":\""#y"\""
|
|
|
|
|
2018-11-02 12:50:12 +00:00
|
|
|
snprintf(buf, sizeof(buf)-1, "{"
|
|
|
|
QS(media,heat)
|
|
|
|
QS(meter,multical302)
|
|
|
|
QS(name,%s)
|
|
|
|
QS(id,%s)
|
|
|
|
Q(total_kwh,%f)
|
|
|
|
Q(total_volume_m3,%f)
|
|
|
|
QS(current_kw,%f)
|
|
|
|
QSE(timestamp,%s)
|
|
|
|
"}",
|
|
|
|
name().c_str(),
|
2019-03-05 20:19:05 +00:00
|
|
|
t->id.c_str(),
|
2018-11-02 12:50:12 +00:00
|
|
|
totalEnergyConsumption(),
|
|
|
|
totalVolume(),
|
|
|
|
currentPowerConsumption(),
|
|
|
|
datetimeOfUpdateRobot().c_str());
|
|
|
|
|
|
|
|
*json = buf;
|
|
|
|
|
|
|
|
envs->push_back(string("METER_JSON=")+*json);
|
|
|
|
envs->push_back(string("METER_TYPE=multical302"));
|
2019-03-05 20:19:05 +00:00
|
|
|
envs->push_back(string("METER_ID=")+t->id);
|
2018-11-02 12:50:12 +00:00
|
|
|
envs->push_back(string("METER_TOTAL_KWH=")+to_string(totalEnergyConsumption()));
|
|
|
|
envs->push_back(string("METER_TOTAL_VOLUME_M3=")+to_string(totalVolume()));
|
|
|
|
envs->push_back(string("METER_CURRENT_KW=")+to_string(currentPowerConsumption()));
|
|
|
|
envs->push_back(string("METER_TIMESTAMP=")+datetimeOfUpdateRobot());
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|