Search in sources :

Example 1 with Packet

use of org.eclipse.smarthome.binding.lifx.internal.protocol.Packet in project smarthome by eclipse.

the class LifxLightStateChanger method createPendingPackets.

private List<PendingPacket> createPendingPackets(Packet... packets) {
    Integer packetType = null;
    List<PendingPacket> pendingPackets = new ArrayList<>();
    for (Packet packet : packets) {
        // the acknowledgement is used to resend the packet in case of packet loss
        packet.setAckRequired(true);
        // the LIFX LAN protocol spec indicates that the response returned for a request would be the
        // previous value
        packet.setResponseRequired(false);
        pendingPackets.add(new PendingPacket(packet));
        if (packetType == null) {
            packetType = packet.getPacketType();
        } else if (packetType != packet.getPacketType()) {
            throw new IllegalArgumentException("Packets should have same packet type");
        }
    }
    return pendingPackets;
}
Also used : Packet(org.eclipse.smarthome.binding.lifx.internal.protocol.Packet) ArrayList(java.util.ArrayList)

Example 2 with Packet

use of org.eclipse.smarthome.binding.lifx.internal.protocol.Packet in project smarthome by eclipse.

the class LifxLightStateChanger method sendPendingPackets.

private void sendPendingPackets() {
    try {
        lock.lock();
        removeFailedPackets();
        PendingPacket pendingPacket = findPacketToSend();
        if (pendingPacket != null) {
            Packet packet = pendingPacket.packet;
            if (pendingPacket.sendCount == 0) {
                // sendPacket will set the sequence number
                logger.debug("{} : Sending {} packet", logId, packet.getClass().getSimpleName());
                communicationHandler.sendPacket(packet);
            } else {
                // resendPacket will reuse the sequence number
                logger.debug("{} : Resending {} packet", logId, packet.getClass().getSimpleName());
                communicationHandler.resendPacket(packet);
            }
            pendingPacket.lastSend = System.currentTimeMillis();
            pendingPacket.sendCount++;
        }
    } catch (Exception e) {
        logger.error("Error occurred while sending packet", e);
    } finally {
        lock.unlock();
    }
}
Also used : Packet(org.eclipse.smarthome.binding.lifx.internal.protocol.Packet)

Example 3 with Packet

use of org.eclipse.smarthome.binding.lifx.internal.protocol.Packet in project smarthome by eclipse.

the class LifxSelectorUtil method supplyParsedPacketToConsumer.

private static void supplyParsedPacketToConsumer(ByteBuffer readBuffer, InetSocketAddress address, BiConsumer<Packet, InetSocketAddress> packetConsumer, String logId) {
    int messageLength = readBuffer.position();
    readBuffer.rewind();
    ByteBuffer packetSize = readBuffer.slice();
    packetSize.position(0);
    packetSize.limit(2);
    int size = Packet.FIELD_SIZE.value(packetSize);
    if (messageLength == size) {
        ByteBuffer packetType = readBuffer.slice();
        packetType.position(32);
        packetType.limit(34);
        int type = Packet.FIELD_PACKET_TYPE.value(packetType);
        PacketHandler<?> handler = PacketFactory.createHandler(type);
        if (handler == null) {
            LOGGER.trace("{} : Unknown packet type: {} (source: {})", logId, String.format("0x%02X", type), address.toString());
        } else {
            Packet packet = handler.handle(readBuffer);
            if (packet == null) {
                LOGGER.warn("Packet handler '{}' was unable to create packet from " + "ByteBuffer for the light ({})", handler.getClass().getName(), logId);
            } else {
                packetConsumer.accept(packet, address);
            }
        }
    }
}
Also used : Packet(org.eclipse.smarthome.binding.lifx.internal.protocol.Packet) ByteBuffer(java.nio.ByteBuffer)

Example 4 with Packet

use of org.eclipse.smarthome.binding.lifx.internal.protocol.Packet in project smarthome by eclipse.

the class LifxLightStateChanger method handleResponsePacket.

public void handleResponsePacket(Packet packet) {
    if (packet instanceof AcknowledgementResponse) {
        long ackTimestamp = System.currentTimeMillis();
        PendingPacket pendingPacket;
        try {
            lock.lock();
            pendingPacket = removeAcknowledgedPacket(packet.getSequence());
        } finally {
            lock.unlock();
        }
        if (pendingPacket != null) {
            Packet sentPacket = pendingPacket.packet;
            logger.debug("{} : {} packet was acknowledged in {}ms", logId, sentPacket.getClass().getSimpleName(), ackTimestamp - pendingPacket.lastSend);
            // LifxLightCurrentStateUpdater
            if (sentPacket instanceof SetPowerRequest) {
                GetLightPowerRequest powerPacket = new GetLightPowerRequest();
                communicationHandler.sendPacket(powerPacket);
            } else if (sentPacket instanceof SetColorRequest) {
                GetRequest colorPacket = new GetRequest();
                communicationHandler.sendPacket(colorPacket);
                getZonesIfZonesAreSet();
            } else if (sentPacket instanceof SetColorZonesRequest) {
                getZonesIfZonesAreSet();
            } else if (sentPacket instanceof SetLightInfraredRequest) {
                GetLightInfraredRequest infraredPacket = new GetLightInfraredRequest();
                communicationHandler.sendPacket(infraredPacket);
            }
        } else {
            logger.debug("{} : No pending packet found for ack with sequence number: {}", logId, packet.getSequence());
        }
    }
}
Also used : Packet(org.eclipse.smarthome.binding.lifx.internal.protocol.Packet) SetLightInfraredRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.SetLightInfraredRequest) GetRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetRequest) SetColorZonesRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.SetColorZonesRequest) AcknowledgementResponse(org.eclipse.smarthome.binding.lifx.internal.protocol.AcknowledgementResponse) GetLightInfraredRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetLightInfraredRequest) GetLightPowerRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetLightPowerRequest) SetPowerRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.SetPowerRequest) SetColorRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.SetColorRequest)

Aggregations

Packet (org.eclipse.smarthome.binding.lifx.internal.protocol.Packet)4 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 AcknowledgementResponse (org.eclipse.smarthome.binding.lifx.internal.protocol.AcknowledgementResponse)1 GetLightInfraredRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.GetLightInfraredRequest)1 GetLightPowerRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.GetLightPowerRequest)1 GetRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.GetRequest)1 SetColorRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.SetColorRequest)1 SetColorZonesRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.SetColorZonesRequest)1 SetLightInfraredRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.SetLightInfraredRequest)1 SetPowerRequest (org.eclipse.smarthome.binding.lifx.internal.protocol.SetPowerRequest)1