Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Dec 6, 2022
2 parents aac2e52 + 2a563b8 commit c379d19
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 79 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- dev
- main
pull_request:
workflow_call:

Expand Down Expand Up @@ -36,3 +37,8 @@ jobs:

- name: Test
run: go test -race -v -coverprofile=coverage.out -covermode=atomic ./...

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# eebus-go

![Build Status](https://github.com/enbility/eebus-go/actions/workflows/default.yml/badge.svg?branch=main)
[![Build Status](https://github.com/enbility/eebus-go/actions/workflows/default.yml/badge.svg?branch=main)](https://github.com/enbility/eebus-go/actions/workflows/default.yml/badge.svg?branch=main)
[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4)](https://godoc.org/github.com/enbility/eebus-go)
[![Coverage Status](https://coveralls.io/repos/github/enbility/eebus-go/badge.svg?branch=main)](https://coveralls.io/github/enbility/eebus-go?branch=main)
[![Go report](https://goreportcard.com/badge/github.com/enbility/eebus-go)](https://goreportcard.com/report/github.com/enbility/eebus-go)

This library provides a complete foundation for implementing [EEBUS](https://eebus.org) use cases. The use cases define various functional scenarios for different device categories, e.g. energy management systems, charging stations, heat pumps, and more.

Expand Down Expand Up @@ -238,6 +241,7 @@ Use `SetLogger` on `Service` to set the logger which needs to conform to the `lo
Example:

```go
h.myService = service.NewEEBUSService(serviceDescription, h)
configuration = service.NewConfiguration(...)
h.myService = service.NewEEBUSService(configuration, h)
h.myService.SetLogging(h)
```
8 changes: 4 additions & 4 deletions cmd/evse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func (h *evse) run() {
log.Fatal(err)
}

serviceDescription, err := service.NewServiceDescription(
configuration, err := service.NewConfiguration(
"Demo", "Demo", "EVSE", "234567890",
model.DeviceTypeTypeChargingStation, port, certificate)
model.DeviceTypeTypeChargingStation, port, certificate, 230)
if err != nil {
log.Fatal(err)
}
serviceDescription.SetAlternateIdentifier("Demo-EVSE-234567890")
configuration.SetAlternateIdentifier("Demo-EVSE-234567890")

h.myService = service.NewEEBUSService(serviceDescription, h)
h.myService = service.NewEEBUSService(configuration, h)
h.myService.SetLogging(h)

if err = h.myService.Setup(); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/hems/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func (h *hems) run() {
log.Fatal(err)
}

serviceDescription, err := service.NewServiceDescription(
configuration, err := service.NewConfiguration(
"Demo", "Demo", "HEMS", "123456789",
model.DeviceTypeTypeEnergyManagementSystem, port, certificate)
model.DeviceTypeTypeEnergyManagementSystem, port, certificate, 230)
if err != nil {
log.Fatal(err)
}
serviceDescription.SetAlternateIdentifier("Demo-HEMS-123456789")
configuration.SetAlternateIdentifier("Demo-HEMS-123456789")

h.myService = service.NewEEBUSService(serviceDescription, h)
h.myService = service.NewEEBUSService(configuration, h)
h.myService.SetLogging(h)

if err = h.myService.Setup(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions features/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ func (m *Measurement) Request() (*model.MsgCounterType, error) {
// return current value of a defined scope
func (m *Measurement) GetValueForScope(scope model.ScopeTypeType, electricalConnection *ElectricalConnection) (float64, error) {
if m.featureRemote == nil {
return 0.0, ErrDataNotAvailable
return 0, ErrDataNotAvailable
}

descRef, err := m.GetDescription()
if err != nil {
return 0.0, ErrMetadataNotAvailable
return 0, ErrMetadataNotAvailable
}

rData := m.featureRemote.Data(model.FunctionTypeMeasurementListData)
if rData == nil {
return 0.0, ErrDataNotAvailable
return 0, ErrDataNotAvailable
}
data := rData.(*model.MeasurementListDataType)

Expand Down Expand Up @@ -218,17 +218,17 @@ func (m *Measurement) GetDescriptionForScope(scope model.ScopeTypeType) (measure
// return current SoC for measurements
func (m *Measurement) GetSoC() (float64, error) {
if m.featureRemote == nil {
return 0.0, ErrDataNotAvailable
return 0, ErrDataNotAvailable
}

descRef, err := m.GetDescription()
if err != nil {
return 0.0, ErrMetadataNotAvailable
return 0, ErrMetadataNotAvailable
}

rData := m.featureRemote.Data(model.FunctionTypeMeasurementListData)
if rData == nil {
return 0.0, ErrDataNotAvailable
return 0, ErrDataNotAvailable
}
data := rData.(*model.MeasurementListDataType)

Expand All @@ -251,7 +251,7 @@ func (m *Measurement) GetSoC() (float64, error) {
}
}

return 0.0, ErrDataNotAvailable
return 0, ErrDataNotAvailable
}

type measurementConstraintMap map[model.MeasurementIdType]model.MeasurementConstraintsDataType
Expand Down
18 changes: 9 additions & 9 deletions service/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const shipWebsocketPath = "/ship/"
const shipZeroConfServiceType = "_ship._tcp"
const shipZeroConfDomain = "local."

// used for randomizing the connection initation delay
// used for randomizing the connection initiation delay
// this limits the possibility of concurrent connection attempts from both sides
type connectionInitiationDelayTimeRange struct {
// defines the minimum and maximum wait time for when to try to initate an connection
Expand Down Expand Up @@ -65,8 +65,8 @@ type connectionsHub struct {
connectionAttemptCounter map[string]int
connectionAttemptRunning map[string]bool

serviceDescription *ServiceDescription
localService *ServiceDetails
configuration *Configuration
localService *ServiceDetails

serviceProvider serviceProvider

Expand All @@ -88,21 +88,21 @@ type connectionsHub struct {
muxMdns sync.Mutex
}

func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.DeviceLocalImpl, serviceDescription *ServiceDescription, localService *ServiceDetails) (*connectionsHub, error) {
func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.DeviceLocalImpl, configuration *Configuration, localService *ServiceDetails) (*connectionsHub, error) {
hub := &connectionsHub{
connections: make(map[string]*ship.ShipConnection),
connectionAttemptCounter: make(map[string]int),
connectionAttemptRunning: make(map[string]bool),
pairedServices: make([]ServiceDetails, 0),
serviceProvider: serviceProvider,
spineLocalDevice: spineLocalDevice,
serviceDescription: serviceDescription,
configuration: configuration,
localService: localService,
}

localService.SKI = util.NormalizeSKI(localService.SKI)

mdns, err := newMDNS(localService.SKI, serviceDescription)
mdns, err := newMDNS(localService.SKI, configuration)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -251,14 +251,14 @@ func (h *connectionsHub) isSkiConnected(ski string) bool {

// start the ship websocket server
func (h *connectionsHub) startWebsocketServer() error {
addr := fmt.Sprintf(":%d", h.serviceDescription.port)
addr := fmt.Sprintf(":%d", h.configuration.port)
logging.Log.Debug("starting websocket server on", addr)

h.httpServer = &http.Server{
Addr: addr,
Handler: h,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{h.serviceDescription.certificate},
Certificates: []tls.Certificate{h.configuration.certificate},
ClientAuth: tls.RequireAnyClientCert, // SHIP 9: Client authentication is required
CipherSuites: ciperSuites,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
Expand Down Expand Up @@ -372,7 +372,7 @@ func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{h.serviceDescription.certificate},
Certificates: []tls.Certificate{h.configuration.certificate},
InsecureSkipVerify: true,
CipherSuites: ciperSuites,
},
Expand Down
40 changes: 20 additions & 20 deletions service/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type MdnsSearch interface {
}

type mdns struct {
serviceDescription *ServiceDescription
ski string
configuration *Configuration
ski string

isAnnounced bool
isSearchingServices bool
Expand All @@ -58,12 +58,12 @@ type mdns struct {
mux sync.Mutex
}

func newMDNS(ski string, serviceDescription *ServiceDescription) (*mdns, error) {
func newMDNS(ski string, configuration *Configuration) (*mdns, error) {
m := &mdns{
ski: ski,
serviceDescription: serviceDescription,
entries: make(map[string]MdnsEntry),
cancelChan: make(chan bool),
ski: ski,
configuration: configuration,
entries: make(map[string]MdnsEntry),
cancelChan: make(chan bool),
}

if av, err := m.setupAvahi(); err == nil {
Expand Down Expand Up @@ -112,10 +112,10 @@ func (m *mdns) interfaces() ([]net.Interface, []int32, error) {
var ifaces []net.Interface
var ifaceIndexes []int32

if len(m.serviceDescription.interfaces) > 0 {
ifaces = make([]net.Interface, len(m.serviceDescription.interfaces))
ifaceIndexes = make([]int32, len(m.serviceDescription.interfaces))
for i, ifaceName := range m.serviceDescription.interfaces {
if len(m.configuration.interfaces) > 0 {
ifaces = make([]net.Interface, len(m.configuration.interfaces))
ifaceIndexes = make([]int32, len(m.configuration.interfaces))
for i, ifaceName := range m.configuration.interfaces {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -146,26 +146,26 @@ func (m *mdns) Announce() error {
return err
}

serviceIdentifier := m.serviceDescription.Identifier()
serviceIdentifier := m.configuration.Identifier()

txt := []string{ // SHIP 7.3.2
"txtvers=1",
"path=" + shipWebsocketPath,
"id=" + serviceIdentifier,
"ski=" + m.ski,
"brand=" + m.serviceDescription.deviceBrand,
"model=" + m.serviceDescription.deviceModel,
"type=" + string(m.serviceDescription.deviceType),
"register=" + fmt.Sprintf("%v", m.serviceDescription.registerAutoAccept),
"brand=" + m.configuration.deviceBrand,
"model=" + m.configuration.deviceModel,
"type=" + string(m.configuration.deviceType),
"register=" + fmt.Sprintf("%v", m.configuration.registerAutoAccept),
}

logging.Log.Debug("mdns: announce")

serviceName := m.serviceDescription.MdnsServiceName()
serviceName := m.configuration.MdnsServiceName()

if m.av == nil {
// use Zeroconf library if avahi is not available
mDNSServer, err := zeroconf.Register(serviceName, shipZeroConfServiceType, shipZeroConfDomain, m.serviceDescription.port, txt, ifaces)
mDNSServer, err := zeroconf.Register(serviceName, shipZeroConfServiceType, shipZeroConfDomain, m.configuration.port, txt, ifaces)
if err == nil {
m.zc = mDNSServer

Expand All @@ -188,7 +188,7 @@ func (m *mdns) Announce() error {
}

for _, iface := range ifaceIndexes {
err = entryGroup.AddService(iface, avahi.ProtoUnspec, 0, serviceName, shipZeroConfServiceType, shipZeroConfDomain, "", uint16(m.serviceDescription.port), btxt)
err = entryGroup.AddService(iface, avahi.ProtoUnspec, 0, serviceName, shipZeroConfServiceType, shipZeroConfDomain, "", uint16(m.configuration.port), btxt)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (m *mdns) resolveEntries() {
case <-m.cancelChan:
ctx.Done()
case service := <-zcEntries:
// Zeroconf has issues with merging mDNS data and sometimes reports non complety records
// Zeroconf has issues with merging mDNS data and sometimes reports incomplete records
if len(service.Text) == 0 {
continue
}
Expand Down
16 changes: 8 additions & 8 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type EEBUSServiceHandler interface {
// A service is the central element of an EEBUS service
// including its websocket server and a zeroconf service.
type EEBUSService struct {
ServiceDescription *ServiceDescription
Configuration *Configuration

// The local service details
LocalService *ServiceDetails
Expand All @@ -46,10 +46,10 @@ type EEBUSService struct {
}

// creates a new EEBUS service
func NewEEBUSService(ServiceDescription *ServiceDescription, serviceHandler EEBUSServiceHandler) *EEBUSService {
func NewEEBUSService(configuration *Configuration, serviceHandler EEBUSServiceHandler) *EEBUSService {
return &EEBUSService{
ServiceDescription: ServiceDescription,
serviceHandler: serviceHandler,
Configuration: configuration,
serviceHandler: serviceHandler,
}
}

Expand Down Expand Up @@ -81,11 +81,11 @@ func (s *EEBUSService) SetLogging(logger logging.Logging) {

// Starts the service by initializeing mDNS and the server.
func (s *EEBUSService) Setup() error {
if s.ServiceDescription.port == 0 {
s.ServiceDescription.port = defaultPort
if s.Configuration.port == 0 {
s.Configuration.port = defaultPort
}

sd := s.ServiceDescription
sd := s.Configuration

leaf, err := x509.ParseCertificate(sd.certificate.Certificate[0])
if err != nil {
Expand Down Expand Up @@ -155,7 +155,7 @@ func (s *EEBUSService) Setup() error {
s.spineLocalDevice.AddEntity(entity)

// Setup connections hub with mDNS and websocket connection handling
hub, err := newConnectionsHub(s, s.spineLocalDevice, s.ServiceDescription, s.LocalService)
hub, err := newConnectionsHub(s, s.spineLocalDevice, s.Configuration, s.LocalService)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c379d19

Please sign in to comment.