Search in sources :

Example 1 with AlarmDataUpdate

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

the class TbAlarmDataSubCtx method sendWsMsg.

private void sendWsMsg(String sessionId, AlarmSubscriptionUpdate subscriptionUpdate) {
    Alarm alarm = subscriptionUpdate.getAlarm();
    AlarmId alarmId = alarm.getId();
    if (subscriptionUpdate.isAlarmDeleted()) {
        Alarm deleted = alarmsMap.remove(alarmId);
        if (deleted != null) {
            fetchAlarms();
        }
    } else {
        AlarmData current = alarmsMap.get(alarmId);
        boolean onCurrentPage = current != null;
        boolean matchesFilter = filter(alarm);
        if (onCurrentPage) {
            if (matchesFilter) {
                AlarmData updated = new AlarmData(alarm, current.getOriginatorName(), current.getEntityId());
                updated.getLatest().putAll(current.getLatest());
                alarmsMap.put(alarmId, updated);
                wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, Collections.singletonList(updated), maxEntitiesPerAlarmSubscription, data.getTotalElements()));
            } else {
                fetchAlarms();
            }
        } else if (matchesFilter && query.getPageLink().getPage() == 0) {
            fetchAlarms();
        }
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 2 with AlarmDataUpdate

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

the class TbAlarmDataSubCtx method doFetchAlarms.

private void doFetchAlarms() {
    AlarmDataUpdate update;
    if (!entitiesMap.isEmpty()) {
        long start = System.currentTimeMillis();
        PageData<AlarmData> alarms = alarmService.findAlarmDataByQueryForEntities(getTenantId(), query, getOrderedEntityIds());
        long end = System.currentTimeMillis();
        stats.getAlarmQueryInvocationCnt().incrementAndGet();
        stats.getAlarmQueryTimeSpent().addAndGet(end - start);
        alarms = setAndMergeAlarmsData(alarms);
        update = new AlarmDataUpdate(cmdId, alarms, null, maxEntitiesPerAlarmSubscription, data.getTotalElements());
    } else {
        update = new AlarmDataUpdate(cmdId, new PageData<>(), null, maxEntitiesPerAlarmSubscription, data.getTotalElements());
    }
    wsService.sendWsMsg(getSessionId(), update);
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 3 with AlarmDataUpdate

use of org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate 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)

Example 4 with AlarmDataUpdate

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

the class TbAlarmDataSubCtx method sendWsMsg.

@Override
void sendWsMsg(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType, boolean resultToLatestValues) {
    EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
    if (entityId != null) {
        Map<String, TsValue> latestUpdate = new HashMap<>();
        subscriptionUpdate.getData().forEach((k, v) -> {
            Object[] data = (Object[]) v.get(0);
            latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
        });
        EntityData entityData = entitiesMap.get(entityId);
        entityData.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
        log.trace("[{}][{}][{}][{}] Received subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
        List<AlarmData> update = alarmsMap.values().stream().filter(alarm -> entityId.equals(alarm.getEntityId())).map(alarm -> {
            alarm.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
            return alarm;
        }).collect(Collectors.toList());
        if (!update.isEmpty()) {
            wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, update, maxEntitiesPerAlarmSubscription, data.getTotalElements()));
        }
    } else {
        log.trace("[{}][{}][{}][{}] Received stale subscription update: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), keyType, subscriptionUpdate);
    }
}
Also used : TsValue(org.thingsboard.server.common.data.query.TsValue) Setter(lombok.Setter) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Getter(lombok.Getter) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) TsValue(org.thingsboard.server.common.data.query.TsValue) AlarmSubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) Map(java.util.Map) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityService(org.thingsboard.server.dao.entity.EntityService) EntityKey(org.thingsboard.server.common.data.query.EntityKey) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Collection(java.util.Collection) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) Set(java.util.Set) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) Collectors(java.util.stream.Collectors) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AlarmData(org.thingsboard.server.common.data.query.AlarmData) AlarmDataQuery(org.thingsboard.server.common.data.query.AlarmDataQuery) AlarmId(org.thingsboard.server.common.data.id.AlarmId) PageData(org.thingsboard.server.common.data.page.PageData) TelemetrySubscriptionUpdate(org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate) TelemetryWebSocketService(org.thingsboard.server.service.telemetry.TelemetryWebSocketService) Collections(java.util.Collections) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) EntityData(org.thingsboard.server.common.data.query.EntityData) ToString(lombok.ToString) EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Aggregations

AlarmDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate)4 AlarmData (org.thingsboard.server.common.data.query.AlarmData)3 Alarm (org.thingsboard.server.common.data.alarm.Alarm)2 AlarmId (org.thingsboard.server.common.data.id.AlarmId)2 PageData (org.thingsboard.server.common.data.page.PageData)2 AlarmDataQuery (org.thingsboard.server.common.data.query.AlarmDataQuery)2 EntityData (org.thingsboard.server.common.data.query.EntityData)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1