use of org.thingsboard.server.common.data.device.data.PowerMode in project thingsboard by thingsboard.
the class DefaultTransportApiService method getDeviceInfoProto.
private DeviceInfoProto getDeviceInfoProto(Device device) throws JsonProcessingException {
DeviceInfoProto.Builder builder = DeviceInfoProto.newBuilder().setTenantIdMSB(device.getTenantId().getId().getMostSignificantBits()).setTenantIdLSB(device.getTenantId().getId().getLeastSignificantBits()).setCustomerIdMSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getMostSignificantBits()).orElse(0L)).setCustomerIdLSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getLeastSignificantBits()).orElse(0L)).setDeviceIdMSB(device.getId().getId().getMostSignificantBits()).setDeviceIdLSB(device.getId().getId().getLeastSignificantBits()).setDeviceName(device.getName()).setDeviceType(device.getType()).setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits()).setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits()).setAdditionalInfo(mapper.writeValueAsString(device.getAdditionalInfo()));
PowerSavingConfiguration psmConfiguration = null;
switch(device.getDeviceData().getTransportConfiguration().getType()) {
case LWM2M:
psmConfiguration = (Lwm2mDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
break;
case COAP:
psmConfiguration = (CoapDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
break;
}
if (psmConfiguration != null) {
PowerMode powerMode = psmConfiguration.getPowerMode();
if (powerMode != null) {
builder.setPowerMode(powerMode.name());
if (powerMode.equals(PowerMode.PSM)) {
builder.setPsmActivityTimer(checkLong(psmConfiguration.getPsmActivityTimer()));
} else if (powerMode.equals(PowerMode.E_DRX)) {
builder.setEdrxCycle(checkLong(psmConfiguration.getEdrxCycle()));
builder.setPagingTransmissionWindow(checkLong(psmConfiguration.getPagingTransmissionWindow()));
}
}
}
return builder.build();
}
use of org.thingsboard.server.common.data.device.data.PowerMode in project thingsboard by thingsboard.
the class DefaultCoapClientContext method onUplink.
private void onUplink(TbCoapClientState client, boolean notifyOtherServers, long uplinkTs) {
PowerMode powerMode = client.getPowerMode();
PowerSavingConfiguration profileSettings = null;
if (powerMode == null) {
var clientProfile = getProfile(client.getProfileId());
if (clientProfile.isPresent()) {
profileSettings = clientProfile.get().getClientSettings();
if (profileSettings != null) {
powerMode = profileSettings.getPowerMode();
}
}
}
if (powerMode == null || PowerMode.DRX.equals(powerMode)) {
client.updateLastUplinkTime(uplinkTs);
return;
}
client.lock();
try {
long uplinkTime = client.updateLastUplinkTime(uplinkTs);
long timeout = getTimeout(client, powerMode, profileSettings);
Future<Void> sleepTask = client.getSleepTask();
if (sleepTask != null) {
sleepTask.cancel(false);
}
Future<Void> task = transportContext.getScheduler().schedule(() -> {
if (uplinkTime == client.getLastUplinkTime()) {
asleep(client);
}
return null;
}, timeout, TimeUnit.MILLISECONDS);
client.setSleepTask(task);
if (notifyOtherServers && partitionService.countTransportsByType(DataConstants.COAP_TRANSPORT_NAME) > 1) {
transportService.notifyAboutUplink(getNewSyncSession(client), TransportProtos.UplinkNotificationMsg.newBuilder().setUplinkTs(uplinkTime).build(), TransportServiceCallback.EMPTY);
}
} finally {
client.unlock();
}
}
use of org.thingsboard.server.common.data.device.data.PowerMode in project thingsboard by thingsboard.
the class DefaultCoapClientContext method isDownlinkAllowed.
private boolean isDownlinkAllowed(TbCoapClientState client) {
PowerMode powerMode = client.getPowerMode();
PowerSavingConfiguration profileSettings = null;
if (powerMode == null) {
var clientProfile = getProfile(client.getProfileId());
if (clientProfile.isPresent()) {
profileSettings = clientProfile.get().getClientSettings();
if (profileSettings != null) {
powerMode = profileSettings.getPowerMode();
}
}
}
if (powerMode == null || PowerMode.DRX.equals(powerMode)) {
return true;
}
client.lock();
long timeSinceLastUplink = System.currentTimeMillis() - client.getLastUplinkTime();
try {
if (PowerMode.PSM.equals(powerMode)) {
Long psmActivityTimer = client.getPsmActivityTimer();
if (psmActivityTimer == null && profileSettings != null) {
psmActivityTimer = profileSettings.getPsmActivityTimer();
}
if (psmActivityTimer == null || psmActivityTimer == 0L) {
psmActivityTimer = config.getPsmActivityTimer();
}
return timeSinceLastUplink <= psmActivityTimer;
} else {
Long pagingTransmissionWindow = client.getPagingTransmissionWindow();
if (pagingTransmissionWindow == null && profileSettings != null) {
pagingTransmissionWindow = profileSettings.getPagingTransmissionWindow();
}
if (pagingTransmissionWindow == null || pagingTransmissionWindow == 0L) {
pagingTransmissionWindow = config.getPagingTransmissionWindow();
}
boolean allowed = timeSinceLastUplink <= pagingTransmissionWindow;
if (!allowed) {
return client.checkFirstDownlink();
} else {
return true;
}
}
} finally {
client.unlock();
}
}
use of org.thingsboard.server.common.data.device.data.PowerMode in project thingsboard by thingsboard.
the class DefaultCoapClientContext method compareAndSetSleepFlag.
private boolean compareAndSetSleepFlag(TbCoapClientState client, boolean sleeping) {
if (sleeping == client.isAsleep()) {
log.trace("[{}] Client is already at sleeping: {}, ignoring event: {}", client.getDeviceId(), client.isAsleep(), sleeping);
return false;
}
client.lock();
try {
if (sleeping == client.isAsleep()) {
log.trace("[{}] Client is already at sleeping: {}, ignoring event: {}", client.getDeviceId(), client.isAsleep(), sleeping);
return false;
} else {
PowerMode powerMode = getPowerMode(client);
if (PowerMode.PSM.equals(powerMode) || PowerMode.E_DRX.equals(powerMode)) {
log.trace("[{}] Switch sleeping from: {} to: {}", client.getDeviceId(), client.isAsleep(), sleeping);
client.setAsleep(sleeping);
// update(client);
return true;
} else {
return false;
}
}
} finally {
client.unlock();
}
}
use of org.thingsboard.server.common.data.device.data.PowerMode in project thingsboard by thingsboard.
the class LwM2mClientContextImpl method onUplink.
@Override
public void onUplink(LwM2mClient client) {
PowerMode powerMode = client.getPowerMode();
OtherConfiguration profileSettings = null;
if (powerMode == null) {
var clientProfile = getProfile(client.getProfileId());
profileSettings = clientProfile.getClientLwM2mSettings();
powerMode = profileSettings.getPowerMode();
if (powerMode == null) {
powerMode = PowerMode.DRX;
}
}
if (PowerMode.DRX.equals(powerMode)) {
client.updateLastUplinkTime();
return;
}
client.lock();
try {
long uplinkTime = client.updateLastUplinkTime();
long timeout;
if (PowerMode.PSM.equals(powerMode)) {
Long psmActivityTimer = client.getPsmActivityTimer();
if (psmActivityTimer == null && profileSettings != null) {
psmActivityTimer = profileSettings.getPsmActivityTimer();
}
if (psmActivityTimer == null || psmActivityTimer == 0L) {
psmActivityTimer = config.getPsmActivityTimer();
}
timeout = psmActivityTimer;
} else {
Long pagingTransmissionWindow = client.getPagingTransmissionWindow();
if (pagingTransmissionWindow == null && profileSettings != null) {
pagingTransmissionWindow = profileSettings.getPagingTransmissionWindow();
}
if (pagingTransmissionWindow == null || pagingTransmissionWindow == 0L) {
pagingTransmissionWindow = config.getPagingTransmissionWindow();
}
timeout = pagingTransmissionWindow;
}
Future<Void> sleepTask = client.getSleepTask();
if (sleepTask != null) {
sleepTask.cancel(false);
}
Future<Void> task = context.getScheduler().schedule(() -> {
if (uplinkTime == client.getLastUplinkTime() && !otaUpdateService.isOtaDownloading(client)) {
asleep(client);
}
return null;
}, timeout, TimeUnit.MILLISECONDS);
client.setSleepTask(task);
} finally {
client.unlock();
}
}
Aggregations