2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2019-02-24 13:08:51 +00:00
|
|
|
Copyright (C) 2017-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/>.
|
|
|
|
*/
|
2017-08-09 10:00:11 +00:00
|
|
|
|
|
|
|
#include"meters.h"
|
2018-03-05 10:29:25 +00:00
|
|
|
#include"meters_common_implementation.h"
|
2019-05-04 11:07:37 +00:00
|
|
|
#include"units.h"
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
#include<memory.h>
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2019-02-23 12:41:17 +00:00
|
|
|
MeterCommonImplementation::MeterCommonImplementation(WMBus *bus, string& name, string& id, string& key,
|
2019-03-05 20:19:05 +00:00
|
|
|
MeterType type, int manufacturer,
|
2018-11-01 16:17:23 +00:00
|
|
|
LinkMode required_link_mode) :
|
2019-04-01 21:49:24 +00:00
|
|
|
type_(type), name_(name), bus_(bus),
|
2018-11-01 16:17:23 +00:00
|
|
|
required_link_mode_(required_link_mode)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
use_aes_ = true;
|
2019-03-05 20:19:05 +00:00
|
|
|
ids_ = splitIds(id);
|
2019-02-23 12:41:17 +00:00
|
|
|
if (key.length() == 0) {
|
2019-02-23 21:06:50 +00:00
|
|
|
use_aes_ = false;
|
2018-03-05 10:29:25 +00:00
|
|
|
} else {
|
2019-02-23 21:06:50 +00:00
|
|
|
hex2bin(key, &key_);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
2019-04-01 21:49:24 +00:00
|
|
|
if (manufacturer) {
|
|
|
|
manufacturers_.insert(manufacturer);
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 11:55:52 +00:00
|
|
|
void MeterCommonImplementation::addConversions(std::vector<Unit> cs)
|
|
|
|
{
|
|
|
|
for (Unit c : cs)
|
|
|
|
{
|
|
|
|
conversions_.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
MeterType MeterCommonImplementation::type()
|
|
|
|
{
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
vector<int> MeterCommonImplementation::media()
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
return media_;
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
void MeterCommonImplementation::addMedia(int m)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2019-03-05 20:19:05 +00:00
|
|
|
media_.push_back(m);
|
|
|
|
}
|
|
|
|
|
2019-05-04 11:55:52 +00:00
|
|
|
void MeterCommonImplementation::addPrint(string vname, Quantity vquantity,
|
|
|
|
function<double(Unit)> getValueFunc, string help, bool field)
|
|
|
|
{
|
|
|
|
prints_.push_back( { vname, vquantity, defaultUnitForQuantity(vquantity), getValueFunc, help, field });
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:49:24 +00:00
|
|
|
void MeterCommonImplementation::addManufacturer(int m)
|
|
|
|
{
|
|
|
|
manufacturers_.insert(m);
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
vector<string> MeterCommonImplementation::ids()
|
|
|
|
{
|
|
|
|
return ids_;
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string MeterCommonImplementation::name()
|
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
WMBus *MeterCommonImplementation::bus()
|
|
|
|
{
|
|
|
|
return bus_;
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:17:23 +00:00
|
|
|
LinkMode MeterCommonImplementation::requiredLinkMode()
|
|
|
|
{
|
|
|
|
return required_link_mode_;
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
void MeterCommonImplementation::onUpdate(function<void(Telegram*,Meter*)> cb)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
on_update_.push_back(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeterCommonImplementation::numUpdates()
|
|
|
|
{
|
|
|
|
return num_updates_;
|
|
|
|
}
|
|
|
|
|
|
|
|
string MeterCommonImplementation::datetimeOfUpdateHumanReadable()
|
|
|
|
{
|
|
|
|
char datetime[40];
|
|
|
|
memset(datetime, 0, sizeof(datetime));
|
|
|
|
strftime(datetime, 20, "%Y-%m-%d %H:%M.%S", localtime(&datetime_of_update_));
|
|
|
|
return string(datetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
string MeterCommonImplementation::datetimeOfUpdateRobot()
|
|
|
|
{
|
|
|
|
char datetime[40];
|
|
|
|
memset(datetime, 0, sizeof(datetime));
|
|
|
|
// This is the date time in the Greenwich timezone (Zulu time), dont get surprised!
|
|
|
|
strftime(datetime, sizeof(datetime), "%FT%TZ", gmtime(&datetime_of_update_));
|
|
|
|
return string(datetime);
|
|
|
|
}
|
|
|
|
|
2019-05-04 08:52:18 +00:00
|
|
|
string toMeterName(MeterType mt)
|
|
|
|
{
|
|
|
|
#define X(mname,link,info,type,cname) if (mt == MeterType::type) return #mname;
|
|
|
|
LIST_OF_METERS
|
|
|
|
#undef X
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
MeterType toMeterType(string& t)
|
|
|
|
{
|
|
|
|
#define X(mname,link,info,type,cname) if (t == #mname) return MeterType::type;
|
|
|
|
LIST_OF_METERS
|
|
|
|
#undef X
|
|
|
|
return MeterType::UNKNOWN;
|
2018-11-01 16:17:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 08:52:18 +00:00
|
|
|
LinkMode toMeterLinkMode(string& t)
|
|
|
|
{
|
|
|
|
#define X(mname,link,info,type,cname) if (t == #mname) return LinkMode::link;
|
|
|
|
LIST_OF_METERS
|
|
|
|
#undef X
|
|
|
|
return LinkMode::UNKNOWN;
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
bool MeterCommonImplementation::isTelegramForMe(Telegram *t)
|
|
|
|
{
|
2019-03-05 20:19:05 +00:00
|
|
|
debug("(meter) %s: for me? %s\n", name_.c_str(), t->id.c_str());
|
|
|
|
|
|
|
|
bool id_match = false;
|
|
|
|
for (auto id : ids_)
|
|
|
|
{
|
|
|
|
if (id == t->id) {
|
|
|
|
id_match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (id == "*") {
|
|
|
|
id_match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!id_match) {
|
2019-03-19 20:32:28 +00:00
|
|
|
// The id must match.
|
2019-03-05 20:19:05 +00:00
|
|
|
debug("(meter) %s: not for me: not my id\n", name_.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:49:24 +00:00
|
|
|
if (manufacturers_.count(t->m_field) == 0) {
|
2019-03-19 20:32:28 +00:00
|
|
|
// We are not that strict for the manufacturer.
|
|
|
|
// Simply warn.
|
|
|
|
warning("(meter) %s: probably not for me since manufacturer differs\n", name_.c_str());
|
2019-03-05 17:38:54 +00:00
|
|
|
}
|
2019-03-05 20:19:05 +00:00
|
|
|
|
|
|
|
bool media_match = false;
|
|
|
|
for (auto m : media_) {
|
|
|
|
if (m == t->a_field_device_type) {
|
|
|
|
media_match = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-05 17:38:54 +00:00
|
|
|
}
|
2019-03-05 20:19:05 +00:00
|
|
|
|
|
|
|
if (!media_match) {
|
2019-03-19 20:32:28 +00:00
|
|
|
warning("(meter) %s: probably not for me since media does not match\n", name_.c_str());
|
2019-03-05 17:38:54 +00:00
|
|
|
}
|
2019-03-05 20:19:05 +00:00
|
|
|
|
|
|
|
debug("(meter) %s: yes for me\n", name_.c_str());
|
|
|
|
return true;
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MeterCommonImplementation::useAes()
|
|
|
|
{
|
|
|
|
return use_aes_;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<uchar> MeterCommonImplementation::key()
|
|
|
|
{
|
|
|
|
return key_;
|
|
|
|
}
|
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
vector<string> MeterCommonImplementation::getRecords()
|
|
|
|
{
|
|
|
|
vector<string> recs;
|
|
|
|
for (auto& p : values_)
|
|
|
|
{
|
|
|
|
recs.push_back(p.first);
|
|
|
|
}
|
|
|
|
return recs;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MeterCommonImplementation::getRecordAsDouble(string record)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t MeterCommonImplementation::getRecordAsUInt16(string record)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
void MeterCommonImplementation::triggerUpdate(Telegram *t)
|
|
|
|
{
|
|
|
|
datetime_of_update_ = time(NULL);
|
|
|
|
num_updates_++;
|
2019-03-05 20:19:05 +00:00
|
|
|
for (auto &cb : on_update_) if (cb) cb(t, this);
|
2018-03-05 10:29:25 +00:00
|
|
|
t->handled = true;
|
|
|
|
}
|