Search in sources :

Example 1 with EdgeEventEntity

use of org.thingsboard.server.dao.model.sql.EdgeEventEntity in project thingsboard by thingsboard.

the class JpaBaseEdgeEventDao method save.

@Override
public EdgeEvent save(EdgeEvent edgeEvent) {
    final Lock readWriteLock = readWriteLocks.computeIfAbsent(edgeEvent.getEdgeId(), id -> new ReentrantLock());
    readWriteLock.lock();
    try {
        log.debug("Save edge event [{}] ", edgeEvent);
        if (edgeEvent.getId() == null) {
            UUID timeBased = Uuids.timeBased();
            edgeEvent.setId(new EdgeEventId(timeBased));
            edgeEvent.setCreatedTime(Uuids.unixTimestamp(timeBased));
        } else if (edgeEvent.getCreatedTime() == 0L) {
            UUID eventId = edgeEvent.getId().getId();
            if (eventId.version() == 1) {
                edgeEvent.setCreatedTime(Uuids.unixTimestamp(eventId));
            } else {
                edgeEvent.setCreatedTime(System.currentTimeMillis());
            }
        }
        if (StringUtils.isEmpty(edgeEvent.getUid())) {
            edgeEvent.setUid(edgeEvent.getId().toString());
        }
        return save(new EdgeEventEntity(edgeEvent)).orElse(null);
    } finally {
        readWriteLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) EdgeEventId(org.thingsboard.server.common.data.id.EdgeEventId) EdgeEventEntity(org.thingsboard.server.dao.model.sql.EdgeEventEntity) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) UUID(java.util.UUID) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

UUID (java.util.UUID)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 EdgeEventId (org.thingsboard.server.common.data.id.EdgeEventId)1 NULL_UUID (org.thingsboard.server.dao.model.ModelConstants.NULL_UUID)1 EdgeEventEntity (org.thingsboard.server.dao.model.sql.EdgeEventEntity)1