|
|
|
@ -15,26 +15,22 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.service.entitiy.dashboard; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.common.data.ResourceType; |
|
|
|
import org.thingsboard.server.common.data.TbResource; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.msg.queue.ServiceType; |
|
|
|
import org.thingsboard.server.dao.resource.ResourceService; |
|
|
|
import org.thingsboard.server.dao.widget.WidgetsBundleService; |
|
|
|
import org.thingsboard.server.queue.discovery.PartitionService; |
|
|
|
import org.thingsboard.server.queue.util.AfterStartUp; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.entitiy.widgets.bundle.TbWidgetsBundleService; |
|
|
|
import org.thingsboard.server.service.sync.GitSyncService; |
|
|
|
import org.thingsboard.server.service.sync.vc.GitRepository.FileType; |
|
|
|
import org.thingsboard.server.service.sync.vc.GitRepository.RepoFile; |
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Stream; |
|
|
|
@ -47,18 +43,19 @@ public class DashboardSyncService { |
|
|
|
|
|
|
|
private final GitSyncService gitSyncService; |
|
|
|
private final ResourceService resourceService; |
|
|
|
private final TbWidgetsBundleService tbWidgetsBundleService; |
|
|
|
private final WidgetsBundleService widgetsBundleService; |
|
|
|
private final PartitionService partitionService; |
|
|
|
|
|
|
|
@Value("${transport.gateway.dashboard.sync.enabled:true}") |
|
|
|
private boolean enabled; |
|
|
|
@Value("${transport.gateway.dashboard.sync.repository_url:}") |
|
|
|
private String repoUrl; |
|
|
|
@Value("${transport.gateway.dashboard.sync.branch:main}") |
|
|
|
private String branch; |
|
|
|
@Value("${transport.gateway.dashboard.sync.fetch_frequency:24}") |
|
|
|
private int fetchFrequencyHours; |
|
|
|
|
|
|
|
private static final String REPO_KEY = "gateways-dashboard"; |
|
|
|
private static final String GATEWAY_RESOURCE_ID_PARAM = "${GATEWAY_RESOURCE_ID}"; |
|
|
|
private static final String GATEWAYS_DASHBOARD_KEY = "gateways_dashboard.json"; |
|
|
|
|
|
|
|
@AfterStartUp(order = AfterStartUp.REGULAR_SERVICE) |
|
|
|
@ -66,7 +63,7 @@ public class DashboardSyncService { |
|
|
|
if (!enabled) { |
|
|
|
return; |
|
|
|
} |
|
|
|
gitSyncService.registerSync(REPO_KEY, repoUrl, "main", TimeUnit.HOURS.toMillis(fetchFrequencyHours), this::update); |
|
|
|
gitSyncService.registerSync(REPO_KEY, repoUrl, branch, TimeUnit.HOURS.toMillis(fetchFrequencyHours), this::update); |
|
|
|
} |
|
|
|
|
|
|
|
private void update() { |
|
|
|
@ -74,47 +71,25 @@ public class DashboardSyncService { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
RepoFile extensionResourceFile = listFiles("resources").get(0); |
|
|
|
String data = getFileContent(extensionResourceFile.path()); |
|
|
|
TbResource extensionResource = createOrUpdateResource(ResourceType.JS_MODULE, extensionResourceFile.name(), data.getBytes(StandardCharsets.UTF_8)); |
|
|
|
String extensionResourceId = extensionResource.getUuidId().toString(); |
|
|
|
List<RepoFile> resources = listFiles("resources"); |
|
|
|
for (RepoFile resourceFile : resources) { |
|
|
|
String data = getFileContent(resourceFile.path()); |
|
|
|
resourceService.updateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data); |
|
|
|
} |
|
|
|
|
|
|
|
Stream<JsonNode> widgetsBundles = listFiles("widget_bundles").stream() |
|
|
|
.map(widgetsBundleFile -> { |
|
|
|
String widgetsBundleDescriptor = getFileContent(widgetsBundleFile.path()); |
|
|
|
widgetsBundleDescriptor = widgetsBundleDescriptor.replace(GATEWAY_RESOURCE_ID_PARAM, extensionResourceId); |
|
|
|
return JacksonUtil.toJsonNode(widgetsBundleDescriptor); |
|
|
|
}); |
|
|
|
Stream<JsonNode> widgetTypes = listFiles("widget_types").stream() |
|
|
|
.map(widgetTypeFile -> { |
|
|
|
String widgetTypeDetails = getFileContent(widgetTypeFile.path()); |
|
|
|
widgetTypeDetails = widgetTypeDetails.replace(GATEWAY_RESOURCE_ID_PARAM, extensionResourceId); |
|
|
|
return JacksonUtil.toJsonNode(widgetTypeDetails); |
|
|
|
}); |
|
|
|
tbWidgetsBundleService.updateWidgets(TenantId.SYS_TENANT_ID, widgetsBundles, widgetTypes); |
|
|
|
Stream<String> widgetsBundles = listFiles("widget_bundles").stream() |
|
|
|
.map(widgetsBundleFile -> getFileContent(widgetsBundleFile.path())); |
|
|
|
Stream<String> widgetTypes = listFiles("widget_types").stream() |
|
|
|
.map(widgetTypeFile -> getFileContent(widgetTypeFile.path())); |
|
|
|
widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes); |
|
|
|
|
|
|
|
RepoFile dashboardFile = listFiles("dashboards").get(0); |
|
|
|
String dashboardJson = getFileContent(dashboardFile.path()).replace(GATEWAY_RESOURCE_ID_PARAM, extensionResourceId); |
|
|
|
createOrUpdateResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, dashboardJson.getBytes(StandardCharsets.UTF_8)); |
|
|
|
String dashboardJson = getFileContent(dashboardFile.path()); |
|
|
|
resourceService.updateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, dashboardJson); |
|
|
|
|
|
|
|
log.info("Gateways dashboard sync completed"); |
|
|
|
} |
|
|
|
|
|
|
|
private TbResource createOrUpdateResource(ResourceType resourceType, String resourceKey, byte[] data) { |
|
|
|
TbResource resource = resourceService.findResourceByTenantIdAndKey(TenantId.SYS_TENANT_ID, resourceType, resourceKey); |
|
|
|
if (resource == null) { |
|
|
|
resource = new TbResource(); |
|
|
|
resource.setTenantId(TenantId.SYS_TENANT_ID); |
|
|
|
resource.setResourceType(resourceType); |
|
|
|
resource.setResourceKey(resourceKey); |
|
|
|
resource.setFileName(resourceKey); |
|
|
|
resource.setTitle(resourceKey); |
|
|
|
} |
|
|
|
resource.setData(data); |
|
|
|
log.debug("{} resource {}", (resource.getId() == null ? "Creating" : "Updating"), resourceKey); |
|
|
|
return resourceService.saveResource(resource); |
|
|
|
} |
|
|
|
|
|
|
|
private List<RepoFile> listFiles(String path) { |
|
|
|
return gitSyncService.listFiles(REPO_KEY, path, 1, FileType.FILE); |
|
|
|
} |
|
|
|
|