From: Wouter Born Date: Wed, 10 Mar 2021 08:52:39 +0000 (+0100) Subject: [lifx] Fix all SAT findings (#10307) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=f3a517e4e1630d30cfb43a93c8800f04fcc5124c;p=openhab-addons.git [lifx] Fix all SAT findings (#10307) * Remove dependency on commons-lang classes * Use HexUtils * Fix/add missing initial contribution author lines * Add unit test for MACAddress * Rename protocol package to dto Signed-off-by: Wouter Born --- diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactory.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactory.java index dca40a8ef1..b507648a71 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactory.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactory.java @@ -19,7 +19,7 @@ import org.openhab.core.thing.ThingUID; /** * The {@link LifxChannelFactory} creates dynamic LIFX channels. * - * @author Wouter Born - Add i18n support + * @author Wouter Born - Initial contribution */ @NonNullByDefault public interface LifxChannelFactory { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactoryImpl.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactoryImpl.java index 7c8c3b12ec..333c610a74 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactoryImpl.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxChannelFactoryImpl.java @@ -34,7 +34,7 @@ import org.osgi.service.component.annotations.Reference; /** * The {@link LifxChannelFactoryImpl} creates dynamic LIFX channels. * - * @author Wouter Born - Add i18n support + * @author Wouter Born - Initial contribution */ @NonNullByDefault @Component(service = LifxChannelFactory.class) diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCommunicationHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCommunicationHandler.java index 375ff33aa8..bd27a3a7a8 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCommunicationHandler.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCommunicationHandler.java @@ -32,12 +32,12 @@ import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.GetServiceRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.StateServiceResponse; import org.openhab.binding.lifx.internal.fields.MACAddress; import org.openhab.binding.lifx.internal.handler.LifxLightHandler.CurrentLightState; import org.openhab.binding.lifx.internal.listener.LifxResponsePacketListener; -import org.openhab.binding.lifx.internal.protocol.GetServiceRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.StateServiceResponse; import org.openhab.binding.lifx.internal.util.LifxNetworkUtil; import org.openhab.binding.lifx.internal.util.LifxSelectorUtil; import org.slf4j.Logger; @@ -46,7 +46,7 @@ import org.slf4j.LoggerFactory; /** * The {@link LifxLightCommunicationHandler} is responsible for the communications with a light. * - * @author Wouter Born - Extracted class from LifxLightHandler + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightCommunicationHandler { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightConfig.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightConfig.java index f39c6fbf51..4b193398c8 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightConfig.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightConfig.java @@ -33,7 +33,7 @@ public class LifxLightConfig { public @Nullable MACAddress getMACAddress() { String localDeviceId = deviceId; - return localDeviceId == null ? null : new MACAddress(localDeviceId, true); + return localDeviceId == null ? null : new MACAddress(localDeviceId); } public @Nullable InetSocketAddress getHost() { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightContext.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightContext.java index 254d87921c..633960006b 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightContext.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightContext.java @@ -17,12 +17,11 @@ import java.util.concurrent.ScheduledExecutorService; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.lifx.internal.handler.LifxLightHandler; import org.openhab.binding.lifx.internal.handler.LifxLightHandler.CurrentLightState; -import org.openhab.binding.lifx.internal.protocol.Product; /** * The {@link LifxLightContext} shares the context of a light with {@link LifxLightHandler} helper objects. * - * @author Wouter Born - Add optional host configuration parameter + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightContext { @@ -31,10 +30,10 @@ public class LifxLightContext { private final LifxLightConfig configuration; private final CurrentLightState currentLightState; private final LifxLightState pendingLightState; - private final Product product; + private final LifxProduct product; private final ScheduledExecutorService scheduler; - public LifxLightContext(String logId, Product product, LifxLightConfig configuration, + public LifxLightContext(String logId, LifxProduct product, LifxLightConfig configuration, CurrentLightState currentLightState, LifxLightState pendingLightState, ScheduledExecutorService scheduler) { this.logId = logId; this.configuration = configuration; @@ -52,7 +51,7 @@ public class LifxLightContext { return configuration; } - public Product getProduct() { + public LifxProduct getProduct() { return product; } diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCurrentStateUpdater.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCurrentStateUpdater.java index ad94aa2651..910c8b7787 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCurrentStateUpdater.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightCurrentStateUpdater.java @@ -13,7 +13,7 @@ package org.openhab.binding.lifx.internal; import static org.openhab.binding.lifx.internal.LifxBindingConstants.MIN_ZONE_INDEX; -import static org.openhab.binding.lifx.internal.protocol.Product.Feature.*; +import static org.openhab.binding.lifx.internal.LifxProduct.Feature.*; import static org.openhab.binding.lifx.internal.util.LifxMessageUtil.infraredToPercentType; import java.util.concurrent.ScheduledExecutorService; @@ -23,22 +23,21 @@ import java.util.concurrent.locks.ReentrantLock; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.GetColorZonesRequest; +import org.openhab.binding.lifx.internal.dto.GetLightInfraredRequest; +import org.openhab.binding.lifx.internal.dto.GetRequest; +import org.openhab.binding.lifx.internal.dto.GetTileEffectRequest; +import org.openhab.binding.lifx.internal.dto.GetWifiInfoRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.StateLightInfraredResponse; +import org.openhab.binding.lifx.internal.dto.StateLightPowerResponse; +import org.openhab.binding.lifx.internal.dto.StateMultiZoneResponse; +import org.openhab.binding.lifx.internal.dto.StatePowerResponse; +import org.openhab.binding.lifx.internal.dto.StateResponse; +import org.openhab.binding.lifx.internal.dto.StateTileEffectResponse; +import org.openhab.binding.lifx.internal.dto.StateWifiInfoResponse; import org.openhab.binding.lifx.internal.fields.HSBK; import org.openhab.binding.lifx.internal.handler.LifxLightHandler.CurrentLightState; -import org.openhab.binding.lifx.internal.protocol.GetColorZonesRequest; -import org.openhab.binding.lifx.internal.protocol.GetLightInfraredRequest; -import org.openhab.binding.lifx.internal.protocol.GetRequest; -import org.openhab.binding.lifx.internal.protocol.GetTileEffectRequest; -import org.openhab.binding.lifx.internal.protocol.GetWifiInfoRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.Product; -import org.openhab.binding.lifx.internal.protocol.StateLightInfraredResponse; -import org.openhab.binding.lifx.internal.protocol.StateLightPowerResponse; -import org.openhab.binding.lifx.internal.protocol.StateMultiZoneResponse; -import org.openhab.binding.lifx.internal.protocol.StatePowerResponse; -import org.openhab.binding.lifx.internal.protocol.StateResponse; -import org.openhab.binding.lifx.internal.protocol.StateTileEffectResponse; -import org.openhab.binding.lifx.internal.protocol.StateWifiInfoResponse; import org.openhab.core.library.types.PercentType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +46,7 @@ import org.slf4j.LoggerFactory; * The {@link LifxLightCurrentStateUpdater} sends packets to a light in order to update the {@code currentLightState} to * the actual light state. * - * @author Wouter Born - Extracted class from LifxLightHandler + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightCurrentStateUpdater { @@ -57,7 +56,7 @@ public class LifxLightCurrentStateUpdater { private final Logger logger = LoggerFactory.getLogger(LifxLightCurrentStateUpdater.class); private final String logId; - private final Product product; + private final LifxProduct product; private final CurrentLightState currentLightState; private final ScheduledExecutorService scheduler; private final LifxLightCommunicationHandler communicationHandler; diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightDiscovery.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightDiscovery.java index 21011010db..cfadb13a67 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightDiscovery.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightDiscovery.java @@ -26,18 +26,16 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.GetLabelRequest; +import org.openhab.binding.lifx.internal.dto.GetServiceRequest; +import org.openhab.binding.lifx.internal.dto.GetVersionRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.StateLabelResponse; +import org.openhab.binding.lifx.internal.dto.StateServiceResponse; +import org.openhab.binding.lifx.internal.dto.StateVersionResponse; import org.openhab.binding.lifx.internal.fields.MACAddress; -import org.openhab.binding.lifx.internal.protocol.GetLabelRequest; -import org.openhab.binding.lifx.internal.protocol.GetServiceRequest; -import org.openhab.binding.lifx.internal.protocol.GetVersionRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.Product; -import org.openhab.binding.lifx.internal.protocol.StateLabelResponse; -import org.openhab.binding.lifx.internal.protocol.StateServiceResponse; -import org.openhab.binding.lifx.internal.protocol.StateVersionResponse; import org.openhab.binding.lifx.internal.util.LifxSelectorUtil; import org.openhab.core.config.discovery.AbstractDiscoveryService; import org.openhab.core.config.discovery.DiscoveryResult; @@ -87,7 +85,7 @@ public class LifxLightDiscovery extends AbstractDiscoveryService { private InetSocketAddress socketAddress; private String logId; private @Nullable String label; - private @Nullable Product product; + private @Nullable LifxProduct product; private long productVersion; private boolean supportedProduct = true; private LifxSelectorContext selectorContext; @@ -300,7 +298,8 @@ public class LifxLightDiscovery extends AbstractDiscoveryService { light.label = ((StateLabelResponse) packet).getLabel().trim(); } else if (packet instanceof StateVersionResponse) { try { - light.product = Product.getProductFromProductID(((StateVersionResponse) packet).getProduct()); + light.product = LifxProduct + .getProductFromProductID(((StateVersionResponse) packet).getProduct()); light.productVersion = ((StateVersionResponse) packet).getVersion(); } catch (IllegalArgumentException e) { logger.debug("Discovered an unsupported light ({}): {}", light.macAddress.getAsLabel(), @@ -322,7 +321,7 @@ public class LifxLightDiscovery extends AbstractDiscoveryService { } private DiscoveryResult createDiscoveryResult(DiscoveredLight light) throws IllegalArgumentException { - Product product = light.product; + LifxProduct product = light.product; if (product == null) { throw new IllegalArgumentException("Product of discovered light is null"); } @@ -331,7 +330,7 @@ public class LifxLightDiscovery extends AbstractDiscoveryService { ThingUID thingUID = new ThingUID(product.getThingTypeUID(), macAsLabel); String label = light.label; - if (StringUtils.isBlank(label)) { + if (label == null || label.isBlank()) { label = product.getName(); } diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightOnlineStateUpdater.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightOnlineStateUpdater.java index c40400e367..62ef83091c 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightOnlineStateUpdater.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightOnlineStateUpdater.java @@ -21,17 +21,17 @@ import java.util.concurrent.locks.ReentrantLock; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.GetEchoRequest; +import org.openhab.binding.lifx.internal.dto.GetServiceRequest; +import org.openhab.binding.lifx.internal.dto.Packet; import org.openhab.binding.lifx.internal.handler.LifxLightHandler.CurrentLightState; -import org.openhab.binding.lifx.internal.protocol.GetEchoRequest; -import org.openhab.binding.lifx.internal.protocol.GetServiceRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The {@link LifxLightOnlineStateUpdater} sets the state of a light offline when it no longer responds to echo packets. * - * @author Wouter Born - Extracted class from LifxLightHandler + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightOnlineStateUpdater { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightPropertiesUpdater.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightPropertiesUpdater.java index 7be78cffbd..9de0a75ff6 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightPropertiesUpdater.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightPropertiesUpdater.java @@ -27,17 +27,16 @@ import java.util.concurrent.locks.ReentrantLock; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.GetHostFirmwareRequest; +import org.openhab.binding.lifx.internal.dto.GetVersionRequest; +import org.openhab.binding.lifx.internal.dto.GetWifiFirmwareRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.StateHostFirmwareResponse; +import org.openhab.binding.lifx.internal.dto.StateVersionResponse; +import org.openhab.binding.lifx.internal.dto.StateWifiFirmwareResponse; import org.openhab.binding.lifx.internal.fields.MACAddress; import org.openhab.binding.lifx.internal.handler.LifxLightHandler.CurrentLightState; import org.openhab.binding.lifx.internal.listener.LifxPropertiesUpdateListener; -import org.openhab.binding.lifx.internal.protocol.GetHostFirmwareRequest; -import org.openhab.binding.lifx.internal.protocol.GetVersionRequest; -import org.openhab.binding.lifx.internal.protocol.GetWifiFirmwareRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.Product; -import org.openhab.binding.lifx.internal.protocol.StateHostFirmwareResponse; -import org.openhab.binding.lifx.internal.protocol.StateVersionResponse; -import org.openhab.binding.lifx.internal.protocol.StateWifiFirmwareResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +44,7 @@ import org.slf4j.LoggerFactory; * The {@link LifxLightPropertiesUpdater} updates the light properties when a light goes online. When packets get lost * the requests are resent when the {@code UPDATE_INTERVAL} elapses. * - * @author Wouter Born - Update light properties when online + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightPropertiesUpdater { @@ -157,7 +156,7 @@ public class LifxLightPropertiesUpdater { properties.put(LifxBindingConstants.PROPERTY_PRODUCT_VERSION, Long.toString(productVersion)); try { - Product product = Product.getProductFromProductID(productId); + LifxProduct product = LifxProduct.getProductFromProductID(productId); properties.put(LifxBindingConstants.PROPERTY_PRODUCT_NAME, product.getName()); properties.put(LifxBindingConstants.PROPERTY_VENDOR_ID, Long.toString(product.getVendor().getID())); properties.put(LifxBindingConstants.PROPERTY_VENDOR_NAME, product.getVendor().getName()); diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightState.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightState.java index a4db52c45c..b3bbefaf20 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightState.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightState.java @@ -22,11 +22,11 @@ import java.util.concurrent.CopyOnWriteArrayList; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.Effect; +import org.openhab.binding.lifx.internal.dto.PowerState; +import org.openhab.binding.lifx.internal.dto.SignalStrength; import org.openhab.binding.lifx.internal.fields.HSBK; import org.openhab.binding.lifx.internal.listener.LifxLightStateListener; -import org.openhab.binding.lifx.internal.protocol.Effect; -import org.openhab.binding.lifx.internal.protocol.PowerState; -import org.openhab.binding.lifx.internal.protocol.SignalStrength; import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.PercentType; @@ -34,7 +34,7 @@ import org.openhab.core.library.types.PercentType; /** * The {@link LifxLightState} stores the properties that represent the state of a light. * - * @author Wouter Born - Extracted class from LifxLightHandler, added listener logic + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightState { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightStateChanger.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightStateChanger.java index 4e2ccd16ab..c2ee8d1c9b 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightStateChanger.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxLightStateChanger.java @@ -13,7 +13,7 @@ package org.openhab.binding.lifx.internal; import static org.openhab.binding.lifx.internal.LifxBindingConstants.PACKET_INTERVAL; -import static org.openhab.binding.lifx.internal.protocol.Product.Feature.MULTIZONE; +import static org.openhab.binding.lifx.internal.LifxProduct.Feature.MULTIZONE; import static org.openhab.binding.lifx.internal.util.LifxMessageUtil.*; import java.time.Duration; @@ -29,25 +29,24 @@ import java.util.concurrent.locks.ReentrantLock; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.dto.AcknowledgementResponse; +import org.openhab.binding.lifx.internal.dto.ApplicationRequest; +import org.openhab.binding.lifx.internal.dto.Effect; +import org.openhab.binding.lifx.internal.dto.GetColorZonesRequest; +import org.openhab.binding.lifx.internal.dto.GetLightInfraredRequest; +import org.openhab.binding.lifx.internal.dto.GetLightPowerRequest; +import org.openhab.binding.lifx.internal.dto.GetRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.PowerState; +import org.openhab.binding.lifx.internal.dto.SetColorRequest; +import org.openhab.binding.lifx.internal.dto.SetColorZonesRequest; +import org.openhab.binding.lifx.internal.dto.SetLightInfraredRequest; +import org.openhab.binding.lifx.internal.dto.SetLightPowerRequest; +import org.openhab.binding.lifx.internal.dto.SetPowerRequest; +import org.openhab.binding.lifx.internal.dto.SetTileEffectRequest; +import org.openhab.binding.lifx.internal.dto.SignalStrength; import org.openhab.binding.lifx.internal.fields.HSBK; import org.openhab.binding.lifx.internal.listener.LifxLightStateListener; -import org.openhab.binding.lifx.internal.protocol.AcknowledgementResponse; -import org.openhab.binding.lifx.internal.protocol.ApplicationRequest; -import org.openhab.binding.lifx.internal.protocol.Effect; -import org.openhab.binding.lifx.internal.protocol.GetColorZonesRequest; -import org.openhab.binding.lifx.internal.protocol.GetLightInfraredRequest; -import org.openhab.binding.lifx.internal.protocol.GetLightPowerRequest; -import org.openhab.binding.lifx.internal.protocol.GetRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.PowerState; -import org.openhab.binding.lifx.internal.protocol.Product; -import org.openhab.binding.lifx.internal.protocol.SetColorRequest; -import org.openhab.binding.lifx.internal.protocol.SetColorZonesRequest; -import org.openhab.binding.lifx.internal.protocol.SetLightInfraredRequest; -import org.openhab.binding.lifx.internal.protocol.SetLightPowerRequest; -import org.openhab.binding.lifx.internal.protocol.SetPowerRequest; -import org.openhab.binding.lifx.internal.protocol.SetTileEffectRequest; -import org.openhab.binding.lifx.internal.protocol.SignalStrength; import org.openhab.core.library.types.PercentType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,7 +56,7 @@ import org.slf4j.LoggerFactory; * light so the change the actual light state to that of the {@code pendingLightState}. When the light does not * acknowledge a packet, it resends it (max 3 times). * - * @author Wouter Born - Extracted class from LifxLightHandler, added logic for handling packet loss + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxLightStateChanger implements LifxLightStateListener { @@ -75,7 +74,7 @@ public class LifxLightStateChanger implements LifxLightStateListener { private final Logger logger = LoggerFactory.getLogger(LifxLightStateChanger.class); private final String logId; - private final Product product; + private final LifxProduct product; private final Duration fadeTime; private final LifxLightState pendingLightState; private final ScheduledExecutorService scheduler; diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxProduct.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxProduct.java new file mode 100644 index 0000000000..0416d378e2 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxProduct.java @@ -0,0 +1,311 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal; + +import static org.openhab.binding.lifx.internal.LifxProduct.Feature.*; +import static org.openhab.binding.lifx.internal.LifxProduct.TemperatureRange.*; +import static org.openhab.binding.lifx.internal.LifxProduct.Vendor.LIFX; + +import java.util.Arrays; +import java.util.EnumSet; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.core.thing.ThingTypeUID; + +/** + * Enumerates the LIFX products, their IDs and feature set. + * + * @see https://lan.developer.lifx.com/docs/lifx-products + * + * @author Wouter Born - Initial contribution + * @author Wouter Born - Add temperature ranges and simplify feature definitions + */ +@NonNullByDefault +public enum LifxProduct { + + PRODUCT_1(1, "LIFX Original 1000", TR_2500_9000, COLOR), + PRODUCT_3(3, "LIFX Color 650", TR_2500_9000, COLOR), + PRODUCT_10(10, "LIFX White 800 (Low Voltage)", TR_2700_6500), + PRODUCT_11(11, "LIFX White 800 (High Voltage)", TR_2700_6500), + PRODUCT_15(15, "LIFX Color 1000", TR_2500_9000, COLOR), + PRODUCT_18(18, "LIFX White 900 BR30 (Low Voltage)", TR_2700_6500), + PRODUCT_19(19, "LIFX White 900 BR30 (High Voltage)", TR_2700_6500), + PRODUCT_20(20, "LIFX Color 1000 BR30", TR_2500_9000, COLOR), + PRODUCT_22(22, "LIFX Color 1000", TR_2500_9000, COLOR), + PRODUCT_27(27, "LIFX A19", TR_2500_9000, COLOR), + PRODUCT_28(28, "LIFX BR30", TR_2500_9000, COLOR), + PRODUCT_29(29, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_30(30, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_31(31, "LIFX Z", TR_2500_9000, COLOR, MULTIZONE), + PRODUCT_32(32, "LIFX Z", TR_2500_9000, COLOR, MULTIZONE), + PRODUCT_36(36, "LIFX Downlight", TR_2500_9000, COLOR), + PRODUCT_37(37, "LIFX Downlight", TR_2500_9000, COLOR), + PRODUCT_38(38, "LIFX Beam", TR_2500_9000, COLOR, MULTIZONE), + PRODUCT_39(39, "LIFX Downlight White to Warm", TR_1500_9000), + PRODUCT_40(40, "LIFX Downlight", TR_2500_9000, COLOR), + PRODUCT_43(43, "LIFX A19", TR_2500_9000, COLOR), + PRODUCT_44(44, "LIFX BR30", TR_2500_9000, COLOR), + PRODUCT_45(45, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_46(46, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_49(49, "LIFX Mini Color", TR_2500_9000, COLOR), + PRODUCT_50(50, "LIFX Mini White to Warm", TR_1500_4000), + PRODUCT_51(51, "LIFX Mini White", TR_2700_2700), + PRODUCT_52(52, "LIFX GU10", TR_2500_9000, COLOR), + PRODUCT_53(53, "LIFX GU10", TR_2500_9000, COLOR), + PRODUCT_55(55, "LIFX Tile", TR_2500_9000, CHAIN, COLOR, MATRIX, TILE_EFFECT), + PRODUCT_57(57, "LIFX Candle", TR_1500_9000, COLOR, MATRIX), + PRODUCT_59(59, "LIFX Mini Color", TR_2500_9000, COLOR), + PRODUCT_60(60, "LIFX Mini White to Warm", TR_1500_4000), + PRODUCT_61(61, "LIFX Mini White", TR_2700_2700), + PRODUCT_62(62, "LIFX A19", TR_2500_9000, COLOR), + PRODUCT_63(63, "LIFX BR30", TR_2500_9000, COLOR), + PRODUCT_64(64, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_65(65, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_66(66, "LIFX Mini White", TR_2700_2700), + PRODUCT_68(68, "LIFX Candle", TR_1500_9000, MATRIX), + PRODUCT_81(81, "LIFX Candle White to Warm", TR_2200_6500), + PRODUCT_82(82, "LIFX Filament Clear", TR_2100_2100), + PRODUCT_85(85, "LIFX Filament Amber", TR_2000_2000), + PRODUCT_87(87, "LIFX Mini White", TR_2700_2700), + PRODUCT_88(88, "LIFX Mini White", TR_2700_2700), + PRODUCT_90(90, "LIFX Clean", TR_2500_9000, HEV), + PRODUCT_91(91, "LIFX Color", TR_2500_9000, COLOR), + PRODUCT_92(92, "LIFX Color", TR_2500_9000, COLOR), + PRODUCT_94(94, "LIFX BR30", TR_2500_9000, COLOR), + PRODUCT_96(96, "LIFX Candle White to Warm", TR_2200_6500), + PRODUCT_97(97, "LIFX A19", TR_2500_9000, COLOR), + PRODUCT_98(98, "LIFX BR30", TR_2500_9000, COLOR), + PRODUCT_99(99, "LIFX Clean", TR_2500_9000, HEV), + PRODUCT_100(100, "LIFX Filament Clear", TR_2100_2100), + PRODUCT_101(101, "LIFX Filament Amber", TR_2000_2000), + PRODUCT_109(109, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_110(110, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), + PRODUCT_111(111, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED); + + /** + * Enumerates the product features. + */ + public enum Feature { + CHAIN, + COLOR, + HEV, + INFRARED, + MATRIX, + MULTIZONE, + TILE_EFFECT + } + + /** + * Enumerates the product vendors. + */ + public enum Vendor { + LIFX(1, "LIFX"); + + private final int id; + private final String name; + + Vendor(int id, String name) { + this.id = id; + this.name = name; + } + + public int getID() { + return id; + } + + public String getName() { + return name; + } + } + + /** + * Enumerates the color temperature ranges of lights. + */ + public enum TemperatureRange { + /** + * 1500-4000K + */ + TR_1500_4000(1500, 4000), + + /** + * 1500-9000K + */ + TR_1500_9000(1500, 9000), + + /** + * 2000-2000K + */ + TR_2000_2000(2000, 2000), + + /** + * 2100-2100K + */ + TR_2100_2100(2100, 2100), + + /** + * 2200-6500K + */ + TR_2200_6500(2200, 6500), + + /** + * 2500-9000K + */ + TR_2500_9000(2500, 9000), + + /** + * 2700-2700K + */ + TR_2700_2700(2700, 2700), + + /** + * 2700-6500K + */ + TR_2700_6500(2700, 6500); + + private final int minimum; + private final int maximum; + + TemperatureRange(int minimum, int maximum) { + this.minimum = minimum; + this.maximum = maximum; + } + + /** + * The minimum color temperature in degrees Kelvin. + * + * @return minimum color temperature (K) + */ + public int getMinimum() { + return minimum; + } + + /** + * The maxiumum color temperature in degrees Kelvin. + * + * @return maximum color temperature (K) + */ + public int getMaximum() { + return maximum; + } + + /** + * The color temperature range in degrees Kelvin. + * + * @return difference between maximum and minimum color temperature values + */ + public int getRange() { + return maximum - minimum; + } + } + + private final Vendor vendor; + private final long id; + private final String name; + private final TemperatureRange temperatureRange; + private final EnumSet features = EnumSet.noneOf(Feature.class); + + private LifxProduct(long id, String name, TemperatureRange temperatureRange) { + this(LIFX, id, name, temperatureRange); + } + + private LifxProduct(long id, String name, TemperatureRange temperatureRange, Feature... features) { + this(LIFX, id, name, temperatureRange, features); + } + + private LifxProduct(Vendor vendor, long id, String name, TemperatureRange temperatureRange) { + this(vendor, id, name, temperatureRange, new Feature[0]); + } + + private LifxProduct(Vendor vendor, long id, String name, TemperatureRange temperatureRange, Feature... features) { + this.vendor = vendor; + this.id = id; + this.name = name; + this.temperatureRange = temperatureRange; + this.features.addAll(Arrays.asList(features)); + } + + @Override + public String toString() { + return name; + } + + public Vendor getVendor() { + return vendor; + } + + public long getID() { + return id; + } + + public String getName() { + return name; + } + + public TemperatureRange getTemperatureRange() { + return temperatureRange; + } + + public ThingTypeUID getThingTypeUID() { + if (hasFeature(COLOR)) { + if (hasFeature(TILE_EFFECT)) { + return LifxBindingConstants.THING_TYPE_TILELIGHT; + } else if (hasFeature(INFRARED)) { + return LifxBindingConstants.THING_TYPE_COLORIRLIGHT; + } else if (hasFeature(MULTIZONE)) { + return LifxBindingConstants.THING_TYPE_COLORMZLIGHT; + } else { + return LifxBindingConstants.THING_TYPE_COLORLIGHT; + } + } else { + return LifxBindingConstants.THING_TYPE_WHITELIGHT; + } + } + + public boolean hasFeature(Feature feature) { + return features.contains(feature); + } + + /** + * Returns a product that has the given thing type UID. + * + * @param uid a thing type UID + * @return a product that has the given thing type UID + * @throws IllegalArgumentException when uid is not a valid LIFX thing type UID + */ + public static LifxProduct getLikelyProduct(ThingTypeUID uid) throws IllegalArgumentException { + for (LifxProduct product : LifxProduct.values()) { + if (product.getThingTypeUID().equals(uid)) { + return product; + } + } + + throw new IllegalArgumentException(uid + " is not a valid product thing type UID"); + } + + /** + * Returns the product that has the given product ID. + * + * @param id the product ID + * @return the product that has the given product ID + * @throws IllegalArgumentException when id is not a valid LIFX product ID + */ + public static LifxProduct getProductFromProductID(long id) throws IllegalArgumentException { + for (LifxProduct product : LifxProduct.values()) { + if (product.id == id) { + return product; + } + } + + throw new IllegalArgumentException(id + " is not a valid product ID"); + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSelectorContext.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSelectorContext.java index 0cd6f67195..bc7f3ffd3a 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSelectorContext.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSelectorContext.java @@ -25,7 +25,7 @@ import org.openhab.binding.lifx.internal.fields.MACAddress; * The {@link LifxSelectorContext} stores the context that is used for broadcast and unicast communications with a * light using a {@link Selector}. * - * @author Wouter Born - Make selector logic reusable between discovery and handlers + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxSelectorContext { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSequenceNumberSupplier.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSequenceNumberSupplier.java index 5f9e108991..1d94e14638 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSequenceNumberSupplier.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/LifxSequenceNumberSupplier.java @@ -20,7 +20,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; /** * Supplies sequence numbers for packets in the range [0, 255]. * - * @author Wouter Born - Make selector logic reusable between discovery and handlers + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxSequenceNumberSupplier implements Supplier { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/AcknowledgementResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/AcknowledgementResponse.java new file mode 100644 index 0000000000..74ace861ab --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/AcknowledgementResponse.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class AcknowledgementResponse extends Packet { + + public static final int TYPE = 0x2D; + + public AcknowledgementResponse() { + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/ApplicationRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/ApplicationRequest.java new file mode 100644 index 0000000000..9e53718a87 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/ApplicationRequest.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +/** + * @author Wouter Born - Initial contribution + */ +public enum ApplicationRequest { + + /** + * Don't apply the requested changes until a message with APPLY or APPLY_ONLY is sent. + */ + NO_APPLY(0x00), + + /** + * Apply the changes immediately and apply any pending changes. + */ + APPLY(0x01), + + /** + * Ignore the requested changes in this message and only apply pending changes. + */ + APPLY_ONLY(0x02); + + private final int value; + + private ApplicationRequest(int value) { + this.value = value; + } + + /** + * Gets the integer value of this application request. + * + * @return the integer value + */ + public int getValue() { + return value; + } + + /** + * Returns the {@link ApplicationRequest} for the given integer value. + * + * @param value the integer value + * @return the {@link ApplicationRequest} or null, if no {@link ApplicationRequest} exists for the + * given value + */ + public static ApplicationRequest fromValue(int value) { + for (ApplicationRequest ar : values()) { + if (ar.getValue() == value) { + return ar; + } + } + + return null; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/EchoRequestResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/EchoRequestResponse.java new file mode 100644 index 0000000000..058a76031a --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/EchoRequestResponse.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class EchoRequestResponse extends Packet { + + public static final int TYPE = 0x3B; + + public static final Field FIELD_PAYLOAD = new ByteField(64); + + private ByteBuffer payload; + + public EchoRequestResponse() { + } + + public ByteBuffer getPayload() { + return payload; + } + + public void setPayload(ByteBuffer location) { + this.payload = location; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 64; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + payload = FIELD_PAYLOAD.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_PAYLOAD.bytes(payload)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Effect.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Effect.java new file mode 100644 index 0000000000..2490e22633 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Effect.java @@ -0,0 +1,169 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.lifx.internal.LifxBindingConstants; +import org.openhab.binding.lifx.internal.fields.HSBK; + +/** + * This class represents LIFX Tile effect + * + * @author Pawel Pieczul - Initial contribution + */ +@NonNullByDefault +public class Effect { + public enum EffectType { + OFF(0), + MORPH(2), + FLAME(3); + + Integer type; + + EffectType(Integer type) { + this.type = type; + } + + public static EffectType fromValue(Integer value) { + switch (value) { + case 0: + return OFF; + case 2: + return MORPH; + case 3: + return FLAME; + default: + throw new IllegalArgumentException("Unknown effect type"); + } + } + + public static EffectType fromValue(String value) { + if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF.equals(value)) { + return OFF; + } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH.equals(value)) { + return MORPH; + } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME.equals(value)) { + return FLAME; + } + throw new IllegalArgumentException("Unknown effect type"); + } + + public Integer intValue() { + return type; + } + + public String stringValue() { + switch (type) { + case 2: + return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH; + case 3: + return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME; + default: + return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF; + } + } + } + + final EffectType type; + final Long speed; + final Long duration; + final HSBK[] palette; + + public EffectType getType() { + return type; + } + + public Long getSpeed() { + return speed; + } + + public Long getDuration() { + return duration; + } + + HSBK[] getPalette() { + return palette; + } + + public Effect(EffectType type, Long speed, Long duration, HSBK[] palette) { + this.type = type; + this.palette = palette; + this.duration = duration; + this.speed = speed; + } + + public Effect(Integer type, Long speed, Long duration, HSBK[] palette) { + this(EffectType.fromValue(type), speed, duration, palette); + } + + public Effect() { + this(EffectType.OFF, 3000L, 0L, new HSBK[0]); + } + + public static Effect createDefault(String type, @Nullable Long morphSpeed, @Nullable Long flameSpeed) { + Long speed; + EffectType effectType = EffectType.fromValue(type); + switch (effectType) { + case OFF: + return new Effect(effectType, 0L, 0L, new HSBK[0]); + case MORPH: + if (morphSpeed == null) { + speed = 3000L; + } else { + speed = morphSpeed; + } + HSBK[] p = { new HSBK(0, 65535, 65535, 3500), new HSBK(7281, 65535, 65535, 3500), + new HSBK(10922, 65535, 65535, 3500), new HSBK(22209, 65535, 65535, 3500), + new HSBK(43507, 65535, 65535, 3500), new HSBK(49333, 65535, 65535, 3500), + new HSBK(53520, 65535, 65535, 3500) }; + return new Effect(effectType, speed, 0L, p); + case FLAME: + if (flameSpeed == null) { + speed = 4000L; + } else { + speed = flameSpeed; + } + return new Effect(effectType, speed, 0L, new HSBK[0]); + default: + throw new IllegalArgumentException("Unknown effect type"); + } + } + + @Override + public boolean equals(@Nullable Object o) { + if (this == o) { + return true; + } + if (o == null) { + return false; + } + if (o.getClass() != getClass()) { + return false; + } + Effect n = (Effect) o; + return n.getType().equals(this.getType()) && n.duration.equals(this.duration) && n.speed.equals(this.speed) + && n.palette == this.palette; + } + + @Override + public int hashCode() { + Long hash = 1L; + int prime = 31; + hash = prime * hash + type.hashCode(); + hash = prime * hash + duration; + hash = prime * hash + speed; + hash = prime * hash + palette.hashCode(); + return hash.intValue(); + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericHandler.java new file mode 100644 index 0000000000..8447c2d27b --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericHandler.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.nio.ByteBuffer; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * A generic handler that dynamically creates "standard" packet instances. + * + *

+ * Packet types must have an empty constructor and cannot require any + * additional logic (other than parsing). + * + * @param the packet subtype this handler constructs + * + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +@NonNullByDefault +public class GenericHandler implements PacketHandler { + + private Constructor constructor; + + private boolean typeFound; + private int type; + + public boolean isTypeFound() { + return typeFound; + } + + public int getType() { + return type; + } + + public GenericHandler(Class clazz) { + try { + constructor = clazz.getConstructor(); + } catch (NoSuchMethodException ex) { + throw new IllegalArgumentException("Packet class cannot be handled by GenericHandler", ex); + } + + try { + Field typeField = clazz.getField("TYPE"); + type = (int) typeField.get(null); + typeFound = true; + } catch (NoSuchFieldException | IllegalAccessException ex) { + // silently ignore + typeFound = false; + } + } + + @Override + public T handle(ByteBuffer buf) { + try { + T ret = constructor.newInstance(); + ret.parse(buf); + return ret; + } catch (ReflectiveOperationException ex) { + throw new IllegalArgumentException("Unable to instantiate empty packet", ex); + } + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericPacket.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericPacket.java new file mode 100644 index 0000000000..b0a738054b --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GenericPacket.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GenericPacket extends Packet { + + @Override + public int packetType() { + return 0; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetColorZonesRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetColorZonesRequest.java new file mode 100644 index 0000000000..fbdc0ddec7 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetColorZonesRequest.java @@ -0,0 +1,85 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import static org.openhab.binding.lifx.internal.LifxBindingConstants.*; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class GetColorZonesRequest extends Packet { + + public static final int TYPE = 0x1F6; + + public static final Field FIELD_START_INDEX = new UInt8Field(); + public static final Field FIELD_END_INDEX = new UInt8Field(); + + private int startIndex = MIN_ZONE_INDEX; + private int endIndex = MAX_ZONE_INDEX; + + public GetColorZonesRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public GetColorZonesRequest(int index) { + this(index, index); + } + + public GetColorZonesRequest(int startIndex, int endIndex) { + this(); + this.startIndex = startIndex; + this.endIndex = endIndex; + } + + public int getStartIndex() { + return startIndex; + } + + public int getEndIndex() { + return endIndex; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + startIndex = FIELD_START_INDEX.value(bytes); + endIndex = FIELD_END_INDEX.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_START_INDEX.bytes(startIndex)) + .put(FIELD_END_INDEX.bytes(endIndex)); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateMultiZoneResponse.TYPE, StateZoneResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetEchoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetEchoRequest.java new file mode 100644 index 0000000000..be09a903d5 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetEchoRequest.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetEchoRequest extends Packet { + + public static final int TYPE = 0x3A; + + public static final Field FIELD_PAYLOAD = new ByteField(64); + + private ByteBuffer payload; + + public GetEchoRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public ByteBuffer getPayload() { + return payload; + } + + public void setPayload(ByteBuffer payload) { + this.payload = payload; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 64; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_PAYLOAD.bytes(payload)); + } + + @Override + public int[] expectedResponses() { + return new int[] { EchoRequestResponse.TYPE }; + } + + public static GetEchoRequest currentTimeEchoRequest() { + ByteBuffer payload = ByteBuffer.allocate(Long.SIZE / 8); + payload.putLong(System.currentTimeMillis()); + + GetEchoRequest request = new GetEchoRequest(); + request.setResponseRequired(true); + request.setPayload(payload); + return request; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetGroupRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetGroupRequest.java new file mode 100644 index 0000000000..a6f8e134b9 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetGroupRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetGroupRequest extends Packet { + + public static final int TYPE = 0x33; + + public GetGroupRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateGroupResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostFirmwareRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostFirmwareRequest.java new file mode 100644 index 0000000000..814f8738e6 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostFirmwareRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetHostFirmwareRequest extends Packet { + + public static final int TYPE = 0x0E; + + public GetHostFirmwareRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateHostFirmwareResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostInfoRequest.java new file mode 100644 index 0000000000..a2f1b5131e --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetHostInfoRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetHostInfoRequest extends Packet { + + public static final int TYPE = 0x0C; + + public GetHostInfoRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateHostInfoResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetInfoRequest.java new file mode 100644 index 0000000000..47212e8648 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetInfoRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetInfoRequest extends Packet { + + public static final int TYPE = 0x22; + + public GetInfoRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateInfoResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLabelRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLabelRequest.java new file mode 100644 index 0000000000..681ac98ad9 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLabelRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetLabelRequest extends Packet { + + public static final int TYPE = 0x17; + + public GetLabelRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLabelResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightInfraredRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightInfraredRequest.java new file mode 100644 index 0000000000..f4b2ce1d88 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightInfraredRequest.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Wouter Born - Initial contribution + */ +public class GetLightInfraredRequest extends Packet { + + public static final int TYPE = 0x78; + + public GetLightInfraredRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLightInfraredResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightPowerRequest.java new file mode 100644 index 0000000000..2fe69366d0 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLightPowerRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetLightPowerRequest extends Packet { + + public static final int TYPE = 0x74; + + public GetLightPowerRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLightPowerResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLocationRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLocationRequest.java new file mode 100644 index 0000000000..af75939bee --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetLocationRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetLocationRequest extends Packet { + + public static final int TYPE = 0x30; + + public GetLocationRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLocationResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetPowerRequest.java new file mode 100644 index 0000000000..2b9bfdddbc --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetPowerRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetPowerRequest extends Packet { + + public static final int TYPE = 0x14; + + public GetPowerRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StatePowerResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetRequest.java new file mode 100644 index 0000000000..ab64dc6d50 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetRequest extends Packet { + + public static final int TYPE = 0x65; + + public GetRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetServiceRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetServiceRequest.java new file mode 100644 index 0000000000..b0b8d745df --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetServiceRequest.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetServiceRequest extends Packet { + + public static final int TYPE = 0x02; + + public GetServiceRequest() { + setTagged(true); + setAddressable(true); + } + + @Override + public int packetLength() { + return 0; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // empty + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateServiceResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagLabelsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagLabelsRequest.java new file mode 100644 index 0000000000..c3c313e956 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagLabelsRequest.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetTagLabelsRequest extends Packet { + + public static final int TYPE = 0x1D; + + public static final Field FIELD_TAGS = new UInt64Field(); + + private long tags; + + public long getTags() { + return tags; + } + + public GetTagLabelsRequest() { + } + + public GetTagLabelsRequest(long tags) { + this.tags = tags; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 8; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + tags = FIELD_TAGS.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); + } + + @Override + public int[] expectedResponses() { + return new int[] { TagLabelsResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagsRequest.java new file mode 100644 index 0000000000..c0570cca52 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTagsRequest.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetTagsRequest extends Packet { + + public static final int TYPE = 0x1A; + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { TagsResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTileEffectRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTileEffectRequest.java new file mode 100644 index 0000000000..fde9dcc647 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetTileEffectRequest.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * Implementation of GetTileEffect packet + * + * @author Pawel Pieczul - Initial contribution + */ +public class GetTileEffectRequest extends Packet { + + public static final int TYPE = 0x2CE; + + public GetTileEffectRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateTileEffectResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetVersionRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetVersionRequest.java new file mode 100644 index 0000000000..a278480581 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetVersionRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetVersionRequest extends Packet { + + public static final int TYPE = 0x20; + + public GetVersionRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateVersionResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiFirmwareRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiFirmwareRequest.java new file mode 100644 index 0000000000..e08771de08 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiFirmwareRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetWifiFirmwareRequest extends Packet { + + public static final int TYPE = 0x12; + + public GetWifiFirmwareRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateWifiFirmwareResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiInfoRequest.java new file mode 100644 index 0000000000..3b97981a1c --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/GetWifiInfoRequest.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class GetWifiInfoRequest extends Packet { + + public static final int TYPE = 0x10; + + public GetWifiInfoRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 0; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + // do nothing + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(0); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateWifiInfoResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Packet.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Packet.java new file mode 100644 index 0000000000..3b65afc370 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/Packet.java @@ -0,0 +1,397 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.MACAddress; +import org.openhab.binding.lifx.internal.fields.MACAddressField; +import org.openhab.binding.lifx.internal.fields.UInt16Field; +import org.openhab.binding.lifx.internal.fields.UInt32Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * Represents an abstract packet, providing conversion functionality to and from + * {@link ByteBuffer}s for common packet (preamble) fields. Subtypes of this + * class can provide conversion functionality for specialized fields. + * + *

+ * Defining new packet types essentially involves extending this class, + * defining the fields and implementing {@link #packetType()}, + * {@link #packetLength()}, and {@link #packetBytes()}. By convention, packet + * type should be stored in a {@code public static final int PACKET_TYPE} field + * in each subtype, followed by a listing of fields contained in the packet. + * Field definitions should remain accessible to outside classes in the event + * they need to worked with directly elsewhere. + * + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public abstract class Packet { + + public static final Field FIELD_SIZE = new UInt16Field().little(); + public static final Field FIELD_PROTOCOL = new UInt16Field().little(); + public static final Field FIELD_SOURCE = new UInt32Field().little(); + public static final Field FIELD_TARGET = new MACAddressField(); + public static final Field FIELD_RESERVED_1 = new ByteField(6); + public static final Field FIELD_ACK = new UInt8Field(); + public static final Field FIELD_SEQUENCE = new UInt8Field().little(); + public static final Field FIELD_RESERVED_2 = new ByteField(8); + public static final Field FIELD_PACKET_TYPE = new UInt16Field().little(); + public static final Field FIELD_RESERVED_3 = new ByteField(2); + + /** + * An ordered array of all fields contained in the common packet preamble. + */ + public static final Field[] PREAMBLE_FIELDS = new Field[] { FIELD_SIZE, FIELD_PROTOCOL, FIELD_SOURCE, + FIELD_TARGET, FIELD_RESERVED_1, FIELD_ACK, FIELD_SEQUENCE, FIELD_RESERVED_2, FIELD_PACKET_TYPE, + FIELD_RESERVED_3 }; + + protected int size; + protected int protocol; + protected long source; + protected MACAddress target; + protected ByteBuffer reserved1; + protected int ackbyte; + protected int sequence; + protected ByteBuffer reserved2; + protected int packetType; + protected ByteBuffer reserved3; + + protected long timeStamp; + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public int getOrigin() { + return (protocol & 0xC000) >> 14; + } + + public void setOrigin(int origin) { + protocol = (protocol & ~(1 << 14)) | (origin << 14); + } + + public boolean getTagged() { + return (protocol & 0x2000) >> 13 == 1 ? true : false; + } + + public void setTagged(boolean flag) { + protocol = (protocol & ~(1 << 13)) | ((flag ? 1 : 0) << 13); + } + + public boolean getAddressable() { + return (protocol & 0x1000) >> 12 == 1 ? true : false; + } + + public void setAddressable(boolean flag) { + this.protocol = (protocol & ~(1 << 12)) | ((flag ? 1 : 0) << 12); + } + + public int getProtocol() { + return protocol & 0x0FFF; + } + + public void setProtocol(int protocol) { + this.protocol = this.protocol | protocol; + } + + public long getSource() { + return source; + } + + public void setSource(long source) { + this.source = source; + } + + public MACAddress getTarget() { + return target; + } + + public void setTarget(MACAddress lightAddress) { + this.target = lightAddress != null ? lightAddress : MACAddress.BROADCAST_ADDRESS; + } + + public ByteBuffer getReserved1() { + return reserved1; + } + + public void setReserved1(ByteBuffer reserved2) { + this.reserved1 = reserved2; + } + + public boolean getAckRequired() { + return (ackbyte & 0x02) >> 1 == 1 ? true : false; + } + + public void setAckRequired(boolean flag) { + this.ackbyte = (ackbyte & ~(1 << 1)) | ((flag ? 1 : 0) << 1); + } + + public boolean getResponseRequired() { + return (ackbyte & 0x01) >> 0 == 1 ? true : false; + } + + public void setResponseRequired(boolean flag) { + this.ackbyte = (ackbyte & ~(1 << 0)) | ((flag ? 1 : 0) << 0); + } + + public int getSequence() { + return sequence; + } + + public void setSequence(int sequence) { + if (0 <= sequence && sequence < 256) { + this.sequence = sequence; + } else { + throw new IllegalArgumentException("Sequence number '" + sequence + "' is not in range [0, 255]"); + } + } + + public ByteBuffer getReserved2() { + return reserved2; + } + + public void setReserved2(ByteBuffer reserved3) { + this.reserved2 = reserved3; + } + + public int getPacketType() { + return packetType; + } + + public void setPacketType(int packetType) { + this.packetType = packetType; + } + + public ByteBuffer getReserved3() { + return reserved3; + } + + public void setReserved3(ByteBuffer reserved4) { + this.reserved3 = reserved4; + } + + public long getTimeStamp() { + return timeStamp; + } + + /** + * Creates an empty packet, setting some default values via + * {@link #preambleDefaults()}. + */ + public Packet() { + preambleDefaults(); + timeStamp = System.currentTimeMillis(); + } + + /** + * Parses, in order, the defined preamble fields, storing collected values. + * The buffer's position will be left at the end of the parsed fields and + * should be equal to the value returned by {@link #preambleLength()}. + * + * @param bytes the buffer to read from. + */ + protected void parsePreamble(ByteBuffer bytes) { + size = FIELD_SIZE.value(bytes); + protocol = FIELD_PROTOCOL.value(bytes); + source = FIELD_SOURCE.value(bytes); + target = FIELD_TARGET.value(bytes); + reserved1 = FIELD_RESERVED_1.value(bytes); + ackbyte = FIELD_ACK.value(bytes); + sequence = FIELD_SEQUENCE.value(bytes); + reserved2 = FIELD_RESERVED_2.value(bytes); + packetType = FIELD_PACKET_TYPE.value(bytes); + reserved3 = FIELD_RESERVED_3.value(bytes); + } + + /** + * Calculates the length of the packet header, defined as the sum of the + * lengths of all defined fields (see {@link #PREAMBLE_FIELDS}). + * + * @return the sum of the length of preamble fields + */ + protected int preambleLength() { + int sum = 0; + + for (Field f : PREAMBLE_FIELDS) { + sum += f.getLength(); + } + + return sum; + } + + /** + * Returns a new {@code ByteBuffer} containing the encoded preamble. Note + * that the returned buffer will have its position set at the end of the + * buffer and will need to have {@link ByteBuffer#rewind()} called before + * use. + * + *

+ * The length of the buffer is the sum of the lengths of the defined + * preamble fields (see {@link #PREAMBLE_FIELDS} for an ordered list), which + * may also be accessed via {@link #preambleLength()}. + * + *

+ * Certain fields are set to default values based on other class methods. + * For example, the size and packet type fields will be set to the values + * returned from {@link #length()} and {@link #packetType()}, respectively. + * Other defaults (such as the protocol, light address, site, and timestamp) + * may be specified either by directly setting the relevant protected + * variables or by overriding {@link #preambleDefaults()}. + * + * @return a new buffer containing the encoded preamble + */ + protected ByteBuffer preambleBytes() { + return ByteBuffer.allocate(preambleLength()).put(FIELD_SIZE.bytes(length())).put(FIELD_PROTOCOL.bytes(protocol)) + .put(FIELD_SOURCE.bytes(source)).put(FIELD_TARGET.bytes(target)) + .put(ByteBuffer.allocate(FIELD_RESERVED_1.getLength())) // empty + .put(FIELD_ACK.bytes(ackbyte)).put(FIELD_SEQUENCE.bytes(sequence)) + .put(ByteBuffer.allocate(FIELD_RESERVED_2.getLength())) // empty + .put(FIELD_PACKET_TYPE.bytes(packetType())).put(ByteBuffer.allocate(FIELD_RESERVED_3.getLength())); // empty + } + + /** + * Sets default preamble values. If needed, subclasses may override these + * values by specifically overriding this method, or by setting individual + * values within the constructor, as this method is called automatically + * during initialization. + */ + protected void preambleDefaults() { + size = 0; + protocol = 1024; + target = new MACAddress(); + sequence = 0; + packetType = packetType(); + } + + /** + * Returns the packet type. Note that this value is technically distinct + * from {@code getPacketType()} in that it returns the packet type the + * current {@code Packet} subtype is designed to parse, while + * {@code getPacketType()} returns the actual {@code packetType} field of + * a parsed packet. However, these values should always match. + * + * @return the packet type intended to be handled by this Packet subtype + */ + public abstract int packetType(); + + /** + * Returns the length of the payload specific to this packet subtype. The + * length of the preamble is specifically excluded. + * + * @return the length of this specialized packet payload + */ + protected abstract int packetLength(); + + /** + * Parses the given {@link ByteBuffer} into class fields. Subtypes may + * implement {@link #parsePacket(ByteBuffer)} to parse additional fields; + * the preamble by default is always parsed. + * + * @param bytes the buffer to extract data from + */ + public void parse(ByteBuffer bytes) { + bytes.rewind(); + parsePreamble(bytes); + parsePacket(bytes); + } + + /** + * Extracts data from the given {@link ByteBuffer} into fields specific to + * this packet subtype. The preamble will already have been parsed; as such, + * the buffer will be positioned at the end of the preamble. If needed, + * {@link #preambleLength()} may be used to restore the position of the + * buffer. + * + * @param bytes the raw bytes to parse + */ + protected abstract void parsePacket(ByteBuffer bytes); + + /** + * Returns a {@link ByteBuffer} containing the full payload for this packet, + * including the populated preamble and any specialized packet payload. The + * returned buffer will be at position zero. + * + * @return the full packet payload + */ + public ByteBuffer bytes() { + ByteBuffer preamble = preambleBytes(); + preamble.rewind(); + + ByteBuffer packet = packetBytes(); + packet.rewind(); + + ByteBuffer ret = ByteBuffer.allocate(length()).put(preamble).put(packet); + ret.rewind(); + + return ret; + } + + /** + * Returns a {@link ByteBuffer} containing the payload for this packet. Its + * length must match the value of {@link #packetLength()}. This specifically + * excludes preamble fields and should contain only data specific to the + * packet subtype. + *

+ * Note that returned ByteBuffers will have {@link ByteBuffer#rewind()} + * called automatically before they are appended to the final packet + * buffer. + * + * @return the packet payload + */ + protected abstract ByteBuffer packetBytes(); + + /** + * Gets the total length of this packet, in bytes. Specifically, this method + * is the sum of the preamble ({@link #preambleLength()}) and the payload + * length ({@link #packetLength()}); subtypes should override methods for + * those values if desired. + * + * @return the total length of this packet + */ + public int length() { + return preambleLength() + packetLength(); + } + + /** + * Returns a list of expected response packet types. An empty array means + * no responses are expected (suitable for response packet definitions), + * + * @return a list of expected responses + */ + public abstract int[] expectedResponses(); + + public boolean isExpectedResponse(int type) { + for (int a : expectedResponses()) { + if (a == type) { + return true; + } + } + + return false; + } + + public boolean isFulfilled(Packet somePacket) { + if (isExpectedResponse(somePacket.getPacketType())) { + return true; + } + return false; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketFactory.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketFactory.java new file mode 100644 index 0000000000..ba9ea39f0f --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketFactory.java @@ -0,0 +1,157 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +/** + * A static factory for registering packet types that may be received and + * dispatched to client cod * request types, like {@code PowerStateRequest}) or types received only via UDP + * e. Packet handlers (used to construct actual packet + * instances) may be retrieved via their packet type. + * + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality + */ +@NonNullByDefault +public class PacketFactory { + + private static @Nullable PacketFactory instance; + + public static synchronized PacketFactory getInstance() { + PacketFactory result = instance; + if (result == null) { + result = new PacketFactory(); + instance = result; + } + return result; + } + + private final Map> handlers; + + private PacketFactory() { + handlers = new HashMap<>(); + + register(AcknowledgementResponse.class); + register(EchoRequestResponse.class); + register(GetColorZonesRequest.class); + register(GetEchoRequest.class); + register(GetGroupRequest.class); + register(GetHostFirmwareRequest.class); + register(GetHostInfoRequest.class); + register(GetInfoRequest.class); + register(GetLabelRequest.class); + register(GetLightInfraredRequest.class); + register(GetLightPowerRequest.class); + register(GetLocationRequest.class); + register(GetPowerRequest.class); + register(GetRequest.class); + register(GetServiceRequest.class); + register(GetTagLabelsRequest.class); + register(GetTagsRequest.class); + register(GetTileEffectRequest.class); + register(GetVersionRequest.class); + register(GetWifiFirmwareRequest.class); + register(GetWifiInfoRequest.class); + register(SetColorRequest.class); + register(SetColorZonesRequest.class); + register(SetDimAbsoluteRequest.class); + register(SetLabelRequest.class); + register(SetLightInfraredRequest.class); + register(SetLightPowerRequest.class); + register(SetPowerRequest.class); + register(SetTagsRequest.class); + register(StateGroupResponse.class); + register(StateHostFirmwareResponse.class); + register(StateHostInfoResponse.class); + register(StateInfoResponse.class); + register(StateLabelResponse.class); + register(StateLightInfraredResponse.class); + register(StateLightPowerResponse.class); + register(StateLocationResponse.class); + register(StateMultiZoneResponse.class); + register(StatePowerResponse.class); + register(StateResponse.class); + register(StateServiceResponse.class); + register(StateTileEffectResponse.class); + register(StateVersionResponse.class); + register(StateWifiFirmwareResponse.class); + register(StateWifiInfoResponse.class); + register(StateZoneResponse.class); + register(TagLabelsResponse.class); + register(TagsResponse.class); + } + + /** + * Registers a packet handler for the given packet type. + * + * @param type the type to register + * @param handler the packet handler to associate with the type + */ + public final void register(int type, PacketHandler handler) { + handlers.put(type, handler); + } + + /** + * Registers a new generic packet handler for the given packet class. The + * packet class must meet the criteria for {@link GenericHandler}; + * specifically, it must have an no-argument constructor and require no + * parsing logic outside of an invocation of + * {@link Packet#parse(java.nio.ByteBuffer)}. + * + * @param type the type of the packet to register + * @param clazz the class of the packet to register + */ + public final void register(int type, Class clazz) { + handlers.put(type, new GenericHandler<>(clazz)); + } + + /** + * Registers a generic packet type. All requirements of + * {@link GenericHandler} must met; specifically, classes must have a + * no-args constructor and require no additional parsing logic. + * Additionally, a public static integer {@code TYPE} field must be defined. + * + * @param the packet type to register + * @param clazz the packet class to register + */ + public final void register(Class clazz) { + GenericHandler handler = new GenericHandler<>(clazz); + + if (!handler.isTypeFound()) { + throw new IllegalArgumentException("Unable to register generic packet with no TYPE field."); + } + + handlers.put(handler.getType(), handler); + } + + /** + * Gets a registered handler for the given packet type, if any exists. If + * no matching handler can be found, {@code null} is returned. + * + * @param packetType the packet type of the handler to retrieve + * @return a packet handler, or null + */ + public @Nullable PacketHandler getHandler(int packetType) { + return handlers.get(packetType); + } + + public static @Nullable PacketHandler createHandler(int packetType) { + return getInstance().getHandler(packetType); + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketHandler.java new file mode 100644 index 0000000000..a93b4759be --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PacketHandler.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * A packet handler responsible for converting a ByteBuffer into a Packet + * instance. + * + * @param the generic packet type + * + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +@NonNullByDefault +public interface PacketHandler { + + /** + * Creates a packet from the given buffer. + * + * @param buf the buffer used for creating the packet + * @return the packet created from the buffer + * @throws IllegalArgumentException when an empty packet could not be created or the data in the buffer + * could not be parsed + */ + public abstract T handle(ByteBuffer buf); +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PowerState.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PowerState.java new file mode 100644 index 0000000000..f12ba58b7d --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/PowerState.java @@ -0,0 +1,57 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import org.openhab.core.library.types.OnOffType; + +/** + * Represents light power states (on or off). + * + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + * @author Wouter Born - Added OnOffType conversion methods + */ +public enum PowerState { + + ON(0xFFFF), + OFF(0x0000); + + private final int value; + + private PowerState(int value) { + this.value = value; + } + + /** + * Gets the integer value of this power state. + * + * @return the integer value + */ + public int getValue() { + return value; + } + + public static PowerState fromValue(int value) { + // a response can have a power level between 0 and 65535 when the light + // has just been switched ON or OFF + return value == OFF.value ? OFF : ON; + } + + public static PowerState fromOnOffType(OnOffType onOff) { + return onOff == OnOffType.ON ? ON : OFF; + } + + public OnOffType toOnOffType() { + return this == ON ? OnOffType.ON : OnOffType.OFF; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorRequest.java new file mode 100644 index 0000000000..2ebe1760fe --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorRequest.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetColorRequest extends Packet { + + public static final int TYPE = 0x66; + + public static final Field FIELD_STREAM = new ByteField(1); + public static final HSBKField FIELD_COLOR = new HSBKField(); + public static final Field FIELD_FADE_TIME = new UInt32Field().little(); + + private ByteBuffer stream; + + private HSBK color; + private long fadeTime; + + public ByteBuffer getStream() { + return stream; + } + + public HSBK getColor() { + return color; + } + + public long getFadeTime() { + return fadeTime; + } + + public SetColorRequest() { + stream = ByteBuffer.allocate(1); + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public SetColorRequest(HSBK color, long fadeTime) { + this(); + this.color = color; + this.fadeTime = fadeTime; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 13; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + stream = FIELD_STREAM.value(bytes); + color = FIELD_COLOR.value(bytes); + fadeTime = FIELD_FADE_TIME.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_STREAM.bytes(stream)).put(FIELD_COLOR.bytes(color)) + .put(FIELD_FADE_TIME.bytes(fadeTime)); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorZonesRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorZonesRequest.java new file mode 100644 index 0000000000..5e40d1b06a --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetColorZonesRequest.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import static org.openhab.binding.lifx.internal.LifxBindingConstants.*; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class SetColorZonesRequest extends Packet { + + public static final int TYPE = 0x1F5; + + public static final Field FIELD_START_INDEX = new UInt8Field(); + public static final Field FIELD_END_INDEX = new UInt8Field(); + public static final HSBKField FIELD_COLOR = new HSBKField(); + public static final Field FIELD_FADE_TIME = new UInt32Field().little(); + public static final Field FIELD_APPLY = new UInt8Field(); + + private int startIndex = MIN_ZONE_INDEX; + private int endIndex = MAX_ZONE_INDEX; + private HSBK color; + private long fadeTime; + private ApplicationRequest apply; + + public HSBK getColor() { + return color; + } + + public int getHue() { + return color.getHue(); + } + + public int getSaturation() { + return color.getSaturation(); + } + + public int getBrightness() { + return color.getBrightness(); + } + + public int getKelvin() { + return color.getKelvin(); + } + + public long getFadeTime() { + return fadeTime; + } + + public ApplicationRequest getApply() { + return apply; + } + + public SetColorZonesRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public SetColorZonesRequest(HSBK color, long fadeTime, ApplicationRequest apply) { + this(MIN_ZONE_INDEX, MAX_ZONE_INDEX, color, fadeTime, apply); + } + + public SetColorZonesRequest(int index, HSBK color, long fadeTime, ApplicationRequest apply) { + this(index, index, color, fadeTime, apply); + } + + public SetColorZonesRequest(int startIndex, int endIndex, HSBK color, long fadeTime, ApplicationRequest apply) { + this(); + this.startIndex = startIndex; + this.endIndex = endIndex; + this.color = color; + this.fadeTime = fadeTime; + this.apply = apply; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 15; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + startIndex = FIELD_START_INDEX.value(bytes); + endIndex = FIELD_END_INDEX.value(bytes); + color = FIELD_COLOR.value(bytes); + fadeTime = FIELD_FADE_TIME.value(bytes); + apply = ApplicationRequest.fromValue(FIELD_APPLY.value(bytes)); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_START_INDEX.bytes(startIndex)) + .put(FIELD_END_INDEX.bytes(endIndex)).put(FIELD_COLOR.bytes(color)).put(FIELD_FADE_TIME.bytes(fadeTime)) + .put(FIELD_APPLY.bytes(apply.getValue())); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetDimAbsoluteRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetDimAbsoluteRequest.java new file mode 100644 index 0000000000..ca92dedca7 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetDimAbsoluteRequest.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetDimAbsoluteRequest extends Packet { + + public static final int TYPE = 0x68; + + public static final Field FIELD_DIM = new UInt16Field().little(); + public static final Field FIELD_DURATION = new UInt32Field().little(); + + private int dim; + private long duration; + + public int getDim() { + return dim; + } + + public long getDuration() { + return duration; + } + + public SetDimAbsoluteRequest() { + } + + public SetDimAbsoluteRequest(int dim, long duration) { + this.dim = dim; + this.duration = duration; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 6; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + dim = FIELD_DIM.value(bytes); + duration = FIELD_DURATION.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_DIM.bytes(dim)).put(FIELD_DURATION.bytes(duration)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLabelRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLabelRequest.java new file mode 100644 index 0000000000..ee11b747cb --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLabelRequest.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.StringField; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetLabelRequest extends Packet { + + public static final int TYPE = 0x18; + + public static final Field FIELD_LABEL = new StringField(32).utf8(); + + private String label; + + public SetLabelRequest() { + setTagged(false); + setAddressable(true); + setAckRequired(true); + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 32; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + label = FIELD_LABEL.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return FIELD_LABEL.bytes(label); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightInfraredRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightInfraredRequest.java new file mode 100644 index 0000000000..1a0abda764 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightInfraredRequest.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class SetLightInfraredRequest extends Packet { + + public static final int TYPE = 0x7A; + + public static final Field FIELD_STATE = new UInt16Field().little(); + + private int infrared; + + public int getInfrared() { + return infrared; + } + + public SetLightInfraredRequest() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public SetLightInfraredRequest(int infrared) { + this(); + this.infrared = infrared; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + infrared = FIELD_STATE.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(2).put(FIELD_STATE.bytes(infrared)); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLightInfraredResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightPowerRequest.java new file mode 100644 index 0000000000..ecbfb7a1a7 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetLightPowerRequest.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetLightPowerRequest extends Packet { + + public static final int TYPE = 0x75; + + public static final Field FIELD_STATE = new UInt16Field(); + public static final Field FIELD_DURATION = new UInt32Field().little(); + + private PowerState state; + private long duration; + + public PowerState getState() { + return state; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public SetLightPowerRequest() { + state = PowerState.OFF; + this.duration = 0; + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public SetLightPowerRequest(PowerState state) { + this.state = state; + this.duration = 0; + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 6; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + state = PowerState.fromValue(FIELD_STATE.value(bytes)); + duration = FIELD_DURATION.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())) + .put(FIELD_DURATION.bytes(duration)); + } + + @Override + public int[] expectedResponses() { + return new int[] { StateLightPowerResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetPowerRequest.java new file mode 100644 index 0000000000..ff4988b08b --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetPowerRequest.java @@ -0,0 +1,74 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetPowerRequest extends Packet { + + public static final int TYPE = 0x15; + + public static final Field FIELD_STATE = new UInt16Field(); + + private PowerState state; + + public PowerState getState() { + return state; + } + + public SetPowerRequest() { + state = PowerState.OFF; + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + public SetPowerRequest(PowerState state) { + this.state = state; + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + state = PowerState.fromValue(FIELD_STATE.value(bytes)); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(2).put(FIELD_STATE.bytes(state.getValue())); + } + + @Override + public int[] expectedResponses() { + return new int[] { StatePowerResponse.TYPE }; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTagsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTagsRequest.java new file mode 100644 index 0000000000..abbf5b58dd --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTagsRequest.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class SetTagsRequest extends Packet { + + public static final int TYPE = 0x1B; + + public static final Field FIELD_TAGS = new UInt64Field(); + + private long tags; + + public long getTags() { + return tags; + } + + public void setTags(long tags) { + this.tags = tags; + } + + public SetTagsRequest() { + } + + public SetTagsRequest(long tags) { + this.tags = tags; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 8; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + tags = FIELD_TAGS.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTileEffectRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTileEffectRequest.java new file mode 100644 index 0000000000..21d398e495 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SetTileEffectRequest.java @@ -0,0 +1,153 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of SetTileEffect packet + * + * @author Pawel Pieczul - Initial contribution + */ +public class SetTileEffectRequest extends Packet { + + public static final int TYPE = 0x2CF; + + // size.from.to....what + // ------------------------------ + // 1....0....0.....reserved + // 1....1....1.....reserved + // 4....2....5.....instance ID + // 1....6....6.....effect type + // 4....7....10....speed + // 8....11...18....duration + // 4....19...22....reserved + // 4....23...26....reserved + // 32...27...58....parameters (8*32 bits) + // 1....59...59....palette count + // 128..60...187...palette (16*8 bits) + + private static final Field FIELD_RESERVED_0 = new UInt8Field(); + private static final Field FIELD_RESERVED_1 = new UInt8Field(); + private static final Field FIELD_INSTANCE_ID = new UInt32Field().little(); + private static final Field FIELD_TYPE = new UInt8Field(); + private static final Field FIELD_SPEED = new UInt32Field().little(); + private static final Field FIELD_DURATION = new UInt64Field().little(); + private static final Field FIELD_RESERVED_19_TO_26 = new UInt32Field().little(); + private static final Field FIELD_PARAMETER_27_TO_58 = new UInt32Field().little(); + private static final Field FIELD_PALETTE_COUNT = new UInt8Field(); + private static final Field FIELD_PALETTE_60_TO_187 = new HSBKField(); + + private final Logger logger = LoggerFactory.getLogger(SetTileEffectRequest.class); + + private Integer reserved0 = 0; + private Integer reserved1 = 0; + private Long reserved19to22 = 0L; + private Long reserved23to26 = 0L; + private Effect effect; + + public SetTileEffectRequest() { + setTagged(false); + setAddressable(true); + setAckRequired(true); + } + + public SetTileEffectRequest(Effect effect) { + this(); + this.effect = effect; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 188; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + reserved0 = FIELD_RESERVED_0.value(bytes); + reserved1 = FIELD_RESERVED_1.value(bytes); + FIELD_INSTANCE_ID.value(bytes); + Integer effectType = FIELD_TYPE.value(bytes); + Long speed = FIELD_SPEED.value(bytes); + Long duration = FIELD_DURATION.value(bytes); + reserved19to22 = FIELD_RESERVED_19_TO_26.value(bytes); + reserved23to26 = FIELD_RESERVED_19_TO_26.value(bytes); + Long[] parameters = new Long[8]; + for (int i = 0; i < parameters.length; i++) { + parameters[i] = FIELD_PARAMETER_27_TO_58.value(bytes); + } + Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes); + HSBK[] palette = new HSBK[paletteCount]; + for (int i = 0; i < palette.length; i++) { + palette[i] = FIELD_PALETTE_60_TO_187.value(bytes); + } + try { + effect = new Effect(effectType, speed, duration, palette); + } catch (IllegalArgumentException e) { + logger.debug("Wrong effect type received: {}", effectType); + effect = null; + } + } + + @Override + protected ByteBuffer packetBytes() { + ByteBuffer buffer = ByteBuffer.allocate(packetLength()); + buffer.put(FIELD_RESERVED_0.bytes(reserved0)); + buffer.put(FIELD_RESERVED_1.bytes(reserved1)); + buffer.put(FIELD_INSTANCE_ID.bytes(0L)); + buffer.put(FIELD_TYPE.bytes(effect.getType().intValue())); + buffer.put(FIELD_SPEED.bytes(effect.getSpeed())); + buffer.put(FIELD_DURATION.bytes(effect.getDuration())); + buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved19to22)); + buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved23to26)); + for (int i = 0; i < 8; i++) { + buffer.put(FIELD_PARAMETER_27_TO_58.bytes(0L)); + } + buffer.put(FIELD_PALETTE_COUNT.bytes(effect.getPalette().length)); + HSBK[] palette = effect.getPalette(); + for (int i = 0; i < palette.length; i++) { + buffer.put(FIELD_PALETTE_60_TO_187.bytes(palette[i])); + } + HSBK hsbkZero = new HSBK(0, 0, 0, 0); + for (int i = 0; i < 16 - palette.length; i++) { + buffer.put(FIELD_PALETTE_60_TO_187.bytes(hsbkZero)); + } + if (logger.isDebugEnabled()) { + logger.debug("SetTileEffectRequest: type={}, speed={}, duration={}, palette_count={}", effect.getType(), + effect.getSpeed(), effect.getDuration(), palette.length); + for (int i = 0; i < palette.length; i++) { + logger.debug("SetTileEffectRequest palette[{}] = {}", i, palette[i]); + } + } + return buffer; + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SignalStrength.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SignalStrength.java new file mode 100644 index 0000000000..716a93f940 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/SignalStrength.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +/** + * The signal strength of a light. + * + * @author Wouter Born - Initial contribution + */ +public class SignalStrength { + + private double milliWatts; + + public SignalStrength(double milliWatts) { + this.milliWatts = milliWatts; + } + + /** + * Returns the signal strength. + * + * @return the signal strength in milliwatts (mW). + */ + public double getMilliWatts() { + return milliWatts; + } + + /** + * Returns the signal strength as a quality percentage: + *

    + *
  • RSSI <= -100: returns 0 + *
  • -100 < RSSI < -50: returns a value between 0 and 1 (linearly distributed) + *
  • RSSI >= -50: returns 1 + *
      + * + * @return a value between 0 and 1. 0 being worst strength and 1 + * being best strength. + */ + public double toQualityPercentage() { + return Math.min(100, Math.max(0, 2 * (toRSSI() + 100))) / 100; + } + + /** + * Returns the signal strength as a quality rating. + * + * @return one of the values: 0, 1, 2, 3 or 4. 0 being worst strength and 4 + * being best strength. + */ + public byte toQualityRating() { + return (byte) Math.round(toQualityPercentage() * 4); + } + + /** + * Returns the received signal strength indicator (RSSI). + * + * @return a value <= 0. 0 being best strength and more negative values indicate worser strength. + */ + public double toRSSI() { + return 10 * Math.log10(milliWatts); + } + + @Override + public String toString() { + return "SignalStrength [milliWatts=" + milliWatts + ", rssi=" + Math.round(toRSSI()) + "]"; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateGroupResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateGroupResponse.java new file mode 100644 index 0000000000..2463edacf9 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateGroupResponse.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.StringField; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateGroupResponse extends Packet { + + public static final int TYPE = 0x35; + + public static final Field FIELD_GROUP = new ByteField(16); + public static final Field FIELD_LABEL = new StringField(32).utf8(); + public static final Field FIELD_UPDATED_AT = new UInt64Field().little(); + + private ByteBuffer group; + private String label; + private long updatedAt; + + public static int getType() { + return TYPE; + } + + public ByteBuffer getGroup() { + return group; + } + + public void setGroup(ByteBuffer group) { + this.group = group; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public long getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 14; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + group = FIELD_GROUP.value(bytes); + label = FIELD_LABEL.value(bytes); + updatedAt = FIELD_UPDATED_AT.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_GROUP.bytes(group)).put(FIELD_LABEL.bytes(label)) + .put(FIELD_UPDATED_AT.bytes(updatedAt)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostFirmwareResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostFirmwareResponse.java new file mode 100644 index 0000000000..065c06595f --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostFirmwareResponse.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; +import org.openhab.binding.lifx.internal.fields.Version; +import org.openhab.binding.lifx.internal.fields.VersionField; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateHostFirmwareResponse extends Packet { + + public static final int TYPE = 0x0F; + + public static final Field FIELD_BUILD = new UInt64Field().little(); + public static final Field FIELD_RESERVED = new UInt64Field().little(); + public static final Field FIELD_VERSION = new VersionField().little(); + + private long build; + private long reserved; + private Version version; + + public long getBuild() { + return build; + } + + public void setBuild(long build) { + this.build = build; + } + + public Version getVersion() { + return version; + } + + public void setVersion(Version version) { + this.version = version; + } + + public StateHostFirmwareResponse() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 20; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + build = FIELD_BUILD.value(bytes); + reserved = FIELD_RESERVED.value(bytes); + version = FIELD_VERSION.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_BUILD.bytes(build)).put(FIELD_RESERVED.bytes(reserved)) + .put(FIELD_VERSION.bytes(version)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostInfoResponse.java new file mode 100644 index 0000000000..1690bd2351 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateHostInfoResponse.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.FloatField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateHostInfoResponse extends Packet { + + public static final int TYPE = 0x0D; + + public static final Field FIELD_SIGNAL = new FloatField().little(); + public static final Field FIELD_TX = new UInt32Field().little(); + public static final Field FIELD_RX = new UInt32Field().little(); + public static final Field FIELD_RESERVED_5 = new ByteField(2); + + private float signal; + private long tx; + private long rx; + + public SignalStrength getSignalStrength() { + return new SignalStrength(signal); + } + + public long getTx() { + return tx; + } + + public long getRx() { + return rx; + } + + public StateHostInfoResponse() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 14; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + signal = FIELD_SIGNAL.value(bytes); + tx = FIELD_TX.value(bytes); + rx = FIELD_RX.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_SIGNAL.bytes(signal)).put(FIELD_TX.bytes(tx)) + .put(FIELD_RX.bytes(rx)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateInfoResponse.java new file mode 100644 index 0000000000..ca3ccf276d --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateInfoResponse.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateInfoResponse extends Packet { + + public static final int TYPE = 0x23; + + public static final Field FIELD_TIME = new UInt64Field().little(); + public static final Field FIELD_UPTIME = new UInt64Field().little(); + public static final Field FIELD_DOWNTIME = new UInt64Field().little(); + + private long time; + private long uptime; + private long downtime; + + public long getTime() { + return time; + } + + public void setTime(long time) { + this.time = time; + } + + public long getUptime() { + return uptime; + } + + public void setUptime(long uptime) { + this.uptime = uptime; + } + + public long getDowntime() { + return downtime; + } + + public void setDowntime(long downtime) { + this.downtime = downtime; + } + + public StateInfoResponse() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 24; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + time = FIELD_TIME.value(bytes); + uptime = FIELD_UPTIME.value(bytes); + downtime = FIELD_DOWNTIME.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_TIME.bytes(time)).put(FIELD_UPTIME.bytes(uptime)) + .put(FIELD_DOWNTIME.bytes(downtime)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLabelResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLabelResponse.java new file mode 100644 index 0000000000..409d61f400 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLabelResponse.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.StringField; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateLabelResponse extends Packet { + + public static final int TYPE = 0x19; + + public static final Field FIELD_LABEL = new StringField(32).utf8(); + + private String label; + + public static int getType() { + return TYPE; + } + + public static Field getFieldLabel() { + return FIELD_LABEL; + } + + public String getLabel() { + return label; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 32; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + label = FIELD_LABEL.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return FIELD_LABEL.bytes(label); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightInfraredResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightInfraredResponse.java new file mode 100644 index 0000000000..53d561be49 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightInfraredResponse.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class StateLightInfraredResponse extends Packet { + + public static final int TYPE = 0x79; + + public static final Field FIELD_STATE = new UInt16Field().little(); + + private int infrared; + + public int getInfrared() { + return infrared; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + infrared = FIELD_STATE.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(infrared)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightPowerResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightPowerResponse.java new file mode 100644 index 0000000000..9d248a16ee --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLightPowerResponse.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateLightPowerResponse extends Packet { + + public static final int TYPE = 0x76; + + public static final Field FIELD_STATE = new UInt16Field(); + + private PowerState state; + + public PowerState getState() { + return state; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + int stateValue = FIELD_STATE.value(bytes); + state = PowerState.fromValue(stateValue); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLocationResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLocationResponse.java new file mode 100644 index 0000000000..77c0146889 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateLocationResponse.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.ByteField; +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.StringField; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateLocationResponse extends Packet { + + public static final int TYPE = 0x32; + + public static final Field FIELD_LOCATION = new ByteField(16); + public static final Field FIELD_LABEL = new StringField(32).utf8(); + public static final Field FIELD_UPDATED_AT = new UInt64Field().little(); + + private ByteBuffer location; + private String label; + private long updatedAt; + + public static int getType() { + return TYPE; + } + + public ByteBuffer getLocation() { + return location; + } + + public void setLocation(ByteBuffer location) { + this.location = location; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public long getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 14; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + location = FIELD_LOCATION.value(bytes); + label = FIELD_LABEL.value(bytes); + updatedAt = FIELD_UPDATED_AT.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_LOCATION.bytes(location)).put(FIELD_LABEL.bytes(label)) + .put(FIELD_UPDATED_AT.bytes(updatedAt)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateMultiZoneResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateMultiZoneResponse.java new file mode 100644 index 0000000000..4e2443bdd2 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateMultiZoneResponse.java @@ -0,0 +1,98 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class StateMultiZoneResponse extends Packet { + + public static final int TYPE = 0x1FA; + public static final int ZONES = 8; + + public static final Field FIELD_COUNT = new UInt8Field(); + public static final Field FIELD_INDEX = new UInt8Field(); + public static final HSBKField FIELD_COLOR = new HSBKField(); + + private int count; + private int index; + private HSBK[] colors = new HSBK[ZONES]; + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("count="); + sb.append(count); + sb.append(", index="); + sb.append(index); + + for (int i = 0; i < ZONES; i++) { + sb.append(", "); + sb.append(colors[i].toString("color[" + i + "]")); + } + + return sb.toString(); + } + + public int getCount() { + return count; + } + + public int getIndex() { + return index; + } + + public HSBK[] getColors() { + return colors; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 66; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + count = FIELD_COUNT.value(bytes); + index = FIELD_INDEX.value(bytes); + for (int i = 0; i < ZONES; i++) { + colors[i] = FIELD_COLOR.value(bytes); + } + } + + @Override + protected ByteBuffer packetBytes() { + ByteBuffer bb = ByteBuffer.allocate(packetLength()).put(FIELD_COUNT.bytes(count)).put(FIELD_INDEX.bytes(index)); + for (int i = 0; i < ZONES; i++) { + bb.put(FIELD_COLOR.bytes(colors[i])); + } + return bb; + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StatePowerResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StatePowerResponse.java new file mode 100644 index 0000000000..7d86030cfc --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StatePowerResponse.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt16Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StatePowerResponse extends Packet { + + public static final int TYPE = 0x16; + + public static final Field FIELD_STATE = new UInt16Field(); + + private PowerState state; + + public PowerState getState() { + return state; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 2; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + int stateValue = FIELD_STATE.value(bytes); + state = PowerState.fromValue(stateValue); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateResponse.java new file mode 100644 index 0000000000..808577b376 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateResponse.java @@ -0,0 +1,98 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.StringField; +import org.openhab.binding.lifx.internal.fields.UInt16Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateResponse extends Packet { + + public static final int TYPE = 0x6B; + + public static final HSBKField FIELD_COLOR = new HSBKField(); + public static final Field FIELD_DIM = new UInt16Field().little(); + public static final Field FIELD_POWER = new UInt16Field(); + public static final Field FIELD_LABEL = new StringField(32); + public static final Field FIELD_TAGS = new UInt64Field(); + + private HSBK color; + private int dim; + private PowerState power; + private String label; + private long tags; + + @Override + public String toString() { + return color.toString("color") + ", dim=" + dim + ", power=" + power + ", label=" + label; + } + + public HSBK getColor() { + return color; + } + + public int getDim() { + return dim; + } + + public PowerState getPower() { + return power; + } + + public String getLabel() { + return label; + } + + public long getTags() { + return tags; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 52; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + color = FIELD_COLOR.value(bytes); + dim = FIELD_DIM.value(bytes); + power = PowerState.fromValue(FIELD_POWER.value(bytes)); + label = FIELD_LABEL.value(bytes); + tags = FIELD_TAGS.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_COLOR.bytes(color)).put(FIELD_DIM.bytes(dim)) + .put(FIELD_POWER.bytes(power.getValue())).put(FIELD_LABEL.bytes(label)).put(FIELD_TAGS.bytes(tags)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateServiceResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateServiceResponse.java new file mode 100644 index 0000000000..599da30b70 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateServiceResponse.java @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.LittleField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateServiceResponse extends Packet { + + public static final int TYPE = 0x03; + + public static final Field FIELD_SERVICE = new UInt8Field(); + public static final Field FIELD_PORT = new LittleField<>(new UInt32Field()); + + private int service; + private long port; + + public int getService() { + return service; + } + + public long getPort() { + return port; + } + + public StateServiceResponse() { + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + service = FIELD_SERVICE.value(bytes); + port = FIELD_PORT.value(bytes); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 5; + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_SERVICE.bytes(service)).put(FIELD_PORT.bytes(port)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateTileEffectResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateTileEffectResponse.java new file mode 100644 index 0000000000..c9b6f40e9d --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateTileEffectResponse.java @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt32Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; +import org.openhab.binding.lifx.internal.fields.UInt8Field; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of StateTileEffect packet + * + * @author Pawel Pieczul - Initial contribution + */ +public class StateTileEffectResponse extends Packet { + + public static final int TYPE = 0x2D0; + + // size.from.to....what + // ------------------------------ + // 1....0....0.....reserved + // 4....1....4.....instance ID + // 1....5....5.....effect type + // 4....6....9.....speed + // 8....10...17....duration + // 4....18...21....reserved + // 4....22...25....reserved + // 32...26...57....parameters (8*32 bits) + // 1....58...58....palette count + // 128..59...186...palette (16*8 bits) + + private static final Field FIELD_RESERVED_0 = new UInt8Field(); + private static final Field FIELD_INSTANCE_ID = new UInt32Field().little(); + private static final Field FIELD_TYPE = new UInt8Field(); + private static final Field FIELD_SPEED = new UInt32Field().little(); + private static final Field FIELD_DURATION = new UInt64Field().little(); + private static final Field FIELD_RESERVED_18_TO_25 = new UInt32Field().little(); + private static final Field FIELD_PARAMETER_26_TO_57 = new UInt32Field().little(); + private static final Field FIELD_PALETTE_COUNT = new UInt8Field(); + private static final Field FIELD_PALETTE_59_TO_186 = new HSBKField(); + + private Integer reserved0 = 0; + private Long reserved18to21 = 0L; + private Long reserved22to25 = 0L; + private Effect effect; + + private final Logger logger = LoggerFactory.getLogger(StateTileEffectResponse.class); + + public Effect getEffect() { + return effect; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 187; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + reserved0 = FIELD_RESERVED_0.value(bytes); + Long instanceId = FIELD_INSTANCE_ID.value(bytes); + Integer effectType = FIELD_TYPE.value(bytes); + Long speed = FIELD_SPEED.value(bytes); + Long duration = FIELD_DURATION.value(bytes); + reserved18to21 = FIELD_RESERVED_18_TO_25.value(bytes); + reserved22to25 = FIELD_RESERVED_18_TO_25.value(bytes); + Long[] parameters = new Long[8]; + for (int i = 0; i < 8; i++) { + parameters[i] = FIELD_PARAMETER_26_TO_57.value(bytes); + } + Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes); + HSBK[] palette = new HSBK[paletteCount]; + for (int i = 0; i < palette.length; i++) { + palette[i] = FIELD_PALETTE_59_TO_186.value(bytes); + } + try { + effect = new Effect(effectType, speed, duration, palette); + } catch (IllegalArgumentException e) { + logger.debug("Wrong effect type received: {}", effectType); + effect = null; + } + if (logger.isDebugEnabled()) { + logger.debug("StateTileEffectResponse: instanceId={}, type={}, speed={}, duration={}, palette_count={}", + instanceId, effectType, speed, duration, paletteCount); + logger.debug("StateTileEffectResponse parameters=[{}, {}, {}, {}, {}, {}, {}, {}]", parameters[0], + parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], + parameters[7]); + for (int i = 0; i < palette.length; i++) { + logger.debug("StateTileEffectResponse palette[{}] = {}", i, palette[i]); + } + } + } + + @Override + protected ByteBuffer packetBytes() { + ByteBuffer buffer = ByteBuffer.allocate(packetLength()); + buffer.put(FIELD_RESERVED_0.bytes(reserved0)); + buffer.put(FIELD_INSTANCE_ID.bytes(0L)); + buffer.put(FIELD_TYPE.bytes(effect.getType().intValue())); + buffer.put(FIELD_SPEED.bytes(effect.getSpeed())); + buffer.put(FIELD_DURATION.bytes(effect.getDuration())); + buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved18to21)); + buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved22to25)); + for (int i = 0; i < 8; i++) { + buffer.put(FIELD_PARAMETER_26_TO_57.bytes(0L)); + } + HSBK[] palette = effect.getPalette(); + buffer.put(FIELD_PALETTE_COUNT.bytes(palette.length)); + for (int i = 0; i < palette.length; i++) { + buffer.put(FIELD_PALETTE_59_TO_186.bytes(palette[i])); + } + HSBK hsbkZero = new HSBK(0, 0, 0, 0); + for (int i = 0; i < 16 - palette.length; i++) { + buffer.put(FIELD_PALETTE_59_TO_186.bytes(hsbkZero)); + } + return buffer; + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateVersionResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateVersionResponse.java new file mode 100644 index 0000000000..db0b14e433 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateVersionResponse.java @@ -0,0 +1,93 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateVersionResponse extends Packet { + + public static final int TYPE = 0x21; + + public static final Field FIELD_VENDOR = new UInt32Field().little(); + public static final Field FIELD_PRODUCT = new UInt32Field().little(); + public static final Field FIELD_VERSION = new UInt32Field().little(); + + private long vendor; + private long product; + private long version; + + public long getVendor() { + return vendor; + } + + public void setVendor(long build) { + this.vendor = build; + } + + public long getProduct() { + return product; + } + + public void setProduct(long product) { + this.product = product; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + public StateVersionResponse() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 12; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + vendor = FIELD_VENDOR.value(bytes); + product = FIELD_PRODUCT.value(bytes); + version = FIELD_VERSION.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_VENDOR.bytes(vendor)).put(FIELD_PRODUCT.bytes(product)) + .put(FIELD_VERSION.bytes(version)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiFirmwareResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiFirmwareResponse.java new file mode 100644 index 0000000000..6cf177f0e9 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiFirmwareResponse.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; +import org.openhab.binding.lifx.internal.fields.Version; +import org.openhab.binding.lifx.internal.fields.VersionField; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateWifiFirmwareResponse extends Packet { + + public static final int TYPE = 0x13; + + public static final Field FIELD_BUILD = new UInt64Field().little(); + public static final Field FIELD_RESERVED = new UInt64Field().little(); + public static final Field FIELD_VERSION = new VersionField().little(); + + private long build; + private long reserved; + private Version version; + + public long getBuild() { + return build; + } + + public void setBuild(long build) { + this.build = build; + } + + public Version getVersion() { + return version; + } + + public void setVersion(Version version) { + this.version = version; + } + + public StateWifiFirmwareResponse() { + setTagged(false); + setAddressable(true); + setResponseRequired(true); + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 20; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + build = FIELD_BUILD.value(bytes); + reserved = FIELD_RESERVED.value(bytes); + version = FIELD_VERSION.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_BUILD.bytes(build)).put(FIELD_RESERVED.bytes(reserved)) + .put(FIELD_VERSION.bytes(version)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiInfoResponse.java new file mode 100644 index 0000000000..516562d2da --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateWifiInfoResponse.java @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.FloatField; +import org.openhab.binding.lifx.internal.fields.UInt16Field; +import org.openhab.binding.lifx.internal.fields.UInt32Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class StateWifiInfoResponse extends Packet { + + public static final int TYPE = 0x11; + + public static final Field FIELD_SIGNAL = new FloatField().little(); + public static final Field FIELD_RX = new UInt32Field().little(); + public static final Field FIELD_TX = new UInt32Field().little(); + public static final Field FIELD_TEMP = new UInt16Field(); + + private float signal; + private long rx; + private long tx; + private int mcuTemperature; + + public SignalStrength getSignalStrength() { + return new SignalStrength(signal); + } + + public long getRx() { + return rx; + } + + public long getTx() { + return tx; + } + + public int getMcuTemperature() { + return mcuTemperature; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 14; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + signal = FIELD_SIGNAL.value(bytes); + rx = FIELD_RX.value(bytes); + tx = FIELD_TX.value(bytes); + mcuTemperature = FIELD_TEMP.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_SIGNAL.bytes(signal)).put(FIELD_RX.bytes(rx)) + .put(FIELD_TX.bytes(tx)).put(FIELD_TEMP.bytes(mcuTemperature)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateZoneResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateZoneResponse.java new file mode 100644 index 0000000000..26eaf28246 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/StateZoneResponse.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.HSBK; +import org.openhab.binding.lifx.internal.fields.HSBKField; +import org.openhab.binding.lifx.internal.fields.UInt8Field; + +/** + * @author Wouter Born - Initial contribution + */ +public class StateZoneResponse extends Packet { + + public static final int TYPE = 0x1F7; + + public static final Field FIELD_COUNT = new UInt8Field(); + public static final Field FIELD_INDEX = new UInt8Field(); + public static final HSBKField FIELD_COLOR = new HSBKField(); + + private int count; + private int index; + private HSBK color; + + @Override + public String toString() { + return "count=" + count + ", index=" + index + ", " + color.toString("color"); + } + + public int getCount() { + return count; + } + + public int getIndex() { + return index; + } + + public HSBK getColor() { + return color; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 10; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + count = FIELD_COUNT.value(bytes); + index = FIELD_INDEX.value(bytes); + color = FIELD_COLOR.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_COUNT.bytes(count)).put(FIELD_INDEX.bytes(index)) + .put(FIELD_COLOR.bytes(color)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagLabelsResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagLabelsResponse.java new file mode 100644 index 0000000000..5ecd16b0c7 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagLabelsResponse.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.StringField; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class TagLabelsResponse extends Packet { + + public static final int TYPE = 0x1F; + + public static final Field FIELD_TAGS = new UInt64Field(); + public static final Field FIELD_LABEL = new StringField(32).utf8(); + + private long tags; + private String label; + + public long getTags() { + return tags; + } + + public String getLabel() { + return label; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 40; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + tags = FIELD_TAGS.value(bytes); + label = FIELD_LABEL.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)).put(FIELD_LABEL.bytes(label)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagsResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagsResponse.java new file mode 100644 index 0000000000..104ad08b26 --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/dto/TagsResponse.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.dto; + +import java.nio.ByteBuffer; + +import org.openhab.binding.lifx.internal.fields.Field; +import org.openhab.binding.lifx.internal.fields.UInt64Field; + +/** + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification + */ +public class TagsResponse extends Packet { + + public static final int TYPE = 0x1C; + + public static final Field FIELD_TAGS = new UInt64Field(); + + private long tags; + + public long getTags() { + return tags; + } + + @Override + public int packetType() { + return TYPE; + } + + @Override + protected int packetLength() { + return 8; + } + + @Override + protected void parsePacket(ByteBuffer bytes) { + tags = FIELD_TAGS.value(bytes); + } + + @Override + protected ByteBuffer packetBytes() { + return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); + } + + @Override + public int[] expectedResponses() { + return new int[] {}; + } +} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/ByteField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/ByteField.java index 29418f3263..5649a32d81 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/ByteField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/ByteField.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class ByteField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Field.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Field.java index e2bcc7d9e6..1a1ae2c92c 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Field.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Field.java @@ -22,7 +22,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; * * @param the field datatype * - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public abstract class Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/FloatField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/FloatField.java index 7e29fd5819..ffb7bd40f0 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/FloatField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/FloatField.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class FloatField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBK.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBK.java index e31629cbfa..23885500c6 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBK.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBK.java @@ -21,7 +21,7 @@ import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.PercentType; /** - * @author Wouter Born - Add support for MultiZone light control + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class HSBK { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBKField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBKField.java index c254f4bdb7..75d1e16701 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBKField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/HSBKField.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Wouter Born - Add support for MultiZone light control + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class HSBKField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/LittleField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/LittleField.java index 8dfcb257f4..5352491dfe 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/LittleField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/LittleField.java @@ -19,7 +19,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; /** * Reads a wrapped field in reversed byte order. * - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class LittleField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddress.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddress.java index bfce15f801..91190f4242 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddress.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddress.java @@ -12,30 +12,23 @@ */ package org.openhab.binding.lifx.internal.fields; -import java.io.ByteArrayInputStream; -import java.io.IOException; import java.nio.ByteBuffer; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Objects; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openhab.core.util.HexUtils; /** - * @author Tim Buckley - * @author Karel Goderis + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Initial contribution */ @NonNullByDefault public class MACAddress { - public static final MACAddress BROADCAST_ADDRESS = new MACAddress("000000000000", true); - - private final Logger logger = LoggerFactory.getLogger(MACAddress.class); + public static final MACAddress BROADCAST_ADDRESS = new MACAddress("000000000000"); private ByteBuffer bytes; private String hex = ""; @@ -54,28 +47,10 @@ public class MACAddress { createHex(); } - public MACAddress(String string, boolean isHex) { - if (!isHex) { - this.bytes = ByteBuffer.wrap(string.getBytes()); - createHex(); - } else { - this.bytes = ByteBuffer.wrap(parseHexBinary(string)); - - try { - formatHex(string, 2, ":"); - } catch (IOException e) { - logger.error("An exception occurred while formatting an HEX string : '{}'", e.getMessage()); - } - } - } - - private byte[] parseHexBinary(String s) { - int len = s.length(); - byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); - } - return data; + public MACAddress(String string) { + byte[] byteArray = HexUtils.hexToBytes(string); + this.bytes = ByteBuffer.wrap(byteArray); + this.hex = HexUtils.bytesToHex(byteArray, ":"); } public MACAddress() { @@ -90,7 +65,7 @@ public class MACAddress { byteStrings.add(String.format("%02X", bytes.get())); } - hex = StringUtils.join(byteStrings, ':'); + hex = String.join(":", byteStrings); bytes.rewind(); } @@ -108,21 +83,6 @@ public class MACAddress { return hex.toString(); } - private void formatHex(String original, int length, String separator) throws IOException { - ByteArrayInputStream bis = new ByteArrayInputStream(original.getBytes()); - byte[] buffer = new byte[length]; - String result = ""; - while (bis.read(buffer) > 0) { - for (byte b : buffer) { - result += (char) b; - } - Arrays.fill(buffer, (byte) 0); - result += separator; - } - - hex = StringUtils.left(result, result.length() - 1); - } - @Override public int hashCode() { int hash = 7; diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddressField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddressField.java index 417348cb71..57471120c0 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddressField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/MACAddressField.java @@ -17,8 +17,8 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley - * @author Karel Goderis + * @author Tim Buckley - Initial contribution + * @author Karel Goderis - Initial contribution */ @NonNullByDefault public class MACAddressField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/StringField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/StringField.java index 360ceb2816..0184d65c09 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/StringField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/StringField.java @@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class StringField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt16Field.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt16Field.java index 3cdcb44c28..a9c65d2e53 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt16Field.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt16Field.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class UInt16Field extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt32Field.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt32Field.java index 04b9d7acbe..89ce6fb882 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt32Field.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt32Field.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class UInt32Field extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt64Field.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt64Field.java index bfc229a4dd..b72ef6b2db 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt64Field.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt64Field.java @@ -21,7 +21,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; * unexpected values will likely be shown if exposed to users. Most bit-level * operations should still work (addition, multiplication, shifting, etc). * - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class UInt64Field extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt8Field.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt8Field.java index 23ba2353bd..969c468061 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt8Field.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/UInt8Field.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Tim Buckley + * @author Tim Buckley - Initial contribution */ @NonNullByDefault public class UInt8Field extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Version.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Version.java index 559bae30ec..3d53df7703 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Version.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/Version.java @@ -15,7 +15,7 @@ package org.openhab.binding.lifx.internal.fields; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Wouter Born - Add Thing properties + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class Version { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/VersionField.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/VersionField.java index 7cec7a8e96..26af5382c6 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/VersionField.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/fields/VersionField.java @@ -17,7 +17,7 @@ import java.nio.ByteBuffer; import org.eclipse.jdt.annotation.NonNullByDefault; /** - * @author Wouter Born - Add Thing properties + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class VersionField extends Field { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/handler/LifxLightHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/handler/LifxLightHandler.java index af941d2f96..812371394e 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/handler/LifxLightHandler.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/handler/LifxLightHandler.java @@ -13,7 +13,7 @@ package org.openhab.binding.lifx.internal.handler; import static org.openhab.binding.lifx.internal.LifxBindingConstants.*; -import static org.openhab.binding.lifx.internal.protocol.Product.Feature.*; +import static org.openhab.binding.lifx.internal.LifxProduct.Feature.*; import static org.openhab.binding.lifx.internal.util.LifxMessageUtil.*; import java.net.InetSocketAddress; @@ -38,18 +38,18 @@ import org.openhab.binding.lifx.internal.LifxLightOnlineStateUpdater; import org.openhab.binding.lifx.internal.LifxLightPropertiesUpdater; import org.openhab.binding.lifx.internal.LifxLightState; import org.openhab.binding.lifx.internal.LifxLightStateChanger; +import org.openhab.binding.lifx.internal.LifxProduct; +import org.openhab.binding.lifx.internal.dto.Effect; +import org.openhab.binding.lifx.internal.dto.GetLightInfraredRequest; +import org.openhab.binding.lifx.internal.dto.GetLightPowerRequest; +import org.openhab.binding.lifx.internal.dto.GetRequest; +import org.openhab.binding.lifx.internal.dto.GetTileEffectRequest; +import org.openhab.binding.lifx.internal.dto.GetWifiInfoRequest; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.PowerState; +import org.openhab.binding.lifx.internal.dto.SignalStrength; import org.openhab.binding.lifx.internal.fields.HSBK; import org.openhab.binding.lifx.internal.fields.MACAddress; -import org.openhab.binding.lifx.internal.protocol.Effect; -import org.openhab.binding.lifx.internal.protocol.GetLightInfraredRequest; -import org.openhab.binding.lifx.internal.protocol.GetLightPowerRequest; -import org.openhab.binding.lifx.internal.protocol.GetRequest; -import org.openhab.binding.lifx.internal.protocol.GetTileEffectRequest; -import org.openhab.binding.lifx.internal.protocol.GetWifiInfoRequest; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.PowerState; -import org.openhab.binding.lifx.internal.protocol.Product; -import org.openhab.binding.lifx.internal.protocol.SignalStrength; import org.openhab.core.config.core.Configuration; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.HSBType; @@ -90,7 +90,7 @@ public class LifxLightHandler extends BaseThingHandler { private static final Duration MAX_STATE_CHANGE_DURATION = Duration.ofSeconds(4); private final LifxChannelFactory channelFactory; - private @NonNullByDefault({}) Product product; + private @NonNullByDefault({}) LifxProduct product; private @Nullable PercentType powerOnBrightness; private @Nullable HSBType powerOnColor; @@ -405,10 +405,10 @@ public class LifxLightHandler extends BaseThingHandler { return speed == null ? null : new Double(speed.toString()); } - private Product getProduct() { + private LifxProduct getProduct() { Object propertyValue = getThing().getProperties().get(LifxBindingConstants.PROPERTY_PRODUCT_ID); if (propertyValue == null) { - return Product.getLikelyProduct(getThing().getThingTypeUID()); + return LifxProduct.getLikelyProduct(getThing().getThingTypeUID()); } try { // Without first conversion to double, on a very first thing creation from discovery inbox, @@ -416,9 +416,9 @@ public class LifxLightHandler extends BaseThingHandler { // (e.g. 50.0 instead of 50) Double d = Double.parseDouble((String) propertyValue); long productID = d.longValue(); - return Product.getProductFromProductID(productID); + return LifxProduct.getProductFromProductID(productID); } catch (IllegalArgumentException e) { - return Product.getLikelyProduct(getThing().getThingTypeUID()); + return LifxProduct.getLikelyProduct(getThing().getThingTypeUID()); } } diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxLightStateListener.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxLightStateListener.java index 68b7c460aa..fab89fa7f1 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxLightStateListener.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxLightStateListener.java @@ -15,10 +15,10 @@ package org.openhab.binding.lifx.internal.listener; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.lifx.internal.LifxLightState; +import org.openhab.binding.lifx.internal.dto.Effect; +import org.openhab.binding.lifx.internal.dto.PowerState; +import org.openhab.binding.lifx.internal.dto.SignalStrength; import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.protocol.Effect; -import org.openhab.binding.lifx.internal.protocol.PowerState; -import org.openhab.binding.lifx.internal.protocol.SignalStrength; import org.openhab.core.library.types.PercentType; /** diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxPropertiesUpdateListener.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxPropertiesUpdateListener.java index 8909ee73bf..c4dedcdea3 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxPropertiesUpdateListener.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxPropertiesUpdateListener.java @@ -21,7 +21,7 @@ import org.openhab.binding.lifx.internal.LifxLightPropertiesUpdater; * The {@link LifxPropertiesUpdateListener} is notified when the {@link LifxLightPropertiesUpdater} has * updated light properties. * - * @author Wouter Born - Update light properties when online + * @author Wouter Born - Initial contribution */ @NonNullByDefault public interface LifxPropertiesUpdateListener { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxResponsePacketListener.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxResponsePacketListener.java index fa622f6e64..10396ac5e8 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxResponsePacketListener.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/listener/LifxResponsePacketListener.java @@ -14,7 +14,7 @@ package org.openhab.binding.lifx.internal.listener; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.lifx.internal.LifxLightCommunicationHandler; -import org.openhab.binding.lifx.internal.protocol.Packet; +import org.openhab.binding.lifx.internal.dto.Packet; /** * The {@link LifxResponsePacketListener} is notified when the {@link LifxLightCommunicationHandler} receives a response diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/AcknowledgementResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/AcknowledgementResponse.java deleted file mode 100644 index 79eb57808d..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/AcknowledgementResponse.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class AcknowledgementResponse extends Packet { - - public static final int TYPE = 0x2D; - - public AcknowledgementResponse() { - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/ApplicationRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/ApplicationRequest.java deleted file mode 100644 index 233cfde2bd..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/ApplicationRequest.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -/** - * @author Wouter Born - Add support for MultiZone light control - */ -public enum ApplicationRequest { - - /** - * Don't apply the requested changes until a message with APPLY or APPLY_ONLY is sent. - */ - NO_APPLY(0x00), - - /** - * Apply the changes immediately and apply any pending changes. - */ - APPLY(0x01), - - /** - * Ignore the requested changes in this message and only apply pending changes. - */ - APPLY_ONLY(0x02); - - private final int value; - - private ApplicationRequest(int value) { - this.value = value; - } - - /** - * Gets the integer value of this application request. - * - * @return the integer value - */ - public int getValue() { - return value; - } - - /** - * Returns the {@link ApplicationRequest} for the given integer value. - * - * @param value the integer value - * @return the {@link ApplicationRequest} or null, if no {@link ApplicationRequest} exists for the - * given value - */ - public static ApplicationRequest fromValue(int value) { - for (ApplicationRequest ar : values()) { - if (ar.getValue() == value) { - return ar; - } - } - - return null; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/EchoRequestResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/EchoRequestResponse.java deleted file mode 100644 index 8ea752276b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/EchoRequestResponse.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class EchoRequestResponse extends Packet { - - public static final int TYPE = 0x3B; - - public static final Field FIELD_PAYLOAD = new ByteField(64); - - private ByteBuffer payload; - - public EchoRequestResponse() { - } - - public ByteBuffer getPayload() { - return payload; - } - - public void setPayload(ByteBuffer location) { - this.payload = location; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 64; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - payload = FIELD_PAYLOAD.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_PAYLOAD.bytes(payload)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Effect.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Effect.java deleted file mode 100644 index 73f9d6437d..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Effect.java +++ /dev/null @@ -1,169 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.binding.lifx.internal.LifxBindingConstants; -import org.openhab.binding.lifx.internal.fields.HSBK; - -/** - * This class represents LIFX Tile effect - * - * @author Pawel Pieczul - initial contribution - */ -@NonNullByDefault -public class Effect { - public enum EffectType { - OFF(0), - MORPH(2), - FLAME(3); - - Integer type; - - EffectType(Integer type) { - this.type = type; - } - - public static EffectType fromValue(Integer value) { - switch (value) { - case 0: - return OFF; - case 2: - return MORPH; - case 3: - return FLAME; - default: - throw new IllegalArgumentException("Unknown effect type"); - } - } - - public static EffectType fromValue(String value) { - if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF.equals(value)) { - return OFF; - } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH.equals(value)) { - return MORPH; - } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME.equals(value)) { - return FLAME; - } - throw new IllegalArgumentException("Unknown effect type"); - } - - public Integer intValue() { - return type; - } - - public String stringValue() { - switch (type) { - case 2: - return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH; - case 3: - return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME; - default: - return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF; - } - } - } - - final EffectType type; - final Long speed; - final Long duration; - final HSBK[] palette; - - public EffectType getType() { - return type; - } - - public Long getSpeed() { - return speed; - } - - public Long getDuration() { - return duration; - } - - HSBK[] getPalette() { - return palette; - } - - public Effect(EffectType type, Long speed, Long duration, HSBK[] palette) { - this.type = type; - this.palette = palette; - this.duration = duration; - this.speed = speed; - } - - public Effect(Integer type, Long speed, Long duration, HSBK[] palette) { - this(EffectType.fromValue(type), speed, duration, palette); - } - - public Effect() { - this(EffectType.OFF, 3000L, 0L, new HSBK[0]); - } - - public static Effect createDefault(String type, @Nullable Long morphSpeed, @Nullable Long flameSpeed) { - Long speed; - EffectType effectType = EffectType.fromValue(type); - switch (effectType) { - case OFF: - return new Effect(effectType, 0L, 0L, new HSBK[0]); - case MORPH: - if (morphSpeed == null) { - speed = 3000L; - } else { - speed = morphSpeed; - } - HSBK[] p = { new HSBK(0, 65535, 65535, 3500), new HSBK(7281, 65535, 65535, 3500), - new HSBK(10922, 65535, 65535, 3500), new HSBK(22209, 65535, 65535, 3500), - new HSBK(43507, 65535, 65535, 3500), new HSBK(49333, 65535, 65535, 3500), - new HSBK(53520, 65535, 65535, 3500) }; - return new Effect(effectType, speed, 0L, p); - case FLAME: - if (flameSpeed == null) { - speed = 4000L; - } else { - speed = flameSpeed; - } - return new Effect(effectType, speed, 0L, new HSBK[0]); - default: - throw new IllegalArgumentException("Unknown effect type"); - } - } - - @Override - public boolean equals(@Nullable Object o) { - if (this == o) { - return true; - } - if (o == null) { - return false; - } - if (o.getClass() != getClass()) { - return false; - } - Effect n = (Effect) o; - return n.getType().equals(this.getType()) && n.duration.equals(this.duration) && n.speed.equals(this.speed) - && n.palette == this.palette; - } - - @Override - public int hashCode() { - Long hash = 1L; - int prime = 31; - hash = prime * hash + type.hashCode(); - hash = prime * hash + duration; - hash = prime * hash + speed; - hash = prime * hash + palette.hashCode(); - return hash.intValue(); - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericHandler.java deleted file mode 100644 index 13127f9fc3..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericHandler.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.nio.ByteBuffer; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * A generic handler that dynamically creates "standard" packet instances. - * - *

      - * Packet types must have an empty constructor and cannot require any - * additional logic (other than parsing). - * - * @param the packet subtype this handler constructs - * - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -@NonNullByDefault -public class GenericHandler implements PacketHandler { - - private Constructor constructor; - - private boolean typeFound; - private int type; - - public boolean isTypeFound() { - return typeFound; - } - - public int getType() { - return type; - } - - public GenericHandler(Class clazz) { - try { - constructor = clazz.getConstructor(); - } catch (NoSuchMethodException ex) { - throw new IllegalArgumentException("Packet class cannot be handled by GenericHandler", ex); - } - - try { - Field typeField = clazz.getField("TYPE"); - type = (int) typeField.get(null); - typeFound = true; - } catch (NoSuchFieldException | IllegalAccessException ex) { - // silently ignore - typeFound = false; - } - } - - @Override - public T handle(ByteBuffer buf) { - try { - T ret = constructor.newInstance(); - ret.parse(buf); - return ret; - } catch (ReflectiveOperationException ex) { - throw new IllegalArgumentException("Unable to instantiate empty packet", ex); - } - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericPacket.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericPacket.java deleted file mode 100644 index 49667965ad..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GenericPacket.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GenericPacket extends Packet { - - @Override - public int packetType() { - return 0; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetColorZonesRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetColorZonesRequest.java deleted file mode 100644 index c53515412d..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetColorZonesRequest.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import static org.openhab.binding.lifx.internal.LifxBindingConstants.*; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * @author Wouter Born - Add support for MultiZone light control - */ -public class GetColorZonesRequest extends Packet { - - public static final int TYPE = 0x1F6; - - public static final Field FIELD_START_INDEX = new UInt8Field(); - public static final Field FIELD_END_INDEX = new UInt8Field(); - - private int startIndex = MIN_ZONE_INDEX; - private int endIndex = MAX_ZONE_INDEX; - - public GetColorZonesRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public GetColorZonesRequest(int index) { - this(index, index); - } - - public GetColorZonesRequest(int startIndex, int endIndex) { - this(); - this.startIndex = startIndex; - this.endIndex = endIndex; - } - - public int getStartIndex() { - return startIndex; - } - - public int getEndIndex() { - return endIndex; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - startIndex = FIELD_START_INDEX.value(bytes); - endIndex = FIELD_END_INDEX.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_START_INDEX.bytes(startIndex)) - .put(FIELD_END_INDEX.bytes(endIndex)); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateMultiZoneResponse.TYPE, StateZoneResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetEchoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetEchoRequest.java deleted file mode 100644 index 43b0fc262b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetEchoRequest.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetEchoRequest extends Packet { - - public static final int TYPE = 0x3A; - - public static final Field FIELD_PAYLOAD = new ByteField(64); - - private ByteBuffer payload; - - public GetEchoRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public ByteBuffer getPayload() { - return payload; - } - - public void setPayload(ByteBuffer payload) { - this.payload = payload; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 64; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_PAYLOAD.bytes(payload)); - } - - @Override - public int[] expectedResponses() { - return new int[] { EchoRequestResponse.TYPE }; - } - - public static GetEchoRequest currentTimeEchoRequest() { - ByteBuffer payload = ByteBuffer.allocate(Long.SIZE / 8); - payload.putLong(System.currentTimeMillis()); - - GetEchoRequest request = new GetEchoRequest(); - request.setResponseRequired(true); - request.setPayload(payload); - return request; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetGroupRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetGroupRequest.java deleted file mode 100644 index 8bb6cf23e4..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetGroupRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetGroupRequest extends Packet { - - public static final int TYPE = 0x33; - - public GetGroupRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateGroupResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostFirmwareRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostFirmwareRequest.java deleted file mode 100644 index 9905cecec2..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostFirmwareRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetHostFirmwareRequest extends Packet { - - public static final int TYPE = 0x0E; - - public GetHostFirmwareRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateHostFirmwareResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostInfoRequest.java deleted file mode 100644 index 290c934c42..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetHostInfoRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetHostInfoRequest extends Packet { - - public static final int TYPE = 0x0C; - - public GetHostInfoRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateHostInfoResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetInfoRequest.java deleted file mode 100644 index 33e830f987..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetInfoRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetInfoRequest extends Packet { - - public static final int TYPE = 0x22; - - public GetInfoRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateInfoResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLabelRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLabelRequest.java deleted file mode 100644 index 20d6b5a03b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLabelRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetLabelRequest extends Packet { - - public static final int TYPE = 0x17; - - public GetLabelRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLabelResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightInfraredRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightInfraredRequest.java deleted file mode 100644 index 1297026e87..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightInfraredRequest.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality - */ -public class GetLightInfraredRequest extends Packet { - - public static final int TYPE = 0x78; - - public GetLightInfraredRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLightInfraredResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightPowerRequest.java deleted file mode 100644 index d8853b7a44..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLightPowerRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetLightPowerRequest extends Packet { - - public static final int TYPE = 0x74; - - public GetLightPowerRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLightPowerResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLocationRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLocationRequest.java deleted file mode 100644 index abf7b8337f..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetLocationRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetLocationRequest extends Packet { - - public static final int TYPE = 0x30; - - public GetLocationRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLocationResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetPowerRequest.java deleted file mode 100644 index 6e09a2d79d..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetPowerRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetPowerRequest extends Packet { - - public static final int TYPE = 0x14; - - public GetPowerRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StatePowerResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetRequest.java deleted file mode 100644 index 3f11ae49ec..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetRequest extends Packet { - - public static final int TYPE = 0x65; - - public GetRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetServiceRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetServiceRequest.java deleted file mode 100644 index 400a19bb58..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetServiceRequest.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetServiceRequest extends Packet { - - public static final int TYPE = 0x02; - - public GetServiceRequest() { - setTagged(true); - setAddressable(true); - } - - @Override - public int packetLength() { - return 0; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // empty - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateServiceResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagLabelsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagLabelsRequest.java deleted file mode 100644 index 39aeb592e0..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagLabelsRequest.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetTagLabelsRequest extends Packet { - - public static final int TYPE = 0x1D; - - public static final Field FIELD_TAGS = new UInt64Field(); - - private long tags; - - public long getTags() { - return tags; - } - - public GetTagLabelsRequest() { - } - - public GetTagLabelsRequest(long tags) { - this.tags = tags; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 8; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - tags = FIELD_TAGS.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); - } - - @Override - public int[] expectedResponses() { - return new int[] { TagLabelsResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagsRequest.java deleted file mode 100644 index 1b76a2df2f..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTagsRequest.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetTagsRequest extends Packet { - - public static final int TYPE = 0x1A; - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { TagsResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTileEffectRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTileEffectRequest.java deleted file mode 100644 index afb4fa641f..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetTileEffectRequest.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * Implementation of GetTileEffect packet - * - * @author Pawel Pieczul - Add support for Tile Effects - */ -public class GetTileEffectRequest extends Packet { - - public static final int TYPE = 0x2CE; - - public GetTileEffectRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateTileEffectResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetVersionRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetVersionRequest.java deleted file mode 100644 index 9e789f8793..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetVersionRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetVersionRequest extends Packet { - - public static final int TYPE = 0x20; - - public GetVersionRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateVersionResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiFirmwareRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiFirmwareRequest.java deleted file mode 100644 index ca18a4a15c..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiFirmwareRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetWifiFirmwareRequest extends Packet { - - public static final int TYPE = 0x12; - - public GetWifiFirmwareRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateWifiFirmwareResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiInfoRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiInfoRequest.java deleted file mode 100644 index 5740ccea6f..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/GetWifiInfoRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class GetWifiInfoRequest extends Packet { - - public static final int TYPE = 0x10; - - public GetWifiInfoRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 0; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - // do nothing - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(0); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateWifiInfoResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Packet.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Packet.java deleted file mode 100644 index d9084ab866..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Packet.java +++ /dev/null @@ -1,397 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.MACAddress; -import org.openhab.binding.lifx.internal.fields.MACAddressField; -import org.openhab.binding.lifx.internal.fields.UInt16Field; -import org.openhab.binding.lifx.internal.fields.UInt32Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * Represents an abstract packet, providing conversion functionality to and from - * {@link ByteBuffer}s for common packet (preamble) fields. Subtypes of this - * class can provide conversion functionality for specialized fields. - * - *

      - * Defining new packet types essentially involves extending this class, - * defining the fields and implementing {@link #packetType()}, - * {@link #packetLength()}, and {@link #packetBytes()}. By convention, packet - * type should be stored in a {@code public static final int PACKET_TYPE} field - * in each subtype, followed by a listing of fields contained in the packet. - * Field definitions should remain accessible to outside classes in the event - * they need to worked with directly elsewhere. - * - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public abstract class Packet { - - public static final Field FIELD_SIZE = new UInt16Field().little(); - public static final Field FIELD_PROTOCOL = new UInt16Field().little(); - public static final Field FIELD_SOURCE = new UInt32Field().little(); - public static final Field FIELD_TARGET = new MACAddressField(); - public static final Field FIELD_RESERVED_1 = new ByteField(6); - public static final Field FIELD_ACK = new UInt8Field(); - public static final Field FIELD_SEQUENCE = new UInt8Field().little(); - public static final Field FIELD_RESERVED_2 = new ByteField(8); - public static final Field FIELD_PACKET_TYPE = new UInt16Field().little(); - public static final Field FIELD_RESERVED_3 = new ByteField(2); - - /** - * An ordered array of all fields contained in the common packet preamble. - */ - public static final Field[] PREAMBLE_FIELDS = new Field[] { FIELD_SIZE, FIELD_PROTOCOL, FIELD_SOURCE, - FIELD_TARGET, FIELD_RESERVED_1, FIELD_ACK, FIELD_SEQUENCE, FIELD_RESERVED_2, FIELD_PACKET_TYPE, - FIELD_RESERVED_3 }; - - protected int size; - protected int protocol; - protected long source; - protected MACAddress target; - protected ByteBuffer reserved1; - protected int ackbyte; - protected int sequence; - protected ByteBuffer reserved2; - protected int packetType; - protected ByteBuffer reserved3; - - protected long timeStamp; - - public int getSize() { - return size; - } - - public void setSize(int size) { - this.size = size; - } - - public int getOrigin() { - return (protocol & 0xC000) >> 14; - } - - public void setOrigin(int origin) { - protocol = (protocol & ~(1 << 14)) | (origin << 14); - } - - public boolean getTagged() { - return (protocol & 0x2000) >> 13 == 1 ? true : false; - } - - public void setTagged(boolean flag) { - protocol = (protocol & ~(1 << 13)) | ((flag ? 1 : 0) << 13); - } - - public boolean getAddressable() { - return (protocol & 0x1000) >> 12 == 1 ? true : false; - } - - public void setAddressable(boolean flag) { - this.protocol = (protocol & ~(1 << 12)) | ((flag ? 1 : 0) << 12); - } - - public int getProtocol() { - return protocol & 0x0FFF; - } - - public void setProtocol(int protocol) { - this.protocol = this.protocol | protocol; - } - - public long getSource() { - return source; - } - - public void setSource(long source) { - this.source = source; - } - - public MACAddress getTarget() { - return target; - } - - public void setTarget(MACAddress lightAddress) { - this.target = lightAddress != null ? lightAddress : MACAddress.BROADCAST_ADDRESS; - } - - public ByteBuffer getReserved1() { - return reserved1; - } - - public void setReserved1(ByteBuffer reserved2) { - this.reserved1 = reserved2; - } - - public boolean getAckRequired() { - return (ackbyte & 0x02) >> 1 == 1 ? true : false; - } - - public void setAckRequired(boolean flag) { - this.ackbyte = (ackbyte & ~(1 << 1)) | ((flag ? 1 : 0) << 1); - } - - public boolean getResponseRequired() { - return (ackbyte & 0x01) >> 0 == 1 ? true : false; - } - - public void setResponseRequired(boolean flag) { - this.ackbyte = (ackbyte & ~(1 << 0)) | ((flag ? 1 : 0) << 0); - } - - public int getSequence() { - return sequence; - } - - public void setSequence(int sequence) { - if (0 <= sequence && sequence < 256) { - this.sequence = sequence; - } else { - throw new IllegalArgumentException("Sequence number '" + sequence + "' is not in range [0, 255]"); - } - } - - public ByteBuffer getReserved2() { - return reserved2; - } - - public void setReserved2(ByteBuffer reserved3) { - this.reserved2 = reserved3; - } - - public int getPacketType() { - return packetType; - } - - public void setPacketType(int packetType) { - this.packetType = packetType; - } - - public ByteBuffer getReserved3() { - return reserved3; - } - - public void setReserved3(ByteBuffer reserved4) { - this.reserved3 = reserved4; - } - - public long getTimeStamp() { - return timeStamp; - } - - /** - * Creates an empty packet, setting some default values via - * {@link #preambleDefaults()}. - */ - public Packet() { - preambleDefaults(); - timeStamp = System.currentTimeMillis(); - } - - /** - * Parses, in order, the defined preamble fields, storing collected values. - * The buffer's position will be left at the end of the parsed fields and - * should be equal to the value returned by {@link #preambleLength()}. - * - * @param bytes the buffer to read from. - */ - protected void parsePreamble(ByteBuffer bytes) { - size = FIELD_SIZE.value(bytes); - protocol = FIELD_PROTOCOL.value(bytes); - source = FIELD_SOURCE.value(bytes); - target = FIELD_TARGET.value(bytes); - reserved1 = FIELD_RESERVED_1.value(bytes); - ackbyte = FIELD_ACK.value(bytes); - sequence = FIELD_SEQUENCE.value(bytes); - reserved2 = FIELD_RESERVED_2.value(bytes); - packetType = FIELD_PACKET_TYPE.value(bytes); - reserved3 = FIELD_RESERVED_3.value(bytes); - } - - /** - * Calculates the length of the packet header, defined as the sum of the - * lengths of all defined fields (see {@link #PREAMBLE_FIELDS}). - * - * @return the sum of the length of preamble fields - */ - protected int preambleLength() { - int sum = 0; - - for (Field f : PREAMBLE_FIELDS) { - sum += f.getLength(); - } - - return sum; - } - - /** - * Returns a new {@code ByteBuffer} containing the encoded preamble. Note - * that the returned buffer will have its position set at the end of the - * buffer and will need to have {@link ByteBuffer#rewind()} called before - * use. - * - *

      - * The length of the buffer is the sum of the lengths of the defined - * preamble fields (see {@link #PREAMBLE_FIELDS} for an ordered list), which - * may also be accessed via {@link #preambleLength()}. - * - *

      - * Certain fields are set to default values based on other class methods. - * For example, the size and packet type fields will be set to the values - * returned from {@link #length()} and {@link #packetType()}, respectively. - * Other defaults (such as the protocol, light address, site, and timestamp) - * may be specified either by directly setting the relevant protected - * variables or by overriding {@link #preambleDefaults()}. - * - * @return a new buffer containing the encoded preamble - */ - protected ByteBuffer preambleBytes() { - return ByteBuffer.allocate(preambleLength()).put(FIELD_SIZE.bytes(length())).put(FIELD_PROTOCOL.bytes(protocol)) - .put(FIELD_SOURCE.bytes(source)).put(FIELD_TARGET.bytes(target)) - .put(ByteBuffer.allocate(FIELD_RESERVED_1.getLength())) // empty - .put(FIELD_ACK.bytes(ackbyte)).put(FIELD_SEQUENCE.bytes(sequence)) - .put(ByteBuffer.allocate(FIELD_RESERVED_2.getLength())) // empty - .put(FIELD_PACKET_TYPE.bytes(packetType())).put(ByteBuffer.allocate(FIELD_RESERVED_3.getLength())); // empty - } - - /** - * Sets default preamble values. If needed, subclasses may override these - * values by specifically overriding this method, or by setting individual - * values within the constructor, as this method is called automatically - * during initialization. - */ - protected void preambleDefaults() { - size = 0; - protocol = 1024; - target = new MACAddress(); - sequence = 0; - packetType = packetType(); - } - - /** - * Returns the packet type. Note that this value is technically distinct - * from {@code getPacketType()} in that it returns the packet type the - * current {@code Packet} subtype is designed to parse, while - * {@code getPacketType()} returns the actual {@code packetType} field of - * a parsed packet. However, these values should always match. - * - * @return the packet type intended to be handled by this Packet subtype - */ - public abstract int packetType(); - - /** - * Returns the length of the payload specific to this packet subtype. The - * length of the preamble is specifically excluded. - * - * @return the length of this specialized packet payload - */ - protected abstract int packetLength(); - - /** - * Parses the given {@link ByteBuffer} into class fields. Subtypes may - * implement {@link #parsePacket(ByteBuffer)} to parse additional fields; - * the preamble by default is always parsed. - * - * @param bytes the buffer to extract data from - */ - public void parse(ByteBuffer bytes) { - bytes.rewind(); - parsePreamble(bytes); - parsePacket(bytes); - } - - /** - * Extracts data from the given {@link ByteBuffer} into fields specific to - * this packet subtype. The preamble will already have been parsed; as such, - * the buffer will be positioned at the end of the preamble. If needed, - * {@link #preambleLength()} may be used to restore the position of the - * buffer. - * - * @param bytes the raw bytes to parse - */ - protected abstract void parsePacket(ByteBuffer bytes); - - /** - * Returns a {@link ByteBuffer} containing the full payload for this packet, - * including the populated preamble and any specialized packet payload. The - * returned buffer will be at position zero. - * - * @return the full packet payload - */ - public ByteBuffer bytes() { - ByteBuffer preamble = preambleBytes(); - preamble.rewind(); - - ByteBuffer packet = packetBytes(); - packet.rewind(); - - ByteBuffer ret = ByteBuffer.allocate(length()).put(preamble).put(packet); - ret.rewind(); - - return ret; - } - - /** - * Returns a {@link ByteBuffer} containing the payload for this packet. Its - * length must match the value of {@link #packetLength()}. This specifically - * excludes preamble fields and should contain only data specific to the - * packet subtype. - *

      - * Note that returned ByteBuffers will have {@link ByteBuffer#rewind()} - * called automatically before they are appended to the final packet - * buffer. - * - * @return the packet payload - */ - protected abstract ByteBuffer packetBytes(); - - /** - * Gets the total length of this packet, in bytes. Specifically, this method - * is the sum of the preamble ({@link #preambleLength()}) and the payload - * length ({@link #packetLength()}); subtypes should override methods for - * those values if desired. - * - * @return the total length of this packet - */ - public int length() { - return preambleLength() + packetLength(); - } - - /** - * Returns a list of expected response packet types. An empty array means - * no responses are expected (suitable for response packet definitions), - * - * @return a list of expected responses - */ - public abstract int[] expectedResponses(); - - public boolean isExpectedResponse(int type) { - for (int a : expectedResponses()) { - if (a == type) { - return true; - } - } - - return false; - } - - public boolean isFulfilled(Packet somePacket) { - if (isExpectedResponse(somePacket.getPacketType())) { - return true; - } - return false; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketFactory.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketFactory.java deleted file mode 100644 index 6718d31f3c..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketFactory.java +++ /dev/null @@ -1,157 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.util.HashMap; -import java.util.Map; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; - -/** - * A static factory for registering packet types that may be received and - * dispatched to client cod * request types, like {@code PowerStateRequest}) or types received only via UDP - * e. Packet handlers (used to construct actual packet - * instances) may be retrieved via their packet type. - * - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality - */ -@NonNullByDefault -public class PacketFactory { - - private static @Nullable PacketFactory instance; - - public static synchronized PacketFactory getInstance() { - PacketFactory result = instance; - if (result == null) { - result = new PacketFactory(); - instance = result; - } - return result; - } - - private final Map> handlers; - - private PacketFactory() { - handlers = new HashMap<>(); - - register(AcknowledgementResponse.class); - register(EchoRequestResponse.class); - register(GetColorZonesRequest.class); - register(GetEchoRequest.class); - register(GetGroupRequest.class); - register(GetHostFirmwareRequest.class); - register(GetHostInfoRequest.class); - register(GetInfoRequest.class); - register(GetLabelRequest.class); - register(GetLightInfraredRequest.class); - register(GetLightPowerRequest.class); - register(GetLocationRequest.class); - register(GetPowerRequest.class); - register(GetRequest.class); - register(GetServiceRequest.class); - register(GetTagLabelsRequest.class); - register(GetTagsRequest.class); - register(GetTileEffectRequest.class); - register(GetVersionRequest.class); - register(GetWifiFirmwareRequest.class); - register(GetWifiInfoRequest.class); - register(SetColorRequest.class); - register(SetColorZonesRequest.class); - register(SetDimAbsoluteRequest.class); - register(SetLabelRequest.class); - register(SetLightInfraredRequest.class); - register(SetLightPowerRequest.class); - register(SetPowerRequest.class); - register(SetTagsRequest.class); - register(StateGroupResponse.class); - register(StateHostFirmwareResponse.class); - register(StateHostInfoResponse.class); - register(StateInfoResponse.class); - register(StateLabelResponse.class); - register(StateLightInfraredResponse.class); - register(StateLightPowerResponse.class); - register(StateLocationResponse.class); - register(StateMultiZoneResponse.class); - register(StatePowerResponse.class); - register(StateResponse.class); - register(StateServiceResponse.class); - register(StateTileEffectResponse.class); - register(StateVersionResponse.class); - register(StateWifiFirmwareResponse.class); - register(StateWifiInfoResponse.class); - register(StateZoneResponse.class); - register(TagLabelsResponse.class); - register(TagsResponse.class); - } - - /** - * Registers a packet handler for the given packet type. - * - * @param type the type to register - * @param handler the packet handler to associate with the type - */ - public final void register(int type, PacketHandler handler) { - handlers.put(type, handler); - } - - /** - * Registers a new generic packet handler for the given packet class. The - * packet class must meet the criteria for {@link GenericHandler}; - * specifically, it must have an no-argument constructor and require no - * parsing logic outside of an invocation of - * {@link Packet#parse(java.nio.ByteBuffer)}. - * - * @param type the type of the packet to register - * @param clazz the class of the packet to register - */ - public final void register(int type, Class clazz) { - handlers.put(type, new GenericHandler<>(clazz)); - } - - /** - * Registers a generic packet type. All requirements of - * {@link GenericHandler} must met; specifically, classes must have a - * no-args constructor and require no additional parsing logic. - * Additionally, a public static integer {@code TYPE} field must be defined. - * - * @param the packet type to register - * @param clazz the packet class to register - */ - public final void register(Class clazz) { - GenericHandler handler = new GenericHandler<>(clazz); - - if (!handler.isTypeFound()) { - throw new IllegalArgumentException("Unable to register generic packet with no TYPE field."); - } - - handlers.put(handler.getType(), handler); - } - - /** - * Gets a registered handler for the given packet type, if any exists. If - * no matching handler can be found, {@code null} is returned. - * - * @param packetType the packet type of the handler to retrieve - * @return a packet handler, or null - */ - public @Nullable PacketHandler getHandler(int packetType) { - return handlers.get(packetType); - } - - public static @Nullable PacketHandler createHandler(int packetType) { - return getInstance().getHandler(packetType); - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketHandler.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketHandler.java deleted file mode 100644 index fe8944eb2c..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PacketHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.eclipse.jdt.annotation.NonNullByDefault; - -/** - * A packet handler responsible for converting a ByteBuffer into a Packet - * instance. - * - * @param the generic packet type - * - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -@NonNullByDefault -public interface PacketHandler { - - /** - * Creates a packet from the given buffer. - * - * @param buf the buffer used for creating the packet - * @return the packet created from the buffer - * @throws IllegalArgumentException when an empty packet could not be created or the data in the buffer - * could not be parsed - */ - public abstract T handle(ByteBuffer buf); -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PowerState.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PowerState.java deleted file mode 100644 index 69d6ad11fd..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/PowerState.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import org.openhab.core.library.types.OnOffType; - -/** - * Represents light power states (on or off). - * - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - * @author Wouter Born - Added OnOffType conversion methods - */ -public enum PowerState { - - ON(0xFFFF), - OFF(0x0000); - - private final int value; - - private PowerState(int value) { - this.value = value; - } - - /** - * Gets the integer value of this power state. - * - * @return the integer value - */ - public int getValue() { - return value; - } - - public static PowerState fromValue(int value) { - // a response can have a power level between 0 and 65535 when the light - // has just been switched ON or OFF - return value == OFF.value ? OFF : ON; - } - - public static PowerState fromOnOffType(OnOffType onOff) { - return onOff == OnOffType.ON ? ON : OFF; - } - - public OnOffType toOnOffType() { - return this == ON ? OnOffType.ON : OnOffType.OFF; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Product.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Product.java deleted file mode 100644 index cee8e8d04d..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/Product.java +++ /dev/null @@ -1,312 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import static org.openhab.binding.lifx.internal.protocol.Product.Feature.*; -import static org.openhab.binding.lifx.internal.protocol.Product.TemperatureRange.*; -import static org.openhab.binding.lifx.internal.protocol.Product.Vendor.LIFX; - -import java.util.Arrays; -import java.util.EnumSet; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.binding.lifx.internal.LifxBindingConstants; -import org.openhab.core.thing.ThingTypeUID; - -/** - * Enumerates the LIFX products, their IDs and feature set. - * - * @see https://lan.developer.lifx.com/docs/lifx-products - * - * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality - * @author Wouter Born - Add temperature ranges and simplify feature definitions - */ -@NonNullByDefault -public enum Product { - - PRODUCT_1(1, "LIFX Original 1000", TR_2500_9000, COLOR), - PRODUCT_3(3, "LIFX Color 650", TR_2500_9000, COLOR), - PRODUCT_10(10, "LIFX White 800 (Low Voltage)", TR_2700_6500), - PRODUCT_11(11, "LIFX White 800 (High Voltage)", TR_2700_6500), - PRODUCT_15(15, "LIFX Color 1000", TR_2500_9000, COLOR), - PRODUCT_18(18, "LIFX White 900 BR30 (Low Voltage)", TR_2700_6500), - PRODUCT_19(19, "LIFX White 900 BR30 (High Voltage)", TR_2700_6500), - PRODUCT_20(20, "LIFX Color 1000 BR30", TR_2500_9000, COLOR), - PRODUCT_22(22, "LIFX Color 1000", TR_2500_9000, COLOR), - PRODUCT_27(27, "LIFX A19", TR_2500_9000, COLOR), - PRODUCT_28(28, "LIFX BR30", TR_2500_9000, COLOR), - PRODUCT_29(29, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_30(30, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_31(31, "LIFX Z", TR_2500_9000, COLOR, MULTIZONE), - PRODUCT_32(32, "LIFX Z", TR_2500_9000, COLOR, MULTIZONE), - PRODUCT_36(36, "LIFX Downlight", TR_2500_9000, COLOR), - PRODUCT_37(37, "LIFX Downlight", TR_2500_9000, COLOR), - PRODUCT_38(38, "LIFX Beam", TR_2500_9000, COLOR, MULTIZONE), - PRODUCT_39(39, "LIFX Downlight White to Warm", TR_1500_9000), - PRODUCT_40(40, "LIFX Downlight", TR_2500_9000, COLOR), - PRODUCT_43(43, "LIFX A19", TR_2500_9000, COLOR), - PRODUCT_44(44, "LIFX BR30", TR_2500_9000, COLOR), - PRODUCT_45(45, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_46(46, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_49(49, "LIFX Mini Color", TR_2500_9000, COLOR), - PRODUCT_50(50, "LIFX Mini White to Warm", TR_1500_4000), - PRODUCT_51(51, "LIFX Mini White", TR_2700_2700), - PRODUCT_52(52, "LIFX GU10", TR_2500_9000, COLOR), - PRODUCT_53(53, "LIFX GU10", TR_2500_9000, COLOR), - PRODUCT_55(55, "LIFX Tile", TR_2500_9000, CHAIN, COLOR, MATRIX, TILE_EFFECT), - PRODUCT_57(57, "LIFX Candle", TR_1500_9000, COLOR, MATRIX), - PRODUCT_59(59, "LIFX Mini Color", TR_2500_9000, COLOR), - PRODUCT_60(60, "LIFX Mini White to Warm", TR_1500_4000), - PRODUCT_61(61, "LIFX Mini White", TR_2700_2700), - PRODUCT_62(62, "LIFX A19", TR_2500_9000, COLOR), - PRODUCT_63(63, "LIFX BR30", TR_2500_9000, COLOR), - PRODUCT_64(64, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_65(65, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_66(66, "LIFX Mini White", TR_2700_2700), - PRODUCT_68(68, "LIFX Candle", TR_1500_9000, MATRIX), - PRODUCT_81(81, "LIFX Candle White to Warm", TR_2200_6500), - PRODUCT_82(82, "LIFX Filament Clear", TR_2100_2100), - PRODUCT_85(85, "LIFX Filament Amber", TR_2000_2000), - PRODUCT_87(87, "LIFX Mini White", TR_2700_2700), - PRODUCT_88(88, "LIFX Mini White", TR_2700_2700), - PRODUCT_90(90, "LIFX Clean", TR_2500_9000, HEV), - PRODUCT_91(91, "LIFX Color", TR_2500_9000, COLOR), - PRODUCT_92(92, "LIFX Color", TR_2500_9000, COLOR), - PRODUCT_94(94, "LIFX BR30", TR_2500_9000, COLOR), - PRODUCT_96(96, "LIFX Candle White to Warm", TR_2200_6500), - PRODUCT_97(97, "LIFX A19", TR_2500_9000, COLOR), - PRODUCT_98(98, "LIFX BR30", TR_2500_9000, COLOR), - PRODUCT_99(99, "LIFX Clean", TR_2500_9000, HEV), - PRODUCT_100(100, "LIFX Filament Clear", TR_2100_2100), - PRODUCT_101(101, "LIFX Filament Amber", TR_2000_2000), - PRODUCT_109(109, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_110(110, "LIFX BR30 Night Vision", TR_2500_9000, COLOR, INFRARED), - PRODUCT_111(111, "LIFX A19 Night Vision", TR_2500_9000, COLOR, INFRARED); - - /** - * Enumerates the product features. - */ - public enum Feature { - CHAIN, - COLOR, - HEV, - INFRARED, - MATRIX, - MULTIZONE, - TILE_EFFECT - } - - /** - * Enumerates the product vendors. - */ - public enum Vendor { - LIFX(1, "LIFX"); - - private final int id; - private final String name; - - Vendor(int id, String name) { - this.id = id; - this.name = name; - } - - public int getID() { - return id; - } - - public String getName() { - return name; - } - } - - /** - * Enumerates the color temperature ranges of lights. - */ - public enum TemperatureRange { - /** - * 1500-4000K - */ - TR_1500_4000(1500, 4000), - - /** - * 1500-9000K - */ - TR_1500_9000(1500, 9000), - - /** - * 2000-2000K - */ - TR_2000_2000(2000, 2000), - - /** - * 2100-2100K - */ - TR_2100_2100(2100, 2100), - - /** - * 2200-6500K - */ - TR_2200_6500(2200, 6500), - - /** - * 2500-9000K - */ - TR_2500_9000(2500, 9000), - - /** - * 2700-2700K - */ - TR_2700_2700(2700, 2700), - - /** - * 2700-6500K - */ - TR_2700_6500(2700, 6500); - - private final int minimum; - private final int maximum; - - TemperatureRange(int minimum, int maximum) { - this.minimum = minimum; - this.maximum = maximum; - } - - /** - * The minimum color temperature in degrees Kelvin. - * - * @return minimum color temperature (K) - */ - public int getMinimum() { - return minimum; - } - - /** - * The maxiumum color temperature in degrees Kelvin. - * - * @return maximum color temperature (K) - */ - public int getMaximum() { - return maximum; - } - - /** - * The color temperature range in degrees Kelvin. - * - * @return difference between maximum and minimum color temperature values - */ - public int getRange() { - return maximum - minimum; - } - } - - private final Vendor vendor; - private final long id; - private final String name; - private final TemperatureRange temperatureRange; - private final EnumSet features = EnumSet.noneOf(Feature.class); - - private Product(long id, String name, TemperatureRange temperatureRange) { - this(LIFX, id, name, temperatureRange); - } - - private Product(long id, String name, TemperatureRange temperatureRange, Feature... features) { - this(LIFX, id, name, temperatureRange, features); - } - - private Product(Vendor vendor, long id, String name, TemperatureRange temperatureRange) { - this(vendor, id, name, temperatureRange, new Feature[0]); - } - - private Product(Vendor vendor, long id, String name, TemperatureRange temperatureRange, Feature... features) { - this.vendor = vendor; - this.id = id; - this.name = name; - this.temperatureRange = temperatureRange; - this.features.addAll(Arrays.asList(features)); - } - - @Override - public String toString() { - return name; - } - - public Vendor getVendor() { - return vendor; - } - - public long getID() { - return id; - } - - public String getName() { - return name; - } - - public TemperatureRange getTemperatureRange() { - return temperatureRange; - } - - public ThingTypeUID getThingTypeUID() { - if (hasFeature(COLOR)) { - if (hasFeature(TILE_EFFECT)) { - return LifxBindingConstants.THING_TYPE_TILELIGHT; - } else if (hasFeature(INFRARED)) { - return LifxBindingConstants.THING_TYPE_COLORIRLIGHT; - } else if (hasFeature(MULTIZONE)) { - return LifxBindingConstants.THING_TYPE_COLORMZLIGHT; - } else { - return LifxBindingConstants.THING_TYPE_COLORLIGHT; - } - } else { - return LifxBindingConstants.THING_TYPE_WHITELIGHT; - } - } - - public boolean hasFeature(Feature feature) { - return features.contains(feature); - } - - /** - * Returns a product that has the given thing type UID. - * - * @param uid a thing type UID - * @return a product that has the given thing type UID - * @throws IllegalArgumentException when uid is not a valid LIFX thing type UID - */ - public static Product getLikelyProduct(ThingTypeUID uid) throws IllegalArgumentException { - for (Product product : Product.values()) { - if (product.getThingTypeUID().equals(uid)) { - return product; - } - } - - throw new IllegalArgumentException(uid + " is not a valid product thing type UID"); - } - - /** - * Returns the product that has the given product ID. - * - * @param id the product ID - * @return the product that has the given product ID - * @throws IllegalArgumentException when id is not a valid LIFX product ID - */ - public static Product getProductFromProductID(long id) throws IllegalArgumentException { - for (Product product : Product.values()) { - if (product.id == id) { - return product; - } - } - - throw new IllegalArgumentException(id + " is not a valid product ID"); - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorRequest.java deleted file mode 100644 index 3a24126e6b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorRequest.java +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetColorRequest extends Packet { - - public static final int TYPE = 0x66; - - public static final Field FIELD_STREAM = new ByteField(1); - public static final HSBKField FIELD_COLOR = new HSBKField(); - public static final Field FIELD_FADE_TIME = new UInt32Field().little(); - - private ByteBuffer stream; - - private HSBK color; - private long fadeTime; - - public ByteBuffer getStream() { - return stream; - } - - public HSBK getColor() { - return color; - } - - public long getFadeTime() { - return fadeTime; - } - - public SetColorRequest() { - stream = ByteBuffer.allocate(1); - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public SetColorRequest(HSBK color, long fadeTime) { - this(); - this.color = color; - this.fadeTime = fadeTime; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 13; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - stream = FIELD_STREAM.value(bytes); - color = FIELD_COLOR.value(bytes); - fadeTime = FIELD_FADE_TIME.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_STREAM.bytes(stream)).put(FIELD_COLOR.bytes(color)) - .put(FIELD_FADE_TIME.bytes(fadeTime)); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorZonesRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorZonesRequest.java deleted file mode 100644 index 5553877994..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetColorZonesRequest.java +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import static org.openhab.binding.lifx.internal.LifxBindingConstants.*; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * @author Wouter Born - Add support for MultiZone light control - */ -public class SetColorZonesRequest extends Packet { - - public static final int TYPE = 0x1F5; - - public static final Field FIELD_START_INDEX = new UInt8Field(); - public static final Field FIELD_END_INDEX = new UInt8Field(); - public static final HSBKField FIELD_COLOR = new HSBKField(); - public static final Field FIELD_FADE_TIME = new UInt32Field().little(); - public static final Field FIELD_APPLY = new UInt8Field(); - - private int startIndex = MIN_ZONE_INDEX; - private int endIndex = MAX_ZONE_INDEX; - private HSBK color; - private long fadeTime; - private ApplicationRequest apply; - - public HSBK getColor() { - return color; - } - - public int getHue() { - return color.getHue(); - } - - public int getSaturation() { - return color.getSaturation(); - } - - public int getBrightness() { - return color.getBrightness(); - } - - public int getKelvin() { - return color.getKelvin(); - } - - public long getFadeTime() { - return fadeTime; - } - - public ApplicationRequest getApply() { - return apply; - } - - public SetColorZonesRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public SetColorZonesRequest(HSBK color, long fadeTime, ApplicationRequest apply) { - this(MIN_ZONE_INDEX, MAX_ZONE_INDEX, color, fadeTime, apply); - } - - public SetColorZonesRequest(int index, HSBK color, long fadeTime, ApplicationRequest apply) { - this(index, index, color, fadeTime, apply); - } - - public SetColorZonesRequest(int startIndex, int endIndex, HSBK color, long fadeTime, ApplicationRequest apply) { - this(); - this.startIndex = startIndex; - this.endIndex = endIndex; - this.color = color; - this.fadeTime = fadeTime; - this.apply = apply; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 15; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - startIndex = FIELD_START_INDEX.value(bytes); - endIndex = FIELD_END_INDEX.value(bytes); - color = FIELD_COLOR.value(bytes); - fadeTime = FIELD_FADE_TIME.value(bytes); - apply = ApplicationRequest.fromValue(FIELD_APPLY.value(bytes)); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_START_INDEX.bytes(startIndex)) - .put(FIELD_END_INDEX.bytes(endIndex)).put(FIELD_COLOR.bytes(color)).put(FIELD_FADE_TIME.bytes(fadeTime)) - .put(FIELD_APPLY.bytes(apply.getValue())); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetDimAbsoluteRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetDimAbsoluteRequest.java deleted file mode 100644 index 648db5845c..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetDimAbsoluteRequest.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetDimAbsoluteRequest extends Packet { - - public static final int TYPE = 0x68; - - public static final Field FIELD_DIM = new UInt16Field().little(); - public static final Field FIELD_DURATION = new UInt32Field().little(); - - private int dim; - private long duration; - - public int getDim() { - return dim; - } - - public long getDuration() { - return duration; - } - - public SetDimAbsoluteRequest() { - } - - public SetDimAbsoluteRequest(int dim, long duration) { - this.dim = dim; - this.duration = duration; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 6; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - dim = FIELD_DIM.value(bytes); - duration = FIELD_DURATION.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_DIM.bytes(dim)).put(FIELD_DURATION.bytes(duration)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLabelRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLabelRequest.java deleted file mode 100644 index e55dbe5ab6..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLabelRequest.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.StringField; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetLabelRequest extends Packet { - - public static final int TYPE = 0x18; - - public static final Field FIELD_LABEL = new StringField(32).utf8(); - - private String label; - - public SetLabelRequest() { - setTagged(false); - setAddressable(true); - setAckRequired(true); - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 32; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - label = FIELD_LABEL.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return FIELD_LABEL.bytes(label); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightInfraredRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightInfraredRequest.java deleted file mode 100644 index e9d601cf32..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightInfraredRequest.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; - -/** - * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality - */ -public class SetLightInfraredRequest extends Packet { - - public static final int TYPE = 0x7A; - - public static final Field FIELD_STATE = new UInt16Field().little(); - - private int infrared; - - public int getInfrared() { - return infrared; - } - - public SetLightInfraredRequest() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public SetLightInfraredRequest(int infrared) { - this(); - this.infrared = infrared; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - infrared = FIELD_STATE.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(2).put(FIELD_STATE.bytes(infrared)); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLightInfraredResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightPowerRequest.java deleted file mode 100644 index ed186fd097..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetLightPowerRequest.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetLightPowerRequest extends Packet { - - public static final int TYPE = 0x75; - - public static final Field FIELD_STATE = new UInt16Field(); - public static final Field FIELD_DURATION = new UInt32Field().little(); - - private PowerState state; - private long duration; - - public PowerState getState() { - return state; - } - - public long getDuration() { - return duration; - } - - public void setDuration(long duration) { - this.duration = duration; - } - - public SetLightPowerRequest() { - state = PowerState.OFF; - this.duration = 0; - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public SetLightPowerRequest(PowerState state) { - this.state = state; - this.duration = 0; - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 6; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - state = PowerState.fromValue(FIELD_STATE.value(bytes)); - duration = FIELD_DURATION.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())) - .put(FIELD_DURATION.bytes(duration)); - } - - @Override - public int[] expectedResponses() { - return new int[] { StateLightPowerResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetPowerRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetPowerRequest.java deleted file mode 100644 index 8b0d61a30b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetPowerRequest.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetPowerRequest extends Packet { - - public static final int TYPE = 0x15; - - public static final Field FIELD_STATE = new UInt16Field(); - - private PowerState state; - - public PowerState getState() { - return state; - } - - public SetPowerRequest() { - state = PowerState.OFF; - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - public SetPowerRequest(PowerState state) { - this.state = state; - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - state = PowerState.fromValue(FIELD_STATE.value(bytes)); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(2).put(FIELD_STATE.bytes(state.getValue())); - } - - @Override - public int[] expectedResponses() { - return new int[] { StatePowerResponse.TYPE }; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTagsRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTagsRequest.java deleted file mode 100644 index 384ae11df8..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTagsRequest.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class SetTagsRequest extends Packet { - - public static final int TYPE = 0x1B; - - public static final Field FIELD_TAGS = new UInt64Field(); - - private long tags; - - public long getTags() { - return tags; - } - - public void setTags(long tags) { - this.tags = tags; - } - - public SetTagsRequest() { - } - - public SetTagsRequest(long tags) { - this.tags = tags; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 8; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - tags = FIELD_TAGS.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTileEffectRequest.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTileEffectRequest.java deleted file mode 100644 index f4cef593ae..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SetTileEffectRequest.java +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Implementation of SetTileEffect packet - * - * @author Pawel Pieczul - Initial contribution - */ -public class SetTileEffectRequest extends Packet { - - public static final int TYPE = 0x2CF; - - // size.from.to....what - // ------------------------------ - // 1....0....0.....reserved - // 1....1....1.....reserved - // 4....2....5.....instance ID - // 1....6....6.....effect type - // 4....7....10....speed - // 8....11...18....duration - // 4....19...22....reserved - // 4....23...26....reserved - // 32...27...58....parameters (8*32 bits) - // 1....59...59....palette count - // 128..60...187...palette (16*8 bits) - - private static final Field FIELD_RESERVED_0 = new UInt8Field(); - private static final Field FIELD_RESERVED_1 = new UInt8Field(); - private static final Field FIELD_INSTANCE_ID = new UInt32Field().little(); - private static final Field FIELD_TYPE = new UInt8Field(); - private static final Field FIELD_SPEED = new UInt32Field().little(); - private static final Field FIELD_DURATION = new UInt64Field().little(); - private static final Field FIELD_RESERVED_19_TO_26 = new UInt32Field().little(); - private static final Field FIELD_PARAMETER_27_TO_58 = new UInt32Field().little(); - private static final Field FIELD_PALETTE_COUNT = new UInt8Field(); - private static final Field FIELD_PALETTE_60_TO_187 = new HSBKField(); - - private final Logger logger = LoggerFactory.getLogger(SetTileEffectRequest.class); - - private Integer reserved0 = 0; - private Integer reserved1 = 0; - private Long reserved19to22 = 0L; - private Long reserved23to26 = 0L; - private Effect effect; - - public SetTileEffectRequest() { - setTagged(false); - setAddressable(true); - setAckRequired(true); - } - - public SetTileEffectRequest(Effect effect) { - this(); - this.effect = effect; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 188; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - reserved0 = FIELD_RESERVED_0.value(bytes); - reserved1 = FIELD_RESERVED_1.value(bytes); - FIELD_INSTANCE_ID.value(bytes); - Integer effectType = FIELD_TYPE.value(bytes); - Long speed = FIELD_SPEED.value(bytes); - Long duration = FIELD_DURATION.value(bytes); - reserved19to22 = FIELD_RESERVED_19_TO_26.value(bytes); - reserved23to26 = FIELD_RESERVED_19_TO_26.value(bytes); - Long[] parameters = new Long[8]; - for (int i = 0; i < parameters.length; i++) { - parameters[i] = FIELD_PARAMETER_27_TO_58.value(bytes); - } - Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes); - HSBK[] palette = new HSBK[paletteCount]; - for (int i = 0; i < palette.length; i++) { - palette[i] = FIELD_PALETTE_60_TO_187.value(bytes); - } - try { - effect = new Effect(effectType, speed, duration, palette); - } catch (IllegalArgumentException e) { - logger.debug("Wrong effect type received: {}", effectType); - effect = null; - } - } - - @Override - protected ByteBuffer packetBytes() { - ByteBuffer buffer = ByteBuffer.allocate(packetLength()); - buffer.put(FIELD_RESERVED_0.bytes(reserved0)); - buffer.put(FIELD_RESERVED_1.bytes(reserved1)); - buffer.put(FIELD_INSTANCE_ID.bytes(0L)); - buffer.put(FIELD_TYPE.bytes(effect.getType().intValue())); - buffer.put(FIELD_SPEED.bytes(effect.getSpeed())); - buffer.put(FIELD_DURATION.bytes(effect.getDuration())); - buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved19to22)); - buffer.put(FIELD_RESERVED_19_TO_26.bytes(reserved23to26)); - for (int i = 0; i < 8; i++) { - buffer.put(FIELD_PARAMETER_27_TO_58.bytes(0L)); - } - buffer.put(FIELD_PALETTE_COUNT.bytes(effect.getPalette().length)); - HSBK[] palette = effect.getPalette(); - for (int i = 0; i < palette.length; i++) { - buffer.put(FIELD_PALETTE_60_TO_187.bytes(palette[i])); - } - HSBK hsbkZero = new HSBK(0, 0, 0, 0); - for (int i = 0; i < 16 - palette.length; i++) { - buffer.put(FIELD_PALETTE_60_TO_187.bytes(hsbkZero)); - } - if (logger.isDebugEnabled()) { - logger.debug("SetTileEffectRequest: type={}, speed={}, duration={}, palette_count={}", effect.getType(), - effect.getSpeed(), effect.getDuration(), palette.length); - for (int i = 0; i < palette.length; i++) { - logger.debug("SetTileEffectRequest palette[{}] = {}", i, palette[i]); - } - } - return buffer; - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SignalStrength.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SignalStrength.java deleted file mode 100644 index 5a10b8a2da..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/SignalStrength.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -/** - * The signal strength of a light. - * - * @author Wouter Born - Add signal strength channel - */ -public class SignalStrength { - - private double milliWatts; - - public SignalStrength(double milliWatts) { - this.milliWatts = milliWatts; - } - - /** - * Returns the signal strength. - * - * @return the signal strength in milliwatts (mW). - */ - public double getMilliWatts() { - return milliWatts; - } - - /** - * Returns the signal strength as a quality percentage: - *

        - *
      • RSSI <= -100: returns 0 - *
      • -100 < RSSI < -50: returns a value between 0 and 1 (linearly distributed) - *
      • RSSI >= -50: returns 1 - *
          - * - * @return a value between 0 and 1. 0 being worst strength and 1 - * being best strength. - */ - public double toQualityPercentage() { - return Math.min(100, Math.max(0, 2 * (toRSSI() + 100))) / 100; - } - - /** - * Returns the signal strength as a quality rating. - * - * @return one of the values: 0, 1, 2, 3 or 4. 0 being worst strength and 4 - * being best strength. - */ - public byte toQualityRating() { - return (byte) Math.round(toQualityPercentage() * 4); - } - - /** - * Returns the received signal strength indicator (RSSI). - * - * @return a value <= 0. 0 being best strength and more negative values indicate worser strength. - */ - public double toRSSI() { - return 10 * Math.log10(milliWatts); - } - - @Override - public String toString() { - return "SignalStrength [milliWatts=" + milliWatts + ", rssi=" + Math.round(toRSSI()) + "]"; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateGroupResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateGroupResponse.java deleted file mode 100644 index 274a9c510a..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateGroupResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.StringField; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateGroupResponse extends Packet { - - public static final int TYPE = 0x35; - - public static final Field FIELD_GROUP = new ByteField(16); - public static final Field FIELD_LABEL = new StringField(32).utf8(); - public static final Field FIELD_UPDATED_AT = new UInt64Field().little(); - - private ByteBuffer group; - private String label; - private long updatedAt; - - public static int getType() { - return TYPE; - } - - public ByteBuffer getGroup() { - return group; - } - - public void setGroup(ByteBuffer group) { - this.group = group; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public long getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(long updatedAt) { - this.updatedAt = updatedAt; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 14; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - group = FIELD_GROUP.value(bytes); - label = FIELD_LABEL.value(bytes); - updatedAt = FIELD_UPDATED_AT.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_GROUP.bytes(group)).put(FIELD_LABEL.bytes(label)) - .put(FIELD_UPDATED_AT.bytes(updatedAt)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostFirmwareResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostFirmwareResponse.java deleted file mode 100644 index 4eb17305a7..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostFirmwareResponse.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; -import org.openhab.binding.lifx.internal.fields.Version; -import org.openhab.binding.lifx.internal.fields.VersionField; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateHostFirmwareResponse extends Packet { - - public static final int TYPE = 0x0F; - - public static final Field FIELD_BUILD = new UInt64Field().little(); - public static final Field FIELD_RESERVED = new UInt64Field().little(); - public static final Field FIELD_VERSION = new VersionField().little(); - - private long build; - private long reserved; - private Version version; - - public long getBuild() { - return build; - } - - public void setBuild(long build) { - this.build = build; - } - - public Version getVersion() { - return version; - } - - public void setVersion(Version version) { - this.version = version; - } - - public StateHostFirmwareResponse() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 20; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - build = FIELD_BUILD.value(bytes); - reserved = FIELD_RESERVED.value(bytes); - version = FIELD_VERSION.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_BUILD.bytes(build)).put(FIELD_RESERVED.bytes(reserved)) - .put(FIELD_VERSION.bytes(version)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostInfoResponse.java deleted file mode 100644 index 5b4b712806..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateHostInfoResponse.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.FloatField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateHostInfoResponse extends Packet { - - public static final int TYPE = 0x0D; - - public static final Field FIELD_SIGNAL = new FloatField().little(); - public static final Field FIELD_TX = new UInt32Field().little(); - public static final Field FIELD_RX = new UInt32Field().little(); - public static final Field FIELD_RESERVED_5 = new ByteField(2); - - private float signal; - private long tx; - private long rx; - - public SignalStrength getSignalStrength() { - return new SignalStrength(signal); - } - - public long getTx() { - return tx; - } - - public long getRx() { - return rx; - } - - public StateHostInfoResponse() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 14; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - signal = FIELD_SIGNAL.value(bytes); - tx = FIELD_TX.value(bytes); - rx = FIELD_RX.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_SIGNAL.bytes(signal)).put(FIELD_TX.bytes(tx)) - .put(FIELD_RX.bytes(rx)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateInfoResponse.java deleted file mode 100644 index 5b7e914330..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateInfoResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateInfoResponse extends Packet { - - public static final int TYPE = 0x23; - - public static final Field FIELD_TIME = new UInt64Field().little(); - public static final Field FIELD_UPTIME = new UInt64Field().little(); - public static final Field FIELD_DOWNTIME = new UInt64Field().little(); - - private long time; - private long uptime; - private long downtime; - - public long getTime() { - return time; - } - - public void setTime(long time) { - this.time = time; - } - - public long getUptime() { - return uptime; - } - - public void setUptime(long uptime) { - this.uptime = uptime; - } - - public long getDowntime() { - return downtime; - } - - public void setDowntime(long downtime) { - this.downtime = downtime; - } - - public StateInfoResponse() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 24; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - time = FIELD_TIME.value(bytes); - uptime = FIELD_UPTIME.value(bytes); - downtime = FIELD_DOWNTIME.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_TIME.bytes(time)).put(FIELD_UPTIME.bytes(uptime)) - .put(FIELD_DOWNTIME.bytes(downtime)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLabelResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLabelResponse.java deleted file mode 100644 index c41afa3184..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLabelResponse.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.StringField; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateLabelResponse extends Packet { - - public static final int TYPE = 0x19; - - public static final Field FIELD_LABEL = new StringField(32).utf8(); - - private String label; - - public static int getType() { - return TYPE; - } - - public static Field getFieldLabel() { - return FIELD_LABEL; - } - - public String getLabel() { - return label; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 32; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - label = FIELD_LABEL.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return FIELD_LABEL.bytes(label); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightInfraredResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightInfraredResponse.java deleted file mode 100644 index 9012583da0..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightInfraredResponse.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; - -/** - * @author Wouter Born - Support LIFX 2016 product line-up and infrared functionality - */ -public class StateLightInfraredResponse extends Packet { - - public static final int TYPE = 0x79; - - public static final Field FIELD_STATE = new UInt16Field().little(); - - private int infrared; - - public int getInfrared() { - return infrared; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - infrared = FIELD_STATE.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(infrared)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightPowerResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightPowerResponse.java deleted file mode 100644 index 6b848afb6c..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLightPowerResponse.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateLightPowerResponse extends Packet { - - public static final int TYPE = 0x76; - - public static final Field FIELD_STATE = new UInt16Field(); - - private PowerState state; - - public PowerState getState() { - return state; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - int stateValue = FIELD_STATE.value(bytes); - state = PowerState.fromValue(stateValue); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLocationResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLocationResponse.java deleted file mode 100644 index 35d726aad2..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateLocationResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.ByteField; -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.StringField; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateLocationResponse extends Packet { - - public static final int TYPE = 0x32; - - public static final Field FIELD_LOCATION = new ByteField(16); - public static final Field FIELD_LABEL = new StringField(32).utf8(); - public static final Field FIELD_UPDATED_AT = new UInt64Field().little(); - - private ByteBuffer location; - private String label; - private long updatedAt; - - public static int getType() { - return TYPE; - } - - public ByteBuffer getLocation() { - return location; - } - - public void setLocation(ByteBuffer location) { - this.location = location; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public long getUpdatedAt() { - return updatedAt; - } - - public void setUpdatedAt(long updatedAt) { - this.updatedAt = updatedAt; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 14; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - location = FIELD_LOCATION.value(bytes); - label = FIELD_LABEL.value(bytes); - updatedAt = FIELD_UPDATED_AT.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_LOCATION.bytes(location)).put(FIELD_LABEL.bytes(label)) - .put(FIELD_UPDATED_AT.bytes(updatedAt)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateMultiZoneResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateMultiZoneResponse.java deleted file mode 100644 index 47f167deb0..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateMultiZoneResponse.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * @author Wouter Born - Add support for MultiZone light control - */ -public class StateMultiZoneResponse extends Packet { - - public static final int TYPE = 0x1FA; - public static final int ZONES = 8; - - public static final Field FIELD_COUNT = new UInt8Field(); - public static final Field FIELD_INDEX = new UInt8Field(); - public static final HSBKField FIELD_COLOR = new HSBKField(); - - private int count; - private int index; - private HSBK[] colors = new HSBK[ZONES]; - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("count="); - sb.append(count); - sb.append(", index="); - sb.append(index); - - for (int i = 0; i < ZONES; i++) { - sb.append(", "); - sb.append(colors[i].toString("color[" + i + "]")); - } - - return sb.toString(); - } - - public int getCount() { - return count; - } - - public int getIndex() { - return index; - } - - public HSBK[] getColors() { - return colors; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 66; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - count = FIELD_COUNT.value(bytes); - index = FIELD_INDEX.value(bytes); - for (int i = 0; i < ZONES; i++) { - colors[i] = FIELD_COLOR.value(bytes); - } - } - - @Override - protected ByteBuffer packetBytes() { - ByteBuffer bb = ByteBuffer.allocate(packetLength()).put(FIELD_COUNT.bytes(count)).put(FIELD_INDEX.bytes(index)); - for (int i = 0; i < ZONES; i++) { - bb.put(FIELD_COLOR.bytes(colors[i])); - } - return bb; - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StatePowerResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StatePowerResponse.java deleted file mode 100644 index b6689a6429..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StatePowerResponse.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt16Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StatePowerResponse extends Packet { - - public static final int TYPE = 0x16; - - public static final Field FIELD_STATE = new UInt16Field(); - - private PowerState state; - - public PowerState getState() { - return state; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 2; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - int stateValue = FIELD_STATE.value(bytes); - state = PowerState.fromValue(stateValue); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_STATE.bytes(state.getValue())); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateResponse.java deleted file mode 100644 index ff3bf40bf2..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateResponse.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.StringField; -import org.openhab.binding.lifx.internal.fields.UInt16Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateResponse extends Packet { - - public static final int TYPE = 0x6B; - - public static final HSBKField FIELD_COLOR = new HSBKField(); - public static final Field FIELD_DIM = new UInt16Field().little(); - public static final Field FIELD_POWER = new UInt16Field(); - public static final Field FIELD_LABEL = new StringField(32); - public static final Field FIELD_TAGS = new UInt64Field(); - - private HSBK color; - private int dim; - private PowerState power; - private String label; - private long tags; - - @Override - public String toString() { - return color.toString("color") + ", dim=" + dim + ", power=" + power + ", label=" + label; - } - - public HSBK getColor() { - return color; - } - - public int getDim() { - return dim; - } - - public PowerState getPower() { - return power; - } - - public String getLabel() { - return label; - } - - public long getTags() { - return tags; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 52; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - color = FIELD_COLOR.value(bytes); - dim = FIELD_DIM.value(bytes); - power = PowerState.fromValue(FIELD_POWER.value(bytes)); - label = FIELD_LABEL.value(bytes); - tags = FIELD_TAGS.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_COLOR.bytes(color)).put(FIELD_DIM.bytes(dim)) - .put(FIELD_POWER.bytes(power.getValue())).put(FIELD_LABEL.bytes(label)).put(FIELD_TAGS.bytes(tags)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateServiceResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateServiceResponse.java deleted file mode 100644 index 84c6a23826..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateServiceResponse.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.LittleField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateServiceResponse extends Packet { - - public static final int TYPE = 0x03; - - public static final Field FIELD_SERVICE = new UInt8Field(); - public static final Field FIELD_PORT = new LittleField<>(new UInt32Field()); - - private int service; - private long port; - - public int getService() { - return service; - } - - public long getPort() { - return port; - } - - public StateServiceResponse() { - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - service = FIELD_SERVICE.value(bytes); - port = FIELD_PORT.value(bytes); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 5; - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_SERVICE.bytes(service)).put(FIELD_PORT.bytes(port)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateTileEffectResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateTileEffectResponse.java deleted file mode 100644 index 6bfb630f59..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateTileEffectResponse.java +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt32Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; -import org.openhab.binding.lifx.internal.fields.UInt8Field; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Implementation of StateTileEffect packet - * - * @author Pawel Pieczul - Initial Contribution - */ -public class StateTileEffectResponse extends Packet { - - public static final int TYPE = 0x2D0; - - // size.from.to....what - // ------------------------------ - // 1....0....0.....reserved - // 4....1....4.....instance ID - // 1....5....5.....effect type - // 4....6....9.....speed - // 8....10...17....duration - // 4....18...21....reserved - // 4....22...25....reserved - // 32...26...57....parameters (8*32 bits) - // 1....58...58....palette count - // 128..59...186...palette (16*8 bits) - - private static final Field FIELD_RESERVED_0 = new UInt8Field(); - private static final Field FIELD_INSTANCE_ID = new UInt32Field().little(); - private static final Field FIELD_TYPE = new UInt8Field(); - private static final Field FIELD_SPEED = new UInt32Field().little(); - private static final Field FIELD_DURATION = new UInt64Field().little(); - private static final Field FIELD_RESERVED_18_TO_25 = new UInt32Field().little(); - private static final Field FIELD_PARAMETER_26_TO_57 = new UInt32Field().little(); - private static final Field FIELD_PALETTE_COUNT = new UInt8Field(); - private static final Field FIELD_PALETTE_59_TO_186 = new HSBKField(); - - private Integer reserved0 = 0; - private Long reserved18to21 = 0L; - private Long reserved22to25 = 0L; - private Effect effect; - - private final Logger logger = LoggerFactory.getLogger(StateTileEffectResponse.class); - - public Effect getEffect() { - return effect; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 187; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - reserved0 = FIELD_RESERVED_0.value(bytes); - Long instanceId = FIELD_INSTANCE_ID.value(bytes); - Integer effectType = FIELD_TYPE.value(bytes); - Long speed = FIELD_SPEED.value(bytes); - Long duration = FIELD_DURATION.value(bytes); - reserved18to21 = FIELD_RESERVED_18_TO_25.value(bytes); - reserved22to25 = FIELD_RESERVED_18_TO_25.value(bytes); - Long[] parameters = new Long[8]; - for (int i = 0; i < 8; i++) { - parameters[i] = FIELD_PARAMETER_26_TO_57.value(bytes); - } - Integer paletteCount = FIELD_PALETTE_COUNT.value(bytes); - HSBK[] palette = new HSBK[paletteCount]; - for (int i = 0; i < palette.length; i++) { - palette[i] = FIELD_PALETTE_59_TO_186.value(bytes); - } - try { - effect = new Effect(effectType, speed, duration, palette); - } catch (IllegalArgumentException e) { - logger.debug("Wrong effect type received: {}", effectType); - effect = null; - } - if (logger.isDebugEnabled()) { - logger.debug("StateTileEffectResponse: instanceId={}, type={}, speed={}, duration={}, palette_count={}", - instanceId, effectType, speed, duration, paletteCount); - logger.debug("StateTileEffectResponse parameters=[{}, {}, {}, {}, {}, {}, {}, {}]", parameters[0], - parameters[1], parameters[2], parameters[3], parameters[4], parameters[5], parameters[6], - parameters[7]); - for (int i = 0; i < palette.length; i++) { - logger.debug("StateTileEffectResponse palette[{}] = {}", i, palette[i]); - } - } - } - - @Override - protected ByteBuffer packetBytes() { - ByteBuffer buffer = ByteBuffer.allocate(packetLength()); - buffer.put(FIELD_RESERVED_0.bytes(reserved0)); - buffer.put(FIELD_INSTANCE_ID.bytes(0L)); - buffer.put(FIELD_TYPE.bytes(effect.getType().intValue())); - buffer.put(FIELD_SPEED.bytes(effect.getSpeed())); - buffer.put(FIELD_DURATION.bytes(effect.getDuration())); - buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved18to21)); - buffer.put(FIELD_RESERVED_18_TO_25.bytes(reserved22to25)); - for (int i = 0; i < 8; i++) { - buffer.put(FIELD_PARAMETER_26_TO_57.bytes(0L)); - } - HSBK[] palette = effect.getPalette(); - buffer.put(FIELD_PALETTE_COUNT.bytes(palette.length)); - for (int i = 0; i < palette.length; i++) { - buffer.put(FIELD_PALETTE_59_TO_186.bytes(palette[i])); - } - HSBK hsbkZero = new HSBK(0, 0, 0, 0); - for (int i = 0; i < 16 - palette.length; i++) { - buffer.put(FIELD_PALETTE_59_TO_186.bytes(hsbkZero)); - } - return buffer; - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateVersionResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateVersionResponse.java deleted file mode 100644 index 410dc3720b..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateVersionResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateVersionResponse extends Packet { - - public static final int TYPE = 0x21; - - public static final Field FIELD_VENDOR = new UInt32Field().little(); - public static final Field FIELD_PRODUCT = new UInt32Field().little(); - public static final Field FIELD_VERSION = new UInt32Field().little(); - - private long vendor; - private long product; - private long version; - - public long getVendor() { - return vendor; - } - - public void setVendor(long build) { - this.vendor = build; - } - - public long getProduct() { - return product; - } - - public void setProduct(long product) { - this.product = product; - } - - public long getVersion() { - return version; - } - - public void setVersion(long version) { - this.version = version; - } - - public StateVersionResponse() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 12; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - vendor = FIELD_VENDOR.value(bytes); - product = FIELD_PRODUCT.value(bytes); - version = FIELD_VERSION.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_VENDOR.bytes(vendor)).put(FIELD_PRODUCT.bytes(product)) - .put(FIELD_VERSION.bytes(version)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiFirmwareResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiFirmwareResponse.java deleted file mode 100644 index b0cbc95010..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiFirmwareResponse.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; -import org.openhab.binding.lifx.internal.fields.Version; -import org.openhab.binding.lifx.internal.fields.VersionField; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateWifiFirmwareResponse extends Packet { - - public static final int TYPE = 0x13; - - public static final Field FIELD_BUILD = new UInt64Field().little(); - public static final Field FIELD_RESERVED = new UInt64Field().little(); - public static final Field FIELD_VERSION = new VersionField().little(); - - private long build; - private long reserved; - private Version version; - - public long getBuild() { - return build; - } - - public void setBuild(long build) { - this.build = build; - } - - public Version getVersion() { - return version; - } - - public void setVersion(Version version) { - this.version = version; - } - - public StateWifiFirmwareResponse() { - setTagged(false); - setAddressable(true); - setResponseRequired(true); - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 20; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - build = FIELD_BUILD.value(bytes); - reserved = FIELD_RESERVED.value(bytes); - version = FIELD_VERSION.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_BUILD.bytes(build)).put(FIELD_RESERVED.bytes(reserved)) - .put(FIELD_VERSION.bytes(version)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiInfoResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiInfoResponse.java deleted file mode 100644 index 3cb832b153..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateWifiInfoResponse.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.FloatField; -import org.openhab.binding.lifx.internal.fields.UInt16Field; -import org.openhab.binding.lifx.internal.fields.UInt32Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class StateWifiInfoResponse extends Packet { - - public static final int TYPE = 0x11; - - public static final Field FIELD_SIGNAL = new FloatField().little(); - public static final Field FIELD_RX = new UInt32Field().little(); - public static final Field FIELD_TX = new UInt32Field().little(); - public static final Field FIELD_TEMP = new UInt16Field(); - - private float signal; - private long rx; - private long tx; - private int mcuTemperature; - - public SignalStrength getSignalStrength() { - return new SignalStrength(signal); - } - - public long getRx() { - return rx; - } - - public long getTx() { - return tx; - } - - public int getMcuTemperature() { - return mcuTemperature; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 14; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - signal = FIELD_SIGNAL.value(bytes); - rx = FIELD_RX.value(bytes); - tx = FIELD_TX.value(bytes); - mcuTemperature = FIELD_TEMP.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_SIGNAL.bytes(signal)).put(FIELD_RX.bytes(rx)) - .put(FIELD_TX.bytes(tx)).put(FIELD_TEMP.bytes(mcuTemperature)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateZoneResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateZoneResponse.java deleted file mode 100644 index e95135fc8e..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/StateZoneResponse.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.fields.HSBKField; -import org.openhab.binding.lifx.internal.fields.UInt8Field; - -/** - * @author Wouter Born - Add support for MultiZone light control - */ -public class StateZoneResponse extends Packet { - - public static final int TYPE = 0x1F7; - - public static final Field FIELD_COUNT = new UInt8Field(); - public static final Field FIELD_INDEX = new UInt8Field(); - public static final HSBKField FIELD_COLOR = new HSBKField(); - - private int count; - private int index; - private HSBK color; - - @Override - public String toString() { - return "count=" + count + ", index=" + index + ", " + color.toString("color"); - } - - public int getCount() { - return count; - } - - public int getIndex() { - return index; - } - - public HSBK getColor() { - return color; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 10; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - count = FIELD_COUNT.value(bytes); - index = FIELD_INDEX.value(bytes); - color = FIELD_COLOR.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_COUNT.bytes(count)).put(FIELD_INDEX.bytes(index)) - .put(FIELD_COLOR.bytes(color)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagLabelsResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagLabelsResponse.java deleted file mode 100644 index ef3aa17676..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagLabelsResponse.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.StringField; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class TagLabelsResponse extends Packet { - - public static final int TYPE = 0x1F; - - public static final Field FIELD_TAGS = new UInt64Field(); - public static final Field FIELD_LABEL = new StringField(32).utf8(); - - private long tags; - private String label; - - public long getTags() { - return tags; - } - - public String getLabel() { - return label; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 40; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - tags = FIELD_TAGS.value(bytes); - label = FIELD_LABEL.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)).put(FIELD_LABEL.bytes(label)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagsResponse.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagsResponse.java deleted file mode 100644 index 257f28eab9..0000000000 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/protocol/TagsResponse.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright (c) 2010-2021 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.binding.lifx.internal.protocol; - -import java.nio.ByteBuffer; - -import org.openhab.binding.lifx.internal.fields.Field; -import org.openhab.binding.lifx.internal.fields.UInt64Field; - -/** - * @author Tim Buckley - Initial Contribution - * @author Karel Goderis - Enhancement for the V2 LIFX Firmware and LAN Protocol Specification - */ -public class TagsResponse extends Packet { - - public static final int TYPE = 0x1C; - - public static final Field FIELD_TAGS = new UInt64Field(); - - private long tags; - - public long getTags() { - return tags; - } - - @Override - public int packetType() { - return TYPE; - } - - @Override - protected int packetLength() { - return 8; - } - - @Override - protected void parsePacket(ByteBuffer bytes) { - tags = FIELD_TAGS.value(bytes); - } - - @Override - protected ByteBuffer packetBytes() { - return ByteBuffer.allocate(packetLength()).put(FIELD_TAGS.bytes(tags)); - } - - @Override - public int[] expectedResponses() { - return new int[] {}; - } -} diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxMessageUtil.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxMessageUtil.java index 7218a29f3d..03c8135e2d 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxMessageUtil.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxMessageUtil.java @@ -17,8 +17,8 @@ import java.math.RoundingMode; import java.util.UUID; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.lifx.internal.LifxProduct.TemperatureRange; import org.openhab.binding.lifx.internal.fields.HSBK; -import org.openhab.binding.lifx.internal.protocol.Product.TemperatureRange; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.IncreaseDecreaseType; import org.openhab.core.library.types.PercentType; @@ -26,7 +26,7 @@ import org.openhab.core.library.types.PercentType; /** * Utility class for sharing message utility methods between objects. * - * @author Wouter Born - Extracted methods from LifxLightHandler + * @author Wouter Born - Initial contribution */ @NonNullByDefault public final class LifxMessageUtil { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxNetworkUtil.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxNetworkUtil.java index 4d47748014..eca9a34a17 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxNetworkUtil.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxNetworkUtil.java @@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory; * The {@link LifxNetworkUtil} provides network interface information to the LIFX binding objects. The information is * updated when it is older than {@link #UPDATE_INTERVAL_MILLIS}. * - * @author Wouter Born - Periodically update available interface information + * @author Wouter Born - Initial contribution */ @NonNullByDefault public final class LifxNetworkUtil { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxSelectorUtil.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxSelectorUtil.java index 10dec21b90..1630ec296a 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxSelectorUtil.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxSelectorUtil.java @@ -32,17 +32,17 @@ import java.util.function.BiConsumer; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.lifx.internal.LifxSelectorContext; +import org.openhab.binding.lifx.internal.dto.Packet; +import org.openhab.binding.lifx.internal.dto.PacketFactory; +import org.openhab.binding.lifx.internal.dto.PacketHandler; import org.openhab.binding.lifx.internal.fields.MACAddress; -import org.openhab.binding.lifx.internal.protocol.Packet; -import org.openhab.binding.lifx.internal.protocol.PacketFactory; -import org.openhab.binding.lifx.internal.protocol.PacketHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Utility class for sharing {@link Selector} logic between objects. * - * @author Wouter Born - Make selector logic reusable between discovery and handlers + * @author Wouter Born - Initial contribution */ @NonNullByDefault public class LifxSelectorUtil { diff --git a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxThrottlingUtil.java b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxThrottlingUtil.java index 397df9e07e..8001d1e74b 100644 --- a/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxThrottlingUtil.java +++ b/bundles/org.openhab.binding.lifx/src/main/java/org/openhab/binding/lifx/internal/util/LifxThrottlingUtil.java @@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory; * sent to LIFX lights. The LIFX LAN Protocol Specification states that lights can process up to 20 messages per second, * not more. * - * @author Karel Goderis - Initial Contribution + * @author Karel Goderis - Initial contribution * @author Wouter Born - Deadlock fix */ @NonNullByDefault diff --git a/bundles/org.openhab.binding.lifx/src/test/java/org/openhab/binding/lifx/internal/fields/MACAddressTest.java b/bundles/org.openhab.binding.lifx/src/test/java/org/openhab/binding/lifx/internal/fields/MACAddressTest.java new file mode 100644 index 0000000000..78b620b7af --- /dev/null +++ b/bundles/org.openhab.binding.lifx/src/test/java/org/openhab/binding/lifx/internal/fields/MACAddressTest.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.lifx.internal.fields; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.openhab.binding.lifx.internal.fields.MACAddress.BROADCAST_ADDRESS; +import static org.openhab.core.util.HexUtils.bytesToHex; + +import java.nio.ByteBuffer; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.junit.jupiter.api.Test; +import org.openhab.core.util.HexUtils; + +/** + * Tests {@link MACAddress}. + * + * @author Wouter Born - Initial contribution + */ +@NonNullByDefault +public class MACAddressTest { + + @Test + public void broadcastAddress() { + assertThat(BROADCAST_ADDRESS.getAsLabel(), is("000000000000")); + assertThat(BROADCAST_ADDRESS.getHex(), is("00:00:00:00:00:00")); + assertThat(bytesToHex(BROADCAST_ADDRESS.getBytes().array()), is("000000000000")); + } + + @Test + public void defaultConstructor() { + MACAddress macAddress = new MACAddress(); + assertThat(macAddress.getAsLabel(), is("000000000000")); + assertThat(macAddress.getHex(), is("00:00:00:00:00:00")); + } + + @Test + public void constructFromByteBuffer() { + MACAddress macAddress = new MACAddress(ByteBuffer.wrap(HexUtils.hexToBytes("D073D5123456"))); + assertThat(macAddress.getAsLabel(), is("D073D5123456")); + assertThat(macAddress.getHex(), is("D0:73:D5:12:34:56")); + assertThat(bytesToHex(macAddress.getBytes().array()), is("D073D5123456")); + } + + @Test + public void constructFromString() { + MACAddress macAddress = new MACAddress("D073D5ABCDEF"); + assertThat(macAddress.getAsLabel(), is("D073D5ABCDEF")); + assertThat(macAddress.getHex(), is("D0:73:D5:AB:CD:EF")); + assertThat(bytesToHex(macAddress.getBytes().array()), is("D073D5ABCDEF")); + } + + @Test + public void broadcastAddressComparison() { + assertThat(BROADCAST_ADDRESS, is(BROADCAST_ADDRESS)); + assertThat(BROADCAST_ADDRESS.hashCode(), is(BROADCAST_ADDRESS.hashCode())); + + assertThat(BROADCAST_ADDRESS, is(new MACAddress())); + assertThat(BROADCAST_ADDRESS.hashCode(), is(new MACAddress().hashCode())); + + assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF")))); + assertThat(BROADCAST_ADDRESS, is(not(new MACAddress("D073D5ABCDEF").hashCode()))); + } + + @Test + public void macAddressComparison() { + assertThat(new MACAddress("D073D5ABCDEF"), is(new MACAddress("D073D5ABCDEF"))); + assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(new MACAddress("D073D5ABCDEF").hashCode())); + + assertThat(new MACAddress("D073D5ABCDEF"), is(not(BROADCAST_ADDRESS))); + assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(BROADCAST_ADDRESS.hashCode()))); + + assertThat(new MACAddress("D073D5ABCDEF"), is(not(new MACAddress("D073D5123456")))); + assertThat(new MACAddress("D073D5ABCDEF").hashCode(), is(not(new MACAddress("D073D5123456").hashCode()))); + } +}