From 19c9240c4a5e5b46dd699ef98c10741a695b41a5 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 9 Oct 2021 15:35:18 +0200 Subject: [PATCH] Added support to send an Image directly (#11346) Signed-off-by: Christoph Weitkamp --- bundles/org.openhab.binding.pushsafer/README.md | 17 ++++++++++++++++- .../connection/PushsaferMessageBuilder.java | 7 +++++-- .../resources/OH-INF/i18n/pushsafer.properties | 4 ++-- .../OH-INF/i18n/pushsafer_de.properties | 2 +- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.pushsafer/README.md b/bundles/org.openhab.binding.pushsafer/README.md index 220287f1966bf..727cd9e6c336c 100644 --- a/bundles/org.openhab.binding.pushsafer/README.md +++ b/bundles/org.openhab.binding.pushsafer/README.md @@ -47,8 +47,23 @@ The parameter `message` is **mandatory**, the `title` parameter defaults to what - `sendPushsaferMonospaceMessage(String message, @Nullable String title)` - This method is used to send a monospace message. -- `sendPushsaferAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType, @Nullable String authentication)` - This method is used to send a message with an image attachment. It takes a local path or url to the image attachment (parameter `attachment` **mandatory**), an optional `contentType` to define the content-type of the attachment (default: `"jpeg"`, possible values: `"jpeg"`, `"png"`, `"gif"`) and an optional `authentication` for the given URL to define the authentication if needed (default: `""`, example: `"user:password"`). +- `sendPushsaferAttachmentMessage(String message, @Nullable String title, String attachment, @Nullable String contentType, @Nullable String authentication)` - This method is used to send a message with an image attachment. It takes a local path or URL to the image attachment (parameter `attachment` **mandatory**), an optional `contentType` to define the content-type of the attachment (default: `"jpeg"`, possible values: `"jpeg"`, `"png"`, `"gif"`) and an optional `authentication` for the given URL to define the credentials to authenticate a user if needed (default: `""`, example: `"user:password"`). - `sendPushsaferURLMessage(String message, @Nullable String title, String url, @Nullable String urlTitle)` - This method is used to send a message with an URL. A supplementary `url` to show with the message and a `urlTitle` for the URL, otherwise just the URL is shown. - `sendPushsaferPriorityMessage(String message, @Nullable String title, @Nullable Integer priority)` - This method is used to send a priority message. Parameter `priority` is the priority (`-2`, `-1`, `0`, `1`, `2`) to be used (default: `2`). + +## Example + +demo.rules + +```java +```java +val actions = getActions("pushsafer", "pushsafer:pushsafer-account:account") +// send message with attachment +actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "/path/to/my-local-image.png", "png", null) +actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "https://www.openhab.org/openhab-logo-square.png", "png", "user:password") +actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", "data:[][;base64],", null, null) +// in case you want to send the content of an Image Item (RawType) +actions.sendPushsaferAttachmentMessage("Hello World!", "openHAB", myImageItem.state.toFullString, null, null) +``` diff --git a/bundles/org.openhab.binding.pushsafer/src/main/java/org/openhab/binding/pushsafer/internal/connection/PushsaferMessageBuilder.java b/bundles/org.openhab.binding.pushsafer/src/main/java/org/openhab/binding/pushsafer/internal/connection/PushsaferMessageBuilder.java index 958bedf692c3b..a009ee9fc83c8 100644 --- a/bundles/org.openhab.binding.pushsafer/src/main/java/org/openhab/binding/pushsafer/internal/connection/PushsaferMessageBuilder.java +++ b/bundles/org.openhab.binding.pushsafer/src/main/java/org/openhab/binding/pushsafer/internal/connection/PushsaferMessageBuilder.java @@ -310,9 +310,10 @@ public ContentProvider build() throws PushsaferCommunicationException { body.addFieldPart(MESSAGE_KEY_TIME2LIVE, new StringContentProvider(String.valueOf(time2live)), null); if (attachment != null) { + String localAttachment = attachment; final String encodedString; try { - if (attachment.startsWith("http")) { + if (localAttachment.startsWith("http")) { Properties headers = new Properties(); headers.put("User-Agent", "Mozilla/5.0"); if (!authentication.isBlank()) { @@ -324,7 +325,9 @@ public ContentProvider build() throws PushsaferCommunicationException { throw new IllegalArgumentException( String.format("Skip sending the message as content '%s' does not exist.", attachment)); } - encodedString = "data:" + contentType + ";base64," + content; + encodedString = "data:image/" + contentType + ";base64," + content; + } else if (localAttachment.startsWith("data:")) { + encodedString = localAttachment; } else { File file = new File(attachment); if (!file.exists()) { diff --git a/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer.properties b/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer.properties index 8b4177f90b275..e43d12e6e1257 100644 --- a/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer.properties +++ b/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer.properties @@ -30,9 +30,9 @@ sendPushsaferMonospaceMessageActionDescription = This method is used to send a m sendPushsaferAttachmentMessageActionLabel = send a plain text message with an image attachment sendPushsaferAttachmentMessageActionDescription = This method is used to send a message with an image attachment. sendPushsaferMessageActionInputAttachmentLabel = Image Attachment -sendPushsaferMessageActionInputAttachmentDescription = A local path or url to the image. +sendPushsaferMessageActionInputAttachmentDescription = A local path or URL to the image. sendPushsaferMessageActionInputContentTypeLabel = Image Type -sendPushsaferMessageActionInputContentTypeDescription = The image type of the attachment. Defaults to "jpeg", possible values "jpeg,png,gif". +sendPushsaferMessageActionInputContentTypeDescription = The image type of the attachment. Defaults to "jpeg", possible values "jpeg" ,"png" or "gif". sendPushsaferMessageActionInputAuthenticationLabel = Authentication sendPushsaferMessageActionInputAuthenticationDescription = Basic access authentication for HTTP(S) requests. Default: "", Example: "user:password". diff --git a/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer_de.properties b/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer_de.properties index 2b84da47cf257..b1cfe68027d9b 100644 --- a/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer_de.properties +++ b/bundles/org.openhab.binding.pushsafer/src/main/resources/OH-INF/i18n/pushsafer_de.properties @@ -73,7 +73,7 @@ sendPushsaferAttachmentMessageActionDescription = Action zum Versenden einer Tex sendPushsaferMessageActionInputAttachmentLabel = Bild-Anhang sendPushsaferMessageActionInputAttachmentDescription = Lokaler Pfad oder URL zum Anhang. sendPushsaferMessageActionInputContentTypeLabel = Bild-Typ -sendPushsaferMessageActionInputContentTypeDescription = Der Bild-Typ für den Anhang. Default: "jpeg", mögliche Werte "jpeg,png,gif". +sendPushsaferMessageActionInputContentTypeDescription = Der Bild-Typ für den Anhang. Default: "jpeg", mögliche Werte "jpeg", "png" oder "gif". sendPushsaferMessageActionInputAuthenticationLabel = Authentifizierung sendPushsaferMessageActionInputAuthenticationDescription = Basisauthentifizierung für HTTP(S) Aufrufe. Default: "", Beispiel: "user:passwort".