package com.muyu.carData.listener;

import com.alibaba.fastjson.JSONObject;
import com.muyu.carData.config.lotdbconfig.IotDBSessionConfig;
import com.muyu.carData.event.EsSaveEvent;
import com.muyu.carData.event.EventListener;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

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

/** 数据持久化事件
 * @Author：张腾
 * @Package：com.muyu.carData.listener
 * @Project：cloud-server-8
 * @name：InsertIotDBListener
 * @Date：2024/10/6 9:25
 */
@Log4j2
@Component
public class InsertIotDBListener implements EventListener {
    @Override
    public void onEvent(EsSaveEvent event) {
        JSONObject jsonObject = event.getData();
        log.info("持久化:监听到数据：{}", jsonObject);
        List<String> keys = new ArrayList<>();
        List<String> values = new ArrayList<>();

        jsonObject.forEach((key,value) -> {
            keys.add(key);
            values.add((String) value);
        });

        IotDBSessionConfig iotDBSessionConfig = new IotDBSessionConfig();
        long time = System.currentTimeMillis();
        iotDBSessionConfig.execute("root.vehicle",time,keys,values);
        log.info("数据写入成功");
    }

    @Override
    public void onApplicationEvent(EsSaveEvent event) {
        onEvent(event);
    }
}
