Skip to content

Commit

Permalink
Feat: TriggerEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Pjiesco committed Oct 6, 2023
1 parent ee1d089 commit 0bc2035
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Object> states = new HashMap<>();
states.put(TouchPortalSampleJavaPluginConstants.BaseCategory.States.CustomStateWithEvent.ID, "Value");
touchPortalSampleJavaPlugin.sendTriggerEvent(TouchPortalSampleJavaPluginConstants.BaseCategory.Events.CustomStateWithEvent.ID, states);
}
}
}
Expand Down

0 comments on commit 0bc2035

Please sign in to comment.