| | |
| | | import com.zhitan.model.entity.ElectricPower; |
| | | import com.zhitan.influxdb.InfluxdbRepository; |
| | | import com.zhitan.mapper.CommonMapper; |
| | | import com.zhitan.model.entity.KtDataEntity; |
| | | import com.zhitan.model.entity.PowerEntity; |
| | | import com.zhitan.redis.RedisCache; |
| | | import com.zhitan.service.IDataService; |
| | |
| | | repository.writePoints(points); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 写入KT数据 |
| | | * |
| | | * @param ktDataEntity 固定格式的数据 |
| | | */ |
| | | @Override |
| | | public void writeTimeSeriesData(KtDataEntity ktDataEntity) { |
| | | List<IndexTemplate> templates = getIndexTemplate(); |
| | | // 获取类中所有声明的字段 |
| | | Field[] fields = ktDataEntity.getClass().getDeclaredFields(); |
| | | List<Point> points = new ArrayList<>(); |
| | | for (Field field : fields) { |
| | | IndexTemplate indexTemplate = templates.stream().filter(template -> |
| | | field.getName().equalsIgnoreCase(template.getGatewayKey())) |
| | | .findFirst().orElse(null); |
| | | if (indexTemplate != null) { |
| | | Point point = Point |
| | | .measurement(influxdbConfig.getMeasurement()) |
| | | .addTag(TAG, ktDataEntity.getSn() + "_" + indexTemplate.getCode()) |
| | | .time(Instant.now(), WritePrecision.S); |
| | | field.setAccessible(true); |
| | | if (Number.class.isAssignableFrom(field.getType()) || field.getType().isPrimitive()) { |
| | | try { |
| | | // 获取字段值 |
| | | Object o = field.get(ktDataEntity); |
| | | if (o!=null) { |
| | | // 安全类型转换 |
| | | if (o instanceof Number) { |
| | | double value = ((Number) o).doubleValue(); |
| | | point.addField(FIELD_VALUE, value); |
| | | // 使用 value... |
| | | } else { |
| | | log.error("字段 {} 类型非法: {}", field.getName(), o.getClass()); |
| | | } |
| | | } |
| | | points.add(point); |
| | | } catch (IllegalAccessException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | repository.writePoints(points); |
| | | } |
| | | |
| | | /** |
| | | * 获取点位模板 |
| | | */ |