package com.malk.tonglibo.utils;

import com.malk.tonglibo.entity.RawDeviceData;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 功能：
 * 作者：hanxue
 * 日期：2025/10/18 0:50
 */
@Component
public class DataBuffer {
    // 内存队列，缓冲原始数据
    private final BlockingQueue<RawDeviceData> queue = new LinkedBlockingQueue<>(10000);

    public boolean offer(RawDeviceData data) {
        return queue.offer(data);
    }

    public List<RawDeviceData> drain(int maxSize) {
        List<RawDeviceData> list = new ArrayList<>();
        queue.drainTo(list, maxSize);
        return list;
    }

    public int size() {
        return queue.size();
    }
}
