Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetancollaud committed Dec 15, 2023
1 parent 04c72f2 commit 1568b48
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 88 deletions.
3 changes: 1 addition & 2 deletions config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
digitalstrom_host: 192.168.1.X
digitalstrom_username: dssadmin
digitalstrom_password: XXX
digitalstrom_api_key: XXX
mqtt_url: tcp://192.168.1.X:1883
refresh_at_start: false
2 changes: 1 addition & 1 deletion migration_v1_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You can now manage the api-key in the digitalSTROM web api under System -> Acces
Circuits don't exist anymore. They are replaced by the concept of "controllers". In addition, the power consumption
has been moved to a new entity called "metering". Thus, the MQTT interface don't expose circuits anymore but
"meterings". On top of that there is one metering for the "apartment", which basically just sum up all the meterings of
thecontrollers.
the controllers.

## Metering interval

Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/modules/meterings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
energyMeter string = "energyWh"
)

// Circuits Module encapsulates all the logic regarding the controllers. The logic
// is the following: every 30 seconds the controllers meter values are being checked and
// Meterings Module encapsulates all the logic regarding the meterings of the controllers. The logic
// is the following: every 10 seconds the meterings values are being checked and
// pushed to the corresponding topic in the MQTT server.
type MeteringsModule struct {
mqttClient mqtt.Client
Expand Down
4 changes: 4 additions & 0 deletions pkg/digitalstrom/api-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"strings"
)

/**
* This file is here only to generate a new API key for the integration. It should never be used for another purpose.
*/

type NewApiKeyRequest struct {
Data NewApiKeyRequestData `json:"data"`
}
Expand Down
9 changes: 1 addition & 8 deletions pkg/digitalstrom/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func NewClient(options *ClientOptions) Client {
}
}

// Connect retrieves the token from the server by performing the login call.
func (c *client) Connect() error {
// TODO handle reconnection
websocketHost := "ws://" + c.options.Host + ":8090/api/v1/apartment/notifications"
Expand Down Expand Up @@ -114,11 +113,9 @@ func (c *client) Connect() error {
return nil
}

// Disconnect stops all work on the It stops any running event loop,
// unsubscribe from any event in the server and closes any idle connection.
// Disconnect stops all the ongoing calls and unsubscribe from the notification websocket
func (c *client) Disconnect() error {

// Close all current connections.
c.httpClient.CloseIdleConnections()
c.websocketConnection.Close()

Expand Down Expand Up @@ -227,10 +224,6 @@ func (c *client) patchRequest(path string, body interface{}) error {
return err
}

// getRequest performs a GET request to the DigitalStrom server given the path
// and parameters. It will parse the returned message to identify errors in the
// request and return a generic interface that corresponds to the `result` item
// in the response.
func (c *client) getRequest(path string, params url.Values) (interface{}, error) {
body, err := c.doRequest(http.MethodGet, path, params, nil)
if err != nil {
Expand Down
65 changes: 8 additions & 57 deletions pkg/digitalstrom/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,25 @@ import (

// ClientOptions contains configurable options for a Digitalstrom Client.
type ClientOptions struct {
Host string
Port int
ApiKey string
MaxRetries int
RetryDuration time.Duration
EventSubscriptionId int
RunEventLoop bool
EventRequestTimeout time.Duration
Host string
Port int
ApiKey string
}

// NewClientOptions will create a new ClientClientOptions type with some
// default values.
//
// Host: dss.local
// Port: 8080
// MaxRetries: 3
// RetryDuration: 2 seconds
// EventSubscriptionId: (randomly generated)
// RunEventLoop: true
// EventRequestTimeout: 10 seconds
// Host: dss.local
// Port: 8080
func NewClientOptions() *ClientOptions {
// Random generate subscriptionId in order to not have collisions of
// multiple instances running at the same time.
rand.Seed(time.Now().UnixNano())

return &ClientOptions{
Host: "dss.local",
Port: 8080,
ApiKey: "",
MaxRetries: 3,
RetryDuration: 2 * time.Second,
EventSubscriptionId: int(rand.Int31n(1 << 20)),
RunEventLoop: true,
EventRequestTimeout: 10 * time.Second,
Host: "dss.local",
Port: 8080,
ApiKey: "",
}
}

Expand All @@ -62,37 +47,3 @@ func (o *ClientOptions) SetApiKey(u string) *ClientOptions {
o.ApiKey = u
return o
}

// SetMaxRetries will set the number of retries the client will perform when
// login errors are found. This applies to all API calls.
func (o *ClientOptions) SetMaxRetries(maxRetries int) *ClientOptions {
o.MaxRetries = maxRetries
return o
}

// SetRetryDuration will set the time the client will wait between retries.
func (o *ClientOptions) SetRetryDuration(duration time.Duration) *ClientOptions {
o.RetryDuration = duration
return o
}

// SetEventSubscriptionId will define the ID used to identify the subscription
// to the server when receiving events. Make sure the ID is unique between
// instances that are subscribing to events on the same DigitalStrom server.
func (o *ClientOptions) SetEventSubscriptionId(id int) *ClientOptions {
o.EventSubscriptionId = id
return o
}

// SetRunEventLoop will define whether a event loop is triggered to listen to
// all new events coming from the DigitalStrom server.
func (o *ClientOptions) SetRunEventLoop(b bool) *ClientOptions {
o.RunEventLoop = b
return o
}

// SetEventRequestTimeout will set the timeout for the get event requests.
func (o *ClientOptions) SetEventRequestTimeout(timeout time.Duration) *ClientOptions {
o.EventRequestTimeout = timeout
return o
}
1 change: 1 addition & 0 deletions pkg/digitalstrom/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type DeviceChangeCallback func(deviceId string, outputId string, oldValue float64, newValue float64)

// Registry The registry hold the current structure of the appartement and the latest known state
type Registry interface {
Start() error

Expand Down
18 changes: 0 additions & 18 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package utils

import (
"encoding/json"
"regexp"
"strings"

"github.com/rs/zerolog/log"
)

func CheckNoErrorAndPrint(e error) bool {
if e != nil {
log.Error().Err(e).Msg("Error")
}
return e == nil
}

func PrettyPrint(value interface{}) string {
b, err := json.Marshal(value)
if err != nil {
log.Info().Err(err).Msg("Cannot pretty print")
}
return string(b)
}

func RemoveRegexp(value string, expression string) string {
if expression == "" {
return value
Expand Down

0 comments on commit 1568b48

Please sign in to comment.