package com.imdroid.sideslope.executor;

import com.imdroid.sideslope.message.D314PingMessage;
import com.imdroid.sideslope.server.OnlineChannels;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.stereotype.Component;

/**
 * @author Layton
 * @date 2023/2/2 20:49
 */
@Component
public class D314PingMessageExecutor implements Executor<D314PingMessage, Void> {

    @Override
    public Void execute(D314PingMessage message) {
        String deviceId = message.getId();
        byte[] forwardBytes = message.getSrcData();
        OnlineChannels.INSTANCE.get(deviceId).ifPresent(deviceChannel -> {
            forwardBytes[12] = (byte) 1;//ping ack
            ByteBuf buf = Unpooled.buffer();
            buf.writeBytes(forwardBytes);
            deviceChannel.writeAndFlush(buf);
        });

        return null;
    }

    @Override
    public Class<?> getMessageType() {
        return D314PingMessage.class;
    }
}
