Skip to content

Commit

Permalink
Chnages for v1.0.5 -> Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NamidM committed May 31, 2022
1 parent 5e40be1 commit 5af6cca
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 119 deletions.
10 changes: 5 additions & 5 deletions eebus.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package go_eebus

import (
"github.com/LMF-DHBW/go-eebus/ressources"
"github.com/LMF-DHBW/go-eebus/resources"
"github.com/LMF-DHBW/go-eebus/spine"
)

type EebusNode struct {
isGateway bool
SpineNode *spine.SpineNode
DeviceStructure *ressources.DeviceModel
DeviceStructure *resources.DeviceModel
Update Updater
}

type Updater func(ressources.DatagramType, spine.SpineConnection)
type Updater func(resources.DatagramType, spine.SpineConnection)

func NewEebusNode(hostname string, isGateway bool, certName string, devId string, brand string, devType string) *EebusNode {
deviceModel := &ressources.DeviceModel{}
deviceModel := &resources.DeviceModel{}
newEebusNode := &EebusNode{isGateway, nil, deviceModel, nil}
newEebusNode.SpineNode = spine.NewSpineNode(hostname, isGateway, deviceModel, newEebusNode.SubscriptionNofity, certName, devId, brand, devType)
return newEebusNode
}

func (eebusNode *EebusNode) SubscriptionNofity(datagram ressources.DatagramType, conn spine.SpineConnection) {
func (eebusNode *EebusNode) SubscriptionNofity(datagram resources.DatagramType, conn spine.SpineConnection) {
if eebusNode.Update != nil {
eebusNode.Update(datagram, conn)
}
Expand Down
4 changes: 2 additions & 2 deletions ressources/actuatorswitch.go → resources/actuatorswitch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

type Notifier func(string, string, FeatureAddressType)

Expand All @@ -11,7 +11,7 @@ type DescriptionElement struct {
Description string `xml:"description"`
}

func ActuatorSwitch(role string, label string, description string, ChangeNotify Notifier) []*FunctionModel {
func ActuatorSwitch(label string, description string, ChangeNotify Notifier) []*FunctionModel {
return []*FunctionModel{
{
FunctionName: "actuatorSwitchData",
Expand Down
2 changes: 1 addition & 1 deletion ressources/bindsubscribe.go → resources/bindsubscribe.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

type NodeManagementBindingData struct {
BindingEntries []*BindSubscribeEntry `xml:"bindingEntries"`
Expand Down
2 changes: 1 addition & 1 deletion ressources/datagram.go → resources/datagram.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

const SPECIFICATION_VERSION = "1.0.0"

Expand Down
2 changes: 1 addition & 1 deletion ressources/discovery.go → resources/discovery.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

type NodeManagementDetailedDiscovery struct {
SpecificationVersionList []*NodeManagementSpecificationVersionListType `xml:"specificationVersionList"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

import (
"encoding/xml"
Expand Down
2 changes: 1 addition & 1 deletion ressources/measurement.go → resources/measurement.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ressources
package resources

type TimePeriodType struct {
StartTime string `xml:"startTime"`
Expand Down
14 changes: 7 additions & 7 deletions ship/comissioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"strings"

"github.com/LMF-DHBW/go-eebus/ressources"
"github.com/LMF-DHBW/go-eebus/resources"
)

func ReadSkis() ([]string, []string) {
Expand All @@ -21,7 +21,7 @@ func ReadSkis() ([]string, []string) {
}

file, err := os.Open("skis.txt")
ressources.CheckError(err)
resources.CheckError(err)
defer file.Close()

var skis []string
Expand All @@ -35,13 +35,13 @@ func ReadSkis() ([]string, []string) {
devices = append(devices, line[1])
}
}
ressources.CheckError(scanner.Err())
resources.CheckError(scanner.Err())
return skis, devices
}

func WriteSkis(newSkis []string, newDevices []string) {
file, err := os.OpenFile("skis.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
ressources.CheckError(err)
resources.CheckError(err)
defer file.Close()
if len(newSkis) == len(newDevices) {
result := ""
Expand All @@ -56,7 +56,7 @@ func WriteSkis(newSkis []string, newDevices []string) {
}

_, err = file.WriteString(result)
ressources.CheckError(err)
resources.CheckError(err)
}

}
Expand All @@ -66,7 +66,7 @@ func (shipNode *ShipNode) getSki() string {
var err error

file, err = os.ReadFile(shipNode.CertName + ".crt")
ressources.CheckError(err)
resources.CheckError(err)

crt := string(file)

Expand All @@ -76,7 +76,7 @@ func (shipNode *ShipNode) getSki() string {
pubkey := cert.PublicKey.(*rsa.PublicKey)

publicKey, err := x509.MarshalPKIXPublicKey(pubkey)
ressources.CheckError(err)
resources.CheckError(err)

hasher := sha1.New()
hasher.Write(publicKey)
Expand Down
8 changes: 4 additions & 4 deletions ship/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"time"

"github.com/LMF-DHBW/go-eebus/ressources"
"github.com/LMF-DHBW/go-eebus/resources"

"github.com/grandcat/zeroconf"
)
Expand All @@ -15,7 +15,7 @@ func (shipNode *ShipNode) BrowseDns() {
log.Println("Browsing for entries")
// Discover all ship services on the network
resolver, err := zeroconf.NewResolver(nil)
ressources.CheckError(err)
resources.CheckError(err)

entries := make(chan *zeroconf.ServiceEntry)
go func(results <-chan *zeroconf.ServiceEntry) {
Expand All @@ -27,7 +27,7 @@ func (shipNode *ShipNode) BrowseDns() {
ctx, _ := context.WithCancel(context.Background())

err = resolver.Browse(ctx, "_ship._tcp", "local.", entries)
ressources.CheckError(err)
resources.CheckError(err)

<-ctx.Done()
}
Expand All @@ -40,7 +40,7 @@ func (shipNode *ShipNode) RegisterDns() {
txtRecord := []string{"txtvers=1", "id=" + id, "path=wss://" + shipNode.hostname + ":" + port, "SKI=" + shipNode.getSki(), "register=true", "brand=" + shipNode.brand, "type=" + shipNode.devType}
log.Println("Registering: ", txtRecord)
server, err := zeroconf.Register("Device "+port, "_ship._tcp", "local.", shipNode.serverPort, txtRecord, nil)
ressources.CheckError(err)
resources.CheckError(err)

defer server.Shutdown()
defer log.Println("Registering stopped")
Expand Down
20 changes: 10 additions & 10 deletions ship/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"time"

"github.com/LMF-DHBW/go-eebus/ressources"
"github.com/LMF-DHBW/go-eebus/resources"

"golang.org/x/net/websocket"
)
Expand All @@ -30,8 +30,8 @@ type Message struct {
}

type DataValue struct {
Header HeaderType `xml:"header"`
Payload ressources.DatagramType `xml:"payload"`
Header HeaderType `xml:"header"`
Payload resources.DatagramType `xml:"payload"`
}

type HeaderType struct {
Expand Down Expand Up @@ -64,23 +64,23 @@ func (SME *SMEInstance) StartCMI() {
}
SME.connectionState = "CMI_STATE_SERVER_EVALUATE"
Message := CmiMessage{}
ressources.CheckError(json.Unmarshal(msg, &Message))
resources.CheckError(json.Unmarshal(msg, &Message))
if Message.MessageType != 0 || Message.MessageValue != 0 {
defer SME.Connection.Close()
}
bytes, err := json.Marshal(CmiMessage{
MessageType: 0,
MessageValue: 0,
})
ressources.CheckError(err)
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
} else {
SME.connectionState = "CMI_STATE_CLIENT_SEND"
bytes, err := json.Marshal(CmiMessage{
MessageType: 0,
MessageValue: 0,
})
ressources.CheckError(err)
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
SME.connectionState = "CMI_STATE_CLIENT_WAIT"
msg := SME.RecieveTimeout(CMI_TIMEOUT)
Expand All @@ -90,7 +90,7 @@ func (SME *SMEInstance) StartCMI() {
}
SME.connectionState = "CMI_STATE_CLIENT_EVALUATE"
Message := CmiMessage{}
ressources.CheckError(json.Unmarshal(msg, &Message))
resources.CheckError(json.Unmarshal(msg, &Message))
if Message.MessageType != 0 || Message.MessageValue != 0 {
SME.Connection.Close()
}
Expand Down Expand Up @@ -129,15 +129,15 @@ func (SME *SMEInstance) Recieve(handleFunc dataHandler) {
break
}
Message := Message{}
ressources.CheckError(xml.Unmarshal(msg, &Message))
resources.CheckError(xml.Unmarshal(msg, &Message))
if Message.MessageType == 2 {
handleFunc(Message.MessageValue.Payload)
}
}
}

/* Sends messages in json format */
func (SME *SMEInstance) Send(payload ressources.DatagramType) {
func (SME *SMEInstance) Send(payload resources.DatagramType) {
bytes, err := xml.Marshal(Message{
MessageType: 2,
MessageValue: DataValue{
Expand All @@ -147,6 +147,6 @@ func (SME *SMEInstance) Send(payload ressources.DatagramType) {
Payload: payload,
},
})
ressources.CheckError(err)
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
}
18 changes: 9 additions & 9 deletions ship/ship.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

"github.com/LMF-DHBW/go-eebus/ressources"
"github.com/LMF-DHBW/go-eebus/resources"

"github.com/grandcat/zeroconf"
"github.com/phayes/freeport"
Expand All @@ -20,7 +20,7 @@ type ConnectionManager func(string, *websocket.Conn)
type ConnectionManagerSpine func(*SMEInstance, string)
type CloseHandler func(*SMEInstance)
type handler func([]byte)
type dataHandler func(ressources.DatagramType)
type dataHandler func(resources.DatagramType)

type ShipNode struct {
serverPort int
Expand Down Expand Up @@ -50,7 +50,7 @@ func NewShipNode(hostname string, IsGateway bool, certName string, devId string,
func (shipNode *ShipNode) Start() {
// ShipNode start -> assign port, create server
port, err := freeport.GetFreePort()
ressources.CheckError(err)
resources.CheckError(err)
shipNode.serverPort = port
// Start server, Register Dns and search for other DNS entries
if !shipNode.IsGateway {
Expand All @@ -67,7 +67,7 @@ func (shipNode *ShipNode) handleFoundService(entry *zeroconf.ServiceEntry) {
log.Println("Found new service", entry.HostName, entry.Port)

skis, _ := ReadSkis()
if ressources.StringInSlice(strings.Split(entry.Text[3], "=")[1], skis) {
if resources.StringInSlice(strings.Split(entry.Text[3], "=")[1], skis) {
// Device is trusted
go shipNode.Connect(strings.Split(entry.Text[2], "=")[1], strings.Split(entry.Text[3], "=")[1])
} else {
Expand All @@ -91,7 +91,7 @@ func (shipNode *ShipNode) handleFoundService(entry *zeroconf.ServiceEntry) {
func (shipNode *ShipNode) newConnection(role string, conn *websocket.Conn, ski string) {
skiIsNew := ""
skis, _ := ReadSkis()
if !ressources.StringInSlice(ski, skis) && shipNode.IsGateway {
if !resources.StringInSlice(ski, skis) && shipNode.IsGateway {
skiIsNew = ski
}

Expand All @@ -112,18 +112,18 @@ func (shipNode *ShipNode) newConnection(role string, conn *websocket.Conn, ski s

func (shipNode *ShipNode) Connect(service string, ski string) {
conf, err := websocket.NewConfig(service, "http://"+shipNode.hostname)
ressources.CheckError(err)
resources.CheckError(err)

var cert tls.Certificate
cert, err = tls.LoadX509KeyPair(shipNode.CertName+".crt", shipNode.CertName+".key")
ressources.CheckError(err)
resources.CheckError(err)

conf.TlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}

conn, err := websocket.DialConfig(conf)
ressources.CheckError(err)
resources.CheckError(err)

shipNode.newConnection("client", conn, ski)
}
Expand All @@ -144,5 +144,5 @@ func (shipNode *ShipNode) StartServer() {
}),
}
err := server.ListenAndServeTLS(shipNode.CertName+".crt", shipNode.CertName+".key")
ressources.CheckError(err)
resources.CheckError(err)
}
Loading

0 comments on commit 5af6cca

Please sign in to comment.