Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API 7] Trigger Event #63

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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