2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2022-02-12 14:28:17 +00:00
|
|
|
Copyright (C) 2018-2022 Fredrik Öhrström (gpl-3.0-or-later)
|
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"
|
2022-05-01 18:54:12 +00:00
|
|
|
#include"threads.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-11-10 21:06:51 +00:00
|
|
|
|
|
|
|
// Values in a meter are stored based on vname + Quantity.
|
|
|
|
// I.e. you can have a total_m3 and a total_kwh even though they share the same "total" vname
|
|
|
|
// since they have two different quantities (Volume and Energy).
|
|
|
|
// The field total_l refers to the same field storage in the meter as total_m3.
|
|
|
|
// If a wacko meter sends different values, one m3 and one l. then you
|
|
|
|
// have to name the fields using different vnames.
|
2022-04-22 15:28:57 +00:00
|
|
|
struct NumericField
|
|
|
|
{
|
|
|
|
Unit unit {};
|
|
|
|
double value {};
|
|
|
|
FieldInfo *field_info {};
|
2023-08-27 14:48:40 +00:00
|
|
|
DVEntry dv_entry {};
|
2022-04-22 15:28:57 +00:00
|
|
|
|
|
|
|
NumericField() {}
|
|
|
|
NumericField(Unit u, double v, FieldInfo *f) : unit(u), value(v), field_info(f) {}
|
2023-08-27 14:48:40 +00:00
|
|
|
NumericField(Unit u, double v, FieldInfo *f, DVEntry &dve) : unit(u), value(v), field_info(f), dv_entry(dve) {}
|
2022-04-22 15:28:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct StringField
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
FieldInfo *field_info {};
|
|
|
|
|
|
|
|
StringField() {}
|
|
|
|
StringField(std::string v, FieldInfo *f) : value(v), field_info(f) {}
|
|
|
|
};
|
|
|
|
|
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();
|
2022-04-23 06:20:22 +00:00
|
|
|
vector<FieldInfo> &fieldInfos();
|
2022-11-24 15:22:37 +00:00
|
|
|
vector<string> &extraConstantFields();
|
2018-03-05 10:29:25 +00:00
|
|
|
string name();
|
2022-01-06 17:28:22 +00:00
|
|
|
DriverName driverName();
|
2023-01-10 19:52:14 +00:00
|
|
|
bool hasProcessContent();
|
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();
|
2022-04-27 19:09:50 +00:00
|
|
|
time_t timestampLastUpdate();
|
|
|
|
void setPollInterval(time_t interval);
|
|
|
|
time_t pollInterval();
|
2022-05-01 18:54:12 +00:00
|
|
|
bool usesPolling();
|
2022-11-01 20:15:28 +00:00
|
|
|
void addExtraCalculatedField(std::string ef);
|
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
|
|
|
|
2023-01-10 19:52:14 +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;
|
|
|
|
|
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-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);
|
2022-12-11 17:29:45 +00:00
|
|
|
void setMfctTPLStatusBits(Translate::Lookup &lookup);
|
2022-05-21 12:22:56 +00:00
|
|
|
|
2022-04-11 12:01:54 +00:00
|
|
|
|
2022-04-19 07:43:10 +00:00
|
|
|
void addNumericFieldWithExtractor(
|
2022-04-23 14:28:54 +00:00
|
|
|
string vname, // Name of value without unit, eg "total" "total_month{storagenr}"
|
|
|
|
string help, // Information about this field.
|
2022-04-19 07:43:10 +00:00
|
|
|
PrintProperties print_properties, // Should this be printed by default in fields,json and hr.
|
2022-04-23 14:28:54 +00:00
|
|
|
Quantity vquantity, // Value belongs to this quantity, this quantity determines the default unit.
|
|
|
|
VifScaling vif_scaling, // How should any Vif value be scaled.
|
2022-06-24 08:10:14 +00:00
|
|
|
FieldMatcher matcher,
|
2023-08-22 20:28:04 +00:00
|
|
|
Unit display_unit = Unit::Unknown, // If specified use this unit for the json field instead instead of the default unit.
|
|
|
|
double scale = 1.0); // A hard coded extra scale factor. Useful for manufacturer specific values.
|
2022-10-10 19:43:11 +00:00
|
|
|
|
|
|
|
void addNumericFieldWithCalculator(
|
|
|
|
string vname, // Name of value without unit, eg "total" "total_month{storagenr}"
|
|
|
|
string help, // Information about this field.
|
|
|
|
PrintProperties print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
Quantity vquantity, // Value belongs to this quantity, this quantity determines the default unit.
|
|
|
|
string formula, // The formula can reference the other fields and + them together.
|
2022-12-12 16:41:22 +00:00
|
|
|
Unit display_unit = Unit::Unknown); // If specified use this unit for the json field instead instead of the default unit.
|
2022-04-19 07:43:10 +00:00
|
|
|
|
2022-11-26 14:39:29 +00:00
|
|
|
void addNumericFieldWithCalculatorAndMatcher(
|
|
|
|
string vname, // Name of value without unit, eg "total" "total_month{storagenr}"
|
|
|
|
string help, // Information about this field.
|
|
|
|
PrintProperties print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
Quantity vquantity, // Value belongs to this quantity, this quantity determines the default unit.
|
|
|
|
string formula, // The formula can reference the other fields and + them together.
|
|
|
|
FieldMatcher matcher, // We can generate a calculated field per match.
|
2022-12-12 16:41:22 +00:00
|
|
|
Unit display_unit = Unit::Unknown); // If specified use this unit for the json field instead instead of the default unit.
|
2022-11-26 14:39:29 +00:00
|
|
|
|
2022-11-23 17:42:59 +00:00
|
|
|
void addNumericField(
|
|
|
|
string vname, // Name of value without unit, eg total
|
|
|
|
Quantity vquantity, // Value belongs to this quantity.
|
|
|
|
PrintProperties print_properties, // Should this be printed by default in fields,json and hr.
|
|
|
|
string help,
|
2022-12-12 16:41:22 +00:00
|
|
|
Unit display_unit = Unit::Unknown); // If specified use this unit for the json field instead instead of the default unit.
|
2022-11-23 17:42:59 +00:00
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
void addStringFieldWithExtractor(
|
2022-04-23 14:28:54 +00:00
|
|
|
string vname,
|
|
|
|
string help,
|
|
|
|
PrintProperties print_properties,
|
|
|
|
FieldMatcher matcher);
|
|
|
|
|
2022-04-23 16:32:30 +00:00
|
|
|
void addStringFieldWithExtractorAndLookup(
|
2022-04-23 16:23:04 +00:00
|
|
|
string vname,
|
|
|
|
string help,
|
|
|
|
PrintProperties print_properties,
|
|
|
|
FieldMatcher matcher,
|
|
|
|
Translate::Lookup lookup); // Translate the bits/indexes.
|
|
|
|
|
2022-06-19 09:31:28 +00:00
|
|
|
// Used only for status field from tpl_status only.
|
|
|
|
void addStringField(
|
|
|
|
string vname,
|
|
|
|
string help,
|
|
|
|
PrintProperties print_properties);
|
|
|
|
|
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 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-10-10 19:43:11 +00:00
|
|
|
FieldInfo *findFieldInfo(string vname, Quantity xuantity);
|
|
|
|
string renderJsonOnlyDefaultUnit(string vname, Quantity xuantity);
|
2022-11-23 17:42:59 +00:00
|
|
|
string debugValues();
|
2021-12-31 11:27:57 +00:00
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
void processFieldExtractors(Telegram *t);
|
2022-10-10 19:43:11 +00:00
|
|
|
void processFieldCalculators();
|
|
|
|
|
2022-01-06 17:28:22 +00:00
|
|
|
virtual void processContent(Telegram *t);
|
2019-05-04 15:38:10 +00:00
|
|
|
|
2022-11-23 17:42:59 +00:00
|
|
|
void setNumericValue(string vname, Unit u, double v);
|
2022-11-24 15:22:37 +00:00
|
|
|
void setNumericValue(FieldInfo *fi, DVEntry *dve, Unit u, double v);
|
|
|
|
double getNumericValue(string vname, Unit u);
|
2022-04-23 06:20:22 +00:00
|
|
|
double getNumericValue(FieldInfo *fi, Unit u);
|
2022-11-27 00:53:02 +00:00
|
|
|
void setStringValue(string vname, std::string v);
|
2022-04-23 06:20:22 +00:00
|
|
|
void setStringValue(FieldInfo *fi, std::string v);
|
|
|
|
std::string getStringValue(FieldInfo *fi);
|
2022-09-10 06:44:55 +00:00
|
|
|
|
|
|
|
// Check if the meter has received a value for this field.
|
|
|
|
bool hasValue(FieldInfo *fi);
|
|
|
|
bool hasNumericValue(FieldInfo *fi);
|
|
|
|
bool hasStringValue(FieldInfo *fi);
|
|
|
|
|
2022-05-21 12:22:56 +00:00
|
|
|
std::string decodeTPLStatusByte(uchar sts);
|
2022-04-19 07:10:14 +00:00
|
|
|
|
2022-11-12 15:30:10 +00:00
|
|
|
void addOptionalCommonFields(string fields);
|
|
|
|
void addOptionalFlowRelatedFields(string fields);
|
2022-11-16 16:34:29 +00:00
|
|
|
void addHCARelatedFields(string fields);
|
2022-06-18 21:03:18 +00:00
|
|
|
|
2022-09-01 15:44:59 +00:00
|
|
|
vector<string> &selectedFields() { return selected_fields_; }
|
|
|
|
void setSelectedFields(vector<string> &f) { selected_fields_ = f; }
|
|
|
|
|
2022-09-28 19:20:43 +00:00
|
|
|
void forceMfctIndex(int i) { force_mfct_index_ = i; }
|
|
|
|
|
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-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_ {};
|
2023-01-02 14:28:18 +00:00
|
|
|
time_t datetime_of_poll_ {};
|
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_;
|
2022-04-27 19:09:50 +00:00
|
|
|
time_t poll_interval_ {};
|
2022-05-21 12:22:56 +00:00
|
|
|
Translate::Lookup mfct_tpl_status_bits_ = NoLookup;
|
2022-09-28 19:20:43 +00:00
|
|
|
int force_mfct_index_ = -1;
|
2023-01-10 19:52:14 +00:00
|
|
|
bool has_process_content_ = false;
|
2018-04-20 09:42:46 +00:00
|
|
|
|
|
|
|
protected:
|
2022-09-28 19:20:43 +00:00
|
|
|
|
2022-04-23 06:20:22 +00:00
|
|
|
vector<FieldInfo> field_infos_;
|
|
|
|
vector<string> field_names_;
|
2022-09-01 15:44:59 +00:00
|
|
|
// Defaults to a setting specified in the driver. Can be overridden in the meter file.
|
|
|
|
// There is also a global selected_fields that can be set on the command line or in the conf file.
|
|
|
|
vector<string> selected_fields_;
|
2022-04-19 07:10:14 +00:00
|
|
|
// Map difvif key to hex values from telegrams.
|
|
|
|
std::map<std::string,std::pair<int,std::string>> hex_values_;
|
2023-08-27 14:48:40 +00:00
|
|
|
// Map field name including unit (total_volume_m3) to numeric value.
|
2022-04-23 21:02:17 +00:00
|
|
|
std::map<pair<std::string,Quantity>,NumericField> numeric_values_;
|
2022-04-19 07:10:14 +00:00
|
|
|
// Map field name (at_date) to string value.
|
2022-04-22 15:28:57 +00:00
|
|
|
std::map<std::string,StringField> string_values_;
|
2022-05-21 12:22:56 +00:00
|
|
|
// Used to block next poll, until this poll has received a respones.
|
2022-05-01 18:54:12 +00:00
|
|
|
Semaphore waiting_for_poll_response_sem_;
|
2018-03-05 10:29:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|