Skip to content

Commit

Permalink
Polish service container logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Aug 16, 2024
1 parent 858a64f commit 973b89f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/devices/UglyDucklingMk4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class UglyDucklingMk4 : public DeviceDefinition<Mk4Config> {
};

const ServiceRef<CurrentSensingMotorDriver> motor { "motor", motorDriver };
const std::list<ServiceRef<CurrentSensingMotorDriver>> motors { motor };
const ServiceContainer<CurrentSensingMotorDriver> motors { { motor } };

ValveFactory valveFactory { motors, ValveControlStrategyType::NormallyClosed };
FlowMeterFactory flowMeterFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/devices/UglyDucklingMk5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class UglyDucklingMk5 : public DeviceDefinition<Mk5Config> {

const ServiceRef<CurrentSensingMotorDriver> motorA { "a", motorADriver };
const ServiceRef<CurrentSensingMotorDriver> motorB { "b", motorBDriver };
const std::list<ServiceRef<CurrentSensingMotorDriver>> motors { motorA, motorB };
const ServiceContainer<CurrentSensingMotorDriver> motors { { motorA, motorB } };

ValveFactory valveFactory { motors, ValveControlStrategyType::Latching };
FlowMeterFactory flowMeterFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/devices/UglyDucklingMk6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class UglyDucklingMk6 : public DeviceDefinition<Mk6Config> {

const ServiceRef<CurrentSensingMotorDriver> motorA { "a", motorADriver };
const ServiceRef<CurrentSensingMotorDriver> motorB { "b", motorBDriver };
const std::list<ServiceRef<CurrentSensingMotorDriver>> motors { motorA, motorB };
const ServiceContainer<CurrentSensingMotorDriver> motors { { motorA, motorB } };

ValveFactory valveFactory { motors, ValveControlStrategyType::Latching };
FlowMeterFactory flowMeterFactory;
Expand Down
3 changes: 1 addition & 2 deletions src/peripherals/Peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ class ServiceContainer {
: services(services) {
}

protected:
T& findService(const String& name) {
T& findService(const String& name) const {
// If there's only one service and no name is specified, use it
if (name.isEmpty() && services.size() == 1) {
return services.front().get();
Expand Down
13 changes: 8 additions & 5 deletions src/peripherals/chicken_door/ChickenDoor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,16 @@ class NoLightSensorComponent : public LightSensorComponent {
};

class ChickenDoorFactory
: public PeripheralFactory<ChickenDoorDeviceConfig, ChickenDoorConfig>,
protected ServiceContainer<CurrentSensingMotorDriver> {
: public PeripheralFactory<ChickenDoorDeviceConfig, ChickenDoorConfig> {
public:
ChickenDoorFactory(const std::list<ServiceRef<CurrentSensingMotorDriver>>& motors)
ChickenDoorFactory(
const ServiceContainer<CurrentSensingMotorDriver>& motors)
: PeripheralFactory<ChickenDoorDeviceConfig, ChickenDoorConfig>("chicken-door")
, ServiceContainer<CurrentSensingMotorDriver>(motors) {
, motors(motors) {
}

unique_ptr<Peripheral<ChickenDoorConfig>> createPeripheral(const String& name, const ChickenDoorDeviceConfig& deviceConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot, PeripheralServices& services) override {
CurrentSensingMotorDriver& motor = findService(deviceConfig.motor.get());
CurrentSensingMotorDriver& motor = motors.findService(deviceConfig.motor.get());
auto lightSensorType = deviceConfig.lightSensor.get().type.get();
try {
if (lightSensorType == "bh1750") {
Expand All @@ -449,6 +449,9 @@ class ChickenDoorFactory
return std::make_unique<ChickenDoor<NoLightSensorComponent>>(name, mqttRoot, services.i2c, 0x00, services.sleepManager, services.switches, motor, deviceConfig);
}
}

private:
const ServiceContainer<CurrentSensingMotorDriver>& motors;
};

} // namespace farmhub::peripherals::chicken_door
12 changes: 7 additions & 5 deletions src/peripherals/flow_control/FlowControl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,20 @@ class FlowControlDeviceConfig
};

class FlowControlFactory
: public PeripheralFactory<FlowControlDeviceConfig, FlowControlConfig, ValveControlStrategyType>,
protected ServiceContainer<CurrentSensingMotorDriver> {
: public PeripheralFactory<FlowControlDeviceConfig, FlowControlConfig, ValveControlStrategyType> {
public:
FlowControlFactory(
const std::list<ServiceRef<CurrentSensingMotorDriver>>& motors,
const ServiceContainer<CurrentSensingMotorDriver>& motors,
ValveControlStrategyType defaultStrategy)
: PeripheralFactory<FlowControlDeviceConfig, FlowControlConfig, ValveControlStrategyType>("flow-control", defaultStrategy)
, ServiceContainer<CurrentSensingMotorDriver>(motors) {
, motors(motors) {
}

unique_ptr<Peripheral<FlowControlConfig>> createPeripheral(const String& name, const FlowControlDeviceConfig& deviceConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot, PeripheralServices& services) override {
const ValveDeviceConfig& valveConfig = deviceConfig.valve.get();
const FlowMeterDeviceConfig& flowMeterConfig = deviceConfig.flowMeter.get();

PwmMotorDriver& targetMotor = findService(valveConfig.motor.get());
PwmMotorDriver& targetMotor = motors.findService(valveConfig.motor.get());
ValveControlStrategy* strategy;
try {
strategy = createValveControlStrategy(
Expand All @@ -109,6 +108,9 @@ class FlowControlFactory
flowMeterConfig.qFactor.get(),
flowMeterConfig.measurementFrequency.get());
}

private:
const ServiceContainer<CurrentSensingMotorDriver>& motors;
};

} // namespace farmhub::peripherals::flow_control
12 changes: 7 additions & 5 deletions src/peripherals/valve/Valve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ class Valve
};

class ValveFactory
: public PeripheralFactory<ValveDeviceConfig, ValveConfig, ValveControlStrategyType>,
protected ServiceContainer<CurrentSensingMotorDriver> {
: public PeripheralFactory<ValveDeviceConfig, ValveConfig, ValveControlStrategyType> {
public:
ValveFactory(
const std::list<ServiceRef<CurrentSensingMotorDriver>>& motors,
const ServiceContainer<CurrentSensingMotorDriver>& motors,
ValveControlStrategyType defaultStrategy)
: PeripheralFactory<ValveDeviceConfig, ValveConfig, ValveControlStrategyType>("valve", defaultStrategy)
, ServiceContainer<CurrentSensingMotorDriver>(motors) {
, motors(motors) {
}

unique_ptr<Peripheral<ValveConfig>> createPeripheral(const String& name, const ValveDeviceConfig& deviceConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot, PeripheralServices& services) override {
CurrentSensingMotorDriver& targetMotor = findService(deviceConfig.motor.get());
CurrentSensingMotorDriver& targetMotor = motors.findService(deviceConfig.motor.get());
ValveControlStrategy* strategy;
try {
strategy = createValveControlStrategy(
Expand All @@ -80,6 +79,9 @@ class ValveFactory
}
return make_unique<Valve>(name, services.sleepManager, targetMotor, *strategy, mqttRoot);
}

private:
const ServiceContainer<CurrentSensingMotorDriver>& motors;
};

} // namespace farmhub::peripherals::valve

0 comments on commit 973b89f

Please sign in to comment.