Skip to content

Commit

Permalink
add new parameter - png compression value
Browse files Browse the repository at this point in the history
  • Loading branch information
erkexzcx committed Oct 10, 2023
1 parent b679e11 commit 16bae15
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
9 changes: 8 additions & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ http:

map:
# Do not render map more than once within below specified interval
min_refresh_int: 1000ms
min_refresh_int: 5000ms

# Specify compression level for Golang's PNG library:
# 0 - Best speed
# 1 - Best compression
# 2 - Default compression
# 3 - No compression
png_compression: 0

# 4 is default
scale: 4
Expand Down
30 changes: 25 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ type TopicsConfig struct {
}

type MapConfig struct {
MinRefreshInt time.Duration `yaml:"min_refresh_int"`
Scale float64 `yaml:"scale"`
RotationTimes int `yaml:"rotate"`
CustomLimits struct {
MinRefreshInt time.Duration `yaml:"min_refresh_int"`
PNGCompression int `yaml:"png_compression"`
Scale float64 `yaml:"scale"`
RotationTimes int `yaml:"rotate"`
CustomLimits struct {
StartX int `yaml:"start_x"`
StartY int `yaml:"start_y"`
EndX int `yaml:"end_x"`
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewConfig(configFile string) (*Config, error) {
}

func validate(c *Config) (*Config, error) {
// Check if any section is nil (missing)
if c.Mqtt == nil {
return nil, errors.New("missing mqtt section")
}
Expand All @@ -79,14 +81,32 @@ func validate(c *Config) (*Config, error) {
if c.Map == nil {
return nil, errors.New("missing map section")
}

if c.Mqtt.Connection == nil {
return nil, errors.New("missing mqtt.connection section")
}
if c.Mqtt.Topics == nil {
return nil, errors.New("missing mqtt.topics section")
}

// Check MQTT topics section
if c.Mqtt.Topics.ValetudoIdentifier == "" {
return nil, errors.New("missing mqtt.topics.valetudo_identifier value")
}
if c.Mqtt.Topics.ValetudoPrefix == "" {
return nil, errors.New("missing mqtt.topics.valetudo_prefix value")
}
if c.Mqtt.Topics.HaAutoconfPrefix == "" {
return nil, errors.New("missing mqtt.topics.ha_autoconf_prefix value")
}

// Check map section
if c.Map.Scale < 1 {
return nil, errors.New("missing map.scale cannot be lower than 1")
}
if c.Map.PNGCompression < 0 || c.Map.PNGCompression > 3 {
return nil, errors.New("invalid map.png_compression value")
}

// Everything else should fail when used (e.g. wrong IP/port will cause
// fatal error when starting http server)

Expand Down
16 changes: 14 additions & 2 deletions pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ type Renderer struct {
}

type Settings struct {
Scale float64
RotationTimes int
Scale float64
PNGCompression int
RotationTimes int

// Hardcoded limits for a map within robot's coordinates system
StaticStartX, StaticStartY int
StaticEndX, StaticEndY int
}

func New(s *Settings) *Renderer {
switch s.PNGCompression {
case 0:
pngEncoder.CompressionLevel = png.BestSpeed
case 1:
pngEncoder.CompressionLevel = png.BestCompression
case 2:
pngEncoder.CompressionLevel = png.DefaultCompression
case 3:
pngEncoder.CompressionLevel = png.NoCompression
}

r := &Renderer{
settings: s,
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/renderer/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"image/png"
)

var pngEncoder = png.Encoder{CompressionLevel: png.BestSpeed}
// Set compression value from 'New' function.
var pngEncoder = png.Encoder{}

type Result struct {
Image *image.Image
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ var (

func Start(c *config.Config) {
r := renderer.New(&renderer.Settings{
Scale: c.Map.Scale,
RotationTimes: c.Map.RotationTimes,
Scale: c.Map.Scale,
PNGCompression: c.Map.PNGCompression,
RotationTimes: c.Map.RotationTimes,

StaticStartX: c.Map.CustomLimits.StartX,
StaticStartY: c.Map.CustomLimits.StartY,
Expand Down

0 comments on commit 16bae15

Please sign in to comment.