Skip to content

Commit

Permalink
Move API and HTTP Client to their own packages
Browse files Browse the repository at this point in the history
Right now they are quite independent and there are no external dependencies and therefore makes sense to have them as a separate packages
  • Loading branch information
albertomontesg committed May 4, 2022
1 parent cb17e4f commit 1cb8643
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 88 deletions.
29 changes: 28 additions & 1 deletion digitalstrom/api.go → digitalstrom/api/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
package digitalstrom
package api

import "strings"

type DeviceType string

const (
Light DeviceType = "GE"
Blind DeviceType = "GR"
Joker DeviceType = "SW"
Unknown DeviceType = "Unknown"
)

type Action string

const (
MoveUp Action = "app.moveUp"
MoveDown Action = "app.moveDown"
StepUp Action = "app.stepUp"
StepDown Action = "app.stepDown"
SunProtection Action = "app.sunProtection"
Stop Action = "app.stop"
)

type ChannelType string

const (
Brightness ChannelType = "brightness"
Hue ChannelType = "hue"
)

// A Device is the smallest entity represented in the DigitalStrom system and
// represents a input/output physical device that can receive human input
// (button push) or can actuate (open light).
Expand Down
12 changes: 7 additions & 5 deletions digitalstrom/circuits.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package digitalstrom

import (
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/api"
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/client"
"github.com/gaetancollaud/digitalstrom-mqtt/utils"
"github.com/rs/zerolog/log"
)

type CircuitValueChanged struct {
Circuit Circuit
Circuit api.Circuit
ConsumptionW int64
EnergyWs int64
}

type CircuitsManager struct {
httpClient *HttpClient
circuits []Circuit
httpClient *client.HttpClient
circuits []api.Circuit
circuitValuesChan chan CircuitValueChanged
}

