|
|
|
@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import com.google.common.collect.Iterators; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.SneakyThrows; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
@ -26,8 +27,6 @@ import org.springframework.stereotype.Component; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.common.data.Dashboard; |
|
|
|
import org.thingsboard.server.common.data.HasImage; |
|
|
|
import org.thingsboard.server.common.data.ResourceExportData; |
|
|
|
import org.thingsboard.server.common.data.ResourceType; |
|
|
|
import org.thingsboard.server.common.data.TbResourceInfo; |
|
|
|
import org.thingsboard.server.common.data.id.DashboardId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
@ -142,52 +141,71 @@ public class ResourcesUpdater { |
|
|
|
log.info("Updated {} widgets", updatedCount); |
|
|
|
} |
|
|
|
|
|
|
|
// TODO: remove after updating PE
|
|
|
|
@SneakyThrows |
|
|
|
public void updateFiles() { |
|
|
|
Path widgetsDirectory = Path.of("/home/viacheslav/Desktop/thingsboard-ce/application/src/main/data/json/system/widget_types"); |
|
|
|
try { |
|
|
|
Files.list(widgetsDirectory).forEach(path -> { |
|
|
|
WidgetTypeDetails widgetTypeDetails = JacksonUtil.readValue(path.toFile(), WidgetTypeDetails.class); |
|
|
|
widgetTypeDetails.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
Path dashboardsDirectory = Path.of("/home/viacheslav/Desktop/thingsboard-ce/application/src/main/data/json/demo/dashboards"); |
|
|
|
|
|
|
|
imageService.updateImagesUsage(widgetTypeDetails); |
|
|
|
resourceService.updateResourcesUsage(widgetTypeDetails); |
|
|
|
Files.list(widgetsDirectory).forEach(path -> { |
|
|
|
WidgetTypeDetails widgetTypeDetails = JacksonUtil.readValue(path.toFile(), WidgetTypeDetails.class); |
|
|
|
widgetTypeDetails.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
|
|
|
|
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>(); |
|
|
|
for (TbResourceInfo imageInfo : imageService.getUsedImages(widgetTypeDetails)) { |
|
|
|
resources.putIfAbsent(imageInfo.getId(), imageInfo); |
|
|
|
} |
|
|
|
for (TbResourceInfo resourceInfo : resourceService.getUsedResources(widgetTypeDetails)) { |
|
|
|
resources.putIfAbsent(resourceInfo.getId(), resourceInfo); |
|
|
|
} |
|
|
|
imageService.updateImagesUsage(widgetTypeDetails); |
|
|
|
resourceService.updateResourcesUsage(widgetTypeDetails); |
|
|
|
|
|
|
|
widgetTypeDetails.setResources(resources.values().stream() |
|
|
|
.map(resourceInfo -> { |
|
|
|
if (resourceInfo.getResourceType() == ResourceType.IMAGE) { |
|
|
|
ResourceExportData imageExportData = imageService.exportImage(resourceInfo); |
|
|
|
imageExportData.setResourceKey(null); // so that the image is not updated by resource key on import
|
|
|
|
return imageExportData; |
|
|
|
} else { |
|
|
|
return resourceService.exportResource(resourceInfo); |
|
|
|
} |
|
|
|
}) |
|
|
|
.toList()); |
|
|
|
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>(); |
|
|
|
for (TbResourceInfo imageInfo : imageService.getUsedImages(widgetTypeDetails)) { |
|
|
|
resources.putIfAbsent(imageInfo.getId(), imageInfo); |
|
|
|
} |
|
|
|
for (TbResourceInfo resourceInfo : resourceService.getUsedResources(widgetTypeDetails)) { |
|
|
|
resources.putIfAbsent(resourceInfo.getId(), resourceInfo); |
|
|
|
} |
|
|
|
|
|
|
|
ObjectNode json = (ObjectNode) JacksonUtil.valueToTree(widgetTypeDetails); |
|
|
|
Iterators.removeIf(json.fields(), field -> field.getValue() == null |
|
|
|
|| field.getValue().isNull()); |
|
|
|
json.remove(List.of("id", "createdTime", "tenantId", "externalId", "version", "scada")); |
|
|
|
JsonNode resourcesNode = json.remove("resources"); |
|
|
|
json.set("resources", resourcesNode); |
|
|
|
try { |
|
|
|
Files.writeString(path, json.toPrettyString()); |
|
|
|
log.info("UPDATED {}: {} resources used", path, resources.size()); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
widgetTypeDetails.setResources(resourceService.exportResources(TenantId.SYS_TENANT_ID, resources.values())); |
|
|
|
|
|
|
|
ObjectNode json = (ObjectNode) JacksonUtil.valueToTree(widgetTypeDetails); |
|
|
|
Iterators.removeIf(json.fields(), field -> field.getValue() == null |
|
|
|
|| field.getValue().isNull()); |
|
|
|
json.remove(List.of("id", "createdTime", "tenantId", "externalId", "version", "scada")); |
|
|
|
JsonNode resourcesNode = json.remove("resources"); |
|
|
|
json.set("resources", resourcesNode); |
|
|
|
try { |
|
|
|
Files.writeString(path, json.toPrettyString()); |
|
|
|
// log.info("UPDATED {}: {} resources used", path, resources.size());
|
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
Files.list(dashboardsDirectory).forEach(path -> { |
|
|
|
Dashboard dashboard = JacksonUtil.readValue(path.toFile(), Dashboard.class); |
|
|
|
dashboard.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
|
|
|
|
imageService.updateImagesUsage(dashboard); |
|
|
|
resourceService.updateResourcesUsage(dashboard); |
|
|
|
|
|
|
|
Map<TbResourceId, TbResourceInfo> resources = new HashMap<>(); |
|
|
|
for (TbResourceInfo imageInfo : imageService.getUsedImages(dashboard)) { |
|
|
|
resources.putIfAbsent(imageInfo.getId(), imageInfo); |
|
|
|
} |
|
|
|
for (TbResourceInfo resourceInfo : resourceService.getUsedResources(dashboard)) { |
|
|
|
resources.putIfAbsent(resourceInfo.getId(), resourceInfo); |
|
|
|
} |
|
|
|
|
|
|
|
dashboard.setResources(resourceService.exportResources(TenantId.SYS_TENANT_ID, resources.values())); |
|
|
|
|
|
|
|
ObjectNode json = (ObjectNode) JacksonUtil.valueToTree(dashboard); |
|
|
|
json.remove(List.of("id", "createdTime", "tenantId", "externalId", "version", "scada")); |
|
|
|
JsonNode resourcesNode = json.remove("resources"); |
|
|
|
json.set("resources", resourcesNode); |
|
|
|
try { |
|
|
|
Files.writeString(path, json.toPrettyString()); |
|
|
|
// log.info("UPDATED {}: {} resources used", path, resources.size());
|
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public void updateDeviceProfilesImages() { |
|
|
|
|