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"
|
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,
|
2018-11-01 16:17:23 +00:00
|
|
|
MeterType type, int manufacturer, int media,
|
|
|
|
LinkMode required_link_mode) :
|
|
|
|
type_(type), manufacturer_(manufacturer), media_(media), name_(name), bus_(bus),
|
|
|
|
required_link_mode_(required_link_mode)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
|
|
|
use_aes_ = true;
|
2019-03-05 17:38:54 +00:00
|
|
|
id_ = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MeterType MeterCommonImplementation::type()
|
|
|
|
{
|
|
|
|
return type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeterCommonImplementation::manufacturer()
|
|
|
|
{
|
|
|
|
return manufacturer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeterCommonImplementation::media()
|
|
|
|
{
|
|
|
|
return media_;
|
|
|
|
}
|
|
|
|
|
|
|
|
string MeterCommonImplementation::id()
|
|
|
|
{
|
2019-03-05 17:38:54 +00:00
|
|
|
return id_;
|
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 17:38:54 +00:00
|
|
|
void MeterCommonImplementation::onUpdate(function<void(string,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-02-23 12:41:17 +00:00
|
|
|
MeterType toMeterType(string& type)
|
2018-03-05 10:29:25 +00:00
|
|
|
{
|
2019-02-23 12:41:17 +00:00
|
|
|
if (type == "multical21") return MULTICAL21_METER;
|
|
|
|
if (type == "flowiq3100") return FLOWIQ3100_METER;
|
|
|
|
if (type == "multical302") return MULTICAL302_METER;
|
|
|
|
if (type == "omnipower") return OMNIPOWER_METER;
|
|
|
|
if (type == "supercom587") return SUPERCOM587_METER;
|
|
|
|
if (type == "iperl") return IPERL_METER;
|
|
|
|
if (type == "qcaloric") return QCALORIC_METER;
|
2018-03-05 10:29:25 +00:00
|
|
|
return UNKNOWN_METER;
|
|
|
|
}
|
|
|
|
|
2019-02-23 12:41:17 +00:00
|
|
|
LinkMode toMeterLinkMode(string& type)
|
2018-11-01 16:17:23 +00:00
|
|
|
{
|
2019-02-23 12:41:17 +00:00
|
|
|
if (type == "multical21") return LinkModeC1;
|
|
|
|
if (type == "flowiq3100") return LinkModeC1;
|
|
|
|
if (type == "multical302") return LinkModeC1;
|
|
|
|
if (type == "omnipower") return LinkModeC1;
|
|
|
|
if (type == "supercom587") return LinkModeT1;
|
|
|
|
if (type == "iperl") return LinkModeT1;
|
2019-02-28 18:38:43 +00:00
|
|
|
if (type == "qcaloric") return LinkModeC1;
|
2018-11-01 16:17:23 +00:00
|
|
|
|
|
|
|
return UNKNOWN_LINKMODE;
|
|
|
|
}
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
bool MeterCommonImplementation::isTelegramForMe(Telegram *t)
|
|
|
|
{
|
2019-03-05 17:38:54 +00:00
|
|
|
if (manufacturer_ != 0 && t->m_field != manufacturer_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (id_ == t->id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (id_ == "*") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
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 17:38:54 +00:00
|
|
|
for (auto &cb : on_update_) if (cb) cb(t->id, this);
|
2018-03-05 10:29:25 +00:00
|
|
|
t->handled = true;
|
|
|
|
}
|
2018-11-01 19:45:44 +00:00
|
|
|
|
|
|
|
void MeterCommonImplementation::updateMedia(int media)
|
|
|
|
{
|
|
|
|
media_ = media;
|
|
|
|
}
|