io.github.zeroone3010.yahueapi.v2.BasicHueEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yetanotherhueapi Show documentation
Show all versions of yetanotherhueapi Show documentation
A library for controlling Philips Hue lights.
The newest version!
package io.github.zeroone3010.yahueapi.v2;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.launchdarkly.eventsource.MessageEvent;
import com.launchdarkly.eventsource.background.BackgroundEventHandler;
import io.github.zeroone3010.yahueapi.v2.domain.HueEvent;
import io.github.zeroone3010.yahueapi.v2.domain.event.ButtonEvent;
import io.github.zeroone3010.yahueapi.v2.domain.event.MotionEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class BasicHueEventHandler implements BackgroundEventHandler {
private static final Logger logger = LoggerFactory.getLogger(BasicHueEventHandler.class);
public static final TypeReference> EVENT_LIST_TYPE_REF = new TypeReference>() {
};
private final ObjectMapper objectMapper;
private final Hue hue;
private final HueEventListener eventListener;
public BasicHueEventHandler(final Hue hue, final HueEventListener eventListener) {
this.objectMapper = hue.objectMapper;
this.hue = hue;
this.eventListener = eventListener;
}
@Override
public void onOpen() throws Exception {
logger.trace("Connection opened.");
eventListener.connectionOpened();
}
@Override
public void onClosed() throws Exception {
logger.trace("Connection closed.");
eventListener.connectionClosed();
}
@Override
public void onMessage(final String event, final MessageEvent messageEvent) throws Exception {
logger.debug("Message: " + messageEvent.getData());
final List hueEvents = objectMapper.readValue(messageEvent.getData(), EVENT_LIST_TYPE_REF);
eventListener.receive(hueEvents);
parseAndAnnounceButtonEvents(hueEvents);
}
private void parseAndAnnounceButtonEvents(final List hueEvents) {
hueEvents.stream().flatMap(eventsItem ->
eventsItem.getData().stream()
.filter(data -> data.getButton().isPresent())
.map(data -> {
final Switch theSwitch = hue.getSwitches().get(data.getOwner().getResourceId());
return new ButtonEvent(eventsItem.getCreationTime(),
theSwitch,
theSwitch.getButtons().get(data.getResourceId()),
ButtonEventType.parseFromButtonEventType(data.getButton().get().getLastEvent()),
eventsItem.getId());
})
).forEach(eventListener::receiveButtonEvent);
hueEvents.stream().flatMap(eventsItem ->
eventsItem.getData().stream()
.filter(data -> data.getMotion().isPresent())
.map(motion -> {
final Device device = hue.getMotionSensors().get(motion.getOwner().getResourceId());
return new MotionEvent(eventsItem.getCreationTime(),
eventsItem.getId(),
device,
motion.getMotion().get().isMotion(),
motion.getMotion().get().isMotionValid()
);
})
).forEach(eventListener::receiveMotionEvent);
}
@Override
public void onComment(final String comment) {
logger.trace("Comment received: " + comment);
}
@Override
public void onError(final Throwable t) {
logger.info("onError: " + t);
}
}