func NewCircuitManager(httpClient *HttpClient) *CircuitsManager {
func NewCircuitManager(httpClient *client.HttpClient) *CircuitsManager {
dm := new(CircuitsManager)
dm.httpClient = httpClient
dm.circuitValuesChan = make(chan CircuitValueChanged)
Expand Down Expand Up @@ -65,7 +67,7 @@ func (dm *CircuitsManager) UpdateCircuitsValue() {
}
}

func (dm *CircuitsManager) updateValue(circuit Circuit, newConsumptionW int64, newEnergyWs int64) {
func (dm *CircuitsManager) updateValue(circuit api.Circuit, newConsumptionW int64, newEnergyWs int64) {
dm.circuitValuesChan <- CircuitValueChanged{
Circuit: circuit,
ConsumptionW: newConsumptionW,
Expand Down
67 changes: 25 additions & 42 deletions digitalstrom/http-client.go → digitalstrom/client/http-client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package digitalstrom
package client

import (
"crypto/tls"
Expand All @@ -13,6 +13,7 @@ import (
"time"

"github.com/gaetancollaud/digitalstrom-mqtt/config"
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/api"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -135,71 +136,53 @@ func wrapApiResponse[T any](response interface{}, err error) (*T, error) {

// Methods for the specific endpoints called in DigitalStrom API.

type Action string

const (
MoveUp Action = "app.moveUp"
MoveDown Action = "app.moveDown"
StepUp Action = "app.stepUp"
StepDown Action = "app.stepDown"
SunProtection Action = "app.sunProtection"
Stop Action = "app.stop"
)

type ChannelType string

const (
Brightness ChannelType = "brightness"
Hue ChannelType = "hue"
)

func (httpClient *HttpClient) SystemLogin(user string, password string) (*TokenResponse, error) {
func (httpClient *HttpClient) SystemLogin(user string, password string) (*api.TokenResponse, error) {
params := url.Values{}
params.Set("user", user)
params.Set("password", password)
response, err := httpClient.getRequest("json/system/login", params)
return wrapApiResponse[TokenResponse](response, err)
return wrapApiResponse[api.TokenResponse](response, err)
}

func (httpClient *HttpClient) ApartmentGetCircuits() (*ApartmentGetCircuitsResponse, error) {
func (httpClient *HttpClient) ApartmentGetCircuits() (*api.ApartmentGetCircuitsResponse, error) {
response, err := httpClient.getRequestWithToken("json/apartment/getCircuits", url.Values{})
return wrapApiResponse[ApartmentGetCircuitsResponse](response, err)
return wrapApiResponse[api.ApartmentGetCircuitsResponse](response, err)
}

func (httpClient *HttpClient) ApartmentGetDevices() (*ApartmentGetDevicesResponse, error) {
func (httpClient *HttpClient) ApartmentGetDevices() (*api.ApartmentGetDevicesResponse, error) {
response, err := httpClient.getRequestWithToken("json/apartment/getDevices", url.Values{})
return wrapApiResponse[ApartmentGetDevicesResponse](response, err)
return wrapApiResponse[api.ApartmentGetDevicesResponse](response, err)
}

func (httpClient *HttpClient) CircuitGetConsumption(dsid string) (*CircuitGetConsumptionResponse, error) {
func (httpClient *HttpClient) CircuitGetConsumption(dsid string) (*api.CircuitGetConsumptionResponse, error) {
params := url.Values{}
params.Set("id", dsid)
response, err := httpClient.getRequestWithToken("json/circuit/getConsumption", params)
return wrapApiResponse[CircuitGetConsumptionResponse](response, err)
return wrapApiResponse[api.CircuitGetConsumptionResponse](response, err)
}

func (httpClient *HttpClient) CircuitGetEnergyMeterValue(dsid string) (*CircuitGetEnergyMeterValueResponse, error) {
func (httpClient *HttpClient) CircuitGetEnergyMeterValue(dsid string) (*api.CircuitGetEnergyMeterValueResponse, error) {
params := url.Values{}
params.Set("id", dsid)
response, err := httpClient.getRequestWithToken("json/circuit/getEnergyMeterValue", params)
return wrapApiResponse[CircuitGetEnergyMeterValueResponse](response, err)
return wrapApiResponse[api.CircuitGetEnergyMeterValueResponse](response, err)
}

func (httpClient *HttpClient) PropertyGetFloating(path string) (*FloatValue, error) {
func (httpClient *HttpClient) PropertyGetFloating(path string) (*api.FloatValue, error) {
params := url.Values{}
params.Set("path", path)
response, err := httpClient.getRequestWithToken("json/property/getFloating", params)
return wrapApiResponse[FloatValue](response, err)
return wrapApiResponse[api.FloatValue](response, err)
}

func (httpClient *HttpClient) ZoneGetName(zoneId int) (*ZoneGetNameResponse, error) {
func (httpClient *HttpClient) ZoneGetName(zoneId int) (*api.ZoneGetNameResponse, error) {
params := url.Values{}
params.Set("id", strconv.Itoa(zoneId))
response, err := httpClient.getRequestWithToken("json/zone/getName", params)
return wrapApiResponse[ZoneGetNameResponse](response, err)
return wrapApiResponse[api.ZoneGetNameResponse](response, err)
}

func (httpClient *HttpClient) ZoneCallAction(zoneId int, action Action) error {
func (httpClient *HttpClient) ZoneCallAction(zoneId int, action api.Action) error {
params := url.Values{}
params.Set("application", "2")
params.Set("id", strconv.Itoa(zoneId))
Expand All @@ -208,21 +191,21 @@ func (httpClient *HttpClient) ZoneCallAction(zoneId int, action Action) error {
return err
}

func (httpClient *HttpClient) ZoneSceneGetName(zoneId int, groupId int, sceneId int) (*ZoneSceneGetNameResponse, error) {
func (httpClient *HttpClient) ZoneSceneGetName(zoneId int, groupId int, sceneId int) (*api.ZoneSceneGetNameResponse, error) {
params := url.Values{}
params.Set("id", strconv.Itoa(zoneId))
params.Set("groupID", strconv.Itoa(groupId))
params.Set("sceneNumber", strconv.Itoa(sceneId))
response, err := httpClient.getRequestWithToken("json/zone/sceneGetName", params)
return wrapApiResponse[ZoneSceneGetNameResponse](response, err)
return wrapApiResponse[api.ZoneSceneGetNameResponse](response, err)
}

func (httpClient *HttpClient) ZoneGetReachableScenes(zoneId int, groupId int) (*ZoneGetReachableScenesResponse, error) {
func (httpClient *HttpClient) ZoneGetReachableScenes(zoneId int, groupId int) (*api.ZoneGetReachableScenesResponse, error) {
params := url.Values{}
params.Set("id", strconv.Itoa(zoneId))
params.Set("groupID", strconv.Itoa(groupId))
response, err := httpClient.getRequestWithToken("json/zone/getReachableScenes", params)
return wrapApiResponse[ZoneGetReachableScenesResponse](response, err)
return wrapApiResponse[api.ZoneGetReachableScenesResponse](response, err)
}

func (httpClient *HttpClient) DeviceSetOutputChannelValue(dsid string, channelValues map[string]int) error {
Expand All @@ -238,12 +221,12 @@ func (httpClient *HttpClient) DeviceSetOutputChannelValue(dsid string, channelVa
return err
}

func (httpClient *HttpClient) DeviceGetOutputChannelValue(dsid string, channels []string) (*DeviceGetOutputChannelValueResponse, error) {
func (httpClient *HttpClient) DeviceGetOutputChannelValue(dsid string, channels []string) (*api.DeviceGetOutputChannelValueResponse, error) {
params := url.Values{}
params.Set("dsid", dsid)
params.Set("channels", strings.Join(channels, ";"))
response, err := httpClient.getRequestWithToken("json/device/getOutputChannelValue", params)
return wrapApiResponse[DeviceGetOutputChannelValueResponse](response, err)
return wrapApiResponse[api.DeviceGetOutputChannelValueResponse](response, err)
}

func (httpClient *HttpClient) EventSubscribe(event string, subscriptionId int) error {
Expand All @@ -262,9 +245,9 @@ func (httpClient *HttpClient) EventUnsubscribe(event string, subscriptionId int)
return err
}

func (httpClient *HttpClient) EventGet(subscriptionId int) (*EventGetResponse, error) {
func (httpClient *HttpClient) EventGet(subscriptionId int) (*api.EventGetResponse, error) {
params := url.Values{}
params.Set("subscriptionID", strconv.Itoa(subscriptionId))
response, err := httpClient.getRequestWithToken("json/event/get", params)
return wrapApiResponse[EventGetResponse](response, err)
return wrapApiResponse[api.EventGetResponse](response, err)
}
8 changes: 4 additions & 4 deletions digitalstrom/token.go → digitalstrom/client/token.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package digitalstrom
package client

import (
"time"
Expand All @@ -12,14 +12,14 @@ type TokenManager struct {
httpClient *HttpClient
token string
lastTokenTime time.Time
tokenCounter int
TokenCounter int
}

func NewTokenManager(config *config.ConfigDigitalstrom, httpClient *HttpClient) *TokenManager {
tm := new(TokenManager)
tm.config = config
tm.httpClient = httpClient
tm.tokenCounter = 0
tm.TokenCounter = 0
return tm
}

Expand All @@ -40,7 +40,7 @@ func (tm *TokenManager) GetToken() string {
log.Debug().Dur("last token", time.Since(tm.lastTokenTime)).Msg("Refreshing token")
tm.token = tm.refreshToken()
tm.lastTokenTime = time.Now()
tm.tokenCounter++
tm.TokenCounter++
}
return tm.token
}
Expand Down
25 changes: 9 additions & 16 deletions digitalstrom/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"strings"
"time"

"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/api"
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/client"
"github.com/gaetancollaud/digitalstrom-mqtt/utils"
"github.com/rs/zerolog/log"
)

type DeviceType string

const (
Light DeviceType = "GE"
Blind DeviceType = "GR"
Joker DeviceType = "SW"
Unknown DeviceType = "Unknown"
)

type DeviceAction string

const (
Expand All @@ -27,7 +20,7 @@ const (
)

type DeviceStateChanged struct {
Device Device
Device api.Device
Channel string
NewValue float64
}
Expand All @@ -40,14 +33,14 @@ type DeviceCommand struct {
}

type DevicesManager struct {
httpClient *HttpClient
httpClient *client.HttpClient
invertBlindsPosition bool
devices []Device
devices []api.Device
deviceStateChan chan DeviceStateChanged
lastDeviceCommand time.Time
}

func NewDevicesManager(httpClient *HttpClient, invertBlindsPosition bool) *DevicesManager {
func NewDevicesManager(httpClient *client.HttpClient, invertBlindsPosition bool) *DevicesManager {
dm := new(DevicesManager)
dm.httpClient = httpClient
dm.invertBlindsPosition = invertBlindsPosition
Expand Down Expand Up @@ -100,7 +93,7 @@ func (dm *DevicesManager) updateGroup(groupId int) {
}
}

func (dm *DevicesManager) updateDevice(device Device) {
func (dm *DevicesManager) updateDevice(device api.Device) {
// device need to be updated
if len(device.OutputChannels) == 0 {
log.Debug().Str("device", device.Name).Msg("Skipping update. No output channels.")
Expand All @@ -124,7 +117,7 @@ func (dm *DevicesManager) updateDevice(device Device) {
}
}

func (dm *DevicesManager) updateValue(device Device, channel string, newValue float64) {
func (dm *DevicesManager) updateValue(device api.Device, channel string, newValue float64) {
newValue = dm.invertValueIfNeeded(channel, newValue)

// Always send new updated value to the channel. If the current value is not
Expand Down Expand Up @@ -168,7 +161,7 @@ func (dm *DevicesManager) SetValue(command DeviceCommand) error {

var err error
if command.Action == CommandStop {
err = dm.httpClient.ZoneCallAction(device.ZoneId, Stop)
err = dm.httpClient.ZoneCallAction(device.ZoneId, api.Stop)
} else {
err = dm.httpClient.DeviceSetOutputChannelValue(device.Dsid, map[string]int{c.Name: int(newValue)})
}
Expand Down
12 changes: 7 additions & 5 deletions digitalstrom/digitalstrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"time"

"github.com/gaetancollaud/digitalstrom-mqtt/config"
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/api"
"github.com/gaetancollaud/digitalstrom-mqtt/digitalstrom/client"
"github.com/rs/zerolog/log"
)

type Digitalstrom struct {
config *config.Config
cron DigitalstromCron
httpClient *HttpClient
httpClient *client.HttpClient
eventsManager *EventsManager
devicesManager *DevicesManager
circuitManager *CircuitsManager
Expand All @@ -25,7 +27,7 @@ type DigitalstromCron struct {
func New(config *config.Config) *Digitalstrom {
ds := new(Digitalstrom)
ds.config = config
ds.httpClient = NewHttpClient(&config.Digitalstrom)
ds.httpClient = client.NewHttpClient(&config.Digitalstrom)
ds.eventsManager = NewDigitalstromEvents(ds.httpClient)
ds.devicesManager = NewDevicesManager(ds.httpClient, config.InvertBlindsPosition)
ds.circuitManager = NewCircuitManager(ds.httpClient)
Expand Down Expand Up @@ -74,7 +76,7 @@ func (ds *Digitalstrom) digitalstromCron() {
}
}

func (ds *Digitalstrom) eventReceived(events chan Event) {
func (ds *Digitalstrom) eventReceived(events chan api.Event) {
for event := range events {
log.Info().
Int("SceneId", event.Properties.SceneId).
Expand Down Expand Up @@ -130,10 +132,10 @@ func (ds *Digitalstrom) refreshAllDevices() {
}
}

func (ds *Digitalstrom) GetAllDevices() []Device {
func (ds *Digitalstrom) GetAllDevices() []api.Device {
return ds.devicesManager.devices
}

func (ds *Digitalstrom) GetAllCircuits() []Circuit {
func (ds *Digitalstrom) GetAllCircuits() []api.Circuit {
return ds.circuitManager.circuits
}
Loading

0 comments on commit 1cb8643

Please sign in to comment.