diff --git a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SentMessageHelper.java b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SentMessageHelper.java index a852b12..bfb1339 100644 --- a/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SentMessageHelper.java +++ b/Helpers/src/main/java/com/christophecvb/touchportal/helpers/SentMessageHelper.java @@ -34,6 +34,7 @@ public class SentMessageHelper { public static final String TYPE_SETTING_UPDATE = "settingUpdate"; public static final String TYPE_SHOW_NOTIFICATION = "showNotification"; public static final String TYPE_CONNECTOR_UPDATE = "connectorUpdate"; + public static final String TYPE_TRIGGER_EVENT = "triggerEvent"; public static final String INSTANCE_ID = "instanceId"; public static final String ID = GenericHelper.ID; public static final String VALUE = GenericHelper.VALUE; @@ -48,5 +49,7 @@ public class SentMessageHelper { public static final String CONNECTOR_ID = "connectorId"; public static final String SHORT_ID = "shortId"; public static final String PARENT_GROUP = "parentGroup"; + public static final String EVENT_ID = "eventId"; + public static final String STATES = "states"; } \ No newline at end of file diff --git a/Library/src/main/java/com/christophecvb/touchportal/TouchPortalPlugin.java b/Library/src/main/java/com/christophecvb/touchportal/TouchPortalPlugin.java index 700c690..9ba6233 100644 --- a/Library/src/main/java/com/christophecvb/touchportal/TouchPortalPlugin.java +++ b/Library/src/main/java/com/christophecvb/touchportal/TouchPortalPlugin.java @@ -26,7 +26,6 @@ import com.christophecvb.touchportal.model.deserializer.TPMessageDeserializer; import com.google.gson.*; import okhttp3.*; -import org.jetbrains.annotations.NotNull; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; @@ -1050,6 +1049,36 @@ public boolean sendShowNotification(String notificationId, String title, String return sent; } + /** + * Send a Trigger Event message to the Touch Portal Plugin System + * + * @param eventId String + * @param states Map<String, Object> Key value pair of state id and value + * @return Boolean sent + */ + public boolean sendTriggerEvent(String eventId, Map states) { + boolean sent = false; + if (eventId != null && !eventId.isEmpty()) { + JsonObject triggerEventMessage = new JsonObject(); + triggerEventMessage.addProperty(SentMessageHelper.TYPE, SentMessageHelper.TYPE_TRIGGER_EVENT); + triggerEventMessage.addProperty(SentMessageHelper.EVENT_ID, eventId); + + if (states != null && !states.isEmpty()) { + JsonObject jsonStates = new JsonObject(); + + states.forEach((id, value) -> { + jsonStates.addProperty(id, String.valueOf(value)); + }); + triggerEventMessage.add(SentMessageHelper.STATES, jsonStates); + } + + sent = this.send(triggerEventMessage); + TouchPortalPlugin.LOGGER.info("Trigger Event [" + eventId + "] Sent [" + sent + "]"); + } + + return sent; + } + /** * Send a Connector Update Message to the Touch Portal Plugin System * diff --git a/Library/src/test/java/com/christophecvb/touchportal/test/LibraryTests.java b/Library/src/test/java/com/christophecvb/touchportal/test/LibraryTests.java index d71df95..dc16d5a 100644 --- a/Library/src/test/java/com/christophecvb/touchportal/test/LibraryTests.java +++ b/Library/src/test/java/com/christophecvb/touchportal/test/LibraryTests.java @@ -395,6 +395,19 @@ public void testSendFail() { assertFalse(this.touchPortalPluginTest.sendRemoveState("BaseCategory", "StateId")); } + @Test + public void testTriggerEvent() { + LOGGER.log(Level.FINE, "Now"); + assertFalse(this.touchPortalPluginTest.sendTriggerEvent(null, null)); + assertFalse(this.touchPortalPluginTest.sendTriggerEvent("", null)); + + assertTrue(this.touchPortalPluginTest.sendTriggerEvent(TouchPortalPluginTestConstants.BaseCategory.Events.CustomState.ID, null)); + + HashMap states = new HashMap<>(); + states.put(TouchPortalPluginTestConstants.BaseCategory.States.CustomState.ID, "StateValue"); + assertTrue(this.touchPortalPluginTest.sendTriggerEvent(TouchPortalPluginTestConstants.BaseCategory.Events.CustomState.ID, states)); + } + @Test public void testReceiveActionNoId() throws IOException, InterruptedException { LOGGER.log(Level.FINE, "Now"); diff --git a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java index c7f15fc..2737b69 100644 --- a/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java +++ b/SampleJava/src/main/java/com/christophecvb/touchportal/samplejava/TouchPortalSampleJavaPlugin.java @@ -30,6 +30,8 @@ import com.google.gson.JsonObject; import java.io.File; +import java.util.HashMap; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; @@ -155,6 +157,10 @@ public static void main(String... args) { } catch (InterruptedException ignored) { } touchPortalSampleJavaPlugin.sendConnectorUpdate(TouchPortalSampleJavaPluginConstants.ID, TouchPortalSampleJavaPluginConstants.BaseCategory.Connectors.ConnectorSimple.ID, 50, null); + + HashMap states = new HashMap<>(); + states.put(TouchPortalSampleJavaPluginConstants.BaseCategory.States.CustomStateWithEvent.ID, "Value"); + touchPortalSampleJavaPlugin.sendTriggerEvent(TouchPortalSampleJavaPluginConstants.BaseCategory.Events.CustomStateWithEvent.ID, states); } } }