Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Refactor config (#18)
Browse files Browse the repository at this point in the history
* Ignore package-lock.json

* Ignore config file.

* Openhab colorscheme for map.

* Update Jimp

* Allow configuring colors.

* Implement drawing an overlay image

* Enable drawing an underlay image

* Remove unused config parameter border

* Only write config.json if it does not exist.

If the configuration file is present, but malformed,
exit with status code 1.

Fixes #11

* Move mapSettings to top level of config

If an old-style configuration is found, i.e. with mapSettings as child
of "mqtt", these settings are ignored and a warning is displayed.

Closes #17

* Updated documentation for new config structure
  • Loading branch information
SvenFestersen committed Feb 10, 2020
1 parent c586f96 commit 3cf4f8a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ A basic example configuration would look like this:

```json
{
"mapSettings": {
"drawPath": true,
"drawCharger": true,
"drawRobot": true,
"scale": 4
},
"mqtt" : {
"identifier": "rockrobo",
"topicPrefix": "valetudo",
"autoconfPrefix": "homeassistant",
"broker_url": "mqtt://user:pass@foobar.example",
"caPath": "",
"mapSettings": {
"drawPath": true,
"drawCharger": true,
"drawRobot": true,
"scale": 4
},
"mapDataTopic": "valetudo/rockrobo/map_data",
"minMillisecondsBetweenMapUpdates": 10000,
"publishMapImage": true
Expand All @@ -64,7 +64,7 @@ By default, the image data is published via MQTT to the topic set in
`mqtt.mapDataTopic`.

## Advanced Map Configuration
The appearance of the map is configured by the `mqtt.mapSettings`
The appearance of the map is configured by the `mapSettings`
object. The default settings already produce a nice map, but you can
use these advanced settings to further customize the map for better
integration into your home automation.
Expand All @@ -84,13 +84,13 @@ brevity):

```json
{
"mapSettings": {
"crop_x1": 50,
"crop_y1": 50,
"crop_x2": 250,
"crop_y2": 150
},
"mqtt" : {
"mapSettings": {
"crop_x1": 50,
"crop_y1": 50,
"crop_x2": 250,
"crop_y2": 150
},
},
"webserver": {
}
Expand All @@ -103,28 +103,28 @@ brevity):
Since the map data has a resolution of approximately 5 cm per pixel, the
resulting images have a relatively low resolution. The map is therefore
scaled up by a factor of 4 by default. The scaling factor can be
configured by setting `mqtt.mapSettings.scale` to the desired value.
configured by setting `mapSettings.scale` to the desired value.

To avoid blurred edges, the scaling is done with nearest-neighbor
interpolation.

### Colors
The map is rendered using a blueish color map by default. The colors
of the floor, hard and weak obstacles as well as the robot's path can
be set via the `mqtt.mapSettings.colors` object, e.g. (other settings
be set via the `mapSettings.colors` object, e.g. (other settings
omitted for brevity):

```json
{
"mapSettings": {
"colors": {
"floor": "transparent",
"obstacle_weak": "rgba(0,0,0,0.1)",
"obstacle_strong": "hsl(120, 20%, 50%)",
"path": "#333333"
}
},
"mqtt" : {
"mapSettings": {
"colors": {
"floor": "transparent",
"obstacle_weak": "rgba(0,0,0,0.1)",
"obstacle_strong": "hsl(120, 20%, 50%)",
"path": "#333333"
}
},
},
"webserver": {
}
Expand All @@ -145,13 +145,13 @@ for brevity):

```json
{
"mapSettings": {
"underlay_path": "/absolute/path/to/background_image.png",
"underlay_scale": 1,
"underlay_x": 0,
"underlay_y": 0
},
"mqtt" : {
"mapSettings": {
"underlay_path": "/absolute/path/to/background_image.png",
"underlay_scale": 1,
"underlay_x": 0,
"underlay_y": 0
},
},
"webserver": {
}
Expand All @@ -162,13 +162,13 @@ And, similarly for a overlay image (other settings omitted for brevity):

```json
{
"mapSettings": {
"overlay_path": "/absolute/path/to/overlay_image.png",
"overlay_scale": 1,
"overlay_x": 0,
"overlay_y": 0
},
"mqtt" : {
"mapSettings": {
"overlay_path": "/absolute/path/to/overlay_image.png",
"overlay_scale": 1,
"overlay_x": 0,
"overlay_y": 0
},
},
"webserver": {
}
Expand Down
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(conf.get("mqtt")) {
identifier: conf.get("mqtt").identifier,
topicPrefix: conf.get("mqtt").topicPrefix,
autoconfPrefix: conf.get("mqtt").autoconfPrefix,
mapSettings: conf.get("mqtt").mapSettings,
mapSettings: conf.get("mapSettings"),
mapDataTopic: conf.get("mqtt").mapDataTopic,
minMillisecondsBetweenMapUpdates: conf.get("mqtt").minMillisecondsBetweenMapUpdates,
publishMapImage: conf.get("mqtt").publishMapImage,
Expand All @@ -27,4 +27,4 @@ if(conf.get("mqtt")) {
}
} else {
console.error("Missing configuration.mqtt");
}
}
43 changes: 24 additions & 19 deletions lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,29 @@ const Jimp = require("jimp");
const Configuration = function() {
this.location = path.join(__dirname, "../config.json");
this.settings = {
"mqtt" : {
mapSettings: {
drawPath: true,
drawCharger: true,
drawRobot: true,
scale: 4,
colors: {
floor: "#0076ff",
obstacle_weak: "#6699ff",
obstacle_strong: "#52aeff",
path: "#ffffff"
}
},
mqtt : {
identifier: "rockrobo",
topicPrefix: "valetudo",
autoconfPrefix: "homeassistant",
broker_url: "mqtt://user:pass@foobar.example",
caPath: "",
mapSettings: {
drawPath: true,
drawCharger: true,
drawRobot: true,
border: 2,
scale: 4,
colors: {
floor: "#0076ff",
obstacle_weak: "#6699ff",
obstacle_strong: "#52aeff",
path: "#ffffff"
}
},
mapDataTopic: "valetudo/rockrobo/map_data",
minMillisecondsBetweenMapUpdates: 10000,
publishMapImage: true
},
"webserver": {
webserver: {
enabled: false,
port: 3000
}
Expand All @@ -44,12 +43,18 @@ const Configuration = function() {

try {
this.settings = Object.assign(this.settings, JSON.parse(fs.readFileSync(this.location)));
this.persist();
} catch(e) {
console.error("Invalid configuration file!");
console.error(e)
console.log("Writing new file using defaults");
this.persist();
console.error(e);
process.exit(1);
}

if (this.settings.mqtt.mapSettings) {
// If an old (v0.2.0 and below) configuration structure is found, i.e. mapSettings is not a top-level
// object but a child of mqtt, display a warning.
console.warn("WARNING: You are using an old configuration file structure!");
console.warn("WARNING: mapSettings must be moved to the top level.");
console.warn("WARNING: Your current map settings are ignored.");
}
} else {
console.log("No configuration file present. Creating one at:", this.location);
Expand Down
1 change: 0 additions & 1 deletion lib/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const Tools = {
drawPath: true,
drawCharger: true,
drawRobot: true,
border: 2,
scale: 4,
crop_x1: 0,
crop_x2: Number.MAX_VALUE,
Expand Down

0 comments on commit 3cf4f8a

Please sign in to comment.