Skip to content

Commit

Permalink
#119 feat: Add config change listener to SensorStateService
Browse files Browse the repository at this point in the history
  • Loading branch information
TWilkin committed May 7, 2024
1 parent 416616b commit 799ccf3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
7 changes: 5 additions & 2 deletions services/api/src/services/ApiSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class ApiSocketService {
this.device = new DeviceListener(configRetriever, mqttService, this);

this.sensors = configService.sensors.map(
(sensor) => new SensorListener(mqttService, sensor, this),
(sensor) => new SensorListener(configRetriever, mqttService, sensor, this),
);
}

Expand Down Expand Up @@ -144,11 +144,12 @@ class DeviceListener extends DeviceStateListener {

class SensorListener extends SensorStateListener {
constructor(
configRetriever: ConfigRetrieverService,
mqttService: MqttService,
sensor: ISensor,
private readonly socketService: ApiSocketService,
) {
super(mqttService, sensor);
super(configRetriever, mqttService, sensor);
}

protected onSensorStateMessage(sensorName: string, state: string, timestamp?: number): void {
Expand All @@ -172,4 +173,6 @@ class SensorListener extends SensorStateListener {
): void {
this.socketService.onBatteryMessage("sensor", sensorName, value, charging, timestamp);
}

protected onConfigChange(_: ConfigFileType) {}
}
6 changes: 5 additions & 1 deletion services/api/src/services/SensorStateService.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Message, MqttConsumer, MqttService } from "@powerpi/common";
import { ConfigRetrieverService, Message, MqttConsumer, MqttService } from "@powerpi/common";
import { capture, instance, mock, resetCalls, when } from "ts-mockito";
import ConfigService from "./ConfigService";
import SensorStateService from "./SensorStateService";
import { BatteryMessage } from "./listeners/BatteryStateListener";
import { EventMessage } from "./listeners/SensorStateListener";

const mockedConfigService = mock<ConfigService>();
const mockedConfigRetrieverService = mock<ConfigRetrieverService>();
const mockedMqttService = mock<MqttService>();

describe("SensorStateService", () => {
Expand Down Expand Up @@ -55,6 +56,7 @@ describe("SensorStateService", () => {

subject = new SensorStateService(
instance(mockedConfigService),
instance(mockedConfigRetrieverService),
instance(mockedMqttService),
);

Expand Down Expand Up @@ -88,6 +90,7 @@ describe("SensorStateService", () => {
test("none", () => {
subject = new SensorStateService(
instance(mockedConfigService),
instance(mockedConfigRetrieverService),
instance(mockedMqttService),
);

Expand All @@ -101,6 +104,7 @@ describe("SensorStateService", () => {

subject = new SensorStateService(
instance(mockedConfigService),
instance(mockedConfigRetrieverService),
instance(mockedMqttService),
);

Expand Down
17 changes: 13 additions & 4 deletions services/api/src/services/SensorStateService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISensor } from "@powerpi/common";
import { ConfigFileType, ConfigRetrieverService, ISensor } from "@powerpi/common";
import { Service } from "@tsed/di";
import ConfigService from "./ConfigService";
import MqttService from "./MqttService";
Expand All @@ -10,6 +10,7 @@ export default class SensorStateService {

constructor(
private readonly config: ConfigService,
private readonly configRetriever: ConfigRetrieverService,
private readonly mqttService: MqttService,
) {
this._sensors = undefined;
Expand All @@ -31,14 +32,18 @@ export default class SensorStateService {

private initialise() {
this._sensors = this.config.sensors.map(
(sensor) => new SensorConsumer(this.mqttService, sensor),
(sensor) => new SensorConsumer(this.configRetriever, this.mqttService, sensor),
);
}
}

class SensorConsumer extends SensorStateListener {
constructor(mqttService: MqttService, sensor: ISensor) {
super(mqttService, sensor);
constructor(
configRetriever: ConfigRetrieverService,
mqttService: MqttService,
sensor: ISensor,
) {
super(configRetriever, mqttService, sensor);
}

protected onSensorStateMessage(_: string, __: string, ___?: number): void {
Expand All @@ -52,4 +57,8 @@ class SensorConsumer extends SensorStateListener {
protected onSensorBatteryMessage(_: string, __: number, ___?: number): void {
return;
}

protected onConfigChange(_: ConfigFileType): void {
return;
}
}
19 changes: 18 additions & 1 deletion services/api/src/services/listeners/SensorStateListener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ISensor, Message, MqttConsumer } from "@powerpi/common";
import {
ConfigFileType,
ConfigRetrieverService,
ISensor,
Message,
MqttConsumer,
} from "@powerpi/common";
import { Sensor } from "@powerpi/common-api";
import MqttService from "../MqttService";
import BatteryStateListener, { BatteryMessage } from "./BatteryStateListener";
Expand All @@ -16,6 +22,7 @@ export default abstract class SensorStateListener
private readonly _sensor: Sensor;

constructor(
private readonly configRetriever: ConfigRetrieverService,
private readonly mqttService: MqttService,
sensor: ISensor,
) {
Expand Down Expand Up @@ -51,6 +58,14 @@ export default abstract class SensorStateListener
message: (_: string, __: string, ___: string, message: BatteryMessage) =>
this.batteryMessage(this.sensor.entity!, message),
});

this.configRetriever.addListener(ConfigFileType.Devices, {
onConfigChange: (type: ConfigFileType) => {
if (type === ConfigFileType.Devices) {
this.onConfigChange(type);
}
},
});
}

public message(_: string, __: string, ___: string, message: EventMessage) {
Expand Down Expand Up @@ -100,4 +115,6 @@ export default abstract class SensorStateListener
timestamp?: number,
charging?: boolean,
): void;

protected abstract onConfigChange(type: ConfigFileType): void;
}

0 comments on commit 799ccf3

Please sign in to comment.