150 changed files with 3188 additions and 1432 deletions
@ -0,0 +1,37 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.config.mqtt; |
|||
|
|||
import jakarta.validation.constraints.PositiveOrZero; |
|||
import lombok.Data; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.validation.annotation.Validated; |
|||
|
|||
@Data |
|||
@Validated |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "mqtt.client.retransmission") |
|||
public class MqttClientRetransmissionSettingsComponent { |
|||
|
|||
@PositiveOrZero |
|||
private int maxAttempts; |
|||
@PositiveOrZero |
|||
private long initialDelayMillis; |
|||
@PositiveOrZero |
|||
private double jitterFactor; |
|||
|
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.config.mqtt; |
|||
|
|||
import lombok.EqualsAndHashCode; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.ToString; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.thingsboard.rule.engine.api.MqttClientSettings; |
|||
|
|||
@ToString |
|||
@EqualsAndHashCode |
|||
@Configuration |
|||
@RequiredArgsConstructor |
|||
public class MqttClientSettingsComponent implements MqttClientSettings { |
|||
|
|||
private final MqttClientRetransmissionSettingsComponent retransmissionSettingsComponent; |
|||
|
|||
@Override |
|||
public int getRetransmissionMaxAttempts() { |
|||
return retransmissionSettingsComponent.getMaxAttempts(); |
|||
} |
|||
|
|||
@Override |
|||
public long getRetransmissionInitialDelayMillis() { |
|||
return retransmissionSettingsComponent.getInitialDelayMillis(); |
|||
} |
|||
|
|||
@Override |
|||
public double getRetransmissionJitterFactor() { |
|||
return retransmissionSettingsComponent.getJitterFactor(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.controller; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.security.access.prepost.PreAuthorize; |
|||
import org.springframework.security.core.annotation.AuthenticationPrincipal; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
|||
import org.thingsboard.server.config.annotations.ApiOperation; |
|||
import org.thingsboard.server.dao.trendz.TrendzSettingsService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
import org.thingsboard.server.service.security.permission.Operation; |
|||
import org.thingsboard.server.service.security.permission.Resource; |
|||
|
|||
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_END; |
|||
import static org.thingsboard.server.controller.ControllerConstants.MARKDOWN_CODE_BLOCK_START; |
|||
import static org.thingsboard.server.controller.ControllerConstants.NEW_LINE; |
|||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_AUTHORITY_PARAGRAPH; |
|||
import static org.thingsboard.server.controller.ControllerConstants.TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH; |
|||
|
|||
@RestController |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
@RequestMapping("/api") |
|||
public class TrendzController extends BaseController { |
|||
|
|||
private final TrendzSettingsService trendzSettingsService; |
|||
|
|||
@ApiOperation(value = "Save Trendz settings (saveTrendzSettings)", |
|||
notes = "Saves Trendz settings for this tenant.\n" + NEW_LINE + |
|||
"Here is an example of the Trendz settings:\n" + |
|||
MARKDOWN_CODE_BLOCK_START + |
|||
"{\n" + |
|||
" \"enabled\": true,\n" + |
|||
" \"baseUrl\": \"https://some.domain.com:18888/also_necessary_prefix\"\n" + |
|||
"}" + |
|||
MARKDOWN_CODE_BLOCK_END + |
|||
TENANT_AUTHORITY_PARAGRAPH) |
|||
@PostMapping("/trendz/settings") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") |
|||
public TrendzSettings saveTrendzSettings(@RequestBody TrendzSettings trendzSettings, |
|||
@AuthenticationPrincipal SecurityUser user) throws ThingsboardException { |
|||
accessControlService.checkPermission(user, Resource.ADMIN_SETTINGS, Operation.WRITE); |
|||
TenantId tenantId = user.getTenantId(); |
|||
trendzSettingsService.saveTrendzSettings(tenantId, trendzSettings); |
|||
return trendzSettings; |
|||
} |
|||
|
|||
@ApiOperation(value = "Get Trendz Settings (getTrendzSettings)", |
|||
notes = "Retrieves Trendz settings for this tenant." + |
|||
TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH) |
|||
@GetMapping("/trendz/settings") |
|||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
|||
public TrendzSettings getTrendzSettings(@AuthenticationPrincipal SecurityUser user) { |
|||
TenantId tenantId = user.getTenantId(); |
|||
return trendzSettingsService.findTrendzSettings(tenantId); |
|||
} |
|||
|
|||
} |
|||
File diff suppressed because one or more lines are too long
@ -0,0 +1,77 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.controller; |
|||
|
|||
import org.junit.Before; |
|||
import org.junit.Test; |
|||
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
public class TrendzControllerTest extends AbstractControllerTest { |
|||
|
|||
private final String trendzUrl = "https://some.domain.com:18888/also_necessary_prefix"; |
|||
|
|||
@Before |
|||
public void setUp() throws Exception { |
|||
loginTenantAdmin(); |
|||
|
|||
TrendzSettings trendzSettings = new TrendzSettings(); |
|||
trendzSettings.setEnabled(true); |
|||
trendzSettings.setBaseUrl(trendzUrl); |
|||
|
|||
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk()); |
|||
} |
|||
|
|||
@Test |
|||
public void testTrendzSettingsWhenTenant() throws Exception { |
|||
loginTenantAdmin(); |
|||
|
|||
TrendzSettings trendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
|||
|
|||
assertThat(trendzSettings).isNotNull(); |
|||
assertThat(trendzSettings.isEnabled()).isTrue(); |
|||
assertThat(trendzSettings.getBaseUrl()).isEqualTo(trendzUrl); |
|||
|
|||
String updatedUrl = "https://some.domain.com:18888/tenant_trendz"; |
|||
trendzSettings.setBaseUrl(updatedUrl); |
|||
|
|||
doPost("/api/trendz/settings", trendzSettings).andExpect(status().isOk()); |
|||
|
|||
TrendzSettings updatedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
|||
assertThat(updatedTrendzSettings).isEqualTo(trendzSettings); |
|||
} |
|||
|
|||
@Test |
|||
public void testTrendzSettingsWhenCustomer() throws Exception { |
|||
loginCustomerUser(); |
|||
|
|||
TrendzSettings newTrendzSettings = new TrendzSettings(); |
|||
newTrendzSettings.setEnabled(true); |
|||
newTrendzSettings.setBaseUrl("https://some.domain.com:18888/customer_trendz"); |
|||
|
|||
doPost("/api/trendz/settings", newTrendzSettings).andExpect(status().isForbidden()); |
|||
|
|||
TrendzSettings fetchedTrendzSettings = doGet("/api/trendz/settings", TrendzSettings.class); |
|||
assertThat(fetchedTrendzSettings).isNotNull(); |
|||
assertThat(fetchedTrendzSettings.isEnabled()).isTrue(); |
|||
assertThat(fetchedTrendzSettings.getBaseUrl()).isEqualTo(trendzUrl); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.trendz; |
|||
|
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
|||
|
|||
public interface TrendzSettingsService { |
|||
|
|||
void saveTrendzSettings(TenantId tenantId, TrendzSettings settings); |
|||
|
|||
TrendzSettings findTrendzSettings(TenantId tenantId); |
|||
|
|||
void deleteTrendzSettings(TenantId tenantId); |
|||
|
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.trendz; |
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class TrendzSettings { |
|||
|
|||
private boolean enabled; |
|||
private String baseUrl; |
|||
|
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.dao.trendz; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.cache.annotation.CacheEvict; |
|||
import org.springframework.cache.annotation.Cacheable; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.AdminSettings; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.trendz.TrendzSettings; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
|
|||
import java.util.Optional; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
@Slf4j |
|||
public class DefaultTrendzSettingsService implements TrendzSettingsService { |
|||
|
|||
private final AdminSettingsService adminSettingsService; |
|||
|
|||
private static final String SETTINGS_KEY = "trendz"; |
|||
|
|||
@CacheEvict(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
|||
@Override |
|||
public void saveTrendzSettings(TenantId tenantId, TrendzSettings settings) { |
|||
AdminSettings adminSettings = Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
|||
.orElseGet(() -> { |
|||
AdminSettings newAdminSettings = new AdminSettings(); |
|||
newAdminSettings.setTenantId(tenantId); |
|||
newAdminSettings.setKey(SETTINGS_KEY); |
|||
return newAdminSettings; |
|||
}); |
|||
adminSettings.setJsonValue(JacksonUtil.valueToTree(settings)); |
|||
adminSettingsService.saveAdminSettings(tenantId, adminSettings); |
|||
} |
|||
|
|||
@Cacheable(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
|||
@Override |
|||
public TrendzSettings findTrendzSettings(TenantId tenantId) { |
|||
return Optional.ofNullable(adminSettingsService.findAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY)) |
|||
.map(adminSettings -> JacksonUtil.treeToValue(adminSettings.getJsonValue(), TrendzSettings.class)) |
|||
.orElseGet(TrendzSettings::new); |
|||
} |
|||
|
|||
@CacheEvict(cacheNames = CacheConstants.TRENDZ_SETTINGS_CACHE, key = "#tenantId") |
|||
@Override |
|||
public void deleteTrendzSettings(TenantId tenantId) { |
|||
adminSettingsService.deleteAdminSettingsByTenantIdAndKey(tenantId, SETTINGS_KEY); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,252 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.monitoring.service; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang3.RandomStringUtils; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.common.util.RegexUtils; |
|||
import org.thingsboard.monitoring.client.TbClient; |
|||
import org.thingsboard.monitoring.config.transport.DeviceConfig; |
|||
import org.thingsboard.monitoring.config.transport.TransportMonitoringConfig; |
|||
import org.thingsboard.monitoring.config.transport.TransportMonitoringTarget; |
|||
import org.thingsboard.monitoring.config.transport.TransportType; |
|||
import org.thingsboard.monitoring.util.ResourceUtils; |
|||
import org.thingsboard.server.common.data.DataConstants; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.DeviceProfile; |
|||
import org.thingsboard.server.common.data.DeviceProfileType; |
|||
import org.thingsboard.server.common.data.DeviceTransportType; |
|||
import org.thingsboard.server.common.data.TbResource; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.cf.CalculatedField; |
|||
import org.thingsboard.server.common.data.cf.CalculatedFieldType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Argument; |
|||
import org.thingsboard.server.common.data.cf.configuration.ArgumentType; |
|||
import org.thingsboard.server.common.data.cf.configuration.Output; |
|||
import org.thingsboard.server.common.data.cf.configuration.OutputType; |
|||
import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; |
|||
import org.thingsboard.server.common.data.cf.configuration.ScriptCalculatedFieldConfiguration; |
|||
import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MBootstrapClientCredentials; |
|||
import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials; |
|||
import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecBootstrapClientCredential; |
|||
import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredential; |
|||
import org.thingsboard.server.common.data.device.data.DefaultDeviceConfiguration; |
|||
import org.thingsboard.server.common.data.device.data.DefaultDeviceTransportConfiguration; |
|||
import org.thingsboard.server.common.data.device.data.DeviceData; |
|||
import org.thingsboard.server.common.data.device.data.Lwm2mDeviceTransportConfiguration; |
|||
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration; |
|||
import org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration; |
|||
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.kv.KvEntry; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.common.data.rule.RuleChain; |
|||
import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
|||
import org.thingsboard.server.common.data.rule.RuleChainType; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|||
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.stream.Collectors; |
|||
|
|||
import static org.thingsboard.monitoring.service.BaseHealthChecker.TEST_CF_TELEMETRY_KEY; |
|||
import static org.thingsboard.monitoring.service.BaseHealthChecker.TEST_TELEMETRY_KEY; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class MonitoringEntityService { |
|||
|
|||
private final TbClient tbClient; |
|||
|
|||
@Value("${monitoring.calculated_fields.enabled:true}") |
|||
private boolean calculatedFieldsMonitoringEnabled; |
|||
|
|||
public void checkEntities() { |
|||
RuleChain ruleChain = tbClient.getRuleChains(RuleChainType.CORE, new PageLink(10)).getData().stream() |
|||
.filter(RuleChain::isRoot) |
|||
.findFirst().orElseThrow(); |
|||
RuleChainId ruleChainId = ruleChain.getId(); |
|||
|
|||
JsonNode ruleChainDescriptor = ResourceUtils.getResource("rule_chain.json"); |
|||
List<String> attributeKeys = tbClient.getAttributeKeys(ruleChainId); |
|||
Map<String, String> attributes = tbClient.getAttributeKvEntries(ruleChainId, attributeKeys).stream() |
|||
.collect(Collectors.toMap(KvEntry::getKey, KvEntry::getValueAsString)); |
|||
|
|||
int currentVersion = Integer.parseInt(attributes.getOrDefault("version", "0")); |
|||
int newVersion = ruleChainDescriptor.get("version").asInt(); |
|||
if (currentVersion == newVersion) { |
|||
log.info("Not updating rule chain, version is the same ({})", currentVersion); |
|||
return; |
|||
} else { |
|||
log.info("Updating rule chain '{}' from version {} to {}", ruleChain.getName(), currentVersion, newVersion); |
|||
} |
|||
|
|||
String metadataJson = RegexUtils.replace(ruleChainDescriptor.get("metadata").toString(), |
|||
"\\$\\{MONITORING:(.+?)}", matchResult -> { |
|||
String key = matchResult.group(1); |
|||
String value = attributes.get(key); |
|||
if (value == null) { |
|||
throw new IllegalArgumentException("No attribute found for key " + key); |
|||
} |
|||
log.info("Using {}: {}", key, value); |
|||
return value; |
|||
}); |
|||
RuleChainMetaData metaData = JacksonUtil.fromString(metadataJson, RuleChainMetaData.class); |
|||
metaData.setRuleChainId(ruleChainId); |
|||
tbClient.saveRuleChainMetaData(metaData); |
|||
tbClient.saveEntityAttributesV2(ruleChainId, DataConstants.SERVER_SCOPE, JacksonUtil.newObjectNode() |
|||
.put("version", newVersion)); |
|||
} |
|||
|
|||
public Asset getOrCreateMonitoringAsset() { |
|||
String assetName = "[Monitoring] Latencies"; |
|||
return tbClient.findAsset(assetName).orElseGet(() -> { |
|||
Asset asset = new Asset(); |
|||
asset.setType("Monitoring"); |
|||
asset.setName(assetName); |
|||
asset = tbClient.saveAsset(asset); |
|||
log.info("Created monitoring asset {}", asset.getId()); |
|||
return asset; |
|||
}); |
|||
} |
|||
|
|||
public void checkEntities(TransportMonitoringConfig config, TransportMonitoringTarget target) { |
|||
Device device = getOrCreateDevice(config, target); |
|||
DeviceCredentials credentials = tbClient.getDeviceCredentialsByDeviceId(device.getId()) |
|||
.orElseThrow(() -> new IllegalArgumentException("No credentials found for device " + device.getId())); |
|||
|
|||
DeviceConfig deviceConfig = new DeviceConfig(); |
|||
deviceConfig.setId(device.getId().toString()); |
|||
deviceConfig.setName(device.getName()); |
|||
deviceConfig.setCredentials(credentials); |
|||
target.setDevice(deviceConfig); |
|||
} |
|||
|
|||
private Device getOrCreateDevice(TransportMonitoringConfig config, TransportMonitoringTarget target) { |
|||
TransportType transportType = config.getTransportType(); |
|||
String deviceName = String.format("%s %s (%s) - %s", target.getNamePrefix(), transportType.getName(), target.getQueue(), target.getBaseUrl()).trim(); |
|||
Device device = tbClient.getTenantDevice(deviceName).orElse(null); |
|||
if (device != null) { |
|||
if (calculatedFieldsMonitoringEnabled) { |
|||
CalculatedField calculatedField = tbClient.getCalculatedFieldsByEntityId(device.getId(), new PageLink(1, 0, TEST_CF_TELEMETRY_KEY)) |
|||
.getData().stream().findFirst().orElse(null); |
|||
if (calculatedField == null) { |
|||
createCalculatedField(device); |
|||
} |
|||
} |
|||
return device; |
|||
} |
|||
|
|||
log.info("Creating new device '{}'", deviceName); |
|||
device = new Device(); |
|||
device.setName(deviceName); |
|||
|
|||
DeviceCredentials credentials = new DeviceCredentials(); |
|||
credentials.setCredentialsId(RandomStringUtils.randomAlphabetic(20)); |
|||
DeviceData deviceData = new DeviceData(); |
|||
deviceData.setConfiguration(new DefaultDeviceConfiguration()); |
|||
|
|||
DeviceProfile deviceProfile = getOrCreateDeviceProfile(config, target); |
|||
device.setType(deviceProfile.getName()); |
|||
device.setDeviceProfileId(deviceProfile.getId()); |
|||
|
|||
if (transportType != TransportType.LWM2M) { |
|||
deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration()); |
|||
credentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN); |
|||
} else { |
|||
deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration()); |
|||
credentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS); |
|||
LwM2MDeviceCredentials lwm2mCreds = new LwM2MDeviceCredentials(); |
|||
NoSecClientCredential client = new NoSecClientCredential(); |
|||
client.setEndpoint(credentials.getCredentialsId()); |
|||
lwm2mCreds.setClient(client); |
|||
LwM2MBootstrapClientCredentials bootstrap = new LwM2MBootstrapClientCredentials(); |
|||
bootstrap.setBootstrapServer(new NoSecBootstrapClientCredential()); |
|||
bootstrap.setLwm2mServer(new NoSecBootstrapClientCredential()); |
|||
lwm2mCreds.setBootstrap(bootstrap); |
|||
credentials.setCredentialsValue(JacksonUtil.toString(lwm2mCreds)); |
|||
} |
|||
|
|||
device = tbClient.saveDeviceWithCredentials(device, credentials).get(); |
|||
if (calculatedFieldsMonitoringEnabled) { |
|||
createCalculatedField(device); |
|||
} |
|||
return device; |
|||
} |
|||
|
|||
private DeviceProfile getOrCreateDeviceProfile(TransportMonitoringConfig config, TransportMonitoringTarget target) { |
|||
TransportType transportType = config.getTransportType(); |
|||
String profileName = String.format("%s %s (%s)", target.getNamePrefix(), transportType.getName(), target.getQueue()).trim(); |
|||
DeviceProfile deviceProfile = tbClient.getDeviceProfiles(new PageLink(1, 0, profileName)).getData() |
|||
.stream().findFirst().orElse(null); |
|||
if (deviceProfile != null) { |
|||
return deviceProfile; |
|||
} |
|||
|
|||
log.info("Creating new device profile '{}'", profileName); |
|||
if (transportType != TransportType.LWM2M) { |
|||
deviceProfile = new DeviceProfile(); |
|||
deviceProfile.setType(DeviceProfileType.DEFAULT); |
|||
deviceProfile.setTransportType(DeviceTransportType.DEFAULT); |
|||
DeviceProfileData profileData = new DeviceProfileData(); |
|||
profileData.setConfiguration(new DefaultDeviceProfileConfiguration()); |
|||
profileData.setTransportConfiguration(new DefaultDeviceProfileTransportConfiguration()); |
|||
deviceProfile.setProfileData(profileData); |
|||
} else { |
|||
tbClient.getResources(new PageLink(1, 0, "LwM2M Monitoring")).getData() |
|||
.stream().findFirst() |
|||
.orElseGet(() -> { |
|||
TbResource newResource = ResourceUtils.getResource("lwm2m/resource.json", TbResource.class); |
|||
log.info("Creating LwM2M resource"); |
|||
return tbClient.saveResource(newResource); |
|||
}); |
|||
deviceProfile = ResourceUtils.getResource("lwm2m/device_profile.json", DeviceProfile.class); |
|||
} |
|||
|
|||
deviceProfile.setName(profileName); |
|||
deviceProfile.setDefaultQueueName(target.getQueue()); |
|||
return tbClient.saveDeviceProfile(deviceProfile); |
|||
} |
|||
|
|||
private void createCalculatedField(Device device) { |
|||
log.info("Creating calculated field for device '{}'", device.getName()); |
|||
CalculatedField calculatedField = new CalculatedField(); |
|||
calculatedField.setName(TEST_CF_TELEMETRY_KEY); |
|||
calculatedField.setEntityId(device.getId()); |
|||
calculatedField.setType(CalculatedFieldType.SCRIPT); |
|||
ScriptCalculatedFieldConfiguration configuration = new ScriptCalculatedFieldConfiguration(); |
|||
Argument testDataArgument = new Argument(); |
|||
testDataArgument.setRefEntityKey(new ReferencedEntityKey(TEST_TELEMETRY_KEY, ArgumentType.TS_LATEST, null)); |
|||
configuration.setArguments(Map.of( |
|||
TEST_TELEMETRY_KEY, testDataArgument |
|||
)); |
|||
configuration.setExpression("return { \"" + TEST_CF_TELEMETRY_KEY + "\": " + TEST_TELEMETRY_KEY + " + \"-cf\" };"); |
|||
Output output = new Output(); |
|||
output.setType(OutputType.TIME_SERIES); |
|||
configuration.setOutput(output); |
|||
calculatedField.setConfiguration(configuration); |
|||
calculatedField.setDebugMode(true); |
|||
tbClient.saveCalculatedField(calculatedField); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.mqtt; |
|||
|
|||
public class MaxRetransmissionsReachedException extends RuntimeException { |
|||
|
|||
public MaxRetransmissionsReachedException(String message) { |
|||
super(message); |
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue