编程语言
首页 > 编程语言> > Thingsboard源码分析(三)遥测数据获取

Thingsboard源码分析(三)遥测数据获取

作者:互联网

获取遥测数据

TelemetryController

AccessValidator

TimeseriesService

@Service
@Slf4j
public class BaseTimeseriesService implements TimeseriesService {
    
    // 这个组件负责查找所有最新遥测数据
    @Autowired
    private TimeseriesLatestDao timeseriesLatestDao;
    
    @Override
    public ListenableFuture<List<TsKvEntry>> findLatest(TenantId tenantId, EntityId entityId, Collection<String> keys) {
        validate(entityId);
        List<ListenableFuture<TsKvEntry>> futures = Lists.newArrayListWithExpectedSize(keys.size());
        keys.forEach(key -> Validator.validateString(key, "Incorrect key " + key));
        keys.forEach(key -> futures.add(timeseriesLatestDao.findLatest(tenantId, entityId, key)));
        return Futures.allAsList(futures);
    }

    @Override
    public ListenableFuture<List<TsKvEntry>> findAllLatest(TenantId tenantId, EntityId entityId) {
        //校验当前deviceId是否合法
        validate(entityId);
        // 根据tenantId和deviceId返回结果
        return timeseriesLatestDao.findAllLatest(tenantId, entityId);
    }
}

TimeseriesLatestDao

@Slf4j
@Component
@SqlTsLatestAnyDaopublic 
class SqlTimeseriesLatestDao extends BaseAbstractSqlTimeseriesDao implements TimeseriesLatestDao {    		@Autowired    
    private SearchTsKvLatestRepository searchTsKvLatestRepository;
    @Override    
    public ListenableFuture<List<TsKvEntry>> findAllLatest(TenantId tenantId, EntityId entityId) {
        return getFindAllLatestFuture(entityId);    
    }        
    protected ListenableFuture<List<TsKvEntry>> getFindAllLatestFuture(EntityId entityId) {        
        return Futures.immediateFuture(DaoUtil.convertDataList(Lists.newArrayList(
            searchTsKvLatestRepository.findAllByEntityId(entityId.getId()))));
    }
}

SearchTsKvLatestDao

@SqlTsLatestAnyDao@Repositorypublic class SearchTsKvLatestRepository {
    public static final String FIND_ALL_BY_ENTITY_ID = "findAllByEntityId"; 
    public static final String FIND_ALL_BY_ENTITY_ID_QUERY = "SELECT ts_kv_latest.entity_id AS entityId, ts_kv_latest.key AS key, ts_kv_dictionary.key AS strKey, ts_kv_latest.str_v AS strValue," +            " ts_kv_latest.bool_v AS boolValue, ts_kv_latest.long_v AS longValue, ts_kv_latest.dbl_v AS doubleValue, ts_kv_latest.json_v AS jsonValue, ts_kv_latest.ts AS ts FROM ts_kv_latest " +            "INNER JOIN ts_kv_dictionary ON ts_kv_latest.key = ts_kv_dictionary.key_id WHERE ts_kv_latest.entity_id = cast(:id AS uuid)";    
    @PersistenceContext   
    private EntityManager entityManager;   
    public List<TsKvLatestEntity> findAllByEntityId(UUID entityId) { 
        return entityManager.createNamedQuery(FIND_ALL_BY_ENTITY_ID, 
               TsKvLatestEntity.class.setParameter("id",entityId).getResultList());    
    }
}

EntityManager

Futures

标签:currentUser,return,遥测,ts,public,源码,EntityId,Thingsboard,entityId
来源: https://blog.csdn.net/qq_46331393/article/details/123086108