2019-03-19 19:53:44 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2019 Fredrik Öhrström
|
|
|
|
|
|
|
|
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"dvparser.h"
|
|
|
|
#include"meters.h"
|
|
|
|
#include"meters_common_implementation.h"
|
|
|
|
#include"wmbus.h"
|
|
|
|
#include"wmbus_utils.h"
|
|
|
|
#include"util.h"
|
|
|
|
|
|
|
|
#include<map>
|
|
|
|
#include<memory.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<string>
|
|
|
|
#include<time.h>
|
|
|
|
#include<vector>
|
|
|
|
|
|
|
|
struct MeterAmiplus : public virtual ElectricityMeter, public virtual MeterCommonImplementation {
|
|
|
|
MeterAmiplus(WMBus *bus, string& name, string& id, string& key);
|
|
|
|
|
|
|
|
double totalEnergyConsumption();
|
|
|
|
double currentPowerConsumption();
|
2019-03-20 21:16:45 +00:00
|
|
|
double totalEnergyProduction();
|
|
|
|
double currentPowerProduction();
|
2019-03-19 19:53:44 +00:00
|
|
|
|
|
|
|
void printMeter(Telegram *t,
|
|
|
|
string *human_readable,
|
|
|
|
string *fields, char separator,
|
|
|
|
string *json,
|
|
|
|
vector<string> *envs);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handleTelegram(Telegram *t);
|
|
|
|
void processContent(Telegram *t);
|
|
|
|
|
|
|
|
double total_energy_ {};
|
|
|
|
double current_power_ {};
|
2019-03-20 21:16:45 +00:00
|
|
|
double total_energy_returned_ {};
|
|
|
|
double current_power_returned_ {};
|
|
|
|
string device_date_time_;
|
2019-03-19 19:53:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MeterAmiplus::MeterAmiplus(WMBus *bus, string& name, string& id, string& key) :
|
2019-04-01 21:49:24 +00:00
|
|
|
MeterCommonImplementation(bus, name, id, key, AMIPLUS_METER, 0, LinkMode::T1)
|
2019-03-19 19:53:44 +00:00
|
|
|
{
|
2019-04-02 16:57:37 +00:00
|
|
|
// This is one manufacturer of Amiplus compatible meters.
|
2019-04-01 21:49:24 +00:00
|
|
|
addManufacturer(MANUFACTURER_APA);
|
2019-04-02 16:57:37 +00:00
|
|
|
addMedia(0x02); // Electricity meter
|
|
|
|
|
|
|
|
// This is another manufacturer
|
2019-04-01 21:49:24 +00:00
|
|
|
addManufacturer(MANUFACTURER_DEV);
|
2019-04-02 16:57:37 +00:00
|
|
|
// Oddly, this device has not been configured to send as a electricity meter,
|
|
|
|
// but instead a device/media type that is used for gateway or relays or something?
|
|
|
|
addMedia(0x37); // Radio converter (meter side)
|
2019-03-19 19:53:44 +00:00
|
|
|
MeterCommonImplementation::bus()->onTelegram(calll(this,handleTelegram,Telegram*));
|
|
|
|
}
|
|
|
|
|
|
|
|
double MeterAmiplus::totalEnergyConsumption()
|
|
|
|
{
|
|
|
|
return total_energy_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MeterAmiplus::currentPowerConsumption()
|
|
|
|
{
|
|
|
|
return current_power_;
|
|
|
|
}
|
|
|
|
|
2019-03-20 21:16:45 +00:00
|
|
|
double MeterAmiplus::totalEnergyProduction()
|
|
|
|
{
|
|
|
|
return total_energy_returned_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MeterAmiplus::currentPowerProduction()
|
|
|
|
{
|
|
|
|
return current_power_returned_;
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:53:44 +00:00
|
|
|
void MeterAmiplus::handleTelegram(Telegram *t) {
|
|
|
|
|
|
|
|
if (!isTelegramForMe(t)) {
|
|
|
|
// This telegram is not intended for this meter.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:04:35 +00:00
|
|
|
verbose("(amiplus) %s %02x%02x%02x%02x ",
|
2019-03-19 19:53:44 +00:00
|
|
|
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 20:32:28 +00:00
|
|
|
t->expectVersion("amiplus", 0x02);
|
2019-03-19 19:53:44 +00:00
|
|
|
|
|
|
|
if (t->isEncrypted() && !useAes() && !t->isSimulated()) {
|
2019-03-19 20:04:35 +00:00
|
|
|
warning("(amiplus) warning: telegram is encrypted but no key supplied!\n");
|
2019-03-19 19:53:44 +00:00
|
|
|
}
|
|
|
|
if (useAes()) {
|
|
|
|
vector<uchar> aeskey = key();
|
|
|
|
decryptMode5_AES_CBC(t, aeskey);
|
|
|
|
} else {
|
|
|
|
t->content = t->payload;
|
|
|
|
}
|
2019-03-19 20:04:35 +00:00
|
|
|
logTelegram("(amiplus) log", t->parsed, t->content);
|
2019-03-19 19:53:44 +00:00
|
|
|
int content_start = t->parsed.size();
|
|
|
|
processContent(t);
|
|
|
|
if (isDebugEnabled()) {
|
2019-03-19 20:04:35 +00:00
|
|
|
t->explainParse("(amiplus)", content_start);
|
2019-03-19 19:53:44 +00:00
|
|
|
}
|
|
|
|
triggerUpdate(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeterAmiplus::processContent(Telegram *t)
|
|
|
|
{
|
|
|
|
map<string,pair<int,DVEntry>> values;
|
|
|
|
parseDV(t, t->content, t->content.begin(), t->content.size(), &values);
|
|
|
|
|
|
|
|
int offset;
|
2019-03-20 21:16:45 +00:00
|
|
|
string key;
|
|
|
|
|
|
|
|
if (findKey(ValueInformation::EnergyWh, 0, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, &total_energy_);
|
|
|
|
t->addMoreExplanation(offset, " total energy (%f kwh)", total_energy_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (findKey(ValueInformation::PowerW, 0, &key, &values)) {
|
|
|
|
extractDVdouble(&values, key, &offset, ¤t_power_);
|
|
|
|
t->addMoreExplanation(offset, " current power (%f kw)", current_power_);
|
|
|
|
}
|
|
|
|
|
|
|
|
extractDVdouble(&values, "0E833C", &offset, &total_energy_returned_);
|
|
|
|
t->addMoreExplanation(offset, " total energy returned (%f kwh)", total_energy_returned_);
|
|
|
|
|
|
|
|
extractDVdouble(&values, "0BAB3C", &offset, ¤t_power_returned_);
|
|
|
|
t->addMoreExplanation(offset, " current power returned (%f kw)", current_power_returned_);
|
|
|
|
|
|
|
|
if (findKey(ValueInformation::DateTime, 0, &key, &values)) {
|
|
|
|
struct tm datetime;
|
|
|
|
extractDVdate(&values, key, &offset, &datetime);
|
|
|
|
device_date_time_ = strdatetime(&datetime);
|
|
|
|
t->addMoreExplanation(offset, " device datetime (%s)", device_date_time_.c_str());
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<ElectricityMeter> createAmiplus(WMBus *bus, string& name, string& id, string& key)
|
|
|
|
{
|
|
|
|
return unique_ptr<ElectricityMeter>(new MeterAmiplus(bus,name,id,key));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeterAmiplus::printMeter(Telegram *t,
|
|
|
|
string *human_readable,
|
|
|
|
string *fields, char separator,
|
|
|
|
string *json,
|
|
|
|
vector<string> *envs)
|
|
|
|
{
|
|
|
|
|
|
|
|
char buf[65536];
|
|
|
|
buf[65535] = 0;
|
|
|
|
|
2019-03-20 21:16:45 +00:00
|
|
|
snprintf(buf, sizeof(buf)-1, "%s\t%s\t% 3.3f kwh\t% 3.3f kw\t% 3.3f kwh\t% 3.3f kw\t%s",
|
2019-03-19 19:53:44 +00:00
|
|
|
name().c_str(),
|
|
|
|
t->id.c_str(),
|
|
|
|
totalEnergyConsumption(),
|
|
|
|
currentPowerConsumption(),
|
2019-03-20 21:16:45 +00:00
|
|
|
totalEnergyProduction(),
|
|
|
|
currentPowerProduction(),
|
2019-03-19 19:53:44 +00:00
|
|
|
datetimeOfUpdateHumanReadable().c_str());
|
|
|
|
|
|
|
|
*human_readable = buf;
|
|
|
|
|
2019-03-20 21:16:45 +00:00
|
|
|
snprintf(buf, sizeof(buf)-1, "%s%c%s%c%f%c%f%c%f%c%f%c%s",
|
2019-03-19 19:53:44 +00:00
|
|
|
name().c_str(), separator,
|
|
|
|
t->id.c_str(), separator,
|
|
|
|
totalEnergyConsumption(), separator,
|
|
|
|
currentPowerConsumption(), separator,
|
2019-03-20 21:16:45 +00:00
|
|
|
totalEnergyProduction(), separator,
|
|
|
|
currentPowerProduction(), separator,
|
2019-03-19 19:53:44 +00:00
|
|
|
datetimeOfUpdateRobot().c_str());
|
|
|
|
|
|
|
|
*fields = buf;
|
|
|
|
|
|
|
|
#define Q(x,y) "\""#x"\":"#y","
|
|
|
|
#define QS(x,y) "\""#x"\":\""#y"\","
|
|
|
|
#define QSE(x,y) "\""#x"\":\""#y"\""
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf)-1, "{"
|
|
|
|
QS(media,electricity)
|
2019-03-19 20:04:35 +00:00
|
|
|
QS(meter,amiplus)
|
2019-03-19 19:53:44 +00:00
|
|
|
QS(name,%s)
|
|
|
|
QS(id,%s)
|
2019-03-20 21:16:45 +00:00
|
|
|
Q(total_energy_consumption_kwh,%f)
|
|
|
|
Q(current_power_consumption_kw,%f)
|
|
|
|
Q(total_energy_production_kwh,%f)
|
|
|
|
Q(current_power_production_kw,%f)
|
|
|
|
QS(device_date_time,%s)
|
2019-03-19 19:53:44 +00:00
|
|
|
QSE(timestamp,%s)
|
|
|
|
"}",
|
|
|
|
name().c_str(),
|
|
|
|
t->id.c_str(),
|
|
|
|
totalEnergyConsumption(),
|
|
|
|
currentPowerConsumption(),
|
2019-03-20 21:16:45 +00:00
|
|
|
totalEnergyProduction(),
|
|
|
|
currentPowerProduction(),
|
|
|
|
device_date_time_.c_str(),
|
2019-03-19 19:53:44 +00:00
|
|
|
datetimeOfUpdateRobot().c_str());
|
|
|
|
|
|
|
|
*json = buf;
|
|
|
|
|
|
|
|
envs->push_back(string("METER_JSON=")+*json);
|
2019-03-19 20:04:35 +00:00
|
|
|
envs->push_back(string("METER_TYPE=amiplus"));
|
2019-03-19 19:53:44 +00:00
|
|
|
envs->push_back(string("METER_ID=")+t->id);
|
2019-03-20 21:16:45 +00:00
|
|
|
envs->push_back(string("METER_TOTAL_ENERGY_CONSUMPTION_KWH=")+to_string(totalEnergyConsumption()));
|
|
|
|
envs->push_back(string("METER_CURRENT_POWER_CONSUMPTION_KW=")+to_string(currentPowerConsumption()));
|
|
|
|
envs->push_back(string("METER_TOTAL_ENERGY_PRODUCTION_KWH=")+to_string(totalEnergyProduction()));
|
|
|
|
envs->push_back(string("METER_CURRENT_POWER_PRODUCTION_KW=")+to_string(currentPowerProduction()));
|
2019-03-19 19:53:44 +00:00
|
|
|
envs->push_back(string("METER_TIMESTAMP=")+datetimeOfUpdateRobot());
|
|
|
|
}
|