Search in sources :

Example 1 with AlarmDataCmd

use of org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataCmd in project thingsboard by thingsboard.

the class DefaultTbEntityDataSubscriptionService method handleCmd.

@Override
public void handleCmd(TelemetryWebSocketSessionRef session, AlarmDataCmd cmd) {
    TbAlarmDataSubCtx ctx = getSubCtx(session.getSessionId(), cmd.getCmdId());
    if (ctx == null) {
        log.debug("[{}][{}] Creating new alarm subscription using: {}", session.getSessionId(), cmd.getCmdId(), cmd);
        ctx = createSubCtx(session, cmd);
    }
    ctx.setAndResolveQuery(cmd.getQuery());
    AlarmDataQuery adq = ctx.getQuery();
    long start = System.currentTimeMillis();
    ctx.fetchData();
    long end = System.currentTimeMillis();
    stats.getRegularQueryInvocationCnt().incrementAndGet();
    stats.getRegularQueryTimeSpent().addAndGet(end - start);
    List<EntityData> entities = ctx.getEntitiesData();
    ctx.cancelTasks();
    ctx.clearEntitySubscriptions();
    if (entities.isEmpty()) {
        AlarmDataUpdate update = new AlarmDataUpdate(cmd.getCmdId(), new PageData<>(), null, 0, 0);
        wsService.sendWsMsg(ctx.getSessionId(), update);
    } else {
        ctx.fetchAlarms();
        ctx.createLatestValuesSubscriptions(cmd.getQuery().getLatestValues());
        if (adq.getPageLink().getTimeWindow() > 0) {
            TbAlarmDataSubCtx finalCtx = ctx;
            ScheduledFuture<?> task = scheduler.scheduleWithFixedDelay(finalCtx::checkAndResetInvocationCounter, dynamicPageLinkRefreshInterval, dynamicPageLinkRefreshInterval, TimeUnit.SECONDS);
            finalCtx.setRefreshTask(task);
        }
    }
}
Also used : AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) EntityData(org.thingsboard.server.common.data.query.EntityData) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery)

Aggregations

AlarmDataQuery (org.thingsboard.server.common.data.query.AlarmDataQuery)1 EntityData (org.thingsboard.server.common.data.query.EntityData)1 AlarmDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate)1