Remove global meter added callback. This is handled inside the printer instead.

master
Fredrik Öhrström 2024-11-24 17:27:02 +01:00
rodzic f9ec834734
commit 39c4bbe615
4 zmienionych plików z 6 dodań i 47 usunięć

Wyświetl plik

@ -599,40 +599,8 @@ bool start(Configuration *config)
// configures the devices according to the specification.
bus_manager_ = createBusManager(serial_manager_, meter_manager_);
// When a meter is added, print it, shell it, log it, etc.
meter_manager_->whenMeterAdded(
[&](Meter *meter)
{
vector<string> *shells = &config->new_meter_shells;
if (meter->shellCmdlinesMeterAdded().size() > 0) {
shells = &meter->shellCmdlinesMeterAdded();
}
if (shells->size() < 1) {
// Early return when no meter_shell configured by user
return;
}
vector<string> envs;
string id = "";
if (meter->addressExpressions().size() > 0)
{
id = meter->addressExpressions().back().id;
}
meter->createMeterEnv(id, &envs, &config->extra_constant_fields);
for (auto &s : *shells) {
vector<string> args;
args.push_back("-c");
args.push_back(s);
invokeShell("/bin/sh", args, envs);
}
}
);
// When a meter is updated, print it, shell it, log it, etc.
// The first update will trigger the add callback (metershell)
meter_manager_->whenMeterUpdated(
[&](Telegram *t,Meter *meter)
{

Wyświetl plik

@ -47,7 +47,6 @@ private:
vector<MeterInfo> meter_templates_;
vector<shared_ptr<Meter>> meters_;
vector<function<bool(AboutTelegram&,vector<uchar>)>> telegram_listeners_;
function<void(Meter *Meter)> on_meter_added_;
function<void(Telegram*t,Meter*)> on_meter_updated_;
public:
@ -64,11 +63,6 @@ public:
meter->setMeterManager(this);
}
void triggerMeterAdded(Meter *meter)
{
if (on_meter_added_) on_meter_added_(meter);
}
Meter *lastAddedMeter()
{
return meters_.back().get();
@ -298,11 +292,6 @@ public:
telegram_listeners_.push_back(cb);
}
void whenMeterAdded(std::function<void(Meter*)> cb)
{
on_meter_added_ = cb;
}
void whenMeterUpdated(std::function<void(Telegram*t,Meter*)> cb)
{
on_meter_updated_ = cb;

Wyświetl plik

@ -465,7 +465,6 @@ struct MeterManager
{
virtual void addMeterTemplate(MeterInfo &mi) = 0;
virtual void addMeter(shared_ptr<Meter> meter) = 0;
virtual void triggerMeterAdded(Meter *meter) = 0;
virtual Meter*lastAddedMeter() = 0;
virtual void removeAllMeters() = 0;
virtual void forEachMeter(std::function<void(Meter*)> cb) = 0;
@ -473,7 +472,6 @@ struct MeterManager
virtual bool hasAllMetersReceivedATelegram() = 0;
virtual bool hasMeters() = 0;
virtual void onTelegram(function<bool(AboutTelegram&,vector<uchar>)> cb) = 0;
virtual void whenMeterAdded(std::function<void(Meter*)> cb) = 0;
virtual void whenMeterUpdated(std::function<void(Telegram*t,Meter*)> cb) = 0;
virtual void pollMeters(shared_ptr<BusManager> bus) = 0;
virtual void analyzeEnabled(bool b, OutputFormat f, string force_driver, string key, bool verbose, int profile) = 0;

Wyświetl plik

@ -57,9 +57,13 @@ void Printer::print(Telegram *t, Meter *meter,
(new_meter_shell_cmdlines_.size() > 0 || meter->shellCmdlinesMeterAdded().size() > 0))
{
meter->markFirstTelegramReceived();
envs.push_back("METER_ADDED=true");
envs.push_back("METER_FIRST_TELEGRAM=true");
printNewMeterShells(meter, envs);
}
else
{
envs.push_back("METER_FIRST_TELEGRAM=false");
}
if (shell_cmdlines_.size() > 0 || meter->shellCmdlinesMeterUpdated().size() > 0) {
printShells(meter, envs);
printed = true;