| | |
| | | import com.influxdb.client.WriteApiBlocking; |
| | | import com.influxdb.client.domain.HealthCheck; |
| | | import com.influxdb.client.domain.WritePrecision; |
| | | import com.influxdb.client.write.Point; |
| | | import com.zhitan.engine.enums.CollectionModes; |
| | | import com.zhitan.engine.enums.GroupTimeType; |
| | | import com.zhitan.engine.enums.Quality; |
| | |
| | | write.writeMeasurements(WritePrecision.MS, writePoints); |
| | | } |
| | | |
| | | /** |
| | | * 写入单个点位 |
| | | */ |
| | | public void writePoint(Point point) { |
| | | if (null == point) { |
| | | return; |
| | | } |
| | | WriteApiBlocking writeApi = client.getWriteApiBlocking(); |
| | | writeApi.writePoint(point); |
| | | } |
| | | public TagValue query(String tagCode, Date time) { |
| | | List<TagValue> values = query(Collections.singletonList(tagCode), time); |
| | | return !values.isEmpty() ? values.get(0) : new TagValue(); |
| | |
| | | config.getMeasurement(), field, startTime, endTime); |
| | | return 0.0; |
| | | } |
| | | if (field.contains("total_A")) { |
| | | System.out.println(values); |
| | | } |
| | | |
| | | // 计算累计值(最后一个值减去第一个值) |
| | | Object firstValue = values.get(0); |
| | |
| | | return 0.0; |
| | | } |
| | | } |
| | | |
| | | public double getLastPoint(String measurement, String tag, String s) { |
| | | if (client == null || !config.isEnable()) { |
| | | log.warn("InfluxDB client is not initialized or disabled."); |
| | | return 0; |
| | | } |
| | | |
| | | String query = String.format("from(bucket: \"%s\") " + |
| | | "|> range(start: -1h) " + |
| | | "|> filter(fn: (r) => r._measurement == \"%s\" and r._field == \"value\") " + |
| | | "|> filter(fn: (r) => r.tag == \"%s\") " + |
| | | "|> last()", config.getBucket(), measurement, s); |
| | | |
| | | List<FluxTable> tables = client.getQueryApi().query(query, config.getOrg()); |
| | | |
| | | if (tables != null && !tables.isEmpty()) { |
| | | List<FluxRecord> records = tables.get(0).getRecords(); |
| | | if (records != null && !records.isEmpty()) { |
| | | FluxRecord record = records.get(0); |
| | | double value = (double) record.getValue(); |
| | | |
| | | return value; |
| | | } |
| | | } |
| | | |
| | | log.warn("No data found for measurement: {}, tag: {}, field: {}", measurement, tag, s); |
| | | return 0; |
| | | } |
| | | } |