/* Copyright (C) 2018-2020 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 . */ #ifndef METERS_COMMON_IMPLEMENTATION_H_ #define METERS_COMMON_IMPLEMENTATION_H_ #include"meters.h" #include"units.h" #include #include struct Print { string vname; // Value name, like: total current previous target Quantity quantity; // Quantity: Energy, Volume Unit default_unit; // Default unit for above quantity: KWH, M3 function getValueDouble; // Callback to fetch the value from the meter. function getValueString; // Callback to fetch the value from the meter. string help; // Helpful information on this meters use of this value. bool field; // If true, print in hr/fields output. bool json; // If true, print in json and shell env variables. }; struct MeterCommonImplementation : public virtual Meter { vector ids(); vector fields(); string name(); MeterType type(); vector media(); WMBus *bus(); ELLSecurityMode expectedELLSecurityMode(); TPLSecurityMode expectedTPLSecurityMode(); string datetimeOfUpdateHumanReadable(); string datetimeOfUpdateRobot(); void onUpdate(function cb); int numUpdates(); bool isTelegramForMe(Telegram *t); MeterKeys *meterKeys(); std::vector getRecords(); double getRecordAsDouble(std::string record); uint16_t getRecordAsUInt16(std::string record); MeterCommonImplementation(WMBus *bus, MeterInfo &mi, MeterType type, int manufacturer); ~MeterCommonImplementation() = default; string meterName() { return toMeterName(type_); } protected: void triggerUpdate(Telegram *t); void addExpectedVersion(int version); void setExpectedELLSecurityMode(ELLSecurityMode dsm); void setExpectedTPLSecurityMode(TPLSecurityMode tsm); bool isExpectedVersion(int version); void addConversions(std::vector cs); void addShell(std::string cmdline); void addJson(std::string json); std::vector &shellCmdlines(); std::vector &additionalJsons(); void addMedia(int media); void addLinkMode(LinkMode lm); void addManufacturer(int m); void addPrint(string vname, Quantity vquantity, function getValueFunc, string help, bool field, bool json); void addPrint(string vname, Quantity vquantity, Unit unit, function getValueFunc, string help, bool field, bool json); void addPrint(string vname, Quantity vquantity, function getValueFunc, string help, bool field, bool json); bool handleTelegram(vector frame); void printMeter(Telegram *t, string *human_readable, string *fields, char separator, string *json, vector *envs, vector *more_json, // Add this json "key"="value" strings. vector *selected_fields); // Only print these fields. Json always everything. virtual void processContent(Telegram *t) = 0; private: MeterType type_ {}; MeterKeys meter_keys_ {}; ELLSecurityMode expected_ell_sec_mode_ {}; TPLSecurityMode expected_tpl_sec_mode_ {}; set expected_versions_; vector media_; set manufacturers_; string name_; vector ids_; WMBus *bus_ {}; vector> on_update_; int num_updates_ {}; time_t datetime_of_update_ {}; LinkModeSet link_modes_ {}; vector shell_cmdlines_; vector jsons_; protected: std::map> values_; vector conversions_; vector prints_; vector fields_; }; #endif