CoderSilence
2025-03-31 1d92915d0a858a43677a4c7d0d67795e043d2b43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.zhitan.influxdb;
 
import com.influxdb.LogLevel;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.write.Point;
import com.zhitan.config.influxdb.InfluxdbConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import java.util.List;
 
/**
 * influxdb的基础服务
 *
 * @author Silence
 * @version 1.0
 */
@Slf4j
@Repository
public class InfluxdbRepository {
 
    protected InfluxdbConfig config;
    protected InfluxDBClient client;
 
    @Autowired
    public InfluxdbRepository(InfluxdbConfig config) {
        this.config = config;
        init();
    }
 
    /**
     * 初始化
     */
    private void init() {
        if (config.isEnable()) {
            if (null == client) {
                client = InfluxDBClientFactory.create(config.getHost(), config.getToken().toCharArray(),
                                config.getOrg(), config.getBucket())
                        .enableGzip()
                        .setLogLevel(LogLevel.BASIC);
            }
            if (!client.ping()) {
                log.error("实时库连接失败");
            } else {
                log.info("实时库连接成功");
            }
        } else {
            log.debug("时序库不可用");
        }
    }
 
    /**
     * 写入单个点位
     */
    public void writePoint(Point point) {
        if (null == point) {
            return;
        }
        WriteApiBlocking writeApi = client.getWriteApiBlocking();
        writeApi.writePoint(point);
    }
 
    /**
     * 写入多个点位
     */
    public void writePoints(List<Point> points) {
        if (null == points || points.isEmpty()) {
            return;
        }
        WriteApiBlocking writeApi = client.getWriteApiBlocking();
        writeApi.writePoints(points);
    }
}