100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added options for custom tile server and tile uploads (#1721)
Ekhorn695c619e70d9
diff --git a/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java b/manager/src/main/java/org/openremote/manager/app/ConfigurationService.javaindex 8f9325536..0b47bc313 100644--- a/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java+++ b/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java@@ -19,7 +19,6 @@*/package org.openremote.manager.app;-import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.node.ObjectNode;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.timer.TimerService;@@ -30,7 +29,6 @@ import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.file.FileInfo;import org.openremote.model.manager.ManagerAppConfig;-import org.openremote.model.manager.ManagerAppRealmConfig;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;@@ -40,7 +38,9 @@ import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;-import java.util.*;+import java.util.Arrays;+import java.util.Objects;+import java.util.Optional;import java.util.concurrent.atomic.AtomicReference;import java.util.logging.Level;import java.util.logging.Logger;@@ -109,7 +109,7 @@ public class ConfigurationService implements ContainerService {LOG.info("Configuration Service Used files:");LOG.info("\t- manager_config.json: " + managerConfigPath);LOG.info("\t- mapsettings.json: " + mapSettingsPath);- LOG.info("\t- mapdata.mbtiles: " + mapTilesPath);+ LOG.info("\t- mapdata.mbtiles: " + Optional.of(getCustomMapTilesPath(true)).map(p -> p.toFile().isDirectory() ? null : p).orElse(mapTilesPath));}@Override@@ -164,6 +164,39 @@ public class ConfigurationService implements ContainerService {return mapTilesPath;}+ /**+ * Must be called after {@link #mapTilesPath} is initialized.+ * @return the parent directory path for custom mbtiles, or the custom mbtiles file if findMBTilesFile is true. If+ * no custom mbtiles file is found or it is the same file as {@link #mapTilesPath} then the parent directory path+ * is returned (i.e. as if no custom mbtiles file can be found).+ * @throws IOException if persistence directory cannot be created or read+ */+ public Path getCustomMapTilesPath(boolean findMBTilesFile) throws IOException {+ Path parentDir = getPersistedCustomTilesPath();++ try {+ if (!Files.exists(parentDir)) Files.createDirectories(parentDir);+ } catch (IOException e) {+ LOG.log(Level.SEVERE, "Could not create parent directory for custom tiles", e);+ throw e;+ }++ if (!findMBTilesFile) {+ return parentDir;+ }++ // If mapTilesPath has resolved to an mbtiles file in the persistence dir then it isn't considered a custom file+ Path defaultMapTilesFilename = Optional.ofNullable(this.mapTilesPath)+ .map(Path::toAbsolutePath)+ .orElse(null);++ return Arrays.stream(Objects.requireNonNull(parentDir.toFile().listFiles((dir, name) -> name.endsWith(".mbtiles"))))+ .map(File::toPath)+ .filter(p -> !p.toAbsolutePath().equals(defaultMapTilesFilename))+ .findFirst()+ .orElse(parentDir);+ }+public ManagerAppConfig getManagerConfig() {if (managerAppConfig != null) {return managerAppConfig.get();@@ -263,6 +296,10 @@ public class ConfigurationService implements ContainerService {return persistenceService.getStorageDir().resolve("manager").resolve("mapsettings.json");}+ protected Path getPersistedCustomTilesPath() {+ return persistenceService.getStorageDir().resolve("map");+ }+protected Path getPersistedManagerConfigImagePath() {return this.persistenceService.resolvePath("manager").resolve("images");}diff --git a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.javaindex 350a2d62f..5a9ff57a9 100644--- a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java@@ -20,19 +20,23 @@package org.openremote.manager.map;import com.fasterxml.jackson.databind.node.ObjectNode;+import jakarta.ws.rs.ServerErrorException;+import jakarta.ws.rs.WebApplicationException;+import jakarta.ws.rs.core.Response;import org.openremote.container.web.WebResource;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.model.http.RequestParams;-import org.openremote.model.manager.MapRealmConfig;+import org.openremote.model.manager.MapConfig;import org.openremote.model.map.MapResource;-import jakarta.ws.rs.WebApplicationException;-import jakarta.ws.rs.core.Response;--import java.util.Map;+import java.io.IOException;+import java.io.InputStream;+import java.util.logging.Level;+import java.util.logging.Logger;public class MapResourceImpl extends WebResource implements MapResource {+ private static final Logger LOG = Logger.getLogger(MapResourceImpl.class.getName());protected final MapService mapService;protected final ManagerIdentityService identityService;@@ -42,7 +46,7 @@ public class MapResourceImpl extends WebResource implements MapResource {}@Override- public Object saveSettings(RequestParams requestParams, Map<String, MapRealmConfig> mapConfig) {+ public Object saveSettings(RequestParams requestParams, MapConfig mapConfig) {return mapService.saveMapConfig(mapConfig);}@@ -71,4 +75,48 @@ public class MapResourceImpl extends WebResource implements MapResource {throw new WebApplicationException(Response.Status.NO_CONTENT);}}++ @Override+ public ObjectNode uploadMap(RequestParams requestParams, String filename) {+ if (request.getContentLength() > mapService.customMapLimit) {+ throw new WebApplicationException(Response.Status.REQUEST_ENTITY_TOO_LARGE);+ }++ try (InputStream stream = request.getInputStream()) {+ mapService.saveUploadedFile(filename, stream);+ return mapService.getMapSettings(+ getRequestRealmName(),+ requestParams.getExternalSchemeHostAndPort()+ );+ } catch (IOException e) {+ LOG.log(Level.INFO, "Failed to save custom map tiles", e);+ throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);+ } catch (IllegalArgumentException e) {+ LOG.log(Level.INFO, "Invalid filename specified", e);+ throw new WebApplicationException(Response.Status.BAD_REQUEST);+ }+ }++ @Override+ public ObjectNode getCustomMapInfo() {+ try {+ return mapService.getCustomMapInfo();+ } catch (IOException e) {+ throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR, e);+ }+ }++ @Override+ public ObjectNode deleteMap(RequestParams requestParams) {+ try {+ mapService.deleteUploadedFile();+ return mapService.getMapSettings(+ getRequestRealmName(),+ requestParams.getExternalSchemeHostAndPort()+ );+ } catch (IOException e) {+ LOG.log(Level.INFO, "Failed to delete custom map tiles", e);+ throw new WebApplicationException(Response.Status.BAD_REQUEST);+ }+ }}diff --git a/manager/src/main/java/org/openremote/manager/map/MapService.java b/manager/src/main/java/org/openremote/manager/map/MapService.javaindex de30a211e..823446242 100644--- a/manager/src/main/java/org/openremote/manager/map/MapService.java+++ b/manager/src/main/java/org/openremote/manager/map/MapService.java@@ -32,20 +32,25 @@ import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Container;import org.openremote.model.ContainerService;-import org.openremote.model.manager.MapRealmConfig;+import org.openremote.model.manager.MapConfig;+import org.openremote.model.manager.MapSourceConfig;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;+import java.io.File;+import java.io.IOException;+import java.io.InputStream;+import java.io.OutputStream;import java.net.URI;+import java.nio.file.Files;import java.nio.file.Path;import java.sql.*;-import java.util.HashMap;-import java.util.Map;-import java.util.Optional;+import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ConcurrentMap;import java.util.logging.Level;import java.util.logging.Logger;+import java.util.stream.StreamSupport;import static org.openremote.container.util.MapAccess.getInteger;import static org.openremote.container.util.MapAccess.getString;@@ -59,6 +64,8 @@ public class MapService implements ContainerService {public static final String OR_MAP_TILESERVER_HOST_DEFAULT = null;public static final String OR_MAP_TILESERVER_PORT = "OR_MAP_TILESERVER_PORT";public static final int OR_MAP_TILESERVER_PORT_DEFAULT = 8082;+ public static final String OR_CUSTOM_MAP_SIZE_LIMIT = "OR_CUSTOM_MAP_SIZE_LIMIT";+ public static final int OR_CUSTOM_MAP_SIZE_LIMIT_DEFAULT = 30_000_000;public static final String RASTER_MAP_TILE_PATH = "/raster_map/tile";public static final String TILESERVER_TILE_PATH = "/styles/standard";public static final String OR_MAP_TILESERVER_REQUEST_TIMEOUT = "OR_MAP_TILESERVER_REQUEST_TIMEOUT";@@ -66,6 +73,7 @@ public class MapService implements ContainerService {public static final String OR_PATH_PREFIX = "OR_PATH_PREFIX";public static final String OR_PATH_PREFIX_DEFAULT = "";private static final Logger LOG = Logger.getLogger(MapService.class.getName());+ private static final String DEFAULT_VECTOR_TILES_URL = "mbtiles://mapdata.mbtiles";private static ConfigurationService configurationService;// Shared SQL connection is fine concurrently in SQLite@@ -75,14 +83,37 @@ public class MapService implements ContainerService {protected ConcurrentMap<String, ObjectNode> mapSettings = new ConcurrentHashMap<>();protected ConcurrentMap<String, ObjectNode> mapSettingsJs = new ConcurrentHashMap<>();protected String pathPrefix;+ protected int customMapLimit = OR_CUSTOM_MAP_SIZE_LIMIT_DEFAULT;- public ObjectNode saveMapConfig(Map<String, MapRealmConfig> mapConfiguration) throws RuntimeException {+ public ObjectNode saveMapConfig(MapConfig mapConfiguration) throws RuntimeException {if (mapConfig == null) {mapConfig = ValueUtil.JSON.createObjectNode();}- mapConfig.putPOJO("options", mapConfiguration);++ ObjectNode vectorTiles = ValueUtil.JSON.valueToTree(mapConfiguration.sources.get("vector_tiles"));+ String tileUrl = Optional.ofNullable(vectorTiles.get("tiles"))+ .map(tiles -> tiles.get(0))+ .filter(JsonNode::isTextual)+ .map(JsonNode::textValue)+ .orElse(null);+ // Saves custom tile server url if custom is true in the vector_tiles source and the url follows the required xyz scheme.+ // Otherwise, replace it with the default configuration.+ if (vectorTiles.get("custom").booleanValue() && tileUrl != null && tileUrl.contains("/{z}/{x}/{y}")) {+ vectorTiles.put("url", tileUrl);+ } else {+ vectorTiles = ValueUtil.JSON.createObjectNode()+ .put("type", "vector")+ .put("url", DEFAULT_VECTOR_TILES_URL);+ }+ mapConfiguration.sources.put("vector_tiles", ValueUtil.JSON.convertValue(vectorTiles, MapSourceConfig.class));++ mapConfig.putPOJO("options", mapConfiguration.options);+ mapConfig.putPOJO("sources", mapConfiguration.sources);+configurationService.saveMapConfig(mapConfig);+ mapConfig = configurationService.getMapConfig();mapSettings.clear();+return mapConfig;}@@ -151,6 +182,7 @@ public class MapService implements ContainerService {String tileServerHost = getString(container.getConfig(), OR_MAP_TILESERVER_HOST, OR_MAP_TILESERVER_HOST_DEFAULT);int tileServerPort = getInteger(container.getConfig(), OR_MAP_TILESERVER_PORT, OR_MAP_TILESERVER_PORT_DEFAULT);pathPrefix = getString(container.getConfig(), OR_PATH_PREFIX, OR_PATH_PREFIX_DEFAULT);+ customMapLimit = getInteger(container.getConfig(), OR_CUSTOM_MAP_SIZE_LIMIT, OR_CUSTOM_MAP_SIZE_LIMIT_DEFAULT);if (!TextUtil.isNullOrEmpty(tileServerHost)) {@@ -184,30 +216,91 @@ public class MapService implements ContainerService {@Overridepublic void start(Container container) throws Exception {- setData();+ setData(false);}- public void setData() throws ClassNotFoundException, SQLException, NullPointerException {- Path mapTilesPath = configurationService.getMapTilesPath();- if (mapTilesPath == null) {- return;+ /**+ * Connects to mbtiles DB and loads metadata+ * @return Path of connected mbtiles file+ * @throws IOException+ * @throws ClassNotFoundException+ * @throws SQLException+ * @throws NullPointerException+ */+ public Path setData(boolean skipCustom) {+ try {+ if (connection != null) {+ connection.close();+ }+ } catch (SQLException e) {+ LOG.log(Level.WARNING, "Could not close existing connection", e);}- mapTilesPath = mapTilesPath.toAbsolutePath();- if (!mapTilesPath.toFile().exists()) {- return;++ Path connectedFile = null;++ if (!skipCustom) {+ // Try and load custom map tiles first+ try {+ Path customMapTilesPath = configurationService.getCustomMapTilesPath(true);++ if (customMapTilesPath.toFile().isFile()) {+ Class.forName(org.sqlite.JDBC.class.getName());+ connection = DriverManager.getConnection("jdbc:sqlite:" + customMapTilesPath);+ metadata = getMetadata(connection);++ if (!metadata.isValid()) {+ LOG.warning("Custom map meta data could not be loaded, falling back to default map");+ try {+ if (connection != null) {+ connection.close();+ }+ } catch (SQLException e) {+ LOG.log(Level.WARNING, "Could not close connection", e);+ }+ } else {+ connectedFile = customMapTilesPath;+ }+ }+ } catch (IOException | ClassNotFoundException | SQLException e) {+ LOG.log(Level.WARNING, "An error occurred whilst trying to load custom map tiles file", e);+ }}- Class.forName(org.sqlite.JDBC.class.getName());- connection = DriverManager.getConnection("jdbc:sqlite:" + mapTilesPath);- metadata = getMetadata(connection);- if (metadata.isValid()) {- mapConfig = configurationService.getMapConfig();- if (mapConfig == null) {- return;+ // Fallback on default map tiles+ if (connectedFile == null) {+ try {+ Path mapTilesPath = configurationService.getMapTilesPath();+ if (mapTilesPath != null) {+ Class.forName(org.sqlite.JDBC.class.getName());+ connection = DriverManager.getConnection("jdbc:sqlite:" + mapTilesPath);+ metadata = getMetadata(connection);++ if (!metadata.isValid()) {+ LOG.warning("Default map meta data could not be loaded, map will not work");+ try {+ if (connection != null) {+ connection.close();+ }+ } catch (SQLException e) {+ LOG.log(Level.WARNING, "Could not close connection", e);+ }+ } else {+ connectedFile = mapTilesPath;+ }+ }+ } catch (ClassNotFoundException | SQLException e) {+ LOG.log(Level.WARNING, "An error occurred whilst trying to load map tiles file", e);}- } else {- LOG.warning("Map meta data could not be loaded, map functionality will not work");- return;+ }++ if (connectedFile == null) {+ return null;+ }++ mapConfig = configurationService.getMapConfig();++ if (mapConfig == null) {+ return connectedFile;}ObjectNode options = Optional.ofNullable((ObjectNode)mapConfig.get("options")).orElse(mapConfig.objectNode());@@ -236,6 +329,8 @@ public class MapService implements ContainerService {if (!defaultOptions.has("bounds") && metadata.getBounds() != null) {defaultOptions.set("bounds", metadata.getBounds());}++ return connectedFile;}@Override@@ -291,8 +386,25 @@ public class MapService implements ContainerService {}));ArrayNode tilesArray = mapConfig.arrayNode();- String tileUrl = UriBuilder.fromUri(host).replacePath(pathPrefix + API_PATH).path(realm).path("map/tile").build().toString() + "/{z}/{x}/{y}";+ String tileUrl = UriBuilder.fromUri(host)+ .replacePath(pathPrefix + API_PATH)+ .path(realm)+ .path("map/tile")+ .build()+ .toString() + "/{z}/{x}/{y}";tilesArray.insert(0, tileUrl);++ Optional.ofNullable(vectorTilesObj.get("tiles"))+ .map(tiles -> tiles.get(0))+ .filter(JsonNode::isTextual)+ .map(JsonNode::textValue)+ .ifPresent(url -> {+ if (!url.contentEquals(DEFAULT_VECTOR_TILES_URL)) {+ tilesArray.remove(0);+ tilesArray.insert(0, url);+ }+ });+vectorTilesObj.replace("tiles", tilesArray);});@@ -377,6 +489,118 @@ public class MapService implements ContainerService {}}+ public void saveUploadedFile(String filename, InputStream fileInputStream) throws IOException, IllegalArgumentException {+ Path customTilesDir = configurationService.getCustomMapTilesPath(false);+ Path tilesPath = customTilesDir.resolve(filename);+ Path previousCustomTilesPath = configurationService.getCustomMapTilesPath(true);++ // Check there's no back refs in the filename+ boolean isValid = tilesPath.toFile().getCanonicalPath().contains(customTilesDir.toFile().getCanonicalPath() + File.separator);+ if (!isValid) {+ String msg = "Filename outside permitted directory: " + filename;+ LOG.warning(msg);+ throw new IllegalArgumentException(msg);+ }++ try (OutputStream outputStream = Files.newOutputStream(tilesPath)) {+ byte[] buffer = new byte[4096];+ int bytesRead;+ int written = 0;+ while ((bytesRead = fileInputStream.read(buffer)) != -1) {+ if (written > customMapLimit) {+ String msg = "Stream continued passed custom map limit size: " + tilesPath;+ LOG.log(Level.SEVERE, msg);+ throw new IOException(msg);+ }+ outputStream.write(buffer, 0, bytesRead);+ written += bytesRead;+ }+ } catch (IOException e) {+ try {+ // Attempt to delete this invalid map data file+ Files.deleteIfExists(tilesPath);+ } catch (IOException ex) {+ LOG.log(Level.WARNING, "Could not delete partially written file: " + filename, ex);+ }+ throw e;+ }++ // Ensure we can access this new file and that it is a valid mbtiles file+ Path loadedFile = setData(false);+ // TODO: Investigate whether this can be combined into setData (see https://github.com/openremote/openremote/issues/1833).+ // The metadata is set to ensure the bounding box is not outside the visible tile area.+ saveMapMetadata(metadata);++ if (loadedFile == null || !loadedFile.toAbsolutePath().equals(tilesPath.toAbsolutePath())) {+ try {+ // Attempt to delete this invalid map data file+ Files.deleteIfExists(tilesPath);+ } catch (IOException ex) {+ LOG.log(Level.WARNING, "Could not delete partially written file: " + filename, ex);+ }+ throw new IOException("Failed to load map data ensure the uploaded file is a valid mbtiles file: " + filename);+ }++ // Now delete any previous custom mbtiles file+ if (previousCustomTilesPath.toFile().isFile()) {+ try {+ // Attempt to delete this old map data file+ Files.deleteIfExists(previousCustomTilesPath);+ } catch (IOException ex) {+ LOG.log(Level.WARNING, "Could not delete old file: " + previousCustomTilesPath, ex);+ // This is problematic as which custom file gets discovered first on next startup??+ }+ }+ }++ public void deleteUploadedFile() throws IOException {+ Path previousCustomTilesPath = configurationService.getCustomMapTilesPath(true);++ if (previousCustomTilesPath.toFile().isFile()) {++ setData(true);+ // TODO: Investigate whether this can be combined into setData (see https://github.com/openremote/openremote/issues/1833).+ // The metadata is set to ensure the bounding box is not outside the visible tile area.+ saveMapMetadata(metadata);++ // Attempt to delete this old map data file+ Files.deleteIfExists(previousCustomTilesPath);+ }+ }++ public void saveMapMetadata(Metadata metadata) {+ Optional<JsonNode> options = Optional.ofNullable(mapConfig.get("options"));+ if (metadata.isValid() && options.isPresent()) {+ Iterator<Map.Entry<String, JsonNode>> fields = options.get().fields();+ while (fields.hasNext()) {+ Map.Entry<String, JsonNode> field = fields.next();+ ObjectNode value = (ObjectNode)field.getValue();+ value.set("center", metadata.getCenter());+ value.set("bounds", metadata.getBounds());+ }+ configurationService.saveMapConfig(mapConfig);+ mapConfig = configurationService.getMapConfig();+ mapSettings.clear();+ }+ }++ public ObjectNode getCustomMapInfo() throws IOException {+ return ValueUtil.JSON+ .createObjectNode()+ .put("limit", this.customMapLimit)+ .put("filename",+ Optional.ofNullable(configurationService.getCustomMapTilesPath(true))+ .map(p -> p.toFile().isFile() ? p : null)+ .map(p -> p.getFileName().toString())+ .orElse(null)+ );+ }++ /**+ * The {@link Metadata} class validates that {@link #bounds} and {@link #center} follow the mbtiles-spec.+ *+ * @implSpec https://github.com/mapbox/mbtiles-spec/blob/master/1.3/spec.md#metadata+ */protected static final class Metadata {protected String attribution;protected ArrayNode vectorLayers;@@ -393,7 +617,18 @@ public class MapService implements ContainerService {this.center = center;this.maxZoom = maxZoom;this.minZoom = minZoom;- valid = true;++ boolean boundsValid = this.bounds.size() == 4+ && StreamSupport.stream(Spliterators.spliterator(this.bounds.elements(), 0, 4), false).allMatch(v -> v.numberType() != null);+ boolean centerValid = this.center.size() == 3+ && StreamSupport.stream(Spliterators.spliterator(this.bounds.elements(), 0, 3), false).allMatch(v -> v.numberType() != null);+ if (!boundsValid) {+ LOG.log(Level.WARNING, "Map bounds are invalid.");+ }+ if (!centerValid) {+ LOG.log(Level.WARNING, "Map center is invalid.");+ }+ valid = boundsValid && centerValid;}public Metadata() {diff --git a/manager/src/map/mapdata.mbtiles b/manager/src/map/mapdata.mbtilesindex 3a111ac4f..92df6f0fe 100644Binary files a/manager/src/map/mapdata.mbtiles and b/manager/src/map/mapdata.mbtiles differdiff --git a/model/build.gradle b/model/build.gradleindex 9124af3db..17ae611bf 100644--- a/model/build.gradle+++ b/model/build.gradle@@ -11,7 +11,6 @@ dependencies {api "org.hibernate.orm:hibernate-core:$hibernateVersion"api "org.hibernate.validator:hibernate-validator:$hibernateValidatorVersion"api "jakarta.el:jakarta.el-api:$javaxELAPIVersion"-api "jakarta.annotation:jakarta.annotation-api:$javaxAnnotationsVersion"api "jakarta.ws.rs:jakarta.ws.rs-api:$jaxrsSpecVersion"diff --git a/model/src/main/java/org/openremote/model/manager/MapConfig.java b/model/src/main/java/org/openremote/model/manager/MapConfig.javaindex 0ac7dabbb..e791e841c 100644--- a/model/src/main/java/org/openremote/model/manager/MapConfig.java+++ b/model/src/main/java/org/openremote/model/manager/MapConfig.java@@ -22,5 +22,6 @@ package org.openremote.model.manager;import java.util.Map;public class MapConfig {- protected Map<String, MapRealmConfig> options;+ public Map<String, MapRealmConfig> options;+ public Map<String, MapSourceConfig> sources;}diff --git a/model/src/main/java/org/openremote/model/manager/MapSourceConfig.java b/model/src/main/java/org/openremote/model/manager/MapSourceConfig.javanew file mode 100644index 000000000..8d7a91187--- /dev/null+++ b/model/src/main/java/org/openremote/model/manager/MapSourceConfig.java@@ -0,0 +1,89 @@+/*+ * Copyright 2025, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.manager;++/**+ * Implements MapLibre style spec sources property.+ *+ * @see https://maplibre.org/maplibre-style-spec/sources+ */+public class MapSourceConfig {+ /**+ * The type of the source.+ *+ * @implSpec Required enum. Possible values: <code>vector</code>.+ */+ protected String type;+ /**+ * A URL to a TileJSON resource.+ *+ * @implSpec Optional string. Supported protocols are <code>http:</code> and <code>https:</code>.+ */+ protected String url;+ /**+ * An array of one or more tile source URLs, as in the TileJSON spec.+ *+ * @implSpec Optional array. Possible values: vector.+ */+ protected String[] tiles;+ /**+ * An array containing the longitude and latitude of the southwest and northeast corners of the source's bounding box in the following order: <code>[sw.lng, sw.lat, ne.lng, ne.lat]</code>. When this property is included in a source, no tiles outside of the given bounds are requested by MapLibre.+ *+ * @implSpec Optional array. Possible values: vector.+ */+ protected float[] bounds;+ /**+ * Influences the y direction of the tile coordinates. The global-mercator (aka Spherical Mercator) profile is assumed.+ * <p><ul>+ * <li><code>xyz</code>: Slippy map tilenames scheme.</li>+ * <li><code>tms</code>: OSGeo spec scheme.</li>+ * </ul></p>+ *+ * @implSpec Optional enum. Possible values: <code>xyz</code>, <code>tms</code>. Defaults to <code>xyz</code>.+ */+ protected String scheme;+ /**+ * Minimum zoom level for which tiles are available, as in the TileJSON spec.+ *+ * @implSpec Optional number. Defaults to <code>0</code>.+ */+ protected Integer minzoom;+ /**+ * Maximum zoom level for which tiles are available, as in the TileJSON spec. Data from tiles at the maxzoom are used when displaying the map at higher zoom levels.+ *+ * @implSpec Optional number. Defaults to <code>22</code>.+ */+ protected Integer maxzoom;+ /**+ * Contains an attribution to be displayed when the map is shown to a user.+ *+ * @implSpec Optional string.+ */+ protected String attribution;+ /**+ * The custom property enables users to specify the tile server is not part of OpenRemote.+ *+ * The Maplibre sources follow the TileJSON specification. The TileJSON specification allows additional properties not in the spec.+ * Those properties MUST be ignored by MapLibre, but can be used in OpenRemote.+ *+ * @see https://github.com/mapbox/tilejson-spec/blob/master/3.0.0/README.md#3-structure+ */+ protected boolean custom;+}diff --git a/model/src/main/java/org/openremote/model/map/MapResource.java b/model/src/main/java/org/openremote/model/map/MapResource.javaindex 08cd05277..e2d1c6c72 100644--- a/model/src/main/java/org/openremote/model/map/MapResource.java+++ b/model/src/main/java/org/openremote/model/map/MapResource.java@@ -23,15 +23,14 @@ import com.fasterxml.jackson.databind.node.ObjectNode;import io.swagger.v3.oas.annotations.Operation;import io.swagger.v3.oas.annotations.tags.Tag;import org.openremote.model.Constants;+import jakarta.ws.rs.core.Response;import org.openremote.model.http.RequestParams;-import org.openremote.model.manager.MapRealmConfig;+import org.openremote.model.manager.MapConfig;import jakarta.annotation.security.RolesAllowed;import jakarta.ws.rs.*;import jakarta.ws.rs.core.MediaType;-import java.util.Map;-@Tag(name = "Map", description = "Operations on maps")@Path("map")public interface MapResource {@@ -44,7 +43,7 @@ public interface MapResource {@Produces(MediaType.APPLICATION_JSON)@RolesAllowed({Constants.WRITE_ADMIN_ROLE})@Operation(operationId = "saveSettings", summary = "Update map settings")- Object saveSettings(@BeanParam RequestParams requestParams, Map<String, MapRealmConfig> mapConfig);+ Object saveSettings(@BeanParam RequestParams requestParams, MapConfig mapConfig);/*** Returns style used to initialise Mapbox GL@@ -73,4 +72,35 @@ public interface MapResource {@Path("tile/{zoom}/{column}/{row}")@Operation(operationId = "getTile", summary = "Retrieve the vector tile data for Mapbox GL")byte[] getTile(@PathParam("zoom")int zoom, @PathParam("column")int column, @PathParam("row")int row);++ /**+ * Saves mbtiles file+ */+ @POST+ @Path("upload")+ @Consumes(MediaType.APPLICATION_OCTET_STREAM)+ @Produces(MediaType.APPLICATION_JSON)+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})+ @Operation(operationId = "uploadMap", summary = "Saves mbtiles file")+ ObjectNode uploadMap(@BeanParam RequestParams requestParams, @QueryParam("filename") String filename);++ /**+ * Retrieve if the map is custom and custom map limit+ */+ @GET+ @Path("getCustomMapInfo")+ @Produces(MediaType.APPLICATION_JSON)+ @RolesAllowed({Constants.READ_ADMIN_ROLE})+ @Operation(operationId = "getCustomMapInfo", summary = "Retrieve if the map is custom and custom map limit")+ ObjectNode getCustomMapInfo();++ /**+ * Removes mbtiles file+ */+ @DELETE+ @Path("deleteMap")+ @Produces(MediaType.APPLICATION_JSON)+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})+ @Operation(operationId = "deleteMap", summary = "Removes mbtiles file")+ ObjectNode deleteMap(@BeanParam RequestParams requestParams);}diff --git a/profile/deploy.yml b/profile/deploy.ymlindex d37fbb2ba..d410c19ac 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -271,6 +271,9 @@ services:# manager's map, as well as other style details and colours.OR_MAP_SETTINGS_PATH: ${OR_MAP_SETTINGS_PATH:-/deployment/map/mapsettings.json}+ # Override the maximum allowed custom map tiles database file size in bytes.+ OR_CUSTOM_MAP_SIZE_LIMIT: ${OR_CUSTOM_MAP_SIZE_LIMIT:-30000000}+# Set the tileserver host name and port; the manager will reverse proxy to this server# to provide raster map tiles to frontend appsOR_MAP_TILESERVER_HOST:diff --git a/ui/app/manager/src/components/configuration/or-conf-map/or-conf-map-card.ts b/ui/app/manager/src/components/configuration/or-conf-map/or-conf-map-card.tsindex 52a537706..d92fb3747 100644--- a/ui/app/manager/src/components/configuration/or-conf-map/or-conf-map-card.ts+++ b/ui/app/manager/src/components/configuration/or-conf-map/or-conf-map-card.ts@@ -17,7 +17,7 @@* You should have received a copy of the GNU Affero General Public License* along with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { css, html, LitElement } from "lit";+import { css, html, TemplateResult, LitElement } from "lit";import { customElement, property, state } from "lit/decorators.js";import { when } from 'lit/directives/when.js';import "@openremote/or-components/or-file-uploader";@@ -33,66 +33,9 @@ import "./or-conf-map-geojson";export class OrConfMapCard extends LitElement {static styles = css`- #remove-map {- margin: 12px 0 0 0;- }-- .subheader {- padding: 10px 0 4px;- font-weight: bolder;- }-- .d-inline-flex {- display: inline-flex;- }-- .panel-content {- padding: 0 24px 24px;- }-- .description {- font-size: 12px;- }-or-collapsible-panel {margin-bottom: 10px;}-- .boundary-container {- width: 50%;- }-- .boundary-item, .input {- width: 100%;- max-width: 800px;- padding: 10px 0;- }-- .input or-mwc-input:not([icon]) {- width: 80%;- }-- .zoom-group {- width: 50%;- padding-left: 12px;- }-- .map-container {- width: 100%- }- .settings-container{- display: inline-flex;- }-- @media screen and (max-width: 768px) {- .zoom-group, .boundary-container{- width: 100%;- padding: unset;- }- .settings-container{- display: block;- }- }`;@property({ attribute: false })@@ -187,129 +130,190 @@ export class OrConfMapCard extends LitElement {this.zoom = nr;}-- render() {+ protected async getMapContentTemplate(map: MapRealmConfig): Promise<TemplateResult> {return html`- <or-collapsible-panel .expanded="${this.expanded}">- <div slot="header" class="header-container">- ${this.name}+ <style>+ #remove-map {+ margin: 12px 0 0 0;+ }++ .subheader {+ padding: 10px 0 4px;+ font-weight: bolder;+ }++ .d-inline-flex {+ display: inline-flex;+ }++ .panel-content {+ padding: 0 24px 24px;+ }++ .description {+ font-size: 12px;+ }++ .boundary-container {+ width: 50%;+ }++ .boundary-item, .input {+ width: 100%;+ max-width: 800px;+ padding: 10px 0;+ }++ .input or-mwc-input:not([icon]) {+ width: 80%;+ }++ .zoom-group {+ width: 50%;+ padding-left: 12px;+ }++ .map-container {+ width: 100%+ }+ .settings-container{+ display: inline-flex;+ }++ @media screen and (max-width: 768px) {+ .zoom-group, .boundary-container{+ width: 100%;+ padding: unset;+ }+ .settings-container{+ display: block;+ }+ }+ </style>+ <div slot="content" class="panel-content">+ <div class="map-container">+ <div class="subheader">${i18next.t("map")}</div>+ <or-map id="vectorMap" .showBoundaryBoxControl="${true}" .zoom="${this.zoom}"+ .boundary="${map.bounds}"+ @or-map-long-press="${(ev: OrMapLongPressEvent) => {+ this.setCenter(ev.detail.lngLat);+ }}" .showGeoCodingControl="${true}"+ .showGeoJson="${map.geoJson != undefined}"+ .geoJson="${map.geoJson}"+ .useZoomControl="${false}"+ style="height: 500px; width: 100%;">+ ${when(map.center, () => html`+ <or-map-marker id="geo-json-point-marker" .lng="${map.center[0]}" .lat="${map.center[1]}"></or-map-marker>+ `)}+ </or-map></div>- <div slot="content" class="panel-content">- <div class="map-container">- <div class="subheader">${i18next.t("map")}</div>- <or-map id="vectorMap" .showBoundaryBoxControl="${true}" .zoom="${this.zoom}"- .boundary="${this.map.bounds}"- @or-map-long-press="${(ev: OrMapLongPressEvent) => {- this.setCenter(ev.detail.lngLat);- }}" .showGeoCodingControl="${true}"- .showGeoJson="${this.map.geoJson != undefined}"- .geoJson="${this.map.geoJson}"- .useZoomControl="${false}"- style="height: 500px; width: 100%;">- ${when(this.map.center, () => html`- <or-map-marker id="geo-json-point-marker" .lng="${this.map.center[0]}" .lat="${this.map.center[1]}"></or-map-marker>- `)}- </or-map>++ <div class="settings-container">+ <div class="boundary-container">+ <div class="subheader">${i18next.t("configuration.mapBounds")}</div>+ <span>${i18next.t("configuration.mapBoundsDescription")}</span>+ <or-mwc-input .value="${map?.bounds[3]}" .type="${InputType.NUMBER}" .label="${i18next.t("north")}"+ class="boundary-item"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(3, e.detail.value)}"+ .step="${.01}"></or-mwc-input>+ <or-mwc-input .value="${map?.bounds[2]}" .type="${InputType.NUMBER}" .label="${i18next.t("east")}"+ class="boundary-item"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(2, e.detail.value)}"+ .step="${.01}"></or-mwc-input>+ <or-mwc-input .value="${map?.bounds[1]}" .type="${InputType.NUMBER}" .label="${i18next.t("south")}"+ class="boundary-item"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(1, e.detail.value)}"+ .step="${.01}"></or-mwc-input>+ <or-mwc-input .value="${map?.bounds[0]}" .type="${InputType.NUMBER}" .label="${i18next.t("west")}"+ class="boundary-item"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(0, e.detail.value)}"+ .step="${.01}"></or-mwc-input>+ <div class="subheader">${i18next.t("configuration.center")}</div>+ <span>${i18next.t("configuration.centerDescription")}</span>+ <or-mwc-input .value="${Array.isArray(map.center) ? map.center.join() : undefined}"+ .type="${InputType.TEXT}" label="${i18next.t("configuration.center")}"+ class="boundary-item"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setCenter(e.detail.value)}"+ .step="${.01}"></or-mwc-input>+ <div class="subheader">${i18next.t("configuration.geoJson")}</div>+ <span>${i18next.t("configuration.geoJsonDescription")}</span>+ <div class="input" style="height: 56px; display: flex; align-items: center;">+ <or-conf-map-geojson .geoJson="${map.geoJson}" @update="${(e: CustomEvent) => {+ map.geoJson = e.detail.value;+ this.requestUpdate();+ this.notifyConfigChange(map);+ }}"></or-conf-map-geojson>+ </div></div>- <div class="settings-container">- <div class="boundary-container">- <div class="subheader">${i18next.t("configuration.mapBounds")}</div>- <span>${i18next.t("configuration.mapBoundsDescription")}</span>- <or-mwc-input .value="${this.map?.bounds[3]}" .type="${InputType.NUMBER}" .label="${i18next.t("north")}"- class="boundary-item"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(3, e.detail.value)}"- .step="${.01}"></or-mwc-input>- <or-mwc-input .value="${this.map?.bounds[2]}" .type="${InputType.NUMBER}" .label="${i18next.t("east")}"- class="boundary-item"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(2, e.detail.value)}"- .step="${.01}"></or-mwc-input>- <or-mwc-input .value="${this.map?.bounds[1]}" .type="${InputType.NUMBER}" .label="${i18next.t("south")}"- class="boundary-item"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(1, e.detail.value)}"- .step="${.01}"></or-mwc-input>- <or-mwc-input .value="${this.map?.bounds[0]}" .type="${InputType.NUMBER}" .label="${i18next.t("west")}"- class="boundary-item"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setBoundary(0, e.detail.value)}"- .step="${.01}"></or-mwc-input>- <div class="subheader">${i18next.t("configuration.center")}</div>- <span>${i18next.t("configuration.centerDescription")}</span>- <or-mwc-input .value="${Array.isArray(this.map.center) ? this.map.center.join() : undefined}"- .type="${InputType.TEXT}" label="${i18next.t("configuration.center")}"- class="boundary-item"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setCenter(e.detail.value)}"- .step="${.01}"></or-mwc-input>- <div class="subheader">${i18next.t("configuration.geoJson")}</div>- <span>${i18next.t("configuration.geoJsonDescription")}</span>- <div class="input" style="height: 56px; display: flex; align-items: center;">- <or-conf-map-geojson .geoJson="${this.map.geoJson}" @update="${(e: CustomEvent) => {- this.map.geoJson = e.detail.value;- this.requestUpdate();- this.notifyConfigChange(this.map);- }}"></or-conf-map-geojson>- </div>+ <div class="zoom-group">+ <div class="subheader">${i18next.t("configuration.mapZoom")}</div>+ <span>${i18next.t("configuration.mapZoomDescription")}</span>+ <div class="input">+ <or-mwc-input .value="${map.zoom}" .type="${InputType.NUMBER}" .label="${i18next.t('default')}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {+ map.zoom = e.detail.value;+ this.notifyConfigChange(map);+ }}"+ .step="${1}"></or-mwc-input>+ <or-mwc-input .type="${InputType.BUTTON}" icon="eye"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(map.zoom)}"+ .step="${1}"></or-mwc-input>+ </div>+ <div class="input">+ <or-mwc-input .value="${map.minZoom}" .type="${InputType.NUMBER}"+ .label="${i18next.t("configuration.minZoom")}"+ max="${map.maxZoom}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {+ map.minZoom = e.detail.value;+ this.requestUpdate();+ this.notifyConfigChange(map);+ }}"+ .step="${1}"></or-mwc-input>+ <or-mwc-input .type="${InputType.BUTTON}" icon="eye"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(map.minZoom)}"+ .step="${1}"></or-mwc-input>+ </div>+ <div class="input">+ <or-mwc-input .value="${map.maxZoom}" .type="${InputType.NUMBER}"+ .label="${i18next.t("configuration.maxZoom")}"+ min="${map.minZoom}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {+ map.maxZoom = e.detail.value;+ this.requestUpdate("map");+ this.notifyConfigChange(map);+ }}"+ .step="${1}"></or-mwc-input>+ <or-mwc-input .type="${InputType.BUTTON}" icon="eye"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(map.maxZoom)}"+ .step="${1}"></or-mwc-input></div>- <div class="zoom-group">- <div class="subheader">${i18next.t("configuration.mapZoom")}</div>- <span>${i18next.t("configuration.mapZoomDescription")}</span>- <div class="input">- <or-mwc-input .value="${this.map.zoom}" .type="${InputType.NUMBER}" .label="${i18next.t('default')}"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => {- this.map.zoom = e.detail.value;- this.notifyConfigChange(this.map);- }}"- .step="${1}"></or-mwc-input>- <or-mwc-input .type="${InputType.BUTTON}" icon="eye"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(this.map.zoom)}"- .step="${1}"></or-mwc-input>- </div>- <div class="input">- <or-mwc-input .value="${this.map.minZoom}" .type="${InputType.NUMBER}"- .label="${i18next.t("configuration.minZoom")}"- max="${this.map.maxZoom}"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => {- this.map.minZoom = e.detail.value;- this.requestUpdate();- this.notifyConfigChange(this.map);- }}"- .step="${1}"></or-mwc-input>- <or-mwc-input .type="${InputType.BUTTON}" icon="eye"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(this.map.minZoom)}"- .step="${1}"></or-mwc-input>- </div>- <div class="input">- <or-mwc-input .value="${this.map.maxZoom}" .type="${InputType.NUMBER}"- .label="${i18next.t("configuration.maxZoom")}"- min="${this.map.minZoom}"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => {- this.map.maxZoom = e.detail.value;- this.requestUpdate("map");- this.notifyConfigChange(this.map);- }}"- .step="${1}"></or-mwc-input>- <or-mwc-input .type="${InputType.BUTTON}" icon="eye"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => this.setZoom(this.map.maxZoom)}"- .step="${1}"></or-mwc-input>- </div>-- <div class="input" style="height: 56px;">- <or-mwc-input .value="${this.map.boxZoom}" .type="${InputType.SWITCH}" label="BoxZoom"- @or-mwc-input-changed="${(e: OrInputChangedEvent) => {- this.map.boxZoom = e.detail.value;- this.notifyConfigChange(this.map);- }}"- .step="${1}"></or-mwc-input>- </div>+ <div class="input" style="height: 56px;">+ <or-mwc-input .value="${map.boxZoom}" .type="${InputType.SWITCH}" label="BoxZoom"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {+ map.boxZoom = e.detail.value;+ this.notifyConfigChange(map);+ }}"+ .step="${1}"></or-mwc-input></div></div>-- ${when(this.canRemove, () => html`- <or-mwc-input outlined .type="${InputType.BUTTON}" id="remove-map"- label="configuration.deleteMapCustomization"- @click="${() => { this._showRemoveMapDialog(); }}"- ></or-mwc-input>- `)}+ </div>++ ${when(this.canRemove, () => html`+ <or-mwc-input outlined .type="${InputType.BUTTON}" id="remove-map"+ label="configuration.deleteMapCustomization"+ @click="${() => { this._showRemoveMapDialog(); }}"+ ></or-mwc-input>+ `)}+ </div>`+ }++ render() {+ return html`+ <or-collapsible-panel .expanded="${this.expanded}" .lazycontent=${this.getMapContentTemplate(this.map)}>+ <div slot="header" class="header-container">+ ${this.name}</div></or-collapsible-panel>`;diff --git a/ui/app/manager/src/components/configuration/or-conf-panel.ts b/ui/app/manager/src/components/configuration/or-conf-panel.tsindex b956358dc..6b23eeb97 100644--- a/ui/app/manager/src/components/configuration/or-conf-panel.ts+++ b/ui/app/manager/src/components/configuration/or-conf-panel.ts@@ -2,7 +2,7 @@ import {html, LitElement, TemplateResult} from "lit";import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";import {customElement, property} from "lit/decorators.js";import {when} from 'lit/directives/when.js';-import {ManagerAppConfig} from "@openremote/model";+import {ManagerAppConfig, MapConfig} from "@openremote/model";import {DialogAction, OrMwcDialog, showDialog} from "@openremote/or-mwc-components/or-mwc-dialog";import {i18next} from "@openremote/or-translate";import "@openremote/or-components/or-loading-indicator";@@ -15,7 +15,7 @@ import {OrConfMapCard} from "./or-conf-map/or-conf-map-card";export class OrConfPanel extends LitElement {@property()- public config?: {[id: string]: any} | ManagerAppConfig = {};+ public config?: MapConfig | ManagerAppConfig = {};@property()public realmOptions: { name: string, displayName: string, canDelete: boolean }[] = [];@@ -34,23 +34,26 @@ export class OrConfPanel extends LitElement {if(this.isManagerConfig(config)) {return config.realms;} else if(this.isMapConfig(config)) {- return config;+ return config.options;}return undefined;}- protected isMapConfig(object: unknown): boolean {- const keys = Object.keys(object);- // Instead of looking for the exact same entries, ensure that at least one of them can be mapped.- // This also shows realm configs that don't have parent realms, but are still existing in the mapsettings.json- return this.realmOptions.filter((o) => keys.includes(o.name)).length > 0;+ protected isMapConfig(object: unknown): object is MapConfig {+ if (typeof object === 'object' && 'options' in object) {+ const keys = Object.keys(object.options);+ // Instead of looking for the exact same entries, ensure that at least one of them can be mapped.+ // This also shows realm configs that don't have parent realms, but are still existing in the mapsettings.json+ return this.realmOptions.filter((o) => keys.includes(o.name)).length > 0;+ }+ return false;}protected isManagerConfig(object: any): object is ManagerAppConfig {return 'realms' in object;}- protected notifyConfigChange(config: {[id: string]: any} | ManagerAppConfig) {+ protected notifyConfigChange(config: MapConfig | ManagerAppConfig) {this.dispatchEvent(new CustomEvent("change", { detail: config }));}@@ -118,7 +121,7 @@ export class OrConfPanel extends LitElement {// Filter the list of realms that are not present in the config.// Most used for the "add realm" dialog, to hide the realms that are already present.- protected getAvailableRealms(config?: ManagerAppConfig | {[id: string]: any}, realmOptions?: { name: string, displayName: string, canDelete: boolean }[]): { name: string, displayName: string, canDelete: boolean }[] {+ protected getAvailableRealms(config?: ManagerAppConfig | MapConfig, realmOptions?: { name: string, displayName: string, canDelete: boolean }[]): { name: string, displayName: string, canDelete: boolean }[] {const realms = this.getRealmsProperty(config);if(realms) {return realmOptions.filter((r) => {diff --git a/ui/app/manager/src/pages/page-configuration.ts b/ui/app/manager/src/pages/page-configuration.tsindex b5f93aebf..e23998a32 100644--- a/ui/app/manager/src/pages/page-configuration.ts+++ b/ui/app/manager/src/pages/page-configuration.ts@@ -29,13 +29,14 @@ import "@openremote/or-components/or-collapsible-panel";import "@openremote/or-mwc-components/or-mwc-input";import "../components/configuration/or-conf-json";import "../components/configuration/or-conf-panel";-import {ManagerAppConfig, MapRealmConfig, Realm} from "@openremote/model";+import {ManagerAppConfig, MapConfig, Realm} from "@openremote/model";import {i18next} from "@openremote/or-translate";import "@openremote/or-components/or-loading-indicator";import {OrConfRealmCard} from "../components/configuration/or-conf-realm/or-conf-realm-card";import {OrConfPanel} from "../components/configuration/or-conf-panel";-import { InputType } from "@openremote/or-mwc-components/or-mwc-input";+import { InputType, OrInputChangedEvent } from "@openremote/or-mwc-components/or-mwc-input";import {DefaultHeaderMainMenu, DefaultHeaderSecondaryMenu, DefaultRealmConfig} from "../index";+import { showSnackbar } from "@openremote/or-mwc-components/or-mwc-snackbar";declare const APP_VERSION: string;@@ -142,6 +143,59 @@ export class PageConfiguration extends Page<AppStateKeyed> {display: none;}}++ /* Global map settings */++ .subheader {+ padding: 10px 0 4px;+ font-weight: bolder;+ }++ .global-settings-container {+ display: flex;+ }++ .global-server-group {+ width: 50%;+ }++ .custom-tile-group {+ width: 50%;+ padding-left: 12px;+ }++ @media screen and (max-width: 768px) {+ .custom-tile-group, .global-server-group {+ width: 100%;+ padding: unset;+ }+ .global-settings-container {+ display: block;+ }+ }++ .input {+ width: 100%;+ max-width: 800px;+ padding: 10px 0;+ }++ .input or-mwc-input:not([icon]) {+ width: 80%;+ }++ .note {+ color: rgba(0, 0, 0, 0.6);+ }++ or-file-uploader {+ width: 108px;+ height: 108px;+ }++ .d-inline-flex {+ display: inline-flex;+ }`;}@@ -153,7 +207,7 @@ export class PageConfiguration extends Page<AppStateKeyed> {public managerConfiguration?: ManagerAppConfig;@state()- public mapConfig?: {[id: string]: any};+ public mapConfig?: MapConfig;@state()protected realms?: Realm[];@@ -167,6 +221,18 @@ export class PageConfiguration extends Page<AppStateKeyed> {@state()protected mapConfigChanged = false;+ @state()+ protected customMapFilename: string;++ @state()+ protected customMapLimit: number = 1e+9;++ @state()+ protected tilesForUpload: File;++ @state()+ protected tilesForDeletion: boolean = false;+@query("#managerConfig-panel")protected realmConfigPanel?: OrConfPanel;@@ -176,6 +242,18 @@ export class PageConfiguration extends Page<AppStateKeyed> {public stateChanged(state: AppStateKeyed) {}+ public async firstUpdated() {+ const response = await manager.rest.api.MapResource.getCustomMapInfo();+ this.customMapLimit = response.data.limit as number;+ this.customMapFilename = response.data.filename as string | null;+ }++ public humanReadableBytes(bytes: number) {+ const unit = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];+ const exponent = Math.floor(Math.log(bytes) / Math.log(1000));+ return (bytes / Math.pow(1000, exponent)).toFixed(2) + " " + unit[exponent];+ }+// On every update..willUpdate(changedProps: PropertyValues<this>) {@@ -226,9 +304,10 @@ export class PageConfiguration extends Page<AppStateKeyed> {${when(this.loading, () => html`<or-loading-indicator></or-loading-indicator>`, () => {+ const saveDisabled = !this.managerConfigurationChanged && !this.mapConfigChanged && !this.tilesForUpload && !this.tilesForDeletion;const realmHeading = html`<div id="heading" style="justify-content: space-between;">- <span style="margin: 0;">${i18next.t("configuration.realmStyling").toUpperCase()}</span>+ <span style="margin: 0;"><or-translate style="text-transform: uppercase;" value="configuration.realmStyling"></or-translate></span><or-conf-json .managerConfig="${this.managerConfiguration}" class="hide-mobile"@saveLocalManagerConfig="${(ev: CustomEvent) => {this.managerConfiguration = ev.detail.value as ManagerAppConfig;@@ -237,6 +316,11 @@ export class PageConfiguration extends Page<AppStateKeyed> {></or-conf-json></div>`;+ const mapHeading = html`+ <div id="heading" style="justify-content: space-between;">+ <span style="margin: 0;"><or-translate style="text-transform: uppercase;" value="configuration.mapSettings"></or-translate></span>+ </div>+ `;const realmOptions = this.realms?.map((r) => ({name: r.name, displayName: r.displayName, canDelete: true}));realmOptions.push({name: 'default', displayName: 'Default', canDelete: false});return html`@@ -244,10 +328,10 @@ export class PageConfiguration extends Page<AppStateKeyed> {<div id="header-wrapper"><div id="header-title"><or-icon icon="palette-outline"></or-icon>- ${i18next.t("appearance")}+ <or-translate value="appearance"></or-translate></div><div id="header-actions">- <or-mwc-input id="save-btn" .disabled="${!this.managerConfigurationChanged && !this.mapConfigChanged}" raised type="button" label="save"+ <or-mwc-input id="save-btn" .disabled="${saveDisabled}" raised type="button" label="save"@or-mwc-input-changed="${() => this.saveAllConfigs(this.managerConfiguration, this.mapConfig)}"></or-mwc-input></div>@@ -259,7 +343,7 @@ export class PageConfiguration extends Page<AppStateKeyed> {></or-conf-panel>`, () => html`<div class="notFound-container">- <span>${i18next.t('configuration.managerConfigNotFound')}</span>+ <span><or-translate value="configuration.managerConfigNotFound"></or-translate></span><or-mwc-input type="${InputType.BUTTON}" label="configuration.tryAgain"@or-mwc-input-changed="${() => this.getManagerConfig().then(val => {this.managerConfiguration = val;@@ -268,14 +352,55 @@ export class PageConfiguration extends Page<AppStateKeyed> {</div>`)}</or-panel>- <or-panel .heading="${i18next.t("configuration.mapSettings").toUpperCase()}">+ <or-panel .heading="${mapHeading}">${when(this.mapConfig, () => html`+ <div class="global-settings-container">+ <div class="global-server-group">+ <div class="subheader"><or-translate value="configuration.global.tileServer"></or-translate></div>+ <span>+ <or-translate value="configuration.global.tileServerDescription"></or-translate><br>+ <or-translate style="font-style: italic;" class="note" value="configuration.global.tileServerNote"></or-translate>+ </span>+ <or-mwc-input class="input"+ .value="${this.mapConfig.sources?.vector_tiles?.custom ? this.mapConfig.sources?.vector_tiles?.tiles?.[0] : undefined}"+ .type="${InputType.URL}"+ .label="${i18next.t("configuration.global.tileServerPlaceholder")}"+ placeholder="https://api.example.com/tileset/{z}/{x}/{y}"+ @or-mwc-input-changed="${this.setCustomVectorTilesUrl}"+ ></or-mwc-input>+ </div>++ <div class="custom-tile-group">+ <div class="subheader"><or-translate value="configuration.global.mapTiles"></or-translate></div>+ <span>+ <or-translate value="configuration.global.uploadMapTiles"></or-translate><br>+ <or-translate style="font-style: italic;" class="note" value="configuration.global.uploadMapTilesPrecedence"+ .options=${{customMapLimit: this.humanReadableBytes(this.customMapLimit)}}+ ></or-translate>+ </span>+ <div class="input d-inline-flex" style="height: 56px">+ <div id="fileupload" style="display: flex; align-items: center">+ <or-mwc-input outlined label="selectFile" style="width: fit-content; padding-right: 12px;" .type="${InputType.BUTTON}" @or-mwc-input-changed="${+ () => this.shadowRoot.getElementById('fileupload-elem').click()+ }">+ <input id="fileupload-elem" name="configfile" type="file" accept=".mbtiles" @change="${(e) => this.uploadCustomMap(e)}"/>+ </or-mwc-input>+ <or-mwc-input id="filename-elem" style="width: unset" .value="${this.customMapFilename}" .label="${i18next.t("file")}" .type="${InputType.TEXT}" disabled>+ </or-mwc-input>+ ${when(this.customMapFilename, () => html`<or-mwc-input type="${InputType.BUTTON}" iconColor="black" icon="delete"+ @or-mwc-input-changed="${async () => await this.deleteCustomMap()}"+ ></or-mwc-input>`)}+ </div>+ </div>+ </div>+ </div>+ <div class="subheader"><or-translate value="configuration.realmMapSettingsTitle"></or-translate></div><or-conf-panel id="mapConfig-panel" .config="${this.mapConfig}" .realmOptions="${realmOptions}"@change="${() => { this.mapConfigChanged = true; }}"></or-conf-panel>`, () => html`<div class="notFound-container">- <span>${i18next.t('configuration.mapSettingsNotFound')}</span>+ <span><or-translate value="configuration.mapSettingsNotFound"></or-translate></span><or-mwc-input type="${InputType.BUTTON}" label="configuration.tryAgain"@or-mwc-input-changed="${() => this.getMapConfig().then(val => {this.mapConfig = val;@@ -298,7 +423,34 @@ export class PageConfiguration extends Page<AppStateKeyed> {// FETCH METHODS- protected async getManagerConfig(): Promise<ManagerAppConfig> {+ protected async setCustomVectorTilesUrl(e: OrInputChangedEvent) {+ this.mapConfig.sources.vector_tiles = {+ type: "vector",+ ...(e.detail.value ? { tiles: [e.detail.value], custom: true } : {}),+ }+ this.mapConfigChanged = true;+ }++ protected async uploadCustomMap(e: CustomEvent) {+ const file = (e.target as HTMLInputElement).files[0];+ if (file.size > this.customMapLimit) {+ showSnackbar(undefined, "configuration.global.uploadMapTilesError")+ return;+ }+ const filenameEl = this.shadowRoot.getElementById('filename-elem') as HTMLInputElement+ if (filenameEl) {+ filenameEl.value = file.name;+ }+ this.tilesForUpload = file;+ this.tilesForDeletion = false;+ }++ protected async deleteCustomMap() {+ this.customMapFilename = undefined;+ this.tilesForDeletion = true;+ }++ protected async getManagerConfig(): Promise<ManagerAppConfig | undefined> {const response = await manager.rest.api.ConfigurationResource.getManagerConfig();return response.status === 200 ? response.data as ManagerAppConfig : {realms: {@@ -310,28 +462,32 @@ export class PageConfiguration extends Page<AppStateKeyed> {};}- protected async getMapConfig(): Promise<{[id: string]: any}> {+ protected async getMapConfig(): Promise<MapConfig> {const response = await manager.rest.api.MapResource.getSettings();- return (response.data.options as {[id: string]: any});+ if (response.data) {+ const { options, sources } = response.data as MapConfig;+ return { options, sources };+ }+ return null;}protected async getAccessibleRealms(): Promise<Realm[]> {return (await manager.rest.api.RealmResource.getAccessible()).data;}- protected saveAllConfigs(config: ManagerAppConfig, mapConfig: {[p: string]: MapRealmConfig}) {+ protected saveAllConfigs(config: ManagerAppConfig, mapConfig: MapConfig) {this.loading = true;let managerPromise;// Save the images to the server that have been uploaded by the user.// TODO: Optimize code so it only saves images that have been changed.- const imagePromises = [];+ const filePromises = [];if(this.realmConfigPanel !== undefined) {const elems = this.realmConfigPanel.getCardElements() as OrConfRealmCard[];elems.forEach((elem, index) => {const files = elem?.getFiles();Object.entries(files).forEach(async ([x, y]) => {- imagePromises.push(+ filePromises.push(manager.rest.api.ConfigurationResource.fileUpload(y, {path: (y as any).path}).then(file =>{config.realms[elem.name][x] = file.data;})@@ -352,16 +508,43 @@ export class PageConfiguration extends Page<AppStateKeyed> {});}+ if (this.tilesForUpload) {+ showSnackbar(undefined, "configuration.global.uploadingMapTiles");+ const filename = this.tilesForUpload.name;+ filePromises.push(manager.rest.api.MapResource.uploadMap({ filename }, {+ headers: {'Content-Type': 'application/octet-stream'},+ data: this.tilesForUpload+ }).then(({ data }) => {+ this.customMapFilename = filename;+ this.mapConfig = data as MapConfig;+ }).catch((reason) => {+ showSnackbar(undefined, "configuration.global.uploadingMapTilesError");+ console.error(reason);+ }).finally(() => {+ this.tilesForUpload = null;+ }));+ }- // We first wait for the imagePromises to finish, so that+ // We first wait for the filePromises to finish, so that// we can use the path returned from the backend to store to the// manager_config.- Promise.all(imagePromises).then((arr:string[]) => {+ Promise.all(filePromises).then((arr:string[]) => {// Wait for all requests to complete, then finish loading.const promises = [this.managerConfigurationChanged ? manager.rest.api.ConfigurationResource.update(config) : null,- this.mapConfigChanged ? mapPromise : null];+ this.mapConfigChanged ? mapPromise : null+ ];Promise.all(promises).finally(() => {+ // The deletion must happen after the config changes since deletion will re-center to the default map.+ if (this.tilesForDeletion && !this.tilesForUpload) {+ manager.rest.api.MapResource.deleteMap().then(({ data }) => {+ this.tilesForDeletion = false;+ this.customMapFilename = undefined;+ this.mapConfig = data as MapConfig;+ }).catch((reason) => {+ console.error(reason);+ });+ }this.requestUpdate();this.loading = false;this.managerConfigurationChanged = false;diff --git a/ui/app/shared/locales/en/or.json b/ui/app/shared/locales/en/or.jsonindex 9e1720afc..fd98d7166 100644--- a/ui/app/shared/locales/en/or.json+++ b/ui/app/shared/locales/en/or.json@@ -697,6 +697,8 @@"display": "Display","screen": "Screen","uploadFile" : "Upload file",+ "fileSelected" : "File selected",+ "fileSaved" : "File saved","thresholds": "Thresholds","threshold": "Threshold","addInsights": "Add dashboard",@@ -791,6 +793,7 @@"tryAgain": "Try again","addRealmCustomization" : "Add Realm Style","realmTitle": "Manager app title",+ "realmMapSettingsTitle": "Realm Map Settings","defaultLanguage": "Default language","images": "Images","logo": "Logo",@@ -826,7 +829,19 @@"geoJsonDescription": "Customize the GeoJSON; add points, lines and polygons to be drawn on the map.","deleteMapCustomization": "Delete map settings","deleteMapCustomizationConfirm":"Are you sure you want to delete this Map customization?",- "addMapCustomization" : "Add map settings"+ "addMapCustomization" : "Add map settings",+ "global": {+ "tileServer": "Tile Server",+ "tileServerDescription": "Configure a tile server.",+ "tileServerNote": "The map through a tile server gets priority over custom map tiles.",+ "tileServerPlaceholder": "Map Server URL",+ "mapTiles": "Custom Map Tiles",+ "uploadMapTiles": "Upload custom map tiles.",+ "uploadMapTilesPrecedence": "The maximum allowed file size is {{customMapLimit}}.",+ "uploadingMapTiles": "Uploading map tiles... This may take a moment.",+ "uploadMapTilesError": "Custom map tiles file exceeds size limit.",+ "uploadingMapTilesError": "Failed to upload map tiles"+ }},"mqttSessions": "MQTT sessions","address": "Address",diff --git a/ui/app/shared/locales/nl/or.json b/ui/app/shared/locales/nl/or.jsonindex 4c6ee395a..c9c3be1a9 100644--- a/ui/app/shared/locales/nl/or.json+++ b/ui/app/shared/locales/nl/or.json@@ -696,6 +696,8 @@"display": "Weergave","screen": "Scherm","uploadFile" : "Uploaden bestand",+ "fileSelected" : "Bestand geselecteerd",+ "fileSaved" : "Bestand opgeslagen","thresholds": "Drempelwaardes","threshold": "Drempelwaarde","addInsights": "Dashboard toevoegen"@@ -790,6 +792,7 @@"tryAgain": "Probeer opnieuw","addRealmCustomization" : "Voeg realm stijl toe","realmTitle": "Manager app titel",+ "realmMapSettingsTitle": "Realm Kaart Stijl","defaultLanguage": "Standaard taal","images": "Afbeeldingen","logo": "Logo",@@ -825,7 +828,19 @@"geoJsonDescription": "Bewerk de GeoJSON; voeg punten, lijnen, en andere vormen toe aan de kaart.","deleteMapCustomization": "Kaartinstelling verwijderen","deleteMapCustomizationConfirm":"Weet je zeker dat je deze kaart wilt verwijderen",- "addMapCustomization" : "Voeg een Kaartinstelling toe"+ "addMapCustomization" : "Voeg een Kaartinstelling toe",+ "global": {+ "tileServer": "Kaartserver",+ "tileServerDescription": "Configureer een kaartserver.",+ "tileServerNote": "De kaart van de server URL heeft voorrang op de kaarttegels.",+ "tileServerPlaceholder": "Kaartserver URL",+ "mapTiles": "Eigen Kaarttegels",+ "uploadMapTiles": "Upload aangepaste kaarttegels.",+ "uploadMapTilesPrecedence": "De maximaal toegestaande bestandsgrootte is {{customMapLimit}}.",+ "uploadingMapTiles": "Kaarttegels uploaden... Dit kan even duren.",+ "uploadMapTilesError": "Kaarttegel bestand overschrijdt de maximale bestandsgrootte.",+ "uploadingMapTilesError": "Het uploaden van kaarttegels is mislukt."+ }},"realmRole": {"admin": "Super admin"diff --git a/ui/component/or-components/src/or-collapsible-panel.ts b/ui/component/or-components/src/or-collapsible-panel.tsindex 23cde4aa5..0373fe8a6 100644--- a/ui/component/or-components/src/or-collapsible-panel.ts+++ b/ui/component/or-components/src/or-collapsible-panel.ts@@ -17,9 +17,11 @@* You should have received a copy of the GNU Affero General Public License* along with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { css, html, LitElement, unsafeCSS } from "lit";+import { css, html, LitElement, TemplateResult, unsafeCSS } from "lit";import { customElement, property, query } from "lit/decorators.js";+import { i18next } from "@openremote/or-translate";import { DefaultColor5 } from "@openremote/core";+import { until } from 'lit/directives/until.js';// language=CSSconst style = css`@@ -116,6 +118,8 @@ export class OrCollapsiblePanel extends LitElement {];}+ @property({type: Promise<TemplateResult>})+ lazycontent!: Promise<TemplateResult>;@property({type: Boolean})expanded: boolean = false;@property({type: Boolean})@@ -132,7 +136,6 @@ export class OrCollapsiblePanel extends LitElement {}render() {-return html`<div id="header" class="${this.expandable ? "expandable" : ""} ${this.expandable && this.expanded ? "expanded" : ""}" @click="${(ev:MouseEvent) => this._onHeaderClicked(ev)}">${this.expandable ? html`<or-icon icon="chevron-${this.expanded ? "down" : "right"}"></or-icon>` : ""}@@ -142,7 +145,7 @@ export class OrCollapsiblePanel extends LitElement {</span></div><div id="content" class="${this.expandable && this.expanded ? "expanded" : ""}">- <slot name="content"></slot>+ ${this.lazycontent ? this.expanded && until(this.lazycontent, html`${i18next.t('loading')}`) : html`<slot name="content"></slot>`}</div>`;}diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.tsindex d47537396..e3f35d1cb 100644--- a/ui/component/or-map/src/mapwidget.ts+++ b/ui/component/or-map/src/mapwidget.ts@@ -313,8 +313,11 @@ export class MapWidget {container: this._mapContainer,style: settings as StyleSpecification,transformRequest: (url, resourceType) => {+ // Cross-domain tile servers usually have the following headers specified "access-control-allow-methods GET", "access-control-allow-origin *", "allow GET,HEAD". The "Access-Control-Request-Headers: Authorization" may not be set e.g. with Mapbox tile servers. The CORS preflight request (OPTION) will in this case fail if the "authorization" header is being requested cross-domain. The only headers allowed are so called "simple request" headers, see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests.+ const headers = new URL(window.origin).hostname === new URL(url).hostname+ ? {Authorization: manager.getAuthorizationHeader()} : {}return {- headers: {Authorization: manager.getAuthorizationHeader()},+ headers,url};}