|
|
|
@ -29,6 +29,8 @@ import org.thingsboard.monitoring.service.BaseHealthChecker; |
|
|
|
import org.thingsboard.monitoring.util.ResourceUtils; |
|
|
|
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.device.credentials.lwm2m.LwM2MBootstrapClientCredentials; |
|
|
|
import org.thingsboard.server.common.data.device.credentials.lwm2m.LwM2MDeviceCredentials; |
|
|
|
@ -38,6 +40,9 @@ 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.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
@ -45,27 +50,19 @@ import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
@Slf4j |
|
|
|
public abstract class TransportHealthChecker<C extends TransportMonitoringConfig> extends BaseHealthChecker<C, TransportMonitoringTarget> { |
|
|
|
|
|
|
|
private static final String DEFAULT_DEVICE_NAME = "[Monitoring] %s transport (%s)"; |
|
|
|
private static final String DEFAULT_PROFILE_NAME = "[Monitoring] %s"; |
|
|
|
|
|
|
|
public TransportHealthChecker(C config, TransportMonitoringTarget target) { |
|
|
|
super(config, target); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void initialize(TbClient tbClient) { |
|
|
|
String deviceName = String.format(DEFAULT_DEVICE_NAME, config.getTransportType(), target.getBaseUrl()); |
|
|
|
Device device = tbClient.getTenantDevice(deviceName) |
|
|
|
.orElseGet(() -> { |
|
|
|
log.info("Creating new device '{}'", deviceName); |
|
|
|
return createDevice(config.getTransportType(), deviceName, tbClient); |
|
|
|
}); |
|
|
|
Device device = getOrCreateDevice(tbClient); |
|
|
|
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(deviceName); |
|
|
|
deviceConfig.setName(device.getName()); |
|
|
|
deviceConfig.setCredentials(credentials); |
|
|
|
target.setDevice(deviceConfig); |
|
|
|
} |
|
|
|
@ -77,51 +74,43 @@ public abstract class TransportHealthChecker<C extends TransportMonitoringConfig |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Object getInfo() { |
|
|
|
return new TransportInfo(getTransportType(), target.getBaseUrl()); |
|
|
|
return new TransportInfo(getTransportType(), target.getBaseUrl(), target.getQueue()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected String getKey() { |
|
|
|
return getTransportType().name().toLowerCase() + "Transport"; |
|
|
|
return getTransportType().name().toLowerCase() + (target.getQueue().equals("Main") ? "" : target.getQueue()) + "Transport"; |
|
|
|
} |
|
|
|
|
|
|
|
protected abstract TransportType getTransportType(); |
|
|
|
|
|
|
|
|
|
|
|
private Device createDevice(TransportType transportType, String name, TbClient tbClient) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName(name); |
|
|
|
private Device getOrCreateDevice(TbClient tbClient) { |
|
|
|
TransportType transportType = config.getTransportType(); |
|
|
|
String deviceName = String.format("%s (%s) - %s", transportType.getName(), target.getQueue(), target.getBaseUrl()); |
|
|
|
Device device = tbClient.getTenantDevice(deviceName).orElse(null); |
|
|
|
if (device != null) { |
|
|
|
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(tbClient); |
|
|
|
device.setType(deviceProfile.getName()); |
|
|
|
device.setDeviceProfileId(deviceProfile.getId()); |
|
|
|
|
|
|
|
if (transportType != TransportType.LWM2M) { |
|
|
|
device.setType("default"); |
|
|
|
deviceData.setTransportConfiguration(new DefaultDeviceTransportConfiguration()); |
|
|
|
credentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN); |
|
|
|
} 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); |
|
|
|
}); |
|
|
|
String profileName = String.format(DEFAULT_PROFILE_NAME, transportType); |
|
|
|
DeviceProfile profile = tbClient.getDeviceProfiles(new PageLink(1, 0, profileName)).getData() |
|
|
|
.stream().findFirst() |
|
|
|
.orElseGet(() -> { |
|
|
|
DeviceProfile newProfile = ResourceUtils.getResource("lwm2m/device_profile.json", DeviceProfile.class); |
|
|
|
newProfile.setName(profileName); |
|
|
|
log.info("Creating LwM2M device profile"); |
|
|
|
return tbClient.saveDeviceProfile(newProfile); |
|
|
|
}); |
|
|
|
device.setType(profileName); |
|
|
|
device.setDeviceProfileId(profile.getId()); |
|
|
|
deviceData.setTransportConfiguration(new Lwm2mDeviceTransportConfiguration()); |
|
|
|
|
|
|
|
credentials.setCredentialsType(DeviceCredentialsType.LWM2M_CREDENTIALS); |
|
|
|
LwM2MDeviceCredentials lwm2mCreds = new LwM2MDeviceCredentials(); |
|
|
|
NoSecClientCredential client = new NoSecClientCredential(); |
|
|
|
@ -133,7 +122,42 @@ public abstract class TransportHealthChecker<C extends TransportMonitoringConfig |
|
|
|
lwm2mCreds.setBootstrap(bootstrap); |
|
|
|
credentials.setCredentialsValue(JacksonUtil.toString(lwm2mCreds)); |
|
|
|
} |
|
|
|
|
|
|
|
return tbClient.saveDeviceWithCredentials(device, credentials).get(); |
|
|
|
} |
|
|
|
|
|
|
|
private DeviceProfile getOrCreateDeviceProfile(TbClient tbClient) { |
|
|
|
TransportType transportType = config.getTransportType(); |
|
|
|
String profileName = String.format("%s (%s)", transportType.getName(), target.getQueue()); |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|