2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2020-01-27 08:29:40 +00:00
|
|
|
Copyright (C) 2018-2020 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
|
|
|
|
|
|
|
#ifndef METERS_COMMON_IMPLEMENTATION_H_
|
|
|
|
#define METERS_COMMON_IMPLEMENTATION_H_
|
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
#include"dvparser.h"
|
2018-03-05 10:29:25 +00:00
|
|
|
#include"meters.h"
|
2019-05-04 06:52:25 +00:00
|
|
|
#include"units.h"
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-04-20 09:42:46 +00:00
|
|
|
#include<map>
|
2019-04-01 21:49:24 +00:00
|
|
|
#include<set>
|
2018-04-20 09:42:46 +00:00
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
enum PrintProperty
|
|
|
|
{
|
|
|
|
JSON = 1, // This field should be printed when using --format=json
|
|
|
|
FIELD = 2, // This field should be printed when using --format=field
|
|
|
|
IMPORTANT = 4, // The most important field.
|
|
|
|
};
|
|
|
|
|
2018-12-28 17:35:32 +00:00
|
|
|
struct MeterCommonImplementation : public virtual Meter
|
|
|
|
{
|
2021-02-20 21:21:01 +00:00
|
|
|
int index();
|
|
|
|
void setIndex(int i);
|
2021-03-14 08:41:35 +00:00
|
|
|
string bus();
|
2021-02-20 21:21:01 +00:00
|
|
|
vector<string>& ids();
|
|
|
|
string idsc();
|
|
|
|
vector<string> fields();
|
2022-01-06 17:28:22 +00:00
|
|
|
vector<FieldInfo> prints();
|
2018-03-05 10:29:25 +00:00
|
|
|
string name();
|
2021-03-08 07:40:48 +00:00
|
|
|
MeterDriver driver();
|
2022-01-06 17:28:22 +00:00
|
|
|
DriverName driverName();
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2020-01-27 08:29:40 +00:00
|
|
|
ELLSecurityMode expectedELLSecurityMode();
|
|
|
|
TPLSecurityMode expectedTPLSecurityMode();
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
string datetimeOfUpdateHumanReadable();
|
|
|
|
string datetimeOfUpdateRobot();
|
2021-08-01 16:24:19 +00:00
|
|
|
string unixTimestampOfUpdate();
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-03-05 20:19:05 +00:00
|
|
|
void onUpdate(function<void(Telegram*,Meter*)> cb);
|
2018-03-05 10:29:25 +00:00
|
|
|
int numUpdates();
|
|
|
|
|
2021-02-20 21:21:01 +00:00
|
|
|
static bool isTelegramForMeter(Telegram *t, Meter *meter, MeterInfo *mi);
|
2020-01-27 08:29:40 +00:00
|
|
|
MeterKeys *meterKeys();
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2022-01-02 12:08:36 +00:00
|
|
|
MeterCommonImplementation(MeterInfo &mi, string driver);
|
2022-01-06 17:28:22 +00:00
|
|
|
MeterCommonImplementation(MeterInfo &mi, DriverInfo &di);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2018-12-28 17:35:32 +00:00
|
|
|
~MeterCommonImplementation() = default;
|
|
|
|
|
2022-01-02 12:08:36 +00:00
|
|
|
string meterDriver() { return driver_; }
|
2019-05-04 08:52:18 +00:00
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void triggerUpdate(Telegram *t);
|
2020-01-27 08:29:40 +00:00
|
|
|
void setExpectedELLSecurityMode(ELLSecurityMode dsm);
|
|
|
|
void setExpectedTPLSecurityMode(TPLSecurityMode tsm);
|
2019-05-04 11:55:52 +00:00
|
|
|
void addConversions(std::vector<Unit> cs);
|
2022-01-06 17:28:22 +00:00
|
|
|
std::vector<Unit>& conversions() { return conversions_; }
|
2019-05-21 12:19:54 +00:00
|
|
|
void addShell(std::string cmdline);
|
2021-08-01 22:22:13 +00:00
|
|
|
void addExtraConstantField(std::string ecf);
|
2019-05-21 12:19:54 +00:00
|
|
|
std::vector<std::string> &shellCmdlines();
|
2021-08-01 22:22:13 +00:00
|
|
|
std::vector<std::string> &meterExtraConstantFields();
|
2021-12-31 11:27:57 +00:00
|
|
|
void setMeterType(MeterType mt);
|
2019-06-06 15:28:20 +00:00
|
|
|
void addLinkMode(LinkMode lm);
|
2020-06-30 14:02:38 +00:00
|
|
|
// Print with the default unit for this quantity.
|
2019-05-04 17:56:17 +00:00
|
|
|
void addPrint(string vname, Quantity vquantity,
|
|
|
|
function<double(Unit)> getValueFunc, string help, bool field, bool json);
|
2020-06-30 14:02:38 +00:00
|
|
|
// Print with exactly this unit for this quantity.
|
2020-04-22 20:38:40 +00:00
|
|
|
void addPrint(string vname, Quantity vquantity, Unit unit,
|
|
|
|
function<double(Unit)> getValueFunc, string help, bool field, bool json);
|
2020-06-30 14:02:38 +00:00
|
|
|
// Print the dimensionless Text quantity, no unit is needed.
|
2019-05-04 17:56:17 +00:00
|
|
|
void addPrint(string vname, Quantity vquantity,
|
|
|
|
function<std::string()> getValueFunc, string help, bool field, bool json);
|
2021-12-31 11:27:57 +00:00
|
|
|
|
2022-01-08 14:50:15 +00:00
|
|
|
#define SET_FUNC(varname,to_unit) {[=](Unit from_unit, double d){varname = convert(d, from_unit, to_unit);}}
|
|
|
|
#define GET_FUNC(varname,from_unit) {[=](Unit to_unit){return convert(varname, from_unit, to_unit);}}
|
2022-01-06 17:28:22 +00:00
|
|
|
|
|
|
|
void addFieldWithExtractor(
|
|
|
|
string vname, // Name of value without unit, eg total
|
|
|
|
Quantity vquantity, // Value belongs to this quantity.
|
|
|
|
DifVifKey dif_vif_key, // You can hardocde a dif vif header here or use NoDifVifKey
|
|
|
|
VifScaling vif_scaling,
|
|
|
|
MeasurementType mt, // If not using a hardcoded key, search for mt,vi,s,t and i instead.
|
|
|
|
ValueInformation vi,
|
|
|
|
StorageNr s,
|
|
|
|
TariffNr t,
|
|
|
|
IndexNr i,
|
|
|
|
int print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
string help,
|
|
|
|
function<void(Unit,double)> setValueFunc, // Use the SET macro above.
|
|
|
|
function<double(Unit)> getValueFunc); // Use the GET macro above.
|
|
|
|
|
2022-01-08 14:50:15 +00:00
|
|
|
#define SET_STRING_FUNC(varname) {[=](string s){varname = s;}}
|
|
|
|
#define GET_STRING_FUNC(varname) {[=](){return varname; }}
|
2022-01-06 17:28:22 +00:00
|
|
|
|
|
|
|
void addStringFieldWithExtractor(
|
|
|
|
string vname, // Name of value without unit, eg total
|
|
|
|
Quantity vquantity, // Value belongs to this quantity.
|
|
|
|
DifVifKey dif_vif_key, // You can hardocde a dif vif header here or use NoDifVifKey
|
|
|
|
MeasurementType mt, // If not using a hardcoded key, search for mt,vi,s,t and i instead.
|
|
|
|
ValueInformation vi,
|
|
|
|
StorageNr s,
|
|
|
|
TariffNr t,
|
|
|
|
IndexNr i,
|
|
|
|
int print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
string help,
|
|
|
|
function<void(string)> setValueFunc, // Use the SET_STRING macro above.
|
|
|
|
function<string()> getValueFunc); // Use the GET_STRING macro above.
|
|
|
|
|
2022-01-08 14:50:15 +00:00
|
|
|
void addStringFieldWithExtractorAndLookup(
|
|
|
|
string vname, // Name of value without unit, eg total
|
|
|
|
Quantity vquantity, // Value belongs to this quantity.
|
|
|
|
DifVifKey dif_vif_key, // You can hardocde a dif vif header here or use NoDifVifKey
|
|
|
|
MeasurementType mt, // If not using a hardcoded key, search for mt,vi,s,t and i instead.
|
|
|
|
ValueInformation vi,
|
|
|
|
StorageNr s,
|
|
|
|
TariffNr t,
|
|
|
|
IndexNr i,
|
|
|
|
int print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
string help,
|
|
|
|
function<void(string)> setValueFunc, // Use the SET_STRING macro above.
|
|
|
|
function<string()> getValueFunc, // Use the GET_STRING macro above.
|
|
|
|
Translate::Lookup lookup); // Translate the bits/indexes.
|
2022-01-06 17:28:22 +00:00
|
|
|
|
2021-03-08 16:14:03 +00:00
|
|
|
// The default implementation of poll does nothing.
|
|
|
|
// Override for mbus meters that need to be queried and likewise for C2/T2 wmbus-meters.
|
2021-03-14 08:41:35 +00:00
|
|
|
void poll(shared_ptr<BusManager> bus);
|
2021-12-07 22:56:29 +00:00
|
|
|
bool handleTelegram(AboutTelegram &about, vector<uchar> frame,
|
|
|
|
bool simulated, string *id, bool *id_match, Telegram *out_analyzed = NULL);
|
2019-05-04 14:37:35 +00:00
|
|
|
void printMeter(Telegram *t,
|
|
|
|
string *human_readable,
|
|
|
|
string *fields, char separator,
|
|
|
|
string *json,
|
2019-10-20 17:19:17 +00:00
|
|
|
vector<string> *envs,
|
2020-05-09 21:43:30 +00:00
|
|
|
vector<string> *more_json, // Add this json "key"="value" strings.
|
2022-01-25 19:10:38 +00:00
|
|
|
vector<string> *selected_fields, // Only print these fields.
|
|
|
|
bool pretty_print); // Insert newlines and indentation.
|
2021-08-08 15:16:52 +00:00
|
|
|
// Json fields cannot be modified expect by adding conversions.
|
|
|
|
// Json fields include all values except timestamp_ut, timestamp_utc, timestamp_lt
|
|
|
|
// since Json is assumed to be decoded by a program and the current timestamp which is the
|
|
|
|
// same as timestamp_utc, can always be decoded/recoded into local time or a unix timestamp.
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
FieldInfo *findFieldInfo(string vname);
|
|
|
|
string renderJsonOnlyDefaultUnit(string vname);
|
2021-12-31 11:27:57 +00:00
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
void processFieldExtractors(Telegram *t);
|
|
|
|
virtual void processContent(Telegram *t);
|
2019-05-04 15:38:10 +00:00
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
private:
|
|
|
|
|
2021-02-20 21:21:01 +00:00
|
|
|
int index_ {};
|
2021-12-31 11:27:57 +00:00
|
|
|
MeterType type_ {};
|
2022-01-02 12:08:36 +00:00
|
|
|
string driver_ {};
|
2022-01-06 17:28:22 +00:00
|
|
|
DriverName driver_name_;
|
2021-03-14 08:41:35 +00:00
|
|
|
string bus_ {};
|
2020-01-27 08:29:40 +00:00
|
|
|
MeterKeys meter_keys_ {};
|
|
|
|
ELLSecurityMode expected_ell_sec_mode_ {};
|
|
|
|
TPLSecurityMode expected_tpl_sec_mode_ {};
|
2018-03-05 10:29:25 +00:00
|
|
|
string name_;
|
2019-03-05 20:19:05 +00:00
|
|
|
vector<string> ids_;
|
2021-02-20 21:21:01 +00:00
|
|
|
string idsc_;
|
2019-03-05 20:19:05 +00:00
|
|
|
vector<function<void(Telegram*,Meter*)>> on_update_;
|
2018-03-05 10:29:25 +00:00
|
|
|
int num_updates_ {};
|
|
|
|
time_t datetime_of_update_ {};
|
2019-06-06 15:28:20 +00:00
|
|
|
LinkModeSet link_modes_ {};
|
2019-05-21 12:19:54 +00:00
|
|
|
vector<string> shell_cmdlines_;
|
2021-08-01 22:22:13 +00:00
|
|
|
vector<string> extra_constant_fields_;
|
2018-04-20 09:42:46 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::map<std::string,std::pair<int,std::string>> values_;
|
2019-05-04 11:55:52 +00:00
|
|
|
vector<Unit> conversions_;
|
2022-01-06 17:28:22 +00:00
|
|
|
vector<FieldInfo> prints_;
|
2020-05-09 21:43:30 +00:00
|
|
|
vector<string> fields_;
|
2018-03-05 10:29:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|