Skip to content

Commit

Permalink
Fix serialization of stoveId for mqtt notifications channel (#171)
Browse files Browse the repository at this point in the history
* Fix serialization of stoveId for notifications
Add notification type (in order to be more generic)
Update doc and tests accordingly
* Fix logo path and typo
  • Loading branch information
sebastienvermeille authored Jan 24, 2024
1 parent 26877f7 commit 1c8fc0e
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# RIKA2MQTT

![logo](old-doc/docs/assets/rika2mqtt-128x128.png)
![logo](docs/static/assets/rika2mqtt-128x128.png)

RIKA2MQTT is a free and opensource bridge enabling end-users to connect their RIKA firenet stove to an MQTT server.
It goes both way so you can monitor and also take control of your stove directly via MQTT.
It goes both way, so you can monitor and also take control of your stove directly via MQTT.

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.cookiecode/rika2mqtt-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.cookiecode/rika2mqtt-parent)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
Expand Down
34 changes: 22 additions & 12 deletions bridge/src/main/java/dev/cookiecode/rika2mqtt/bridge/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import dev.cookiecode.rika2mqtt.bridge.misc.EmailObfuscator;
import dev.cookiecode.rika2mqtt.bridge.model.ErrorNotification;
import dev.cookiecode.rika2mqtt.plugins.internal.v1.Rika2MqttPluginService;
import dev.cookiecode.rika2mqtt.plugins.internal.v1.event.PolledStoveStatusEvent;
import dev.cookiecode.rika2mqtt.plugins.internal.v1.event.StoveErrorEvent;
Expand Down Expand Up @@ -144,18 +145,7 @@ void publishToMqtt() {
.stoveStatus(stoveStatusMapper.toApiStoveStatus(status))
.build());

status
.getError()
.ifPresent(
stoveError -> {
final var enrichedStoveError =
stoveErrorMapper.toApiStoveError(stoveId.id(), stoveError);
final var jsonError = gson.toJson(enrichedStoveError);
mqttService.publishNotification(jsonError);

applicationEventPublisher.publishEvent(
StoveErrorEvent.builder().stoveError(enrichedStoveError).build());
});
handleStoveErrors(status);
} catch (InvalidStoveIdException e) {
// TODO: could occurs if a stove is added later (after deployment of this rika2mqtt
// instance, might be valuable to perform a reload of stoves "periodically") -> should
Expand All @@ -174,6 +164,26 @@ void publishToMqtt() {
});
}

private void handleStoveErrors(@NonNull StoveStatus status) {
status
.getError()
.ifPresent(
stoveError -> {
final var stoveId = status.getStoveId();

final var enrichedStoveError = stoveErrorMapper.toApiStoveError(stoveId, stoveError);

final var notification =
new ErrorNotification(stoveId, enrichedStoveError.getErrorCode());

final var jsonNotification = gson.toJson(notification);
mqttService.publishNotification(jsonNotification);

applicationEventPublisher.publishEvent(
StoveErrorEvent.builder().stoveError(enrichedStoveError).build());
});
}

/** Forward MQTT commands to rika-firenet */
@EventListener(MqttCommandEvent.class)
public void onReceiveMqttCommand(@NonNull MqttCommandEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License
* Copyright © 2022 Sebastien Vermeille
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package dev.cookiecode.rika2mqtt.bridge.model;

import static dev.cookiecode.rika2mqtt.bridge.model.NotificationType.ERROR;

import lombok.Getter;

/**
* Notification send to MQTT as json
*
* @author Sebastien Vermeille
*/
@Getter
public class ErrorNotification extends Notification {

private String errorCode;

public ErrorNotification(Long stoveId, String errorCode) {
super(stoveId, ERROR);
this.errorCode = errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License
* Copyright © 2022 Sebastien Vermeille
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package dev.cookiecode.rika2mqtt.bridge.model;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Notification send to MQTT as json
*
* @author Sebastien Vermeille
*/
@Getter
@RequiredArgsConstructor
public class Notification {
private final Long stoveId;
private final NotificationType type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* The MIT License
* Copyright © 2022 Sebastien Vermeille
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package dev.cookiecode.rika2mqtt.bridge.model;

public enum NotificationType {
ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void publishToMqttShouldInvokeRikaFirenetServiceGetStatusForEachStove() throws E
}

@Test
void publishToMqttShouldInvokeMqttServicePublishErrorForEachStoveHavingAnError()
void publishToMqttShouldInvokeMqttServicePublishNotificationForEachStoveHavingAnError()
throws Exception {
// GIVEN
final var stoves = List.of(StoveId.of(1L), StoveId.of(2L), StoveId.of(3L));
Expand All @@ -214,6 +214,9 @@ void publishToMqttShouldInvokeMqttServicePublishErrorForEachStoveHavingAnError()
when(stoveStatus.getError())
.thenReturn(Optional.of(StoveError.builder().statusError(1).statusSubError(12).build()));

final var enrichedError = mock(dev.cookiecode.rika2mqtt.plugins.api.v1.model.StoveError.class);
when(stoveErrorMapper.toApiStoveError(anyLong(), any())).thenReturn(enrichedError);

when(rikaFirenetService.getStatus(any())).thenReturn(stoveStatus);

// WHEN
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/examples/example-notification-mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ By default the notifications are published to the topic: `tele/rika2mqtt-notific
The payload is in JSON format :
```
{
"stoveId": 42,
"errorCode": "E0001.02",
"stoveId": 42,
"type": "ERROR"
}
```

Expand Down

0 comments on commit 1c8fc0e

Please sign in to comment.