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;
}
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();
}
}
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);
}
}
}
}
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());
}
}
}
Aggregations