wmbusmeters/src/meters.cc

182 wiersze
4.3 KiB
C++

/*
Copyright (C) 2017-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"meters.h"
#include"meters_common_implementation.h"
#include<memory.h>
MeterCommonImplementation::MeterCommonImplementation(WMBus *bus, string& name, string& id, string& key,
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)
{
use_aes_ = true;
id_ = id;
if (key.length() == 0) {
use_aes_ = false;
} else {
hex2bin(key, &key_);
}
}
MeterType MeterCommonImplementation::type()
{
return type_;
}
int MeterCommonImplementation::manufacturer()
{
return manufacturer_;
}
int MeterCommonImplementation::media()
{
return media_;
}
string MeterCommonImplementation::id()
{
return id_;
}
string MeterCommonImplementation::name()
{
return name_;
}
WMBus *MeterCommonImplementation::bus()
{
return bus_;
}
LinkMode MeterCommonImplementation::requiredLinkMode()
{
return required_link_mode_;
}
void MeterCommonImplementation::onUpdate(function<void(string,Meter*)> cb)
{
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);
}
MeterType toMeterType(string& type)
{
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;
return UNKNOWN_METER;
}
LinkMode toMeterLinkMode(string& type)
{
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;
if (type == "qcaloric") return LinkModeC1;
return UNKNOWN_LINKMODE;
}
bool MeterCommonImplementation::isTelegramForMe(Telegram *t)
{
if (manufacturer_ != 0 && t->m_field != manufacturer_) {
return false;
}
if (id_ == t->id) {
return true;
}
if (id_ == "*") {
return true;
}
return false;
}
bool MeterCommonImplementation::useAes()
{
return use_aes_;
}
vector<uchar> MeterCommonImplementation::key()
{
return key_;
}
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;
}
void MeterCommonImplementation::triggerUpdate(Telegram *t)
{
datetime_of_update_ = time(NULL);
num_updates_++;
for (auto &cb : on_update_) if (cb) cb(t->id, this);
t->handled = true;
}
void MeterCommonImplementation::updateMedia(int media)
{
media_ = media;
}