package com.gyee.edge.gateway.bridge.rocketmq.producter;

import com.gyee.protocol.gyfp2.message.GYMessage;
import com.gyee.protocol.gyfp2.message.MessageData;
import com.gyee.protocol.gyfp2.message.MessageMutable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class Gyfp2Protocol {
    public byte[] toBytes(){
        GYMessage gyMessage = new GYMessage();
        gyMessage.setAttribute(0);

        MessageMutable messageMutable = new MessageMutable();
        messageMutable.setMessageType((byte) 11);
        messageMutable.setKey(1002);
        messageMutable.setTs(new Date().getTime());
        gyMessage.setMessageMutable(messageMutable);

        List<MessageData> list = new ArrayList<>();
//        for (int i = 0;i < 1000;i++){
//            MessageData messageData = new MessageData();
//            messageData.setGYFP2_TYPE_FLOAT32(java.util.Optional.of(28.5f));
//            messageData.setGYFP2_TYPE_BOOLEAN(java.util.Optional.of(true));
//            double d = 1 + Math.random() * (80 - 1);
//            messageData.setGYFP2_TYPE_FLOAT64(java.util.Optional.of(d));
//            list.add(messageData);
//        }

        MessageData messageData = new MessageData();
        messageData.setDataKey(1002);
        messageData.setDataType((byte) 0);
        messageData.setTs(new Date().getTime());
        messageData.setGYFP2_TYPE_BOOLEAN(java.util.Optional.of(false));
        list.add(messageData);

        MessageData messageData1 = new MessageData();
        messageData1.setDataKey(1002);
        messageData1.setDataType((byte) 11);
        messageData1.setTs(new Date().getTime());
        double d = 1 + Math.random() * (80 - 1);
        messageData1.setGYFP2_TYPE_FLOAT64(java.util.Optional.of(d));
        list.add(messageData1);
        gyMessage.setMessageDataList(list);
        Date sTdate = new Date();
        byte[] bytes = gyMessage.toBytes();
        Date eTdate = new Date();
        log.info("序列化时间"+(eTdate.getTime()-sTdate.getTime()));
        return bytes;
    }
}
