wmbusmeters/src/meters.h

467 wiersze
18 KiB
C
Czysty Zwykły widok Historia

2018-04-01 06:53:37 +00:00
/*
Copyright (C) 2017-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/>.
*/
2017-08-09 10:00:11 +00:00
#ifndef METER_H_
#define METER_H_
2022-01-06 17:28:22 +00:00
#include"dvparser.h"
#include"formula.h"
2017-08-09 10:00:11 +00:00
#include"util.h"
#include"units.h"
2022-01-08 14:50:15 +00:00
#include"translatebits.h"
2017-08-09 10:00:11 +00:00
#include"wmbus.h"
2022-04-11 12:01:54 +00:00
#include<assert.h>
2020-09-07 08:36:39 +00:00
#include<functional>
#include<numeric>
#include<string>
2017-08-09 10:00:11 +00:00
#include<vector>
2022-04-11 12:01:54 +00:00
#define LIST_OF_METER_TYPES \
2022-01-06 17:28:22 +00:00
X(AutoMeter) \
X(UnknownMeter) \
X(DoorWindowDetector) \
X(ElectricityMeter) \
X(GasMeter) \
X(HeatCostAllocationMeter) \
X(HeatMeter) \
2022-06-18 21:03:18 +00:00
X(HeatCoolingMeter) \
X(PulseCounter) \
X(SmokeDetector) \
X(TempHygroMeter) \
X(WaterMeter) \
2022-05-21 20:13:40 +00:00
X(PressureSensor) \
enum class MeterType {
#define X(name) name,
LIST_OF_METER_TYPES
#undef X
};
2022-01-06 17:28:22 +00:00
struct DriverName
{
DriverName() {};
DriverName(string s) : name_(s) {};
2022-11-27 23:03:12 +00:00
const string &str() const { return name_; }
bool operator==(const DriverName &dn) const { return name_ == dn.name_; }
2022-01-06 17:28:22 +00:00
private:
string name_;
};
2020-09-04 09:31:49 +00:00
// Return a list of matching drivers, like: multical21
void detectMeterDrivers(int manufacturer, int media, int version, std::vector<std::string> *drivers);
2020-09-04 09:31:49 +00:00
// When entering the driver, check that the telegram is indeed known to be
// compatible with the driver(type), if not then print a warning.
2022-11-27 23:03:12 +00:00
bool isMeterDriverValid(DriverName driver_name, int manufacturer, int media, int version);
// For an unknown telegram, when analyzing check if the media type is reasonable in relation to the driver.
// Ie. do not try to decode a door sensor telegram with a water meter driver.
2022-11-27 23:03:12 +00:00
bool isMeterDriverReasonableForMedia(string driver_name, int media);
2020-09-04 09:31:49 +00:00
2022-11-15 19:37:28 +00:00
struct MeterInfo;
bool isValidKey(const string& key, MeterInfo &mt);
2017-08-09 10:00:11 +00:00
using namespace std;
typedef unsigned char uchar;
2022-04-26 09:44:00 +00:00
struct Address
{
// Example address: 12345678
// Or fully qualified: 12345678.M=PII.T=1b.V=01
// which means manufacturer triplet PII, type/media=0x1b, version=0x01
string id;
bool wildcard_used {}; // The id contains a *
bool mbus_primary {}; // Signals that the id is 0-250
uint16_t mfct {};
uchar type {};
uchar version {};
bool parse(string &s);
};
struct MeterInfo
{
string bus; // The bus used to communicate with this meter. A device like /dev/ttyUSB0 or an alias like BUS1.
// A bus can be an mbus or a wmbus dongle.
// The bus can be the empty string, which means that it will fallback to the first defined bus.
string name; // User specified name of this (group of) meters.
2022-01-06 17:28:22 +00:00
DriverName driver_name; // Will replace MeterDriver.
2021-03-08 07:40:48 +00:00
string extras; // Extra driver specific settings.
vector<string> ids; // Match expressions for ids.
string idsc; // Comma separated ids.
string key; // Decryption key.
LinkModeSet link_modes;
int bps {}; // For mbus communication you need to know the baud rate.
vector<string> shells;
2021-08-01 22:22:13 +00:00
vector<string> extra_constant_fields; // Additional static fields that are added to each message.
2022-11-01 20:15:28 +00:00
vector<string> extra_calculated_fields; // Additional field calculated using formulas.
vector<string> selected_fields; // Usually set to the default fields, but can be override in meter config.
2021-03-08 16:14:03 +00:00
// If this is a meter that needs to be polled.
2022-04-27 19:09:50 +00:00
int poll_interval; // Poll every x seconds.
2021-03-08 16:14:03 +00:00
MeterInfo()
{
}
string str();
2022-01-08 17:52:06 +00:00
DriverName driverName();
2022-11-27 23:03:12 +00:00
MeterInfo(string b, string n, string e, vector<string> i, string k, LinkModeSet lms, int baud, vector<string> &s, vector<string> &j, vector<string> &calcfs)
{
bus = b;
name = n;
extras = e,
ids = i;
idsc = toIdsCommaSeparated(ids);
key = k;
shells = s;
2021-08-01 22:22:13 +00:00
extra_constant_fields = j;
2022-11-01 20:15:28 +00:00
extra_calculated_fields = calcfs;
link_modes = lms;
bps = baud;
}
void clear()
{
bus = "";
name = "";
ids.clear();
idsc = "";
key = "";
shells.clear();
2021-08-01 22:22:13 +00:00
extra_constant_fields.clear();
2022-11-01 20:15:28 +00:00
extra_calculated_fields.clear();
link_modes.clear();
bps = 0;
}
bool parse(string name, string driver, string id, string key);
bool usesPolling();
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Dynamic loading of drivers based on the driver info.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct DriverDetect
{
uint16_t mfct;
uchar type;
uchar version;
};
struct DriverInfo
{
2022-01-06 17:28:22 +00:00
private:
DriverName name_; // auto, unknown, amiplus, lse_07_17, multical21 etc
vector<DriverName> name_aliases_; // Secondary names that will map to this driver.
2022-01-06 17:28:22 +00:00
LinkModeSet linkmodes_; // C1, T1, S1 or combinations thereof.
2022-05-21 12:22:56 +00:00
Translate::Lookup mfct_tpl_status_bits_; // Translate any mfct specific bits in tpl status.
2022-01-06 17:28:22 +00:00
MeterType type_; // Water, Electricity etc.
function<shared_ptr<Meter>(MeterInfo&,DriverInfo&di)> constructor_; // Invoke this to create an instance of the driver.
vector<DriverDetect> detect_;
vector<string> default_fields_;
int force_mfct_index_ = -1; // Used for meters not declaring mfct specific data using the dif 0f.
2022-01-06 17:28:22 +00:00
public:
DriverInfo() {};
void setName(std::string n) { name_ = n; }
void addNameAlias(std::string n) { name_aliases_.push_back(n); }
2022-01-06 17:28:22 +00:00
void setMeterType(MeterType t) { type_ = t; }
void setDefaultFields(string f) { default_fields_ = splitString(f, ','); }
2022-01-06 17:28:22 +00:00
void addLinkMode(LinkMode lm) { linkmodes_.addLinkMode(lm); }
void forceMfctIndex(int i) { force_mfct_index_ = i; }
2022-05-21 12:22:56 +00:00
void addMfctTPLStatusBits(Translate::Lookup lookup) { mfct_tpl_status_bits_ = lookup; }
2022-01-06 17:28:22 +00:00
void setConstructor(function<shared_ptr<Meter>(MeterInfo&,DriverInfo&)> c) { constructor_ = c; }
void addDetection(uint16_t mfct, uchar type, uchar ver) { detect_.push_back({ mfct, type, ver }); }
vector<DriverDetect> &detect() { return detect_; }
DriverName name() { return name_; }
vector<DriverName>& nameAliases() { return name_aliases_; }
2022-11-27 23:03:12 +00:00
bool hasDriverName(DriverName dn) {
if (name_ == dn) return true;
for (auto &i : name_aliases_) if (i == dn) return true;
return false;
}
2022-01-06 17:28:22 +00:00
MeterType type() { return type_; }
vector<string>& defaultFields() { return default_fields_; }
2022-01-06 17:28:22 +00:00
LinkModeSet linkModes() { return linkmodes_; }
2022-05-21 12:22:56 +00:00
Translate::Lookup &mfctTPLStatusBits() { return mfct_tpl_status_bits_; }
2022-01-06 17:28:22 +00:00
shared_ptr<Meter> construct(MeterInfo& mi) { return constructor_(mi, *this); }
bool detect(uint16_t mfct, uchar type, uchar version);
bool isValidMedia(uchar type);
bool isCloseEnoughMedia(uchar type);
int forceMfctIndex() { return force_mfct_index_; }
};
2022-01-06 17:28:22 +00:00
bool registerDriver(function<void(DriverInfo&di)> setup);
2022-11-01 11:14:48 +00:00
bool lookupDriverInfo(const string& driver, DriverInfo *di = NULL);
2022-01-06 17:28:22 +00:00
// Return the best driver match for a telegram.
DriverInfo pickMeterDriver(Telegram *t);
// Return true for mbus and S2/C2/T2 drivers.
2022-11-27 23:03:12 +00:00
bool driverNeedsPolling(DriverName& dn);
vector<DriverInfo*>& allDrivers();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
2022-01-06 17:28:22 +00:00
enum class VifScaling
{
2022-01-13 14:06:53 +00:00
None, // No auto scaling.
Auto, // Scale to normalized VIF unit (ie kwh, m3, m3h etc)
NoneSigned, // No auto scaling however assume the value is signed.
AutoSigned // Scale and assume the value is signed.
2022-01-06 17:28:22 +00:00
};
const char* toString(VifScaling s);
2022-04-11 12:01:54 +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.
OPTIONAL = 8, // If no data has arrived, then do not include this field in the json output.
2022-05-21 12:22:56 +00:00
REQUIRED = 16, // If no data has arrived, then print this field anyway with NaN or null.
DEPRECATED = 32, // This field is about to be removed or changed in a newer driver, which will have a new name.
STATUS = 64, // This is >the< status field and it should read OK of not error flags are set.
JOIN_TPL_STATUS = 128, // This text field also includes the tpl status decoding. multiple OK:s collapse to a single OK.
JOIN_INTO_STATUS = 256, // This text field is injected into the already defined status field. multiple OK:s collapse.
OFFICIAL = 512, // This field is listed as an official field for the driver.
HIDE = 1024 // This field is only used in calculations, do not print it!
2022-04-11 12:01:54 +00:00
};
struct PrintProperties
{
2022-05-21 12:22:56 +00:00
PrintProperties(int x) : props_(x) {}
2022-04-11 12:01:54 +00:00
bool hasJSON() { return props_ & PrintProperty::JSON; }
bool hasHIDE() { return props_ & PrintProperty::HIDE; }
2022-04-11 12:01:54 +00:00
bool hasFIELD() { return props_ & PrintProperty::FIELD; }
bool hasIMPORTANT() { return props_ & PrintProperty::IMPORTANT; }
bool hasOPTIONAL() { return props_ & PrintProperty::OPTIONAL; }
2022-05-21 12:22:56 +00:00
bool hasDEPRECATED() { return props_ & PrintProperty::DEPRECATED; }
bool hasSTATUS() { return props_ & PrintProperty::STATUS; }
2022-05-21 12:22:56 +00:00
bool hasJOINTPLSTATUS() { return props_ & PrintProperty::JOIN_TPL_STATUS; }
bool hasJOININTOSTATUS() { return props_ & PrintProperty::JOIN_INTO_STATUS; }
2022-04-11 12:01:54 +00:00
private:
int props_;
};
2022-01-06 17:28:22 +00:00
struct FieldInfo
{
~FieldInfo();
FieldInfo(int index,
string vname,
2022-01-06 17:28:22 +00:00
Quantity xuantity,
Unit default_unit,
VifScaling vif_scaling,
2022-04-17 13:54:01 +00:00
FieldMatcher matcher,
2022-01-06 17:28:22 +00:00
string help,
2022-04-11 12:01:54 +00:00
PrintProperties print_properties,
function<double(Unit)> get_numeric_value_override,
function<string()> get_string_value_override,
function<void(Unit,double)> set_numeric_value_override,
function<void(string)> set_string_value_override,
Translate::Lookup lookup,
Formula *formula
);
2022-01-06 17:28:22 +00:00
int index() { return index_; }
2022-01-06 17:28:22 +00:00
string vname() { return vname_; }
Quantity xuantity() { return xuantity_; }
Unit defaultUnit() { return default_unit_; }
VifScaling vifScaling() { return vif_scaling_; }
FieldMatcher& matcher() { return matcher_; }
2022-01-06 17:28:22 +00:00
string help() { return help_; }
2022-04-11 12:01:54 +00:00
PrintProperties printProperties() { return print_properties_; }
2022-01-06 17:28:22 +00:00
double getNumericValueOverride(Unit u) { if (get_numeric_value_override_) return get_numeric_value_override_(u); else return -12345678; }
bool hasGetNumericValueOverride() { return get_numeric_value_override_ != NULL; }
string getStringValueOverride() { if (get_string_value_override_) return get_string_value_override_(); else return "?"; }
bool hasGetStringValueOverride() { return get_string_value_override_ != NULL; }
2022-01-06 17:28:22 +00:00
void setNumericValueOverride(Unit u, double v) { if (set_numeric_value_override_) set_numeric_value_override_(u, v); }
bool hasSetNumericValueOverride() { return set_numeric_value_override_ != NULL; }
void setStringValueOverride(string v) { if (set_string_value_override_) set_string_value_override_(v); }
bool hasSetStringValueOverride() { return set_string_value_override_ != NULL; }
2022-01-06 17:28:22 +00:00
2022-04-17 13:54:01 +00:00
bool extractNumeric(Meter *m, Telegram *t, DVEntry *dve = NULL);
bool extractString(Meter *m, Telegram *t, DVEntry *dve = NULL);
bool hasMatcher();
bool hasFormula();
2022-04-17 13:54:01 +00:00
bool matches(DVEntry *dve);
void performExtraction(Meter *m, Telegram *t, DVEntry *dve);
2022-01-06 17:28:22 +00:00
void performCalculation(Meter *m);
string renderJsonOnlyDefaultUnit(Meter *m);
string renderJson(Meter *m, DVEntry *dve);
string renderJsonText(Meter *m);
// Render the field name based on the actual field from the telegram.
// A FieldInfo can be declared to handle any number of storage fields of a certain range.
// The vname is then a pattern total_at_month_{storage_counter} that gets translated into
// total_at_month_2 (for the dventry with storage nr 2.)
string generateFieldNameWithUnit(DVEntry *dve);
string generateFieldNameNoUnit(DVEntry *dve);
// Check if the meter object stores a value for this field.
bool hasValue(Meter *m);
2022-01-08 14:50:15 +00:00
Translate::Lookup& lookup() { return lookup_; }
string str();
2022-01-06 17:28:22 +00:00
private:
int index_; // The field infos for a meter are ordered.
string vname_; // Value name, like: total current previous target, ie no unit suffix.
2022-01-06 17:28:22 +00:00
Quantity xuantity_; // Quantity: Energy, Volume
Unit default_unit_; // Default unit for above quantity: KWH, M3
VifScaling vif_scaling_;
2022-04-17 13:54:01 +00:00
FieldMatcher matcher_;
2022-01-06 17:28:22 +00:00
string help_; // Helpful information on this meters use of this value.
2022-04-11 12:01:54 +00:00
PrintProperties print_properties_;
2022-01-06 17:28:22 +00:00
// Normally the values are stored inside the meter object using its setNumeric/setString/getNumeric/getString
// But for special fields we can override this default location with these setters/getters.
function<double(Unit)> get_numeric_value_override_; // Callback to fetch the value from the meter.
function<string()> get_string_value_override_; // Callback to fetch the value from the meter.
function<void(Unit,double)> set_numeric_value_override_; // Call back to set the value in the c++ object
function<void(string)> set_string_value_override_; // Call back to set the value string in the c++ object
// Lookup bits to strings.
2022-01-08 14:50:15 +00:00
Translate::Lookup lookup_;
// For calculated fields.
shared_ptr<Formula> formula_;
// For the generated field name.
shared_ptr<StringInterpolator> field_name_;
// If the field name template could not be parsed.
bool valid_field_name_ {};
};
struct BusManager;
2019-05-04 17:56:17 +00:00
struct Meter
{
// Meters are instantiated on the fly from a template, when a telegram arrives
// and no exact meter exists. Index 1 is the first meter created etc.
virtual int index() = 0;
virtual void setIndex(int i) = 0;
2021-03-08 16:14:03 +00:00
// Use this bus to send messages to the meter.
virtual string bus() = 0;
2020-05-09 21:43:30 +00:00
// This meter listens to these ids.
virtual vector<string> &ids() = 0;
// Comma separated ids.
virtual string idsc() = 0;
2020-05-09 21:43:30 +00:00
// This meter can report these fields, like total_m3, temp_c.
virtual vector<FieldInfo> &fieldInfos() = 0;
virtual vector<string> &extraConstantFields() = 0;
// Either the default fields specified in the driver, or override fields in the meter configuration file.
virtual vector<string> &selectedFields() = 0;
virtual void setSelectedFields(vector<string> &f) = 0;
2017-08-09 10:00:11 +00:00
virtual string name() = 0;
2022-11-27 23:03:12 +00:00
virtual DriverName driverName() = 0;
virtual string datetimeOfUpdateHumanReadable() = 0;
virtual string datetimeOfUpdateRobot() = 0;
virtual string unixTimestampOfUpdate() = 0;
2022-04-27 19:09:50 +00:00
virtual time_t timestampLastUpdate() = 0;
virtual void setPollInterval(time_t interval) = 0;
virtual time_t pollInterval() = 0;
virtual bool usesPolling() = 0;
virtual void setNumericValue(string vname, Unit u, double v) = 0;
virtual void setNumericValue(FieldInfo *fi, DVEntry *dve, Unit u, double v) = 0;
virtual double getNumericValue(string vname, Unit u) = 0;
virtual double getNumericValue(FieldInfo *fi, Unit u) = 0;
virtual void setStringValue(FieldInfo *fi, std::string v) = 0;
virtual std::string getStringValue(FieldInfo *fi) = 0;
2022-05-21 12:22:56 +00:00
virtual std::string decodeTPLStatusByte(uchar sts) = 0;
2020-09-07 08:36:39 +00:00
virtual void onUpdate(std::function<void(Telegram*t,Meter*)> cb) = 0;
virtual int numUpdates() = 0;
2017-09-02 21:26:57 +00:00
virtual void printMeter(Telegram *t,
2019-03-05 17:38:54 +00:00
string *human_readable,
string *fields, char separator,
string *json,
vector<string> *envs,
2020-05-09 21:43:30 +00:00
vector<string> *more_json,
2022-01-25 19:10:38 +00:00
vector<string> *selected_fields,
bool pretty_print_json) = 0;
// The handleTelegram expects an input_frame where the DLL crcs have been removed.
2020-09-07 08:36:39 +00:00
// Returns true of this meter handled this telegram!
// Sets id_match to true, if there was an id match, even though the telegram could not be properly handled.
virtual bool handleTelegram(AboutTelegram &about, vector<uchar> input_frame,
bool simulated, string *id, bool *id_match, Telegram *out_t = NULL) = 0;
2020-01-27 08:29:40 +00:00
virtual MeterKeys *meterKeys() = 0;
2022-11-01 20:15:28 +00:00
virtual void addExtraCalculatedField(std::string ecf) = 0;
virtual void addShell(std::string cmdline) = 0;
virtual vector<string> &shellCmdlines() = 0;
virtual void poll(shared_ptr<BusManager> bus) = 0;
virtual FieldInfo *findFieldInfo(string vname, Quantity xuantity) = 0;
virtual string renderJsonOnlyDefaultUnit(string vname, Quantity xuantity) = 0;
2022-01-06 17:28:22 +00:00
virtual string debugValues() = 0;
virtual ~Meter() = default;
};
2020-09-07 08:36:39 +00:00
struct MeterManager
{
virtual void addMeterTemplate(MeterInfo &mi) = 0;
virtual void addMeter(shared_ptr<Meter> meter) = 0;
virtual Meter*lastAddedMeter() = 0;
2020-09-07 08:36:39 +00:00
virtual void removeAllMeters() = 0;
virtual void forEachMeter(std::function<void(Meter*)> cb) = 0;
2020-10-14 18:59:14 +00:00
virtual bool handleTelegram(AboutTelegram &about, vector<uchar> data, bool simulated) = 0;
2020-09-07 08:36:39 +00:00
virtual bool hasAllMetersReceivedATelegram() = 0;
2020-09-08 12:55:01 +00:00
virtual bool hasMeters() = 0;
2022-03-06 21:42:44 +00:00
virtual void onTelegram(function<bool(AboutTelegram&,vector<uchar>)> cb) = 0;
virtual void whenMeterUpdated(std::function<void(Telegram*t,Meter*)> cb) = 0;
virtual void pollMeters(shared_ptr<BusManager> bus) = 0;
2022-02-06 17:49:55 +00:00
virtual void analyzeEnabled(bool b, OutputFormat f, string force_driver, string key, bool verbose) = 0;
2021-12-07 18:51:26 +00:00
virtual void analyzeTelegram(AboutTelegram &about, vector<uchar> &input_frame, bool simulated) = 0;
2020-09-07 08:36:39 +00:00
virtual ~MeterManager() = default;
};
shared_ptr<MeterManager> createMeterManager(bool daemon);
2020-09-07 08:36:39 +00:00
2022-01-06 17:28:22 +00:00
const char *toString(MeterType type);
string toString(DriverInfo &driver);
2022-11-01 11:14:48 +00:00
LinkModeSet toMeterLinkModeSet(const string& driver);
2020-11-14 09:48:49 +00:00
2021-02-20 10:08:23 +00:00
struct Configuration;
struct MeterInfo;
shared_ptr<Meter> createMeter(MeterInfo *mi);
2021-02-20 10:08:23 +00:00
2017-08-09 10:00:11 +00:00
#endif